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