@@ -3,25 +3,23 @@ package requester
33import (
44 "fmt"
55 "sync"
6- "time"
76
87 flowsdk "github.com/onflow/flow-go-sdk"
98 "github.com/onflow/flow-go-sdk/crypto"
10- flowGo "github.com/onflow/flow-go/model/flow"
119)
1210
1311var ErrNoKeysAvailable = fmt .Errorf ("no keys available" )
1412
15- const accountKeyExpiry = 10 * time . Second
13+ const accountKeyBlockExpiration = 1_000
1614
1715type AccountKey struct {
1816 flowsdk.AccountKey
1917
20- mu sync.Mutex
21- ks * KeyStore
22- Address flowsdk.Address
23- Signer crypto.Signer
24- lastUsed time. Time
18+ mu sync.Mutex
19+ ks * KeyStore
20+ Address flowsdk.Address
21+ Signer crypto.Signer
22+ lastUsedBlock uint64
2523}
2624
2725// Done unlocks a key after use and puts it back into the pool.
@@ -58,13 +56,14 @@ func (k *AccountKey) SetProposerPayerAndSign(
5856 SignEnvelope (k .Address , k .Index , k .Signer )
5957}
6058
61- func (k * AccountKey ) expired () bool {
62- return time .Since (k .lastUsed ) > flowGo .DefaultTransactionExpiry
63- }
64-
6559type KeyLock interface {
66- LockKey (txID flowsdk.Identifier , key * AccountKey )
60+ LockKey (
61+ txID flowsdk.Identifier ,
62+ referenceBlockHeight uint64 ,
63+ key * AccountKey ,
64+ )
6765 UnlockKey (txID flowsdk.Identifier )
66+ Notify (blockHeight uint64 )
6867}
6968
7069type KeyStore struct {
@@ -88,8 +87,6 @@ func NewKeyStore(keys []*AccountKey) *KeyStore {
8887 ks .size = len (keys )
8988 ks .availableKeys = availableKeys
9089
91- go ks .keyExpiryChecker ()
92-
9390 return ks
9491}
9592
@@ -106,11 +103,15 @@ func (k *KeyStore) Take() (*AccountKey, error) {
106103 }
107104}
108105
109- func (k * KeyStore ) LockKey (txID flowsdk.Identifier , key * AccountKey ) {
106+ func (k * KeyStore ) LockKey (
107+ txID flowsdk.Identifier ,
108+ referenceBlockHeight uint64 ,
109+ key * AccountKey ,
110+ ) {
110111 key .mu .Lock ()
111112 defer key .mu .Unlock ()
112113
113- key .lastUsed = time . Now ()
114+ key .lastUsedBlock = referenceBlockHeight
114115 k .usedKeys [txID ] = key
115116}
116117
@@ -122,12 +123,10 @@ func (k *KeyStore) UnlockKey(txID flowsdk.Identifier) {
122123 }
123124}
124125
125- func (k * KeyStore ) keyExpiryChecker () {
126- for range time .Tick (accountKeyExpiry ) {
127- for txID , key := range k .usedKeys {
128- if key .expired () {
129- k .UnlockKey (txID )
130- }
126+ func (k * KeyStore ) Notify (blockHeight uint64 ) {
127+ for txID , key := range k .usedKeys {
128+ if blockHeight - key .lastUsedBlock >= accountKeyBlockExpiration {
129+ k .UnlockKey (txID )
131130 }
132131 }
133132}
0 commit comments