github.com/consideritdone/landslidecore@v0.0.0-20230718131026-a8b21c5cf8a7/test/e2e/node/config.go (about)

     1  // nolint: goconst
     2  package main
     3  
     4  import (
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/BurntSushi/toml"
     9  
    10  	"github.com/consideritdone/landslidecore/test/e2e/app"
    11  )
    12  
    13  // Config is the application configuration.
    14  type Config struct {
    15  	ChainID          string `toml:"chain_id"`
    16  	Listen           string
    17  	Protocol         string
    18  	Dir              string
    19  	Mode             string                      `toml:"mode"`
    20  	PersistInterval  uint64                      `toml:"persist_interval"`
    21  	SnapshotInterval uint64                      `toml:"snapshot_interval"`
    22  	RetainBlocks     uint64                      `toml:"retain_blocks"`
    23  	ValidatorUpdates map[string]map[string]uint8 `toml:"validator_update"`
    24  	PrivValServer    string                      `toml:"privval_server"`
    25  	PrivValKey       string                      `toml:"privval_key"`
    26  	PrivValState     string                      `toml:"privval_state"`
    27  	Misbehaviors     map[string]string           `toml:"misbehaviors"`
    28  	KeyType          string                      `toml:"key_type"`
    29  }
    30  
    31  // App extracts out the application specific configuration parameters
    32  func (cfg *Config) App() *app.Config {
    33  	return &app.Config{
    34  		Dir:              cfg.Dir,
    35  		SnapshotInterval: cfg.SnapshotInterval,
    36  		RetainBlocks:     cfg.RetainBlocks,
    37  		KeyType:          cfg.KeyType,
    38  		ValidatorUpdates: cfg.ValidatorUpdates,
    39  		PersistInterval:  cfg.PersistInterval,
    40  	}
    41  }
    42  
    43  // LoadConfig loads the configuration from disk.
    44  func LoadConfig(file string) (*Config, error) {
    45  	cfg := &Config{
    46  		Listen:          "unix:///var/run/app.sock",
    47  		Protocol:        "socket",
    48  		PersistInterval: 1,
    49  	}
    50  	_, err := toml.DecodeFile(file, &cfg)
    51  	if err != nil {
    52  		return nil, fmt.Errorf("failed to load config from %q: %w", file, err)
    53  	}
    54  	return cfg, cfg.Validate()
    55  }
    56  
    57  // Validate validates the configuration. We don't do exhaustive config
    58  // validation here, instead relying on Testnet.Validate() to handle it.
    59  func (cfg Config) Validate() error {
    60  	switch {
    61  	case cfg.ChainID == "":
    62  		return errors.New("chain_id parameter is required")
    63  	case cfg.Listen == "" && cfg.Protocol != "builtin":
    64  		return errors.New("listen parameter is required")
    65  	default:
    66  		return nil
    67  	}
    68  }