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,50 @@ 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+ blockIDNonExpired := identifierFixture()
77+ client := &testutils.MockClient{
78+ Client: &mocks.Client{},
79+ GetTransactionResultsByBlockIDFunc: func(ctx context.Context, blockID sdk.Identifier) ([]*sdk.TransactionResult, error) {
80+ if blockID == blockID10 {
81+ return []*sdk.TransactionResult{}, nil
82+ }
83+
84+ if blockID == blockIDNonExpired {
85+ return []*sdk.TransactionResult{
86+ {
87+ Status: sdk.TransactionStatusFinalized,
88+ Error: nil,
89+ Events: []sdk.Event{},
90+ BlockID: blockID,
91+ BlockHeight: blockHeight + accountKeyBlockExpiration - 1,
92+ TransactionID: identifierFixture(),
93+ CollectionID: identifierFixture(),
94+ ComputationUsage: 104_512,
95+ },
96+ }, nil
97+ }
98+
99+ return []*sdk.TransactionResult{
100+ {
101+ Status: sdk.TransactionStatusFinalized,
102+ Error: nil,
103+ Events: []sdk.Event{},
104+ BlockID: blockID,
105+ BlockHeight: blockHeight + accountKeyBlockExpiration,
106+ TransactionID: identifierFixture(),
107+ CollectionID: identifierFixture(),
108+ ComputationUsage: 104_512,
109+ },
110+ }, nil
111+ },
112+ }
113+ ks := New(context.Background(), keys, client, zerolog.Nop())
114+
70115 key, err := ks.Take()
71116 require.NoError(t, err)
72117
73- blockHeight := uint64(10)
74118 txID := identifierFixture()
75119 key.SetLockMetadata(txID, blockHeight)
76120
@@ -82,13 +126,21 @@ func TestTake(t *testing.T) {
82126 assert.Equal(t, key, ks.usedKeys[txID])
83127
84128 // notify for one block before the expiration block, key should still be reserved
85- ks.NotifyBlock(blockHeight + accountKeyBlockExpiration - 1)
129+ ks.NotifyBlock(blockIDNonExpired)
130+
131+ // Give some time to allow the KeyStore to check for the
132+ // transaction result statuses in the background.
133+ time.Sleep(time.Second * 2)
86134
87135 assert.True(t, key.inUse.Load())
88136 assert.Equal(t, key, ks.usedKeys[txID])
89137
90138 // notify for the expiration block
91- ks.NotifyBlock(blockHeight + accountKeyBlockExpiration)
139+ ks.NotifyBlock(identifierFixture())
140+
141+ // Give some time to allow the KeyStore to check for the
142+ // transaction result statuses in the background.
143+ time.Sleep(time.Second * 2)
92144
93145 // keystore and key should be reset
94146 assert.Equal(t, 2, ks.AvailableKeys())
@@ -106,9 +158,14 @@ func TestKeySigning(t *testing.T) {
106158 accountKey.Index = 0 // the fixture starts from index 1
107159 accountKey.SequenceNumber = 42
108160
109- ks := New([]*AccountKey{
110- NewAccountKey(*accountKey, address, signer),
111- })
161+ ks := New(
162+ context.Background(),
163+ []*AccountKey{
164+ NewAccountKey(*accountKey, address, signer),
165+ },
166+ testutils.SetupClientForRange(1, 100),
167+ zerolog.Nop(),
168+ )
112169
113170 key, err := ks.Take()
114171 require.NoError(t, err)
@@ -154,7 +211,7 @@ func TestConcurrentUse(t *testing.T) {
154211 keys = append(keys, key)
155212 }
156213
157- ks := New(keys)
214+ ks := New(context.Background(), keys, testutils.SetupClientForRange(1, 100), zerolog.Nop() )
158215
159216 g := errgroup.Group{}
160217 g.SetLimit(concurrentTxCount)
0 commit comments