Skip to content

Commit 7fd7c1f

Browse files
holimans1na
andauthored
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 be5df74 commit 7fd7c1f

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

eth/tracers/api.go

Lines changed: 2 additions & 2 deletions
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"
@@ -982,7 +981,8 @@ func (api *API) traceTx(ctx context.Context, tx *types.Transaction, message *cor
982981
return nil, err
983982
}
984983
}
985-
vmenv := vm.NewEVM(vmctx, vm.TxContext{GasPrice: big.NewInt(0)}, statedb, api.backend.ChainConfig(), vm.Config{Tracer: tracer.Hooks, NoBaseFee: true})
984+
// The actual TxContext will be created as part of ApplyTransactionWithEVM.
985+
vmenv := vm.NewEVM(vmctx, vm.TxContext{GasPrice: message.GasPrice, BlobFeeCap: message.BlobGasFeeCap}, statedb, api.backend.ChainConfig(), vm.Config{Tracer: tracer.Hooks, NoBaseFee: true})
986986
statedb.SetLogger(tracer.Hooks)
987987

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

eth/tracers/api_test.go

Lines changed: 88 additions & 0 deletions
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"
@@ -994,3 +995,90 @@ func TestTraceChain(t *testing.T) {
994995
}
995996
}
996997
}
998+
999+
// newTestMergedBackend creates a post-merge chain
1000+
func newTestMergedBackend(t *testing.T, n int, gspec *core.Genesis, generator func(i int, b *core.BlockGen)) *testBackend {
1001+
backend := &testBackend{
1002+
chainConfig: gspec.Config,
1003+
engine: beacon.NewFaker(),
1004+
chaindb: rawdb.NewMemoryDatabase(),
1005+
}
1006+
// Generate blocks for testing
1007+
_, blocks, _ := core.GenerateChainWithGenesis(gspec, backend.engine, n, generator)
1008+
1009+
// Import the canonical chain
1010+
cacheConfig := &core.CacheConfig{
1011+
TrieCleanLimit: 256,
1012+
TrieDirtyLimit: 256,
1013+
TrieTimeLimit: 5 * time.Minute,
1014+
SnapshotLimit: 0,
1015+
TrieDirtyDisabled: true, // Archive mode
1016+
}
1017+
chain, err := core.NewBlockChain(backend.chaindb, cacheConfig, gspec, nil, backend.engine, vm.Config{}, nil, nil)
1018+
if err != nil {
1019+
t.Fatalf("failed to create tester chain: %v", err)
1020+
}
1021+
if n, err := chain.InsertChain(blocks); err != nil {
1022+
t.Fatalf("block %d: failed to insert into chain: %v", n, err)
1023+
}
1024+
backend.chain = chain
1025+
return backend
1026+
}
1027+
1028+
func TestTraceBlockWithBasefee(t *testing.T) {
1029+
t.Parallel()
1030+
accounts := newAccounts(1)
1031+
target := common.HexToAddress("0x1111111111111111111111111111111111111111")
1032+
genesis := &core.Genesis{
1033+
Config: params.AllDevChainProtocolChanges,
1034+
Alloc: types.GenesisAlloc{
1035+
accounts[0].addr: {Balance: big.NewInt(1 * params.Ether)},
1036+
target: {Nonce: 1, Code: []byte{
1037+
byte(vm.BASEFEE), byte(vm.STOP),
1038+
}},
1039+
},
1040+
}
1041+
genBlocks := 1
1042+
signer := types.HomesteadSigner{}
1043+
var txHash common.Hash
1044+
var baseFee = new(big.Int)
1045+
backend := newTestMergedBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
1046+
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
1047+
Nonce: uint64(i),
1048+
To: &target,
1049+
Value: big.NewInt(0),
1050+
Gas: 5 * params.TxGas,
1051+
GasPrice: b.BaseFee(),
1052+
Data: nil}),
1053+
signer, accounts[0].key)
1054+
b.AddTx(tx)
1055+
txHash = tx.Hash()
1056+
baseFee.Set(b.BaseFee())
1057+
})
1058+
defer backend.chain.Stop()
1059+
api := NewAPI(backend)
1060+
1061+
var testSuite = []struct {
1062+
blockNumber rpc.BlockNumber
1063+
config *TraceConfig
1064+
want string
1065+
}{
1066+
// Trace head block
1067+
{
1068+
blockNumber: rpc.BlockNumber(genBlocks),
1069+
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),
1070+
},
1071+
}
1072+
for i, tc := range testSuite {
1073+
result, err := api.TraceBlockByNumber(context.Background(), tc.blockNumber, tc.config)
1074+
if err != nil {
1075+
t.Errorf("test %d, want no error, have %v", i, err)
1076+
continue
1077+
}
1078+
have, _ := json.Marshal(result)
1079+
want := tc.want
1080+
if string(have) != want {
1081+
t.Errorf("test %d, result mismatch\nhave: %v\nwant: %v\n", i, string(have), want)
1082+
}
1083+
}
1084+
}

0 commit comments

Comments
 (0)