htlcswitch/test: use real invoice registry with temp db as mock

In further commits the behaviour of invoice registry becomes more
intrinsically connected to the link. This commit prepares for that by
allowing link and registry to be tested as a single unit.
This commit is contained in:
Joost Jager 2019-02-21 14:52:13 +01:00
parent e464ed18c7
commit aeb35d9898
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
3 changed files with 67 additions and 57 deletions

@ -2046,7 +2046,9 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) {
// We must add the invoice to the registry, such that Alice expects // We must add the invoice to the registry, such that Alice expects
// this payment. // this payment.
err = coreLink.cfg.Registry.(*mockInvoiceRegistry).AddInvoice(*invoice) err = coreLink.cfg.Registry.(*mockInvoiceRegistry).AddInvoice(
*invoice, htlc.PaymentHash,
)
if err != nil { if err != nil {
t.Fatalf("unable to add invoice to registry: %v", err) t.Fatalf("unable to add invoice to registry: %v", err)
} }
@ -2148,7 +2150,9 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to create payment: %v", err) t.Fatalf("unable to create payment: %v", err)
} }
err = coreLink.cfg.Registry.(*mockInvoiceRegistry).AddInvoice(*invoice) err = coreLink.cfg.Registry.(*mockInvoiceRegistry).AddInvoice(
*invoice, htlc.PaymentHash,
)
if err != nil { if err != nil {
t.Fatalf("unable to add invoice to registry: %v", err) t.Fatalf("unable to add invoice to registry: %v", err)
} }
@ -3804,7 +3808,9 @@ func TestChannelLinkAcceptDuplicatePayment(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := n.carolServer.registry.AddInvoice(*invoice); err != nil {
err = n.carolServer.registry.AddInvoice(*invoice, htlc.PaymentHash)
if err != nil {
t.Fatalf("unable to add invoice in carol registry: %v", err) t.Fatalf("unable to add invoice in carol registry: %v", err)
} }
@ -4182,7 +4188,8 @@ func generateHtlc(t *testing.T, coreLink *channelLink,
// We must add the invoice to the registry, such that Alice // We must add the invoice to the registry, such that Alice
// expects this payment. // expects this payment.
err := coreLink.cfg.Registry.(*mockInvoiceRegistry).AddInvoice( err := coreLink.cfg.Registry.(*mockInvoiceRegistry).AddInvoice(
*invoice) *invoice, htlc.PaymentHash,
)
if err != nil { if err != nil {
t.Fatalf("unable to add invoice to registry: %v", err) t.Fatalf("unable to add invoice to registry: %v", err)
} }

@ -8,6 +8,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"net" "net"
"os"
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
@ -23,6 +24,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/contractcourt"
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lnpeer" "github.com/lightningnetwork/lnd/lnpeer"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
@ -703,82 +705,83 @@ func (f *mockChannelLink) UpdateShortChanID() (lnwire.ShortChannelID, error) {
var _ ChannelLink = (*mockChannelLink)(nil) var _ ChannelLink = (*mockChannelLink)(nil)
type mockInvoiceRegistry struct { func newDB() (*channeldb.DB, func(), error) {
sync.Mutex // First, create a temporary directory to be used for the duration of
// this test.
tempDirName, err := ioutil.TempDir("", "channeldb")
if err != nil {
return nil, nil, err
}
invoices map[lntypes.Hash]channeldb.Invoice // Next, create channeldb for the first time.
finalDelta uint32 cdb, err := channeldb.Open(tempDirName)
if err != nil {
os.RemoveAll(tempDirName)
return nil, nil, err
}
cleanUp := func() {
cdb.Close()
os.RemoveAll(tempDirName)
}
return cdb, cleanUp, nil
}
type mockInvoiceRegistry struct {
settleChan chan lntypes.Hash
registry *invoices.InvoiceRegistry
cleanup func()
} }
func newMockRegistry(minDelta uint32) *mockInvoiceRegistry { func newMockRegistry(minDelta uint32) *mockInvoiceRegistry {
cdb, cleanup, err := newDB()
if err != nil {
panic(err)
}
decodeExpiry := func(invoice string) (uint32, error) {
return 3, nil
}
registry := invoices.NewRegistry(cdb, decodeExpiry)
registry.Start()
return &mockInvoiceRegistry{ return &mockInvoiceRegistry{
finalDelta: minDelta, registry: registry,
invoices: make(map[lntypes.Hash]channeldb.Invoice), cleanup: cleanup,
} }
} }
func (i *mockInvoiceRegistry) LookupInvoice(rHash lntypes.Hash) (channeldb.Invoice, uint32, error) { func (i *mockInvoiceRegistry) LookupInvoice(rHash lntypes.Hash) (channeldb.Invoice, uint32, error) {
i.Lock() return i.registry.LookupInvoice(rHash)
defer i.Unlock()
invoice, ok := i.invoices[rHash]
if !ok {
return channeldb.Invoice{}, 0, fmt.Errorf("can't find mock "+
"invoice: %x", rHash[:])
}
return invoice, i.finalDelta, nil
} }
func (i *mockInvoiceRegistry) SettleInvoice(rhash lntypes.Hash, func (i *mockInvoiceRegistry) SettleInvoice(rhash lntypes.Hash,
amt lnwire.MilliSatoshi) error { amt lnwire.MilliSatoshi) error {
i.Lock() err := i.registry.SettleInvoice(rhash, amt)
defer i.Unlock() if err != nil {
return err
invoice, ok := i.invoices[rhash]
if !ok {
return fmt.Errorf("can't find mock invoice: %x", rhash[:])
} }
if i.settleChan != nil {
if invoice.Terms.State == channeldb.ContractSettled { i.settleChan <- rhash
return nil
} }
invoice.Terms.State = channeldb.ContractSettled
invoice.AmtPaid = amt
i.invoices[rhash] = invoice
return nil return nil
} }
func (i *mockInvoiceRegistry) CancelInvoice(payHash lntypes.Hash) error { func (i *mockInvoiceRegistry) CancelInvoice(payHash lntypes.Hash) error {
i.Lock() return i.registry.CancelInvoice(payHash)
defer i.Unlock()
invoice, ok := i.invoices[payHash]
if !ok {
return channeldb.ErrInvoiceNotFound
}
if invoice.Terms.State == channeldb.ContractCanceled {
return nil
}
invoice.Terms.State = channeldb.ContractCanceled
i.invoices[payHash] = invoice
return nil
} }
func (i *mockInvoiceRegistry) AddInvoice(invoice channeldb.Invoice) error { func (i *mockInvoiceRegistry) AddInvoice(invoice channeldb.Invoice,
i.Lock() paymentHash lntypes.Hash) error {
defer i.Unlock()
rhash := invoice.Terms.PaymentPreimage.Hash() _, err := i.registry.AddInvoice(&invoice, paymentHash)
i.invoices[rhash] = invoice return err
return nil
} }
var _ InvoiceDatabase = (*mockInvoiceRegistry)(nil) var _ InvoiceDatabase = (*mockInvoiceRegistry)(nil)

@ -742,7 +742,7 @@ func preparePayment(sendingPeer, receivingPeer lnpeer.Peer,
} }
// Check who is last in the route and add invoice to server registry. // Check who is last in the route and add invoice to server registry.
if err := receiver.registry.AddInvoice(*invoice); err != nil { if err := receiver.registry.AddInvoice(*invoice, htlc.PaymentHash); err != nil {
return nil, nil, err return nil, nil, err
} }