@@ -358,6 +358,75 @@ func TestTransactionTimeSort(t *testing.T) {
358
358
}
359
359
}
360
360
361
+ func TestTransactionForward (t * testing.T ) {
362
+ // Generate a batch of accounts to start with
363
+ keys := make ([]* ecdsa.PrivateKey , 5 )
364
+ for i := 0 ; i < len (keys ); i ++ {
365
+ keys [i ], _ = crypto .GenerateKey ()
366
+ }
367
+ signer := HomesteadSigner {}
368
+
369
+ // Generate a batch of transactions with overlapping prices, but different creation times
370
+ groups := map [common.Address ]Transactions {}
371
+ for start , key := range keys {
372
+ addr := crypto .PubkeyToAddress (key .PublicKey )
373
+
374
+ tx , _ := SignTx (NewTransaction (0 , common.Address {}, big .NewInt (100 ), 100 , big .NewInt (1 ), nil ), signer , key )
375
+ tx2 , _ := SignTx (NewTransaction (1 , common.Address {}, big .NewInt (100 ), 100 , big .NewInt (1 ), nil ), signer , key )
376
+
377
+ tx .time = time .Unix (0 , int64 (len (keys )- start ))
378
+ tx2 .time = time .Unix (1 , int64 (len (keys )- start ))
379
+
380
+ groups [addr ] = append (groups [addr ], tx )
381
+ groups [addr ] = append (groups [addr ], tx2 )
382
+
383
+ }
384
+ // Sort the transactions
385
+ txset := NewTransactionsByPriceAndNonce (signer , groups )
386
+ txsetCpy := txset .Copy ()
387
+
388
+ txs := Transactions {}
389
+ for tx := txsetCpy .Peek (); tx != nil ; tx = txsetCpy .Peek () {
390
+ txs = append (txs , tx )
391
+ txsetCpy .Shift ()
392
+ }
393
+
394
+ tmp := txset .Copy ()
395
+ for j := 0 ; j < 10 ; j ++ {
396
+ txset = tmp .Copy ()
397
+ txsetCpy = tmp .Copy ()
398
+ i := 0
399
+ for ; i < j ; i ++ {
400
+ txset .Shift ()
401
+ }
402
+ tx := txset .Peek ()
403
+ txsetCpy .Forward (tx )
404
+ txCpy := txsetCpy .Peek ()
405
+ if txCpy == nil {
406
+ if tx == nil {
407
+ continue
408
+ }
409
+ txset .Shift ()
410
+ if txset .Peek () != nil {
411
+ t .Errorf ("forward got an incorrect result, got %v, want %v" , txCpy , tx )
412
+ } else {
413
+ continue
414
+ }
415
+ }
416
+ txset .Shift ()
417
+ for ; i < len (txs )- 1 ; i ++ {
418
+ tx = txset .Peek ()
419
+ txCpy = txsetCpy .Peek ()
420
+ if txCpy != tx {
421
+ t .Errorf ("forward got an incorrect result, got %v, want %v" , txCpy , tx )
422
+ }
423
+ txsetCpy .Shift ()
424
+ txset .Shift ()
425
+ }
426
+
427
+ }
428
+ }
429
+
361
430
// TestTransactionCoding tests serializing/de-serializing to/from rlp and JSON.
362
431
func TestTransactionCoding (t * testing.T ) {
363
432
key , err := crypto .GenerateKey ()
0 commit comments