You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

  1. //nolint: goconst
  2. package main
  3. import (
  4. "errors"
  5. "fmt"
  6. "github.com/BurntSushi/toml"
  7. )
  8. // Config is the application configuration.
  9. type Config struct {
  10. ChainID string `toml:"chain_id"`
  11. Listen string
  12. Protocol string
  13. Dir string
  14. PersistInterval uint64 `toml:"persist_interval"`
  15. SnapshotInterval uint64 `toml:"snapshot_interval"`
  16. RetainBlocks uint64 `toml:"retain_blocks"`
  17. ValidatorUpdates map[string]map[string]uint8 `toml:"validator_update"`
  18. PrivValServer string `toml:"privval_server"`
  19. PrivValKey string `toml:"privval_key"`
  20. PrivValState string `toml:"privval_state"`
  21. Misbehaviors map[string]string `toml:"misbehaviors"`
  22. }
  23. // LoadConfig loads the configuration from disk.
  24. func LoadConfig(file string) (*Config, error) {
  25. cfg := &Config{
  26. Listen: "unix:///var/run/app.sock",
  27. Protocol: "socket",
  28. PersistInterval: 1,
  29. }
  30. _, err := toml.DecodeFile(file, &cfg)
  31. if err != nil {
  32. return nil, fmt.Errorf("failed to load config from %q: %w", file, err)
  33. }
  34. return cfg, cfg.Validate()
  35. }
  36. // Validate validates the configuration. We don't do exhaustive config
  37. // validation here, instead relying on Testnet.Validate() to handle it.
  38. func (cfg Config) Validate() error {
  39. switch {
  40. case cfg.ChainID == "":
  41. return errors.New("chain_id parameter is required")
  42. case cfg.Listen == "" && cfg.Protocol != "builtin":
  43. return errors.New("listen parameter is required")
  44. default:
  45. return nil
  46. }
  47. }