core/chain_makers: add SetParentBeaconRoot(..) to chain makers (#28252)

This commit is contained in:
lightclient 2023-10-17 06:43:46 -06:00 committed by GitHub
parent b85c86022e
commit 20d5256e40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 3 deletions

View File

@ -88,6 +88,17 @@ func (b *BlockGen) SetPoS() {
b.header.Difficulty = new(big.Int)
}
// SetParentBeaconRoot sets the parent beacon root field of the generated
// block.
func (b *BlockGen) SetParentBeaconRoot(root common.Hash) {
b.header.ParentBeaconRoot = &root
var (
blockContext = NewEVMBlockContext(b.header, nil, &b.header.Coinbase)
vmenv = vm.NewEVM(blockContext, vm.TxContext{}, b.statedb, b.config, vm.Config{})
)
ProcessBeaconBlockRoot(root, vmenv, b.statedb)
}
// addTx adds a transaction to the generated block. If no coinbase has
// been set, the block's coinbase is set to the zero address.
//

View File

@ -32,7 +32,7 @@ import (
"github.com/ethereum/go-ethereum/trie"
)
func TestGenerateWithdrawalChain(t *testing.T) {
func TestGeneratePOSChain(t *testing.T) {
var (
keyHex = "9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c"
key, _ = crypto.HexToECDSA(keyHex)
@ -41,9 +41,13 @@ func TestGenerateWithdrawalChain(t *testing.T) {
bb = common.Address{0xbb}
funds = big.NewInt(0).Mul(big.NewInt(1337), big.NewInt(params.Ether))
config = *params.AllEthashProtocolChanges
asm4788 = common.Hex2Bytes("3373fffffffffffffffffffffffffffffffffffffffe14604d57602036146024575f5ffd5b5f35801560495762001fff810690815414603c575f5ffd5b62001fff01545f5260205ff35b5f5ffd5b62001fff42064281555f359062001fff015500")
gspec = &Genesis{
Config: &config,
Alloc: GenesisAlloc{address: {Balance: funds}},
Config: &config,
Alloc: GenesisAlloc{
address: {Balance: funds},
params.BeaconRootsStorageAddress: {Balance: common.Big0, Code: asm4788},
},
BaseFee: big.NewInt(params.InitialBaseFee),
Difficulty: common.Big1,
GasLimit: 5_000_000,
@ -56,6 +60,7 @@ func TestGenerateWithdrawalChain(t *testing.T) {
config.TerminalTotalDifficultyPassed = true
config.TerminalTotalDifficulty = common.Big0
config.ShanghaiTime = u64(0)
config.CancunTime = u64(0)
// init 0xaa with some storage elements
storage := make(map[common.Hash]common.Hash)
@ -78,6 +83,7 @@ func TestGenerateWithdrawalChain(t *testing.T) {
genesis := gspec.MustCommit(gendb, trie.NewDatabase(gendb, trie.HashDefaults))
chain, _ := GenerateChain(gspec.Config, genesis, beacon.NewFaker(), gendb, 4, func(i int, gen *BlockGen) {
gen.SetParentBeaconRoot(common.Hash{byte(i + 1)})
tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(address), address, big.NewInt(1000), params.TxGas, new(big.Int).Add(gen.BaseFee(), common.Big1), nil), signer, key)
gen.AddTx(tx)
if i == 1 {
@ -125,6 +131,8 @@ func TestGenerateWithdrawalChain(t *testing.T) {
if block == nil {
t.Fatalf("block %d not found", i)
}
// Verify withdrawals.
if len(block.Withdrawals()) == 0 {
continue
}
@ -134,6 +142,18 @@ func TestGenerateWithdrawalChain(t *testing.T) {
}
withdrawalIndex += 1
}
// Verify parent beacon root.
want := common.Hash{byte(i)}
if got := block.BeaconRoot(); *got != want {
t.Fatalf("block %d, wrong parent beacon root: got %s, want %s", i, got, want)
}
state, _ := blockchain.State()
idx := block.Time()%8191 + 8191
got := state.GetState(params.BeaconRootsStorageAddress, common.BigToHash(new(big.Int).SetUint64(idx)))
if got != want {
t.Fatalf("block %d, wrong parent beacon root in state: got %s, want %s", i, got, want)
}
}
}