channeldb: store hold invoice

This commit is contained in:
Joost Jager committed 2019-03-15 10:08:55 +01:00
1 parent 8392f6d28f
commit 19f79613df
7 files changed
+111 -43

No files matched your search

+16 -13
View File
@@ -2,7 +2,6 @@ package channeldb
import (
"crypto/rand"
"crypto/sha256"
"reflect"
"testing"
"time"
@@ -67,17 +66,18 @@ func TestInvoiceWorkflow(t *testing.T) {
copy(fakeInvoice.Terms.PaymentPreimage[:], rev[:])
fakeInvoice.Terms.Value = lnwire.NewMSatFromSatoshis(10000)
paymentHash := fakeInvoice.Terms.PaymentPreimage.Hash()
// Add the invoice to the database, this should succeed as there aren't
// any existing invoices within the database with the same payment
// hash.
if _, err := db.AddInvoice(fakeInvoice); err != nil {
if _, err := db.AddInvoice(fakeInvoice, paymentHash); err != nil {
t.Fatalf("unable to find invoice: %v", err)
}
// Attempt to retrieve the invoice which was just added to the
// database. It should be found, and the invoice returned should be
// identical to the one created above.
paymentHash := sha256.Sum256(fakeInvoice.Terms.PaymentPreimage[:])
dbInvoice, err := db.LookupInvoice(paymentHash)
if err != nil {
t.Fatalf("unable to find invoice: %v", err)
@@ -126,7 +126,7 @@ func TestInvoiceWorkflow(t *testing.T) {
// Attempt to insert generated above again, this should fail as
// duplicates are rejected by the processing logic.
if _, err := db.AddInvoice(fakeInvoice); err != ErrDuplicateInvoice {
if _, err := db.AddInvoice(fakeInvoice, paymentHash); err != ErrDuplicateInvoice {
t.Fatalf("invoice insertion should fail due to duplication, "+
"instead %v", err)
}
@@ -149,7 +149,8 @@ func TestInvoiceWorkflow(t *testing.T) {
t.Fatalf("unable to create invoice: %v", err)
}
if _, err := db.AddInvoice(invoice); err != nil {
hash := invoice.Terms.PaymentPreimage.Hash()
if _, err := db.AddInvoice(invoice, hash); err != nil {
t.Fatalf("unable to add invoice %v", err)
}
@@ -198,7 +199,9 @@ func TestInvoiceAddTimeSeries(t *testing.T) {
t.Fatalf("unable to create invoice: %v", err)
}
if _, err := db.AddInvoice(invoice); err != nil {
paymentHash := invoice.Terms.PaymentPreimage.Hash()
if _, err := db.AddInvoice(invoice, paymentHash); err != nil {
t.Fatalf("unable to add invoice %v", err)
}
@@ -256,9 +259,7 @@ func TestInvoiceAddTimeSeries(t *testing.T) {
for i := 10; i < len(invoices); i++ {
invoice := &invoices[i]
paymentHash := sha256.Sum256(
invoice.Terms.PaymentPreimage[:],
)
paymentHash := invoice.Terms.PaymentPreimage.Hash()
_, err := db.SettleInvoice(paymentHash, 0)
if err != nil {
@@ -334,12 +335,13 @@ func TestDuplicateSettleInvoice(t *testing.T) {
t.Fatalf("unable to create invoice: %v", err)
}
if _, err := db.AddInvoice(invoice); err != nil {
payHash := invoice.Terms.PaymentPreimage.Hash()
if _, err := db.AddInvoice(invoice, payHash); 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)
@@ -397,13 +399,14 @@ func TestQueryInvoices(t *testing.T) {
t.Fatalf("unable to create invoice: %v", err)
}
if _, err := db.AddInvoice(invoice); err != nil {
paymentHash := invoice.Terms.PaymentPreimage.Hash()
if _, err := db.AddInvoice(invoice, paymentHash); err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
// We'll only settle half of all invoices created.
if i%2 == 0 {
paymentHash := sha256.Sum256(invoice.Terms.PaymentPreimage[:])
if _, err := db.SettleInvoice(paymentHash, i); err != nil {
t.Fatalf("unable to settle invoice: %v", err)
}
+10 -7
View File
@@ -2,7 +2,6 @@ package channeldb
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
@@ -16,6 +15,10 @@ import (
)
var (
// UnknownPreimage is an all-zeroes preimage that indicates that the
// preimage for this invoice is not yet known.
UnknownPreimage lntypes.Preimage
// invoiceBucket is the name of the bucket within the database that
// stores all data related to invoices no matter their final state.
// Within the invoice bucket, each invoice is keyed by its invoice ID
@@ -218,7 +221,9 @@ func validateInvoice(i *Invoice) error {
// insertion will be aborted and rejected due to the strict policy banning any
// duplicate payment hashes. A side effect of this function is that it sets
// AddIndex on newInvoice.
func (d *DB) AddInvoice(newInvoice *Invoice) (uint64, error) {
func (d *DB) AddInvoice(newInvoice *Invoice, paymentHash lntypes.Hash) (
uint64, error) {
if err := validateInvoice(newInvoice); err != nil {
return 0, err
}
@@ -245,9 +250,6 @@ func (d *DB) AddInvoice(newInvoice *Invoice) (uint64, error) {
// Ensure that an invoice an identical payment hash doesn't
// already exist within the index.
paymentHash := sha256.Sum256(
newInvoice.Terms.PaymentPreimage[:],
)
if invoiceIndex.Get(paymentHash[:]) != nil {
return ErrDuplicateInvoice
}
@@ -269,6 +271,7 @@ func (d *DB) AddInvoice(newInvoice *Invoice) (uint64, error) {
newIndex, err := putInvoice(
invoices, invoiceIndex, addIndex, newInvoice, invoiceNum,
paymentHash,
)
if err != nil {
return err
@@ -744,7 +747,8 @@ func (d *DB) InvoicesSettledSince(sinceSettleIndex uint64) ([]Invoice, error) {
}
func putInvoice(invoices, invoiceIndex, addIndex *bbolt.Bucket,
i *Invoice, invoiceNum uint32) (uint64, error) {
i *Invoice, invoiceNum uint32, paymentHash lntypes.Hash) (
uint64, error) {
// Create the invoice key which is just the big-endian representation
// of the invoice number.
@@ -763,7 +767,6 @@ func putInvoice(invoices, invoiceIndex, addIndex *bbolt.Bucket,
// Add the payment hash to the invoice index. This will let us quickly
// identify if we can settle an incoming payment, and also to possibly
// allow a single invoice to have multiple payment installations.
paymentHash := sha256.Sum256(i.Terms.PaymentPreimage[:])
err := invoiceIndex.Put(paymentHash[:], invoiceKey[:])
if err != nil {
return 0, err