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 EthClient struct {
72 CallGasLimit uint64 `validate:"required"`
73 DeployGasLimit uint64 `validate:"required"`
74 GasPriceDiv uint64 `validate:"required"`
75 ReceiptTimeout Duration `validate:"required"`
76 ReceiptLoopInterval Duration `validate:"required"`
77 // CheckLoopInterval is the waiting interval between receipt
78 // checks of ethereum transactions in the TxManager
79 CheckLoopInterval Duration `validate:"required"`
80 // Attempts is the number of attempts to do an eth client RPC
81 // call before giving up
82 Attempts int `validate:"required"`
83 // AttemptsDelay is delay between attempts do do an eth client
84 // RPC call
85 AttemptsDelay Duration `validate:"required"`
86 Keystore struct {
87 Path string `validate:"required"`
88 Password string `validate:"required"`
89 } `validate:"required"`
90 } `validate:"required"`
91 API struct {
92 Coordinator bool
93 } `validate:"required"`
94 Debug struct {
95 // BatchPath if set, specifies the path where batchInfo is stored
96 // in JSON in every step/update of the pipeline
97 BatchPath string
98 // LightScrypt if set, uses light parameters for the ethereum
99 // keystore encryption algorithm.
100 LightScrypt bool
101 }
102}
103
104// Node is the hermez node configuration.
105type Node struct {
106 PriceUpdater struct {
107 Interval Duration `valudate:"required"`
108 URL string `valudate:"required"`
109 Type string `valudate:"required"`
110 } `validate:"required"`
111 StateDB struct {
112 Path string `validate:"required"`
113 Keep int `validate:"required"`
114 } `validate:"required"`
115 PostgreSQL struct {
116 Port int `validate:"required"`
117 Host string `validate:"required"`
118 User string `validate:"required"`
119 Password string `validate:"required"`
120 Name string `validate:"required"`
121 } `validate:"required"`
122 Web3 struct {
123 URL string `validate:"required"`
124 } `validate:"required"`
125 Synchronizer struct {
126 SyncLoopInterval Duration `validate:"required"`
127 StatsRefreshPeriod Duration `validate:"required"`
128 } `validate:"required"`
129 SmartContracts struct {
130 Rollup ethCommon.Address `validate:"required"`
131 Auction ethCommon.Address `validate:"required"`
132 WDelayer ethCommon.Address `validate:"required"`
133 TokenHEZ ethCommon.Address `validate:"required"`
134 TokenHEZName string `validate:"required"`
135 } `validate:"required"`
136 API struct {
137 Address string
138 Explorer bool
139 UpdateMetricsInterval Duration
140 UpdateRecommendedFeeInterval Duration
141 } `validate:"required"`
142 Debug struct {
143 APIAddress string
144 }
145 Coordinator Coordinator `validate:"-"`
146}
147
148// Load loads a generic config.
149func Load(path string, cfg interface{}) error {
150 bs, err := ioutil.ReadFile(path) //nolint:gosec
151 if err != nil {
152 return tracerr.Wrap(err)
153 }
154 cfgToml := string(bs)
155 if _, err := toml.Decode(cfgToml, cfg); err != nil {
156 return tracerr.Wrap(err)
157 }
158 return nil
159}
160
161// LoadCoordinator loads the Coordinator configuration from path.
162func LoadCoordinator(path string) (*Node, error) {
163 var cfg Node
164 if err := Load(path, &cfg); err != nil {
165 return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
166 }
167 validate := validator.New()
168 if err := validate.Struct(cfg); err != nil {
169 return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
170 }
171 if err := validate.Struct(cfg.Coordinator); err != nil {
172 return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
173 }
174 return &cfg, nil
175}
176
177// LoadNode loads the Node configuration from path.
178func LoadNode(path string) (*Node, error) {
179 var cfg Node
180 if err := Load(path, &cfg); err != nil {
181 return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
182 }
183 validate := validator.New()
184 if err := validate.Struct(cfg); err != nil {
185 return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
186 }
187 return &cfg, nil
188}