11package keystore
22
33import (
4+ "context"
45 "errors"
56 "sync"
67 "testing"
78 "time"
89
10+ "github.com/onflow/flow-evm-gateway/services/testutils"
911 sdk "github.com/onflow/flow-go-sdk"
12+ "github.com/onflow/flow-go-sdk/access/mocks"
1013 "github.com/onflow/flow-go-sdk/test"
1114 "github.com/onflow/flow-go/utils/unittest"
15+ "github.com/rs/zerolog"
1216 "golang.org/x/sync/errgroup"
1317
1418 "github.com/stretchr/testify/assert"
@@ -25,7 +29,7 @@ func TestTake(t *testing.T) {
2529 keys = append (keys , NewAccountKey (* accountKey , addrGenerator .New (), signer ))
2630 }
2731
28- ks := New (keys )
32+ ks := New (context . Background (), keys , testutils . SetupClientForRange ( 1 , 100 ), zerolog . Nop () )
2933
3034 t .Run ("Take with no metadata updates" , func (t * testing.T ) {
3135 key , err := ks .Take ()
@@ -67,10 +71,34 @@ func TestTake(t *testing.T) {
6771 })
6872
6973 t .Run ("Take with expiration" , func (t * testing.T ) {
74+ blockHeight := uint64 (10 )
75+ blockID10 := identifierFixture ()
76+ client := & testutils.MockClient {
77+ Client : & mocks.Client {},
78+ GetTransactionResultsByBlockIDFunc : func (ctx context.Context , blockID sdk.Identifier ) ([]* sdk.TransactionResult , error ) {
79+ if blockID == blockID10 {
80+ return []* sdk.TransactionResult {}, nil
81+ }
82+
83+ return []* sdk.TransactionResult {
84+ {
85+ Status : sdk .TransactionStatusFinalized ,
86+ Error : nil ,
87+ Events : []sdk.Event {},
88+ BlockID : blockID ,
89+ BlockHeight : blockHeight + accountKeyBlockExpiration ,
90+ TransactionID : identifierFixture (),
91+ CollectionID : identifierFixture (),
92+ ComputationUsage : 104_512 ,
93+ },
94+ }, nil
95+ },
96+ }
97+ ks := New (context .Background (), keys , client , zerolog .Nop ())
98+
7099 key , err := ks .Take ()
71100 require .NoError (t , err )
72101
73- blockHeight := uint64 (10 )
74102 txID := identifierFixture ()
75103 key .SetLockMetadata (txID , blockHeight )
76104
@@ -82,13 +110,17 @@ func TestTake(t *testing.T) {
82110 assert .Equal (t , key , ks .usedKeys [txID ])
83111
84112 // notify for one block before the expiration block, key should still be reserved
85- ks .NotifyBlock (blockHeight + accountKeyBlockExpiration - 1 )
113+ ks .NotifyBlock (blockID10 )
86114
87115 assert .True (t , key .inUse .Load ())
88116 assert .Equal (t , key , ks .usedKeys [txID ])
89117
90118 // notify for the expiration block
91- ks .NotifyBlock (blockHeight + accountKeyBlockExpiration )
119+ ks .NotifyBlock (identifierFixture ())
120+
121+ // Give some time to allow the KeyStore to check for the
122+ // transaction result statuses in the background.
123+ time .Sleep (time .Second * 2 )
92124
93125 // keystore and key should be reset
94126 assert .Equal (t , 2 , ks .AvailableKeys ())
@@ -106,9 +138,14 @@ func TestKeySigning(t *testing.T) {
106138 accountKey .Index = 0 // the fixture starts from index 1
107139 accountKey .SequenceNumber = 42
108140
109- ks := New ([]* AccountKey {
110- NewAccountKey (* accountKey , address , signer ),
111- })
141+ ks := New (
142+ context .Background (),
143+ []* AccountKey {
144+ NewAccountKey (* accountKey , address , signer ),
145+ },
146+ testutils .SetupClientForRange (1 , 100 ),
147+ zerolog .Nop (),
148+ )
112149
113150 key , err := ks .Take ()
114151 require .NoError (t , err )
@@ -154,7 +191,7 @@ func TestConcurrentUse(t *testing.T) {
154191 keys = append (keys , key )
155192 }
156193
157- ks := New (keys )
194+ ks := New (context . Background (), keys , testutils . SetupClientForRange ( 1 , 100 ), zerolog . Nop () )
158195
159196 g := errgroup.Group {}
160197 g .SetLimit (concurrentTxCount )
0 commit comments