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.
This commit is contained in:
Keagan McClelland 2024-08-09 14:52:21 -07:00 committed by Oliver Gugger
parent 214dac0c45
commit b337213fb2
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
2 changed files with 60 additions and 50 deletions

View File

@ -282,8 +282,7 @@ type commitment struct {
// new commitment sent to the remote party includes an index in the // new commitment sent to the remote party includes an index in the
// shared log which details which of their updates we're including in // shared log which details which of their updates we're including in
// this new commitment. // this new commitment.
ourMessageIndex uint64 messageIndices lntypes.Dual[uint64]
theirMessageIndex uint64
// [our|their]HtlcIndex are the current running counters for the HTLCs // [our|their]HtlcIndex are the current running counters for the HTLCs
// offered by either party. This value is incremented each time a party // offered by either party. This value is incremented each time a party
@ -500,9 +499,9 @@ func (c *commitment) toDiskCommit(
commit := &channeldb.ChannelCommitment{ commit := &channeldb.ChannelCommitment{
CommitHeight: c.height, CommitHeight: c.height,
LocalLogIndex: c.ourMessageIndex, LocalLogIndex: c.messageIndices.Local,
LocalHtlcIndex: c.ourHtlcIndex, LocalHtlcIndex: c.ourHtlcIndex,
RemoteLogIndex: c.theirMessageIndex, RemoteLogIndex: c.messageIndices.Remote,
RemoteHtlcIndex: c.theirHtlcIndex, RemoteHtlcIndex: c.theirHtlcIndex,
LocalBalance: c.ourBalance, LocalBalance: c.ourBalance,
RemoteBalance: c.theirBalance, RemoteBalance: c.theirBalance,
@ -772,24 +771,28 @@ func (lc *LightningChannel) diskCommitToMemCommit(
return nil, err return nil, err
} }
messageIndices := lntypes.Dual[uint64]{
Local: diskCommit.LocalLogIndex,
Remote: diskCommit.RemoteLogIndex,
}
// With the necessary items generated, we'll now re-construct the // With the necessary items generated, we'll now re-construct the
// commitment state as it was originally present in memory. // commitment state as it was originally present in memory.
commit := &commitment{ commit := &commitment{
height: diskCommit.CommitHeight, height: diskCommit.CommitHeight,
whoseCommit: whoseCommit, whoseCommit: whoseCommit,
ourBalance: diskCommit.LocalBalance, ourBalance: diskCommit.LocalBalance,
theirBalance: diskCommit.RemoteBalance, theirBalance: diskCommit.RemoteBalance,
ourMessageIndex: diskCommit.LocalLogIndex, messageIndices: messageIndices,
ourHtlcIndex: diskCommit.LocalHtlcIndex, ourHtlcIndex: diskCommit.LocalHtlcIndex,
theirMessageIndex: diskCommit.RemoteLogIndex, theirHtlcIndex: diskCommit.RemoteHtlcIndex,
theirHtlcIndex: diskCommit.RemoteHtlcIndex, txn: diskCommit.CommitTx,
txn: diskCommit.CommitTx, sig: diskCommit.CommitSig,
sig: diskCommit.CommitSig, fee: diskCommit.CommitFee,
fee: diskCommit.CommitFee, feePerKw: chainfee.SatPerKWeight(diskCommit.FeePerKw),
feePerKw: chainfee.SatPerKWeight(diskCommit.FeePerKw), incomingHTLCs: incomingHtlcs,
incomingHTLCs: incomingHtlcs, outgoingHTLCs: outgoingHtlcs,
outgoingHTLCs: outgoingHtlcs, customBlob: diskCommit.CustomBlob,
customBlob: diskCommit.CustomBlob,
} }
if whoseCommit.IsLocal() { if whoseCommit.IsLocal() {
commit.dustLimit = lc.channelState.LocalChanCfg.DustLimit commit.dustLimit = lc.channelState.LocalChanCfg.DustLimit
@ -1758,7 +1761,7 @@ func (lc *LightningChannel) restorePendingRemoteUpdates(
// height as this commitment will include these updates for // height as this commitment will include these updates for
// their new remote commitment. // their new remote commitment.
if pendingRemoteCommit != nil { if pendingRemoteCommit != nil {
if logIdx < pendingRemoteCommit.theirMessageIndex { if logIdx < pendingRemoteCommit.messageIndices.Remote {
height = pendingRemoteCommit.height height = pendingRemoteCommit.height
heightSet = true heightSet = true
} }
@ -2751,22 +2754,26 @@ func (lc *LightningChannel) fetchCommitmentView(
return nil, fmt.Errorf("unable to fetch aux leaves: %w", err) return nil, fmt.Errorf("unable to fetch aux leaves: %w", err)
} }
messageIndices := lntypes.Dual[uint64]{
Local: ourLogIndex,
Remote: theirLogIndex,
}
// With the commitment view created, store the resulting balances and // With the commitment view created, store the resulting balances and
// transaction with the other parameters for this height. // transaction with the other parameters for this height.
c := &commitment{ c := &commitment{
ourBalance: commitTx.ourBalance, ourBalance: commitTx.ourBalance,
theirBalance: commitTx.theirBalance, theirBalance: commitTx.theirBalance,
txn: commitTx.txn, txn: commitTx.txn,
fee: commitTx.fee, fee: commitTx.fee,
ourMessageIndex: ourLogIndex, messageIndices: messageIndices,
ourHtlcIndex: ourHtlcIndex, ourHtlcIndex: ourHtlcIndex,
theirMessageIndex: theirLogIndex, theirHtlcIndex: theirHtlcIndex,
theirHtlcIndex: theirHtlcIndex, height: nextHeight,
height: nextHeight, feePerKw: feePerKw,
feePerKw: feePerKw, dustLimit: dustLimit,
dustLimit: dustLimit, whoseCommit: whoseCommitChain,
whoseCommit: whoseCommitChain, customBlob: newCommitBlob,
customBlob: newCommitBlob,
} }
// In order to ensure _none_ of the HTLC's associated with this new // In order to ensure _none_ of the HTLC's associated with this new
@ -3493,10 +3500,12 @@ func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate {
chanID := lnwire.NewChanIDFromOutPoint(lc.channelState.FundingOutpoint) chanID := lnwire.NewChanIDFromOutPoint(lc.channelState.FundingOutpoint)
// Fetch the last remote update that we have signed for. // Fetch the last remote update that we have signed for.
lastRemoteCommitted := lc.commitChains.Remote.tail().theirMessageIndex lastRemoteCommitted :=
lc.commitChains.Remote.tail().messageIndices.Remote
// Fetch the last remote update that we have acked. // Fetch the last remote update that we have acked.
lastLocalCommitted := lc.commitChains.Local.tail().theirMessageIndex lastLocalCommitted :=
lc.commitChains.Local.tail().messageIndices.Remote
// We'll now run through the remote update log to locate the items that // We'll now run through the remote update log to locate the items that
// we haven't signed for yet. This will be the set of items we need to // 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) {
} }
// Determine the last update on the remote log that has been locked in. // Determine the last update on the remote log that has been locked in.
remoteACKedIndex := lc.commitChains.Local.tail().theirMessageIndex remoteACKedIndex := lc.commitChains.Local.tail().messageIndices.Remote
remoteHtlcIndex := lc.commitChains.Local.tail().theirHtlcIndex remoteHtlcIndex := lc.commitChains.Local.tail().theirHtlcIndex
// Before we extend this new commitment to the remote commitment chain, // Before we extend this new commitment to the remote commitment chain,
@ -5014,7 +5023,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSigs *CommitSigs) error {
} }
// Determine the last update on the local log that has been locked in. // Determine the last update on the local log that has been locked in.
localACKedIndex := lc.commitChains.Remote.tail().ourMessageIndex localACKedIndex := lc.commitChains.Remote.tail().messageIndices.Local
localHtlcIndex := lc.commitChains.Remote.tail().ourHtlcIndex localHtlcIndex := lc.commitChains.Remote.tail().ourHtlcIndex
// Ensure that this new local update from the remote node respects all // Ensure that this new local update from the remote node respects all
@ -5345,13 +5354,13 @@ func (lc *LightningChannel) oweCommitment(issuer lntypes.ChannelParty) bool {
// There are local updates pending if our local update log is // There are local updates pending if our local update log is
// not in sync with our remote commitment tx. // not in sync with our remote commitment tx.
localUpdatesPending = lc.updateLogs.Local.logIndex != localUpdatesPending = lc.updateLogs.Local.logIndex !=
lastRemoteCommit.ourMessageIndex lastRemoteCommit.messageIndices.Local
// There are remote updates pending if their remote commitment // There are remote updates pending if their remote commitment
// tx (our local commitment tx) contains updates that we don't // tx (our local commitment tx) contains updates that we don't
// have added to our remote commitment tx yet. // have added to our remote commitment tx yet.
remoteUpdatesPending = lastLocalCommit.theirMessageIndex != remoteUpdatesPending = lastLocalCommit.messageIndices.Remote !=
lastRemoteCommit.theirMessageIndex lastRemoteCommit.messageIndices.Remote
} else { } else {
perspective = "remote" perspective = "remote"
@ -5360,13 +5369,13 @@ func (lc *LightningChannel) oweCommitment(issuer lntypes.ChannelParty) bool {
// updates to their remote tx pending for which they haven't // updates to their remote tx pending for which they haven't
// signed yet. // signed yet.
localUpdatesPending = lc.updateLogs.Remote.logIndex != localUpdatesPending = lc.updateLogs.Remote.logIndex !=
lastLocalCommit.theirMessageIndex lastLocalCommit.messageIndices.Remote
// There are remote updates pending (remote updates from the // There are remote updates pending (remote updates from the
// perspective of the remote party) if we have updates on our // perspective of the remote party) if we have updates on our
// remote commitment tx that they haven't added to theirs yet. // remote commitment tx that they haven't added to theirs yet.
remoteUpdatesPending = lastRemoteCommit.ourMessageIndex != remoteUpdatesPending = lastRemoteCommit.messageIndices.Local !=
lastLocalCommit.ourMessageIndex lastLocalCommit.messageIndices.Local
} }
// If any of the conditions above is true, we owe a commitment // If any of the conditions above is true, we owe a commitment
@ -5388,7 +5397,8 @@ func (lc *LightningChannel) PendingLocalUpdateCount() uint64 {
lastRemoteCommit := lc.commitChains.Remote.tip() lastRemoteCommit := lc.commitChains.Remote.tip()
return lc.updateLogs.Local.logIndex - lastRemoteCommit.ourMessageIndex return lc.updateLogs.Local.logIndex -
lastRemoteCommit.messageIndices.Local
} }
// RevokeCurrentCommitment revokes the next lowest unrevoked commitment // RevokeCurrentCommitment revokes the next lowest unrevoked commitment
@ -5647,8 +5657,8 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
// We use the remote commitment chain's tip as it will soon become the tail // We use the remote commitment chain's tip as it will soon become the tail
// once advanceTail is called. // once advanceTail is called.
remoteMessageIndex := lc.commitChains.Remote.tip().ourMessageIndex remoteMessageIndex := lc.commitChains.Remote.tip().messageIndices.Local
localMessageIndex := lc.commitChains.Local.tail().ourMessageIndex localMessageIndex := lc.commitChains.Local.tail().messageIndices.Local
localPeerUpdates := lc.unsignedLocalUpdates( localPeerUpdates := lc.unsignedLocalUpdates(
remoteMessageIndex, localMessageIndex, chanID, remoteMessageIndex, localMessageIndex, chanID,
@ -5970,7 +5980,7 @@ func (lc *LightningChannel) validateAddHtlc(pd *PaymentDescriptor,
buffer BufferType) error { buffer BufferType) error {
// Make sure adding this HTLC won't violate any of the constraints we // Make sure adding this HTLC won't violate any of the constraints we
// must keep on the commitment transactions. // must keep on the commitment transactions.
remoteACKedIndex := lc.commitChains.Local.tail().theirMessageIndex remoteACKedIndex := lc.commitChains.Local.tail().messageIndices.Remote
// First we'll check whether this HTLC can be added to the remote // First we'll check whether this HTLC can be added to the remote
// commitment transaction without violation any of the constraints. // commitment transaction without violation any of the constraints.
@ -6026,7 +6036,7 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64,
// PR). // PR).
} }
localACKedIndex := lc.commitChains.Remote.tail().ourMessageIndex localACKedIndex := lc.commitChains.Remote.tail().messageIndices.Local
// Clamp down on the number of HTLC's we can receive by checking the // Clamp down on the number of HTLC's we can receive by checking the
// commitment sanity. // commitment sanity.
@ -8172,7 +8182,7 @@ func (lc *LightningChannel) availableBalance(
// We'll grab the current set of log updates that the remote has // We'll grab the current set of log updates that the remote has
// ACKed. // ACKed.
remoteACKedIndex := lc.commitChains.Local.tip().theirMessageIndex remoteACKedIndex := lc.commitChains.Local.tip().messageIndices.Remote
htlcView := lc.fetchHTLCView(remoteACKedIndex, htlcView := lc.fetchHTLCView(remoteACKedIndex,
lc.updateLogs.Local.logIndex) lc.updateLogs.Local.logIndex)

View File

@ -5328,7 +5328,7 @@ func TestChanCommitWeightDustHtlcs(t *testing.T) {
// transaction. // transaction.
remoteCommitWeight := func(lc *LightningChannel) lntypes.WeightUnit { remoteCommitWeight := func(lc *LightningChannel) lntypes.WeightUnit {
remoteACKedIndex := remoteACKedIndex :=
lc.commitChains.Local.tip().theirMessageIndex lc.commitChains.Local.tip().messageIndices.Remote
htlcView := lc.fetchHTLCView(remoteACKedIndex, htlcView := lc.fetchHTLCView(remoteACKedIndex,
lc.updateLogs.Local.logIndex) lc.updateLogs.Local.logIndex)