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.

54 lines
1.7 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. Mode string `toml:"mode"`
  15. PersistInterval uint64 `toml:"persist_interval"`
  16. SnapshotInterval uint64 `toml:"snapshot_interval"`
  17. RetainBlocks uint64 `toml:"retain_blocks"`
  18. ValidatorUpdates map[string]map[string]uint8 `toml:"validator_update"`
  19. PrivValServer string `toml:"privval_server"`
  20. PrivValKey string `toml:"privval_key"`
  21. PrivValState string `toml:"privval_state"`
  22. Misbehaviors map[string]string `toml:"misbehaviors"`
  23. KeyType string `toml:"key_type"`
  24. }
  25. // LoadConfig loads the configuration from disk.
  26. func LoadConfig(file string) (*Config, error) {
  27. cfg := &Config{
  28. Listen: "unix:///var/run/app.sock",
  29. Protocol: "socket",
  30. PersistInterval: 1,
  31. }
  32. _, err := toml.DecodeFile(file, &cfg)
  33. if err != nil {
  34. return nil, fmt.Errorf("failed to load config from %q: %w", file, err)
  35. }
  36. return cfg, cfg.Validate()
  37. }
  38. // Validate validates the configuration. We don't do exhaustive config
  39. // validation here, instead relying on Testnet.Validate() to handle it.
  40. func (cfg Config) Validate() error {
  41. switch {
  42. case cfg.ChainID == "":
  43. return errors.New("chain_id parameter is required")
  44. case cfg.Listen == "" && cfg.Protocol != "builtin":
  45. return errors.New("listen parameter is required")
  46. default:
  47. return nil
  48. }
  49. }