From 90934ff15599a1bf6a4a3433e1da203308fdf99f Mon Sep 17 00:00:00 2001 From: ericlee Date: Wed, 11 Dec 2024 17:55:14 +0800 Subject: [PATCH] cmd: add goat flags --- cmd/geth/main.go | 10 +++++- cmd/utils/flags.go | 63 +++++++++++++++++++++++++++++++++---- cmd/utils/goat_flags.go | 70 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 cmd/utils/goat_flags.go diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 9d9256862b632..a7e39432d7bbb 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -154,6 +154,7 @@ var ( utils.BeaconGenesisRootFlag, utils.BeaconGenesisTimeFlag, utils.BeaconCheckpointFlag, + utils.GoatPresetFlag, }, utils.NetworkFlags, utils.DatabaseFlags) rpcFlags = []cli.Flag{ @@ -310,6 +311,12 @@ func prepare(ctx *cli.Context) { to 0, and discovery is disabled. `) + case ctx.IsSet(utils.GoatNetworkFlag.Name): + log.Info("Starting Geth on GOAT network...", "network", ctx.String(utils.GoatNetworkFlag.Name)) + if ctx.String(utils.GoatNetworkFlag.Name) == "mainnet" { + log.Info("Bumping default cache on mainnet to 4096") + ctx.Set(utils.CacheFlag.Name, strconv.Itoa(4096)) + } case !ctx.IsSet(utils.NetworkIdFlag.Name): log.Info("Starting Geth on Ethereum mainnet...") } @@ -318,7 +325,8 @@ func prepare(ctx *cli.Context) { // Make sure we're not on any supported preconfigured testnet either if !ctx.IsSet(utils.HoleskyFlag.Name) && !ctx.IsSet(utils.SepoliaFlag.Name) && - !ctx.IsSet(utils.DeveloperFlag.Name) { + !ctx.IsSet(utils.DeveloperFlag.Name) && + !ctx.IsSet(utils.GoatNetworkFlag.Name) { // Nope, we're really on mainnet. Bump that cache up! log.Info("Bumping default cache on mainnet", "provided", ctx.Int(utils.CacheFlag.Name), "updated", 4096) ctx.Set(utils.CacheFlag.Name, strconv.Itoa(4096)) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 8ef39e88dea3d..16145aa6035e7 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -334,6 +334,7 @@ var ( Name: "txpool.nolocals", Usage: "Disables price exemptions for locally submitted transactions", Category: flags.TxPoolCategory, + Value: true, } TxPoolJournalFlag = &cli.StringFlag{ Name: "txpool.journal", @@ -774,7 +775,7 @@ var ( Aliases: []string{"discv4"}, Usage: "Enables the V4 discovery mechanism", Category: flags.NetworkingCategory, - Value: true, + Value: false, } DiscoveryV5Flag = &cli.BoolFlag{ Name: "discovery.v5", @@ -937,7 +938,7 @@ var ( HoleskyFlag, } // NetworkFlags is the flag group of all built-in supported networks. - NetworkFlags = append([]cli.Flag{MainnetFlag}, TestnetFlags...) + NetworkFlags = append([]cli.Flag{MainnetFlag, GoatNetworkFlag}, TestnetFlags...) // DatabaseFlags is the flag group of all database flags. DatabaseFlags = []cli.Flag{ @@ -1050,6 +1051,14 @@ func setBootstrapNodesV5(ctx *cli.Context, cfg *p2p.Config) { urls = SplitAndTrim(ctx.String(BootnodesFlag.Name)) case cfg.BootstrapNodesV5 != nil: return // already set, don't apply defaults. + case ctx.IsSet(GoatNetworkFlag.Name): + goatNetwork := ctx.String(GoatNetworkFlag.Name) + switch goatNetwork { + case params.GoatTestnet3Name: + urls = params.V5GoatTestnet3Bootnodes + case params.GoatMainnetName: + urls = params.V5GoatMainnetBootnodes + } } cfg.BootstrapNodesV5 = make([]*enode.Node, 0, len(urls)) @@ -1084,6 +1093,8 @@ func setNAT(ctx *cli.Context, cfg *p2p.Config) { Fatalf("Option %s: %v", NATFlag.Name, err) } cfg.NAT = natif + } else { + useGoatBootnodePreset(ctx, cfg) } } @@ -1153,6 +1164,23 @@ func setHTTP(ctx *cli.Context, cfg *node.Config) { if ctx.IsSet(BatchResponseMaxSize.Name) { cfg.BatchResponseMaxSize = ctx.Int(BatchResponseMaxSize.Name) } + + if isGoatPreset(ctx, "rpc") { + defaultModules := []string{"web3", "eth", "net"} + if ctx.String(GCModeFlag.Name) == "archive" { + defaultModules = append(defaultModules, "debug", "txpool") + } + publicIF, allOrigin := "0.0.0.0", []string{"*"} + + cfg.HTTPHost = publicIF + cfg.HTTPModules = defaultModules + cfg.HTTPVirtualHosts = allOrigin + cfg.HTTPCors = allOrigin + + cfg.WSHost = publicIF + cfg.WSModules = defaultModules + cfg.WSOrigins = allOrigin + } } // setGraphQL creates the GraphQL listener interface string from the set @@ -1572,7 +1600,7 @@ func CheckExclusive(ctx *cli.Context, args ...interface{}) { // SetEthConfig applies eth-related command line flags to the config. func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { // Avoid conflicting network flags - CheckExclusive(ctx, MainnetFlag, DeveloperFlag, SepoliaFlag, HoleskyFlag) + CheckExclusive(ctx, MainnetFlag, DeveloperFlag, SepoliaFlag, HoleskyFlag, GoatNetworkFlag) CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer // Set configurations from CLI flags @@ -1655,9 +1683,16 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { log.Warn("The flag --txlookuplimit is deprecated and will be removed, please use --history.transactions") cfg.TransactionHistory = ctx.Uint64(TxLookupLimitFlag.Name) } - if ctx.String(GCModeFlag.Name) == "archive" && cfg.TransactionHistory != 0 { - cfg.TransactionHistory = 0 - log.Warn("Disabled transaction unindexing for archive node") + if cfg.NoPruning { + if cfg.TransactionHistory != 0 { + cfg.TransactionHistory = 0 + log.Warn("Disabled transaction unindexing for archive node") + } + + if cfg.StateHistory != 0 { + cfg.StateHistory = 0 + log.Warn("Disabled state pruning for archive node") + } cfg.StateScheme = rawdb.HashScheme log.Warn("Forcing hash state-scheme for archive mode") @@ -1738,6 +1773,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { } cfg.Genesis = core.DefaultSepoliaGenesisBlock() SetDNSDiscoveryDefaults(cfg, params.SepoliaGenesisHash) + case ctx.IsSet(GoatNetworkFlag.Name): + genesis := MakeGenesis(ctx) + if !ctx.IsSet(NetworkIdFlag.Name) { + cfg.NetworkId = genesis.Config.ChainID.Uint64() + } + cfg.Genesis = genesis case ctx.Bool(DeveloperFlag.Name): if !ctx.IsSet(NetworkIdFlag.Name) { cfg.NetworkId = 1337 @@ -2109,6 +2150,16 @@ func DialRPCWithHeaders(endpoint string, headers []string) (*rpc.Client, error) func MakeGenesis(ctx *cli.Context) *core.Genesis { var genesis *core.Genesis switch { + case ctx.IsSet(GoatNetworkFlag.Name): + netwk := ctx.String(GoatNetworkFlag.Name) + switch netwk { + case params.GoatTestnet3Name: + genesis = core.DefaultGoatTestnet3GenesisBlock() + case params.GoatMainnetName: + genesis = core.DefaultGoatMainnetGenesisBlock() + default: + Fatalf("unknown goat network: %s", netwk) + } case ctx.Bool(MainnetFlag.Name): genesis = core.DefaultGenesisBlock() case ctx.Bool(HoleskyFlag.Name): diff --git a/cmd/utils/goat_flags.go b/cmd/utils/goat_flags.go new file mode 100644 index 0000000000000..3ff82fd4ad3e9 --- /dev/null +++ b/cmd/utils/goat_flags.go @@ -0,0 +1,70 @@ +package utils + +import ( + "io" + "net/http" + "slices" + "strings" + + "github.com/ethereum/go-ethereum/internal/flags" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/p2p/nat" + "github.com/urfave/cli/v2" +) + +var ( + GoatNetworkFlag = &cli.StringFlag{ + Name: "goat", + Usage: "Run goat network", + Category: flags.EthCategory, + } + + GoatPresetFlag = &cli.StringFlag{ + Name: "goat.preset", + Usage: "Goat node preset", + Category: flags.NetworkingCategory, + } +) + +func getPublicIP() (string, error) { + resp, err := http.Get("https://checkip.amazonaws.com") + if err != nil { + return "", err + } + defer resp.Body.Close() + + data, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + return strings.TrimSpace(string(data)), nil +} + +func useGoatBootnodePreset(ctx *cli.Context, cfg *p2p.Config) { + if !isGoatPreset(ctx, "bootnode") { + return + } + + ip, err := getPublicIP() + if err != nil { + log.Error("Failed to fetch external ip", "err", err.Error()) + return + } + log.Info("Set external public IP", "ip", ip) + natif, err := nat.Parse("extip:" + ip) + if err != nil { + Fatalf("Failed to parse external ip", "err", err.Error()) + } + cfg.NAT = natif + cfg.MaxPeers = 200 +} + +func isGoatPreset(ctx *cli.Context, preset string) bool { + if !ctx.IsSet(GoatPresetFlag.Name) { + return false + } + + presets := strings.Split(ctx.String(GoatPresetFlag.Name), ",") + return slices.Contains(presets, preset) +}