From 273cd84355a5a6719d5b2e3b3d0ea5c7c9285082 Mon Sep 17 00:00:00 2001 From: carla Date: Fri, 20 Dec 2019 12:25:07 +0200 Subject: [PATCH] invoices+contractcourt: add HtlcResolution constructor This commit adds a constructor for HtlcResolution creation to enforce provision of all relevant values when an event is created. A custom construstor which also takes a preimage is added for settle events. --- contractcourt/htlc_incoming_resolver_test.go | 38 +++++----- invoices/invoiceregistry.go | 80 ++++++++++++-------- 2 files changed, 67 insertions(+), 51 deletions(-) diff --git a/contractcourt/htlc_incoming_resolver_test.go b/contractcourt/htlc_incoming_resolver_test.go index a27c5883..ef5277f4 100644 --- a/contractcourt/htlc_incoming_resolver_test.go +++ b/contractcourt/htlc_incoming_resolver_test.go @@ -21,10 +21,11 @@ const ( ) var ( - testResPreimage = lntypes.Preimage{1, 2, 3} - testResHash = testResPreimage.Hash() - testResCircuitKey = channeldb.CircuitKey{} - testOnionBlob = []byte{4, 5, 6} + testResPreimage = lntypes.Preimage{1, 2, 3} + testResHash = testResPreimage.Hash() + testResCircuitKey = channeldb.CircuitKey{} + testOnionBlob = []byte{4, 5, 6} + testAcceptHeight int32 = 1234 ) // TestHtlcIncomingResolverFwdPreimageKnown tests resolution of a forwarded htlc @@ -96,10 +97,10 @@ func TestHtlcIncomingResolverExitSettle(t *testing.T) { defer timeout(t)() ctx := newIncomingResolverTestContext(t) - ctx.registry.notifyResolution = &invoices.HtlcResolution{ - CircuitKey: testResCircuitKey, - Preimage: &testResPreimage, - } + ctx.registry.notifyResolution = invoices.NewSettleResolution( + testResPreimage, testResCircuitKey, testAcceptHeight, + ) + ctx.resolve() data := <-ctx.registry.notifyChan @@ -126,9 +127,10 @@ func TestHtlcIncomingResolverExitCancel(t *testing.T) { defer timeout(t)() ctx := newIncomingResolverTestContext(t) - ctx.registry.notifyResolution = &invoices.HtlcResolution{ - CircuitKey: testResCircuitKey, - } + ctx.registry.notifyResolution = invoices.NewFailureResolution( + testResCircuitKey, testAcceptHeight, + ) + ctx.resolve() ctx.waitForResult(false) } @@ -143,10 +145,9 @@ func TestHtlcIncomingResolverExitSettleHodl(t *testing.T) { ctx.resolve() notifyData := <-ctx.registry.notifyChan - notifyData.hodlChan <- invoices.HtlcResolution{ - CircuitKey: testResCircuitKey, - Preimage: &testResPreimage, - } + notifyData.hodlChan <- *invoices.NewSettleResolution( + testResPreimage, testResCircuitKey, testAcceptHeight, + ) ctx.waitForResult(true) } @@ -172,9 +173,10 @@ func TestHtlcIncomingResolverExitCancelHodl(t *testing.T) { ctx := newIncomingResolverTestContext(t) ctx.resolve() notifyData := <-ctx.registry.notifyChan - notifyData.hodlChan <- invoices.HtlcResolution{ - CircuitKey: testResCircuitKey, - } + notifyData.hodlChan <- *invoices.NewFailureResolution( + testResCircuitKey, testAcceptHeight, + ) + ctx.waitForResult(false) } diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index bc1b38b0..a65799c5 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -50,6 +50,28 @@ type HtlcResolution struct { AcceptHeight int32 } +// NewFailureResolution returns a htlc failure resolution. +func NewFailureResolution(key channeldb.CircuitKey, + acceptHeight int32) *HtlcResolution { + + return &HtlcResolution{ + CircuitKey: key, + AcceptHeight: acceptHeight, + } +} + +// NewSettleResolution returns a htlc resolution which is associated with a +// settle. +func NewSettleResolution(preimage lntypes.Preimage, key channeldb.CircuitKey, + acceptHeight int32) *HtlcResolution { + + return &HtlcResolution{ + Preimage: &preimage, + CircuitKey: key, + AcceptHeight: acceptHeight, + } +} + // RegistryConfig contains the configuration parameters for invoice registry. type RegistryConfig struct { // FinalCltvRejectDelta defines the number of blocks before the expiry @@ -652,11 +674,9 @@ func (i *InvoiceRegistry) cancelSingleHtlc(hash lntypes.Hash, return fmt.Errorf("htlc %v not found", key) } if htlc.State == channeldb.HtlcStateCanceled { - i.notifyHodlSubscribers(HtlcResolution{ - CircuitKey: key, - AcceptHeight: int32(htlc.AcceptHeight), - Preimage: nil, - }) + i.notifyHodlSubscribers( + *NewFailureResolution(key, int32(htlc.AcceptHeight)), + ) } return nil } @@ -745,10 +765,7 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash, // If it isn't recorded, cancel htlc. if !ok { - return &HtlcResolution{ - CircuitKey: circuitKey, - AcceptHeight: currentHeight, - }, nil + return NewFailureResolution(circuitKey, currentHeight), nil } // Determine accepted height of this htlc. If the htlc reached the @@ -759,10 +776,7 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash, switch invoiceHtlc.State { case channeldb.HtlcStateCanceled: - return &HtlcResolution{ - CircuitKey: circuitKey, - AcceptHeight: acceptHeight, - }, nil + return NewFailureResolution(circuitKey, acceptHeight), nil case channeldb.HtlcStateSettled: // Also settle any previously accepted htlcs. The invoice state @@ -773,18 +787,17 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash, continue } - i.notifyHodlSubscribers(HtlcResolution{ - CircuitKey: key, - Preimage: &invoice.Terms.PaymentPreimage, - AcceptHeight: int32(htlc.AcceptHeight), - }) + resolution := *NewSettleResolution( + invoice.Terms.PaymentPreimage, key, + acceptHeight, + ) + + i.notifyHodlSubscribers(resolution) } - return &HtlcResolution{ - CircuitKey: circuitKey, - Preimage: &invoice.Terms.PaymentPreimage, - AcceptHeight: acceptHeight, - }, nil + return NewSettleResolution( + invoice.Terms.PaymentPreimage, circuitKey, acceptHeight, + ), nil case channeldb.HtlcStateAccepted: // (Re)start the htlc timer if the invoice is still open. It can @@ -836,7 +849,9 @@ func (i *InvoiceRegistry) SettleHodlInvoice(preimage lntypes.Preimage) error { hash := preimage.Hash() invoice, err := i.cdb.UpdateInvoice(hash, updateInvoice) if err != nil { - log.Errorf("SettleHodlInvoice with preimage %v: %v", preimage, err) + log.Errorf("SettleHodlInvoice with preimage %v: %v", + preimage, err) + return err } @@ -854,11 +869,11 @@ func (i *InvoiceRegistry) SettleHodlInvoice(preimage lntypes.Preimage) error { continue } - i.notifyHodlSubscribers(HtlcResolution{ - CircuitKey: key, - Preimage: &preimage, - AcceptHeight: int32(htlc.AcceptHeight), - }) + resolution := *NewSettleResolution( + preimage, key, int32(htlc.AcceptHeight), + ) + + i.notifyHodlSubscribers(resolution) } i.notifyClients(hash, invoice, invoice.State) @@ -932,10 +947,9 @@ func (i *InvoiceRegistry) cancelInvoiceImpl(payHash lntypes.Hash, continue } - i.notifyHodlSubscribers(HtlcResolution{ - CircuitKey: key, - AcceptHeight: int32(htlc.AcceptHeight), - }) + i.notifyHodlSubscribers( + *NewFailureResolution(key, int32(htlc.AcceptHeight)), + ) } i.notifyClients(payHash, invoice, channeldb.ContractCanceled)