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.

107 lines
2.8 KiB

  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "time"
  6. "github.com/BurntSushi/toml"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "gopkg.in/go-playground/validator.v9"
  9. )
  10. // Duration is a wrapper type that parses time duration from text.
  11. type Duration struct {
  12. time.Duration
  13. }
  14. // UnmarshalText unmarshalls time duration from text.
  15. func (d *Duration) UnmarshalText(data []byte) error {
  16. duration, err := time.ParseDuration(string(data))
  17. if err != nil {
  18. return err
  19. }
  20. d.Duration = duration
  21. return nil
  22. }
  23. // ServerProof is the server proof configuration data.
  24. type ServerProof struct {
  25. URL string `validate:"required"`
  26. }
  27. // Coordinator is the coordinator specific configuration.
  28. type Coordinator struct {
  29. ForgerAddress ethCommon.Address `validate:"required"`
  30. ForgeLoopInterval Duration `validate:"required"`
  31. L2DB struct {
  32. Name string `validate:"required"`
  33. SafetyPeriod uint16 `validate:"required"`
  34. MaxTxs uint32 `validate:"required"`
  35. TTL Duration `validate:"required"`
  36. } `validate:"required"`
  37. TxSelector struct {
  38. Path string `validate:"required"`
  39. } `validate:"required"`
  40. BatchBuilder struct {
  41. Path string `validate:"required"`
  42. } `validate:"required"`
  43. ServerProofs []ServerProof `validate:"required"`
  44. }
  45. // Node is the hermez node configuration.
  46. type Node struct {
  47. StateDB struct {
  48. Path string
  49. } `validate:"required"`
  50. PostgreSQL struct {
  51. Port int `validate:"required"`
  52. Host string `validate:"required"`
  53. User string `validate:"required"`
  54. Password string `validate:"required"`
  55. } `validate:"required"`
  56. HistoryDB struct {
  57. Name string `validate:"required"`
  58. } `validate:"required"`
  59. Web3 struct {
  60. URL string `validate:"required"`
  61. } `validate:"required"`
  62. Synchronizer struct {
  63. SyncLoopInterval Duration `validate:"required"`
  64. } `validate:"required"`
  65. }
  66. // Load loads a generic config.
  67. func Load(path string, cfg interface{}) error {
  68. bs, err := ioutil.ReadFile(path) //nolint:gosec
  69. if err != nil {
  70. return err
  71. }
  72. cfgToml := string(bs)
  73. if _, err := toml.Decode(cfgToml, cfg); err != nil {
  74. return err
  75. }
  76. validate := validator.New()
  77. if err := validate.Struct(cfg); err != nil {
  78. return fmt.Errorf("error validating configuration file: %w", err)
  79. }
  80. return nil
  81. }
  82. // LoadCoordinator loads the Coordinator configuration from path.
  83. func LoadCoordinator(path string) (*Coordinator, error) {
  84. var cfg Coordinator
  85. if err := Load(path, &cfg); err != nil {
  86. return nil, fmt.Errorf("error loading coordinator configuration file: %w", err)
  87. }
  88. return &cfg, nil
  89. }
  90. // LoadNode loads the Node configuration from path.
  91. func LoadNode(path string) (*Node, error) {
  92. var cfg Node
  93. if err := Load(path, &cfg); err != nil {
  94. return nil, fmt.Errorf("error loading node configuration file: %w", err)
  95. }
  96. return &cfg, nil
  97. }