/
settle_transaction.go
563 lines (489 loc) · 14.6 KB
/
settle_transaction.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
558
559
560
561
562
563
package endpoint
import (
"context"
"encoding/base64"
"fmt"
"math/big"
"net/http"
"time"
"golang.org/x/crypto/scrypt"
"github.com/spolu/settle/lib/db"
"github.com/spolu/settle/lib/errors"
"github.com/spolu/settle/lib/format"
"github.com/spolu/settle/lib/ptr"
"github.com/spolu/settle/lib/svc"
"github.com/spolu/settle/mint"
"github.com/spolu/settle/mint/async"
"github.com/spolu/settle/mint/async/task"
"github.com/spolu/settle/mint/lib/authentication"
"github.com/spolu/settle/mint/lib/plan"
"github.com/spolu/settle/mint/model"
"goji.io/pat"
)
const (
// EndPtSettleTransaction settles a reserved transaction.
EndPtSettleTransaction EndPtName = "SettleTransaction"
)
func init() {
registrar[EndPtSettleTransaction] = NewSettleTransaction
}
// SettleTransaction creates a new transaction.
type SettleTransaction struct {
Client *mint.Client
// Parameters
Hop int8
ID string
Token string
Owner string
Secret string
// State
Tx *model.Transaction
Plan *plan.TxPlan
}
// NewSettleTransaction constructs and initialiezes the endpoint.
func NewSettleTransaction(
r *http.Request,
) (Endpoint, error) {
ctx := r.Context()
client := &mint.Client{}
err := client.Init(ctx)
if err != nil {
return nil, errors.Trace(err) // 500
}
return &SettleTransaction{
Client: client,
}, nil
}
// Validate validates the input parameters.
func (e *SettleTransaction) Validate(
r *http.Request,
) error {
ctx := r.Context()
switch authentication.Get(ctx).Status {
case authentication.AutStSkipped:
// Validate hop.
hop, err := ValidateHop(ctx, r.PostFormValue("hop"))
if err != nil {
return errors.Trace(err)
}
e.Hop = *hop
// Validate secret.
secret, err := ValidateSecret(ctx, r.PostFormValue("secret"))
if err != nil {
return errors.Trace(err)
}
e.Secret = *secret
}
// Validate id.
id, owner, token, err := ValidateID(ctx, pat.Param(r, "transaction"))
if err != nil {
return errors.Trace(err)
}
e.ID = *id
e.Token = *token
e.Owner = *owner
return nil
}
// Execute executes the endpoint.
func (e *SettleTransaction) Execute(
ctx context.Context,
) (*int, *svc.Resp, error) {
switch authentication.Get(ctx).Status {
case authentication.AutStSkipped:
return e.ExecutePropagated(ctx)
case authentication.AutStSucceeded:
return e.ExecuteCanonical(ctx)
}
return nil, nil, errors.Trace(errors.Newf(
"Authentication status not expected: %s",
authentication.Get(ctx).Status))
}
// ExecuteCanonical executes the canonical settlement of a transaction (owner
// mint).
func (e *SettleTransaction) ExecuteCanonical(
ctx context.Context,
) (*int, *svc.Resp, error) {
oCtx := ctx
ctx = db.Begin(oCtx, "mint")
defer db.LoggedRollback(ctx)
tx, err := model.LoadCanonicalTransactionByOwnerToken(ctx,
e.Owner, e.Token)
if err != nil {
return nil, nil, errors.Trace(err) // 500
} else if tx == nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
404, "transaction_not_found",
"The transaction you are trying to settle does not "+
"exist: %s.", e.ID,
))
}
e.Tx = tx
owner := fmt.Sprintf("%s@%s",
authentication.Get(ctx).User.Username,
mint.GetHost(ctx))
if e.Tx.Owner != owner {
return nil, nil, errors.Trace(errors.NewUserErrorf(err,
402, "settlement_not_authorized",
"Only the owner of the transaction can settle it: %s"+
e.Tx.Owner,
))
}
// Transaction can be either reserved or settled.
switch e.Tx.Status {
case mint.TxStCanceled:
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "settlement_failed",
"The transaction you are trying to settle is canceled: %s.",
e.ID,
))
case mint.TxStPending:
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "settlement_failed",
"The transaction you are trying to settle is pending: %s ",
e.ID,
))
}
// For canonical settlement we can do away with a shallow plan.
pl, err := plan.Compute(ctx, e.Client, e.Tx, true)
if err != nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(err,
402, "settlement_failed",
"The plan computation for the transaction failed: %s", e.ID,
))
}
e.Plan = pl
// Settle the transaction definitely before we reveal the secret (even if
// it eventually fails).
e.Tx.Status = mint.TxStSettled
err = e.Tx.Save(ctx)
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
db.Commit(ctx)
// At the canonical mint the settlement propagation starts from a virtual
// Hop which is the length of the plan hops plus one.
e.Hop = int8(len(e.Plan.Hops))
e.Secret = *e.Tx.Secret
err = e.Propagate(ctx)
if err != nil {
// If propagation failed we log it and trigger an asyncrhonous one.
mint.Logf(ctx,
"Settlement propagation failed: transaction=%s hop=%d error=%s",
e.ID, e.Hop, err.Error())
err = async.Queue(ctx,
task.NewPropagateSettlement(ctx,
time.Now(), fmt.Sprintf("%s|%d", e.ID, e.Hop)))
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
}
ctx = db.Begin(oCtx, "mint")
defer db.LoggedRollback(ctx)
// Reload the tranasction post propagation.
tx, err = model.LoadTransactionByID(ctx, e.ID)
if err != nil || tx == nil {
return nil, nil, errors.Trace(err) // 500
}
e.Tx = tx
switch e.Tx.Status {
case mint.TxStSettled:
default:
return nil, nil, errors.Newf(
"Unexpected transaction status %s: %s", e.Tx.Status, e.ID) // 500
}
ops, err := model.LoadCanonicalOperationsByTransaction(ctx, e.ID)
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
crs, err := model.LoadCanonicalCrossingsByTransaction(ctx, e.ID)
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
// Commit the transaction in settled state.
db.Commit(ctx)
return ptr.Int(http.StatusOK), &svc.Resp{
"transaction": format.JSONPtr(model.NewTransactionResource(ctx,
tx, ops, crs,
)),
}, nil
}
// ExecutePropagated executes the settlement of a propagated transaction
// (involved mint).
func (e *SettleTransaction) ExecutePropagated(
ctx context.Context,
) (*int, *svc.Resp, error) {
oCtx := ctx
ctx = db.Begin(oCtx, "mint")
defer db.LoggedRollback(ctx)
tx, err := model.LoadTransactionByID(ctx, e.ID)
if err != nil {
return nil, nil, errors.Trace(err) // 500
} else if tx == nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
404, "transaction_not_found",
"The transaction you are trying to settle does not exist: %s.",
e.ID,
))
}
e.Tx = tx
// We first compute a shallow plan to check whether we should cancel
pl, err := plan.Compute(ctx, e.Client, e.Tx, true)
if err != nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(err,
402, "settlement_failed",
"The plan computation for the transaction failed: %s", e.ID,
))
}
if int(e.Hop) >= len(pl.Hops) ||
pl.Hops[e.Hop].Mint != mint.GetHost(ctx) {
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "settlement_failed",
"The hop provided (%d) does not match the current mint (%s) for "+
"transaction: %s", e.Hop, mint.GetHost(ctx), e.ID,
))
}
// Check for potential opportunity to cancel before settling.
if pl.CheckShouldCancel(ctx, e.Client, e.Hop) {
// Commit the transaction while we cancel.
db.Commit(ctx)
// Attempt to trigger a cancelation locally as we know it should
// cancel. If we fail, just continue as we were.
_, err := e.Client.CancelTransaction(ctx,
e.ID, e.Hop, mint.GetHost(ctx))
if err != nil {
mint.Logf(ctx,
"Opportunistic cancellation failed: transaction=%s hop=%d error=%s",
e.ID, e.Hop, err.Error())
}
// Reopen a DB transaction and reload the transaction, hopefully
// updated.
ctx = db.Begin(oCtx, "mint")
defer db.LoggedRollback(ctx)
tx, err = model.LoadTransactionByID(ctx, e.ID)
if err != nil || tx == nil {
return nil, nil, errors.Trace(err) // 500
}
e.Tx = tx
}
// Transaction can be either reserved or settled.
switch e.Tx.Status {
case mint.TxStCanceled:
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "settlement_failed",
"The transaction you are trying to settle is canceled: %s.",
e.ID,
))
case mint.TxStPending:
return nil, nil, errors.Trace(errors.NewUserErrorf(nil,
402, "settlement_failed",
"The transaction you are trying to settle is pending: %s ",
e.ID,
))
}
h, err := scrypt.Key([]byte(e.Secret), []byte(e.Tx.Token), 16384, 8, 1, 64)
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
if e.Tx.Lock != base64.StdEncoding.EncodeToString(h) {
return nil, nil, errors.Trace(errors.NewUserErrorf(err,
402, "settlement_failed",
"The secret provided does not match the lock value for "+
"transaction: %s", e.ID,
))
}
// Compute now the full plan to execute settlement.
pl, err = plan.Compute(ctx, e.Client, e.Tx, false)
if err != nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(err,
402, "settlement_failed",
"The plan computation for the transaction failed: %s", e.ID,
))
}
e.Plan = pl
// Settle will idempotently (at specified hop) settle the
// transaction (generally called on highest hop first). Subsequent calls (on
// same hop will be no-ops).
err = e.Settle(ctx)
if err != nil {
return nil, nil, errors.Trace(errors.NewUserErrorf(err,
402, "settlement_failed",
"The settlement execution failed for the transaction: %s", e.ID,
))
}
// Mark the transaction as settled (if there's a loop we'll call settle on
// the other hop even if marked as settled) and store the secret.
e.Tx.Status = mint.TxStSettled
e.Tx.Secret = &e.Secret
err = e.Tx.Save(ctx)
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
ops, err := model.LoadCanonicalOperationsByTransaction(ctx, e.ID)
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
crs, err := model.LoadCanonicalCrossingsByTransaction(ctx, e.ID)
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
// Commit the transaction as well as operations and crossings as settled.
db.Commit(ctx)
err = e.Propagate(ctx)
if err != nil {
// If propagation failed we log it and trigger an asyncrhonous one.
mint.Logf(ctx,
"Settlement propagation failed: transaction=%s hop=%d error=%s",
e.ID, e.Hop, err.Error())
err = async.Queue(ctx,
task.NewPropagateSettlement(ctx,
time.Now(), fmt.Sprintf("%s|%d", e.ID, e.Hop)))
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
}
return ptr.Int(http.StatusOK), &svc.Resp{
"transaction": format.JSONPtr(model.NewTransactionResource(ctx,
tx, ops, crs,
)),
}, nil
}
// Settle settles idempotently the underlying operations and crossings at the
// current hop.
func (e *SettleTransaction) Settle(
ctx context.Context,
) error {
if int(e.Hop) >= len(e.Plan.Hops) {
return errors.Trace(errors.Newf(
"Hop (%d) is higher than the transaction plan length (%d)",
e.Hop, len(e.Plan.Hops)))
}
h := e.Plan.Hops[e.Hop]
mint.Logf(ctx,
"Executing settlement plan: transaction=%s hop=%d", e.ID, e.Hop)
// Settle the OpAction (should always be defined)
if h.OpAction != nil {
op, err := model.LoadCanonicalOperationByTransactionHop(ctx,
e.ID, e.Hop)
if err != nil {
return errors.Trace(err)
} else if op == nil {
return errors.Trace(errors.Newf(
"Operation not found for transaction %s and hop %d",
e.ID, h))
}
if op.Status == mint.TxStSettled {
mint.Logf(ctx,
"Skipped operation: id=%s[%s] created=%q propagation=%s "+
"asset=%s source=%s destination=%s amount=%s "+
"status=%s transaction=%s",
op.Owner, op.Token, op.Created, op.Propagation, op.Asset,
op.Source, op.Destination, (*big.Int)(&op.Amount).String(),
op.Status, *op.Transaction)
} else {
a := h.OpAction
asset, err := model.LoadCanonicalAssetByName(ctx, *a.OperationAsset)
if err != nil {
return errors.Trace(err)
} else if asset == nil {
return errors.Trace(errors.Newf(
"Asset not found: %s", *a.OperationAsset))
}
var dstBalance *model.Balance
if a.OperationDestination != nil &&
asset.Owner != *a.OperationDestination {
dstBalance, err =
model.LoadOrCreateCanonicalBalanceByAssetHolder(ctx,
asset.Owner, *a.OperationAsset, *a.OperationDestination)
if err != nil {
return errors.Trace(err)
}
}
if dstBalance != nil {
(*big.Int)(&dstBalance.Value).Add(
(*big.Int)(&dstBalance.Value), (*big.Int)(&op.Amount))
// Checks if the dstBalance is positive and not overflown.
b := (*big.Int)(&dstBalance.Value)
if new(big.Int).Abs(b).Cmp(model.MaxAssetAmount) >= 0 ||
b.Cmp(new(big.Int)) < 0 {
return errors.Trace(errors.Newf(
"Invalid resulting balance: %s", b.String()))
}
err = dstBalance.Save(ctx)
if err != nil {
return errors.Trace(err)
}
err = async.Queue(ctx,
task.NewPropagateBalance(ctx, time.Now(), dstBalance.ID()))
if err != nil {
return errors.Trace(err)
}
}
op.Status = mint.TxStSettled
err = op.Save(ctx)
if err != nil {
return errors.Trace(err)
}
mint.Logf(ctx,
"Settled operation: id=%s[%s] created=%q propagation=%s "+
"asset=%s source=%s destination=%s amount=%s "+
"status=%s transaction=%s",
op.Owner, op.Token, op.Created, op.Propagation, op.Asset,
op.Source, op.Destination, (*big.Int)(&op.Amount).String(),
op.Status, *op.Transaction)
opID := op.ID()
err = async.Queue(ctx,
task.NewPropagateOperation(ctx, time.Now(), opID))
if err != nil {
return errors.Trace(err)
}
}
}
if h.CrAction != nil {
cr, err := model.LoadCanonicalCrossingByTransactionHop(ctx,
e.ID, e.Hop)
if err != nil {
return errors.Trace(err)
} else if cr == nil {
return errors.Trace(errors.Newf(
"Crossing not found for transaction %s and hop %d",
e.ID, h))
}
if cr.Status == mint.TxStSettled {
mint.Logf(ctx,
"Skipped crossing: id=%s[%s] created=%q offer=%s amount=%s "+
"status=%s transaction=%s",
cr.Owner, cr.Token, cr.Created, cr.Offer,
(*big.Int)(&cr.Amount).String(), cr.Status, cr.Transaction)
} else {
cr.Status = mint.TxStSettled
err = cr.Save(ctx)
if err != nil {
return errors.Trace(err)
}
mint.Logf(ctx,
"Settled crossing: id=%s[%s] created=%q offer=%s amount=%s "+
"status=%s transaction=%s",
cr.Owner, cr.Token, cr.Created, cr.Offer,
(*big.Int)(&cr.Amount).String(), cr.Status, cr.Transaction)
}
}
return nil
}
// Propagate the lock for settlement. Current hop settlement is already
// performed.
func (e *SettleTransaction) Propagate(
ctx context.Context,
) error {
if int(e.Hop)-1 >= 0 {
m := e.Plan.Hops[e.Hop-1].Mint
mint.Logf(ctx,
"Propagating settlement: transaction=%s hop=%d mint=%s",
e.ID, e.Hop, m)
hop := e.Hop - 1
_, err := e.Client.SettleTransaction(ctx, e.ID, &hop, &e.Secret, &m)
if err != nil {
return errors.Trace(err)
}
}
return nil
}