Skip to content

Commit

Permalink
Merge pull request #19 from flashbots/ferranbt/custom-chain-json-genesis
Browse files Browse the repository at this point in the history
Start with a custom json chain without init command
  • Loading branch information
ferranbt authored Sep 3, 2024
2 parents df7b346 + dae135c commit 7bdc742
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ var (
utils.BeaconGenesisTimeFlag,
utils.BeaconCheckpointFlag,
utils.CollectWitnessFlag,
utils.CustomChainFlag,
}, utils.NetworkFlags, utils.DatabaseFlags)

builderApiFlags = []cli.Flag{
Expand Down
64 changes: 64 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,11 @@ var (
Value: builder.RetryIntervalDefault.String(),
Category: flags.BuilderCategory,
}
CustomChainFlag = &cli.StringFlag{
Name: "chain",
Usage: "Path to a custom chain specification file",
Category: flags.EthCategory,
}

// RPC settings
IPCDisabledFlag = &cli.BoolFlag{
Expand Down Expand Up @@ -1678,6 +1683,13 @@ 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 @@ -2135,6 +2147,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 @@ -2521,3 +2542,46 @@ 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
// overrides.OverrideCancun = &genesis.Config.Cancun
// overrides.OverrideVerkle = &genesis.Config.Verkle

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
}

0 comments on commit 7bdc742

Please sign in to comment.