@@ -187,9 +187,34 @@ var (
187
187
testBalance = big .NewInt (2e15 )
188
188
)
189
189
190
+ var genesis = & core.Genesis {
191
+ Config : params .AllEthashProtocolChanges ,
192
+ Alloc : core.GenesisAlloc {testAddr : {Balance : testBalance }},
193
+ ExtraData : []byte ("test genesis" ),
194
+ Timestamp : 9000 ,
195
+ BaseFee : big .NewInt (params .InitialBaseFee ),
196
+ }
197
+
198
+ var testTx1 = types .MustSignNewTx (testKey , types .LatestSigner (genesis .Config ), & types.LegacyTx {
199
+ Nonce : 0 ,
200
+ Value : big .NewInt (12 ),
201
+ GasPrice : big .NewInt (params .InitialBaseFee ),
202
+ Gas : params .TxGas ,
203
+ To : & common.Address {2 },
204
+ })
205
+
206
+ var testTx2 = types .MustSignNewTx (testKey , types .LatestSigner (genesis .Config ), & types.LegacyTx {
207
+ Nonce : 1 ,
208
+ Value : big .NewInt (8 ),
209
+ GasPrice : big .NewInt (params .InitialBaseFee ),
210
+ Gas : params .TxGas ,
211
+ To : & common.Address {2 },
212
+ })
213
+
190
214
func newTestBackend (t * testing.T ) (* node.Node , []* types.Block ) {
191
215
// Generate test chain.
192
- genesis , blocks := generateTestChain ()
216
+ blocks := generateTestChain ()
217
+
193
218
// Create node
194
219
n , err := node .New (& node.Config {})
195
220
if err != nil {
@@ -212,25 +237,22 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) {
212
237
return n , blocks
213
238
}
214
239
215
- func generateTestChain () ( * core. Genesis , []* types.Block ) {
240
+ func generateTestChain () []* types.Block {
216
241
db := rawdb .NewMemoryDatabase ()
217
- config := params .AllEthashProtocolChanges
218
- genesis := & core.Genesis {
219
- Config : config ,
220
- Alloc : core.GenesisAlloc {testAddr : {Balance : testBalance }},
221
- ExtraData : []byte ("test genesis" ),
222
- Timestamp : 9000 ,
223
- BaseFee : big .NewInt (params .InitialBaseFee ),
224
- }
225
242
generate := func (i int , g * core.BlockGen ) {
226
243
g .OffsetTime (5 )
227
244
g .SetExtra ([]byte ("test" ))
245
+ if i == 1 {
246
+ // Test transactions are included in block #2.
247
+ g .AddTx (testTx1 )
248
+ g .AddTx (testTx2 )
249
+ }
228
250
}
229
251
gblock := genesis .ToBlock (db )
230
252
engine := ethash .NewFaker ()
231
- blocks , _ := core .GenerateChain (config , gblock , engine , db , 1 , generate )
253
+ blocks , _ := core .GenerateChain (genesis . Config , gblock , engine , db , 2 , generate )
232
254
blocks = append ([]* types.Block {gblock }, blocks ... )
233
- return genesis , blocks
255
+ return blocks
234
256
}
235
257
236
258
func TestEthClient (t * testing.T ) {
@@ -242,30 +264,33 @@ func TestEthClient(t *testing.T) {
242
264
tests := map [string ]struct {
243
265
test func (t * testing.T )
244
266
}{
245
- "TestHeader " : {
267
+ "Header " : {
246
268
func (t * testing.T ) { testHeader (t , chain , client ) },
247
269
},
248
- "TestBalanceAt " : {
270
+ "BalanceAt " : {
249
271
func (t * testing.T ) { testBalanceAt (t , client ) },
250
272
},
251
- "TestTxInBlockInterrupted " : {
273
+ "TxInBlockInterrupted " : {
252
274
func (t * testing.T ) { testTransactionInBlockInterrupted (t , client ) },
253
275
},
254
- "TestChainID " : {
276
+ "ChainID " : {
255
277
func (t * testing.T ) { testChainID (t , client ) },
256
278
},
257
- "TestGetBlock " : {
279
+ "GetBlock " : {
258
280
func (t * testing.T ) { testGetBlock (t , client ) },
259
281
},
260
- "TestStatusFunctions " : {
282
+ "StatusFunctions " : {
261
283
func (t * testing.T ) { testStatusFunctions (t , client ) },
262
284
},
263
- "TestCallContract " : {
285
+ "CallContract " : {
264
286
func (t * testing.T ) { testCallContract (t , client ) },
265
287
},
266
- "TestAtFunctions " : {
288
+ "AtFunctions " : {
267
289
func (t * testing.T ) { testAtFunctions (t , client ) },
268
290
},
291
+ "TransactionSender" : {
292
+ func (t * testing.T ) { testTransactionSender (t , client ) },
293
+ },
269
294
}
270
295
271
296
t .Parallel ()
@@ -321,6 +346,11 @@ func testBalanceAt(t *testing.T, client *rpc.Client) {
321
346
want * big.Int
322
347
wantErr error
323
348
}{
349
+ "valid_account_genesis" : {
350
+ account : testAddr ,
351
+ block : big .NewInt (0 ),
352
+ want : testBalance ,
353
+ },
324
354
"valid_account" : {
325
355
account : testAddr ,
326
356
block : big .NewInt (1 ),
@@ -358,23 +388,25 @@ func testBalanceAt(t *testing.T, client *rpc.Client) {
358
388
func testTransactionInBlockInterrupted (t * testing.T , client * rpc.Client ) {
359
389
ec := NewClient (client )
360
390
361
- // Get current block by number
391
+ // Get current block by number.
362
392
block , err := ec .BlockByNumber (context .Background (), nil )
363
393
if err != nil {
364
394
t .Fatalf ("unexpected error: %v" , err )
365
395
}
366
- // Test tx in block interupted
396
+
397
+ // Test tx in block interupted.
367
398
ctx , cancel := context .WithCancel (context .Background ())
368
399
cancel ()
369
- tx , err := ec .TransactionInBlock (ctx , block .Hash (), 1 )
400
+ tx , err := ec .TransactionInBlock (ctx , block .Hash (), 0 )
370
401
if tx != nil {
371
402
t .Fatal ("transaction should be nil" )
372
403
}
373
404
if err == nil || err == ethereum .NotFound {
374
405
t .Fatal ("error should not be nil/notfound" )
375
406
}
376
- // Test tx in block not found
377
- if _ , err := ec .TransactionInBlock (context .Background (), block .Hash (), 1 ); err != ethereum .NotFound {
407
+
408
+ // Test tx in block not found.
409
+ if _ , err := ec .TransactionInBlock (context .Background (), block .Hash (), 20 ); err != ethereum .NotFound {
378
410
t .Fatal ("error should be ethereum.NotFound" )
379
411
}
380
412
}
@@ -392,12 +424,13 @@ func testChainID(t *testing.T, client *rpc.Client) {
392
424
393
425
func testGetBlock (t * testing.T , client * rpc.Client ) {
394
426
ec := NewClient (client )
427
+
395
428
// Get current block number
396
429
blockNumber , err := ec .BlockNumber (context .Background ())
397
430
if err != nil {
398
431
t .Fatalf ("unexpected error: %v" , err )
399
432
}
400
- if blockNumber != 1 {
433
+ if blockNumber != 2 {
401
434
t .Fatalf ("BlockNumber returned wrong number: %d" , blockNumber )
402
435
}
403
436
// Get current block by number
@@ -445,6 +478,7 @@ func testStatusFunctions(t *testing.T, client *rpc.Client) {
445
478
if progress != nil {
446
479
t .Fatalf ("unexpected progress: %v" , progress )
447
480
}
481
+
448
482
// NetworkID
449
483
networkID , err := ec .NetworkID (context .Background ())
450
484
if err != nil {
@@ -453,20 +487,22 @@ func testStatusFunctions(t *testing.T, client *rpc.Client) {
453
487
if networkID .Cmp (big .NewInt (0 )) != 0 {
454
488
t .Fatalf ("unexpected networkID: %v" , networkID )
455
489
}
456
- // SuggestGasPrice (should suggest 1 Gwei)
490
+
491
+ // SuggestGasPrice
457
492
gasPrice , err := ec .SuggestGasPrice (context .Background ())
458
493
if err != nil {
459
494
t .Fatalf ("unexpected error: %v" , err )
460
495
}
461
- if gasPrice .Cmp (big .NewInt (1875000000 )) != 0 { // 1 gwei tip + 0.875 basefee after a 1 gwei fee empty block
496
+ if gasPrice .Cmp (big .NewInt (1000000000 )) != 0 {
462
497
t .Fatalf ("unexpected gas price: %v" , gasPrice )
463
498
}
464
- // SuggestGasTipCap (should suggest 1 Gwei)
499
+
500
+ // SuggestGasTipCap
465
501
gasTipCap , err := ec .SuggestGasTipCap (context .Background ())
466
502
if err != nil {
467
503
t .Fatalf ("unexpected error: %v" , err )
468
504
}
469
- if gasTipCap .Cmp (big .NewInt (1000000000 )) != 0 {
505
+ if gasTipCap .Cmp (big .NewInt (234375000 )) != 0 {
470
506
t .Fatalf ("unexpected gas tip cap: %v" , gasTipCap )
471
507
}
472
508
}
@@ -500,9 +536,11 @@ func testCallContract(t *testing.T, client *rpc.Client) {
500
536
501
537
func testAtFunctions (t * testing.T , client * rpc.Client ) {
502
538
ec := NewClient (client )
539
+
503
540
// send a transaction for some interesting pending status
504
541
sendTransaction (ec )
505
542
time .Sleep (100 * time .Millisecond )
543
+
506
544
// Check pending transaction count
507
545
pending , err := ec .PendingTransactionCount (context .Background ())
508
546
if err != nil {
@@ -561,23 +599,66 @@ func testAtFunctions(t *testing.T, client *rpc.Client) {
561
599
}
562
600
}
563
601
602
+ func testTransactionSender (t * testing.T , client * rpc.Client ) {
603
+ ec := NewClient (client )
604
+ ctx := context .Background ()
605
+
606
+ // Retrieve testTx1 via RPC.
607
+ block2 , err := ec .HeaderByNumber (ctx , big .NewInt (2 ))
608
+ if err != nil {
609
+ t .Fatal ("can't get block 1:" , err )
610
+ }
611
+ tx1 , err := ec .TransactionInBlock (ctx , block2 .Hash (), 0 )
612
+ if err != nil {
613
+ t .Fatal ("can't get tx:" , err )
614
+ }
615
+ if tx1 .Hash () != testTx1 .Hash () {
616
+ t .Fatalf ("wrong tx hash %v, want %v" , tx1 .Hash (), testTx1 .Hash ())
617
+ }
618
+
619
+ // The sender address is cached in tx1, so no additional RPC should be required in
620
+ // TransactionSender. Ensure the server is not asked by canceling the context here.
621
+ canceledCtx , cancel := context .WithCancel (context .Background ())
622
+ cancel ()
623
+ sender1 , err := ec .TransactionSender (canceledCtx , tx1 , block2 .Hash (), 0 )
624
+ if err != nil {
625
+ t .Fatal (err )
626
+ }
627
+ if sender1 != testAddr {
628
+ t .Fatal ("wrong sender:" , sender1 )
629
+ }
630
+
631
+ // Now try to get the sender of testTx2, which was not fetched through RPC.
632
+ // TransactionSender should query the server here.
633
+ sender2 , err := ec .TransactionSender (ctx , testTx2 , block2 .Hash (), 1 )
634
+ if err != nil {
635
+ t .Fatal (err )
636
+ }
637
+ if sender2 != testAddr {
638
+ t .Fatal ("wrong sender:" , sender2 )
639
+ }
640
+ }
641
+
564
642
func sendTransaction (ec * Client ) error {
565
- // Retrieve chainID
566
643
chainID , err := ec .ChainID (context .Background ())
567
644
if err != nil {
568
645
return err
569
646
}
570
- // Create transaction
571
- tx := types .NewTransaction (0 , common.Address {1 }, big .NewInt (1 ), 22000 , big .NewInt (params .InitialBaseFee ), nil )
572
- signer := types .LatestSignerForChainID (chainID )
573
- signature , err := crypto .Sign (signer .Hash (tx ).Bytes (), testKey )
647
+ nonce , err := ec .PendingNonceAt (context .Background (), testAddr )
574
648
if err != nil {
575
649
return err
576
650
}
577
- signedTx , err := tx .WithSignature (signer , signature )
651
+
652
+ signer := types .LatestSignerForChainID (chainID )
653
+ tx , err := types .SignNewTx (testKey , signer , & types.LegacyTx {
654
+ Nonce : nonce ,
655
+ To : & common.Address {2 },
656
+ Value : big .NewInt (1 ),
657
+ Gas : 22000 ,
658
+ GasPrice : big .NewInt (params .InitialBaseFee ),
659
+ })
578
660
if err != nil {
579
661
return err
580
662
}
581
- // Send transaction
582
- return ec .SendTransaction (context .Background (), signedTx )
663
+ return ec .SendTransaction (context .Background (), tx )
583
664
}
0 commit comments