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