@@ -307,6 +307,12 @@ func testTransactionInBlock(t *testing.T, client *rpc.Client) {
307
307
if tx .Hash () != testTx2 .Hash () {
308
308
t .Fatalf ("unexpected transaction: %v" , tx )
309
309
}
310
+
311
+ // Test pending block
312
+ _ , err = ec .BlockByNumber (context .Background (), big .NewInt (int64 (rpc .PendingBlockNumber )))
313
+ if err != nil {
314
+ t .Fatalf ("unexpected error: %v" , err )
315
+ }
310
316
}
311
317
312
318
func testChainID (t * testing.T , client * rpc.Client ) {
@@ -619,6 +625,21 @@ func testAtFunctions(t *testing.T, client *rpc.Client) {
619
625
if gas != 21000 {
620
626
t .Fatalf ("unexpected gas limit: %v" , gas )
621
627
}
628
+
629
+ // Verify that sender address of pending transaction is saved in cache.
630
+ pendingBlock , err := ec .BlockByNumber (context .Background (), big .NewInt (int64 (rpc .PendingBlockNumber )))
631
+ if err != nil {
632
+ t .Fatalf ("unexpected error: %v" , err )
633
+ }
634
+ // No additional RPC should be required, ensure the server is not asked by
635
+ // canceling the context.
636
+ sender , err := ec .TransactionSender (newCanceledContext (), pendingBlock .Transactions ()[0 ], pendingBlock .Hash (), 0 )
637
+ if err != nil {
638
+ t .Fatal ("unable to recover sender:" , err )
639
+ }
640
+ if sender != testAddr {
641
+ t .Fatal ("wrong sender:" , sender )
642
+ }
622
643
}
623
644
624
645
func testTransactionSender (t * testing.T , client * rpc.Client ) {
@@ -640,10 +661,7 @@ func testTransactionSender(t *testing.T, client *rpc.Client) {
640
661
641
662
// The sender address is cached in tx1, so no additional RPC should be required in
642
663
// TransactionSender. Ensure the server is not asked by canceling the context here.
643
- canceledCtx , cancel := context .WithCancel (context .Background ())
644
- cancel ()
645
- <- canceledCtx .Done () // Ensure the close of the Done channel
646
- sender1 , err := ec .TransactionSender (canceledCtx , tx1 , block2 .Hash (), 0 )
664
+ sender1 , err := ec .TransactionSender (newCanceledContext (), tx1 , block2 .Hash (), 0 )
647
665
if err != nil {
648
666
t .Fatal (err )
649
667
}
@@ -662,6 +680,13 @@ func testTransactionSender(t *testing.T, client *rpc.Client) {
662
680
}
663
681
}
664
682
683
+ func newCanceledContext () context.Context {
684
+ ctx , cancel := context .WithCancel (context .Background ())
685
+ cancel ()
686
+ <- ctx .Done () // Ensure the close of the Done channel
687
+ return ctx
688
+ }
689
+
665
690
func sendTransaction (ec * ethclient.Client ) error {
666
691
chainID , err := ec .ChainID (context .Background ())
667
692
if err != nil {
0 commit comments