From bbdf020448734d9cf82497d15d750dde68b3ab43 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Wed, 28 May 2025 11:32:51 +0800 Subject: [PATCH] rework --- cmd/geth/chaincmd.go | 2 +- cmd/geth/main.go | 16 +---------- cmd/utils/flags.go | 22 +++++++++++--- core/genesis.go | 68 +++++++++++++++++++++----------------------- 4 files changed, 53 insertions(+), 55 deletions(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 0d8154cdfd..dc071725c1 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -304,7 +304,7 @@ func dumpGenesis(ctx *cli.Context) error { if utils.IsNetworkPreset(ctx) { genesis = utils.MakeGenesis(ctx) } else if ctx.IsSet(utils.DeveloperFlag.Name) && !ctx.IsSet(utils.DataDirFlag.Name) { - genesis, _, _ = core.DeveloperGenesisBlock(11_500_000, nil) + genesis = core.DeveloperGenesisBlock(11_500_000, nil) } if genesis != nil { diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 9fceb09fab..b3cbb06be5 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -18,7 +18,6 @@ package main import ( - "encoding/hex" "fmt" "os" "slices" @@ -30,8 +29,6 @@ import ( "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/console/prompt" - "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/internal/debug" @@ -331,6 +328,7 @@ func prepare(ctx *cli.Context) { } } } + // geth is the main entry point into the system if no special subcommand is run. // It creates a default node based on the command line arguments and runs it in // blocking mode, waiting for it to be shut down. @@ -344,18 +342,6 @@ func geth(ctx *cli.Context) error { defer stack.Close() startNode(ctx, stack, false) - - if ctx.IsSet(utils.DeveloperFlag.Name) && !ctx.IsSet(utils.DataDirFlag.Name) { - _, faucetAddr, faucetKey := core.DeveloperGenesisBlock(0, nil) - fmt.Println("Developer Faucet Account") - fmt.Println("========================") - fmt.Printf("Address: %s\n", faucetAddr.Hex()) - if faucetKey != nil { - fmt.Printf("Private Key: 0x%s\n", hex.EncodeToString(crypto.FromECDSA(faucetKey))) - } - fmt.Println() - } - stack.Wait() return nil diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 3d550dfdec..029f7c846e 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1787,13 +1787,11 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { log.Info("Using developer account", "address", developer.Address) // Create a new developer genesis block or reuse existing one - cfg.Genesis, _, _ = core.DeveloperGenesisBlock(ctx.Uint64(DeveloperGasLimitFlag.Name), &developer.Address) if ctx.IsSet(DataDirFlag.Name) { chaindb := tryMakeReadOnlyDatabase(ctx, stack) if rawdb.ReadCanonicalHash(chaindb, 0) != (common.Hash{}) { - cfg.Genesis = nil // fallback to db content - - //validate genesis has PoS enabled in block 0 + // validate that the stored genesis config compatible with dev-mode: + // it must be merged at genesis genesis, err := core.ReadGenesis(chaindb) if err != nil { Fatalf("Could not read genesis from database: %v", err) @@ -1808,6 +1806,22 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { } } chaindb.Close() + } else { + // if no datadir is specified, dev mode runs ephemerally in memory + var ( + devAddr = developer.Address + devKey *ecdsa.PrivateKey + ) + if devAddr != (common.Address{}) { + devKey, err = crypto.GenerateKey() + if err != nil { + panic(fmt.Errorf("failed to generate dev mode prefunded account key: %v", err)) + } + devAddr = crypto.PubkeyToAddress(devKey.PublicKey) + + log.Info("running dev mode with prefunded account", "address", devAddr, "private key", crypto.FromECDSA(devKey)) + } + cfg.Genesis = core.DeveloperGenesisBlock(ctx.Uint64(DeveloperGasLimitFlag.Name), &devAddr) } if !ctx.IsSet(MinerGasPriceFlag.Name) { cfg.Miner.GasPrice = big.NewInt(1) diff --git a/core/genesis.go b/core/genesis.go index 6bb2adf469..47b9055197 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -24,8 +24,6 @@ import ( "math/big" "strings" - "crypto/ecdsa" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/math" @@ -646,47 +644,47 @@ func DefaultHoodiGenesisBlock() *Genesis { } } -// DeveloperGenesisBlock returns the 'geth --dev' genesis block, faucet address, and key. -func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) (*Genesis, common.Address, *ecdsa.PrivateKey) { +// DeveloperGenesisBlock returns the genesis block for dev mode, with initial +// gas limit and optional prefunded address +func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis { + // Override the default period to the user requested one config := *params.AllDevChainProtocolChanges - alloc := make(map[common.Address]types.Account) - - var faucetAddr common.Address - var faucetKey *ecdsa.PrivateKey - if faucet == nil { - // Generate a new dev account for the faucet - key, err := crypto.GenerateKey() - if err != nil { - panic(fmt.Errorf("failed to generate dev faucet key: %w", err)) - } - faucetAddr = crypto.PubkeyToAddress(key.PublicKey) - faucetKey = key - } else { - faucetAddr = *faucet - } - alloc[faucetAddr] = types.Account{Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))} - - // Add precompiles and system contracts - precompileAddrs := [][]byte{ - {0x01}, {0x02}, {0x03}, {0x04}, {0x05}, {0x06}, {0x07}, {0x08}, {0x09}, {0x0a}, {0x0b}, {0x0c}, {0x0d}, {0x0e}, {0x0f}, {0x10}, {0x11}, - } - for _, addr := range precompileAddrs { - alloc[common.BytesToAddress(addr)] = types.Account{Balance: big.NewInt(1)} - } - alloc[params.BeaconRootsAddress] = types.Account{Nonce: 1, Code: params.BeaconRootsCode, Balance: common.Big0} - alloc[params.HistoryStorageAddress] = types.Account{Nonce: 1, Code: params.HistoryStorageCode, Balance: common.Big0} - alloc[params.WithdrawalQueueAddress] = types.Account{Nonce: 1, Code: params.WithdrawalQueueCode, Balance: common.Big0} - alloc[params.ConsolidationQueueAddress] = types.Account{Nonce: 1, Code: params.ConsolidationQueueCode, Balance: common.Big0} - + // Assemble and return the genesis with the precompiles and faucet pre-funded genesis := &Genesis{ Config: &config, GasLimit: gasLimit, BaseFee: big.NewInt(params.InitialBaseFee), Difficulty: big.NewInt(0), - Alloc: alloc, + Alloc: map[common.Address]types.Account{ + common.BytesToAddress([]byte{0x01}): {Balance: big.NewInt(1)}, // ECRecover + common.BytesToAddress([]byte{0x02}): {Balance: big.NewInt(1)}, // SHA256 + common.BytesToAddress([]byte{0x03}): {Balance: big.NewInt(1)}, // RIPEMD + common.BytesToAddress([]byte{0x04}): {Balance: big.NewInt(1)}, // Identity + common.BytesToAddress([]byte{0x05}): {Balance: big.NewInt(1)}, // ModExp + common.BytesToAddress([]byte{0x06}): {Balance: big.NewInt(1)}, // ECAdd + common.BytesToAddress([]byte{0x07}): {Balance: big.NewInt(1)}, // ECScalarMul + common.BytesToAddress([]byte{0x08}): {Balance: big.NewInt(1)}, // ECPairing + common.BytesToAddress([]byte{0x09}): {Balance: big.NewInt(1)}, // BLAKE2b + common.BytesToAddress([]byte{0x0a}): {Balance: big.NewInt(1)}, // KZGPointEval + common.BytesToAddress([]byte{0x0b}): {Balance: big.NewInt(1)}, // BLSG1Add + common.BytesToAddress([]byte{0x0c}): {Balance: big.NewInt(1)}, // BLSG1MultiExp + common.BytesToAddress([]byte{0x0d}): {Balance: big.NewInt(1)}, // BLSG2Add + common.BytesToAddress([]byte{0x0e}): {Balance: big.NewInt(1)}, // BLSG2MultiExp + common.BytesToAddress([]byte{0x0f}): {Balance: big.NewInt(1)}, // BLSG1Pairing + common.BytesToAddress([]byte{0x10}): {Balance: big.NewInt(1)}, // BLSG1MapG1 + common.BytesToAddress([]byte{0x11}): {Balance: big.NewInt(1)}, // BLSG2MapG2 + // Pre-deploy system contracts + params.BeaconRootsAddress: {Nonce: 1, Code: params.BeaconRootsCode, Balance: common.Big0}, + params.HistoryStorageAddress: {Nonce: 1, Code: params.HistoryStorageCode, Balance: common.Big0}, + params.WithdrawalQueueAddress: {Nonce: 1, Code: params.WithdrawalQueueCode, Balance: common.Big0}, + params.ConsolidationQueueAddress: {Nonce: 1, Code: params.ConsolidationQueueCode, Balance: common.Big0}, + }, } - return genesis, faucetAddr, faucetKey + if faucet != nil { + genesis.Alloc[*faucet] = types.Account{Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))} + } + return genesis } func decodePrealloc(data string) types.GenesisAlloc {