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