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.

53 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. KeyType string `toml:"key_type"`
  23. }
  24. // LoadConfig loads the configuration from disk.
  25. func LoadConfig(file string) (*Config, error) {
  26. cfg := &Config{
  27. Listen: "unix:///var/run/app.sock",
  28. Protocol: "socket",
  29. PersistInterval: 1,
  30. }
  31. _, err := toml.DecodeFile(file, &cfg)
  32. if err != nil {
  33. return nil, fmt.Errorf("failed to load config from %q: %w", file, err)
  34. }
  35. return cfg, cfg.Validate()
  36. }
  37. // Validate validates the configuration. We don't do exhaustive config
  38. // validation here, instead relying on Testnet.Validate() to handle it.
  39. func (cfg Config) Validate() error {
  40. switch {
  41. case cfg.ChainID == "":
  42. return errors.New("chain_id parameter is required")
  43. case cfg.Listen == "" && cfg.Protocol != "builtin":
  44. return errors.New("listen parameter is required")
  45. default:
  46. return nil
  47. }
  48. }