multi: thread context through FetchPayment
This commit is contained in:
11 files changed
+55
-28
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
@@ -52,7 +53,8 @@ type ControlTower interface {
|
||||
|
||||
// FetchPayment fetches the payment corresponding to the given payment
|
||||
// hash.
|
||||
FetchPayment(paymentHash lntypes.Hash) (paymentsdb.DBMPPayment, error)
|
||||
FetchPayment(ctx context.Context,
|
||||
paymentHash lntypes.Hash) (paymentsdb.DBMPPayment, error)
|
||||
|
||||
// FailPayment transitions a payment into the Failed state, and records
|
||||
// the ultimate reason the payment failed. Note that this should only
|
||||
@@ -164,6 +166,8 @@ func NewControlTower(db paymentsdb.DB) ControlTower {
|
||||
func (p *controlTower) InitPayment(paymentHash lntypes.Hash,
|
||||
info *paymentsdb.PaymentCreationInfo) error {
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
err := p.db.InitPayment(paymentHash, info)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -174,7 +178,7 @@ func (p *controlTower) InitPayment(paymentHash lntypes.Hash,
|
||||
p.paymentsMtx.Lock(paymentHash)
|
||||
defer p.paymentsMtx.Unlock(paymentHash)
|
||||
|
||||
payment, err := p.db.FetchPayment(paymentHash)
|
||||
payment, err := p.db.FetchPayment(ctx, paymentHash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -250,10 +254,11 @@ func (p *controlTower) FailAttempt(paymentHash lntypes.Hash,
|
||||
}
|
||||
|
||||
// FetchPayment fetches the payment corresponding to the given payment hash.
|
||||
func (p *controlTower) FetchPayment(paymentHash lntypes.Hash) (
|
||||
func (p *controlTower) FetchPayment(ctx context.Context,
|
||||
paymentHash lntypes.Hash) (
|
||||
paymentsdb.DBMPPayment, error) {
|
||||
|
||||
return p.db.FetchPayment(paymentHash)
|
||||
return p.db.FetchPayment(ctx, paymentHash)
|
||||
}
|
||||
|
||||
// FailPayment transitions a payment into the Failed state, and records the
|
||||
@@ -293,12 +298,14 @@ func (p *controlTower) FetchInFlightPayments() ([]*paymentsdb.MPPayment,
|
||||
func (p *controlTower) SubscribePayment(paymentHash lntypes.Hash) (
|
||||
ControlTowerSubscriber, error) {
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
// Take lock before querying the db to prevent missing or duplicating an
|
||||
// update.
|
||||
p.paymentsMtx.Lock(paymentHash)
|
||||
defer p.paymentsMtx.Unlock(paymentHash)
|
||||
|
||||
payment, err := p.db.FetchPayment(paymentHash)
|
||||
payment, err := p.db.FetchPayment(ctx, paymentHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
@@ -509,8 +510,8 @@ func (m *mockControlTowerOld) FailPayment(phash lntypes.Hash,
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockControlTowerOld) FetchPayment(phash lntypes.Hash) (
|
||||
paymentsdb.DBMPPayment, error) {
|
||||
func (m *mockControlTowerOld) FetchPayment(_ context.Context,
|
||||
phash lntypes.Hash) (paymentsdb.DBMPPayment, error) {
|
||||
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
@@ -786,8 +787,8 @@ func (m *mockControlTower) FailPayment(phash lntypes.Hash,
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *mockControlTower) FetchPayment(phash lntypes.Hash) (
|
||||
paymentsdb.DBMPPayment, error) {
|
||||
func (m *mockControlTower) FetchPayment(_ context.Context,
|
||||
phash lntypes.Hash) (paymentsdb.DBMPPayment, error) {
|
||||
|
||||
args := m.Called(phash)
|
||||
|
||||
|
||||
@@ -1114,7 +1114,9 @@ func (p *paymentLifecycle) patchLegacyPaymentHash(
|
||||
func (p *paymentLifecycle) reloadInflightAttempts() (paymentsdb.DBMPPayment,
|
||||
error) {
|
||||
|
||||
payment, err := p.router.cfg.Control.FetchPayment(p.identifier)
|
||||
ctx := context.TODO()
|
||||
|
||||
payment, err := p.router.cfg.Control.FetchPayment(ctx, p.identifier)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1139,8 +1141,10 @@ func (p *paymentLifecycle) reloadInflightAttempts() (paymentsdb.DBMPPayment,
|
||||
func (p *paymentLifecycle) reloadPayment() (paymentsdb.DBMPPayment,
|
||||
*paymentsdb.MPPaymentState, error) {
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
// Read the db to get the latest state of the payment.
|
||||
payment, err := p.router.cfg.Control.FetchPayment(p.identifier)
|
||||
payment, err := p.router.cfg.Control.FetchPayment(ctx, p.identifier)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
+3
-1
@@ -1064,13 +1064,15 @@ func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route,
|
||||
firstHopCustomRecords lnwire.CustomRecords) (*paymentsdb.HTLCAttempt,
|
||||
error) {
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
// Helper function to fail a payment. It makes sure the payment is only
|
||||
// failed once so that the failure reason is not overwritten.
|
||||
failPayment := func(paymentIdentifier lntypes.Hash,
|
||||
reason paymentsdb.FailureReason) error {
|
||||
|
||||
payment, fetchErr := r.cfg.Control.FetchPayment(
|
||||
paymentIdentifier,
|
||||
ctx, paymentIdentifier,
|
||||
)
|
||||
if fetchErr != nil {
|
||||
return fetchErr
|
||||
|
||||
@@ -1097,7 +1097,9 @@ func TestSendPaymentErrorPathPruning(t *testing.T) {
|
||||
require.Equal(t, paymentsdb.FailureReasonNoRoute, err)
|
||||
|
||||
// Inspect the two attempts that were made before the payment failed.
|
||||
p, err := ctx.router.cfg.Control.FetchPayment(*payment.paymentHash)
|
||||
p, err := ctx.router.cfg.Control.FetchPayment(
|
||||
t.Context(), *payment.paymentHash,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
htlcs := p.GetHTLCs()
|
||||
|
||||
Reference in new issue
Block a user