channeldb: add new test for duplicate invoice settles

In this commit, we add a new test to ensure that duplicate invoice
settles work as expected. At the present time, this test will fail as
the second to last assertion fails as we'll return a nil invoice the
second time around.
This commit is contained in:
Olaoluwa Osuntokun 2018-07-17 16:27:04 -07:00
parent 6dff599d21
commit 509998db86
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21

@ -314,3 +314,64 @@ func TestInvoiceAddTimeSeries(t *testing.T) {
}
}
}
// 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
// return argument.
func TestDuplicateSettleInvoice(t *testing.T) {
t.Parallel()
db, cleanUp, err := makeTestDB()
defer cleanUp()
if err != nil {
t.Fatalf("unable to make test db: %v", err)
}
// We'll start out by creating an invoice and writing it to the DB.
amt := lnwire.NewMSatFromSatoshis(1000)
invoice, err := randInvoice(amt)
if err != nil {
t.Fatalf("unable to create invoice: %v", err)
}
if _, err := db.AddInvoice(invoice); err != nil {
t.Fatalf("unable to add invoice %v", err)
}
// With the invoice in the DB, we'll now attempt to settle the invoice.
payHash := sha256.Sum256(invoice.Terms.PaymentPreimage[:])
dbInvoice, err := db.SettleInvoice(payHash, amt)
if err != nil {
t.Fatalf("unable to settle invoice: %v", err)
}
// We'll update what we expect the settle invoice to be so that our
// comparison below has the correct assumption.
invoice.SettleIndex = 1
invoice.Terms.Settled = true
invoice.AmtPaid = amt
invoice.SettleDate = dbInvoice.SettleDate
// We should get back the exact same invoice that we just inserted.
if !reflect.DeepEqual(dbInvoice, invoice) {
t.Fatalf("wrong invoice after settle, expected %v got %v",
spew.Sdump(invoice), spew.Sdump(dbInvoice))
}
// If we try to settle the invoice again, then we should get the very
// same invoice back.
dbInvoice, err = db.SettleInvoice(payHash, amt)
if err != nil {
t.Fatalf("unable to settle invoice: %v", err)
}
if dbInvoice == nil {
t.Fatalf("invoice from db is nil after settle!")
}
invoice.SettleDate = dbInvoice.SettleDate
if !reflect.DeepEqual(dbInvoice, invoice) {
t.Fatalf("wrong invoice after second settle, expected %v got %v",
spew.Sdump(invoice), spew.Sdump(dbInvoice))
}
}