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 Name string `validate:"required"`
40 SafetyPeriod common.BatchNum `validate:"required"`
41 MaxTxs uint32 `validate:"required"`
42 TTL Duration `validate:"required"`
43 } `validate:"required"`
44 TxSelector struct {
45 Path string `validate:"required"`
46 } `validate:"required"`
47 BatchBuilder struct {
48 Path string `validate:"required"`
49 } `validate:"required"`
50 ServerProofs []ServerProof `validate:"required"`
51}
52
53// Node is the hermez node configuration.
54type Node struct {
55 StateDB struct {
56 Path string
57 } `validate:"required"`
58 PostgreSQL struct {
59 Port int `validate:"required"`
60 Host string `validate:"required"`
61 User string `validate:"required"`
62 Password string `validate:"required"`
63 } `validate:"required"`
64 HistoryDB struct {
65 Name string `validate:"required"`
66 } `validate:"required"`
67 Web3 struct {
68 URL string `validate:"required"`
69 } `validate:"required"`
70 Synchronizer struct {
71 SyncLoopInterval Duration `validate:"required"`
72 } `validate:"required"`
73}
74
75// Load loads a generic config.
76func Load(path string, cfg interface{}) error {
77 bs, err := ioutil.ReadFile(path) //nolint:gosec
78 if err != nil {
79 return err
80 }
81 cfgToml := string(bs)
82 if _, err := toml.Decode(cfgToml, cfg); err != nil {
83 return err
84 }
85 validate := validator.New()
86 if err := validate.Struct(cfg); err != nil {
87 return fmt.Errorf("error validating configuration file: %w", err)
88 }
89 return nil
90}
91
92// LoadCoordinator loads the Coordinator configuration from path.
93func LoadCoordinator(path string) (*Coordinator, error) {
94 var cfg Coordinator
95 if err := Load(path, &cfg); err != nil {
96 return nil, fmt.Errorf("error loading coordinator configuration file: %w", err)
97 }
98 return &cfg, nil
99}
100
101// LoadNode loads the Node configuration from path.
102func LoadNode(path string) (*Node, error) {
103 var cfg Node
104 if err := Load(path, &cfg); err != nil {
105 return nil, fmt.Errorf("error loading node configuration file: %w", err)
106 }
107 return &cfg, nil
108}