mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-07 19:30:46 +02:00
channeldb+invoices: move invoice accept or settle logic into registry
As the logic around invoice mutations gets more complex, the friction caused by having this logic split between invoice registry and channeldb becomes more apparent. This commit brings a clearer separation of concerns by centralizing the accept/settle logic in the invoice registry. The original AcceptOrSettle method is renamed to UpdateInvoice because the update to perform is controlled by the callback.
This commit is contained in:
@@ -123,9 +123,7 @@ func TestInvoiceWorkflow(t *testing.T) {
|
||||
// now have the settled bit toggle to true and a non-default
|
||||
// SettledDate
|
||||
payAmt := fakeInvoice.Terms.Value * 2
|
||||
_, err = db.AcceptOrSettleInvoice(
|
||||
paymentHash, payAmt, checkHtlcParameters,
|
||||
)
|
||||
_, err = db.UpdateInvoice(paymentHash, getUpdateInvoice(payAmt))
|
||||
if err != nil {
|
||||
t.Fatalf("unable to settle invoice: %v", err)
|
||||
}
|
||||
@@ -288,8 +286,8 @@ func TestInvoiceAddTimeSeries(t *testing.T) {
|
||||
|
||||
paymentHash := invoice.Terms.PaymentPreimage.Hash()
|
||||
|
||||
_, err := db.AcceptOrSettleInvoice(
|
||||
paymentHash, 0, checkHtlcParameters,
|
||||
_, err := db.UpdateInvoice(
|
||||
paymentHash, getUpdateInvoice(0),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to settle invoice: %v", err)
|
||||
@@ -371,8 +369,8 @@ func TestDuplicateSettleInvoice(t *testing.T) {
|
||||
}
|
||||
|
||||
// With the invoice in the DB, we'll now attempt to settle the invoice.
|
||||
dbInvoice, err := db.AcceptOrSettleInvoice(
|
||||
payHash, amt, checkHtlcParameters,
|
||||
dbInvoice, err := db.UpdateInvoice(
|
||||
payHash, getUpdateInvoice(amt),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to settle invoice: %v", err)
|
||||
@@ -393,8 +391,8 @@ func TestDuplicateSettleInvoice(t *testing.T) {
|
||||
|
||||
// If we try to settle the invoice again, then we should get the very
|
||||
// same invoice back, but with an error this time.
|
||||
dbInvoice, err = db.AcceptOrSettleInvoice(
|
||||
payHash, amt, checkHtlcParameters,
|
||||
dbInvoice, err = db.UpdateInvoice(
|
||||
payHash, getUpdateInvoice(amt),
|
||||
)
|
||||
if err != ErrInvoiceAlreadySettled {
|
||||
t.Fatalf("expected ErrInvoiceAlreadySettled")
|
||||
@@ -440,8 +438,8 @@ func TestQueryInvoices(t *testing.T) {
|
||||
|
||||
// We'll only settle half of all invoices created.
|
||||
if i%2 == 0 {
|
||||
_, err := db.AcceptOrSettleInvoice(
|
||||
paymentHash, i, checkHtlcParameters,
|
||||
_, err := db.UpdateInvoice(
|
||||
paymentHash, getUpdateInvoice(i),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to settle invoice: %v", err)
|
||||
@@ -685,10 +683,19 @@ func TestQueryInvoices(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func checkHtlcParameters(invoice *Invoice) error {
|
||||
if invoice.Terms.State == ContractSettled {
|
||||
return ErrInvoiceAlreadySettled
|
||||
}
|
||||
// getUpdateInvoice returns an invoice update callback that, when called,
|
||||
// settles the invoice with the given amount.
|
||||
func getUpdateInvoice(amt lnwire.MilliSatoshi) InvoiceUpdateCallback {
|
||||
return func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
|
||||
if invoice.Terms.State == ContractSettled {
|
||||
return nil, ErrInvoiceAlreadySettled
|
||||
}
|
||||
|
||||
return nil
|
||||
update := &InvoiceUpdateDesc{
|
||||
State: ContractSettled,
|
||||
AmtPaid: amt,
|
||||
}
|
||||
|
||||
return update, nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user