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