lnwallet+htlcswitch: define expanded NumPendingUpdates

This commit squashes the below operations for a net result where
we have an expanded capability of assessing pending updates. This
is made possible by packing the components into Duals in the prior
commits. We squash the operations to simplify review.

htlcswitch+lnwallet: rename PendingLocalUpdateCount

lnwallet: complete pending update queries API for LightningChannel

lnwallet+htlcswitch: consolidate NumPendingUpdates using ChannelParty

This commit makes the observation that we can cleanly define the
NumPendingUpdates function using a single expression by taking
advantage of the relevant fields being properly packed into Duals.
This commit is contained in:
Keagan McClelland 2023-12-11 19:18:57 -08:00
parent ce7fcd30f8
commit 1422729f80
No known key found for this signature in database
GPG Key ID: FA7E65C951F12439
2 changed files with 22 additions and 16 deletions

View File

@ -958,7 +958,7 @@ func (l *channelLink) resolveFwdPkgs() error {
// If any of our reprocessing steps require an update to the commitment // If any of our reprocessing steps require an update to the commitment
// txn, we initiate a state transition to capture all relevant changes. // 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() return l.updateCommitTx()
} }
@ -1286,15 +1286,19 @@ func (l *channelLink) htlcManager() {
// the batch ticker so that it can be cleared. Otherwise pause // the batch ticker so that it can be cleared. Otherwise pause
// the ticker to prevent waking up the htlcManager while the // the ticker to prevent waking up the htlcManager while the
// batch is empty. // batch is empty.
if l.channel.PendingLocalUpdateCount() > 0 { numUpdates := l.channel.NumPendingUpdates(
lntypes.Local, lntypes.Remote,
)
if numUpdates > 0 {
l.cfg.BatchTicker.Resume() l.cfg.BatchTicker.Resume()
l.log.Tracef("BatchTicker resumed, "+ l.log.Tracef("BatchTicker resumed, "+
"PendingLocalUpdateCount=%d", "NumPendingUpdates(Local, Remote)=%d",
l.channel.PendingLocalUpdateCount()) numUpdates,
)
} else { } else {
l.cfg.BatchTicker.Pause() l.cfg.BatchTicker.Pause()
l.log.Trace("BatchTicker paused due to zero " + l.log.Trace("BatchTicker paused due to zero " +
"PendingLocalUpdateCount") "NumPendingUpdates(Local, Remote)")
} }
select { select {
@ -1652,7 +1656,7 @@ func (l *channelLink) handleDownstreamUpdateAdd(pkt *htlcPacket) error {
l.log.Tracef("received downstream htlc: payment_hash=%x, "+ l.log.Tracef("received downstream htlc: payment_hash=%x, "+
"local_log_index=%v, pend_updates=%v", "local_log_index=%v, pend_updates=%v",
htlc.PaymentHash[:], index, htlc.PaymentHash[:], index,
l.channel.PendingLocalUpdateCount()) l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote))
pkt.outgoingChanID = l.ShortChanID() pkt.outgoingChanID = l.ShortChanID()
pkt.outgoingHTLCID = index pkt.outgoingHTLCID = index
@ -1858,7 +1862,8 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) {
// tryBatchUpdateCommitTx updates the commitment transaction if the batch is // tryBatchUpdateCommitTx updates the commitment transaction if the batch is
// full. // full.
func (l *channelLink) tryBatchUpdateCommitTx() { 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 return
} }
@ -1934,7 +1939,6 @@ func (l *channelLink) cleanupSpuriousResponse(pkt *htlcPacket) {
// direct channel with, updating our respective commitment chains. // direct channel with, updating our respective commitment chains.
func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) {
switch msg := msg.(type) { switch msg := msg.(type) {
case *lnwire.UpdateAddHTLC: case *lnwire.UpdateAddHTLC:
if l.IsFlushing(Incoming) { if l.IsFlushing(Incoming) {
// This is forbidden by the protocol specification. // This is forbidden by the protocol specification.
@ -2592,9 +2596,9 @@ func (l *channelLink) updateCommitTx() error {
l.cfg.PendingCommitTicker.Resume() l.cfg.PendingCommitTicker.Resume()
l.log.Trace("PendingCommitTicker resumed") l.log.Trace("PendingCommitTicker resumed")
n := l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote)
l.log.Tracef("revocation window exhausted, unable to send: "+ l.log.Tracef("revocation window exhausted, unable to send: "+
"%v, pend_updates=%v, dangling_closes%v", "%v, pend_updates=%v, dangling_closes%v", n,
l.channel.PendingLocalUpdateCount(),
lnutils.SpewLogClosure(l.openedCircuits), lnutils.SpewLogClosure(l.openedCircuits),
lnutils.SpewLogClosure(l.closedCircuits)) lnutils.SpewLogClosure(l.closedCircuits))

View File

@ -5357,16 +5357,18 @@ func (lc *LightningChannel) oweCommitment(issuer lntypes.ChannelParty) bool {
return oweCommitment return oweCommitment
} }
// PendingLocalUpdateCount returns the number of local updates that still need // NumPendingUpdates returns the number of updates originated by whoseUpdates
// to be applied to the remote commitment tx. // that have not been committed to the *tip* of whoseCommit's commitment chain.
func (lc *LightningChannel) PendingLocalUpdateCount() uint64 { func (lc *LightningChannel) NumPendingUpdates(whoseUpdates lntypes.ChannelParty,
whoseCommit lntypes.ChannelParty) uint64 {
lc.RLock() lc.RLock()
defer lc.RUnlock() 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 - return updateIndex - lastCommit.messageIndices.GetForParty(whoseUpdates)
lastRemoteCommit.messageIndices.Local
} }
// RevokeCurrentCommitment revokes the next lowest unrevoked commitment // RevokeCurrentCommitment revokes the next lowest unrevoked commitment