@@ -434,11 +434,13 @@ func newTestAccountManager(t *testing.T) (*accounts.Manager, accounts.Account) {
434434}
435435
436436type testBackend struct {
437- db ethdb.Database
438- chain * core.BlockChain
439- pending * types.Block
440- accman * accounts.Manager
441- acc accounts.Account
437+ db ethdb.Database
438+ chain * core.BlockChain
439+ accman * accounts.Manager
440+ acc accounts.Account
441+
442+ pending * types.Block
443+ pendingReceipts types.Receipts
442444}
443445
444446func newTestBackend (t * testing.T , n int , gspec * core.Genesis , engine consensus.Engine , generator func (i int , b * core.BlockGen )) * testBackend {
@@ -449,24 +451,26 @@ func newTestBackend(t *testing.T, n int, gspec *core.Genesis, engine consensus.E
449451 gspec .Alloc [acc .Address ] = types.Account {Balance : big .NewInt (params .Ether )}
450452
451453 // Generate blocks for testing
452- db , blocks , _ := core .GenerateChainWithGenesis (gspec , engine , n , generator )
454+ db , blocks , receipts := core .GenerateChainWithGenesis (gspec , engine , n + 1 , generator )
453455
454456 chain , err := core .NewBlockChain (db , gspec , engine , options )
455457 if err != nil {
456458 t .Fatalf ("failed to create tester chain: %v" , err )
457459 }
458- if n , err := chain .InsertChain (blocks ); err != nil {
460+ if n , err := chain .InsertChain (blocks [: n ] ); err != nil {
459461 t .Fatalf ("block %d: failed to insert into chain: %v" , n , err )
460462 }
461-
462- backend := & testBackend {db : db , chain : chain , accman : accman , acc : acc }
463+ backend := & testBackend {
464+ db : db ,
465+ chain : chain ,
466+ accman : accman ,
467+ acc : acc ,
468+ pending : blocks [n ],
469+ pendingReceipts : receipts [n ],
470+ }
463471 return backend
464472}
465473
466- func (b * testBackend ) setPendingBlock (block * types.Block ) {
467- b .pending = block
468- }
469-
470474func (b testBackend ) SyncProgress (ctx context.Context ) ethereum.SyncProgress {
471475 return ethereum.SyncProgress {}
472476}
@@ -558,7 +562,13 @@ func (b testBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOr
558562 }
559563 panic ("only implemented for number" )
560564}
561- func (b testBackend ) Pending () (* types.Block , types.Receipts , * state.StateDB ) { panic ("implement me" ) }
565+ func (b testBackend ) Pending () (* types.Block , types.Receipts , * state.StateDB ) {
566+ block := b .pending
567+ if block == nil {
568+ return nil , nil , nil
569+ }
570+ return block , b .pendingReceipts , nil
571+ }
562572func (b testBackend ) GetReceipts (ctx context.Context , hash common.Hash ) (types.Receipts , error ) {
563573 header , err := b .HeaderByHash (ctx , hash )
564574 if header == nil || err != nil {
@@ -3141,21 +3151,6 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
31413151 }
31423152 genBlocks = 10
31433153 signer = types.HomesteadSigner {}
3144- tx = types .NewTx (& types.LegacyTx {
3145- Nonce : 11 ,
3146- GasPrice : big .NewInt (11111 ),
3147- Gas : 1111 ,
3148- To : & acc2Addr ,
3149- Value : big .NewInt (111 ),
3150- Data : []byte {0x11 , 0x11 , 0x11 },
3151- })
3152- withdrawal = & types.Withdrawal {
3153- Index : 0 ,
3154- Validator : 1 ,
3155- Address : common.Address {0x12 , 0x34 },
3156- Amount : 10 ,
3157- }
3158- pending = types .NewBlock (& types.Header {Number : big .NewInt (11 ), Time : 42 }, & types.Body {Transactions : types.Transactions {tx }, Withdrawals : types.Withdrawals {withdrawal }}, nil , blocktest .NewHasher ())
31593154 )
31603155 backend := newTestBackend (t , genBlocks , genesis , ethash .NewFaker (), func (i int , b * core.BlockGen ) {
31613156 // Transfer from account[0] to account[1]
@@ -3164,7 +3159,6 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
31643159 tx , _ := types .SignTx (types .NewTx (& types.LegacyTx {Nonce : uint64 (i ), To : & acc2Addr , Value : big .NewInt (1000 ), Gas : params .TxGas , GasPrice : b .BaseFee (), Data : nil }), signer , acc1Key )
31653160 b .AddTx (tx )
31663161 })
3167- backend .setPendingBlock (pending )
31683162 api := NewBlockChainAPI (backend )
31693163 blockHashes := make ([]common.Hash , genBlocks + 1 )
31703164 ctx := context .Background ()
@@ -3175,7 +3169,7 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
31753169 }
31763170 blockHashes [i ] = header .Hash ()
31773171 }
3178- pendingHash := pending .Hash ()
3172+ pendingHash := backend . pending .Hash ()
31793173
31803174 var testSuite = []struct {
31813175 blockNumber rpc.BlockNumber
@@ -3406,7 +3400,7 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha
34063400 },
34073401 }
34083402 signer = types .LatestSignerForChainID (params .TestChainConfig .ChainID )
3409- txHashes = make ([]common.Hash , genBlocks )
3403+ txHashes = make ([]common.Hash , 0 , genBlocks )
34103404 )
34113405
34123406 backend := newTestBackend (t , genBlocks , genesis , beacon .New (ethash .NewFaker ()), func (i int , b * core.BlockGen ) {
@@ -3416,9 +3410,6 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha
34163410 )
34173411 b .SetPoS ()
34183412 switch i {
3419- case 0 :
3420- // transfer 1000wei
3421- tx , err = types .SignTx (types .NewTx (& types.LegacyTx {Nonce : uint64 (i ), To : & acc2Addr , Value : big .NewInt (1000 ), Gas : params .TxGas , GasPrice : b .BaseFee (), Data : nil }), types.HomesteadSigner {}, acc1Key )
34223413 case 1 :
34233414 // create contract
34243415 tx , err = types .SignTx (types .NewTx (& types.LegacyTx {Nonce : uint64 (i ), To : nil , Gas : 53100 , GasPrice : b .BaseFee (), Data : common .FromHex ("0x60806040" )}), signer , acc1Key )
@@ -3455,13 +3446,16 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha
34553446 BlobHashes : []common.Hash {{1 }},
34563447 Value : new (uint256.Int ),
34573448 }), signer , acc1Key )
3449+ default :
3450+ // transfer 1000wei
3451+ tx , err = types .SignTx (types .NewTx (& types.LegacyTx {Nonce : uint64 (i ), To : & acc2Addr , Value : big .NewInt (1000 ), Gas : params .TxGas , GasPrice : b .BaseFee (), Data : nil }), types.HomesteadSigner {}, acc1Key )
34583452 }
34593453 if err != nil {
34603454 t .Errorf ("failed to sign tx: %v" , err )
34613455 }
34623456 if tx != nil {
34633457 b .AddTx (tx )
3464- txHashes [ i ] = tx .Hash ()
3458+ txHashes = append ( txHashes , tx .Hash () )
34653459 }
34663460 })
34673461 return backend , txHashes
@@ -3577,6 +3571,11 @@ func TestRPCGetBlockReceipts(t *testing.T) {
35773571 test : rpc .BlockNumberOrHashWithNumber (rpc .LatestBlockNumber ),
35783572 file : "tag-latest" ,
35793573 },
3574+ // 3. pending tag
3575+ {
3576+ test : rpc .BlockNumberOrHashWithNumber (rpc .PendingBlockNumber ),
3577+ file : "tag-pending" ,
3578+ },
35803579 // 4. block with legacy transfer tx(hash)
35813580 {
35823581 test : rpc .BlockNumberOrHashWithHash (blockHashes [1 ], false ),
0 commit comments