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 `validate:"required"`
106		Keep int    `validate:"required"`
107	} `validate:"required"`
108	PostgreSQL struct {
109		Port     int    `validate:"required"`
110		Host     string `validate:"required"`
111		User     string `validate:"required"`
112		Password string `validate:"required"`
113		Name     string `validate:"required"`
114	} `validate:"required"`
115	Web3 struct {
116		URL string `validate:"required"`
117	} `validate:"required"`
118	Synchronizer struct {
119		SyncLoopInterval   Duration `validate:"required"`
120		StatsRefreshPeriod Duration `validate:"required"`
121	} `validate:"required"`
122	SmartContracts struct {
123		Rollup       ethCommon.Address `validate:"required"`
124		Auction      ethCommon.Address `validate:"required"`
125		WDelayer     ethCommon.Address `validate:"required"`
126		TokenHEZ     ethCommon.Address `validate:"required"`
127		TokenHEZName string            `validate:"required"`
128	} `validate:"required"`
129	API struct {
130		Address                      string
131		Explorer                     bool
132		UpdateMetricsInterval        Duration
133		UpdateRecommendedFeeInterval Duration
134	} `validate:"required"`
135	Debug struct {
136		APIAddress string
137	}
138	Coordinator Coordinator `validate:"-"`
139}
140
141// Load loads a generic config.
142func Load(path string, cfg interface{}) error {
143	bs, err := ioutil.ReadFile(path) //nolint:gosec
144	if err != nil {
145		return tracerr.Wrap(err)
146	}
147	cfgToml := string(bs)
148	if _, err := toml.Decode(cfgToml, cfg); err != nil {
149		return tracerr.Wrap(err)
150	}
151	return nil
152}
153
154// LoadCoordinator loads the Coordinator configuration from path.
155func LoadCoordinator(path string) (*Node, error) {
156	var cfg Node
157	if err := Load(path, &cfg); err != nil {
158		return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
159	}
160	validate := validator.New()
161	if err := validate.Struct(cfg); err != nil {
162		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
163	}
164	if err := validate.Struct(cfg.Coordinator); err != nil {
165		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
166	}
167	return &cfg, nil
168}
169
170// LoadNode loads the Node configuration from path.
171func LoadNode(path string) (*Node, error) {
172	var cfg Node
173	if err := Load(path, &cfg); err != nil {
174		return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
175	}
176	validate := validator.New()
177	if err := validate.Struct(cfg); err != nil {
178		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
179	}
180	return &cfg, nil
181}