@@ -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"
@@ -999,3 +1000,90 @@ func TestTraceChain(t *testing.T) {
999
1000
}
1000
1001
}
1001
1002
}
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\n have: %v\n want: %v\n " , i , string (have ), want )
1087
+ }
1088
+ }
1089
+ }
0 commit comments