github.com/cgcardona/r-subnet-evm@v0.1.5/cmd/simulator/config/flags.go (about)

     1  // (c) 2023, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package config
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/spf13/pflag"
    13  	"github.com/spf13/viper"
    14  )
    15  
    16  const Version = "v0.1.0"
    17  
    18  const (
    19  	ConfigFilePathKey = "config-file"
    20  	LogLevelKey       = "log-level"
    21  	EndpointsKey      = "endpoints"
    22  	MaxFeeCapKey      = "max-fee-cap"
    23  	MaxTipCapKey      = "max-tip-cap"
    24  	WorkersKey        = "workers"
    25  	TxsPerWorkerKey   = "txs-per-worker"
    26  	KeyDirKey         = "key-dir"
    27  	VersionKey        = "version"
    28  	TimeoutKey        = "timeout"
    29  	BatchSizeKey      = "batch-size"
    30  )
    31  
    32  var (
    33  	ErrNoEndpoints = errors.New("must specify at least one endpoint")
    34  	ErrNoWorkers   = errors.New("must specify non-zero number of workers")
    35  	ErrNoTxs       = errors.New("must specify non-zero number of txs-per-worker")
    36  )
    37  
    38  type Config struct {
    39  	Endpoints    []string      `json:"endpoints"`
    40  	MaxFeeCap    int64         `json:"max-fee-cap"`
    41  	MaxTipCap    int64         `json:"max-tip-cap"`
    42  	Workers      int           `json:"workers"`
    43  	TxsPerWorker uint64        `json:"txs-per-worker"`
    44  	KeyDir       string        `json:"key-dir"`
    45  	Timeout      time.Duration `json:"timeout"`
    46  	BatchSize    uint64        `json:"batch-size"`
    47  }
    48  
    49  func BuildConfig(v *viper.Viper) (Config, error) {
    50  	c := Config{
    51  		Endpoints:    v.GetStringSlice(EndpointsKey),
    52  		MaxFeeCap:    v.GetInt64(MaxFeeCapKey),
    53  		MaxTipCap:    v.GetInt64(MaxTipCapKey),
    54  		Workers:      v.GetInt(WorkersKey),
    55  		TxsPerWorker: v.GetUint64(TxsPerWorkerKey),
    56  		KeyDir:       v.GetString(KeyDirKey),
    57  		Timeout:      v.GetDuration(TimeoutKey),
    58  		BatchSize:    v.GetUint64(BatchSizeKey),
    59  	}
    60  	if len(c.Endpoints) == 0 {
    61  		return c, ErrNoEndpoints
    62  	}
    63  	if c.Workers == 0 {
    64  		return c, ErrNoWorkers
    65  	}
    66  	if c.TxsPerWorker == 0 {
    67  		return c, ErrNoTxs
    68  	}
    69  	// Note: it's technically valid for the fee/tip cap to be 0, but cannot
    70  	// be less than 0.
    71  	if c.MaxFeeCap < 0 {
    72  		return c, fmt.Errorf("invalid max fee cap %d < 0", c.MaxFeeCap)
    73  	}
    74  	if c.MaxTipCap < 0 {
    75  		return c, fmt.Errorf("invalid max tip cap %d <= 0", c.MaxTipCap)
    76  	}
    77  	return c, nil
    78  }
    79  
    80  func BuildViper(fs *pflag.FlagSet, args []string) (*viper.Viper, error) {
    81  	if err := fs.Parse(args); err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	v := viper.New()
    86  	v.AutomaticEnv()
    87  	v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
    88  	v.SetEnvPrefix("evm_simulator")
    89  	if err := v.BindPFlags(fs); err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	if v.IsSet(ConfigFilePathKey) {
    94  		v.SetConfigFile(v.GetString(ConfigFilePathKey))
    95  		if err := v.ReadInConfig(); err != nil {
    96  			return nil, err
    97  		}
    98  	}
    99  	return v, nil
   100  }
   101  
   102  // BuildFlagSet returns a complete set of flags for simulator
   103  func BuildFlagSet() *pflag.FlagSet {
   104  	fs := pflag.NewFlagSet("simulator", pflag.ContinueOnError)
   105  	addSimulatorFlags(fs)
   106  	return fs
   107  }
   108  
   109  func addSimulatorFlags(fs *pflag.FlagSet) {
   110  	fs.Bool(VersionKey, false, "Print the version and exit")
   111  	fs.String(ConfigFilePathKey, "", "Specify the config path to use to load a YAML config for the simulator")
   112  	fs.StringSlice(EndpointsKey, []string{"ws://127.0.0.1:9650/ext/bc/C/ws"}, "Specify a comma separated list of RPC Websocket Endpoints (minimum of 1 endpoint)")
   113  	fs.Int64(MaxFeeCapKey, 50, "Specify the maximum fee cap to use for transactions denominated in GWei (must be > 0)")
   114  	fs.Int64(MaxTipCapKey, 1, "Specify the max tip cap for transactions denominated in GWei (must be >= 0)")
   115  	fs.Uint64(TxsPerWorkerKey, 100, "Specify the number of transactions to create per worker (must be > 0)")
   116  	fs.Int(WorkersKey, 1, "Specify the number of workers to create for the simulator (must be > 0)")
   117  	fs.String(KeyDirKey, ".simulator/keys", "Specify the directory to save private keys in (INSECURE: only use for testing)")
   118  	fs.Duration(TimeoutKey, 5*time.Minute, "Specify the timeout for the simulator to complete (0 indicates no timeout)")
   119  	fs.String(LogLevelKey, "info", "Specify the log level to use in the simulator")
   120  	fs.Uint64(BatchSizeKey, 100, "Specify the batchsize for the worker to issue and confirm txs")
   121  }