Skip to content

Commit 3382d27

Browse files
holimans1na
authored andcommitted
eth/tracers: fix basefee context for traceBlock (#29811)
This fixes an issue for `debug_traceBlock*` methods where the BASEFEE opcode was returning always 0. This caused the method return invalid results. Co-authored-by: Sina Mahmoodi <[email protected]>
1 parent b6a8f89 commit 3382d27

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

eth/tracers/api.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"encoding/json"
2323
"errors"
2424
"fmt"
25-
"math/big"
2625
"os"
2726
"runtime"
2827
"sync"
@@ -986,7 +985,8 @@ func (api *API) traceTx(ctx context.Context, tx *types.Transaction, message *cor
986985
return nil, err
987986
}
988987
}
989-
vmenv := vm.NewEVM(vmctx, vm.TxContext{GasPrice: big.NewInt(0)}, statedb, api.backend.ChainConfig(), vm.Config{Tracer: tracer.Hooks, NoBaseFee: true})
988+
// The actual TxContext will be created as part of ApplyTransactionWithEVM.
989+
vmenv := vm.NewEVM(vmctx, vm.TxContext{GasPrice: message.GasPrice, BlobFeeCap: message.BlobGasFeeCap}, statedb, api.backend.ChainConfig(), vm.Config{Tracer: tracer.Hooks, NoBaseFee: true})
990990
statedb.SetLogger(tracer.Hooks)
991991

992992
// Define a meaningful timeout of a single transaction trace

eth/tracers/api_test.go

+88
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/ethereum/go-ethereum/common"
3333
"github.com/ethereum/go-ethereum/common/hexutil"
3434
"github.com/ethereum/go-ethereum/consensus"
35+
"github.com/ethereum/go-ethereum/consensus/beacon"
3536
"github.com/ethereum/go-ethereum/consensus/ethash"
3637
"github.com/ethereum/go-ethereum/core"
3738
"github.com/ethereum/go-ethereum/core/rawdb"
@@ -999,3 +1000,90 @@ func TestTraceChain(t *testing.T) {
9991000
}
10001001
}
10011002
}
1003+
1004+
// newTestMergedBackend creates a post-merge chain
1005+
func newTestMergedBackend(t *testing.T, n int, gspec *core.Genesis, generator func(i int, b *core.BlockGen)) *testBackend {
1006+
backend := &testBackend{
1007+
chainConfig: gspec.Config,
1008+
engine: beacon.NewFaker(),
1009+
chaindb: rawdb.NewMemoryDatabase(),
1010+
}
1011+
// Generate blocks for testing
1012+
_, blocks, _ := core.GenerateChainWithGenesis(gspec, backend.engine, n, generator)
1013+
1014+
// Import the canonical chain
1015+
cacheConfig := &core.CacheConfig{
1016+
TrieCleanLimit: 256,
1017+
TrieDirtyLimit: 256,
1018+
TrieTimeLimit: 5 * time.Minute,
1019+
SnapshotLimit: 0,
1020+
TrieDirtyDisabled: true, // Archive mode
1021+
}
1022+
chain, err := core.NewBlockChain(backend.chaindb, cacheConfig, gspec, nil, backend.engine, vm.Config{}, nil, nil)
1023+
if err != nil {
1024+
t.Fatalf("failed to create tester chain: %v", err)
1025+
}
1026+
if n, err := chain.InsertChain(blocks); err != nil {
1027+
t.Fatalf("block %d: failed to insert into chain: %v", n, err)
1028+
}
1029+
backend.chain = chain
1030+
return backend
1031+
}
1032+
1033+
func TestTraceBlockWithBasefee(t *testing.T) {
1034+
t.Parallel()
1035+
accounts := newAccounts(1)
1036+
target := common.HexToAddress("0x1111111111111111111111111111111111111111")
1037+
genesis := &core.Genesis{
1038+
Config: params.AllDevChainProtocolChanges,
1039+
Alloc: types.GenesisAlloc{
1040+
accounts[0].addr: {Balance: big.NewInt(1 * params.Ether)},
1041+
target: {Nonce: 1, Code: []byte{
1042+
byte(vm.BASEFEE), byte(vm.STOP),
1043+
}},
1044+
},
1045+
}
1046+
genBlocks := 1
1047+
signer := types.HomesteadSigner{}
1048+
var txHash common.Hash
1049+
var baseFee = new(big.Int)
1050+
backend := newTestMergedBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
1051+
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
1052+
Nonce: uint64(i),
1053+
To: &target,
1054+
Value: big.NewInt(0),
1055+
Gas: 5 * params.TxGas,
1056+
GasPrice: b.BaseFee(),
1057+
Data: nil}),
1058+
signer, accounts[0].key)
1059+
b.AddTx(tx)
1060+
txHash = tx.Hash()
1061+
baseFee.Set(b.BaseFee())
1062+
})
1063+
defer backend.chain.Stop()
1064+
api := NewAPI(backend)
1065+
1066+
var testSuite = []struct {
1067+
blockNumber rpc.BlockNumber
1068+
config *TraceConfig
1069+
want string
1070+
}{
1071+
// Trace head block
1072+
{
1073+
blockNumber: rpc.BlockNumber(genBlocks),
1074+
want: fmt.Sprintf(`[{"txHash":"%#x","result":{"gas":21002,"failed":false,"returnValue":"","structLogs":[{"pc":0,"op":"BASEFEE","gas":84000,"gasCost":2,"depth":1,"stack":[]},{"pc":1,"op":"STOP","gas":83998,"gasCost":0,"depth":1,"stack":["%#x"]}]}}]`, txHash, baseFee),
1075+
},
1076+
}
1077+
for i, tc := range testSuite {
1078+
result, err := api.TraceBlockByNumber(context.Background(), tc.blockNumber, tc.config)
1079+
if err != nil {
1080+
t.Errorf("test %d, want no error, have %v", i, err)
1081+
continue
1082+
}
1083+
have, _ := json.Marshal(result)
1084+
want := tc.want
1085+
if string(have) != want {
1086+
t.Errorf("test %d, result mismatch\nhave: %v\nwant: %v\n", i, string(have), want)
1087+
}
1088+
}
1089+
}

0 commit comments

Comments
 (0)