github.com/prysmaticlabs/prysm@v1.4.4/shared/cmd/flags.go (about)

     1  // Package cmd defines the command line flags for the shared utlities.
     2  package cmd
     3  
     4  import (
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/prysmaticlabs/prysm/shared/params"
     9  	"github.com/urfave/cli/v2"
    10  	"github.com/urfave/cli/v2/altsrc"
    11  )
    12  
    13  var (
    14  	// MinimalConfigFlag declares to use the minimal config for running Ethereum consensus.
    15  	MinimalConfigFlag = &cli.BoolFlag{
    16  		Name:  "minimal-config",
    17  		Usage: "Use minimal config with parameters as defined in the spec.",
    18  	}
    19  	// E2EConfigFlag declares to use a testing specific config for running Ethereum consensus in end-to-end testing.
    20  	E2EConfigFlag = &cli.BoolFlag{
    21  		Name:  "e2e-config",
    22  		Usage: "Use the E2E testing config, only for use within end-to-end testing.",
    23  	}
    24  	// RPCMaxPageSizeFlag defines the maximum numbers per page returned in RPC responses from this
    25  	// beacon node (default: 500).
    26  	RPCMaxPageSizeFlag = &cli.IntFlag{
    27  		Name:  "rpc-max-page-size",
    28  		Usage: "Max number of items returned per page in RPC responses for paginated endpoints.",
    29  	}
    30  	// VerbosityFlag defines the logrus configuration.
    31  	VerbosityFlag = &cli.StringFlag{
    32  		Name:  "verbosity",
    33  		Usage: "Logging verbosity (trace, debug, info=default, warn, error, fatal, panic)",
    34  		Value: "info",
    35  	}
    36  	// DataDirFlag defines a path on disk.
    37  	DataDirFlag = &cli.StringFlag{
    38  		Name:  "datadir",
    39  		Usage: "Data directory for the databases and keystore",
    40  		Value: DefaultDataDir(),
    41  	}
    42  	// EnableBackupWebhookFlag for users to trigger db backups via an HTTP webhook.
    43  	EnableBackupWebhookFlag = &cli.BoolFlag{
    44  		Name:  "enable-db-backup-webhook",
    45  		Usage: "Serve HTTP handler to initiate database backups. The handler is served on the monitoring port at path /db/backup.",
    46  	}
    47  	// BackupWebhookOutputDir to customize the output directory for db backups.
    48  	BackupWebhookOutputDir = &cli.StringFlag{
    49  		Name:  "db-backup-output-dir",
    50  		Usage: "Output directory for db backups",
    51  	}
    52  	// EnableTracingFlag defines a flag to enable p2p message tracing.
    53  	EnableTracingFlag = &cli.BoolFlag{
    54  		Name:  "enable-tracing",
    55  		Usage: "Enable request tracing.",
    56  	}
    57  	// TracingProcessNameFlag defines a flag to specify a process name.
    58  	TracingProcessNameFlag = &cli.StringFlag{
    59  		Name:  "tracing-process-name",
    60  		Usage: "The name to apply to tracing tag \"process_name\"",
    61  	}
    62  	// TracingEndpointFlag flag defines the http endpoint for serving traces to Jaeger.
    63  	TracingEndpointFlag = &cli.StringFlag{
    64  		Name:  "tracing-endpoint",
    65  		Usage: "Tracing endpoint defines where beacon chain traces are exposed to Jaeger.",
    66  		Value: "http://127.0.0.1:14268/api/traces",
    67  	}
    68  	// TraceSampleFractionFlag defines a flag to indicate what fraction of p2p
    69  	// messages are sampled for tracing.
    70  	TraceSampleFractionFlag = &cli.Float64Flag{
    71  		Name:  "trace-sample-fraction",
    72  		Usage: "Indicate what fraction of p2p messages are sampled for tracing.",
    73  		Value: 0.20,
    74  	}
    75  	// MonitoringHostFlag defines the host used to serve prometheus metrics.
    76  	MonitoringHostFlag = &cli.StringFlag{
    77  		Name:  "monitoring-host",
    78  		Usage: "Host used for listening and responding metrics for prometheus.",
    79  		Value: "127.0.0.1",
    80  	}
    81  	// DisableMonitoringFlag defines a flag to disable the metrics collection.
    82  	DisableMonitoringFlag = &cli.BoolFlag{
    83  		Name:  "disable-monitoring",
    84  		Usage: "Disable monitoring service.",
    85  	}
    86  	// NoDiscovery specifies whether we are running a local network and have no need for connecting
    87  	// to the bootstrap nodes in the cloud
    88  	NoDiscovery = &cli.BoolFlag{
    89  		Name:  "no-discovery",
    90  		Usage: "Enable only local network p2p and do not connect to cloud bootstrap nodes.",
    91  	}
    92  	// StaticPeers specifies a set of peers to connect to explicitly.
    93  	StaticPeers = &cli.StringSliceFlag{
    94  		Name:  "peer",
    95  		Usage: "Connect with this peer. This flag may be used multiple times.",
    96  	}
    97  	// BootstrapNode tells the beacon node which bootstrap node to connect to
    98  	BootstrapNode = &cli.StringSliceFlag{
    99  		Name:  "bootstrap-node",
   100  		Usage: "The address of bootstrap node. Beacon node will connect for peer discovery via DHT.  Multiple nodes can be passed by using the flag multiple times but not comma-separated. You can also pass YAML files containing multiple nodes.",
   101  		Value: cli.NewStringSlice(params.BeaconNetworkConfig().BootstrapNodes...),
   102  	}
   103  	// RelayNode tells the beacon node which relay node to connect to.
   104  	RelayNode = &cli.StringFlag{
   105  		Name: "relay-node",
   106  		Usage: "The address of relay node. The beacon node will connect to the " +
   107  			"relay node and advertise their address via the relay node to other peers",
   108  		Value: "",
   109  	}
   110  	// P2PUDPPort defines the port to be used by discv5.
   111  	P2PUDPPort = &cli.IntFlag{
   112  		Name:  "p2p-udp-port",
   113  		Usage: "The port used by discv5.",
   114  		Value: 12000,
   115  	}
   116  	// P2PTCPPort defines the port to be used by libp2p.
   117  	P2PTCPPort = &cli.IntFlag{
   118  		Name:  "p2p-tcp-port",
   119  		Usage: "The port used by libp2p.",
   120  		Value: 13000,
   121  	}
   122  	// P2PIP defines the local IP to be used by libp2p.
   123  	P2PIP = &cli.StringFlag{
   124  		Name:  "p2p-local-ip",
   125  		Usage: "The local ip address to listen for incoming data.",
   126  		Value: "",
   127  	}
   128  	// P2PHost defines the host IP to be used by libp2p.
   129  	P2PHost = &cli.StringFlag{
   130  		Name:  "p2p-host-ip",
   131  		Usage: "The IP address advertised by libp2p. This may be used to advertise an external IP.",
   132  		Value: "",
   133  	}
   134  	// P2PHostDNS defines the host DNS to be used by libp2p.
   135  	P2PHostDNS = &cli.StringFlag{
   136  		Name:  "p2p-host-dns",
   137  		Usage: "The DNS address advertised by libp2p. This may be used to advertise an external DNS.",
   138  		Value: "",
   139  	}
   140  	// P2PPrivKey defines a flag to specify the location of the private key file for libp2p.
   141  	P2PPrivKey = &cli.StringFlag{
   142  		Name:  "p2p-priv-key",
   143  		Usage: "The file containing the private key to use in communications with other peers.",
   144  		Value: "",
   145  	}
   146  	// P2PMetadata defines a flag to specify the location of the peer metadata file.
   147  	P2PMetadata = &cli.StringFlag{
   148  		Name:  "p2p-metadata",
   149  		Usage: "The file containing the metadata to communicate with other peers.",
   150  		Value: "",
   151  	}
   152  	// P2PMaxPeers defines a flag to specify the max number of peers in libp2p.
   153  	P2PMaxPeers = &cli.IntFlag{
   154  		Name:  "p2p-max-peers",
   155  		Usage: "The max number of p2p peers to maintain.",
   156  		Value: 45,
   157  	}
   158  	// P2PAllowList defines a CIDR subnet to exclusively allow connections.
   159  	P2PAllowList = &cli.StringFlag{
   160  		Name: "p2p-allowlist",
   161  		Usage: "The CIDR subnet for allowing only certain peer connections. " +
   162  			"Using \"public\" would allow only public subnets. Example: " +
   163  			"192.168.0.0/16 would permit connections to peers on your local network only. The " +
   164  			"default is to accept all connections.",
   165  	}
   166  	// P2PDenyList defines a list of CIDR subnets to disallow connections from them.
   167  	P2PDenyList = &cli.StringSliceFlag{
   168  		Name: "p2p-denylist",
   169  		Usage: "The CIDR subnets for denying certainy peer connections. " +
   170  			"Using \"private\" would deny all private subnets. Example: " +
   171  			"192.168.0.0/16 would deny connections from peers on your local network only. The " +
   172  			"default is to accept all connections.",
   173  	}
   174  	// ForceClearDB removes any previously stored data at the data directory.
   175  	ForceClearDB = &cli.BoolFlag{
   176  		Name:  "force-clear-db",
   177  		Usage: "Clear any previously stored data at the data directory",
   178  	}
   179  	// ClearDB prompts user to see if they want to remove any previously stored data at the data directory.
   180  	ClearDB = &cli.BoolFlag{
   181  		Name:  "clear-db",
   182  		Usage: "Prompt for clearing any previously stored data at the data directory",
   183  	}
   184  	// LogFormat specifies the log output format.
   185  	LogFormat = &cli.StringFlag{
   186  		Name:  "log-format",
   187  		Usage: "Specify log formatting. Supports: text, json, fluentd, journald.",
   188  		Value: "text",
   189  	}
   190  	// MaxGoroutines specifies the maximum amount of goroutines tolerated, before a status check fails.
   191  	MaxGoroutines = &cli.IntFlag{
   192  		Name:  "max-goroutines",
   193  		Usage: "Specifies the upper limit of goroutines running before a status check fails",
   194  		Value: 5000,
   195  	}
   196  	// LogFileName specifies the log output file name.
   197  	LogFileName = &cli.StringFlag{
   198  		Name:  "log-file",
   199  		Usage: "Specify log file name, relative or absolute",
   200  	}
   201  	// EnableUPnPFlag specifies if UPnP should be enabled or not. The default value is false.
   202  	EnableUPnPFlag = &cli.BoolFlag{
   203  		Name:  "enable-upnp",
   204  		Usage: "Enable the service (Beacon chain or Validator) to use UPnP when possible.",
   205  	}
   206  	// ConfigFileFlag specifies the filepath to load flag values.
   207  	ConfigFileFlag = &cli.StringFlag{
   208  		Name:  "config-file",
   209  		Usage: "The filepath to a yaml file with flag values",
   210  	}
   211  	// ChainConfigFileFlag specifies the filepath to load flag values.
   212  	ChainConfigFileFlag = &cli.StringFlag{
   213  		Name:  "chain-config-file",
   214  		Usage: "The path to a YAML file with chain config values",
   215  	}
   216  	// GrpcMaxCallRecvMsgSizeFlag defines the max call message size for GRPC
   217  	GrpcMaxCallRecvMsgSizeFlag = &cli.IntFlag{
   218  		Name:  "grpc-max-msg-size",
   219  		Usage: "Integer to define max recieve message call size (default: 4194304 (for 4MB))",
   220  		Value: 1 << 22,
   221  	}
   222  	// AcceptTosFlag specifies user acceptance of ToS for non-interactive environments.
   223  	AcceptTosFlag = &cli.BoolFlag{
   224  		Name:  "accept-terms-of-use",
   225  		Usage: "Accept Terms and Conditions (for non-interactive environments)",
   226  	}
   227  	// RestoreSourceFileFlag specifies the filepath to the backed-up database file
   228  	// which will be used to restore the database.
   229  	RestoreSourceFileFlag = &cli.StringFlag{
   230  		Name:  "restore-source-file",
   231  		Usage: "Filepath to the backed-up database file which will be used to restore the database",
   232  	}
   233  	// RestoreTargetDirFlag specifies the target directory of the restored database.
   234  	RestoreTargetDirFlag = &cli.StringFlag{
   235  		Name:  "restore-target-dir",
   236  		Usage: "Target directory of the restored database",
   237  		Value: DefaultDataDir(),
   238  	}
   239  	// BoltMMapInitialSizeFlag specifies the initial size in bytes of boltdb's mmap syscall.
   240  	BoltMMapInitialSizeFlag = &cli.IntFlag{
   241  		Name:  "bolt-mmap-initial-size",
   242  		Usage: "Specifies the size in bytes of bolt db's mmap syscall allocation",
   243  		Value: 536870912, // 512 Mb as a default value.
   244  	}
   245  )
   246  
   247  // LoadFlagsFromConfig sets flags values from config file if ConfigFileFlag is set.
   248  func LoadFlagsFromConfig(cliCtx *cli.Context, flags []cli.Flag) error {
   249  	if cliCtx.IsSet(ConfigFileFlag.Name) {
   250  		if err := altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc(ConfigFileFlag.Name))(cliCtx); err != nil {
   251  			return err
   252  		}
   253  	}
   254  	return nil
   255  }
   256  
   257  // ValidateNoArgs insures that the application is not run with erroneous arguments or flags.
   258  // This function should be used in the app.Before, whenever the application supports a default command.
   259  func ValidateNoArgs(ctx *cli.Context) error {
   260  	commandList := ctx.App.Commands
   261  	parentCommand := ctx.Command
   262  	isParamForFlag := false
   263  	for _, a := range ctx.Args().Slice() {
   264  		// We don't validate further if
   265  		// the following value is actually
   266  		// a parameter for a flag.
   267  		if isParamForFlag {
   268  			isParamForFlag = false
   269  			continue
   270  		}
   271  		if strings.HasPrefix(a, "-") || strings.HasPrefix(a, "--") {
   272  			// In the event our flag doesn't specify
   273  			// the relevant argument with an equal
   274  			// sign, we can assume the next argument
   275  			// is the relevant value for the flag.
   276  			flagName := strings.TrimPrefix(a, "--")
   277  			flagName = strings.TrimPrefix(flagName, "-")
   278  			if !strings.Contains(a, "=") && !isBoolFlag(parentCommand, flagName) {
   279  				isParamForFlag = true
   280  			}
   281  			continue
   282  		}
   283  		c := checkCommandList(commandList, a)
   284  		if c == nil {
   285  			return fmt.Errorf("unrecognized argument: %s", a)
   286  		}
   287  		// Set the command list as the subcommand's
   288  		// from the current selected parent command.
   289  		commandList = c.Subcommands
   290  		parentCommand = c
   291  	}
   292  	return nil
   293  }
   294  
   295  // verifies that the provided command is in the command list.
   296  func checkCommandList(commands []*cli.Command, name string) *cli.Command {
   297  	for _, c := range commands {
   298  		if c.Name == name {
   299  			return c
   300  		}
   301  	}
   302  	return nil
   303  }
   304  
   305  func isBoolFlag(com *cli.Command, name string) bool {
   306  	for _, f := range com.Flags {
   307  		switch bFlag := f.(type) {
   308  		case *cli.BoolFlag:
   309  			if bFlag.Name == name {
   310  				return true
   311  			}
   312  		case *altsrc.BoolFlag:
   313  			if bFlag.Name == name {
   314  				return true
   315  			}
   316  		}
   317  	}
   318  	return false
   319  }