-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstore.go
557 lines (480 loc) · 15.8 KB
/
store.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package mtg
import (
"context"
"database/sql"
"encoding/hex"
"fmt"
"strings"
"time"
"github.com/MixinNetwork/mixin/crypto"
"github.com/gofrs/uuid/v5"
)
func (s *SQLite3Store) ListActions(ctx context.Context, state ActionState, limit int) ([]*Action, error) {
query := fmt.Sprintf("SELECT %s FROM actions JOIN outputs ON actions.output_id=outputs.output_id WHERE action_state=? ORDER BY actions.sequence ASC", strings.Join(actionJoinCols, ","))
if limit > 0 {
query += fmt.Sprintf(" LIMIT %d", limit)
}
rows, err := s.db.QueryContext(ctx, query, state)
if err != nil {
return nil, err
}
defer rows.Close()
var as []*Action
for rows.Next() {
a, err := actionJoinFromRow(rows)
if err != nil {
return nil, err
}
as = append(as, a)
}
return as, nil
}
func (s *SQLite3Store) readOutput(ctx context.Context, tx *sql.Tx, id string) (*UnifiedOutput, error) {
query := fmt.Sprintf("SELECT %s FROM outputs WHERE output_id=?", strings.Join(outputCols, ","))
row := tx.QueryRowContext(ctx, query, id)
return outputFromRow(row)
}
func (s *SQLite3Store) ReadOutputByHashAndIndex(ctx context.Context, hash string, index uint) (*UnifiedOutput, error) {
query := fmt.Sprintf("SELECT %s FROM outputs WHERE transaction_hash=? AND output_index=?", strings.Join(outputCols, ","))
row := s.db.QueryRowContext(ctx, query, hash, index)
return outputFromRow(row)
}
func (s *SQLite3Store) readAction(ctx context.Context, tx *sql.Tx, id string) (*Action, error) {
query := fmt.Sprintf("SELECT %s FROM actions WHERE output_id=?", strings.Join(actionCols, ","))
row := tx.QueryRowContext(ctx, query, id)
return actionFromRow(row)
}
func (s *SQLite3Store) readRestorableAction(ctx context.Context, txn *sql.Tx, t *Transaction) (*Action, error) {
if len(t.references) != 1 {
return nil, nil
}
hash := t.references[0].String()
query := fmt.Sprintf("SELECT %s FROM actions WHERE action_state=? AND transaction_hash=?", strings.Join(actionCols, ","))
row := txn.QueryRowContext(ctx, query, ActionStateRestorable, hash)
return actionFromRow(row)
}
func (s *SQLite3Store) finishAction(ctx context.Context, tx *sql.Tx, id string, state ActionState, ts []*Transaction) error {
act, err := s.readAction(ctx, tx, id)
if err != nil || act == nil || act.ActionState != ActionStateInitial {
return fmt.Errorf("invalid action to finish => %v %v", act, err)
}
err = s.execOne(ctx, tx, "UPDATE actions SET action_state=? WHERE output_id=? AND action_state=?", state, id, ActionStateInitial)
if err != nil {
return fmt.Errorf("UPDATE actions %v", err)
}
for _, t := range ts {
if len(t.consumed) == 0 {
panic(t.TraceId)
}
if t.State != TransactionStateInitial {
panic(t.TraceId)
}
if t.IsStorage() {
if t.AssetId != StorageAssetId || t.Threshold != 64 || len(t.Receivers) != 1 {
return fmt.Errorf("invalid storage transaction: %#v", t)
}
}
sequence := act.Sequence
if act.restoreSequence > act.Sequence {
sequence = act.restoreSequence
}
if t.Sequence != sequence {
panic(t.Sequence)
}
existed, err := s.checkExistence(ctx, tx, "SELECT trace_id FROM transactions WHERE trace_id=?", t.TraceId)
if err != nil {
return err
}
if existed {
continue
}
err = s.execOne(ctx, tx, buildInsertionSQL("transactions", transactionCols), t.values()...)
if err != nil {
return fmt.Errorf("INSERT transactions %v", err)
}
for _, o := range t.consumed {
query := "UPDATE outputs SET state=?,trace_id=?,updated_at=? WHERE output_id=? AND state=?"
err = s.execOne(ctx, tx, query, SafeUtxoStateAssigned, t.TraceId, time.Now().UTC(), o.OutputId, SafeUtxoStateUnspent)
if err != nil {
return fmt.Errorf("UPDATE outputs %v", err)
}
}
}
return nil
}
func (s *SQLite3Store) FinishAction(ctx context.Context, id string, state ActionState, ts []*Transaction) error {
s.mutex.Lock()
defer s.mutex.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer rollBack(tx)
err = s.finishAction(ctx, tx, id, state, ts)
if err != nil {
return err
}
return tx.Commit()
}
func (s *SQLite3Store) writeOutputAndAction(ctx context.Context, tx *sql.Tx, out *UnifiedOutput, state ActionState) error {
if out.State != SafeUtxoStateUnspent {
panic(out.OutputId)
}
aid := uuid.Must(uuid.FromString(out.AppId))
if aid.String() != out.AppId {
panic(out.AppId)
}
oldAct, err := s.readAction(ctx, tx, out.OutputId)
if err != nil {
return err
}
oldOutput, err := s.readOutput(ctx, tx, out.OutputId)
if err != nil {
return err
}
switch {
case oldAct == nil && oldOutput == nil:
case oldAct != nil && oldOutput != nil:
return nil
default:
reason := fmt.Errorf("action or output exists: %v %v", oldAct, oldOutput)
panic(reason)
}
out.updatedAt = time.Now().UTC()
err = s.execOne(ctx, tx, buildInsertionSQL("outputs", outputCols), out.values()...)
if err != nil {
return fmt.Errorf("INSERT outputs %v", err)
}
a := Action{
ActionState: state,
restoreSequence: 0,
}
a.Sequence = out.Sequence
a.OutputId = out.OutputId
a.TransactionHash = out.TransactionHash
err = s.execOne(ctx, tx, buildInsertionSQL("actions", actionCols), a.values()...)
if err != nil {
return fmt.Errorf("INSERT actions %v", err)
}
return nil
}
func (s *SQLite3Store) WriteAction(ctx context.Context, out *UnifiedOutput, state ActionState) error {
s.mutex.Lock()
defer s.mutex.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer rollBack(tx)
err = s.writeOutputAndAction(ctx, tx, out, state)
if err != nil {
return err
}
return tx.Commit()
}
func (s *SQLite3Store) RestoreAction(ctx context.Context, act *Action, t *Transaction) error {
s.mutex.Lock()
defer s.mutex.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer rollBack(tx)
rAct, err := s.readRestorableAction(ctx, tx, t)
if err != nil || rAct == nil {
return fmt.Errorf("readRestorableAction(%v) => %v %v", t, rAct, err)
}
query := "UPDATE actions SET action_state=?,restore_sequence=? WHERE output_id=? AND action_state=?"
err = s.execOne(ctx, tx, query, ActionStateInitial, act.Sequence, rAct.OutputId, ActionStateRestorable)
if err != nil {
return fmt.Errorf("UPDATE actions %v", err)
}
err = s.finishAction(ctx, tx, act.OutputId, ActionStateDone, nil)
if err != nil {
return err
}
return tx.Commit()
}
func (s *SQLite3Store) listOutputs(ctx context.Context, ids []string) ([]*UnifiedOutput, error) {
cols := strings.Join(outputCols, ",")
sets := "'" + strings.Join(ids, "','") + "'"
query := fmt.Sprintf("SELECT %s FROM outputs WHERE output_id IN (%s) ORDER BY sequence ASC", cols, sets)
rows, err := s.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var os []*UnifiedOutput
for rows.Next() {
o, err := outputFromRow(rows)
if err != nil {
return nil, err
}
os = append(os, o)
}
return os, nil
}
func (s *SQLite3Store) ListOutputsForTransaction(ctx context.Context, traceId string, sequence uint64) ([]*UnifiedOutput, error) {
query := fmt.Sprintf("SELECT %s FROM outputs WHERE trace_id=? AND sequence<=? ORDER BY trace_id, sequence ASC", strings.Join(outputCols, ","))
rows, err := s.db.QueryContext(ctx, query, traceId, sequence)
if err != nil {
return nil, err
}
defer rows.Close()
var os []*UnifiedOutput
for rows.Next() {
o, err := outputFromRow(rows)
if err != nil {
return nil, err
}
os = append(os, o)
}
return os, nil
}
func (s *SQLite3Store) ListOutputsByTransactionHash(ctx context.Context, hash string, sequence uint64) ([]*UnifiedOutput, error) {
query := fmt.Sprintf("SELECT %s FROM outputs WHERE transaction_hash=? AND sequence<=? ORDER BY transaction_hash, sequence ASC", strings.Join(outputCols, ","))
rows, err := s.db.QueryContext(ctx, query, hash, sequence)
if err != nil {
return nil, err
}
defer rows.Close()
var os []*UnifiedOutput
for rows.Next() {
o, err := outputFromRow(rows)
if err != nil {
return nil, err
}
os = append(os, o)
}
return os, nil
}
func (s *SQLite3Store) ListOutputsForAsset(ctx context.Context, appId, assetId string, consumedUntil, sequence uint64, state SafeUtxoState, limit int) ([]*UnifiedOutput, error) {
query := fmt.Sprintf("SELECT %s FROM outputs WHERE app_id=? AND asset_id=? AND state=? AND sequence>? AND sequence<=? ORDER BY app_id, asset_id, state, sequence ASC", strings.Join(outputCols, ","))
if limit > 0 {
query += fmt.Sprintf(" LIMIT %d", limit)
}
rows, err := s.db.QueryContext(ctx, query, appId, assetId, state, consumedUntil, sequence)
if err != nil {
return nil, err
}
defer rows.Close()
var os []*UnifiedOutput
for rows.Next() {
o, err := outputFromRow(rows)
if err != nil {
return nil, err
}
os = append(os, o)
}
return os, nil
}
func (s *SQLite3Store) UpdateTxWithOutputs(ctx context.Context, t *Transaction, os []*UnifiedOutput) error {
s.mutex.Lock()
defer s.mutex.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer rollBack(tx)
var refs []string
for _, r := range t.references {
refs = append(refs, r.String())
}
err = s.execOne(ctx, tx, "UPDATE transactions SET raw=?,hash=?,refs=?,state=?,request_id=?,updated_at=? WHERE trace_id=? AND state=?",
hex.EncodeToString(t.Raw), t.Hash.String(), strings.Join(refs, ","), t.State, t.requestId, t.UpdatedAt, t.TraceId, TransactionStateInitial)
if err != nil {
return fmt.Errorf("UPDATE transactions %v", err)
}
for _, o := range os {
query := "UPDATE outputs SET state=?,signed_by=?,updated_at=? WHERE output_id=? AND state=? AND trace_id=?"
err = s.execOne(ctx, tx, query, o.State, o.SignedBy, t.UpdatedAt, o.OutputId, SafeUtxoStateAssigned, t.TraceId)
if err != nil {
return fmt.Errorf("UPDATE outputs %v", err)
}
}
return tx.Commit()
}
func (s *SQLite3Store) FinishTransaction(ctx context.Context, traceId string) error {
s.mutex.Lock()
defer s.mutex.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer rollBack(tx)
err = s.execOne(ctx, tx, "UPDATE transactions SET state=?, updated_at=? WHERE trace_id=? AND state=?",
TransactionStateSnapshot, time.Now(), traceId, TransactionStateSigned)
if err != nil {
return fmt.Errorf("UPDATE transactions %v", err)
}
_, err = tx.ExecContext(ctx, "UPDATE outputs SET state=?,updated_at=? WHERE trace_id=? AND state=?",
SafeUtxoStateSpent, time.Now().UTC(), traceId, SafeUtxoStateSigned)
if err != nil {
return fmt.Errorf("UPDATE outputs %v", err)
}
return tx.Commit()
}
func (s *SQLite3Store) ConfirmWithdrawalTransaction(ctx context.Context, traceId, hash string) error {
s.mutex.Lock()
defer s.mutex.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer rollBack(tx)
err = s.execOne(ctx, tx, "UPDATE transactions SET withdrawal_hash=?, updated_at=? WHERE trace_id=? AND state=? AND destination IS NOT NULL AND withdrawal_hash IS NULL",
hash, time.Now(), traceId, TransactionStateSnapshot)
if err != nil {
return fmt.Errorf("UPDATE transactions %v", err)
}
return tx.Commit()
}
func (s *SQLite3Store) readIteration(ctx context.Context, txn *sql.Tx, id string) (*Iteration, error) {
query := fmt.Sprintf("SELECT %s FROM iterations WHERE node_id=?", strings.Join(iterationCols, ","))
row := txn.QueryRowContext(ctx, query, id)
return iterationFromRow(row)
}
func (s *SQLite3Store) ListIterations(ctx context.Context) ([]*Iteration, error) {
query := fmt.Sprintf("SELECT %s FROM iterations ORDER BY node_id,created_at ASC", strings.Join(iterationCols, ","))
rows, err := s.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var irs []*Iteration
for rows.Next() {
i, err := iterationFromRow(rows)
if err != nil {
return nil, err
}
irs = append(irs, i)
}
return irs, nil
}
func (s *SQLite3Store) WriteIteration(ctx context.Context, ir *Iteration) error {
s.mutex.Lock()
defer s.mutex.Unlock()
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer rollBack(tx)
old, err := s.readIteration(ctx, tx, ir.NodeId)
if err != nil {
return err
}
if old != nil && old.Action >= ir.Action {
return nil
}
if old != nil {
err = s.execOne(ctx, tx, "UPDATE iterations SET action=?, threshold=?, created_at=? WHERE node_id=?", ir.Action, ir.Threshold, ir.CreatedAt, ir.NodeId)
if err != nil {
return fmt.Errorf("UPDATE iterations %v", err)
}
} else {
err = s.execOne(ctx, tx, buildInsertionSQL("iterations", iterationCols), ir.values()...)
if err != nil {
return fmt.Errorf("INSERT iterations %v", err)
}
}
return tx.Commit()
}
func (s *SQLite3Store) ListPreviousInitialTransactions(ctx context.Context, asset string, sequence uint64) ([]*Transaction, error) {
query := fmt.Sprintf("SELECT %s FROM transactions where asset_id=? AND state=? AND sequence<=? ORDER BY asset_id, state, sequence ASC", strings.Join(transactionCols, ","))
rows, err := s.db.QueryContext(ctx, query, asset, sequence, TransactionStateInitial)
if err != nil {
return nil, err
}
defer rows.Close()
var ts []*Transaction
for rows.Next() {
t, err := transactionFromRow(rows)
if err != nil {
return nil, err
}
ts = append(ts, t)
}
return ts, nil
}
func (s *SQLite3Store) ListTransactions(ctx context.Context, state, limit int) ([]*Transaction, map[string][]*Transaction, error) {
query := fmt.Sprintf("SELECT %s FROM transactions where state=? ORDER BY state,sequence,trace_id ASC", strings.Join(transactionCols, ","))
if limit > 0 {
query += fmt.Sprintf(" LIMIT %d", limit)
}
rows, err := s.db.QueryContext(ctx, query, state)
if err != nil {
return nil, nil, err
}
defer rows.Close()
var ts []*Transaction
assetTxMap := make(map[string][]*Transaction)
for rows.Next() {
t, err := transactionFromRow(rows)
if err != nil {
return nil, nil, err
}
ts = append(ts, t)
assetTxMap[t.AssetId] = append(assetTxMap[t.AssetId], t)
}
return ts, assetTxMap, nil
}
func (s *SQLite3Store) ListUnconfirmedWithdrawalTransactions(ctx context.Context, limit int) ([]*Transaction, error) {
query := fmt.Sprintf("SELECT %s FROM transactions where state=? AND destination IS NOT NULL AND withdrawal_hash IS NULL ORDER BY state,sequence,trace_id ASC", strings.Join(transactionCols, ","))
if limit > 0 {
query += fmt.Sprintf(" LIMIT %d", limit)
}
rows, err := s.db.QueryContext(ctx, query, TransactionStateSnapshot)
if err != nil {
return nil, err
}
defer rows.Close()
var ts []*Transaction
for rows.Next() {
t, err := transactionFromRow(rows)
if err != nil {
return nil, err
}
ts = append(ts, t)
}
return ts, nil
}
func (s *SQLite3Store) ListConfirmedWithdrawalTransactionsAfter(ctx context.Context, offset time.Time, limit int) ([]*Transaction, error) {
query := fmt.Sprintf("SELECT %s FROM transactions where state=? AND withdrawal_hash IS NOT NULL AND updated_at>? ORDER BY updated_at ASC", strings.Join(transactionCols, ","))
if limit > 0 {
query += fmt.Sprintf(" LIMIT %d", limit)
}
rows, err := s.db.QueryContext(ctx, query, TransactionStateSnapshot, offset)
if err != nil {
return nil, err
}
defer rows.Close()
var ts []*Transaction
for rows.Next() {
t, err := transactionFromRow(rows)
if err != nil {
return nil, err
}
ts = append(ts, t)
}
return ts, nil
}
func (s *SQLite3Store) ReadTransactionByHash(ctx context.Context, hash crypto.Hash) (*Transaction, error) {
query := fmt.Sprintf("SELECT %s FROM transactions WHERE hash=?", strings.Join(transactionCols, ","))
row := s.db.QueryRowContext(ctx, query, hash.String())
return transactionFromRow(row)
}
func (s *SQLite3Store) ReadTransactionByTraceId(ctx context.Context, id string) (*Transaction, error) {
query := fmt.Sprintf("SELECT %s FROM transactions WHERE trace_id=?", strings.Join(transactionCols, ","))
row := s.db.QueryRowContext(ctx, query, id)
return transactionFromRow(row)
}
type Row interface {
Scan(dest ...any) error
}
func rollBack(txn *sql.Tx) {
err := txn.Rollback()
const already = "transaction has already been committed or rolled back"
if err != nil && !strings.Contains(err.Error(), already) {
panic(err)
}
}