Skip to content

Commit 1422729

Browse files
committed
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.
1 parent ce7fcd3 commit 1422729

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

htlcswitch/link.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ func (l *channelLink) resolveFwdPkgs() error {
958958

959959
// If any of our reprocessing steps require an update to the commitment
960960
// txn, we initiate a state transition to capture all relevant changes.
961-
if l.channel.PendingLocalUpdateCount() > 0 {
961+
if l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote) > 0 {
962962
return l.updateCommitTx()
963963
}
964964

@@ -1286,15 +1286,19 @@ func (l *channelLink) htlcManager() {
12861286
// the batch ticker so that it can be cleared. Otherwise pause
12871287
// the ticker to prevent waking up the htlcManager while the
12881288
// batch is empty.
1289-
if l.channel.PendingLocalUpdateCount() > 0 {
1289+
numUpdates := l.channel.NumPendingUpdates(
1290+
lntypes.Local, lntypes.Remote,
1291+
)
1292+
if numUpdates > 0 {
12901293
l.cfg.BatchTicker.Resume()
12911294
l.log.Tracef("BatchTicker resumed, "+
1292-
"PendingLocalUpdateCount=%d",
1293-
l.channel.PendingLocalUpdateCount())
1295+
"NumPendingUpdates(Local, Remote)=%d",
1296+
numUpdates,
1297+
)
12941298
} else {
12951299
l.cfg.BatchTicker.Pause()
12961300
l.log.Trace("BatchTicker paused due to zero " +
1297-
"PendingLocalUpdateCount")
1301+
"NumPendingUpdates(Local, Remote)")
12981302
}
12991303

13001304
select {
@@ -1652,7 +1656,7 @@ func (l *channelLink) handleDownstreamUpdateAdd(pkt *htlcPacket) error {
16521656
l.log.Tracef("received downstream htlc: payment_hash=%x, "+
16531657
"local_log_index=%v, pend_updates=%v",
16541658
htlc.PaymentHash[:], index,
1655-
l.channel.PendingLocalUpdateCount())
1659+
l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote))
16561660

16571661
pkt.outgoingChanID = l.ShortChanID()
16581662
pkt.outgoingHTLCID = index
@@ -1858,7 +1862,8 @@ func (l *channelLink) handleDownstreamPkt(pkt *htlcPacket) {
18581862
// tryBatchUpdateCommitTx updates the commitment transaction if the batch is
18591863
// full.
18601864
func (l *channelLink) tryBatchUpdateCommitTx() {
1861-
if l.channel.PendingLocalUpdateCount() < uint64(l.cfg.BatchSize) {
1865+
pending := l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote)
1866+
if pending < uint64(l.cfg.BatchSize) {
18621867
return
18631868
}
18641869

@@ -1934,7 +1939,6 @@ func (l *channelLink) cleanupSpuriousResponse(pkt *htlcPacket) {
19341939
// direct channel with, updating our respective commitment chains.
19351940
func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) {
19361941
switch msg := msg.(type) {
1937-
19381942
case *lnwire.UpdateAddHTLC:
19391943
if l.IsFlushing(Incoming) {
19401944
// This is forbidden by the protocol specification.
@@ -2592,9 +2596,9 @@ func (l *channelLink) updateCommitTx() error {
25922596
l.cfg.PendingCommitTicker.Resume()
25932597
l.log.Trace("PendingCommitTicker resumed")
25942598

2599+
n := l.channel.NumPendingUpdates(lntypes.Local, lntypes.Remote)
25952600
l.log.Tracef("revocation window exhausted, unable to send: "+
2596-
"%v, pend_updates=%v, dangling_closes%v",
2597-
l.channel.PendingLocalUpdateCount(),
2601+
"%v, pend_updates=%v, dangling_closes%v", n,
25982602
lnutils.SpewLogClosure(l.openedCircuits),
25992603
lnutils.SpewLogClosure(l.closedCircuits))
26002604

lnwallet/channel.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5357,16 +5357,18 @@ func (lc *LightningChannel) oweCommitment(issuer lntypes.ChannelParty) bool {
53575357
return oweCommitment
53585358
}
53595359

5360-
// PendingLocalUpdateCount returns the number of local updates that still need
5361-
// to be applied to the remote commitment tx.
5362-
func (lc *LightningChannel) PendingLocalUpdateCount() uint64 {
5360+
// NumPendingUpdates returns the number of updates originated by whoseUpdates
5361+
// that have not been committed to the *tip* of whoseCommit's commitment chain.
5362+
func (lc *LightningChannel) NumPendingUpdates(whoseUpdates lntypes.ChannelParty,
5363+
whoseCommit lntypes.ChannelParty) uint64 {
5364+
53635365
lc.RLock()
53645366
defer lc.RUnlock()
53655367

5366-
lastRemoteCommit := lc.commitChains.Remote.tip()
5368+
lastCommit := lc.commitChains.GetForParty(whoseCommit).tip()
5369+
updateIndex := lc.updateLogs.GetForParty(whoseUpdates).logIndex
53675370

5368-
return lc.updateLogs.Local.logIndex -
5369-
lastRemoteCommit.messageIndices.Local
5371+
return updateIndex - lastCommit.messageIndices.GetForParty(whoseUpdates)
53705372
}
53715373

53725374
// RevokeCurrentCommitment revokes the next lowest unrevoked commitment

0 commit comments

Comments
 (0)