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.

108 lines
2.8 KiB

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