arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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	} `validate:"required"`
 87	API struct {
 88		Coordinator bool
 89	} `validate:"required"`
 90	Debug struct {
 91		// BatchPath if set, specifies the path where batchInfo is stored
 92		// in JSON in every step/update of the pipeline
 93		BatchPath string
 94	}
 95}
 96
 97// Node is the hermez node configuration.
 98type Node struct {
 99	PriceUpdater struct {
100		Interval Duration `valudate:"required"`
101		URL      string   `valudate:"required"`
102		Type     string   `valudate:"required"`
103	} `validate:"required"`
104	StateDB struct {
105		Path string
106	} `validate:"required"`
107	PostgreSQL struct {
108		Port     int    `validate:"required"`
109		Host     string `validate:"required"`
110		User     string `validate:"required"`
111		Password string `validate:"required"`
112		Name     string `validate:"required"`
113	} `validate:"required"`
114	Web3 struct {
115		URL string `validate:"required"`
116	} `validate:"required"`
117	Synchronizer struct {
118		SyncLoopInterval   Duration `validate:"required"`
119		StatsRefreshPeriod Duration `validate:"required"`
120	} `validate:"required"`
121	SmartContracts struct {
122		Rollup       ethCommon.Address `validate:"required"`
123		Auction      ethCommon.Address `validate:"required"`
124		WDelayer     ethCommon.Address `validate:"required"`
125		TokenHEZ     ethCommon.Address `validate:"required"`
126		TokenHEZName string            `validate:"required"`
127	} `validate:"required"`
128	API struct {
129		Address                      string
130		Explorer                     bool
131		UpdateMetricsInterval        Duration
132		UpdateRecommendedFeeInterval Duration
133	} `validate:"required"`
134	Debug struct {
135		APIAddress string
136	}
137	Coordinator Coordinator `validate:"-"`
138}
139
140// Load loads a generic config.
141func Load(path string, cfg interface{}) error {
142	bs, err := ioutil.ReadFile(path) //nolint:gosec
143	if err != nil {
144		return tracerr.Wrap(err)
145	}
146	cfgToml := string(bs)
147	if _, err := toml.Decode(cfgToml, cfg); err != nil {
148		return tracerr.Wrap(err)
149	}
150	return nil
151}
152
153// LoadCoordinator loads the Coordinator configuration from path.
154func LoadCoordinator(path string) (*Node, error) {
155	var cfg Node
156	if err := Load(path, &cfg); err != nil {
157		return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
158	}
159	validate := validator.New()
160	if err := validate.Struct(cfg); err != nil {
161		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
162	}
163	if err := validate.Struct(cfg.Coordinator); err != nil {
164		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
165	}
166	return &cfg, nil
167}
168
169// LoadNode loads the Node configuration from path.
170func LoadNode(path string) (*Node, error) {
171	var cfg Node
172	if err := Load(path, &cfg); err != nil {
173		return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
174	}
175	validate := validator.New()
176	if err := validate.Struct(cfg); err != nil {
177		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
178	}
179	return &cfg, nil
180}