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.

105 lines
2.7 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. SafetyPeriod common.BatchNum `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. Name string `validate:"required"`
  56. } `validate:"required"`
  57. Web3 struct {
  58. URL string `validate:"required"`
  59. } `validate:"required"`
  60. Synchronizer struct {
  61. SyncLoopInterval Duration `validate:"required"`
  62. } `validate:"required"`
  63. }
  64. // Load loads a generic config.
  65. func Load(path string, cfg interface{}) error {
  66. bs, err := ioutil.ReadFile(path) //nolint:gosec
  67. if err != nil {
  68. return err
  69. }
  70. cfgToml := string(bs)
  71. if _, err := toml.Decode(cfgToml, cfg); err != nil {
  72. return err
  73. }
  74. validate := validator.New()
  75. if err := validate.Struct(cfg); err != nil {
  76. return fmt.Errorf("error validating configuration file: %w", err)
  77. }
  78. return nil
  79. }
  80. // LoadCoordinator loads the Coordinator configuration from path.
  81. func LoadCoordinator(path string) (*Coordinator, error) {
  82. var cfg Coordinator
  83. if err := Load(path, &cfg); err != nil {
  84. return nil, fmt.Errorf("error loading coordinator configuration file: %w", err)
  85. }
  86. return &cfg, nil
  87. }
  88. // LoadNode loads the Node configuration from path.
  89. func LoadNode(path string) (*Node, error) {
  90. var cfg Node
  91. if err := Load(path, &cfg); err != nil {
  92. return nil, fmt.Errorf("error loading node configuration file: %w", err)
  93. }
  94. return &cfg, nil
  95. }