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 "gopkg.in/go-playground/validator.v9"
12)
13
14// Duration is a wrapper type that parses time duration from text.
15type Duration struct {
16 time.Duration
17}
18
19// UnmarshalText unmarshalls time duration from text.
20func (d *Duration) UnmarshalText(data []byte) error {
21 duration, err := time.ParseDuration(string(data))
22 if err != nil {
23 return err
24 }
25 d.Duration = duration
26 return nil
27}
28
29// ServerProof is the server proof configuration data.
30type ServerProof struct {
31 URL string `validate:"required"`
32}
33
34// Coordinator is the coordinator specific configuration.
35type Coordinator struct {
36 ForgerAddress ethCommon.Address `validate:"required"`
37 ForgeLoopInterval Duration `validate:"required"`
38 L2DB struct {
39 SafetyPeriod common.BatchNum `validate:"required"`
40 MaxTxs uint32 `validate:"required"`
41 TTL Duration `validate:"required"`
42 } `validate:"required"`
43 TxSelector struct {
44 Path string `validate:"required"`
45 } `validate:"required"`
46 BatchBuilder struct {
47 Path string `validate:"required"`
48 } `validate:"required"`
49 ServerProofs []ServerProof `validate:"required"`
50}
51
52// Node is the hermez node configuration.
53type Node struct {
54 StateDB struct {
55 Path string
56 } `validate:"required"`
57 PostgreSQL struct {
58 Port int `validate:"required"`
59 Host string `validate:"required"`
60 User string `validate:"required"`
61 Password string `validate:"required"`
62 Name string `validate:"required"`
63 } `validate:"required"`
64 Web3 struct {
65 URL string `validate:"required"`
66 } `validate:"required"`
67 Synchronizer struct {
68 SyncLoopInterval Duration `validate:"required"`
69 } `validate:"required"`
70 SmartContracts struct {
71 Rollup ethCommon.Address `validate:"required"`
72 Auction ethCommon.Address `validate:"required"`
73 TokenHEZ ethCommon.Address `validate:"required"`
74 } `validate:"required"`
75 EthClient struct {
76 CallGasLimit uint64 `validate:"required"`
77 DeployGasLimit uint64 `validate:"required"`
78 GasPriceDiv uint64 `validate:"required"`
79 ReceiptTimeout Duration `validate:"required"`
80 IntervalReceiptLoop Duration `validate:"required"`
81 } `validate:"required"`
82}
83
84// Load loads a generic config.
85func Load(path string, cfg interface{}) error {
86 bs, err := ioutil.ReadFile(path) //nolint:gosec
87 if err != nil {
88 return err
89 }
90 cfgToml := string(bs)
91 if _, err := toml.Decode(cfgToml, cfg); err != nil {
92 return err
93 }
94 validate := validator.New()
95 if err := validate.Struct(cfg); err != nil {
96 return fmt.Errorf("error validating configuration file: %w", err)
97 }
98 return nil
99}
100
101// LoadCoordinator loads the Coordinator configuration from path.
102func LoadCoordinator(path string) (*Coordinator, error) {
103 var cfg Coordinator
104 if err := Load(path, &cfg); err != nil {
105 return nil, fmt.Errorf("error loading coordinator configuration file: %w", err)
106 }
107 return &cfg, nil
108}
109
110// LoadNode loads the Node configuration from path.
111func LoadNode(path string) (*Node, error) {
112 var cfg Node
113 if err := Load(path, &cfg); err != nil {
114 return nil, fmt.Errorf("error loading node configuration file: %w", err)
115 }
116 return &cfg, nil
117}