htlcswitch: abtract invoice from link

This commit detaches signaling the invoice registry that an htlc was
locked in from the actually settling of the htlc.

It is a preparation for hodl invoices.
This commit is contained in:
Joost Jager
2019-02-20 12:11:15 +01:00
parent aeb35d9898
commit 1f41a2abce
6 changed files with 162 additions and 98 deletions

View File

@@ -37,22 +37,41 @@ func decodeExpiry(payReq string) (uint32, error) {
return uint32(invoice.MinFinalCLTVExpiry()), nil
}
// TestSettleInvoice tests settling of an invoice and related notifications.
func TestSettleInvoice(t *testing.T) {
var (
testInvoice = &channeldb.Invoice{
Terms: channeldb.ContractTerm{
PaymentPreimage: preimage,
Value: lnwire.MilliSatoshi(100000),
},
PaymentRequest: []byte(testPayReq),
}
)
func newTestContext(t *testing.T) (*InvoiceRegistry, func()) {
cdb, cleanup, err := newDB()
if err != nil {
t.Fatal(err)
}
defer cleanup()
// Instantiate and start the invoice registry.
registry := NewRegistry(cdb, decodeExpiry)
err = registry.Start()
if err != nil {
cleanup()
t.Fatal(err)
}
defer registry.Stop()
return registry, func() {
registry.Stop()
cleanup()
}
}
// TestSettleInvoice tests settling of an invoice and related notifications.
func TestSettleInvoice(t *testing.T) {
registry, cleanup := newTestContext(t)
defer cleanup()
allSubscriptions := registry.SubscribeNotifications(0, 0)
defer allSubscriptions.Cancel()
@@ -66,15 +85,7 @@ func TestSettleInvoice(t *testing.T) {
}
// Add the invoice.
invoice := &channeldb.Invoice{
Terms: channeldb.ContractTerm{
PaymentPreimage: preimage,
Value: lnwire.MilliSatoshi(100000),
},
PaymentRequest: []byte(testPayReq),
}
addIdx, err := registry.AddInvoice(invoice, hash)
addIdx, err := registry.AddInvoice(testInvoice, hash)
if err != nil {
t.Fatal(err)
}
@@ -108,7 +119,7 @@ func TestSettleInvoice(t *testing.T) {
// Settle invoice with a slightly higher amount.
amtPaid := lnwire.MilliSatoshi(100500)
err = registry.SettleInvoice(hash, amtPaid)
_, err = registry.NotifyExitHopHtlc(hash, amtPaid)
if err != nil {
t.Fatal(err)
}
@@ -140,13 +151,13 @@ func TestSettleInvoice(t *testing.T) {
}
// Try to settle again.
err = registry.SettleInvoice(hash, amtPaid)
_, err = registry.NotifyExitHopHtlc(hash, amtPaid)
if err != nil {
t.Fatal("expected duplicate settle to succeed")
}
// Try to settle again with a different amount.
err = registry.SettleInvoice(hash, amtPaid+600)
_, err = registry.NotifyExitHopHtlc(hash, amtPaid+600)
if err != nil {
t.Fatal("expected duplicate settle to succeed")
}
@@ -169,26 +180,14 @@ func TestSettleInvoice(t *testing.T) {
// TestCancelInvoice tests cancelation of an invoice and related notifications.
func TestCancelInvoice(t *testing.T) {
cdb, cleanup, err := newDB()
if err != nil {
t.Fatal(err)
}
registry, cleanup := newTestContext(t)
defer cleanup()
// Instantiate and start the invoice registry.
registry := NewRegistry(cdb, decodeExpiry)
err = registry.Start()
if err != nil {
t.Fatal(err)
}
defer registry.Stop()
allSubscriptions := registry.SubscribeNotifications(0, 0)
defer allSubscriptions.Cancel()
// Try to cancel the not yet existing invoice. This should fail.
err = registry.CancelInvoice(hash)
err := registry.CancelInvoice(hash)
if err != channeldb.ErrInvoiceNotFound {
t.Fatalf("expected ErrInvoiceNotFound, but got %v", err)
}
@@ -203,14 +202,7 @@ func TestCancelInvoice(t *testing.T) {
// Add the invoice.
amt := lnwire.MilliSatoshi(100000)
invoice := &channeldb.Invoice{
Terms: channeldb.ContractTerm{
PaymentPreimage: preimage,
Value: amt,
},
}
_, err = registry.AddInvoice(invoice, hash)
_, err = registry.AddInvoice(testInvoice, hash)
if err != nil {
t.Fatal(err)
}
@@ -270,10 +262,15 @@ func TestCancelInvoice(t *testing.T) {
t.Fatal("expected cancelation of a canceled invoice to succeed")
}
// Try to settle. This should not be possible.
err = registry.SettleInvoice(hash, amt)
if err != channeldb.ErrInvoiceAlreadyCanceled {
t.Fatal("expected settlement of a canceled invoice to fail")
// Notify arrival of a new htlc paying to this invoice. This should
// succeed.
event, err := registry.NotifyExitHopHtlc(hash, amt)
if err != nil {
t.Fatal("expected settlement of a canceled invoice to succeed")
}
if event.Preimage != nil {
t.Fatal("expected cancel hodl event")
}
}