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 is the server proof API URL
 33	URL string `validate:"required"`
 34}
 35
 36// Coordinator is the coordinator specific configuration.
 37type Coordinator struct {
 38	// ForgerAddress is the address under which this coordinator is forging
 39	ForgerAddress ethCommon.Address `validate:"required"`
 40	// ConfirmBlocks is the number of confirmation blocks to wait for sent
 41	// ethereum transactions before forgetting about them
 42	ConfirmBlocks int64 `validate:"required"`
 43	// L1BatchTimeoutPerc is the portion of the range before the L1Batch
 44	// timeout that will trigger a schedule to forge an L1Batch
 45	L1BatchTimeoutPerc float64 `validate:"required"`
 46	// ProofServerPollInterval is the waiting interval between polling the
 47	// ProofServer while waiting for a particular status
 48	ProofServerPollInterval Duration `validate:"required"`
 49	// SyncRetryInterval is the waiting interval between calls to the main
 50	// handler of a synced block after an error
 51	SyncRetryInterval Duration `validate:"required"`
 52	// L2DB is the DB that holds the pool of L2Txs
 53	L2DB struct {
 54		// SafetyPeriod is the number of batches after which
 55		// non-pending L2Txs are deleted from the pool
 56		SafetyPeriod common.BatchNum `validate:"required"`
 57		// MaxTxs is the number of L2Txs that once reached triggers
 58		// deletion of old L2Txs
 59		MaxTxs uint32 `validate:"required"`
 60		// TTL is the Time To Live for L2Txs in the pool.  Once MaxTxs
 61		// L2Txs is reached, L2Txs older than TTL will be deleted.
 62		TTL Duration `validate:"required"`
 63		// PurgeBatchDelay is the delay between batches to purge outdated transactions
 64		PurgeBatchDelay int64 `validate:"required"`
 65		// InvalidateBatchDelay is the delay between batches to mark invalid transactions
 66		InvalidateBatchDelay int64 `validate:"required"`
 67		// PurgeBlockDelay is the delay between blocks to purge outdated transactions
 68		PurgeBlockDelay int64 `validate:"required"`
 69		// InvalidateBlockDelay is the delay between blocks to mark invalid transactions
 70		InvalidateBlockDelay int64 `validate:"required"`
 71	} `validate:"required"`
 72	TxSelector struct {
 73		// Path where the TxSelector StateDB is stored
 74		Path string `validate:"required"`
 75	} `validate:"required"`
 76	BatchBuilder struct {
 77		// Path where the BatchBuilder StateDB is stored
 78		Path string `validate:"required"`
 79	} `validate:"required"`
 80	ServerProofs []ServerProof `validate:"required"`
 81	Circuit      struct {
 82		// VerifierIdx uint8  `validate:"required"`
 83		// MaxTx is the maximum number of txs supported by the circuit
 84		MaxTx int64 `validate:"required"`
 85		// NLevels is the maximum number of merkle tree levels
 86		// supported by the circuit
 87		NLevels int64 `validate:"required"`
 88	} `validate:"required"`
 89	EthClient struct {
 90		// CallGasLimit is the default gas limit set for ethereum
 91		// calls, except for methods  where a particular gas limit is
 92		// harcoded because it's known to be a big value
 93		CallGasLimit uint64 `validate:"required"`
 94		// GasPriceDiv is the gas price division
 95		GasPriceDiv uint64 `validate:"required"`
 96		// CheckLoopInterval is the waiting interval between receipt
 97		// checks of ethereum transactions in the TxManager
 98		CheckLoopInterval Duration `validate:"required"`
 99		// Attempts is the number of attempts to do an eth client RPC
100		// call before giving up
101		Attempts int `validate:"required"`
102		// AttemptsDelay is delay between attempts do do an eth client
103		// RPC call
104		AttemptsDelay Duration `validate:"required"`
105		// Keystore is the ethereum keystore where private keys are kept
106		Keystore struct {
107			// Path to the keystore
108			Path string `validate:"required"`
109			// Password used to decrypt the keys in the keystore
110			Password string `validate:"required"`
111		} `validate:"required"`
112	} `validate:"required"`
113	API struct {
114		// Coordinator enables the coordinator API endpoints
115		Coordinator bool
116	} `validate:"required"`
117	Debug struct {
118		// BatchPath if set, specifies the path where batchInfo is stored
119		// in JSON in every step/update of the pipeline
120		BatchPath string
121		// LightScrypt if set, uses light parameters for the ethereum
122		// keystore encryption algorithm.
123		LightScrypt bool
124	}
125}
126
127// Node is the hermez node configuration.
128type Node struct {
129	PriceUpdater struct {
130		// Interval between price updater calls
131		Interval Duration `valudate:"required"`
132		// URL of the token prices provider
133		URL string `valudate:"required"`
134		// Type of the API of the token prices provider
135		Type string `valudate:"required"`
136	} `validate:"required"`
137	StateDB struct {
138		// Path where the synchronizer StateDB is stored
139		Path string `validate:"required"`
140		// Keep is the number of checkpoints to keep
141		Keep int `validate:"required"`
142	} `validate:"required"`
143	PostgreSQL struct {
144		// Port of the PostgreSQL server
145		Port int `validate:"required"`
146		// Host of the PostgreSQL server
147		Host string `validate:"required"`
148		// User of the PostgreSQL server
149		User string `validate:"required"`
150		// Password of the PostgreSQL server
151		Password string `validate:"required"`
152		// Name of the PostgreSQL server database
153		Name string `validate:"required"`
154	} `validate:"required"`
155	Web3 struct {
156		// URL is the URL of the web3 ethereum-node RPC server
157		URL string `validate:"required"`
158	} `validate:"required"`
159	Synchronizer struct {
160		// SyncLoopInterval is the interval between attempts to
161		// synchronize a new block from an ethereum node
162		SyncLoopInterval Duration `validate:"required"`
163		// StatsRefreshPeriod is the interval between updates of the
164		// synchronizer state Eth parameters (`Eth.LastBlock` and
165		// `Eth.LastBatch`)
166		StatsRefreshPeriod Duration `validate:"required"`
167	} `validate:"required"`
168	SmartContracts struct {
169		// Rollup is the address of the Hermez.sol smart contract
170		Rollup ethCommon.Address `validate:"required"`
171		// Rollup is the address of the HermezAuctionProtocol.sol smart
172		// contract
173		Auction ethCommon.Address `validate:"required"`
174		// WDelayer is the address of the WithdrawalDelayer.sol smart
175		// contract
176		WDelayer ethCommon.Address `validate:"required"`
177		// TokenHEZ is the address of the HEZTokenFull.sol smart
178		// contract
179		TokenHEZ ethCommon.Address `validate:"required"`
180		// TokenHEZName is the name of the HEZ token deployed at
181		// TokenHEZ address
182		TokenHEZName string `validate:"required"`
183	} `validate:"required"`
184	API struct {
185		// Address where the API will listen if set
186		Address string
187		// Explorer enables the Explorer API endpoints
188		Explorer bool
189		// UpdateMetricsInterval is the interval between updates of the
190		// API metrics
191		UpdateMetricsInterval Duration
192		// UpdateMetricsInterval is the interval between updates of the
193		// recommended fees
194		UpdateRecommendedFeeInterval Duration
195	} `validate:"required"`
196	Debug struct {
197		// APIAddress is the address where the debugAPI will listen if
198		// set
199		APIAddress string
200		// MeddlerLogs enables meddler debug mode, where unused columns and struct
201		// fields will be logged
202		MeddlerLogs bool
203	}
204	Coordinator Coordinator `validate:"-"`
205}
206
207// Load loads a generic config.
208func Load(path string, cfg interface{}) error {
209	bs, err := ioutil.ReadFile(path) //nolint:gosec
210	if err != nil {
211		return tracerr.Wrap(err)
212	}
213	cfgToml := string(bs)
214	if _, err := toml.Decode(cfgToml, cfg); err != nil {
215		return tracerr.Wrap(err)
216	}
217	return nil
218}
219
220// LoadCoordinator loads the Coordinator configuration from path.
221func LoadCoordinator(path string) (*Node, error) {
222	var cfg Node
223	if err := Load(path, &cfg); err != nil {
224		return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
225	}
226	validate := validator.New()
227	if err := validate.Struct(cfg); err != nil {
228		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
229	}
230	if err := validate.Struct(cfg.Coordinator); err != nil {
231		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
232	}
233	return &cfg, nil
234}
235
236// LoadNode loads the Node configuration from path.
237func LoadNode(path string) (*Node, error) {
238	var cfg Node
239	if err := Load(path, &cfg); err != nil {
240		return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
241	}
242	validate := validator.New()
243	if err := validate.Struct(cfg); err != nil {
244		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
245	}
246	return &cfg, nil
247}