htlcswitch: face race condition in unit tests by returning invoice

In this commit we modify the primary InvoiceRegistry interface within
the package to instead return a direct value for LookupInvoice rather
than a pointer. This fixes an existing race condition wherein a caller
could modify or read the value of the returned invoice.
This commit is contained in:
Olaoluwa Osuntokun
2017-11-11 16:09:14 -08:00
parent 010815e280
commit b6f64932c2
7 changed files with 23 additions and 14 deletions

View File

@@ -12,7 +12,7 @@ import (
type InvoiceDatabase interface {
// LookupInvoice attempts to look up an invoice according to it's 32
// byte payment hash.
LookupInvoice(chainhash.Hash) (*channeldb.Invoice, error)
LookupInvoice(chainhash.Hash) (channeldb.Invoice, error)
// SettleInvoice attempts to mark an invoice corresponding to the
// passed payment hash as fully settled.

View File

@@ -978,7 +978,7 @@ func TestChannelLinkMultiHopUnknownPaymentHash(t *testing.T) {
invoice.Terms.PaymentPreimage[0] ^= byte(255)
// Check who is last in the route and add invoice to server registry.
if err := n.carolServer.registry.AddInvoice(invoice); err != nil {
if err := n.carolServer.registry.AddInvoice(*invoice); err != nil {
t.Fatalf("unable to add invoice in carol registry: %v", err)
}
@@ -1955,7 +1955,7 @@ func TestChannelRetransmission(t *testing.T) {
// TODO(andrew.shvv) Will be removed if we move the notification center
// to the channel link itself.
var invoice *channeldb.Invoice
var invoice channeldb.Invoice
for i := 0; i < 20; i++ {
select {
case <-time.After(time.Millisecond * 200):

View File

@@ -397,22 +397,22 @@ var _ ChannelLink = (*mockChannelLink)(nil)
type mockInvoiceRegistry struct {
sync.Mutex
invoices map[chainhash.Hash]*channeldb.Invoice
invoices map[chainhash.Hash]channeldb.Invoice
}
func newMockRegistry() *mockInvoiceRegistry {
return &mockInvoiceRegistry{
invoices: make(map[chainhash.Hash]*channeldb.Invoice),
invoices: make(map[chainhash.Hash]channeldb.Invoice),
}
}
func (i *mockInvoiceRegistry) LookupInvoice(rHash chainhash.Hash) (*channeldb.Invoice, error) {
func (i *mockInvoiceRegistry) LookupInvoice(rHash chainhash.Hash) (channeldb.Invoice, error) {
i.Lock()
defer i.Unlock()
invoice, ok := i.invoices[rHash]
if !ok {
return nil, errors.New("can't find mock invoice")
return channeldb.Invoice{}, errors.New("can't find mock invoice")
}
return invoice, nil
@@ -428,11 +428,12 @@ func (i *mockInvoiceRegistry) SettleInvoice(rhash chainhash.Hash) error {
}
invoice.Terms.Settled = true
i.invoices[rhash] = invoice
return nil
}
func (i *mockInvoiceRegistry) AddInvoice(invoice *channeldb.Invoice) error {
func (i *mockInvoiceRegistry) AddInvoice(invoice channeldb.Invoice) error {
i.Lock()
defer i.Unlock()

View File

@@ -549,7 +549,7 @@ func (n *threeHopNetwork) makePayment(sendingPeer, receivingPeer Peer,
rhash = fastsha256.Sum256(invoice.Terms.PaymentPreimage[:])
// 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); err != nil {
paymentErr <- err
return &paymentResponse{
rhash: rhash,