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