github.com/vipernet-xyz/tm@v0.34.24/test/e2e/node/config.go (about)

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