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.

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