From 5078d662efdffc79b2e476ebd321b345c47a4202 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Wed, 10 Apr 2019 15:28:55 +0200 Subject: [PATCH] htlcswitch: remove batch counter Now that channel exposes the number of pending local updates, it is no longer necessary to track the batch size separately in the link. --- htlcswitch/link.go | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index d4888b88b..a2f89bb31 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -291,15 +291,6 @@ type channelLink struct { // sure we don't process any more updates. failed bool - // batchCounter is the number of updates which we received from remote - // side, but not include in commitment transaction yet and plus the - // current number of settles that have been sent, but not yet committed - // to the commitment. - // - // TODO(andrew.shvv) remove after we add additional BatchNumber() - // method in state machine. - batchCounter uint32 - // keystoneBatch represents a volatile list of keystones that must be // written before attempting to sign the next commitment txn. These // represent all the HTLC's forwarded to the link from the switch. Once @@ -1027,7 +1018,7 @@ out: // the batch ticker so that it can be cleared. Otherwise pause // the ticker to prevent waking up the htlcManager while the // batch is empty. - if l.batchCounter > 0 { + if l.channel.PendingLocalUpdateCount() > 0 { l.cfg.BatchTicker.Resume() } else { l.cfg.BatchTicker.Pause() @@ -1145,9 +1136,9 @@ out: if ok && l.overflowQueue.Length() != 0 { l.log.Infof("downstream htlc add update with "+ "payment hash(%x) have been added to "+ - "reprocessing queue, batch_size=%v", + "reprocessing queue, pend_updates=%v", htlc.PaymentHash[:], - l.batchCounter) + l.channel.PendingLocalUpdateCount()) l.overflowQueue.AddPkt(pkt) continue @@ -1225,8 +1216,6 @@ loop: func (l *channelLink) processHodlEvent(hodlEvent invoices.HodlEvent, htlc hodlHtlc) error { - l.batchCounter++ - circuitKey := hodlEvent.CircuitKey // Determine required action for the resolution. @@ -1296,9 +1285,9 @@ func (l *channelLink) handleDownStreamPkt(pkt *htlcPacket, isReProcess bool) { case lnwallet.ErrMaxHTLCNumber: l.log.Infof("downstream htlc add update with "+ "payment hash(%x) have been added to "+ - "reprocessing queue, batch: %v", + "reprocessing queue, pend_updates: %v", htlc.PaymentHash[:], - l.batchCounter) + l.channel.PendingLocalUpdateCount()) l.overflowQueue.AddPkt(pkt) return @@ -1375,8 +1364,9 @@ func (l *channelLink) handleDownStreamPkt(pkt *htlcPacket, isReProcess bool) { } l.log.Tracef("received downstream htlc: payment_hash=%x, "+ - "local_log_index=%v, batch_size=%v", - htlc.PaymentHash[:], index, l.batchCounter+1) + "local_log_index=%v, pend_updates=%v", + htlc.PaymentHash[:], index, + l.channel.PendingLocalUpdateCount()) pkt.outgoingChanID = l.ShortChanID() pkt.outgoingHTLCID = index @@ -1507,11 +1497,11 @@ func (l *channelLink) handleDownStreamPkt(pkt *htlcPacket, isReProcess bool) { isSettle = true } - l.batchCounter++ - // If this newly added update exceeds the min batch size for adds, or // this is a settle request, then initiate an update. - if l.batchCounter >= l.cfg.BatchSize || isSettle { + if l.channel.PendingLocalUpdateCount() >= uint64(l.cfg.BatchSize) || + isSettle { + if err := l.updateCommitTx(); err != nil { l.fail(LinkFailureError{code: ErrInternalError}, "unable to update commitment: %v", err) @@ -1989,8 +1979,9 @@ func (l *channelLink) updateCommitTx() error { theirCommitSig, htlcSigs, pendingHTLCs, err := l.channel.SignNextCommitment() if err == lnwallet.ErrNoWindow { l.log.Tracef("revocation window exhausted, unable to send: "+ - "%v, dangling_opens=%v, dangling_closes%v", - l.batchCounter, newLogClosure(func() string { + "%v, pend_updates=%v, dangling_closes%v", + l.channel.PendingLocalUpdateCount(), + newLogClosure(func() string { return spew.Sdump(l.openedCircuits) }), newLogClosure(func() string { @@ -2036,10 +2027,6 @@ func (l *channelLink) updateCommitTx() error { } l.logCommitTick = nil - // Finally, clear our the current batch, so we can accurately make - // further batch flushing decisions. - l.batchCounter = 0 - return nil }