-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnonce.go
304 lines (246 loc) · 7.73 KB
/
nonce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package transaction
import (
"context"
"errors"
"sync"
"time"
"github.com/mr-tron/base58"
"github.com/code-payments/code-server/pkg/retry"
"github.com/code-payments/code-server/pkg/solana"
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
"github.com/code-payments/code-server/pkg/code/data/nonce"
)
var (
ErrNoAvailableNonces = errors.New("no available nonces")
)
var (
// Temporary global lock, so we avoid any chance of double locking a nonce,
// since we can't check the status of a sync.Mutx.
globalNonceLock sync.Mutex
// Can't used striped lock because we need to hold mutliple nonces at once, so
// deadlock would be possible. This is fine for now, given our nonce pool has
// a fixed and relatively small size.
nonceLocksMu sync.Mutex
nonceLocks map[string]*sync.Mutex
)
func init() {
nonceLocks = make(map[string]*sync.Mutex)
}
// SelectedNonce is a nonce that is available and selected for use in a transaction.
// Implementations should unlock the lock after using the nonce. If used, its state
// must be updated as reserved.
type SelectedNonce struct {
localLock sync.Mutex
distributedLock *sync.Mutex // todo: Use a distributed lock
isUnlocked bool
data code_data.Provider
record *nonce.Record
Account *common.Account
Blockhash solana.Blockhash
}
// SelectAvailableNonce selects an available from the nonce pool for the specified
// use case. The returned nonce is marked as reserved without a signature, so it
// cannot be selected again. It's the responsibility of the external caller to make
// it available again if it doesn't get assigned a fulfillment.
func SelectAvailableNonce(ctx context.Context, data code_data.Provider, useCase nonce.Purpose) (*SelectedNonce, error) {
var lock *sync.Mutex
var account *common.Account
var bh solana.Blockhash
var record *nonce.Record
_, err := retry.Retry(func() error {
globalNonceLock.Lock()
defer globalNonceLock.Unlock()
randomRecord, err := data.GetRandomAvailableNonceByPurpose(ctx, useCase)
if err == nonce.ErrNonceNotFound {
return ErrNoAvailableNonces
} else if err != nil {
return err
}
record = randomRecord
lock = getNonceLock(record.Address)
lock.Lock()
// Refetch because the state could have changed by the time we got the lock
record, err = data.GetNonce(ctx, record.Address)
if err != nil {
lock.Unlock()
return err
}
if record.State != nonce.StateAvailable {
// Unlock and try again
lock.Unlock()
return errors.New("selected nonce that became unavailable")
}
account, err = common.NewAccountFromPublicKeyString(record.Address)
if err != nil {
lock.Unlock()
return err
}
untypedBlockhash, err := base58.Decode(record.Blockhash)
if err != nil {
lock.Unlock()
return err
}
copy(bh[:], untypedBlockhash)
// Reserve the nonce for use with a fulfillment
record.State = nonce.StateReserved
err = data.SaveNonce(ctx, record)
if err != nil {
lock.Unlock()
return err
}
return nil
}, retry.NonRetriableErrors(context.Canceled, ErrNoAvailableNonces), retry.Limit(5))
if err != nil {
return nil, err
}
return &SelectedNonce{
distributedLock: lock,
data: data,
record: record,
Account: account,
Blockhash: bh,
}, nil
}
// SelectNonceFromFulfillmentToUpgrade selects a nonce from a fulfillment that
// is going to be upgraded.
func SelectNonceFromFulfillmentToUpgrade(ctx context.Context, data code_data.Provider, fulfillmentRecord *fulfillment.Record) (*SelectedNonce, error) {
if fulfillmentRecord.State != fulfillment.StateUnknown {
return nil, errors.New("dangerous nonce selection from fulfillment")
}
if fulfillmentRecord.Nonce == nil {
return nil, errors.New("fulfillment doesn't have an assigned nonce")
}
lock := getNonceLock(*fulfillmentRecord.Nonce)
lock.Lock()
// Fetch after locking to get most up-to-date state
nonceRecord, err := data.GetNonce(ctx, *fulfillmentRecord.Nonce)
if err != nil {
lock.Unlock()
return nil, err
}
if nonceRecord.State != nonce.StateReserved {
lock.Unlock()
return nil, errors.New("nonce isn't reserved")
}
if nonceRecord.Blockhash != *fulfillmentRecord.Blockhash {
lock.Unlock()
return nil, errors.New("fulfillment record doesn't have the right blockhash")
}
if nonceRecord.Signature != *fulfillmentRecord.Signature {
lock.Unlock()
return nil, errors.New("nonce isn't mapped to selected fulfillment")
}
account, err := common.NewAccountFromPublicKeyString(nonceRecord.Address)
if err != nil {
lock.Unlock()
return nil, err
}
var bh solana.Blockhash
untypedBlockhash, err := base58.Decode(*fulfillmentRecord.Blockhash)
if err != nil {
lock.Unlock()
return nil, err
}
copy(bh[:], untypedBlockhash)
return &SelectedNonce{
distributedLock: lock,
data: data,
record: nonceRecord,
Account: account,
Blockhash: bh,
}, nil
}
// MarkReservedWithSignature marks the nonce as reserved with a signature
func (n *SelectedNonce) MarkReservedWithSignature(ctx context.Context, sig string) error {
if len(sig) == 0 {
return errors.New("signature is empty")
}
n.localLock.Lock()
defer n.localLock.Unlock()
if n.isUnlocked {
return errors.New("nonce is unlocked")
}
if n.record.Signature == sig {
return nil
}
if len(n.record.Signature) != 0 {
return errors.New("nonce already has a different signature")
}
// Nonce is reserved without a signature, so update its signature
if n.record.State == nonce.StateReserved {
n.record.Signature = sig
return n.data.SaveNonce(ctx, n.record)
}
if n.record.State != nonce.StateAvailable {
return errors.New("nonce must be available to reserve")
}
n.record.State = nonce.StateReserved
n.record.Signature = sig
return n.data.SaveNonce(ctx, n.record)
}
// UpdateSignature updates the signature for a reserved nonce. The use case here
// being transactions that share a nonce, and the new transaction being designated
// as the one to submit to the blockchain.
func (n *SelectedNonce) UpdateSignature(ctx context.Context, sig string) error {
if len(sig) == 0 {
return errors.New("signature is empty")
}
n.localLock.Lock()
defer n.localLock.Unlock()
if n.isUnlocked {
return errors.New("nonce is unlocked")
}
if n.record.Signature == sig {
return nil
}
if n.record.State != nonce.StateReserved {
return errors.New("nonce must be in a reserved state")
}
n.record.Signature = sig
return n.data.SaveNonce(ctx, n.record)
}
// ReleaseIfNotReserved makes a nonce available if it hasn't been reserved with
// a signature. It's recommended to call this in tandem with Unlock when the
// caller knows it's safe to go from the reserved to available state (ie. don't
// use this in uprade flows!).
func (n *SelectedNonce) ReleaseIfNotReserved() error {
n.localLock.Lock()
defer n.localLock.Unlock()
if n.isUnlocked {
return errors.New("nonce is unlocked")
}
if n.record.State == nonce.StateAvailable {
return nil
}
// A nonce is not fully reserved if it's state is reserved, but there is no
// assigned signature.
if n.record.State == nonce.StateReserved && len(n.record.Signature) == 0 {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
n.record.State = nonce.StateAvailable
return n.data.SaveNonce(ctx, n.record)
}
return nil
}
func (n *SelectedNonce) Unlock() {
n.localLock.Lock()
defer n.localLock.Unlock()
if n.isUnlocked {
return
}
n.isUnlocked = true
n.distributedLock.Unlock()
}
func getNonceLock(address string) *sync.Mutex {
nonceLocksMu.Lock()
defer nonceLocksMu.Unlock()
lock, ok := nonceLocks[address]
if !ok {
var mu sync.Mutex
lock = &mu
nonceLocks[address] = &mu
}
return lock
}