go-ethereum/miner/payload_building_test.go
rjl493456442 4d2181aa41
Some checks are pending
/ Linux Build (push) Waiting to run
/ Linux Build (arm) (push) Waiting to run
/ Keeper Build (push) Waiting to run
/ Windows Build (push) Waiting to run
/ Docker Image (push) Waiting to run
cmd, core, eth, miner: apply 7997 in block building (#35285)
This PR applies the 7997 irregular state transition in t8n, block
building, simulation and tracing.
2026-07-07 14:40:25 +02:00

327 lines
9.9 KiB
Go

// Copyright 2022 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package miner
import (
"bytes"
"context"
"math/big"
"reflect"
"testing"
"time"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/consensus/clique"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params"
)
var (
// Test chain configurations
testTxPoolConfig legacypool.Config
ethashChainConfig *params.ChainConfig
cliqueChainConfig *params.ChainConfig
// Test accounts
testBankKey, _ = crypto.GenerateKey()
testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey)
testBankFunds = big.NewInt(1000000000000000000)
testUserKey, _ = crypto.GenerateKey()
testUserAddress = crypto.PubkeyToAddress(testUserKey.PublicKey)
// Test transactions
pendingTxs []*types.Transaction
newTxs []*types.Transaction
testConfig = Config{
PendingFeeRecipient: testBankAddress,
Recommit: time.Second,
GasCeil: params.GenesisGasLimit,
}
)
func init() {
testTxPoolConfig = legacypool.DefaultConfig
testTxPoolConfig.Journal = ""
ethashChainConfig = new(params.ChainConfig)
*ethashChainConfig = *params.TestChainConfig
cliqueChainConfig = new(params.ChainConfig)
*cliqueChainConfig = *params.TestChainConfig
cliqueChainConfig.Clique = &params.CliqueConfig{
Period: 10,
Epoch: 30000,
}
signer := types.LatestSigner(params.TestChainConfig)
tx1 := types.MustSignNewTx(testBankKey, signer, &types.AccessListTx{
ChainID: params.TestChainConfig.ChainID,
Nonce: 0,
To: &testUserAddress,
Value: big.NewInt(1000),
Gas: params.TxGas,
GasPrice: big.NewInt(params.InitialBaseFee),
})
pendingTxs = append(pendingTxs, tx1)
tx2 := types.MustSignNewTx(testBankKey, signer, &types.LegacyTx{
Nonce: 1,
To: &testUserAddress,
Value: big.NewInt(1000),
Gas: params.TxGas,
GasPrice: big.NewInt(params.InitialBaseFee),
})
newTxs = append(newTxs, tx2)
}
// testWorkerBackend implements worker.Backend interfaces and wraps all information needed during the testing.
type testWorkerBackend struct {
db ethdb.Database
txPool *txpool.TxPool
chain *core.BlockChain
genesis *core.Genesis
}
func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, n int) *testWorkerBackend {
var gspec = &core.Genesis{
Config: chainConfig,
Alloc: types.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
}
switch e := engine.(type) {
case *clique.Clique:
gspec.ExtraData = make([]byte, 32+common.AddressLength+crypto.SignatureLength)
copy(gspec.ExtraData[32:32+common.AddressLength], testBankAddress.Bytes())
e.Authorize(testBankAddress)
case *ethash.Ethash:
case *beacon.Beacon:
default:
t.Fatalf("unexpected consensus engine type: %T", engine)
}
chain, err := core.NewBlockChain(db, gspec, engine, &core.BlockChainConfig{ArchiveMode: true})
if err != nil {
t.Fatalf("core.NewBlockChain failed: %v", err)
}
pool := legacypool.New(testTxPoolConfig, chain)
txpool, _ := txpool.New(testTxPoolConfig.PriceLimit, chain, []txpool.SubPool{pool})
return &testWorkerBackend{
db: db,
chain: chain,
txPool: txpool,
genesis: gspec,
}
}
func (b *testWorkerBackend) BlockChain() *core.BlockChain { return b.chain }
func (b *testWorkerBackend) TxPool() *txpool.TxPool { return b.txPool }
func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, blocks int) (*Miner, *testWorkerBackend) {
backend := newTestWorkerBackend(t, chainConfig, engine, db, blocks)
backend.txPool.Add(pendingTxs, true)
w := New(backend, testConfig, engine)
return w, backend
}
func TestBuildPayload(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
recipient = common.HexToAddress("0xdeadbeef")
)
w, b := newTestWorker(t, params.TestChainConfig, ethash.NewFaker(), db, 0)
timestamp := uint64(time.Now().Unix())
args := &BuildPayloadArgs{
Parent: b.chain.CurrentBlock().Hash(),
Timestamp: timestamp,
Random: common.Hash{},
FeeRecipient: recipient,
}
payload, err := w.buildPayload(context.Background(), args, false)
if err != nil {
t.Fatalf("Failed to build payload %v", err)
}
verify := func(outer *engine.ExecutionPayloadEnvelope, txs int) {
payload := outer.ExecutionPayload
if payload.ParentHash != b.chain.CurrentBlock().Hash() {
t.Fatal("Unexpected parent hash")
}
if payload.Random != (common.Hash{}) {
t.Fatal("Unexpected random value")
}
if payload.Timestamp != timestamp {
t.Fatal("Unexpected timestamp")
}
if payload.FeeRecipient != recipient {
t.Fatal("Unexpected fee recipient")
}
if len(payload.Transactions) != txs {
t.Fatal("Unexpected transaction set")
}
}
empty := payload.ResolveEmpty()
verify(empty, 0)
full := payload.ResolveFull()
verify(full, len(pendingTxs))
// Ensure resolve can be called multiple times and the
// result should be unchanged
dataOne := payload.Resolve()
dataTwo := payload.Resolve()
if !reflect.DeepEqual(dataOne, dataTwo) {
t.Fatal("Unexpected payload data")
}
}
// TestBuildPayloadAmsterdamTransition verifies that a locally built payload for
// the first Amsterdam block contains the EIP-7997 deterministic deployment
// factory, i.e. the block-building path applies the same irregular state
// transition as block processing and the resulting block is importable.
func TestBuildPayloadAmsterdamTransition(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
recipient = common.HexToAddress("0xdeadbeef")
)
config := new(params.ChainConfig)
*config = *params.MergedTestChainConfig
config.AmsterdamTime = new(uint64)
*config.AmsterdamTime = 1 // genesis (t=0) is pre-Amsterdam, the first block crosses the fork
w, b := newTestWorker(t, config, beacon.New(ethash.NewFaker()), db, 0)
var (
beaconRoot = common.Hash{0x01}
slotNum = uint64(1)
)
payload, err := w.buildPayload(context.Background(), &BuildPayloadArgs{
Parent: b.chain.CurrentBlock().Hash(),
Timestamp: 1,
FeeRecipient: recipient,
Withdrawals: types.Withdrawals{},
BeaconRoot: &beaconRoot,
SlotNum: &slotNum,
}, false)
if err != nil {
t.Fatalf("Failed to build payload %v", err)
}
block := payload.empty
if !config.IsAmsterdam(block.Number(), block.Time()) {
t.Fatal("transition block is not an Amsterdam block")
}
// The block must be importable: Process applies EIP-7997 independently, so
// a payload built without the factory would fail the state root check here.
if _, err := b.chain.InsertChain(types.Blocks{block}); err != nil {
t.Fatalf("failed to insert transition block: %v", err)
}
statedb, err := b.chain.StateAt(block.Header())
if err != nil {
t.Fatalf("failed to open state at transition block: %v", err)
}
if code := statedb.GetCode(params.DeterministicFactoryAddress); !bytes.Equal(code, params.DeterministicFactoryCode) {
t.Fatalf("factory code missing from built payload state:\n got %x\nwant %x", code, params.DeterministicFactoryCode)
}
if nonce := statedb.GetNonce(params.DeterministicFactoryAddress); nonce != 1 {
t.Fatalf("factory nonce = %d, want 1", nonce)
}
}
func TestPayloadId(t *testing.T) {
t.Parallel()
ids := make(map[string]int)
for i, tt := range []*BuildPayloadArgs{
{
Parent: common.Hash{1},
Timestamp: 1,
Random: common.Hash{0x1},
FeeRecipient: common.Address{0x1},
},
// Different parent
{
Parent: common.Hash{2},
Timestamp: 1,
Random: common.Hash{0x1},
FeeRecipient: common.Address{0x1},
},
// Different timestamp
{
Parent: common.Hash{2},
Timestamp: 2,
Random: common.Hash{0x1},
FeeRecipient: common.Address{0x1},
},
// Different Random
{
Parent: common.Hash{2},
Timestamp: 2,
Random: common.Hash{0x2},
FeeRecipient: common.Address{0x1},
},
// Different fee-recipient
{
Parent: common.Hash{2},
Timestamp: 2,
Random: common.Hash{0x2},
FeeRecipient: common.Address{0x2},
},
// Different withdrawals (non-empty)
{
Parent: common.Hash{2},
Timestamp: 2,
Random: common.Hash{0x2},
FeeRecipient: common.Address{0x2},
Withdrawals: []*types.Withdrawal{
{
Index: 0,
Validator: 0,
Address: common.Address{},
Amount: 0,
},
},
},
// Different withdrawals (non-empty)
{
Parent: common.Hash{2},
Timestamp: 2,
Random: common.Hash{0x2},
FeeRecipient: common.Address{0x2},
Withdrawals: []*types.Withdrawal{
{
Index: 2,
Validator: 0,
Address: common.Address{},
Amount: 0,
},
},
},
} {
id := tt.Id().String()
if prev, exists := ids[id]; exists {
t.Errorf("ID collision, case %d and case %d: id %v", prev, i, id)
}
ids[id] = i
}
}