flags.go 4.26 KB
Newer Older
1 2 3
package flags

import (
4 5 6
	"fmt"
	"strings"

7
	"github.com/urfave/cli/v2"
8

9
	"github.com/ethereum-optimism/optimism/op-node/chaincfg"
10
	"github.com/ethereum-optimism/optimism/op-node/sources"
11
	service "github.com/ethereum-optimism/optimism/op-service"
12
	openum "github.com/ethereum-optimism/optimism/op-service/enum"
13 14 15
	oplog "github.com/ethereum-optimism/optimism/op-service/log"
)

16
const EnvVarPrefix = "OP_PROGRAM"
17

18
func prefixEnvVars(name string) []string {
19
	return service.PrefixEnvVar(EnvVarPrefix, name)
20 21
}

22
var (
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	RollupConfig = &cli.StringFlag{
		Name:    "rollup.config",
		Usage:   "Rollup chain parameters",
		EnvVars: prefixEnvVars("ROLLUP_CONFIG"),
	}
	Network = &cli.StringFlag{
		Name:    "network",
		Usage:   fmt.Sprintf("Predefined network selection. Available networks: %s", strings.Join(chaincfg.AvailableNetworks(), ", ")),
		EnvVars: prefixEnvVars("NETWORK"),
	}
	DataDir = &cli.StringFlag{
		Name:    "datadir",
		Usage:   "Directory to use for preimage data storage. Default uses in-memory storage",
		EnvVars: prefixEnvVars("DATADIR"),
	}
	L2NodeAddr = &cli.StringFlag{
		Name:    "l2",
		Usage:   "Address of L2 JSON-RPC endpoint to use (eth and debug namespace required)",
		EnvVars: prefixEnvVars("L2_RPC"),
	}
	L1Head = &cli.StringFlag{
		Name:    "l1.head",
		Usage:   "Hash of the L1 head block. Derivation stops after this block is processed.",
		EnvVars: prefixEnvVars("L1_HEAD"),
	}
	L2Head = &cli.StringFlag{
		Name:    "l2.head",
		Usage:   "Hash of the agreed L2 block to start derivation from",
		EnvVars: prefixEnvVars("L2_HEAD"),
	}
	L2Claim = &cli.StringFlag{
		Name:    "l2.claim",
		Usage:   "Claimed L2 output root to validate",
		EnvVars: prefixEnvVars("L2_CLAIM"),
	}
	L2BlockNumber = &cli.Uint64Flag{
		Name:    "l2.blocknumber",
		Usage:   "Number of the L2 block that the claim is from",
		EnvVars: prefixEnvVars("L2_BLOCK_NUM"),
	}
	L2GenesisPath = &cli.StringFlag{
		Name:    "l2.genesis",
		Usage:   "Path to the op-geth genesis file",
		EnvVars: prefixEnvVars("L2_GENESIS"),
	}
	L1NodeAddr = &cli.StringFlag{
		Name:    "l1",
		Usage:   "Address of L1 JSON-RPC endpoint to use (eth namespace required)",
		EnvVars: prefixEnvVars("L1_RPC"),
	}
	L1TrustRPC = &cli.BoolFlag{
		Name:    "l1.trustrpc",
		Usage:   "Trust the L1 RPC, sync faster at risk of malicious/buggy RPC providing bad or inconsistent L1 data",
		EnvVars: prefixEnvVars("L1_TRUST_RPC"),
	}
	L1RPCProviderKind = &cli.GenericFlag{
79 80
		Name: "l1.rpckind",
		Usage: "The kind of RPC provider, used to inform optimal transactions receipts fetching, and thus reduce costs. Valid options: " +
81
			openum.EnumString(sources.RPCProviderKinds),
82
		EnvVars: prefixEnvVars("L1_RPC_KIND"),
83 84 85 86 87
		Value: func() *sources.RPCProviderKind {
			out := sources.RPCKindBasic
			return &out
		}(),
	}
88 89 90 91
	Exec = &cli.StringFlag{
		Name:    "exec",
		Usage:   "Run the specified client program as a separate process detached from the host. Default is to run the client program in the host process.",
		EnvVars: prefixEnvVars("EXEC"),
92
	}
93 94 95 96
	Server = &cli.BoolFlag{
		Name:    "server",
		Usage:   "Run in pre-image server mode without executing any client program.",
		EnvVars: prefixEnvVars("SERVER"),
97
	}
98 99
)

100 101 102
// Flags contains the list of configuration options available to the binary.
var Flags []cli.Flag

103 104 105 106
var requiredFlags = []cli.Flag{
	L1Head,
	L2Head,
	L2Claim,
107
	L2BlockNumber,
108
}
109 110 111
var programFlags = []cli.Flag{
	RollupConfig,
	Network,
112
	DataDir,
113
	L2NodeAddr,
114
	L2GenesisPath,
115 116 117
	L1NodeAddr,
	L1TrustRPC,
	L1RPCProviderKind,
118
	Exec,
119
	Server,
120 121
}

122
func init() {
123
	Flags = append(Flags, oplog.CLIFlags(EnvVarPrefix)...)
124
	Flags = append(Flags, requiredFlags...)
125 126 127 128
	Flags = append(Flags, programFlags...)
}

func CheckRequired(ctx *cli.Context) error {
129 130
	rollupConfig := ctx.String(RollupConfig.Name)
	network := ctx.String(Network.Name)
131 132 133 134 135 136
	if rollupConfig == "" && network == "" {
		return fmt.Errorf("flag %s or %s is required", RollupConfig.Name, Network.Name)
	}
	if rollupConfig != "" && network != "" {
		return fmt.Errorf("cannot specify both %s and %s", RollupConfig.Name, Network.Name)
	}
137
	if network == "" && ctx.String(L2GenesisPath.Name) == "" {
138 139
		return fmt.Errorf("flag %s is required for custom networks", L2GenesisPath.Name)
	}
140
	for _, flag := range requiredFlags {
141 142
		if !ctx.IsSet(flag.Names()[0]) {
			return fmt.Errorf("flag %s is required", flag.Names()[0])
143
		}
144
	}
145
	return nil
146
}