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