multi: thread context through SettleAttempt
This commit is contained in:
9 files changed
+37
-28
No files matched your search
@@ -86,7 +86,8 @@ type PaymentControl interface {
|
|||||||
// error to prevent us from making duplicate payments to the same
|
// error to prevent us from making duplicate payments to the same
|
||||||
// payment hash. The provided preimage is atomically saved to the DB
|
// payment hash. The provided preimage is atomically saved to the DB
|
||||||
// for record keeping.
|
// for record keeping.
|
||||||
SettleAttempt(lntypes.Hash, uint64, *HTLCSettleInfo) (*MPPayment, error)
|
SettleAttempt(context.Context, lntypes.Hash, uint64,
|
||||||
|
*HTLCSettleInfo) (*MPPayment, error)
|
||||||
|
|
||||||
// FailAttempt marks the given payment attempt failed.
|
// FailAttempt marks the given payment attempt failed.
|
||||||
FailAttempt(lntypes.Hash, uint64, *HTLCFailInfo) (*MPPayment, error)
|
FailAttempt(lntypes.Hash, uint64, *HTLCFailInfo) (*MPPayment, error)
|
||||||
|
|||||||
@@ -430,7 +430,7 @@ func (p *KVStore) RegisterAttempt(_ context.Context, paymentHash lntypes.Hash,
|
|||||||
// After invoking this method, InitPayment should always return an error to
|
// After invoking this method, InitPayment should always return an error to
|
||||||
// prevent us from making duplicate payments to the same payment hash. The
|
// prevent us from making duplicate payments to the same payment hash. The
|
||||||
// provided preimage is atomically saved to the DB for record keeping.
|
// provided preimage is atomically saved to the DB for record keeping.
|
||||||
func (p *KVStore) SettleAttempt(hash lntypes.Hash,
|
func (p *KVStore) SettleAttempt(_ context.Context, hash lntypes.Hash,
|
||||||
attemptID uint64, settleInfo *HTLCSettleInfo) (*MPPayment, error) {
|
attemptID uint64, settleInfo *HTLCSettleInfo) (*MPPayment, error) {
|
||||||
|
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ func TestKVStoreDeleteNonInFlight(t *testing.T) {
|
|||||||
case p.success:
|
case p.success:
|
||||||
// Verifies that status was changed to StatusSucceeded.
|
// Verifies that status was changed to StatusSucceeded.
|
||||||
_, err := paymentDB.SettleAttempt(
|
_, err := paymentDB.SettleAttempt(
|
||||||
info.PaymentIdentifier, attempt.AttemptID,
|
ctx, info.PaymentIdentifier, attempt.AttemptID,
|
||||||
&HTLCSettleInfo{
|
&HTLCSettleInfo{
|
||||||
Preimage: preimg,
|
Preimage: preimg,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ func createTestPayments(t *testing.T, p DB, payments []*payment) {
|
|||||||
// Settle the attempt
|
// Settle the attempt
|
||||||
case StatusSucceeded:
|
case StatusSucceeded:
|
||||||
_, err := p.SettleAttempt(
|
_, err := p.SettleAttempt(
|
||||||
info.PaymentIdentifier, attempt.AttemptID,
|
ctx, info.PaymentIdentifier, attempt.AttemptID,
|
||||||
&HTLCSettleInfo{
|
&HTLCSettleInfo{
|
||||||
Preimage: preimg,
|
Preimage: preimg,
|
||||||
},
|
},
|
||||||
@@ -1643,6 +1643,7 @@ func TestSuccessesWithoutInFlight(t *testing.T) {
|
|||||||
|
|
||||||
// Attempt to complete the payment should fail.
|
// Attempt to complete the payment should fail.
|
||||||
_, err = paymentDB.SettleAttempt(
|
_, err = paymentDB.SettleAttempt(
|
||||||
|
t.Context(),
|
||||||
info.PaymentIdentifier, 0,
|
info.PaymentIdentifier, 0,
|
||||||
&HTLCSettleInfo{
|
&HTLCSettleInfo{
|
||||||
Preimage: preimg,
|
Preimage: preimg,
|
||||||
@@ -1790,7 +1791,7 @@ func TestSwitchDoubleSend(t *testing.T) {
|
|||||||
|
|
||||||
// After settling, the error should be ErrAlreadyPaid.
|
// After settling, the error should be ErrAlreadyPaid.
|
||||||
_, err = paymentDB.SettleAttempt(
|
_, err = paymentDB.SettleAttempt(
|
||||||
info.PaymentIdentifier, attempt.AttemptID,
|
ctx, info.PaymentIdentifier, attempt.AttemptID,
|
||||||
&HTLCSettleInfo{
|
&HTLCSettleInfo{
|
||||||
Preimage: preimg,
|
Preimage: preimg,
|
||||||
},
|
},
|
||||||
@@ -1926,7 +1927,7 @@ func TestSwitchFail(t *testing.T) {
|
|||||||
// Settle the attempt and verify that status was changed to
|
// Settle the attempt and verify that status was changed to
|
||||||
// StatusSucceeded.
|
// StatusSucceeded.
|
||||||
payment, err = paymentDB.SettleAttempt(
|
payment, err = paymentDB.SettleAttempt(
|
||||||
info.PaymentIdentifier, attempt.AttemptID,
|
ctx, info.PaymentIdentifier, attempt.AttemptID,
|
||||||
&HTLCSettleInfo{
|
&HTLCSettleInfo{
|
||||||
Preimage: preimg,
|
Preimage: preimg,
|
||||||
},
|
},
|
||||||
@@ -2099,7 +2100,7 @@ func TestMultiShard(t *testing.T) {
|
|||||||
var firstFailReason *FailureReason
|
var firstFailReason *FailureReason
|
||||||
if test.settleFirst {
|
if test.settleFirst {
|
||||||
_, err := paymentDB.SettleAttempt(
|
_, err := paymentDB.SettleAttempt(
|
||||||
info.PaymentIdentifier, a.AttemptID,
|
ctx, info.PaymentIdentifier, a.AttemptID,
|
||||||
&HTLCSettleInfo{
|
&HTLCSettleInfo{
|
||||||
Preimage: preimg,
|
Preimage: preimg,
|
||||||
},
|
},
|
||||||
@@ -2193,7 +2194,7 @@ func TestMultiShard(t *testing.T) {
|
|||||||
if test.settleLast {
|
if test.settleLast {
|
||||||
// Settle the last outstanding attempt.
|
// Settle the last outstanding attempt.
|
||||||
_, err = paymentDB.SettleAttempt(
|
_, err = paymentDB.SettleAttempt(
|
||||||
info.PaymentIdentifier, a.AttemptID,
|
ctx, info.PaymentIdentifier, a.AttemptID,
|
||||||
&HTLCSettleInfo{
|
&HTLCSettleInfo{
|
||||||
Preimage: preimg,
|
Preimage: preimg,
|
||||||
},
|
},
|
||||||
@@ -2683,7 +2684,7 @@ func TestQueryPayments(t *testing.T) {
|
|||||||
copy(preimg[:], rev[:])
|
copy(preimg[:], rev[:])
|
||||||
|
|
||||||
_, err = paymentDB.SettleAttempt(
|
_, err = paymentDB.SettleAttempt(
|
||||||
lastPaymentInfo.PaymentIdentifier,
|
ctx, lastPaymentInfo.PaymentIdentifier,
|
||||||
attempt.AttemptID,
|
attempt.AttemptID,
|
||||||
&HTLCSettleInfo{
|
&HTLCSettleInfo{
|
||||||
Preimage: preimg,
|
Preimage: preimg,
|
||||||
@@ -2813,7 +2814,7 @@ func TestFetchInFlightPayments(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
_, err = paymentDB.SettleAttempt(
|
_, err = paymentDB.SettleAttempt(
|
||||||
payments[2].id, 5,
|
ctx, payments[2].id, 5,
|
||||||
&HTLCSettleInfo{
|
&HTLCSettleInfo{
|
||||||
Preimage: preimg,
|
Preimage: preimg,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1496,8 +1496,9 @@ func (s *SQLStore) insertRouteHops(ctx context.Context, db SQLQueries,
|
|||||||
// the PaymentWriter interface and ultimately the DB interface. It represents
|
// the PaymentWriter interface and ultimately the DB interface. It represents
|
||||||
// step 2 in the payment lifecycle control flow, called after InitPayment and
|
// step 2 in the payment lifecycle control flow, called after InitPayment and
|
||||||
// potentially multiple times for multi-path payments.
|
// potentially multiple times for multi-path payments.
|
||||||
func (s *SQLStore) RegisterAttempt(ctx context.Context, paymentHash lntypes.Hash,
|
func (s *SQLStore) RegisterAttempt(ctx context.Context,
|
||||||
attempt *HTLCAttemptInfo) (*MPPayment, error) {
|
paymentHash lntypes.Hash, attempt *HTLCAttemptInfo) (*MPPayment,
|
||||||
|
error) {
|
||||||
|
|
||||||
var mpPayment *MPPayment
|
var mpPayment *MPPayment
|
||||||
|
|
||||||
@@ -1621,11 +1622,9 @@ func (s *SQLStore) RegisterAttempt(ctx context.Context, paymentHash lntypes.Hash
|
|||||||
// the PaymentWriter interface and ultimately the DB interface. It represents
|
// the PaymentWriter interface and ultimately the DB interface. It represents
|
||||||
// step 3a in the payment lifecycle control flow (step 3b is FailAttempt),
|
// step 3a in the payment lifecycle control flow (step 3b is FailAttempt),
|
||||||
// called after RegisterAttempt when an HTLC successfully completes.
|
// called after RegisterAttempt when an HTLC successfully completes.
|
||||||
func (s *SQLStore) SettleAttempt(paymentHash lntypes.Hash,
|
func (s *SQLStore) SettleAttempt(ctx context.Context, paymentHash lntypes.Hash,
|
||||||
attemptID uint64, settleInfo *HTLCSettleInfo) (*MPPayment, error) {
|
attemptID uint64, settleInfo *HTLCSettleInfo) (*MPPayment, error) {
|
||||||
|
|
||||||
ctx := context.TODO()
|
|
||||||
|
|
||||||
var mpPayment *MPPayment
|
var mpPayment *MPPayment
|
||||||
|
|
||||||
err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {
|
err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {
|
||||||
|
|||||||
@@ -44,8 +44,8 @@ type ControlTower interface {
|
|||||||
// for record keeping.
|
// for record keeping.
|
||||||
//
|
//
|
||||||
// NOTE: Subscribers should be notified by the new state of the payment.
|
// NOTE: Subscribers should be notified by the new state of the payment.
|
||||||
SettleAttempt(lntypes.Hash, uint64, *paymentsdb.HTLCSettleInfo) (
|
SettleAttempt(context.Context, lntypes.Hash, uint64,
|
||||||
*paymentsdb.HTLCAttempt, error)
|
*paymentsdb.HTLCSettleInfo) (*paymentsdb.HTLCAttempt, error)
|
||||||
|
|
||||||
// FailAttempt marks the given payment attempt failed.
|
// FailAttempt marks the given payment attempt failed.
|
||||||
//
|
//
|
||||||
@@ -217,14 +217,17 @@ func (p *controlTower) RegisterAttempt(ctx context.Context,
|
|||||||
// SettleAttempt marks the given attempt settled with the preimage. If
|
// SettleAttempt marks the given attempt settled with the preimage. If
|
||||||
// this is a multi shard payment, this might implicitly mean the the
|
// this is a multi shard payment, this might implicitly mean the the
|
||||||
// full payment succeeded.
|
// full payment succeeded.
|
||||||
func (p *controlTower) SettleAttempt(paymentHash lntypes.Hash,
|
func (p *controlTower) SettleAttempt(ctx context.Context,
|
||||||
attemptID uint64, settleInfo *paymentsdb.HTLCSettleInfo) (
|
paymentHash lntypes.Hash, attemptID uint64,
|
||||||
*paymentsdb.HTLCAttempt, error) {
|
settleInfo *paymentsdb.HTLCSettleInfo) (*paymentsdb.HTLCAttempt,
|
||||||
|
error) {
|
||||||
|
|
||||||
p.paymentsMtx.Lock(paymentHash)
|
p.paymentsMtx.Lock(paymentHash)
|
||||||
defer p.paymentsMtx.Unlock(paymentHash)
|
defer p.paymentsMtx.Unlock(paymentHash)
|
||||||
|
|
||||||
payment, err := p.db.SettleAttempt(paymentHash, attemptID, settleInfo)
|
payment, err := p.db.SettleAttempt(
|
||||||
|
ctx, paymentHash, attemptID, settleInfo,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,7 +108,8 @@ func TestControlTowerSubscribeSuccess(t *testing.T) {
|
|||||||
Preimage: preimg,
|
Preimage: preimg,
|
||||||
}
|
}
|
||||||
htlcAttempt, err := pControl.SettleAttempt(
|
htlcAttempt, err := pControl.SettleAttempt(
|
||||||
info.PaymentIdentifier, attempt.AttemptID, &settleInfo,
|
t.Context(), info.PaymentIdentifier, attempt.AttemptID,
|
||||||
|
&settleInfo,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -246,7 +247,8 @@ func TestKVStoreSubscribeAllSuccess(t *testing.T) {
|
|||||||
Preimage: preimg1,
|
Preimage: preimg1,
|
||||||
}
|
}
|
||||||
htlcAttempt1, err := pControl.SettleAttempt(
|
htlcAttempt1, err := pControl.SettleAttempt(
|
||||||
info1.PaymentIdentifier, attempt1.AttemptID, &settleInfo1,
|
t.Context(), info1.PaymentIdentifier, attempt1.AttemptID,
|
||||||
|
&settleInfo1,
|
||||||
)
|
)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(
|
require.Equal(
|
||||||
@@ -259,7 +261,8 @@ func TestKVStoreSubscribeAllSuccess(t *testing.T) {
|
|||||||
Preimage: preimg2,
|
Preimage: preimg2,
|
||||||
}
|
}
|
||||||
htlcAttempt2, err := pControl.SettleAttempt(
|
htlcAttempt2, err := pControl.SettleAttempt(
|
||||||
info2.PaymentIdentifier, attempt2.AttemptID, &settleInfo2,
|
t.Context(), info2.PaymentIdentifier, attempt2.AttemptID,
|
||||||
|
&settleInfo2,
|
||||||
)
|
)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(
|
require.Equal(
|
||||||
|
|||||||
@@ -408,8 +408,8 @@ func (m *mockControlTowerOld) RegisterAttempt(_ context.Context,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockControlTowerOld) SettleAttempt(phash lntypes.Hash,
|
func (m *mockControlTowerOld) SettleAttempt(_ context.Context,
|
||||||
pid uint64, settleInfo *paymentsdb.HTLCSettleInfo) (
|
phash lntypes.Hash, pid uint64, settleInfo *paymentsdb.HTLCSettleInfo) (
|
||||||
*paymentsdb.HTLCAttempt, error) {
|
*paymentsdb.HTLCAttempt, error) {
|
||||||
|
|
||||||
if m.settleAttempt != nil {
|
if m.settleAttempt != nil {
|
||||||
@@ -753,7 +753,7 @@ func (m *mockControlTower) RegisterAttempt(_ context.Context,
|
|||||||
return args.Error(0)
|
return args.Error(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockControlTower) SettleAttempt(phash lntypes.Hash,
|
func (m *mockControlTower) SettleAttempt(_ context.Context, phash lntypes.Hash,
|
||||||
pid uint64, settleInfo *paymentsdb.HTLCSettleInfo) (
|
pid uint64, settleInfo *paymentsdb.HTLCSettleInfo) (
|
||||||
*paymentsdb.HTLCAttempt, error) {
|
*paymentsdb.HTLCAttempt, error) {
|
||||||
|
|
||||||
|
|||||||
@@ -1166,6 +1166,8 @@ func (p *paymentLifecycle) reloadPayment() (paymentsdb.DBMPPayment,
|
|||||||
func (p *paymentLifecycle) handleAttemptResult(attempt *paymentsdb.HTLCAttempt,
|
func (p *paymentLifecycle) handleAttemptResult(attempt *paymentsdb.HTLCAttempt,
|
||||||
result *htlcswitch.PaymentResult) (*attemptResult, error) {
|
result *htlcswitch.PaymentResult) (*attemptResult, error) {
|
||||||
|
|
||||||
|
ctx := context.TODO()
|
||||||
|
|
||||||
// If the result has an error, we need to further process it by failing
|
// If the result has an error, we need to further process it by failing
|
||||||
// the attempt and maybe fail the payment.
|
// the attempt and maybe fail the payment.
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
@@ -1187,7 +1189,7 @@ func (p *paymentLifecycle) handleAttemptResult(attempt *paymentsdb.HTLCAttempt,
|
|||||||
// In case of success we atomically store settle result to the DB and
|
// In case of success we atomically store settle result to the DB and
|
||||||
// move the shard to the settled state.
|
// move the shard to the settled state.
|
||||||
htlcAttempt, err := p.router.cfg.Control.SettleAttempt(
|
htlcAttempt, err := p.router.cfg.Control.SettleAttempt(
|
||||||
p.identifier, attempt.AttemptID,
|
ctx, p.identifier, attempt.AttemptID,
|
||||||
&paymentsdb.HTLCSettleInfo{
|
&paymentsdb.HTLCSettleInfo{
|
||||||
Preimage: result.Preimage,
|
Preimage: result.Preimage,
|
||||||
SettleTime: p.router.cfg.Clock.Now(),
|
SettleTime: p.router.cfg.Clock.Now(),
|
||||||
|
|||||||
Reference in new issue
Block a user