diff --git a/contractcourt/htlc_incoming_contest_resolver.go b/contractcourt/htlc_incoming_contest_resolver.go index abedcc14..3c66fdc6 100644 --- a/contractcourt/htlc_incoming_contest_resolver.go +++ b/contractcourt/htlc_incoming_contest_resolver.go @@ -167,9 +167,9 @@ func (h *htlcIncomingContestResolver) Resolve() (ContractResolver, error) { preimageSubscription := h.PreimageDB.SubscribeUpdates() defer preimageSubscription.CancelSubscription() - // Define closure to process hodl events either direct or triggered by - // later notifcation. - processHodlEvent := func(e invoices.HodlEvent) (ContractResolver, + // Define closure to process htlc resolutions either direct or triggered by + // later notification. + processHodlEvent := func(e invoices.HtlcResolution) (ContractResolver, error) { if e.Preimage == nil { @@ -252,9 +252,9 @@ func (h *htlcIncomingContestResolver) Resolve() (ContractResolver, error) { return &h.htlcSuccessResolver, nil case hodlItem := <-hodlChan: - hodlEvent := hodlItem.(invoices.HodlEvent) + htlcResolution := hodlItem.(invoices.HtlcResolution) - return processHodlEvent(hodlEvent) + return processHodlEvent(htlcResolution) case newBlock, ok := <-blockEpochs.Epochs: if !ok { diff --git a/contractcourt/htlc_incoming_resolver_test.go b/contractcourt/htlc_incoming_resolver_test.go index 850126c0..1c6628a3 100644 --- a/contractcourt/htlc_incoming_resolver_test.go +++ b/contractcourt/htlc_incoming_resolver_test.go @@ -96,7 +96,7 @@ func TestHtlcIncomingResolverExitSettle(t *testing.T) { defer timeout(t)() ctx := newIncomingResolverTestContext(t) - ctx.registry.notifyEvent = &invoices.HodlEvent{ + ctx.registry.notifyEvent = &invoices.HtlcResolution{ CircuitKey: testResCircuitKey, Preimage: &testResPreimage, } @@ -126,7 +126,7 @@ func TestHtlcIncomingResolverExitCancel(t *testing.T) { defer timeout(t)() ctx := newIncomingResolverTestContext(t) - ctx.registry.notifyEvent = &invoices.HodlEvent{ + ctx.registry.notifyEvent = &invoices.HtlcResolution{ CircuitKey: testResCircuitKey, } ctx.resolve() @@ -143,7 +143,7 @@ func TestHtlcIncomingResolverExitSettleHodl(t *testing.T) { ctx.resolve() notifyData := <-ctx.registry.notifyChan - notifyData.hodlChan <- invoices.HodlEvent{ + notifyData.hodlChan <- invoices.HtlcResolution{ CircuitKey: testResCircuitKey, Preimage: &testResPreimage, } @@ -172,7 +172,7 @@ func TestHtlcIncomingResolverExitCancelHodl(t *testing.T) { ctx := newIncomingResolverTestContext(t) ctx.resolve() notifyData := <-ctx.registry.notifyChan - notifyData.hodlChan <- invoices.HodlEvent{ + notifyData.hodlChan <- invoices.HtlcResolution{ CircuitKey: testResCircuitKey, } ctx.waitForResult(false) diff --git a/contractcourt/interfaces.go b/contractcourt/interfaces.go index 45e9b0bd..086a2aee 100644 --- a/contractcourt/interfaces.go +++ b/contractcourt/interfaces.go @@ -27,9 +27,9 @@ type Registry interface { NotifyExitHopHtlc(payHash lntypes.Hash, paidAmount lnwire.MilliSatoshi, expiry uint32, currentHeight int32, circuitKey channeldb.CircuitKey, hodlChan chan<- interface{}, - payload invoices.Payload) (*invoices.HodlEvent, error) + payload invoices.Payload) (*invoices.HtlcResolution, error) - // HodlUnsubscribeAll unsubscribes from all hodl events. + // HodlUnsubscribeAll unsubscribes from all htlc resolutions. HodlUnsubscribeAll(subscriber chan<- interface{}) } diff --git a/contractcourt/mock_registry_test.go b/contractcourt/mock_registry_test.go index 43195249..8ccd0f6c 100644 --- a/contractcourt/mock_registry_test.go +++ b/contractcourt/mock_registry_test.go @@ -18,13 +18,13 @@ type notifyExitHopData struct { type mockRegistry struct { notifyChan chan notifyExitHopData notifyErr error - notifyEvent *invoices.HodlEvent + notifyEvent *invoices.HtlcResolution } func (r *mockRegistry) NotifyExitHopHtlc(payHash lntypes.Hash, paidAmount lnwire.MilliSatoshi, expiry uint32, currentHeight int32, circuitKey channeldb.CircuitKey, hodlChan chan<- interface{}, - payload invoices.Payload) (*invoices.HodlEvent, error) { + payload invoices.Payload) (*invoices.HtlcResolution, error) { r.notifyChan <- notifyExitHopData{ hodlChan: hodlChan, diff --git a/htlcswitch/interfaces.go b/htlcswitch/interfaces.go index 3b9ac6df..bb34b283 100644 --- a/htlcswitch/interfaces.go +++ b/htlcswitch/interfaces.go @@ -27,7 +27,7 @@ type InvoiceDatabase interface { NotifyExitHopHtlc(payHash lntypes.Hash, paidAmount lnwire.MilliSatoshi, expiry uint32, currentHeight int32, circuitKey channeldb.CircuitKey, hodlChan chan<- interface{}, - payload invoices.Payload) (*invoices.HodlEvent, error) + payload invoices.Payload) (*invoices.HtlcResolution, error) // CancelInvoice attempts to cancel the invoice corresponding to the // passed payment hash. @@ -36,7 +36,7 @@ type InvoiceDatabase interface { // SettleHodlInvoice settles a hold invoice. SettleHodlInvoice(preimage lntypes.Preimage) error - // HodlUnsubscribeAll unsubscribes from all hodl events. + // HodlUnsubscribeAll unsubscribes from all htlc resolutions. HodlUnsubscribeAll(subscriber chan<- interface{}) } diff --git a/htlcswitch/link.go b/htlcswitch/link.go index ce4d0073..a262ff21 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -489,8 +489,8 @@ func (l *channelLink) Stop() { l.log.Info("stopping") - // As the link is stopping, we are no longer interested in hodl events - // coming from the invoice registry. + // As the link is stopping, we are no longer interested in htlc + // resolutions coming from the invoice registry. l.cfg.Registry.HodlUnsubscribeAll(l.hodlQueue.ChanIn()) if l.cfg.ChainEvents.Cancel != nil { @@ -1126,11 +1126,11 @@ out: case msg := <-l.upstream: l.handleUpstreamMsg(msg) - // A hodl event is received. This means that we now have a + // A htlc resolution is received. This means that we now have a // resolution for a previously accepted htlc. case hodlItem := <-l.hodlQueue.ChanOut(): - hodlEvent := hodlItem.(invoices.HodlEvent) - err := l.processHodlQueue(hodlEvent) + htlcResolution := hodlItem.(invoices.HtlcResolution) + err := l.processHodlQueue(htlcResolution) if err != nil { l.fail(LinkFailureError{code: ErrInternalError}, fmt.Sprintf("process hodl queue: %v", @@ -1145,24 +1145,26 @@ out: } } -// processHodlQueue processes a received hodl event and continues reading from -// the hodl queue until no more events remain. When this function returns -// without an error, the commit tx should be updated. -func (l *channelLink) processHodlQueue(firstHodlEvent invoices.HodlEvent) error { +// processHodlQueue processes a received htlc resolution and continues reading +// from the hodl queue until no more resolutions remain. When this function +// returns without an error, the commit tx should be updated. +func (l *channelLink) processHodlQueue( + firstResolution invoices.HtlcResolution) error { + // Try to read all waiting resolution messages, so that they can all be // processed in a single commitment tx update. - hodlEvent := firstHodlEvent + htlcResolution := firstResolution loop: for { // Lookup all hodl htlcs that can be failed or settled with this event. // The hodl htlc must be present in the map. - circuitKey := hodlEvent.CircuitKey + circuitKey := htlcResolution.CircuitKey hodlHtlc, ok := l.hodlMap[circuitKey] if !ok { return fmt.Errorf("hodl htlc not found: %v", circuitKey) } - if err := l.processHodlEvent(hodlEvent, hodlHtlc); err != nil { + if err := l.processHodlEvent(htlcResolution, hodlHtlc); err != nil { return err } @@ -1171,7 +1173,7 @@ loop: select { case item := <-l.hodlQueue.ChanOut(): - hodlEvent = item.(invoices.HodlEvent) + htlcResolution = item.(invoices.HtlcResolution) default: break loop } @@ -1185,29 +1187,29 @@ loop: return nil } -// processHodlEvent applies a received hodl event to the provided htlc. When -// this function returns without an error, the commit tx should be updated. -func (l *channelLink) processHodlEvent(hodlEvent invoices.HodlEvent, +// processHodlEvent applies a received htlc resolution to the provided htlc. +// When this function returns without an error, the commit tx should be updated. +func (l *channelLink) processHodlEvent(resolution invoices.HtlcResolution, htlc hodlHtlc) error { - circuitKey := hodlEvent.CircuitKey + circuitKey := resolution.CircuitKey // Determine required action for the resolution. - if hodlEvent.Preimage != nil { - l.log.Debugf("received hodl settle event for %v", circuitKey) + if resolution.Preimage != nil { + l.log.Debugf("received settle resolution for %v", circuitKey) return l.settleHTLC( - *hodlEvent.Preimage, htlc.pd.HtlcIndex, + *resolution.Preimage, htlc.pd.HtlcIndex, htlc.pd.SourceRef, ) } - l.log.Debugf("received hodl cancel event for %v", circuitKey) + l.log.Debugf("received cancel resolution for %v", circuitKey) // In case of a cancel, always return // incorrect_or_unknown_payment_details in order to avoid leaking info. failure := lnwire.NewFailIncorrectDetails( - htlc.pd.Amount, uint32(hodlEvent.AcceptHeight), + htlc.pd.Amount, uint32(resolution.AcceptHeight), ) l.sendHTLCError( diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index 1d0731e8..5c905822 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -819,7 +819,7 @@ func (i *mockInvoiceRegistry) SettleHodlInvoice(preimage lntypes.Preimage) error func (i *mockInvoiceRegistry) NotifyExitHopHtlc(rhash lntypes.Hash, amt lnwire.MilliSatoshi, expiry uint32, currentHeight int32, circuitKey channeldb.CircuitKey, hodlChan chan<- interface{}, - payload invoices.Payload) (*invoices.HodlEvent, error) { + payload invoices.Payload) (*invoices.HtlcResolution, error) { event, err := i.registry.NotifyExitHopHtlc( rhash, amt, expiry, currentHeight, circuitKey, hodlChan, diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index ae93a635..bc1b38b0 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -35,10 +35,10 @@ const ( DefaultHtlcHoldDuration = 120 * time.Second ) -// HodlEvent describes how an htlc should be resolved. If HodlEvent.Preimage is -// set, the event indicates a settle event. If Preimage is nil, it is a cancel -// event. -type HodlEvent struct { +// HtlcResolution describes how an htlc should be resolved. If the preimage +// field is set, the event indicates a settle event. If Preimage is nil, it is +// a cancel event. +type HtlcResolution struct { // Preimage is the htlc preimage. Its value is nil in case of a cancel. Preimage *lntypes.Preimage @@ -652,7 +652,7 @@ func (i *InvoiceRegistry) cancelSingleHtlc(hash lntypes.Hash, return fmt.Errorf("htlc %v not found", key) } if htlc.State == channeldb.HtlcStateCanceled { - i.notifyHodlSubscribers(HodlEvent{ + i.notifyHodlSubscribers(HtlcResolution{ CircuitKey: key, AcceptHeight: int32(htlc.AcceptHeight), Preimage: nil, @@ -679,7 +679,7 @@ func (i *InvoiceRegistry) cancelSingleHtlc(hash lntypes.Hash, func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash, amtPaid lnwire.MilliSatoshi, expiry uint32, currentHeight int32, circuitKey channeldb.CircuitKey, hodlChan chan<- interface{}, - payload Payload) (*HodlEvent, error) { + payload Payload) (*HtlcResolution, error) { i.Lock() defer i.Unlock() @@ -745,7 +745,7 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash, // If it isn't recorded, cancel htlc. if !ok { - return &HodlEvent{ + return &HtlcResolution{ CircuitKey: circuitKey, AcceptHeight: currentHeight, }, nil @@ -759,7 +759,7 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash, switch invoiceHtlc.State { case channeldb.HtlcStateCanceled: - return &HodlEvent{ + return &HtlcResolution{ CircuitKey: circuitKey, AcceptHeight: acceptHeight, }, nil @@ -773,14 +773,14 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash, continue } - i.notifyHodlSubscribers(HodlEvent{ + i.notifyHodlSubscribers(HtlcResolution{ CircuitKey: key, Preimage: &invoice.Terms.PaymentPreimage, AcceptHeight: int32(htlc.AcceptHeight), }) } - return &HodlEvent{ + return &HtlcResolution{ CircuitKey: circuitKey, Preimage: &invoice.Terms.PaymentPreimage, AcceptHeight: acceptHeight, @@ -854,7 +854,7 @@ func (i *InvoiceRegistry) SettleHodlInvoice(preimage lntypes.Preimage) error { continue } - i.notifyHodlSubscribers(HodlEvent{ + i.notifyHodlSubscribers(HtlcResolution{ CircuitKey: key, Preimage: &preimage, AcceptHeight: int32(htlc.AcceptHeight), @@ -932,7 +932,7 @@ func (i *InvoiceRegistry) cancelInvoiceImpl(payHash lntypes.Hash, continue } - i.notifyHodlSubscribers(HodlEvent{ + i.notifyHodlSubscribers(HtlcResolution{ CircuitKey: key, AcceptHeight: int32(htlc.AcceptHeight), }) @@ -1201,9 +1201,10 @@ func (i *InvoiceRegistry) SubscribeSingleInvoice( return client, nil } -// notifyHodlSubscribers sends out the hodl event to all current subscribers. -func (i *InvoiceRegistry) notifyHodlSubscribers(hodlEvent HodlEvent) { - subscribers, ok := i.hodlSubscriptions[hodlEvent.CircuitKey] +// notifyHodlSubscribers sends out the htlc resolution to all current +// subscribers. +func (i *InvoiceRegistry) notifyHodlSubscribers(htlcResolution HtlcResolution) { + subscribers, ok := i.hodlSubscriptions[htlcResolution.CircuitKey] if !ok { return } @@ -1213,18 +1214,18 @@ func (i *InvoiceRegistry) notifyHodlSubscribers(hodlEvent HodlEvent) { // single resolution for each hash. for subscriber := range subscribers { select { - case subscriber <- hodlEvent: + case subscriber <- htlcResolution: case <-i.quit: return } delete( i.hodlReverseSubscriptions[subscriber], - hodlEvent.CircuitKey, + htlcResolution.CircuitKey, ) } - delete(i.hodlSubscriptions, hodlEvent.CircuitKey) + delete(i.hodlSubscriptions, htlcResolution.CircuitKey) } // hodlSubscribe adds a new invoice subscription. diff --git a/invoices/invoiceregistry_test.go b/invoices/invoiceregistry_test.go index e47eaa37..76b60003 100644 --- a/invoices/invoiceregistry_test.go +++ b/invoices/invoiceregistry_test.go @@ -281,7 +281,7 @@ func TestCancelInvoice(t *testing.T) { } if event.Preimage != nil { - t.Fatal("expected cancel hodl event") + t.Fatal("expected cancel htlc resolution") } if event.AcceptHeight != testCurrentHeight { t.Fatalf("expected acceptHeight %v, but got %v", @@ -421,11 +421,11 @@ func TestSettleHoldInvoice(t *testing.T) { t.Fatal("expected set preimage to succeed") } - hodlEvent := (<-hodlChan).(HodlEvent) - if *hodlEvent.Preimage != testInvoicePreimage { - t.Fatal("unexpected preimage in hodl event") + htlcResolution := (<-hodlChan).(HtlcResolution) + if *htlcResolution.Preimage != testInvoicePreimage { + t.Fatal("unexpected preimage in hodl resolution") } - if hodlEvent.AcceptHeight != testCurrentHeight { + if htlcResolution.AcceptHeight != testCurrentHeight { t.Fatalf("expected acceptHeight %v, but got %v", testCurrentHeight, event.AcceptHeight) } @@ -513,9 +513,9 @@ func TestCancelHoldInvoice(t *testing.T) { t.Fatal("cancel invoice failed") } - hodlEvent := (<-hodlChan).(HodlEvent) - if hodlEvent.Preimage != nil { - t.Fatal("expected cancel hodl event") + htlcResolution := (<-hodlChan).(HtlcResolution) + if htlcResolution.Preimage != nil { + t.Fatal("expected cancel htlc resolution") } // Offering the same htlc again at a higher height should still result @@ -538,10 +538,10 @@ func TestCancelHoldInvoice(t *testing.T) { } // TestUnknownInvoice tests that invoice registry returns an error when the -// invoice is unknown. This is to guard against returning a cancel hodl event -// for forwarded htlcs. In the link, NotifyExitHopHtlc is only called if we are -// the exit hop, but in htlcIncomingContestResolver it is called with forwarded -// htlc hashes as well. +// invoice is unknown. This is to guard against returning a cancel htlc +// resolution for forwarded htlcs. In the link, NotifyExitHopHtlc is only called +// if we are the exit hop, but in htlcIncomingContestResolver it is called with +// forwarded htlc hashes as well. func TestUnknownInvoice(t *testing.T) { ctx := newTestContext(t) defer ctx.cleanup() @@ -593,8 +593,8 @@ func TestSettleMpp(t *testing.T) { // Simulate mpp timeout releasing htlc 1. ctx.clock.SetTime(testTime.Add(30 * time.Second)) - hodlEvent := (<-hodlChan1).(HodlEvent) - if hodlEvent.Preimage != nil { + htlcResolution := (<-hodlChan1).(HtlcResolution) + if htlcResolution.Preimage != nil { t.Fatal("expected cancel event") }