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	// ChainID is the ChainID from the Ethereum chain
 52	ChainID uint16 `validate:"required"`
 53	L2DB    struct {
 54		SafetyPeriod common.BatchNum `validate:"required"`
 55		MaxTxs       uint32          `validate:"required"`
 56		TTL          Duration        `validate:"required"`
 57		// PurgeBatchDelay is the delay between batches to purge outdated transactions
 58		PurgeBatchDelay int64 `validate:"required"`
 59		// InvalidateBatchDelay is the delay between batches to mark invalid transactions
 60		InvalidateBatchDelay int64 `validate:"required"`
 61		// PurgeBlockDelay is the delay between blocks to purge outdated transactions
 62		PurgeBlockDelay int64 `validate:"required"`
 63		// InvalidateBlockDelay is the delay between blocks to mark invalid transactions
 64		InvalidateBlockDelay int64 `validate:"required"`
 65	} `validate:"required"`
 66	TxSelector struct {
 67		Path string `validate:"required"`
 68	} `validate:"required"`
 69	BatchBuilder struct {
 70		Path string `validate:"required"`
 71	} `validate:"required"`
 72	ServerProofs []ServerProof `validate:"required"`
 73	EthClient    struct {
 74		CallGasLimit        uint64   `validate:"required"`
 75		DeployGasLimit      uint64   `validate:"required"`
 76		GasPriceDiv         uint64   `validate:"required"`
 77		ReceiptTimeout      Duration `validate:"required"`
 78		ReceiptLoopInterval Duration `validate:"required"`
 79		// CheckLoopInterval is the waiting interval between receipt
 80		// checks of ethereum transactions in the TxManager
 81		CheckLoopInterval Duration `validate:"required"`
 82		// Attempts is the number of attempts to do an eth client RPC
 83		// call before giving up
 84		Attempts int `validate:"required"`
 85		// AttemptsDelay is delay between attempts do do an eth client
 86		// RPC call
 87		AttemptsDelay Duration `validate:"required"`
 88	} `validate:"required"`
 89	API struct {
 90		Coordinator bool
 91	} `validate:"required"`
 92	Debug struct {
 93		// BatchPath if set, specifies the path where batchInfo is stored
 94		// in JSON in every step/update of the pipeline
 95		BatchPath string
 96	}
 97}
 98
 99// Node is the hermez node configuration.
100type Node struct {
101	PriceUpdater struct {
102		Interval Duration `valudate:"required"`
103		URL      string   `valudate:"required"`
104		Type     string   `valudate:"required"`
105	} `validate:"required"`
106	StateDB struct {
107		Path string
108	} `validate:"required"`
109	PostgreSQL struct {
110		Port     int    `validate:"required"`
111		Host     string `validate:"required"`
112		User     string `validate:"required"`
113		Password string `validate:"required"`
114		Name     string `validate:"required"`
115	} `validate:"required"`
116	Web3 struct {
117		URL string `validate:"required"`
118	} `validate:"required"`
119	Synchronizer struct {
120		SyncLoopInterval   Duration `validate:"required"`
121		StatsRefreshPeriod Duration `validate:"required"`
122	} `validate:"required"`
123	SmartContracts struct {
124		Rollup       ethCommon.Address `validate:"required"`
125		Auction      ethCommon.Address `validate:"required"`
126		WDelayer     ethCommon.Address `validate:"required"`
127		TokenHEZ     ethCommon.Address `validate:"required"`
128		TokenHEZName string            `validate:"required"`
129	} `validate:"required"`
130	API struct {
131		Address                      string
132		Explorer                     bool
133		UpdateMetricsInterval        Duration
134		UpdateRecommendedFeeInterval Duration
135	} `validate:"required"`
136	Debug struct {
137		APIAddress string
138	}
139	Coordinator Coordinator `validate:"-"`
140}
141
142// Load loads a generic config.
143func Load(path string, cfg interface{}) error {
144	bs, err := ioutil.ReadFile(path) //nolint:gosec
145	if err != nil {
146		return tracerr.Wrap(err)
147	}
148	cfgToml := string(bs)
149	if _, err := toml.Decode(cfgToml, cfg); err != nil {
150		return tracerr.Wrap(err)
151	}
152	return nil
153}
154
155// LoadCoordinator loads the Coordinator configuration from path.
156func LoadCoordinator(path string) (*Node, error) {
157	var cfg Node
158	if err := Load(path, &cfg); err != nil {
159		return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
160	}
161	validate := validator.New()
162	if err := validate.Struct(cfg); err != nil {
163		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
164	}
165	if err := validate.Struct(cfg.Coordinator); err != nil {
166		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
167	}
168	return &cfg, nil
169}
170
171// LoadNode loads the Node configuration from path.
172func LoadNode(path string) (*Node, error) {
173	var cfg Node
174	if err := Load(path, &cfg); err != nil {
175		return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
176	}
177	validate := validator.New()
178	if err := validate.Struct(cfg); err != nil {
179		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
180	}
181	return &cfg, nil
182}