multi: unify all payment related kv data
All interactions related to the payment db are now part of the kvPaymentDB struct.
This commit is contained in:
4 files changed
+51
-50
No files matched your search
@@ -367,7 +367,7 @@ func (p *KVPaymentsDB) InitPayment(paymentHash lntypes.Hash,
|
|||||||
func (p *KVPaymentsDB) DeleteFailedAttempts(hash lntypes.Hash) error {
|
func (p *KVPaymentsDB) DeleteFailedAttempts(hash lntypes.Hash) error {
|
||||||
if !p.db.keepFailedPaymentAttempts {
|
if !p.db.keepFailedPaymentAttempts {
|
||||||
const failedHtlcsOnly = true
|
const failedHtlcsOnly = true
|
||||||
err := p.db.DeletePayment(hash, failedHtlcsOnly)
|
err := p.DeletePayment(hash, failedHtlcsOnly)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -958,10 +958,10 @@ func htlcBucketKey(prefix, id []byte) []byte {
|
|||||||
// FetchPayments returns all sent payments found in the DB.
|
// FetchPayments returns all sent payments found in the DB.
|
||||||
//
|
//
|
||||||
// nolint: dupl
|
// nolint: dupl
|
||||||
func (d *DB) FetchPayments() ([]*MPPayment, error) {
|
func (p *KVPaymentsDB) FetchPayments() ([]*MPPayment, error) {
|
||||||
var payments []*MPPayment
|
var payments []*MPPayment
|
||||||
|
|
||||||
err := kvdb.View(d, func(tx kvdb.RTx) error {
|
err := kvdb.View(p.db, func(tx kvdb.RTx) error {
|
||||||
paymentsBucket := tx.ReadBucket(paymentsRootBucket)
|
paymentsBucket := tx.ReadBucket(paymentsRootBucket)
|
||||||
if paymentsBucket == nil {
|
if paymentsBucket == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -1196,10 +1196,10 @@ func fetchFailedHtlcKeys(bucket kvdb.RBucket) ([][]byte, error) {
|
|||||||
// QueryPayments is a query to the payments database which is restricted
|
// QueryPayments is a query to the payments database which is restricted
|
||||||
// to a subset of payments by the payments query, containing an offset
|
// to a subset of payments by the payments query, containing an offset
|
||||||
// index and a maximum number of returned payments.
|
// index and a maximum number of returned payments.
|
||||||
func (d *DB) QueryPayments(query PaymentsQuery) (PaymentsResponse, error) {
|
func (p *KVPaymentsDB) QueryPayments(query PaymentsQuery) (PaymentsResponse, error) {
|
||||||
var resp PaymentsResponse
|
var resp PaymentsResponse
|
||||||
|
|
||||||
if err := kvdb.View(d, func(tx kvdb.RTx) error {
|
if err := kvdb.View(p.db, func(tx kvdb.RTx) error {
|
||||||
// Get the root payments bucket.
|
// Get the root payments bucket.
|
||||||
paymentsBucket := tx.ReadBucket(paymentsRootBucket)
|
paymentsBucket := tx.ReadBucket(paymentsRootBucket)
|
||||||
if paymentsBucket == nil {
|
if paymentsBucket == nil {
|
||||||
@@ -1413,10 +1413,10 @@ func fetchPaymentWithSequenceNumber(tx kvdb.RTx, paymentHash lntypes.Hash,
|
|||||||
// DeletePayment deletes a payment from the DB given its payment hash. If
|
// DeletePayment deletes a payment from the DB given its payment hash. If
|
||||||
// failedHtlcsOnly is set, only failed HTLC attempts of the payment will be
|
// failedHtlcsOnly is set, only failed HTLC attempts of the payment will be
|
||||||
// deleted.
|
// deleted.
|
||||||
func (d *DB) DeletePayment(paymentHash lntypes.Hash,
|
func (p *KVPaymentsDB) DeletePayment(paymentHash lntypes.Hash,
|
||||||
failedHtlcsOnly bool) error {
|
failedHtlcsOnly bool) error {
|
||||||
|
|
||||||
return kvdb.Update(d, func(tx kvdb.RwTx) error {
|
return kvdb.Update(p.db, func(tx kvdb.RwTx) error {
|
||||||
payments := tx.ReadWriteBucket(paymentsRootBucket)
|
payments := tx.ReadWriteBucket(paymentsRootBucket)
|
||||||
if payments == nil {
|
if payments == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -1505,9 +1505,9 @@ func (d *DB) DeletePayment(paymentHash lntypes.Hash,
|
|||||||
// failedHtlcsOnly is set, the payment itself won't be deleted, only failed HTLC
|
// failedHtlcsOnly is set, the payment itself won't be deleted, only failed HTLC
|
||||||
// attempts. The method returns the number of deleted payments, which is always
|
// attempts. The method returns the number of deleted payments, which is always
|
||||||
// 0 if failedHtlcsOnly is set.
|
// 0 if failedHtlcsOnly is set.
|
||||||
func (d *DB) DeletePayments(failedOnly, failedHtlcsOnly bool) (int, error) {
|
func (p *KVPaymentsDB) DeletePayments(failedOnly, failedHtlcsOnly bool) (int, error) {
|
||||||
var numPayments int
|
var numPayments int
|
||||||
err := kvdb.Update(d, func(tx kvdb.RwTx) error {
|
err := kvdb.Update(p.db, func(tx kvdb.RwTx) error {
|
||||||
payments := tx.ReadWriteBucket(paymentsRootBucket)
|
payments := tx.ReadWriteBucket(paymentsRootBucket)
|
||||||
if payments == nil {
|
if payments == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -469,12 +469,12 @@ func TestKVPaymentsDBDeleteNonInFlight(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delete all failed payments.
|
// Delete all failed payments.
|
||||||
numPayments, err := db.DeletePayments(true, false)
|
numPayments, err := paymentDB.DeletePayments(true, false)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.EqualValues(t, 1, numPayments)
|
require.EqualValues(t, 1, numPayments)
|
||||||
|
|
||||||
// This should leave the succeeded and in-flight payments.
|
// This should leave the succeeded and in-flight payments.
|
||||||
dbPayments, err := db.FetchPayments()
|
dbPayments, err := paymentDB.FetchPayments()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -505,12 +505,12 @@ func TestKVPaymentsDBDeleteNonInFlight(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now delete all payments except in-flight.
|
// Now delete all payments except in-flight.
|
||||||
numPayments, err = db.DeletePayments(false, false)
|
numPayments, err = paymentDB.DeletePayments(false, false)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.EqualValues(t, 2, numPayments)
|
require.EqualValues(t, 2, numPayments)
|
||||||
|
|
||||||
// This should leave the in-flight payment.
|
// This should leave the in-flight payment.
|
||||||
dbPayments, err = db.FetchPayments()
|
dbPayments, err = paymentDB.FetchPayments()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -567,40 +567,40 @@ func TestKVPaymentsDBDeletePayments(t *testing.T) {
|
|||||||
createTestPayments(t, paymentDB, payments)
|
createTestPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
// Check that all payments are there as we added them.
|
// Check that all payments are there as we added them.
|
||||||
assertPayments(t, db, payments)
|
assertPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
// Delete HTLC attempts for failed payments only.
|
// Delete HTLC attempts for failed payments only.
|
||||||
numPayments, err := db.DeletePayments(true, true)
|
numPayments, err := paymentDB.DeletePayments(true, true)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.EqualValues(t, 0, numPayments)
|
require.EqualValues(t, 0, numPayments)
|
||||||
|
|
||||||
// The failed payment is the only altered one.
|
// The failed payment is the only altered one.
|
||||||
payments[0].htlcs = 0
|
payments[0].htlcs = 0
|
||||||
assertPayments(t, db, payments)
|
assertPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
// Delete failed attempts for all payments.
|
// Delete failed attempts for all payments.
|
||||||
numPayments, err = db.DeletePayments(false, true)
|
numPayments, err = paymentDB.DeletePayments(false, true)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.EqualValues(t, 0, numPayments)
|
require.EqualValues(t, 0, numPayments)
|
||||||
|
|
||||||
// The failed attempts should be deleted, except for the in-flight
|
// The failed attempts should be deleted, except for the in-flight
|
||||||
// payment, that shouldn't be altered until it has completed.
|
// payment, that shouldn't be altered until it has completed.
|
||||||
payments[1].htlcs = 1
|
payments[1].htlcs = 1
|
||||||
assertPayments(t, db, payments)
|
assertPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
// Now delete all failed payments.
|
// Now delete all failed payments.
|
||||||
numPayments, err = db.DeletePayments(true, false)
|
numPayments, err = paymentDB.DeletePayments(true, false)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.EqualValues(t, 1, numPayments)
|
require.EqualValues(t, 1, numPayments)
|
||||||
|
|
||||||
assertPayments(t, db, payments[1:])
|
assertPayments(t, paymentDB, payments[1:])
|
||||||
|
|
||||||
// Finally delete all completed payments.
|
// Finally delete all completed payments.
|
||||||
numPayments, err = db.DeletePayments(false, false)
|
numPayments, err = paymentDB.DeletePayments(false, false)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.EqualValues(t, 1, numPayments)
|
require.EqualValues(t, 1, numPayments)
|
||||||
|
|
||||||
assertPayments(t, db, payments[2:])
|
assertPayments(t, paymentDB, payments[2:])
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestKVPaymentsDBDeleteSinglePayment tests that DeletePayment correctly
|
// TestKVPaymentsDBDeleteSinglePayment tests that DeletePayment correctly
|
||||||
@@ -639,55 +639,55 @@ func TestKVPaymentsDBDeleteSinglePayment(t *testing.T) {
|
|||||||
createTestPayments(t, paymentDB, payments)
|
createTestPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
// Check that all payments are there as we added them.
|
// Check that all payments are there as we added them.
|
||||||
assertPayments(t, db, payments)
|
assertPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
// Delete HTLC attempts for first payment only.
|
// Delete HTLC attempts for first payment only.
|
||||||
require.NoError(t, db.DeletePayment(payments[0].id, true))
|
require.NoError(t, paymentDB.DeletePayment(payments[0].id, true))
|
||||||
|
|
||||||
// The first payment is the only altered one as its failed HTLC should
|
// The first payment is the only altered one as its failed HTLC should
|
||||||
// have been removed but is still present as payment.
|
// have been removed but is still present as payment.
|
||||||
payments[0].htlcs = 0
|
payments[0].htlcs = 0
|
||||||
assertPayments(t, db, payments)
|
assertPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
// Delete the first payment completely.
|
// Delete the first payment completely.
|
||||||
require.NoError(t, db.DeletePayment(payments[0].id, false))
|
require.NoError(t, paymentDB.DeletePayment(payments[0].id, false))
|
||||||
|
|
||||||
// The first payment should have been deleted.
|
// The first payment should have been deleted.
|
||||||
assertPayments(t, db, payments[1:])
|
assertPayments(t, paymentDB, payments[1:])
|
||||||
|
|
||||||
// Now delete the second payment completely.
|
// Now delete the second payment completely.
|
||||||
require.NoError(t, db.DeletePayment(payments[1].id, false))
|
require.NoError(t, paymentDB.DeletePayment(payments[1].id, false))
|
||||||
|
|
||||||
// The Second payment should have been deleted.
|
// The Second payment should have been deleted.
|
||||||
assertPayments(t, db, payments[2:])
|
assertPayments(t, paymentDB, payments[2:])
|
||||||
|
|
||||||
// Delete failed HTLC attempts for the third payment.
|
// Delete failed HTLC attempts for the third payment.
|
||||||
require.NoError(t, db.DeletePayment(payments[2].id, true))
|
require.NoError(t, paymentDB.DeletePayment(payments[2].id, true))
|
||||||
|
|
||||||
// Only the successful HTLC attempt should be left for the third
|
// Only the successful HTLC attempt should be left for the third
|
||||||
// payment.
|
// payment.
|
||||||
payments[2].htlcs = 1
|
payments[2].htlcs = 1
|
||||||
assertPayments(t, db, payments[2:])
|
assertPayments(t, paymentDB, payments[2:])
|
||||||
|
|
||||||
// Now delete the third payment completely.
|
// Now delete the third payment completely.
|
||||||
require.NoError(t, db.DeletePayment(payments[2].id, false))
|
require.NoError(t, paymentDB.DeletePayment(payments[2].id, false))
|
||||||
|
|
||||||
// Only the last payment should be left.
|
// Only the last payment should be left.
|
||||||
assertPayments(t, db, payments[3:])
|
assertPayments(t, paymentDB, payments[3:])
|
||||||
|
|
||||||
// Deleting HTLC attempts from InFlight payments should not work and an
|
// Deleting HTLC attempts from InFlight payments should not work and an
|
||||||
// error returned.
|
// error returned.
|
||||||
require.Error(t, db.DeletePayment(payments[3].id, true))
|
require.Error(t, paymentDB.DeletePayment(payments[3].id, true))
|
||||||
|
|
||||||
// The payment is InFlight and therefore should not have been altered.
|
// The payment is InFlight and therefore should not have been altered.
|
||||||
assertPayments(t, db, payments[3:])
|
assertPayments(t, paymentDB, payments[3:])
|
||||||
|
|
||||||
// Finally deleting the InFlight payment should also not work and an
|
// Finally deleting the InFlight payment should also not work and an
|
||||||
// error returned.
|
// error returned.
|
||||||
require.Error(t, db.DeletePayment(payments[3].id, false))
|
require.Error(t, paymentDB.DeletePayment(payments[3].id, false))
|
||||||
|
|
||||||
// The payment is InFlight and therefore should not have been altered.
|
// The payment is InFlight and therefore should not have been altered.
|
||||||
assertPayments(t, db, payments[3:])
|
assertPayments(t, paymentDB, payments[3:])
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestKVPaymentsDBMultiShard checks the ability of payment control to
|
// TestKVPaymentsDBMultiShard checks the ability of payment control to
|
||||||
@@ -1113,7 +1113,7 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
|
|||||||
createTestPayments(t, paymentDB, payments)
|
createTestPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
// Check that all payments are there as we added them.
|
// Check that all payments are there as we added them.
|
||||||
assertPayments(t, db, payments)
|
assertPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
// Calling DeleteFailedAttempts on a failed payment should delete all
|
// Calling DeleteFailedAttempts on a failed payment should delete all
|
||||||
// HTLCs.
|
// HTLCs.
|
||||||
@@ -1123,7 +1123,7 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
|
|||||||
if !keepFailedPaymentAttempts {
|
if !keepFailedPaymentAttempts {
|
||||||
payments[0].htlcs = 0
|
payments[0].htlcs = 0
|
||||||
}
|
}
|
||||||
assertPayments(t, db, payments)
|
assertPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
// Calling DeleteFailedAttempts on an in-flight payment should return
|
// Calling DeleteFailedAttempts on an in-flight payment should return
|
||||||
// an error.
|
// an error.
|
||||||
@@ -1137,7 +1137,7 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
|
|||||||
|
|
||||||
// Since DeleteFailedAttempts returned an error, we should expect the
|
// Since DeleteFailedAttempts returned an error, we should expect the
|
||||||
// payment to be unchanged.
|
// payment to be unchanged.
|
||||||
assertPayments(t, db, payments)
|
assertPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
// Cleaning up a successful payment should remove failed htlcs.
|
// Cleaning up a successful payment should remove failed htlcs.
|
||||||
require.NoError(t, paymentDB.DeleteFailedAttempts(payments[2].id))
|
require.NoError(t, paymentDB.DeleteFailedAttempts(payments[2].id))
|
||||||
@@ -1146,7 +1146,7 @@ func testDeleteFailedAttempts(t *testing.T, keepFailedPaymentAttempts bool) {
|
|||||||
if !keepFailedPaymentAttempts {
|
if !keepFailedPaymentAttempts {
|
||||||
payments[2].htlcs = 1
|
payments[2].htlcs = 1
|
||||||
}
|
}
|
||||||
assertPayments(t, db, payments)
|
assertPayments(t, paymentDB, payments)
|
||||||
|
|
||||||
if keepFailedPaymentAttempts {
|
if keepFailedPaymentAttempts {
|
||||||
// DeleteFailedAttempts is ignored, even for non-existent
|
// DeleteFailedAttempts is ignored, even for non-existent
|
||||||
@@ -1405,10 +1405,10 @@ func createTestPayments(t *testing.T, p *KVPaymentsDB, payments []*payment) {
|
|||||||
// indices for the slice asserts that exactly the same payments in the
|
// indices for the slice asserts that exactly the same payments in the
|
||||||
// slice for the provided indices exist when fetching payments from the
|
// slice for the provided indices exist when fetching payments from the
|
||||||
// database.
|
// database.
|
||||||
func assertPayments(t *testing.T, db *DB, payments []*payment) {
|
func assertPayments(t *testing.T, paymentDB *KVPaymentsDB, payments []*payment) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
dbPayments, err := db.FetchPayments()
|
dbPayments, err := paymentDB.FetchPayments()
|
||||||
require.NoError(t, err, "could not fetch payments from db")
|
require.NoError(t, err, "could not fetch payments from db")
|
||||||
|
|
||||||
// Make sure that the number of fetched payments is the same
|
// Make sure that the number of fetched payments is the same
|
||||||
|
|||||||
@@ -357,9 +357,11 @@ func TestQueryPayments(t *testing.T) {
|
|||||||
t.Fatalf("unable to init db: %v", err)
|
t.Fatalf("unable to init db: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
paymentDB := NewKVPaymentsDB(db)
|
||||||
|
|
||||||
// Make a preliminary query to make sure it's ok to
|
// Make a preliminary query to make sure it's ok to
|
||||||
// query when we have no payments.
|
// query when we have no payments.
|
||||||
resp, err := db.QueryPayments(tt.query)
|
resp, err := paymentDB.QueryPayments(tt.query)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Len(t, resp.Payments, 0)
|
require.Len(t, resp.Payments, 0)
|
||||||
|
|
||||||
@@ -371,7 +373,6 @@ func TestQueryPayments(t *testing.T) {
|
|||||||
// where we have duplicates in the nested duplicates
|
// where we have duplicates in the nested duplicates
|
||||||
// bucket.
|
// bucket.
|
||||||
nonDuplicatePayments := 6
|
nonDuplicatePayments := 6
|
||||||
paymentDB := NewKVPaymentsDB(db)
|
|
||||||
|
|
||||||
for i := 0; i < nonDuplicatePayments; i++ {
|
for i := 0; i < nonDuplicatePayments; i++ {
|
||||||
// Generate a test payment.
|
// Generate a test payment.
|
||||||
@@ -424,7 +425,7 @@ func TestQueryPayments(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch all payments in the database.
|
// Fetch all payments in the database.
|
||||||
allPayments, err := db.FetchPayments()
|
allPayments, err := paymentDB.FetchPayments()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("payments could not be fetched from "+
|
t.Fatalf("payments could not be fetched from "+
|
||||||
"database: %v", err)
|
"database: %v", err)
|
||||||
@@ -436,7 +437,7 @@ func TestQueryPayments(t *testing.T) {
|
|||||||
len(allPayments), 6)
|
len(allPayments), 6)
|
||||||
}
|
}
|
||||||
|
|
||||||
querySlice, err := db.QueryPayments(tt.query)
|
querySlice, err := paymentDB.QueryPayments(tt.query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -7527,7 +7527,7 @@ func (r *rpcServer) ListPayments(ctx context.Context,
|
|||||||
query.MaxPayments = math.MaxUint64
|
query.MaxPayments = math.MaxUint64
|
||||||
}
|
}
|
||||||
|
|
||||||
paymentsQuerySlice, err := r.server.miscDB.QueryPayments(query)
|
paymentsQuerySlice, err := r.server.kvPaymentsDB.QueryPayments(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -7608,7 +7608,7 @@ func (r *rpcServer) DeletePayment(ctx context.Context,
|
|||||||
rpcsLog.Infof("[DeletePayment] payment_identifier=%v, "+
|
rpcsLog.Infof("[DeletePayment] payment_identifier=%v, "+
|
||||||
"failed_htlcs_only=%v", hash, req.FailedHtlcsOnly)
|
"failed_htlcs_only=%v", hash, req.FailedHtlcsOnly)
|
||||||
|
|
||||||
err = r.server.miscDB.DeletePayment(hash, req.FailedHtlcsOnly)
|
err = r.server.kvPaymentsDB.DeletePayment(hash, req.FailedHtlcsOnly)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -7648,7 +7648,7 @@ func (r *rpcServer) DeleteAllPayments(ctx context.Context,
|
|||||||
"failed_htlcs_only=%v", req.FailedPaymentsOnly,
|
"failed_htlcs_only=%v", req.FailedPaymentsOnly,
|
||||||
req.FailedHtlcsOnly)
|
req.FailedHtlcsOnly)
|
||||||
|
|
||||||
numDeletedPayments, err := r.server.miscDB.DeletePayments(
|
numDeletedPayments, err := r.server.kvPaymentsDB.DeletePayments(
|
||||||
req.FailedPaymentsOnly, req.FailedHtlcsOnly,
|
req.FailedPaymentsOnly, req.FailedHtlcsOnly,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user