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	"gopkg.in/go-playground/validator.v9"
 12)
 13
 14// Duration is a wrapper type that parses time duration from text.
 15type Duration struct {
 16	time.Duration
 17}
 18
 19// UnmarshalText unmarshalls time duration from text.
 20func (d *Duration) UnmarshalText(data []byte) error {
 21	duration, err := time.ParseDuration(string(data))
 22	if err != nil {
 23		return err
 24	}
 25	d.Duration = duration
 26	return nil
 27}
 28
 29// ServerProof is the server proof configuration data.
 30type ServerProof struct {
 31	URL string `validate:"required"`
 32}
 33
 34// Coordinator is the coordinator specific configuration.
 35type Coordinator struct {
 36	ForgerAddress     ethCommon.Address `validate:"required"`
 37	ForgeLoopInterval Duration          `validate:"required"`
 38	L2DB              struct {
 39		SafetyPeriod common.BatchNum `validate:"required"`
 40		MaxTxs       uint32          `validate:"required"`
 41		TTL          Duration        `validate:"required"`
 42	} `validate:"required"`
 43	TxSelector struct {
 44		Path string `validate:"required"`
 45	} `validate:"required"`
 46	BatchBuilder struct {
 47		Path string `validate:"required"`
 48	} `validate:"required"`
 49	ServerProofs []ServerProof `validate:"required"`
 50}
 51
 52// Node is the hermez node configuration.
 53type Node struct {
 54	StateDB struct {
 55		Path string
 56	} `validate:"required"`
 57	PostgreSQL struct {
 58		Port     int    `validate:"required"`
 59		Host     string `validate:"required"`
 60		User     string `validate:"required"`
 61		Password string `validate:"required"`
 62		Name     string `validate:"required"`
 63	} `validate:"required"`
 64	Web3 struct {
 65		URL string `validate:"required"`
 66	} `validate:"required"`
 67	Synchronizer struct {
 68		SyncLoopInterval Duration `validate:"required"`
 69	} `validate:"required"`
 70	SmartContracts struct {
 71		Rollup       ethCommon.Address `validate:"required"`
 72		Auction      ethCommon.Address `validate:"required"`
 73		TokenHEZ     ethCommon.Address `validate:"required"`
 74		TokenHEZName string            `validate:"required"`
 75	} `validate:"required"`
 76	EthClient struct {
 77		CallGasLimit        uint64   `validate:"required"`
 78		DeployGasLimit      uint64   `validate:"required"`
 79		GasPriceDiv         uint64   `validate:"required"`
 80		ReceiptTimeout      Duration `validate:"required"`
 81		IntervalReceiptLoop Duration `validate:"required"`
 82	} `validate:"required"`
 83}
 84
 85// Load loads a generic config.
 86func Load(path string, cfg interface{}) error {
 87	bs, err := ioutil.ReadFile(path) //nolint:gosec
 88	if err != nil {
 89		return err
 90	}
 91	cfgToml := string(bs)
 92	if _, err := toml.Decode(cfgToml, cfg); err != nil {
 93		return err
 94	}
 95	validate := validator.New()
 96	if err := validate.Struct(cfg); err != nil {
 97		return fmt.Errorf("error validating configuration file: %w", err)
 98	}
 99	return nil
100}
101
102// LoadCoordinator loads the Coordinator configuration from path.
103func LoadCoordinator(path string) (*Coordinator, error) {
104	var cfg Coordinator
105	if err := Load(path, &cfg); err != nil {
106		return nil, fmt.Errorf("error loading coordinator configuration file: %w", err)
107	}
108	return &cfg, nil
109}
110
111// LoadNode loads the Node configuration from path.
112func LoadNode(path string) (*Node, error) {
113	var cfg Node
114	if err := Load(path, &cfg); err != nil {
115		return nil, fmt.Errorf("error loading node configuration file: %w", err)
116	}
117	return &cfg, nil
118}