core/genesis: enhance DeveloperGenesisBlock to generate deterministic dev accounts with private keys

This commit is contained in:
Arya Nair 2025-05-25 22:26:11 +05:30
parent 4c76aa016d
commit 5b9918fe99

View file

@ -24,6 +24,9 @@ import (
"math/big"
"strings"
"crypto/ecdsa"
"encoding/hex"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
@ -646,40 +649,73 @@ func DefaultHoodiGenesisBlock() *Genesis {
// DeveloperGenesisBlock returns the 'geth --dev' genesis block.
func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
// Override the default period to the user requested one
config := *params.AllDevChainProtocolChanges
prefunded_accounts := 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
// Generate 10 deterministic dev accounts
type DevAccount struct {
Address common.Address
PrivateKey *ecdsa.PrivateKey
Balance *big.Int
}
var devAccounts []DevAccount
for i := 0; i < 10; i++ {
seed := []byte("dev-account-seed-")
seed = append(seed, byte(i))
key, _ := crypto.ToECDSA(crypto.Keccak256(seed))
addr := crypto.PubkeyToAddress(key.PublicKey)
devAccounts = append(devAccounts, DevAccount{
Address: addr,
PrivateKey: key,
Balance: new(big.Int).Mul(big.NewInt(10000), big.NewInt(1e18)), // 10000 ETH
})
}
alloc := make(map[common.Address]types.Account)
for addr, account := range prefunded_accounts {
alloc[addr] = account
// Prefund dev accounts
for _, acct := range devAccounts {
alloc[acct.Address] = types.Account{Balance: acct.Balance}
}
// Add precompiles and system contracts
alloc[common.BytesToAddress([]byte{0x01})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x02})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x03})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x04})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x05})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x06})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x07})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x08})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x09})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0a})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0b})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0c})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0d})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0e})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0f})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x10})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x11})] = 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
if faucet != nil {
alloc[*faucet] = types.Account{Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))}
}
// Print accounts and keys
fmt.Println("Available Accounts")
fmt.Println("==================")
for i, acct := range devAccounts {
ethBal := new(big.Float).Quo(new(big.Float).SetInt(acct.Balance), big.NewFloat(1e18))
fmt.Printf("(%d) %s (%s ETH)\n", i, acct.Address.Hex(), ethBal.Text('f', 18))
}
fmt.Println()
fmt.Println("Private Keys")
fmt.Println("==================")
for i, acct := range devAccounts {
fmt.Printf("(%d) 0x%s\n", i, hex.EncodeToString(crypto.FromECDSA(acct.PrivateKey)))
}
genesis := &Genesis{
Config: &config,
GasLimit: gasLimit,
@ -687,9 +723,6 @@ func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
Difficulty: big.NewInt(0),
Alloc: alloc,
}
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
}