Merge pull request #9844 from ziggie1984/refactor-payments-code-03
Refactor Payment PR 3
This commit is contained in:
20 files changed
+538
-350
No files matched your search
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
paymentsdb "github.com/lightningnetwork/lnd/payments/db"
|
||||
"github.com/lightningnetwork/lnd/routing/route"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -47,13 +48,19 @@ var (
|
||||
func TestControlTowerSubscribeUnknown(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db := initDB(t, false)
|
||||
db := initDB(t)
|
||||
|
||||
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
|
||||
paymentDB, err := channeldb.NewKVPaymentsDB(
|
||||
db,
|
||||
paymentsdb.WithKeepFailedPaymentAttempts(true),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
pControl := NewControlTower(paymentDB)
|
||||
|
||||
// Subscription should fail when the payment is not known.
|
||||
_, err := pControl.SubscribePayment(lntypes.Hash{1})
|
||||
require.ErrorIs(t, err, channeldb.ErrPaymentNotInitiated)
|
||||
_, err = pControl.SubscribePayment(lntypes.Hash{1})
|
||||
require.ErrorIs(t, err, paymentsdb.ErrPaymentNotInitiated)
|
||||
}
|
||||
|
||||
// TestControlTowerSubscribeSuccess tests that payment updates for a
|
||||
@@ -61,9 +68,12 @@ func TestControlTowerSubscribeUnknown(t *testing.T) {
|
||||
func TestControlTowerSubscribeSuccess(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db := initDB(t, false)
|
||||
db := initDB(t)
|
||||
|
||||
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
|
||||
paymentDB, err := channeldb.NewKVPaymentsDB(db)
|
||||
require.NoError(t, err)
|
||||
|
||||
pControl := NewControlTower(paymentDB)
|
||||
|
||||
// Initiate a payment.
|
||||
info, attempt, preimg, err := genInfo()
|
||||
@@ -180,9 +190,15 @@ func TestKVPaymentsDBSubscribeFail(t *testing.T) {
|
||||
func TestKVPaymentsDBSubscribeAllSuccess(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db := initDB(t, true)
|
||||
db := initDB(t)
|
||||
|
||||
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
|
||||
paymentDB, err := channeldb.NewKVPaymentsDB(
|
||||
db,
|
||||
paymentsdb.WithKeepFailedPaymentAttempts(true),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
pControl := NewControlTower(paymentDB)
|
||||
|
||||
// Initiate a payment.
|
||||
info1, attempt1, preimg1, err := genInfo()
|
||||
@@ -293,9 +309,15 @@ func TestKVPaymentsDBSubscribeAllSuccess(t *testing.T) {
|
||||
func TestKVPaymentsDBSubscribeAllImmediate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db := initDB(t, true)
|
||||
db := initDB(t)
|
||||
|
||||
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
|
||||
paymentDB, err := channeldb.NewKVPaymentsDB(
|
||||
db,
|
||||
paymentsdb.WithKeepFailedPaymentAttempts(true),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
pControl := NewControlTower(paymentDB)
|
||||
|
||||
// Initiate a payment.
|
||||
info, attempt, _, err := genInfo()
|
||||
@@ -330,9 +352,15 @@ func TestKVPaymentsDBSubscribeAllImmediate(t *testing.T) {
|
||||
func TestKVPaymentsDBUnsubscribeSuccess(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db := initDB(t, true)
|
||||
db := initDB(t)
|
||||
|
||||
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
|
||||
paymentDB, err := channeldb.NewKVPaymentsDB(
|
||||
db,
|
||||
paymentsdb.WithKeepFailedPaymentAttempts(true),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
pControl := NewControlTower(paymentDB)
|
||||
|
||||
subscription1, err := pControl.SubscribeAllPayments()
|
||||
require.NoError(t, err, "expected subscribe to succeed, but got: %v")
|
||||
@@ -399,9 +427,17 @@ func TestKVPaymentsDBUnsubscribeSuccess(t *testing.T) {
|
||||
func testKVPaymentsDBSubscribeFail(t *testing.T, registerAttempt,
|
||||
keepFailedPaymentAttempts bool) {
|
||||
|
||||
db := initDB(t, keepFailedPaymentAttempts)
|
||||
db := initDB(t)
|
||||
|
||||
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
|
||||
paymentDB, err := channeldb.NewKVPaymentsDB(
|
||||
db,
|
||||
paymentsdb.WithKeepFailedPaymentAttempts(
|
||||
keepFailedPaymentAttempts,
|
||||
),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
pControl := NewControlTower(paymentDB)
|
||||
|
||||
// Initiate a payment.
|
||||
info, attempt, _, err := genInfo()
|
||||
@@ -517,11 +553,9 @@ func testKVPaymentsDBSubscribeFail(t *testing.T, registerAttempt,
|
||||
}
|
||||
}
|
||||
|
||||
func initDB(t *testing.T, keepFailedPaymentAttempts bool) *channeldb.DB {
|
||||
func initDB(t *testing.T) *channeldb.DB {
|
||||
return channeldb.OpenForTesting(
|
||||
t, t.TempDir(), channeldb.OptionKeepFailedPaymentAttempts(
|
||||
keepFailedPaymentAttempts,
|
||||
),
|
||||
t, t.TempDir(),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+17
-16
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/lightningnetwork/lnd/htlcswitch"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
paymentsdb "github.com/lightningnetwork/lnd/payments/db"
|
||||
"github.com/lightningnetwork/lnd/record"
|
||||
"github.com/lightningnetwork/lnd/routing/route"
|
||||
"github.com/lightningnetwork/lnd/routing/shards"
|
||||
@@ -308,7 +309,7 @@ func (m *mockControlTowerOld) InitPayment(phash lntypes.Hash,
|
||||
|
||||
// Don't allow re-init a successful payment.
|
||||
if _, ok := m.successful[phash]; ok {
|
||||
return channeldb.ErrAlreadyPaid
|
||||
return paymentsdb.ErrAlreadyPaid
|
||||
}
|
||||
|
||||
_, failed := m.failed[phash]
|
||||
@@ -316,7 +317,7 @@ func (m *mockControlTowerOld) InitPayment(phash lntypes.Hash,
|
||||
|
||||
// If the payment is known, only allow re-init if failed.
|
||||
if ok && !failed {
|
||||
return channeldb.ErrPaymentInFlight
|
||||
return paymentsdb.ErrPaymentInFlight
|
||||
}
|
||||
|
||||
delete(m.failed, phash)
|
||||
@@ -330,7 +331,7 @@ func (m *mockControlTowerOld) InitPayment(phash lntypes.Hash,
|
||||
func (m *mockControlTowerOld) DeleteFailedAttempts(phash lntypes.Hash) error {
|
||||
p, ok := m.payments[phash]
|
||||
if !ok {
|
||||
return channeldb.ErrPaymentNotInitiated
|
||||
return paymentsdb.ErrPaymentNotInitiated
|
||||
}
|
||||
|
||||
var inFlight bool
|
||||
@@ -347,7 +348,7 @@ func (m *mockControlTowerOld) DeleteFailedAttempts(phash lntypes.Hash) error {
|
||||
}
|
||||
|
||||
if inFlight {
|
||||
return channeldb.ErrPaymentInFlight
|
||||
return paymentsdb.ErrPaymentInFlight
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -366,7 +367,7 @@ func (m *mockControlTowerOld) RegisterAttempt(phash lntypes.Hash,
|
||||
// Lookup payment.
|
||||
p, ok := m.payments[phash]
|
||||
if !ok {
|
||||
return channeldb.ErrPaymentNotInitiated
|
||||
return paymentsdb.ErrPaymentNotInitiated
|
||||
}
|
||||
|
||||
var inFlight bool
|
||||
@@ -387,15 +388,15 @@ func (m *mockControlTowerOld) RegisterAttempt(phash lntypes.Hash,
|
||||
_, failed := m.failed[phash]
|
||||
|
||||
if settled || failed {
|
||||
return channeldb.ErrPaymentTerminal
|
||||
return paymentsdb.ErrPaymentTerminal
|
||||
}
|
||||
|
||||
if settled && !inFlight {
|
||||
return channeldb.ErrPaymentAlreadySucceeded
|
||||
return paymentsdb.ErrPaymentAlreadySucceeded
|
||||
}
|
||||
|
||||
if failed && !inFlight {
|
||||
return channeldb.ErrPaymentAlreadyFailed
|
||||
return paymentsdb.ErrPaymentAlreadyFailed
|
||||
}
|
||||
|
||||
// Add attempt to payment.
|
||||
@@ -421,7 +422,7 @@ func (m *mockControlTowerOld) SettleAttempt(phash lntypes.Hash,
|
||||
// Only allow setting attempts if the payment is known.
|
||||
p, ok := m.payments[phash]
|
||||
if !ok {
|
||||
return nil, channeldb.ErrPaymentNotInitiated
|
||||
return nil, paymentsdb.ErrPaymentNotInitiated
|
||||
}
|
||||
|
||||
// Find the attempt with this pid, and set the settle info.
|
||||
@@ -431,10 +432,10 @@ func (m *mockControlTowerOld) SettleAttempt(phash lntypes.Hash,
|
||||
}
|
||||
|
||||
if a.Settle != nil {
|
||||
return nil, channeldb.ErrAttemptAlreadySettled
|
||||
return nil, paymentsdb.ErrAttemptAlreadySettled
|
||||
}
|
||||
if a.Failure != nil {
|
||||
return nil, channeldb.ErrAttemptAlreadyFailed
|
||||
return nil, paymentsdb.ErrAttemptAlreadyFailed
|
||||
}
|
||||
|
||||
p.attempts[i].Settle = settleInfo
|
||||
@@ -462,7 +463,7 @@ func (m *mockControlTowerOld) FailAttempt(phash lntypes.Hash, pid uint64,
|
||||
// Only allow failing attempts if the payment is known.
|
||||
p, ok := m.payments[phash]
|
||||
if !ok {
|
||||
return nil, channeldb.ErrPaymentNotInitiated
|
||||
return nil, paymentsdb.ErrPaymentNotInitiated
|
||||
}
|
||||
|
||||
// Find the attempt with this pid, and set the failure info.
|
||||
@@ -472,10 +473,10 @@ func (m *mockControlTowerOld) FailAttempt(phash lntypes.Hash, pid uint64,
|
||||
}
|
||||
|
||||
if a.Settle != nil {
|
||||
return nil, channeldb.ErrAttemptAlreadySettled
|
||||
return nil, paymentsdb.ErrAttemptAlreadySettled
|
||||
}
|
||||
if a.Failure != nil {
|
||||
return nil, channeldb.ErrAttemptAlreadyFailed
|
||||
return nil, paymentsdb.ErrAttemptAlreadyFailed
|
||||
}
|
||||
|
||||
p.attempts[i].Failure = failInfo
|
||||
@@ -499,7 +500,7 @@ func (m *mockControlTowerOld) FailPayment(phash lntypes.Hash,
|
||||
|
||||
// Payment must be known.
|
||||
if _, ok := m.payments[phash]; !ok {
|
||||
return channeldb.ErrPaymentNotInitiated
|
||||
return paymentsdb.ErrPaymentNotInitiated
|
||||
}
|
||||
|
||||
m.failed[phash] = reason
|
||||
@@ -521,7 +522,7 @@ func (m *mockControlTowerOld) fetchPayment(phash lntypes.Hash) (
|
||||
|
||||
p, ok := m.payments[phash]
|
||||
if !ok {
|
||||
return nil, channeldb.ErrPaymentNotInitiated
|
||||
return nil, paymentsdb.ErrPaymentNotInitiated
|
||||
}
|
||||
|
||||
mp := &channeldb.MPPayment{
|
||||
|
||||
+3
-2
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/lightningnetwork/lnd/lnutils"
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
paymentsdb "github.com/lightningnetwork/lnd/payments/db"
|
||||
"github.com/lightningnetwork/lnd/record"
|
||||
"github.com/lightningnetwork/lnd/routing/route"
|
||||
"github.com/lightningnetwork/lnd/routing/shards"
|
||||
@@ -1133,8 +1134,8 @@ func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route,
|
||||
switch {
|
||||
// If this is an MPP attempt and the hash is already registered with
|
||||
// the database, we can go on to launch the shard.
|
||||
case mpp != nil && errors.Is(err, channeldb.ErrPaymentInFlight):
|
||||
case mpp != nil && errors.Is(err, channeldb.ErrPaymentExists):
|
||||
case mpp != nil && errors.Is(err, paymentsdb.ErrPaymentInFlight):
|
||||
case mpp != nil && errors.Is(err, paymentsdb.ErrPaymentExists):
|
||||
|
||||
// Any other error is not tolerated.
|
||||
case err != nil:
|
||||
|
||||
Reference in new issue
Block a user