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		StartBlockNum    struct {
 70			Rollup   int64 `validate:"required"`
 71			Auction  int64 `validate:"required"`
 72			WDelayer int64 `validate:"required"`
 73		} `validate:"required"`
 74	} `validate:"required"`
 75	SmartContracts struct {
 76		Rollup       ethCommon.Address `validate:"required"`
 77		Auction      ethCommon.Address `validate:"required"`
 78		TokenHEZ     ethCommon.Address `validate:"required"`
 79		TokenHEZName string            `validate:"required"`
 80	} `validate:"required"`
 81	EthClient struct {
 82		CallGasLimit        uint64   `validate:"required"`
 83		DeployGasLimit      uint64   `validate:"required"`
 84		GasPriceDiv         uint64   `validate:"required"`
 85		ReceiptTimeout      Duration `validate:"required"`
 86		IntervalReceiptLoop Duration `validate:"required"`
 87	} `validate:"required"`
 88}
 89
 90// Load loads a generic config.
 91func Load(path string, cfg interface{}) error {
 92	bs, err := ioutil.ReadFile(path) //nolint:gosec
 93	if err != nil {
 94		return err
 95	}
 96	cfgToml := string(bs)
 97	if _, err := toml.Decode(cfgToml, cfg); err != nil {
 98		return err
 99	}
100	validate := validator.New()
101	if err := validate.Struct(cfg); err != nil {
102		return fmt.Errorf("error validating configuration file: %w", err)
103	}
104	return nil
105}
106
107// LoadCoordinator loads the Coordinator configuration from path.
108func LoadCoordinator(path string) (*Coordinator, error) {
109	var cfg Coordinator
110	if err := Load(path, &cfg); err != nil {
111		return nil, fmt.Errorf("error loading coordinator configuration file: %w", err)
112	}
113	return &cfg, nil
114}
115
116// LoadNode loads the Node configuration from path.
117func LoadNode(path string) (*Node, error) {
118	var cfg Node
119	if err := Load(path, &cfg); err != nil {
120		return nil, fmt.Errorf("error loading node configuration file: %w", err)
121	}
122	return &cfg, nil
123}