Skip to content

Commit

Permalink
cmd: add goat flags
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlee42 committed Dec 11, 2024
1 parent 2e0f469 commit 90934ff
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 7 deletions.
10 changes: 9 additions & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ var (
utils.BeaconGenesisRootFlag,
utils.BeaconGenesisTimeFlag,
utils.BeaconCheckpointFlag,
utils.GoatPresetFlag,
}, utils.NetworkFlags, utils.DatabaseFlags)

rpcFlags = []cli.Flag{
Expand Down Expand Up @@ -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...")
}
Expand All @@ -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))
Expand Down
63 changes: 57 additions & 6 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
70 changes: 70 additions & 0 deletions cmd/utils/goat_flags.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 90934ff

Please sign in to comment.