invoices: remove the now unused ScanInvoices method
This commit is contained in:
4 files changed
-130
No files matched your search
@@ -1067,66 +1067,6 @@ func TestFetchPendingInvoices(t *testing.T) {
|
|||||||
require.Equal(t, pendingInvoices, pending)
|
require.Equal(t, pendingInvoices, pending)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestScanInvoices tests that ScanInvoices scans through all stored invoices
|
|
||||||
// correctly.
|
|
||||||
func TestScanInvoices(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
db, err := MakeTestInvoiceDB(t)
|
|
||||||
require.NoError(t, err, "unable to make test db")
|
|
||||||
|
|
||||||
var invoices map[lntypes.Hash]*invpkg.Invoice
|
|
||||||
callCount := 0
|
|
||||||
resetCount := 0
|
|
||||||
|
|
||||||
// reset is used to reset/initialize results and is called once
|
|
||||||
// upon calling ScanInvoices and when the underlying transaction is
|
|
||||||
// retried.
|
|
||||||
reset := func() {
|
|
||||||
invoices = make(map[lntypes.Hash]*invpkg.Invoice)
|
|
||||||
callCount = 0
|
|
||||||
resetCount++
|
|
||||||
}
|
|
||||||
|
|
||||||
scanFunc := func(paymentHash lntypes.Hash,
|
|
||||||
invoice *invpkg.Invoice) error {
|
|
||||||
|
|
||||||
invoices[paymentHash] = invoice
|
|
||||||
callCount++
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ctxb := context.Background()
|
|
||||||
// With an empty DB we expect to not scan any invoices.
|
|
||||||
require.NoError(t, db.ScanInvoices(ctxb, scanFunc, reset))
|
|
||||||
require.Equal(t, 0, len(invoices))
|
|
||||||
require.Equal(t, 0, callCount)
|
|
||||||
require.Equal(t, 1, resetCount)
|
|
||||||
|
|
||||||
numInvoices := 5
|
|
||||||
testInvoices := make(map[lntypes.Hash]*invpkg.Invoice)
|
|
||||||
|
|
||||||
// Now populate the DB and check if we can get all invoices with their
|
|
||||||
// payment hashes as expected.
|
|
||||||
for i := 1; i <= numInvoices; i++ {
|
|
||||||
invoice, err := randInvoice(lnwire.MilliSatoshi(i))
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
paymentHash := invoice.Terms.PaymentPreimage.Hash()
|
|
||||||
testInvoices[paymentHash] = invoice
|
|
||||||
|
|
||||||
_, err = db.AddInvoice(ctxb, invoice, paymentHash)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
resetCount = 0
|
|
||||||
require.NoError(t, db.ScanInvoices(ctxb, scanFunc, reset))
|
|
||||||
require.Equal(t, numInvoices, callCount)
|
|
||||||
require.Equal(t, testInvoices, invoices)
|
|
||||||
require.Equal(t, 1, resetCount)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestDuplicateSettleInvoice tests that if we add a new invoice and settle it
|
// TestDuplicateSettleInvoice tests that if we add a new invoice and settle it
|
||||||
// twice, then the second time we also receive the invoice that we settled as a
|
// twice, then the second time we also receive the invoice that we settled as a
|
||||||
// return argument.
|
// return argument.
|
||||||
|
|||||||
@@ -492,53 +492,6 @@ func (d *DB) FetchPendingInvoices(_ context.Context) (
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScanInvoices scans through all invoices and calls the passed scanFunc for
|
|
||||||
// for each invoice with its respective payment hash. Additionally a reset()
|
|
||||||
// closure is passed which is used to reset/initialize partial results and also
|
|
||||||
// to signal if the kvdb.View transaction has been retried.
|
|
||||||
func (d *DB) ScanInvoices(_ context.Context, scanFunc invpkg.InvScanFunc,
|
|
||||||
reset func()) error {
|
|
||||||
|
|
||||||
return kvdb.View(d, func(tx kvdb.RTx) error {
|
|
||||||
invoices := tx.ReadBucket(invoiceBucket)
|
|
||||||
if invoices == nil {
|
|
||||||
return invpkg.ErrNoInvoicesCreated
|
|
||||||
}
|
|
||||||
|
|
||||||
invoiceIndex := invoices.NestedReadBucket(invoiceIndexBucket)
|
|
||||||
if invoiceIndex == nil {
|
|
||||||
// Mask the error if there's no invoice
|
|
||||||
// index as that simply means there are no
|
|
||||||
// invoices added yet to the DB. In this case
|
|
||||||
// we simply return an empty list.
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return invoiceIndex.ForEach(func(k, v []byte) error {
|
|
||||||
// Skip the special numInvoicesKey as that does not
|
|
||||||
// point to a valid invoice.
|
|
||||||
if bytes.Equal(k, numInvoicesKey) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip sub-buckets.
|
|
||||||
if v == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
invoice, err := fetchInvoice(v, invoices)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var paymentHash lntypes.Hash
|
|
||||||
copy(paymentHash[:], k)
|
|
||||||
|
|
||||||
return scanFunc(paymentHash, &invoice)
|
|
||||||
})
|
|
||||||
}, reset)
|
|
||||||
}
|
|
||||||
|
|
||||||
// QueryInvoices allows a caller to query the invoice database for invoices
|
// QueryInvoices allows a caller to query the invoice database for invoices
|
||||||
// within the specified add index range.
|
// within the specified add index range.
|
||||||
func (d *DB) QueryInvoices(_ context.Context, q invpkg.InvoiceQuery) (
|
func (d *DB) QueryInvoices(_ context.Context, q invpkg.InvoiceQuery) (
|
||||||
|
|||||||
@@ -9,10 +9,6 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/record"
|
"github.com/lightningnetwork/lnd/record"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InvScanFunc is a helper type used to specify the type used in the
|
|
||||||
// ScanInvoices methods (part of the InvoiceDB interface).
|
|
||||||
type InvScanFunc func(lntypes.Hash, *Invoice) error
|
|
||||||
|
|
||||||
// InvoiceDB is the database that stores the information about invoices.
|
// InvoiceDB is the database that stores the information about invoices.
|
||||||
type InvoiceDB interface {
|
type InvoiceDB interface {
|
||||||
// AddInvoice inserts the targeted invoice into the database.
|
// AddInvoice inserts the targeted invoice into the database.
|
||||||
@@ -45,17 +41,6 @@ type InvoiceDB interface {
|
|||||||
// payment.
|
// payment.
|
||||||
LookupInvoice(ctx context.Context, ref InvoiceRef) (Invoice, error)
|
LookupInvoice(ctx context.Context, ref InvoiceRef) (Invoice, error)
|
||||||
|
|
||||||
// ScanInvoices scans through all invoices and calls the passed scanFunc
|
|
||||||
// for each invoice with its respective payment hash. Additionally a
|
|
||||||
// reset() closure is passed which is used to reset/initialize partial
|
|
||||||
// results and also to signal if the kvdb.View transaction has been
|
|
||||||
// retried.
|
|
||||||
//
|
|
||||||
// TODO(positiveblue): abstract this functionality so it makes sense for
|
|
||||||
// other backends like sql.
|
|
||||||
ScanInvoices(ctx context.Context, scanFunc InvScanFunc,
|
|
||||||
reset func()) error
|
|
||||||
|
|
||||||
// FetchPendingInvoices returns all invoices that have not yet been
|
// FetchPendingInvoices returns all invoices that have not yet been
|
||||||
// settled or canceled.
|
// settled or canceled.
|
||||||
FetchPendingInvoices(ctx context.Context) (map[lntypes.Hash]Invoice,
|
FetchPendingInvoices(ctx context.Context) (map[lntypes.Hash]Invoice,
|
||||||
|
|||||||
@@ -49,14 +49,6 @@ func (m *MockInvoiceDB) LookupInvoice(ref InvoiceRef) (Invoice, error) {
|
|||||||
return invoice, args.Error(1)
|
return invoice, args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockInvoiceDB) ScanInvoices(scanFunc InvScanFunc,
|
|
||||||
reset func()) error {
|
|
||||||
|
|
||||||
args := m.Called(scanFunc, reset)
|
|
||||||
|
|
||||||
return args.Error(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockInvoiceDB) FetchPendingInvoices(ctx context.Context) (
|
func (m *MockInvoiceDB) FetchPendingInvoices(ctx context.Context) (
|
||||||
map[lntypes.Hash]Invoice, error) {
|
map[lntypes.Hash]Invoice, error) {
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user