From 55930df70d57e1a89fa837d5bfa8d44968b348de Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Wed, 13 May 2020 15:26:41 +0200 Subject: [PATCH] htlcswitch: update commit tx per downstream msg type Unroll common code to allow splitting in separate handlers per message type. --- htlcswitch/link.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index db7f15ff..c5c6335e 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -1261,7 +1261,6 @@ func (l *channelLink) randomFeeUpdateTimeout() time.Duration { // // TODO(roasbeef): add sync ntfn to ensure switch always has consistent view? func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) { - var isSettle bool switch htlc := pkt.htlc.(type) { case *lnwire.UpdateAddHTLC: // If hodl.AddOutgoing mode is active, we exit early to simulate @@ -1328,6 +1327,8 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) { getEventType(pkt), ) + l.tryBatchUpdateCommitTx() + case *lnwire.UpdateFulfillHTLC: // If hodl.SettleOutgoing mode is active, we exit early to // simulate arbitrary delays between the switch adding the @@ -1384,7 +1385,6 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) { // Then we send the HTLC settle message to the connected peer // so we can continue the propagation of the settle message. l.cfg.Peer.SendMessage(false, htlc) - isSettle = true // Send a settle event notification to htlcNotifier. l.cfg.HtlcNotifier.NotifySettleEvent( @@ -1392,6 +1392,9 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) { getEventType(pkt), ) + // Immediately update the commitment tx to minimize latency. + l.updateCommitTxOrFail() + case *lnwire.UpdateFailHTLC: // If hodl.FailOutgoing mode is active, we exit early to // simulate arbitrary delays between the switch adding a FAIL to @@ -1448,7 +1451,6 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) { // We send the HTLC message to the peer which initially created // the HTLC. l.cfg.Peer.SendMessage(false, htlc) - isSettle = true // If the packet does not have a link failure set, it failed // further down the route so we notify a forwarding failure. @@ -1467,17 +1469,20 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) { newHtlcKey(pkt), getEventType(pkt), ) } + + // Immediately update the commitment tx to minimize latency. + l.updateCommitTxOrFail() + } +} + +// tryBatchUpdateCommitTx updates the commitment transaction if the batch is +// full. +func (l *channelLink) tryBatchUpdateCommitTx() { + if l.channel.PendingLocalUpdateCount() < uint64(l.cfg.BatchSize) { + return } - // If this newly added update exceeds the min batch size for adds, or - // this is a settle request, then initiate an update. - if l.channel.PendingLocalUpdateCount() >= uint64(l.cfg.BatchSize) || - isSettle { - - if !l.updateCommitTxOrFail() { - return - } - } + l.updateCommitTxOrFail() } // cleanupSpuriousResponse attempts to ack any AddRef or SettleFailRef