htlcswitch: update commit tx per downstream msg type

Unroll common code to allow splitting in separate handlers per message
type.
This commit is contained in:
Joost Jager 2020-05-13 15:26:41 +02:00
parent b559811bf5
commit 55930df70d
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -1261,7 +1261,6 @@ func (l *channelLink) randomFeeUpdateTimeout() time.Duration {
// //
// TODO(roasbeef): add sync ntfn to ensure switch always has consistent view? // TODO(roasbeef): add sync ntfn to ensure switch always has consistent view?
func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) { func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) {
var isSettle bool
switch htlc := pkt.htlc.(type) { switch htlc := pkt.htlc.(type) {
case *lnwire.UpdateAddHTLC: case *lnwire.UpdateAddHTLC:
// If hodl.AddOutgoing mode is active, we exit early to simulate // If hodl.AddOutgoing mode is active, we exit early to simulate
@ -1328,6 +1327,8 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) {
getEventType(pkt), getEventType(pkt),
) )
l.tryBatchUpdateCommitTx()
case *lnwire.UpdateFulfillHTLC: case *lnwire.UpdateFulfillHTLC:
// If hodl.SettleOutgoing mode is active, we exit early to // If hodl.SettleOutgoing mode is active, we exit early to
// simulate arbitrary delays between the switch adding the // 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 // Then we send the HTLC settle message to the connected peer
// so we can continue the propagation of the settle message. // so we can continue the propagation of the settle message.
l.cfg.Peer.SendMessage(false, htlc) l.cfg.Peer.SendMessage(false, htlc)
isSettle = true
// Send a settle event notification to htlcNotifier. // Send a settle event notification to htlcNotifier.
l.cfg.HtlcNotifier.NotifySettleEvent( l.cfg.HtlcNotifier.NotifySettleEvent(
@ -1392,6 +1392,9 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) {
getEventType(pkt), getEventType(pkt),
) )
// Immediately update the commitment tx to minimize latency.
l.updateCommitTxOrFail()
case *lnwire.UpdateFailHTLC: case *lnwire.UpdateFailHTLC:
// If hodl.FailOutgoing mode is active, we exit early to // If hodl.FailOutgoing mode is active, we exit early to
// simulate arbitrary delays between the switch adding a FAIL 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 // We send the HTLC message to the peer which initially created
// the HTLC. // the HTLC.
l.cfg.Peer.SendMessage(false, htlc) l.cfg.Peer.SendMessage(false, htlc)
isSettle = true
// If the packet does not have a link failure set, it failed // If the packet does not have a link failure set, it failed
// further down the route so we notify a forwarding failure. // further down the route so we notify a forwarding failure.
@ -1467,17 +1469,20 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) {
newHtlcKey(pkt), getEventType(pkt), newHtlcKey(pkt), getEventType(pkt),
) )
} }
// Immediately update the commitment tx to minimize latency.
l.updateCommitTxOrFail()
} }
}
// If this newly added update exceeds the min batch size for adds, or // tryBatchUpdateCommitTx updates the commitment transaction if the batch is
// this is a settle request, then initiate an update. // full.
if l.channel.PendingLocalUpdateCount() >= uint64(l.cfg.BatchSize) || func (l *channelLink) tryBatchUpdateCommitTx() {
isSettle { if l.channel.PendingLocalUpdateCount() < uint64(l.cfg.BatchSize) {
if !l.updateCommitTxOrFail() {
return return
} }
}
l.updateCommitTxOrFail()
} }
// cleanupSpuriousResponse attempts to ack any AddRef or SettleFailRef // cleanupSpuriousResponse attempts to ack any AddRef or SettleFailRef