mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 22:26:42 +00:00
core/genesis: enhance DeveloperGenesisBlock to generate deterministic dev accounts with private keys
This commit is contained in:
parent
4c76aa016d
commit
5b9918fe99
1 changed files with 59 additions and 26 deletions
|
|
@ -24,6 +24,9 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"encoding/hex"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/ethereum/go-ethereum/common/math"
|
"github.com/ethereum/go-ethereum/common/math"
|
||||||
|
|
@ -646,40 +649,73 @@ func DefaultHoodiGenesisBlock() *Genesis {
|
||||||
|
|
||||||
// DeveloperGenesisBlock returns the 'geth --dev' genesis block.
|
// DeveloperGenesisBlock returns the 'geth --dev' genesis block.
|
||||||
func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
|
func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
|
||||||
// Override the default period to the user requested one
|
|
||||||
config := *params.AllDevChainProtocolChanges
|
config := *params.AllDevChainProtocolChanges
|
||||||
|
|
||||||
prefunded_accounts := map[common.Address]types.Account{
|
// Generate 10 deterministic dev accounts
|
||||||
common.BytesToAddress([]byte{0x01}): {Balance: big.NewInt(1)}, // ECRecover
|
type DevAccount struct {
|
||||||
common.BytesToAddress([]byte{0x02}): {Balance: big.NewInt(1)}, // SHA256
|
Address common.Address
|
||||||
common.BytesToAddress([]byte{0x03}): {Balance: big.NewInt(1)}, // RIPEMD
|
PrivateKey *ecdsa.PrivateKey
|
||||||
common.BytesToAddress([]byte{0x04}): {Balance: big.NewInt(1)}, // Identity
|
Balance *big.Int
|
||||||
common.BytesToAddress([]byte{0x05}): {Balance: big.NewInt(1)}, // ModExp
|
}
|
||||||
common.BytesToAddress([]byte{0x06}): {Balance: big.NewInt(1)}, // ECAdd
|
var devAccounts []DevAccount
|
||||||
common.BytesToAddress([]byte{0x07}): {Balance: big.NewInt(1)}, // ECScalarMul
|
for i := 0; i < 10; i++ {
|
||||||
common.BytesToAddress([]byte{0x08}): {Balance: big.NewInt(1)}, // ECPairing
|
seed := []byte("dev-account-seed-")
|
||||||
common.BytesToAddress([]byte{0x09}): {Balance: big.NewInt(1)}, // BLAKE2b
|
seed = append(seed, byte(i))
|
||||||
common.BytesToAddress([]byte{0x0a}): {Balance: big.NewInt(1)}, // KZGPointEval
|
key, _ := crypto.ToECDSA(crypto.Keccak256(seed))
|
||||||
common.BytesToAddress([]byte{0x0b}): {Balance: big.NewInt(1)}, // BLSG1Add
|
addr := crypto.PubkeyToAddress(key.PublicKey)
|
||||||
common.BytesToAddress([]byte{0x0c}): {Balance: big.NewInt(1)}, // BLSG1MultiExp
|
devAccounts = append(devAccounts, DevAccount{
|
||||||
common.BytesToAddress([]byte{0x0d}): {Balance: big.NewInt(1)}, // BLSG2Add
|
Address: addr,
|
||||||
common.BytesToAddress([]byte{0x0e}): {Balance: big.NewInt(1)}, // BLSG2MultiExp
|
PrivateKey: key,
|
||||||
common.BytesToAddress([]byte{0x0f}): {Balance: big.NewInt(1)}, // BLSG1Pairing
|
Balance: new(big.Int).Mul(big.NewInt(10000), big.NewInt(1e18)), // 10000 ETH
|
||||||
common.BytesToAddress([]byte{0x10}): {Balance: big.NewInt(1)}, // BLSG1MapG1
|
})
|
||||||
common.BytesToAddress([]byte{0x11}): {Balance: big.NewInt(1)}, // BLSG2MapG2
|
|
||||||
}
|
}
|
||||||
|
|
||||||
alloc := make(map[common.Address]types.Account)
|
alloc := make(map[common.Address]types.Account)
|
||||||
for addr, account := range prefunded_accounts {
|
// Prefund dev accounts
|
||||||
alloc[addr] = account
|
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.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.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.WithdrawalQueueAddress] = types.Account{Nonce: 1, Code: params.WithdrawalQueueCode, Balance: common.Big0}
|
||||||
alloc[params.ConsolidationQueueAddress] = types.Account{Nonce: 1, Code: params.ConsolidationQueueCode, 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{
|
genesis := &Genesis{
|
||||||
Config: &config,
|
Config: &config,
|
||||||
GasLimit: gasLimit,
|
GasLimit: gasLimit,
|
||||||
|
|
@ -687,9 +723,6 @@ func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
|
||||||
Difficulty: big.NewInt(0),
|
Difficulty: big.NewInt(0),
|
||||||
Alloc: alloc,
|
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
|
return genesis
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue