Skip to content

Commit ce7fcd3

Browse files
committed
lnwallet: pack commitment message indices into Dual
This is yet another commit that packs a symmetric structure into a Dual. This is the last one needed for the time being to consolidate Num{X}UpdatesPendingOn{Y} functions into a single one.
1 parent 2e7fbc4 commit ce7fcd3

File tree

2 files changed

+60
-50
lines changed

2 files changed

+60
-50
lines changed

lnwallet/channel.go

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,7 @@ type commitment struct {
282282
// new commitment sent to the remote party includes an index in the
283283
// shared log which details which of their updates we're including in
284284
// this new commitment.
285-
ourMessageIndex uint64
286-
theirMessageIndex uint64
285+
messageIndices lntypes.Dual[uint64]
287286

288287
// [our|their]HtlcIndex are the current running counters for the HTLCs
289288
// offered by either party. This value is incremented each time a party
@@ -500,9 +499,9 @@ func (c *commitment) toDiskCommit(
500499

501500
commit := &channeldb.ChannelCommitment{
502501
CommitHeight: c.height,
503-
LocalLogIndex: c.ourMessageIndex,
502+
LocalLogIndex: c.messageIndices.Local,
504503
LocalHtlcIndex: c.ourHtlcIndex,
505-
RemoteLogIndex: c.theirMessageIndex,
504+
RemoteLogIndex: c.messageIndices.Remote,
506505
RemoteHtlcIndex: c.theirHtlcIndex,
507506
LocalBalance: c.ourBalance,
508507
RemoteBalance: c.theirBalance,
@@ -772,24 +771,28 @@ func (lc *LightningChannel) diskCommitToMemCommit(
772771
return nil, err
773772
}
774773

774+
messageIndices := lntypes.Dual[uint64]{
775+
Local: diskCommit.LocalLogIndex,
776+
Remote: diskCommit.RemoteLogIndex,
777+
}
778+
775779
// With the necessary items generated, we'll now re-construct the
776780
// commitment state as it was originally present in memory.
777781
commit := &commitment{
778-
height: diskCommit.CommitHeight,
779-
whoseCommit: whoseCommit,
780-
ourBalance: diskCommit.LocalBalance,
781-
theirBalance: diskCommit.RemoteBalance,
782-
ourMessageIndex: diskCommit.LocalLogIndex,
783-
ourHtlcIndex: diskCommit.LocalHtlcIndex,
784-
theirMessageIndex: diskCommit.RemoteLogIndex,
785-
theirHtlcIndex: diskCommit.RemoteHtlcIndex,
786-
txn: diskCommit.CommitTx,
787-
sig: diskCommit.CommitSig,
788-
fee: diskCommit.CommitFee,
789-
feePerKw: chainfee.SatPerKWeight(diskCommit.FeePerKw),
790-
incomingHTLCs: incomingHtlcs,
791-
outgoingHTLCs: outgoingHtlcs,
792-
customBlob: diskCommit.CustomBlob,
782+
height: diskCommit.CommitHeight,
783+
whoseCommit: whoseCommit,
784+
ourBalance: diskCommit.LocalBalance,
785+
theirBalance: diskCommit.RemoteBalance,
786+
messageIndices: messageIndices,
787+
ourHtlcIndex: diskCommit.LocalHtlcIndex,
788+
theirHtlcIndex: diskCommit.RemoteHtlcIndex,
789+
txn: diskCommit.CommitTx,
790+
sig: diskCommit.CommitSig,
791+
fee: diskCommit.CommitFee,
792+
feePerKw: chainfee.SatPerKWeight(diskCommit.FeePerKw),
793+
incomingHTLCs: incomingHtlcs,
794+
outgoingHTLCs: outgoingHtlcs,
795+
customBlob: diskCommit.CustomBlob,
793796
}
794797
if whoseCommit.IsLocal() {
795798
commit.dustLimit = lc.channelState.LocalChanCfg.DustLimit
@@ -1758,7 +1761,7 @@ func (lc *LightningChannel) restorePendingRemoteUpdates(
17581761
// height as this commitment will include these updates for
17591762
// their new remote commitment.
17601763
if pendingRemoteCommit != nil {
1761-
if logIdx < pendingRemoteCommit.theirMessageIndex {
1764+
if logIdx < pendingRemoteCommit.messageIndices.Remote {
17621765
height = pendingRemoteCommit.height
17631766
heightSet = true
17641767
}
@@ -2751,22 +2754,26 @@ func (lc *LightningChannel) fetchCommitmentView(
27512754
return nil, fmt.Errorf("unable to fetch aux leaves: %w", err)
27522755
}
27532756

2757+
messageIndices := lntypes.Dual[uint64]{
2758+
Local: ourLogIndex,
2759+
Remote: theirLogIndex,
2760+
}
2761+
27542762
// With the commitment view created, store the resulting balances and
27552763
// transaction with the other parameters for this height.
27562764
c := &commitment{
2757-
ourBalance: commitTx.ourBalance,
2758-
theirBalance: commitTx.theirBalance,
2759-
txn: commitTx.txn,
2760-
fee: commitTx.fee,
2761-
ourMessageIndex: ourLogIndex,
2762-
ourHtlcIndex: ourHtlcIndex,
2763-
theirMessageIndex: theirLogIndex,
2764-
theirHtlcIndex: theirHtlcIndex,
2765-
height: nextHeight,
2766-
feePerKw: feePerKw,
2767-
dustLimit: dustLimit,
2768-
whoseCommit: whoseCommitChain,
2769-
customBlob: newCommitBlob,
2765+
ourBalance: commitTx.ourBalance,
2766+
theirBalance: commitTx.theirBalance,
2767+
txn: commitTx.txn,
2768+
fee: commitTx.fee,
2769+
messageIndices: messageIndices,
2770+
ourHtlcIndex: ourHtlcIndex,
2771+
theirHtlcIndex: theirHtlcIndex,
2772+
height: nextHeight,
2773+
feePerKw: feePerKw,
2774+
dustLimit: dustLimit,
2775+
whoseCommit: whoseCommitChain,
2776+
customBlob: newCommitBlob,
27702777
}
27712778

27722779
// In order to ensure _none_ of the HTLC's associated with this new
@@ -3493,10 +3500,12 @@ func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate {
34933500
chanID := lnwire.NewChanIDFromOutPoint(lc.channelState.FundingOutpoint)
34943501

34953502
// Fetch the last remote update that we have signed for.
3496-
lastRemoteCommitted := lc.commitChains.Remote.tail().theirMessageIndex
3503+
lastRemoteCommitted :=
3504+
lc.commitChains.Remote.tail().messageIndices.Remote
34973505

34983506
// Fetch the last remote update that we have acked.
3499-
lastLocalCommitted := lc.commitChains.Local.tail().theirMessageIndex
3507+
lastLocalCommitted :=
3508+
lc.commitChains.Local.tail().messageIndices.Remote
35003509

35013510
// We'll now run through the remote update log to locate the items that
35023511
// we haven't signed for yet. This will be the set of items we need to
@@ -3982,7 +3991,7 @@ func (lc *LightningChannel) SignNextCommitment() (*NewCommitState, error) {
39823991
}
39833992

39843993
// Determine the last update on the remote log that has been locked in.
3985-
remoteACKedIndex := lc.commitChains.Local.tail().theirMessageIndex
3994+
remoteACKedIndex := lc.commitChains.Local.tail().messageIndices.Remote
39863995
remoteHtlcIndex := lc.commitChains.Local.tail().theirHtlcIndex
39873996

39883997
// Before we extend this new commitment to the remote commitment chain,
@@ -4982,7 +4991,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSigs *CommitSigs) error {
49824991
}
49834992

49844993
// Determine the last update on the local log that has been locked in.
4985-
localACKedIndex := lc.commitChains.Remote.tail().ourMessageIndex
4994+
localACKedIndex := lc.commitChains.Remote.tail().messageIndices.Local
49864995
localHtlcIndex := lc.commitChains.Remote.tail().ourHtlcIndex
49874996

49884997
// Ensure that this new local update from the remote node respects all
@@ -5313,13 +5322,13 @@ func (lc *LightningChannel) oweCommitment(issuer lntypes.ChannelParty) bool {
53135322
// There are local updates pending if our local update log is
53145323
// not in sync with our remote commitment tx.
53155324
localUpdatesPending = lc.updateLogs.Local.logIndex !=
5316-
lastRemoteCommit.ourMessageIndex
5325+
lastRemoteCommit.messageIndices.Local
53175326

53185327
// There are remote updates pending if their remote commitment
53195328
// tx (our local commitment tx) contains updates that we don't
53205329
// have added to our remote commitment tx yet.
5321-
remoteUpdatesPending = lastLocalCommit.theirMessageIndex !=
5322-
lastRemoteCommit.theirMessageIndex
5330+
remoteUpdatesPending = lastLocalCommit.messageIndices.Remote !=
5331+
lastRemoteCommit.messageIndices.Remote
53235332
} else {
53245333
perspective = "remote"
53255334

@@ -5328,13 +5337,13 @@ func (lc *LightningChannel) oweCommitment(issuer lntypes.ChannelParty) bool {
53285337
// updates to their remote tx pending for which they haven't
53295338
// signed yet.
53305339
localUpdatesPending = lc.updateLogs.Remote.logIndex !=
5331-
lastLocalCommit.theirMessageIndex
5340+
lastLocalCommit.messageIndices.Remote
53325341

53335342
// There are remote updates pending (remote updates from the
53345343
// perspective of the remote party) if we have updates on our
53355344
// remote commitment tx that they haven't added to theirs yet.
5336-
remoteUpdatesPending = lastRemoteCommit.ourMessageIndex !=
5337-
lastLocalCommit.ourMessageIndex
5345+
remoteUpdatesPending = lastRemoteCommit.messageIndices.Local !=
5346+
lastLocalCommit.messageIndices.Local
53385347
}
53395348

53405349
// If any of the conditions above is true, we owe a commitment
@@ -5356,7 +5365,8 @@ func (lc *LightningChannel) PendingLocalUpdateCount() uint64 {
53565365

53575366
lastRemoteCommit := lc.commitChains.Remote.tip()
53585367

5359-
return lc.updateLogs.Local.logIndex - lastRemoteCommit.ourMessageIndex
5368+
return lc.updateLogs.Local.logIndex -
5369+
lastRemoteCommit.messageIndices.Local
53605370
}
53615371

53625372
// RevokeCurrentCommitment revokes the next lowest unrevoked commitment
@@ -5615,8 +5625,8 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
56155625

56165626
// We use the remote commitment chain's tip as it will soon become the tail
56175627
// once advanceTail is called.
5618-
remoteMessageIndex := lc.commitChains.Remote.tip().ourMessageIndex
5619-
localMessageIndex := lc.commitChains.Local.tail().ourMessageIndex
5628+
remoteMessageIndex := lc.commitChains.Remote.tip().messageIndices.Local
5629+
localMessageIndex := lc.commitChains.Local.tail().messageIndices.Local
56205630

56215631
localPeerUpdates := lc.unsignedLocalUpdates(
56225632
remoteMessageIndex, localMessageIndex, chanID,
@@ -5938,7 +5948,7 @@ func (lc *LightningChannel) validateAddHtlc(pd *PaymentDescriptor,
59385948
buffer BufferType) error {
59395949
// Make sure adding this HTLC won't violate any of the constraints we
59405950
// must keep on the commitment transactions.
5941-
remoteACKedIndex := lc.commitChains.Local.tail().theirMessageIndex
5951+
remoteACKedIndex := lc.commitChains.Local.tail().messageIndices.Remote
59425952

59435953
// First we'll check whether this HTLC can be added to the remote
59445954
// commitment transaction without violation any of the constraints.
@@ -5994,7 +6004,7 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64,
59946004
// PR).
59956005
}
59966006

5997-
localACKedIndex := lc.commitChains.Remote.tail().ourMessageIndex
6007+
localACKedIndex := lc.commitChains.Remote.tail().messageIndices.Local
59986008

59996009
// Clamp down on the number of HTLC's we can receive by checking the
60006010
// commitment sanity.
@@ -8140,7 +8150,7 @@ func (lc *LightningChannel) availableBalance(
81408150

81418151
// We'll grab the current set of log updates that the remote has
81428152
// ACKed.
8143-
remoteACKedIndex := lc.commitChains.Local.tip().theirMessageIndex
8153+
remoteACKedIndex := lc.commitChains.Local.tip().messageIndices.Remote
81448154
htlcView := lc.fetchHTLCView(remoteACKedIndex,
81458155
lc.updateLogs.Local.logIndex)
81468156

lnwallet/channel_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5230,7 +5230,7 @@ func TestChanCommitWeightDustHtlcs(t *testing.T) {
52305230
// transaction.
52315231
remoteCommitWeight := func(lc *LightningChannel) lntypes.WeightUnit {
52325232
remoteACKedIndex :=
5233-
lc.commitChains.Local.tip().theirMessageIndex
5233+
lc.commitChains.Local.tip().messageIndices.Remote
52345234

52355235
htlcView := lc.fetchHTLCView(remoteACKedIndex,
52365236
lc.updateLogs.Local.logIndex)

0 commit comments

Comments
 (0)