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
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 ethCommon.Address `validate:"required"`
39 ForgeLoopInterval Duration `validate:"required"`
40 ConfirmBlocks int64 `validate:"required"`
41 L2DB struct {
42 SafetyPeriod common.BatchNum `validate:"required"`
43 MaxTxs uint32 `validate:"required"`
44 TTL Duration `validate:"required"`
45 } `validate:"required"`
46 TxSelector struct {
47 Path string `validate:"required"`
48 } `validate:"required"`
49 BatchBuilder struct {
50 Path string `validate:"required"`
51 } `validate:"required"`
52 ServerProofs []ServerProof `validate:"required"`
53 EthClient struct {
54 CallGasLimit uint64 `validate:"required"`
55 DeployGasLimit uint64 `validate:"required"`
56 GasPriceDiv uint64 `validate:"required"`
57 ReceiptTimeout Duration `validate:"required"`
58 IntervalReceiptLoop Duration `validate:"required"`
59 } `validate:"required"`
60 API struct {
61 Coordinator bool
62 } `validate:"required"`
63}
64
65// Node is the hermez node configuration.
66type Node struct {
67 StateDB struct {
68 Path string
69 } `validate:"required"`
70 PostgreSQL struct {
71 Port int `validate:"required"`
72 Host string `validate:"required"`
73 User string `validate:"required"`
74 Password string `validate:"required"`
75 Name string `validate:"required"`
76 } `validate:"required"`
77 Web3 struct {
78 URL string `validate:"required"`
79 } `validate:"required"`
80 Synchronizer struct {
81 SyncLoopInterval Duration `validate:"required"`
82 StatsRefreshPeriod Duration `validate:"required"`
83 StartBlockNum synchronizer.ConfigStartBlockNum `validate:"required"`
84 InitialVariables synchronizer.SCVariables `validate:"required"`
85 } `validate:"required"`
86 SmartContracts struct {
87 Rollup ethCommon.Address `validate:"required"`
88 Auction ethCommon.Address `validate:"required"`
89 WDelayer ethCommon.Address `validate:"required"`
90 TokenHEZ ethCommon.Address `validate:"required"`
91 TokenHEZName string `validate:"required"`
92 } `validate:"required"`
93 API struct {
94 Address string
95 Explorer bool
96 UpdateMetricsInterval Duration
97 UpdateRecommendedFeeInterval Duration
98 } `validate:"required"`
99 Debug struct {
100 APIAddress string
101 }
102}
103
104// Load loads a generic config.
105func Load(path string, cfg interface{}) error {
106 bs, err := ioutil.ReadFile(path) //nolint:gosec
107 if err != nil {
108 return tracerr.Wrap(err)
109 }
110 cfgToml := string(bs)
111 if _, err := toml.Decode(cfgToml, cfg); err != nil {
112 return tracerr.Wrap(err)
113 }
114 validate := validator.New()
115 if err := validate.Struct(cfg); err != nil {
116 return tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
117 }
118 return nil
119}
120
121// LoadCoordinator loads the Coordinator configuration from path.
122func LoadCoordinator(path string) (*Coordinator, error) {
123 var cfg Coordinator
124 if err := Load(path, &cfg); err != nil {
125 return nil, tracerr.Wrap(fmt.Errorf("error loading coordinator configuration file: %w", err))
126 }
127 return &cfg, nil
128}
129
130// LoadNode loads the Node configuration from path.
131func LoadNode(path string) (*Node, error) {
132 var cfg Node
133 if err := Load(path, &cfg); err != nil {
134 return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
135 }
136 return &cfg, nil
137}