Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'chain' flag to start node from custom genesis.json without init cmd #382

Open
wants to merge 1 commit into
base: optimism
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ var (
utils.BeaconGenesisTimeFlag,
utils.BeaconCheckpointFlag,
utils.CollectWitnessFlag,
utils.CustomChainFlag,
}, utils.NetworkFlags, utils.DatabaseFlags)

rpcFlags = []cli.Flag{
Expand Down
60 changes: 60 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,11 @@ var (
Usage: "Enable state witness generation during block execution. Work in progress flag, don't use.",
Category: flags.MiscCategory,
}
CustomChainFlag = &cli.StringFlag{
Name: "chain",
Usage: "Path to a custom chain specification file",
Category: flags.EthCategory,
}

// MISC settings
SyncTargetFlag = &cli.StringFlag{
Expand Down Expand Up @@ -1619,6 +1624,12 @@ func SetDataDir(ctx *cli.Context, cfg *node.Config) {
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "holesky")
case ctx.IsSet(OPNetworkFlag.Name) && cfg.DataDir == node.DefaultDataDir():
cfg.DataDir = filepath.Join(node.DefaultDataDir(), ctx.String(OPNetworkFlag.Name))
case ctx.IsSet(CustomChainFlag.Name) && cfg.DataDir == node.DefaultDataDir():
genesis, err := readGenesisFromPath(ctx.String(CustomChainFlag.Name))
if err != nil {
Fatalf("Failed to read genesis file: %v", err)
}
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "custom-"+genesis.Config.ChainID.String())
}
}

Expand Down Expand Up @@ -2081,6 +2092,15 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
cfg.NetworkId = genesis.Config.ChainID.Uint64()
}
cfg.Genesis = genesis

case ctx.IsSet(CustomChainFlag.Name):
genesis, _, err := initJSONChainGenesis(ctx, stack, ctx.String(CustomChainFlag.Name))
if err != nil {
Fatalf("Failed to initialize custom chain: %v", err)
}
cfg.Genesis = genesis
cfg.NetworkId = genesis.Config.ChainID.Uint64()

default:
if cfg.NetworkId == 1 {
SetDNSDiscoveryDefaults(cfg, params.MainnetGenesisHash)
Expand Down Expand Up @@ -2459,3 +2479,43 @@ func MakeTrieDatabase(ctx *cli.Context, disk ethdb.Database, preimage bool, read
}
return triedb.NewDatabase(disk, config)
}

func readGenesisFromPath(genesisPath string) (*core.Genesis, error) {
file, err := os.Open(genesisPath)
if err != nil {
return nil, err
}
defer file.Close()

genesis := new(core.Genesis)
if err := json.NewDecoder(file).Decode(genesis); err != nil {
return nil, err
}
return genesis, nil
}

// initJSONChainGenesis based on the init command
func initJSONChainGenesis(ctx *cli.Context, stack *node.Node, genesisPath string) (*core.Genesis, common.Hash, error) {
genesis, err := readGenesisFromPath(genesisPath)
if err != nil {
return nil, common.Hash{}, err
}

var overrides core.ChainOverrides
chaindb, err := stack.OpenDatabaseWithFreezer("chaindata", 0, 0, ctx.String(AncientFlag.Name), "", false)
if err != nil {
return nil, common.Hash{}, err
}
defer chaindb.Close()

triedb := MakeTrieDatabase(ctx, chaindb, ctx.Bool(CachePreimagesFlag.Name), false, genesis.IsVerkle())
defer triedb.Close()

_, hash, err := core.SetupGenesisBlockWithOverride(chaindb, triedb, genesis, &overrides)
if err != nil {
return nil, common.Hash{}, err
}

log.Info("Successfully wrote genesis state", "database", "chaindata", "hash", hash)
return genesis, hash, nil
}