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/hermez-node/synchronizer"
 12	"github.com/hermeznetwork/tracerr"
 13	"gopkg.in/go-playground/validator.v9"
 14)
 15
 16// Duration is a wrapper type that parses time duration from text.
 17type Duration struct {
 18	time.Duration `validate:"required"`
 19}
 20
 21// UnmarshalText unmarshalls time duration from text.
 22func (d *Duration) UnmarshalText(data []byte) error {
 23	duration, err := time.ParseDuration(string(data))
 24	if err != nil {
 25		return tracerr.Wrap(err)
 26	}
 27	d.Duration = duration
 28	return nil
 29}
 30
 31// ServerProof is the server proof configuration data.
 32type ServerProof struct {
 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              struct {
 53		SafetyPeriod common.BatchNum `validate:"required"`
 54		MaxTxs       uint32          `validate:"required"`
 55		TTL          Duration        `validate:"required"`
 56		// PurgeBatchDelay is the delay between batches to purge outdated transactions
 57		PurgeBatchDelay int64 `validate:"required"`
 58		// InvalidateBatchDelay is the delay between batches to mark invalid transactions
 59		InvalidateBatchDelay int64 `validate:"required"`
 60		// PurgeBlockDelay is the delay between blocks to purge outdated transactions
 61		PurgeBlockDelay int64 `validate:"required"`
 62		// InvalidateBlockDelay is the delay between blocks to mark invalid transactions
 63		InvalidateBlockDelay int64 `validate:"required"`
 64	} `validate:"required"`
 65	TxSelector struct {
 66		Path string `validate:"required"`
 67	} `validate:"required"`
 68	BatchBuilder struct {
 69		Path string `validate:"required"`
 70	} `validate:"required"`
 71	ServerProofs []ServerProof `validate:"required"`
 72	EthClient    struct {
 73		CallGasLimit        uint64   `validate:"required"`
 74		DeployGasLimit      uint64   `validate:"required"`
 75		GasPriceDiv         uint64   `validate:"required"`
 76		ReceiptTimeout      Duration `validate:"required"`
 77		ReceiptLoopInterval Duration `validate:"required"`
 78		// CheckLoopInterval is the waiting interval between receipt
 79		// checks of ethereum transactions in the TxManager
 80		CheckLoopInterval Duration `validate:"required"`
 81		// Attempts is the number of attempts to do an eth client RPC
 82		// call before giving up
 83		Attempts int `validate:"required"`
 84		// AttemptsDelay is delay between attempts do do an eth client
 85		// RPC call
 86		AttemptsDelay Duration `validate:"required"`
 87	} `validate:"required"`
 88	API struct {
 89		Coordinator bool
 90	} `validate:"required"`
 91	Debug struct {
 92		// BatchPath if set, specifies the path where batchInfo is stored
 93		// in JSON in every step/update of the pipeline
 94		BatchPath string
 95	}
 96}
 97
 98// Node is the hermez node configuration.
 99type Node struct {
100	PriceUpdater struct {
101		Interval Duration `valudate:"required"`
102		URL      string   `valudate:"required"`
103		Type     string   `valudate:"required"`
104	} `validate:"required"`
105	StateDB struct {
106		Path string
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		StartBlockNum      synchronizer.ConfigStartBlockNum `validate:"required"`
122		InitialVariables   synchronizer.SCVariables         `validate:"required"`
123	} `validate:"required"`
124	SmartContracts struct {
125		Rollup       ethCommon.Address `validate:"required"`
126		Auction      ethCommon.Address `validate:"required"`
127		WDelayer     ethCommon.Address `validate:"required"`
128		TokenHEZ     ethCommon.Address `validate:"required"`
129		TokenHEZName string            `validate:"required"`
130	} `validate:"required"`
131	API struct {
132		Address                      string
133		Explorer                     bool
134		UpdateMetricsInterval        Duration
135		UpdateRecommendedFeeInterval Duration
136	} `validate:"required"`
137	Debug struct {
138		APIAddress string
139	}
140	Coordinator Coordinator `validate:"-"`
141}
142
143// Load loads a generic config.
144func Load(path string, cfg interface{}) error {
145	bs, err := ioutil.ReadFile(path) //nolint:gosec
146	if err != nil {
147		return tracerr.Wrap(err)
148	}
149	cfgToml := string(bs)
150	if _, err := toml.Decode(cfgToml, cfg); err != nil {
151		return tracerr.Wrap(err)
152	}
153	return nil
154}
155
156// LoadCoordinator loads the Coordinator configuration from path.
157func LoadCoordinator(path string) (*Node, error) {
158	var cfg Node
159	if err := Load(path, &cfg); err != nil {
160		return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
161	}
162	validate := validator.New()
163	if err := validate.Struct(cfg); err != nil {
164		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
165	}
166	if err := validate.Struct(cfg.Coordinator); err != nil {
167		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
168	}
169	return &cfg, nil
170}
171
172// LoadNode loads the Node configuration from path.
173func LoadNode(path string) (*Node, error) {
174	var cfg Node
175	if err := Load(path, &cfg); err != nil {
176		return nil, tracerr.Wrap(fmt.Errorf("error loading node configuration file: %w", err))
177	}
178	validate := validator.New()
179	if err := validate.Struct(cfg); err != nil {
180		return nil, tracerr.Wrap(fmt.Errorf("error validating configuration file: %w", err))
181	}
182	return &cfg, nil
183}