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.

50 lines
1.5 KiB

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