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 "github.com/iden3/go-iden3-crypto/babyjub"
13 "gopkg.in/go-playground/validator.v9"
14)
15
16// Duration is a wrapper type that parses time duration from text.
17type Duration struct {
18 time.Duration `validate:"required"`
19}
20
21// UnmarshalText unmarshalls time duration from text.
22func (d *Duration) UnmarshalText(data []byte) error {
23 duration, err := time.ParseDuration(string(data))
24 if err != nil {
25 return tracerr.Wrap(err)
26 }
27 d.Duration = duration
28 return nil
29}
30
31// ServerProof is the server proof configuration data.
32type ServerProof struct {
33 // URL is the server proof API URL
34 URL string `validate:"required"`
35}
36
37// Coordinator is the coordinator specific configuration.
38type Coordinator struct {
39 // ForgerAddress is the address under which this coordinator is forging
40 ForgerAddress ethCommon.Address `validate:"required"`
41 // FeeAccount is the Hermez account that the coordinator uses to receive fees
42 FeeAccount struct {
43 // Address is the ethereum address of the account to receive fees
44 Address ethCommon.Address `validate:"required"`
45 // BJJ is the baby jub jub public key of the account to receive fees
46 BJJ babyjub.PublicKeyComp `validate:"required"`
47 } `validate:"required"`
48 // ConfirmBlocks is the number of confirmation blocks to wait for sent
49 // ethereum transactions before forgetting about them
50 ConfirmBlocks int64 `validate:"required"`
51 // L1BatchTimeoutPerc is the portion of the range before the L1Batch
52 // timeout that will trigger a schedule to forge an L1Batch
53 L1BatchTimeoutPerc float64 `validate:"required"`
54 // ProofServerPollInterval is the waiting interval between polling the
55 // ProofServer while waiting for a particular status
56 ProofServerPollInterval Duration `validate:"required"`
57 // SyncRetryInterval is the waiting interval between calls to the main
58 // handler of a synced block after an error
59 SyncRetryInterval Duration `validate:"required"`
60 // L2DB is the DB that holds the pool of L2Txs
61 L2DB struct {
62 // SafetyPeriod is the number of batches after which
63 // non-pending L2Txs are deleted from the pool
64 SafetyPeriod common.BatchNum `validate:"required"`
65 // MaxTxs is the number of L2Txs that once reached triggers
66 // deletion of old L2Txs
67 MaxTxs uint32 `validate:"required"`
68 // TTL is the Time To Live for L2Txs in the pool. Once MaxTxs
69 // L2Txs is reached, L2Txs older than TTL will be deleted.
70 TTL Duration `validate:"required"`
71 // PurgeBatchDelay is the delay between batches to purge outdated transactions
72 PurgeBatchDelay int64 `validate:"required"`
73 // InvalidateBatchDelay is the delay between batches to mark invalid transactions
74 InvalidateBatchDelay int64 `validate:"required"`
75 // PurgeBlockDelay is the delay between blocks to purge outdated transactions
76 PurgeBlockDelay int64 `validate:"required"`
77 // InvalidateBlockDelay is the delay between blocks to mark invalid transactions
78 InvalidateBlockDelay int64 `validate:"required"`
79 } `validate:"required"`
80 TxSelector struct {
81 // Path where the TxSelector StateDB is stored
82 Path string `validate:"required"`
83 } `validate:"required"`
84 BatchBuilder struct {
85 // Path where the BatchBuilder StateDB is stored
86 Path string `validate:"required"`
87 } `validate:"required"`
88 ServerProofs []ServerProof `validate:"required"`
89 Circuit struct {
90 // VerifierIdx uint8 `validate:"required"`
91 // MaxTx is the maximum number of txs supported by the circuit
92 MaxTx int64 `validate:"required"`
93 // NLevels is the maximum number of merkle tree levels
94 // supported by the circuit
95 NLevels int64 `validate:"required"`
96 } `validate:"required"`
97 EthClient struct {
98 // CallGasLimit is the default gas limit set for ethereum
99 // calls, except for methods where a particular gas limit is
100 // harcoded because it's known to be a big value
101 CallGasLimit uint64 `validate:"required"`
102 // GasPriceDiv is the gas price division
103 GasPriceDiv uint64 `validate:"required"`
104 // CheckLoopInterval is the waiting interval between receipt
105 // checks of ethereum transactions in the TxManager
106 CheckLoopInterval Duration `validate:"required"`
107 // Attempts is the number of attempts to do an eth client RPC
108 // call before giving up
109 Attempts int `validate:"required"`
110 // AttemptsDelay is delay between attempts do do an eth client
111 // RPC call
112 AttemptsDelay Duration `validate:"required"`
113 // Keystore is the ethereum keystore where private keys are kept
114 Keystore struct {
115 // Path to the keystore
116 Path string `validate:"required"`
117 // Password used to decrypt the keys in the keystore
118 Password string `validate:"required"`
119 } `validate:"required"`
120 } `validate:"required"`
121 API struct {
122 // Coordinator enables the coordinator API endpoints
123 Coordinator bool
124 } `validate:"required"`
125 Debug struct {
126 // BatchPath if set, specifies the path where batchInfo is stored
127 // in JSON in every step/update of the pipeline
128 BatchPath string
129 // LightScrypt if set, uses light parameters for the ethereum
130 // keystore encryption algorithm.
131 LightScrypt bool
132 }
133}
134
135// Node is the hermez node configuration.
136type Node struct {
137 PriceUpdater struct {
138 // Interval between price updater calls
139 Interval Duration `valudate:"required"`
140 // URL of the token prices provider
141 URL string `valudate:"required"`
142 // Type of the API of the token prices provider
143 Type string `valudate:"required"`
144 } `validate:"required"`
145 StateDB struct {
146 // Path where the synchronizer StateDB is stored
147 Path string `validate:"required"`
148 // Keep is the number of checkpoints to keep
149 Keep int `validate:"required"`
150 } `validate:"required"`
151 PostgreSQL struct {
152 // Port of the PostgreSQL server
153 Port int `validate:"required"`
154 // Host of the PostgreSQL server
155 Host string `validate:"required"`
156 // User of the PostgreSQL server
157 User string `validate:"required"`
158 // Password of the PostgreSQL server
159 Password string `validate:"required"`
160 // Name of the PostgreSQL server database
161 Name string `validate:"required"`
162 } `validate:"required"`
163 Web3 struct {
164 // URL is the URL of the web3 ethereum-node RPC server
165 URL string `validate:"required"`
166 } `validate:"required"`
167 Synchronizer struct {
168 // SyncLoopInterval is the interval between attempts to
169 // synchronize a new block from an ethereum node
170 SyncLoopInterval Duration `validate:"required"`
171 // StatsRefreshPeriod is the interval between updates of the
172 // synchronizer state Eth parameters (`Eth.LastBlock` and
173 // `Eth.LastBatch`). This value only affects the reported % of
174 // synchronization of blocks and batches, nothing else.
175 StatsRefreshPeriod Duration `validate:"required"`
176 } `validate:"required"`
177 SmartContracts struct {
178 // Rollup is the address of the Hermez.sol smart contract
179 Rollup ethCommon.Address `validate:"required"`
180 // Rollup is the address of the HermezAuctionProtocol.sol smart
181 // contract
182 Auction ethCommon.Address `validate:"required"`
183 // WDelayer is the address of the WithdrawalDelayer.sol smart
184 // contract
185 WDelayer ethCommon.Address `validate:"required"`
186 // TokenHEZ is the address of the HEZTokenFull.sol smart
187 // contract
188 TokenHEZ ethCommon.Address `validate:"required"`
189 // TokenHEZName is the name of the HEZ token deployed at
190 // TokenHEZ address
191 TokenHEZName string `validate:"required"`
192 } `validate:"required"`
193 API struct {
194 // Address where the API will listen if set
195 Address string
196 // Explorer enables the Explorer API endpoints
197 Explorer bool
198 // UpdateMetricsInterval is the interval between updates of the
199 // API metrics
200 UpdateMetricsInterval Duration
201 // UpdateMetricsInterval is the interval between updates of the
202 // recommended fees
203 UpdateRecommendedFeeInterval Duration
204 } `validate:"required"`
205 Debug struct {
206 // APIAddress is the address where the debugAPI will listen if
207 // set
208 APIAddress string
209 // MeddlerLogs enables meddler debug mode, where unused columns and struct
210 // fields will be logged
211 MeddlerLogs bool
212 }
213 Coordinator Coordinator `validate:"-"`
214}
215
216// Load loads a generic config.
217func Load(path string, cfg interface{}) error {
218 bs, err := ioutil.ReadFile(path) //nolint:gosec
219 if err != nil {
220 return tracerr.Wrap(err)
221 }
222 cfgToml := string(bs)
223 if _, err := toml.Decode(cfgToml, cfg); err != nil {
224 return tracerr.Wrap(err)
225 }
226 return nil
227}
228
229// LoadCoordinator loads the Coordinator configuration from path.
230func LoadCoordinator(path string) (*Node, error) {
231 var cfg Node
232 if err := Load(path, &cfg); err != nil {
233 return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
234 }
235 validate := validator.New()
236 if err := validate.Struct(cfg); err != nil {
237 return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
238 }
239 if err := validate.Struct(cfg.Coordinator); err != nil {
240 return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
241 }
242 return &cfg, nil
243}
244
245// LoadNode loads the Node configuration from path.
246func LoadNode(path string) (*Node, error) {
247 var cfg Node
248 if err := Load(path, &cfg); err != nil {
249 return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
250 }
251 validate := validator.New()
252 if err := validate.Struct(cfg); err != nil {
253 return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
254 }
255 return &cfg, nil
256}