@@ -32,6 +32,7 @@ import (
32
32
"github.com/ethereum/go-ethereum/common"
33
33
"github.com/ethereum/go-ethereum/common/hexutil"
34
34
"github.com/ethereum/go-ethereum/consensus"
35
+ "github.com/ethereum/go-ethereum/consensus/beacon"
35
36
"github.com/ethereum/go-ethereum/consensus/ethash"
36
37
"github.com/ethereum/go-ethereum/core"
37
38
"github.com/ethereum/go-ethereum/core/rawdb"
@@ -994,3 +995,90 @@ func TestTraceChain(t *testing.T) {
994
995
}
995
996
}
996
997
}
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\n have: %v\n want: %v\n " , i , string (have ), want )
1082
+ }
1083
+ }
1084
+ }
0 commit comments