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}
 71
 72// Load loads a generic config.
 73func Load(path string, cfg interface{}) error {
 74	bs, err := ioutil.ReadFile(path) //nolint:gosec
 75	if err != nil {
 76		return err
 77	}
 78	cfgToml := string(bs)
 79	if _, err := toml.Decode(cfgToml, cfg); err != nil {
 80		return err
 81	}
 82	validate := validator.New()
 83	if err := validate.Struct(cfg); err != nil {
 84		return fmt.Errorf("error validating configuration file: %w", err)
 85	}
 86	return nil
 87}
 88
 89// LoadCoordinator loads the Coordinator configuration from path.
 90func LoadCoordinator(path string) (*Coordinator, error) {
 91	var cfg Coordinator
 92	if err := Load(path, &cfg); err != nil {
 93		return nil, fmt.Errorf("error loading coordinator configuration file: %w", err)
 94	}
 95	return &cfg, nil
 96}
 97
 98// LoadNode loads the Node configuration from path.
 99func LoadNode(path string) (*Node, error) {
100	var cfg Node
101	if err := Load(path, &cfg); err != nil {
102		return nil, fmt.Errorf("error loading node configuration file: %w", err)
103	}
104	return &cfg, nil
105}