diff --git a/htlcswitch/link.go b/htlcswitch/link.go index c459ef997..81c82cfa2 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -958,7 +958,7 @@ func (l *channelLink) resolveFwdPkgs() error { // If any of our reprocessing steps require an update to the commitment // txn, we initiate a state transition to capture all relevant changes. - if l.channel.PendingLocalUpdateCount() > 0 { + if l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote) > 0 { return l.updateCommitTx() } @@ -1286,15 +1286,19 @@ func (l *channelLink) htlcManager() { // 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.channel.PendingLocalUpdateCount() > 0 { + numUpdates := l.channel.NumPendingUpdates( + lntypes.Local, lntypes.Remote, + ) + if numUpdates > 0 { l.cfg.BatchTicker.Resume() l.log.Tracef("BatchTicker resumed, "+ - "PendingLocalUpdateCount=%d", - l.channel.PendingLocalUpdateCount()) + "NumPendingUpdates(Local, Remote)=%d", + numUpdates, + ) } else { l.cfg.BatchTicker.Pause() l.log.Trace("BatchTicker paused due to zero " + - "PendingLocalUpdateCount") + "NumPendingUpdates(Local, Remote)") } select { @@ -1652,7 +1656,7 @@ func (l *channelLink) handleDownstreamUpdateAdd(pkt *htlcPacket) error { l.log.Tracef("received downstream htlc: payment_hash=%x, "+ "local_log_index=%v, pend_updates=%v", htlc.PaymentHash[:], index, - l.channel.PendingLocalUpdateCount()) + l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote)) pkt.outgoingChanID = l.ShortChanID() pkt.outgoingHTLCID = index @@ -1858,7 +1862,8 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) { // tryBatchUpdateCommitTx updates the commitment transaction if the batch is // full. func (l *channelLink) tryBatchUpdateCommitTx() { - if l.channel.PendingLocalUpdateCount() < uint64(l.cfg.BatchSize) { + pending := l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote) + if pending < uint64(l.cfg.BatchSize) { return } @@ -1934,7 +1939,6 @@ func (l *channelLink) cleanupSpuriousResponse(pkt *htlcPacket) { // direct channel with, updating our respective commitment chains. func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { switch msg := msg.(type) { - case *lnwire.UpdateAddHTLC: if l.IsFlushing(Incoming) { // This is forbidden by the protocol specification. @@ -2592,9 +2596,9 @@ func (l *channelLink) updateCommitTx() error { l.cfg.PendingCommitTicker.Resume() l.log.Trace("PendingCommitTicker resumed") + n := l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote) l.log.Tracef("revocation window exhausted, unable to send: "+ - "%v, pend_updates=%v, dangling_closes%v", - l.channel.PendingLocalUpdateCount(), + "%v, pend_updates=%v, dangling_closes%v", n, lnutils.SpewLogClosure(l.openedCircuits), lnutils.SpewLogClosure(l.closedCircuits)) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index ada7ec8fd..81930aad5 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -5357,16 +5357,18 @@ func (lc *LightningChannel) oweCommitment(issuer lntypes.ChannelParty) bool { return oweCommitment } -// PendingLocalUpdateCount returns the number of local updates that still need -// to be applied to the remote commitment tx. -func (lc *LightningChannel) PendingLocalUpdateCount() uint64 { +// NumPendingUpdates returns the number of updates originated by whoseUpdates +// that have not been committed to the *tip* of whoseCommit's commitment chain. +func (lc *LightningChannel) NumPendingUpdates(whoseUpdates lntypes.ChannelParty, + whoseCommit lntypes.ChannelParty) uint64 { + lc.RLock() defer lc.RUnlock() - lastRemoteCommit := lc.commitChains.Remote.tip() + lastCommit := lc.commitChains.GetForParty(whoseCommit).tip() + updateIndex := lc.updateLogs.GetForParty(whoseUpdates).logIndex - return lc.updateLogs.Local.logIndex - - lastRemoteCommit.messageIndices.Local + return updateIndex - lastCommit.messageIndices.GetForParty(whoseUpdates) } // RevokeCurrentCommitment revokes the next lowest unrevoked commitment