6
6
"fmt"
7
7
"math/big"
8
8
"reflect"
9
+ "sync"
9
10
"testing"
10
11
"time"
11
12
@@ -18,7 +19,8 @@ import (
18
19
"github.com/status-im/status-go/geth/common"
19
20
"github.com/status-im/status-go/geth/params"
20
21
"github.com/status-im/status-go/geth/signal"
21
- "github.com/status-im/status-go/geth/txqueue"
22
+ "github.com/status-im/status-go/geth/transactions"
23
+ "github.com/status-im/status-go/geth/transactions/queue"
22
24
. "github.com/status-im/status-go/testing"
23
25
"github.com/stretchr/testify/suite"
24
26
)
@@ -48,7 +50,7 @@ func (s *TransactionsTestSuite) TestCallRPCSendTransaction() {
48
50
err := json .Unmarshal ([]byte (rawSignal ), & sg )
49
51
s .NoError (err )
50
52
51
- if sg .Type == txqueue .EventTransactionQueued {
53
+ if sg .Type == transactions .EventTransactionQueued {
52
54
event := sg .Event .(map [string ]interface {})
53
55
txID := event ["id" ].(string )
54
56
txHash , err = s .Backend .CompleteTransaction (common .QueuedTxID (txID ), TestConfig .Account1 .Password )
@@ -100,7 +102,7 @@ func (s *TransactionsTestSuite) TestCallRPCSendTransactionUpstream() {
100
102
err := json .Unmarshal ([]byte (rawSignal ), & signalEnvelope )
101
103
s .NoError (err )
102
104
103
- if signalEnvelope .Type == txqueue .EventTransactionQueued {
105
+ if signalEnvelope .Type == transactions .EventTransactionQueued {
104
106
event := signalEnvelope .Event .(map [string ]interface {})
105
107
txID := event ["id" ].(string )
106
108
@@ -156,7 +158,7 @@ func (s *TransactionsTestSuite) TestSendContractTx() {
156
158
err = json .Unmarshal ([]byte (jsonEvent ), & envelope )
157
159
s .NoError (err , fmt .Sprintf ("cannot unmarshal JSON: %s" , jsonEvent ))
158
160
159
- if envelope .Type == txqueue .EventTransactionQueued {
161
+ if envelope .Type == transactions .EventTransactionQueued {
160
162
event := envelope .Event .(map [string ]interface {})
161
163
log .Info ("transaction queued (will be completed shortly)" , "id" , event ["id" ].(string ))
162
164
@@ -182,7 +184,7 @@ func (s *TransactionsTestSuite) TestSendContractTx() {
182
184
)
183
185
s .EqualError (
184
186
err ,
185
- txqueue .ErrInvalidCompleteTxSender .Error (),
187
+ queue .ErrInvalidCompleteTxSender .Error (),
186
188
fmt .Sprintf ("expected error on queued transaction[%v] not thrown" , event ["id" ]),
187
189
)
188
190
@@ -247,7 +249,7 @@ func (s *TransactionsTestSuite) TestSendEther() {
247
249
err = json .Unmarshal ([]byte (jsonEvent ), & envelope )
248
250
s .NoError (err , fmt .Sprintf ("cannot unmarshal JSON: %s" , jsonEvent ))
249
251
250
- if envelope .Type == txqueue .EventTransactionQueued {
252
+ if envelope .Type == transactions .EventTransactionQueued {
251
253
event := envelope .Event .(map [string ]interface {})
252
254
log .Info ("transaction queued (will be completed shortly)" , "id" , event ["id" ].(string ))
253
255
@@ -271,7 +273,7 @@ func (s *TransactionsTestSuite) TestSendEther() {
271
273
common .QueuedTxID (event ["id" ].(string )), TestConfig .Account1 .Password )
272
274
s .EqualError (
273
275
err ,
274
- txqueue .ErrInvalidCompleteTxSender .Error (),
276
+ queue .ErrInvalidCompleteTxSender .Error (),
275
277
fmt .Sprintf ("expected error on queued transaction[%v] not thrown" , event ["id" ]),
276
278
)
277
279
@@ -330,7 +332,7 @@ func (s *TransactionsTestSuite) TestSendEtherTxUpstream() {
330
332
err = json .Unmarshal ([]byte (jsonEvent ), & envelope )
331
333
s .NoError (err , "cannot unmarshal JSON: %s" , jsonEvent )
332
334
333
- if envelope .Type == txqueue .EventTransactionQueued {
335
+ if envelope .Type == transactions .EventTransactionQueued {
334
336
event := envelope .Event .(map [string ]interface {})
335
337
log .Info ("transaction queued (will be completed shortly)" , "id" , event ["id" ].(string ))
336
338
@@ -387,7 +389,7 @@ func (s *TransactionsTestSuite) TestDoubleCompleteQueuedTransactions() {
387
389
err := json .Unmarshal ([]byte (jsonEvent ), & envelope )
388
390
s .NoError (err , fmt .Sprintf ("cannot unmarshal JSON: %s" , jsonEvent ))
389
391
390
- if envelope .Type == txqueue .EventTransactionQueued {
392
+ if envelope .Type == transactions .EventTransactionQueued {
391
393
event := envelope .Event .(map [string ]interface {})
392
394
txID := common .QueuedTxID (event ["id" ].(string ))
393
395
log .Info ("transaction queued (will be failed and completed on the second call)" , "id" , txID )
@@ -407,7 +409,7 @@ func (s *TransactionsTestSuite) TestDoubleCompleteQueuedTransactions() {
407
409
close (completeQueuedTransaction )
408
410
}
409
411
410
- if envelope .Type == txqueue .EventTransactionFailed {
412
+ if envelope .Type == transactions .EventTransactionFailed {
411
413
event := envelope .Event .(map [string ]interface {})
412
414
log .Info ("transaction return event received" , "id" , event ["id" ].(string ))
413
415
@@ -466,7 +468,7 @@ func (s *TransactionsTestSuite) TestDiscardQueuedTransaction() {
466
468
err := json .Unmarshal ([]byte (jsonEvent ), & envelope )
467
469
s .NoError (err , fmt .Sprintf ("cannot unmarshal JSON: %s" , jsonEvent ))
468
470
469
- if envelope .Type == txqueue .EventTransactionQueued {
471
+ if envelope .Type == transactions .EventTransactionQueued {
470
472
event := envelope .Event .(map [string ]interface {})
471
473
txID := common .QueuedTxID (event ["id" ].(string ))
472
474
log .Info ("transaction queued (will be discarded soon)" , "id" , txID )
@@ -488,12 +490,12 @@ func (s *TransactionsTestSuite) TestDiscardQueuedTransaction() {
488
490
close (completeQueuedTransaction )
489
491
}
490
492
491
- if envelope .Type == txqueue .EventTransactionFailed {
493
+ if envelope .Type == transactions .EventTransactionFailed {
492
494
event := envelope .Event .(map [string ]interface {})
493
495
log .Info ("transaction return event received" , "id" , event ["id" ].(string ))
494
496
495
497
receivedErrMessage := event ["error_message" ].(string )
496
- expectedErrMessage := txqueue .ErrQueuedTxDiscarded .Error ()
498
+ expectedErrMessage := queue .ErrQueuedTxDiscarded .Error ()
497
499
s .Equal (receivedErrMessage , expectedErrMessage )
498
500
499
501
receivedErrCode := event ["error_code" ].(string )
@@ -509,7 +511,7 @@ func (s *TransactionsTestSuite) TestDiscardQueuedTransaction() {
509
511
To : common .ToAddress (TestConfig .Account2 .Address ),
510
512
Value : (* hexutil .Big )(big .NewInt (1000000000000 )),
511
513
})
512
- s .EqualError (err , txqueue .ErrQueuedTxDiscarded .Error (), "transaction is expected to be discarded" )
514
+ s .EqualError (err , queue .ErrQueuedTxDiscarded .Error (), "transaction is expected to be discarded" )
513
515
514
516
select {
515
517
case <- completeQueuedTransaction :
@@ -543,7 +545,7 @@ func (s *TransactionsTestSuite) TestCompleteMultipleQueuedTransactions() {
543
545
err := json .Unmarshal ([]byte (jsonEvent ), & envelope )
544
546
s .NoError (err , fmt .Sprintf ("cannot unmarshal JSON: %s" , jsonEvent ))
545
547
546
- if envelope .Type == txqueue .EventTransactionQueued {
548
+ if envelope .Type == transactions .EventTransactionQueued {
547
549
event := envelope .Event .(map [string ]interface {})
548
550
txID := common .QueuedTxID (event ["id" ].(string ))
549
551
log .Info ("transaction queued (will be completed in a single call, once aggregated)" , "id" , txID )
@@ -640,7 +642,7 @@ func (s *TransactionsTestSuite) TestDiscardMultipleQueuedTransactions() {
640
642
var envelope signal.Envelope
641
643
err := json .Unmarshal ([]byte (jsonEvent ), & envelope )
642
644
s .NoError (err )
643
- if envelope .Type == txqueue .EventTransactionQueued {
645
+ if envelope .Type == transactions .EventTransactionQueued {
644
646
event := envelope .Event .(map [string ]interface {})
645
647
txID := common .QueuedTxID (event ["id" ].(string ))
646
648
log .Info ("transaction queued (will be discarded soon)" , "id" , txID )
@@ -650,12 +652,12 @@ func (s *TransactionsTestSuite) TestDiscardMultipleQueuedTransactions() {
650
652
txIDs <- txID
651
653
}
652
654
653
- if envelope .Type == txqueue .EventTransactionFailed {
655
+ if envelope .Type == transactions .EventTransactionFailed {
654
656
event := envelope .Event .(map [string ]interface {})
655
657
log .Info ("transaction return event received" , "id" , event ["id" ].(string ))
656
658
657
659
receivedErrMessage := event ["error_message" ].(string )
658
- expectedErrMessage := txqueue .ErrQueuedTxDiscarded .Error ()
660
+ expectedErrMessage := queue .ErrQueuedTxDiscarded .Error ()
659
661
s .Equal (receivedErrMessage , expectedErrMessage )
660
662
661
663
receivedErrCode := event ["error_code" ].(string )
@@ -675,7 +677,7 @@ func (s *TransactionsTestSuite) TestDiscardMultipleQueuedTransactions() {
675
677
To : common .ToAddress (TestConfig .Account2 .Address ),
676
678
Value : (* hexutil .Big )(big .NewInt (1000000000000 )),
677
679
})
678
- s .EqualError (err , txqueue .ErrQueuedTxDiscarded .Error ())
680
+ s .EqualError (err , queue .ErrQueuedTxDiscarded .Error ())
679
681
680
682
s .True (reflect .DeepEqual (txHashCheck , gethcommon.Hash {}), "transaction returned hash, while it shouldn't" )
681
683
}
@@ -747,7 +749,7 @@ func (s *TransactionsTestSuite) TestNonExistentQueuedTransactions() {
747
749
// try completing non-existing transaction
748
750
_ , err := s .Backend .CompleteTransaction ("some-bad-transaction-id" , TestConfig .Account1 .Password )
749
751
s .Error (err , "error expected and not received" )
750
- s .EqualError (err , txqueue .ErrQueuedTxIDNotFound .Error ())
752
+ s .EqualError (err , queue .ErrQueuedTxIDNotFound .Error ())
751
753
}
752
754
753
755
func (s * TransactionsTestSuite ) TestEvictionOfQueuedTransactions () {
@@ -756,6 +758,24 @@ func (s *TransactionsTestSuite) TestEvictionOfQueuedTransactions() {
756
758
757
759
backend := s .LightEthereumService ().StatusBackend
758
760
s .NotNil (backend )
761
+ var m sync.Mutex
762
+ txCount := 0
763
+ txIDs := [queue .DefaultTxQueueCap + 5 + 10 ]common.QueuedTxID {}
764
+
765
+ signal .SetDefaultNodeNotificationHandler (func (rawSignal string ) {
766
+ var sg signal.Envelope
767
+ err := json .Unmarshal ([]byte (rawSignal ), & sg )
768
+ s .NoError (err )
769
+
770
+ if sg .Type == transactions .EventTransactionQueued {
771
+ event := sg .Event .(map [string ]interface {})
772
+ txID := event ["id" ].(string )
773
+ m .Lock ()
774
+ txIDs [txCount ] = common .QueuedTxID (txID )
775
+ txCount ++
776
+ m .Unlock ()
777
+ }
778
+ })
759
779
760
780
// reset queue
761
781
s .Backend .TxQueueManager ().TransactionQueue ().Reset ()
@@ -764,36 +784,26 @@ func (s *TransactionsTestSuite) TestEvictionOfQueuedTransactions() {
764
784
s .NoError (s .Backend .AccountManager ().SelectAccount (TestConfig .Account1 .Address , TestConfig .Account1 .Password ))
765
785
766
786
txQueue := s .Backend .TxQueueManager ().TransactionQueue ()
767
- var i = 0
768
- txIDs := [txqueue .DefaultTxQueueCap + 5 + 10 ]common.QueuedTxID {}
769
- s .Backend .TxQueueManager ().SetTransactionQueueHandler (func (queuedTx * common.QueuedTx ) {
770
- log .Info ("tx enqueued" , "i" , i + 1 , "queue size" , txQueue .Count (), "id" , queuedTx .ID )
771
- txIDs [i ] = queuedTx .ID
772
- i ++
773
- })
774
-
775
787
s .Zero (txQueue .Count (), "transaction count should be zero" )
776
788
777
789
for j := 0 ; j < 10 ; j ++ {
778
790
go s .Backend .SendTransaction (context .TODO (), common.SendTxArgs {}) // nolint: errcheck
779
791
}
780
- time .Sleep (2 * time .Second ) // FIXME(tiabc): more reliable synchronization to ensure all transactions are enqueued
781
-
782
- log .Info (fmt .Sprintf ("Number of transactions queued: %d. Queue size (shouldn't be more than %d): %d" ,
783
- i , txqueue .DefaultTxQueueCap , txQueue .Count ()))
792
+ time .Sleep (2 * time .Second )
784
793
794
+ log .Info (fmt .Sprintf ("Number of transactions sent: %d. Queue size (shouldn't be more than %d): %d" ,
795
+ txCount , queue .DefaultTxQueueCap , txQueue .Count ()))
785
796
s .Equal (10 , txQueue .Count (), "transaction count should be 10" )
786
797
787
- for i := 0 ; i < txqueue .DefaultTxQueueCap + 5 ; i ++ { // stress test by hitting with lots of goroutines
798
+ for i := 0 ; i < queue .DefaultTxQueueCap + 5 ; i ++ { // stress test by hitting with lots of goroutines
788
799
go s .Backend .SendTransaction (context .TODO (), common.SendTxArgs {}) // nolint: errcheck
789
800
}
790
801
time .Sleep (5 * time .Second )
791
802
792
- s .True (txQueue .Count () <= txqueue .DefaultTxQueueCap , "transaction count should be %d (or %d): got %d" , txqueue .DefaultTxQueueCap , txqueue .DefaultTxQueueCap - 1 , txQueue .Count ())
803
+ s .True (txQueue .Count () <= queue .DefaultTxQueueCap , "transaction count should be %d (or %d): got %d" , queue .DefaultTxQueueCap , queue .DefaultTxQueueCap - 1 , txQueue .Count ())
793
804
794
805
for _ , txID := range txIDs {
795
806
txQueue .Remove (txID )
796
807
}
797
-
798
808
s .Zero (txQueue .Count (), "transaction count should be zero: %d" , txQueue .Count ())
799
809
}
0 commit comments