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