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 "github.com/hermeznetwork/hermez-node/common"
11 "github.com/hermeznetwork/tracerr"
12 "gopkg.in/go-playground/validator.v9"
13)
14
15// Duration is a wrapper type that parses time duration from text.
16type Duration struct {
17 time.Duration `validate:"required"`
18}
19
20// UnmarshalText unmarshalls time duration from text.
21func (d *Duration) UnmarshalText(data []byte) error {
22 duration, err := time.ParseDuration(string(data))
23 if err != nil {
24 return tracerr.Wrap(err)
25 }
26 d.Duration = duration
27 return nil
28}
29
30// ServerProof is the server proof configuration data.
31type ServerProof struct {
32 URL string `validate:"required"`
33}
34
35// Coordinator is the coordinator specific configuration.
36type Coordinator struct {
37 // ForgerAddress is the address under which this coordinator is forging
38 ForgerAddress ethCommon.Address `validate:"required"`
39 // ConfirmBlocks is the number of confirmation blocks to wait for sent
40 // ethereum transactions before forgetting about them
41 ConfirmBlocks int64 `validate:"required"`
42 // L1BatchTimeoutPerc is the portion of the range before the L1Batch
43 // timeout that will trigger a schedule to forge an L1Batch
44 L1BatchTimeoutPerc float64 `validate:"required"`
45 // ProofServerPollInterval is the waiting interval between polling the
46 // ProofServer while waiting for a particular status
47 ProofServerPollInterval Duration `validate:"required"`
48 // SyncRetryInterval is the waiting interval between calls to the main
49 // handler of a synced block after an error
50 SyncRetryInterval Duration `validate:"required"`
51 L2DB struct {
52 SafetyPeriod common.BatchNum `validate:"required"`
53 MaxTxs uint32 `validate:"required"`
54 TTL Duration `validate:"required"`
55 // PurgeBatchDelay is the delay between batches to purge outdated transactions
56 PurgeBatchDelay int64 `validate:"required"`
57 // InvalidateBatchDelay is the delay between batches to mark invalid transactions
58 InvalidateBatchDelay int64 `validate:"required"`
59 // PurgeBlockDelay is the delay between blocks to purge outdated transactions
60 PurgeBlockDelay int64 `validate:"required"`
61 // InvalidateBlockDelay is the delay between blocks to mark invalid transactions
62 InvalidateBlockDelay int64 `validate:"required"`
63 } `validate:"required"`
64 TxSelector struct {
65 Path string `validate:"required"`
66 } `validate:"required"`
67 BatchBuilder struct {
68 Path string `validate:"required"`
69 } `validate:"required"`
70 ServerProofs []ServerProof `validate:"required"`
71 Circuit struct {
72 // VerifierIdx uint8 `validate:"required"`
73 MaxTx int64 `validate:"required"`
74 NLevels int64 `validate:"required"`
75 } `validate:"required"`
76 EthClient struct {
77 CallGasLimit uint64 `validate:"required"`
78 DeployGasLimit uint64 `validate:"required"`
79 GasPriceDiv uint64 `validate:"required"`
80 ReceiptTimeout Duration `validate:"required"`
81 ReceiptLoopInterval Duration `validate:"required"`
82 // CheckLoopInterval is the waiting interval between receipt
83 // checks of ethereum transactions in the TxManager
84 CheckLoopInterval Duration `validate:"required"`
85 // Attempts is the number of attempts to do an eth client RPC
86 // call before giving up
87 Attempts int `validate:"required"`
88 // AttemptsDelay is delay between attempts do do an eth client
89 // RPC call
90 AttemptsDelay Duration `validate:"required"`
91 Keystore struct {
92 Path string `validate:"required"`
93 Password string `validate:"required"`
94 } `validate:"required"`
95 } `validate:"required"`
96 API struct {
97 Coordinator bool
98 } `validate:"required"`
99 Debug struct {
100 // BatchPath if set, specifies the path where batchInfo is stored
101 // in JSON in every step/update of the pipeline
102 BatchPath string
103 // LightScrypt if set, uses light parameters for the ethereum
104 // keystore encryption algorithm.
105 LightScrypt bool
106 }
107}
108
109// Node is the hermez node configuration.
110type Node struct {
111 PriceUpdater struct {
112 Interval Duration `valudate:"required"`
113 URL string `valudate:"required"`
114 Type string `valudate:"required"`
115 } `validate:"required"`
116 StateDB struct {
117 Path string `validate:"required"`
118 Keep int `validate:"required"`
119 } `validate:"required"`
120 PostgreSQL struct {
121 Port int `validate:"required"`
122 Host string `validate:"required"`
123 User string `validate:"required"`
124 Password string `validate:"required"`
125 Name string `validate:"required"`
126 } `validate:"required"`
127 Web3 struct {
128 URL string `validate:"required"`
129 } `validate:"required"`
130 Synchronizer struct {
131 SyncLoopInterval Duration `validate:"required"`
132 StatsRefreshPeriod Duration `validate:"required"`
133 } `validate:"required"`
134 SmartContracts struct {
135 Rollup ethCommon.Address `validate:"required"`
136 Auction ethCommon.Address `validate:"required"`
137 WDelayer ethCommon.Address `validate:"required"`
138 TokenHEZ ethCommon.Address `validate:"required"`
139 TokenHEZName string `validate:"required"`
140 } `validate:"required"`
141 API struct {
142 Address string
143 Explorer bool
144 UpdateMetricsInterval Duration
145 UpdateRecommendedFeeInterval Duration
146 } `validate:"required"`
147 Debug struct {
148 APIAddress string
149 }
150 Coordinator Coordinator `validate:"-"`
151}
152
153// Load loads a generic config.
154func Load(path string, cfg interface{}) error {
155 bs, err := ioutil.ReadFile(path) //nolint:gosec
156 if err != nil {
157 return tracerr.Wrap(err)
158 }
159 cfgToml := string(bs)
160 if _, err := toml.Decode(cfgToml, cfg); err != nil {
161 return tracerr.Wrap(err)
162 }
163 return nil
164}
165
166// LoadCoordinator loads the Coordinator configuration from path.
167func LoadCoordinator(path string) (*Node, error) {
168 var cfg Node
169 if err := Load(path, &cfg); err != nil {
170 return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
171 }
172 validate := validator.New()
173 if err := validate.Struct(cfg); err != nil {
174 return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
175 }
176 if err := validate.Struct(cfg.Coordinator); err != nil {
177 return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
178 }
179 return &cfg, nil
180}
181
182// LoadNode loads the Node configuration from path.
183func LoadNode(path string) (*Node, error) {
184 var cfg Node
185 if err := Load(path, &cfg); err != nil {
186 return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
187 }
188 validate := validator.New()
189 if err := validate.Struct(cfg); err != nil {
190 return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
191 }
192 return &cfg, nil
193}