lnwallet: pack paymentDescriptor add/remove heights into Duals

The purpose of this commit is to begin the process of packing
symmetric fields into the newly introduced Dual structure. The
reason for this is that the Dual structure has a handy indexing
method where we can supply a ChannelParty and get back a value.
This will cut down on the amount of branching code in the main
lines of the codebase logic, making it easier to follow what is
going on.
This commit is contained in:
Keagan McClelland committed 2024-10-11 11:10:37 +02:00
1 parent 0dd58ee529
commit b457b40f80
5 files changed
+301 -224

No files matched your search

+4 -4
View File
@@ -109,10 +109,10 @@ func newAuxHtlcDescriptor(p *paymentDescriptor) AuxHtlcDescriptor {
ParentIndex: p.ParentIndex, ParentIndex: p.ParentIndex,
EntryType: p.EntryType, EntryType: p.EntryType,
CustomRecords: p.CustomRecords.Copy(), CustomRecords: p.CustomRecords.Copy(),
addCommitHeightRemote: p.addCommitHeightRemote, addCommitHeightRemote: p.addCommitHeights.Remote,
addCommitHeightLocal: p.addCommitHeightLocal, addCommitHeightLocal: p.addCommitHeights.Local,
removeCommitHeightRemote: p.removeCommitHeightRemote, removeCommitHeightRemote: p.removeCommitHeights.Remote,
removeCommitHeightLocal: p.removeCommitHeightLocal, removeCommitHeightLocal: p.removeCommitHeights.Local,
} }
} }
+172 -135
View File
@@ -1080,17 +1080,19 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
// as we've included this HTLC in our local commitment chain // as we've included this HTLC in our local commitment chain
// for the remote party. // for the remote party.
pd = &paymentDescriptor{ pd = &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
RHash: wireMsg.PaymentHash, RHash: wireMsg.PaymentHash,
Timeout: wireMsg.Expiry, Timeout: wireMsg.Expiry,
Amount: wireMsg.Amount, Amount: wireMsg.Amount,
EntryType: Add, EntryType: Add,
HtlcIndex: wireMsg.ID, HtlcIndex: wireMsg.ID,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
addCommitHeightRemote: commitHeight, OnionBlob: wireMsg.OnionBlob,
OnionBlob: wireMsg.OnionBlob, BlindingPoint: wireMsg.BlindingPoint,
BlindingPoint: wireMsg.BlindingPoint, CustomRecords: wireMsg.CustomRecords.Copy(),
CustomRecords: wireMsg.CustomRecords.Copy(), addCommitHeights: lntypes.Dual[uint64]{
Remote: commitHeight,
},
} }
isDustRemote := HtlcIsDust( isDustRemote := HtlcIsDust(
@@ -1125,14 +1127,16 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID)
pd = &paymentDescriptor{ pd = &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
RPreimage: wireMsg.PaymentPreimage, RPreimage: wireMsg.PaymentPreimage,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
ParentIndex: ogHTLC.HtlcIndex, ParentIndex: ogHTLC.HtlcIndex,
EntryType: Settle, EntryType: Settle,
removeCommitHeightRemote: commitHeight, removeCommitHeights: lntypes.Dual[uint64]{
Remote: commitHeight,
},
} }
// If we sent a failure for a prior incoming HTLC, then we'll consult // If we sent a failure for a prior incoming HTLC, then we'll consult
@@ -1143,14 +1147,16 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID)
pd = &paymentDescriptor{ pd = &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
ParentIndex: ogHTLC.HtlcIndex, ParentIndex: ogHTLC.HtlcIndex,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
EntryType: Fail, EntryType: Fail,
FailReason: wireMsg.Reason[:], FailReason: wireMsg.Reason[:],
removeCommitHeightRemote: commitHeight, removeCommitHeights: lntypes.Dual[uint64]{
Remote: commitHeight,
},
} }
// HTLC fails due to malformed onion blobs are treated the exact same // HTLC fails due to malformed onion blobs are treated the exact same
@@ -1160,15 +1166,17 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
// TODO(roasbeef): err if nil? // TODO(roasbeef): err if nil?
pd = &paymentDescriptor{ pd = &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
ParentIndex: ogHTLC.HtlcIndex, ParentIndex: ogHTLC.HtlcIndex,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
EntryType: MalformedFail, EntryType: MalformedFail,
FailCode: wireMsg.FailureCode, FailCode: wireMsg.FailureCode,
ShaOnionBlob: wireMsg.ShaOnionBlob, ShaOnionBlob: wireMsg.ShaOnionBlob,
removeCommitHeightRemote: commitHeight, removeCommitHeights: lntypes.Dual[uint64]{
Remote: commitHeight,
},
} }
// For fee updates we'll create a FeeUpdate type to add to the log. We // For fee updates we'll create a FeeUpdate type to add to the log. We
@@ -1184,9 +1192,13 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate,
Amount: lnwire.NewMSatFromSatoshis( Amount: lnwire.NewMSatFromSatoshis(
btcutil.Amount(wireMsg.FeePerKw), btcutil.Amount(wireMsg.FeePerKw),
), ),
EntryType: FeeUpdate, EntryType: FeeUpdate,
addCommitHeightRemote: commitHeight, addCommitHeights: lntypes.Dual[uint64]{
removeCommitHeightRemote: commitHeight, Remote: commitHeight,
},
removeCommitHeights: lntypes.Dual[uint64]{
Remote: commitHeight,
},
} }
} }
@@ -1216,14 +1228,16 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda
ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID)
return &paymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
RPreimage: wireMsg.PaymentPreimage, RPreimage: wireMsg.PaymentPreimage,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
ParentIndex: ogHTLC.HtlcIndex, ParentIndex: ogHTLC.HtlcIndex,
EntryType: Settle, EntryType: Settle,
removeCommitHeightRemote: commitHeight, removeCommitHeights: lntypes.Dual[uint64]{
Remote: commitHeight,
},
}, nil }, nil
// If we sent a failure for a prior incoming HTLC, then we'll consult the // If we sent a failure for a prior incoming HTLC, then we'll consult the
@@ -1233,14 +1247,16 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda
ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID)
return &paymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
ParentIndex: ogHTLC.HtlcIndex, ParentIndex: ogHTLC.HtlcIndex,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
EntryType: Fail, EntryType: Fail,
FailReason: wireMsg.Reason[:], FailReason: wireMsg.Reason[:],
removeCommitHeightRemote: commitHeight, removeCommitHeights: lntypes.Dual[uint64]{
Remote: commitHeight,
},
}, nil }, nil
// HTLC fails due to malformed onion blocks are treated the exact same // HTLC fails due to malformed onion blocks are treated the exact same
@@ -1249,15 +1265,17 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda
ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID)
return &paymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
ParentIndex: ogHTLC.HtlcIndex, ParentIndex: ogHTLC.HtlcIndex,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
EntryType: MalformedFail, EntryType: MalformedFail,
FailCode: wireMsg.FailureCode, FailCode: wireMsg.FailureCode,
ShaOnionBlob: wireMsg.ShaOnionBlob, ShaOnionBlob: wireMsg.ShaOnionBlob,
removeCommitHeightRemote: commitHeight, removeCommitHeights: lntypes.Dual[uint64]{
Remote: commitHeight,
},
}, nil }, nil
case *lnwire.UpdateFee: case *lnwire.UpdateFee:
@@ -1267,9 +1285,13 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda
Amount: lnwire.NewMSatFromSatoshis( Amount: lnwire.NewMSatFromSatoshis(
btcutil.Amount(wireMsg.FeePerKw), btcutil.Amount(wireMsg.FeePerKw),
), ),
EntryType: FeeUpdate, EntryType: FeeUpdate,
addCommitHeightRemote: commitHeight, addCommitHeights: lntypes.Dual[uint64]{
removeCommitHeightRemote: commitHeight, Remote: commitHeight,
},
removeCommitHeights: lntypes.Dual[uint64]{
Remote: commitHeight,
},
}, nil }, nil
default: default:
@@ -1294,17 +1316,19 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd
switch wireMsg := logUpdate.UpdateMsg.(type) { switch wireMsg := logUpdate.UpdateMsg.(type) {
case *lnwire.UpdateAddHTLC: case *lnwire.UpdateAddHTLC:
pd := &paymentDescriptor{ pd := &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
RHash: wireMsg.PaymentHash, RHash: wireMsg.PaymentHash,
Timeout: wireMsg.Expiry, Timeout: wireMsg.Expiry,
Amount: wireMsg.Amount, Amount: wireMsg.Amount,
EntryType: Add, EntryType: Add,
HtlcIndex: wireMsg.ID, HtlcIndex: wireMsg.ID,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
addCommitHeightLocal: commitHeight, OnionBlob: wireMsg.OnionBlob,
OnionBlob: wireMsg.OnionBlob, BlindingPoint: wireMsg.BlindingPoint,
BlindingPoint: wireMsg.BlindingPoint, CustomRecords: wireMsg.CustomRecords.Copy(),
CustomRecords: wireMsg.CustomRecords.Copy(), addCommitHeights: lntypes.Dual[uint64]{
Local: commitHeight,
},
} }
// We don't need to generate an htlc script yet. This will be // We don't need to generate an htlc script yet. This will be
@@ -1319,14 +1343,16 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd
ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID)
return &paymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
RPreimage: wireMsg.PaymentPreimage, RPreimage: wireMsg.PaymentPreimage,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
ParentIndex: ogHTLC.HtlcIndex, ParentIndex: ogHTLC.HtlcIndex,
EntryType: Settle, EntryType: Settle,
removeCommitHeightLocal: commitHeight, removeCommitHeights: lntypes.Dual[uint64]{
Local: commitHeight,
},
}, nil }, nil
// If we received a failure for a prior outgoing HTLC, then we'll // If we received a failure for a prior outgoing HTLC, then we'll
@@ -1336,14 +1362,16 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd
ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID)
return &paymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
ParentIndex: ogHTLC.HtlcIndex, ParentIndex: ogHTLC.HtlcIndex,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
EntryType: Fail, EntryType: Fail,
FailReason: wireMsg.Reason[:], FailReason: wireMsg.Reason[:],
removeCommitHeightLocal: commitHeight, removeCommitHeights: lntypes.Dual[uint64]{
Local: commitHeight,
},
}, nil }, nil
// HTLC fails due to malformed onion blobs are treated the exact same // HTLC fails due to malformed onion blobs are treated the exact same
@@ -1352,15 +1380,17 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd
ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID)
return &paymentDescriptor{ return &paymentDescriptor{
ChanID: wireMsg.ChanID, ChanID: wireMsg.ChanID,
Amount: ogHTLC.Amount, Amount: ogHTLC.Amount,
RHash: ogHTLC.RHash, RHash: ogHTLC.RHash,
ParentIndex: ogHTLC.HtlcIndex, ParentIndex: ogHTLC.HtlcIndex,
LogIndex: logUpdate.LogIndex, LogIndex: logUpdate.LogIndex,
EntryType: MalformedFail, EntryType: MalformedFail,
FailCode: wireMsg.FailureCode, FailCode: wireMsg.FailureCode,
ShaOnionBlob: wireMsg.ShaOnionBlob, ShaOnionBlob: wireMsg.ShaOnionBlob,
removeCommitHeightLocal: commitHeight, removeCommitHeights: lntypes.Dual[uint64]{
Local: commitHeight,
},
}, nil }, nil
// For fee updates we'll create a FeeUpdate type to add to the log. We // For fee updates we'll create a FeeUpdate type to add to the log. We
@@ -1376,9 +1406,13 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd
Amount: lnwire.NewMSatFromSatoshis( Amount: lnwire.NewMSatFromSatoshis(
btcutil.Amount(wireMsg.FeePerKw), btcutil.Amount(wireMsg.FeePerKw),
), ),
EntryType: FeeUpdate, EntryType: FeeUpdate,
addCommitHeightLocal: commitHeight, addCommitHeights: lntypes.Dual[uint64]{
removeCommitHeightLocal: commitHeight, Local: commitHeight,
},
removeCommitHeights: lntypes.Dual[uint64]{
Local: commitHeight,
},
}, nil }, nil
default: default:
@@ -1611,8 +1645,9 @@ func (lc *LightningChannel) restoreStateLogs(
// map we created earlier. Note that if this HTLC is not in // map we created earlier. Note that if this HTLC is not in
// incomingRemoteAddHeights, the remote add height will be set // incomingRemoteAddHeights, the remote add height will be set
// to zero, which indicates that it is not added yet. // to zero, which indicates that it is not added yet.
htlc.addCommitHeightLocal = localCommitment.height htlc.addCommitHeights.Local = localCommitment.height
htlc.addCommitHeightRemote = incomingRemoteAddHeights[htlc.HtlcIndex] htlc.addCommitHeights.Remote =
incomingRemoteAddHeights[htlc.HtlcIndex]
// Restore the htlc back to the remote log. // Restore the htlc back to the remote log.
lc.updateLogs.Remote.restoreHtlc(&htlc) lc.updateLogs.Remote.restoreHtlc(&htlc)
@@ -1626,8 +1661,9 @@ func (lc *LightningChannel) restoreStateLogs(
// As for the incoming HTLCs, we'll use the current remote // As for the incoming HTLCs, we'll use the current remote
// commit height as remote add height, and consult the map // commit height as remote add height, and consult the map
// created above for the local add height. // created above for the local add height.
htlc.addCommitHeightRemote = remoteCommitment.height htlc.addCommitHeights.Remote = remoteCommitment.height
htlc.addCommitHeightLocal = outgoingLocalAddHeights[htlc.HtlcIndex] htlc.addCommitHeights.Local =
outgoingLocalAddHeights[htlc.HtlcIndex]
// Restore the htlc back to the local log. // Restore the htlc back to the local log.
lc.updateLogs.Local.restoreHtlc(&htlc) lc.updateLogs.Local.restoreHtlc(&htlc)
@@ -1722,15 +1758,15 @@ func (lc *LightningChannel) restorePendingRemoteUpdates(
switch payDesc.EntryType { switch payDesc.EntryType {
case FeeUpdate: case FeeUpdate:
if heightSet { if heightSet {
payDesc.addCommitHeightRemote = height payDesc.addCommitHeights.Remote = height
payDesc.removeCommitHeightRemote = height payDesc.removeCommitHeights.Remote = height
} }
lc.updateLogs.Remote.restoreUpdate(payDesc) lc.updateLogs.Remote.restoreUpdate(payDesc)
default: default:
if heightSet { if heightSet {
payDesc.removeCommitHeightRemote = height payDesc.removeCommitHeights.Remote = height
} }
lc.updateLogs.Remote.restoreUpdate(payDesc) lc.updateLogs.Remote.restoreUpdate(payDesc)
@@ -2903,7 +2939,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
// number of satoshis we've received within the channel. // number of satoshis we've received within the channel.
if mutateState && entry.EntryType == Settle && if mutateState && entry.EntryType == Settle &&
whoseCommitChain.IsLocal() && whoseCommitChain.IsLocal() &&
entry.removeCommitHeightLocal == 0 { entry.removeCommitHeights.Local == 0 {
lc.channelState.TotalMSatReceived += entry.Amount lc.channelState.TotalMSatReceived += entry.Amount
} }
@@ -2943,7 +2979,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
// channel. // channel.
if mutateState && entry.EntryType == Settle && if mutateState && entry.EntryType == Settle &&
whoseCommitChain.IsLocal() && whoseCommitChain.IsLocal() &&
entry.removeCommitHeightLocal == 0 { entry.removeCommitHeights.Local == 0 {
lc.channelState.TotalMSatSent += entry.Amount lc.channelState.TotalMSatSent += entry.Amount
} }
@@ -3032,14 +3068,14 @@ func (lc *LightningChannel) fetchParent(entry *paymentDescriptor,
// The parent add height should never be zero at this point. If // The parent add height should never be zero at this point. If
// that's the case we probably forgot to send a new commitment. // that's the case we probably forgot to send a new commitment.
case whoseCommitChain.IsRemote() && case whoseCommitChain.IsRemote() &&
addEntry.addCommitHeightRemote == 0: addEntry.addCommitHeights.Remote == 0:
return nil, fmt.Errorf("parent entry %d for update %d "+ return nil, fmt.Errorf("parent entry %d for update %d "+
"had zero remote add height", entry.ParentIndex, "had zero remote add height", entry.ParentIndex,
entry.LogIndex) entry.LogIndex)
case whoseCommitChain.IsLocal() && case whoseCommitChain.IsLocal() &&
addEntry.addCommitHeightLocal == 0: addEntry.addCommitHeights.Local == 0:
return nil, fmt.Errorf("parent entry %d for update %d "+ return nil, fmt.Errorf("parent entry %d for update %d "+
"had zero local add height", entry.ParentIndex, "had zero local add height", entry.ParentIndex,
@@ -3063,9 +3099,9 @@ func processAddEntry(htlc *paymentDescriptor, ourBalance,
// height w.r.t the local chain. // height w.r.t the local chain.
var addHeight *uint64 var addHeight *uint64
if whoseCommitChain.IsRemote() { if whoseCommitChain.IsRemote() {
addHeight = &htlc.addCommitHeightRemote addHeight = &htlc.addCommitHeights.Remote
} else { } else {
addHeight = &htlc.addCommitHeightLocal addHeight = &htlc.addCommitHeights.Local
} }
if *addHeight != 0 { if *addHeight != 0 {
@@ -3097,9 +3133,9 @@ func processRemoveEntry(htlc *paymentDescriptor, ourBalance,
var removeHeight *uint64 var removeHeight *uint64
if whoseCommitChain.IsRemote() { if whoseCommitChain.IsRemote() {
removeHeight = &htlc.removeCommitHeightRemote removeHeight = &htlc.removeCommitHeights.Remote
} else { } else {
removeHeight = &htlc.removeCommitHeightLocal removeHeight = &htlc.removeCommitHeights.Local
} }
// Ignore any removal entries which have already been processed. // Ignore any removal entries which have already been processed.
@@ -3150,11 +3186,11 @@ func processFeeUpdate(feeUpdate *paymentDescriptor, nextHeight uint64,
var addHeight *uint64 var addHeight *uint64
var removeHeight *uint64 var removeHeight *uint64
if whoseCommitChain.IsRemote() { if whoseCommitChain.IsRemote() {
addHeight = &feeUpdate.addCommitHeightRemote addHeight = &feeUpdate.addCommitHeights.Remote
removeHeight = &feeUpdate.removeCommitHeightRemote removeHeight = &feeUpdate.removeCommitHeights.Remote
} else { } else {
addHeight = &feeUpdate.addCommitHeightLocal addHeight = &feeUpdate.addCommitHeights.Local
removeHeight = &feeUpdate.removeCommitHeightLocal removeHeight = &feeUpdate.removeCommitHeights.Local
} }
if *addHeight != 0 { if *addHeight != 0 {
@@ -3419,8 +3455,8 @@ func (lc *LightningChannel) createCommitDiff(newCommit *commitment,
// If this entry wasn't committed at the exact height of this // If this entry wasn't committed at the exact height of this
// remote commitment, then we'll skip it as it was already // remote commitment, then we'll skip it as it was already
// lingering in the log. // lingering in the log.
if pd.addCommitHeightRemote != newCommit.height && if pd.addCommitHeights.Remote != newCommit.height &&
pd.removeCommitHeightRemote != newCommit.height { pd.removeCommitHeights.Remote != newCommit.height {
continue continue
} }
@@ -5655,19 +5691,20 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
// both of the remote and local heights are non-zero. If either // both of the remote and local heights are non-zero. If either
// of these values is zero, it has yet to be committed in both // of these values is zero, it has yet to be committed in both
// the local and remote chains. // the local and remote chains.
committedAdd := pd.addCommitHeightRemote > 0 && committedAdd := pd.addCommitHeights.Remote > 0 &&
pd.addCommitHeightLocal > 0 pd.addCommitHeights.Local > 0
committedRmv := pd.removeCommitHeightRemote > 0 && committedRmv := pd.removeCommitHeights.Remote > 0 &&
pd.removeCommitHeightLocal > 0 pd.removeCommitHeights.Local > 0
// Using the height of the remote and local commitments, // Using the height of the remote and local commitments,
// preemptively compute whether or not to forward this HTLC for // preemptively compute whether or not to forward this HTLC for
// the case in which this in an Add HTLC, or if this is a // the case in which this in an Add HTLC, or if this is a
// Settle, Fail, or MalformedFail. // Settle, Fail, or MalformedFail.
shouldFwdAdd := remoteChainTail == pd.addCommitHeightRemote && shouldFwdAdd := remoteChainTail == pd.addCommitHeights.Remote &&
localChainTail >= pd.addCommitHeightLocal localChainTail >= pd.addCommitHeights.Local
shouldFwdRmv := remoteChainTail == pd.removeCommitHeightRemote && shouldFwdRmv := remoteChainTail ==
localChainTail >= pd.removeCommitHeightLocal pd.removeCommitHeights.Remote &&
localChainTail >= pd.removeCommitHeights.Local
// We'll only forward any new HTLC additions iff, it's "freshly // We'll only forward any new HTLC additions iff, it's "freshly
// locked in". Meaning that the HTLC was only *just* considered // locked in". Meaning that the HTLC was only *just* considered
+118 -76
View File
@@ -7614,13 +7614,13 @@ func TestChannelRestoreCommitHeight(t *testing.T) {
t.Fatalf("htlc not found in log") t.Fatalf("htlc not found in log")
} }
if pd.addCommitHeightLocal != expLocal { if pd.addCommitHeights.Local != expLocal {
t.Fatalf("expected local add height to be %d, was %d", t.Fatalf("expected local add height to be %d, was %d",
expLocal, pd.addCommitHeightLocal) expLocal, pd.addCommitHeights.Local)
} }
if pd.addCommitHeightRemote != expRemote { if pd.addCommitHeights.Remote != expRemote {
t.Fatalf("expected remote add height to be %d, was %d", t.Fatalf("expected remote add height to be %d, was %d",
expRemote, pd.addCommitHeightRemote) expRemote, pd.addCommitHeights.Remote)
} }
return newChannel return newChannel
} }
@@ -8402,16 +8402,20 @@ func TestFetchParent(t *testing.T) {
remoteEntries: []*paymentDescriptor{ remoteEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
addCommitHeightLocal: 100, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 100, Local: 100,
Remote: 100,
},
}, },
// This entry will be added at log index =1, it // This entry will be added at log index =1, it
// is the parent entry we are looking for. // is the parent entry we are looking for.
{ {
HtlcIndex: 2, HtlcIndex: 2,
addCommitHeightLocal: 100, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 0, Local: 100,
Remote: 0,
},
}, },
}, },
whoseCommitChain: lntypes.Remote, whoseCommitChain: lntypes.Remote,
@@ -8424,16 +8428,20 @@ func TestFetchParent(t *testing.T) {
remoteEntries: []*paymentDescriptor{ remoteEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
addCommitHeightLocal: 100, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 100, Local: 100,
Remote: 100,
},
}, },
// This entry will be added at log index =1, it // This entry will be added at log index =1, it
// is the parent entry we are looking for. // is the parent entry we are looking for.
{ {
HtlcIndex: 2, HtlcIndex: 2,
addCommitHeightLocal: 0, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 100, Local: 0,
Remote: 100,
},
}, },
}, },
localEntries: nil, localEntries: nil,
@@ -8447,16 +8455,20 @@ func TestFetchParent(t *testing.T) {
localEntries: []*paymentDescriptor{ localEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
addCommitHeightLocal: 100, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 100, Local: 100,
Remote: 100,
},
}, },
// This entry will be added at log index =1, it // This entry will be added at log index =1, it
// is the parent entry we are looking for. // is the parent entry we are looking for.
{ {
HtlcIndex: 2, HtlcIndex: 2,
addCommitHeightLocal: 0, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 100, Local: 0,
Remote: 100,
},
}, },
}, },
remoteEntries: nil, remoteEntries: nil,
@@ -8471,16 +8483,20 @@ func TestFetchParent(t *testing.T) {
localEntries: []*paymentDescriptor{ localEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
addCommitHeightLocal: 100, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 100, Local: 100,
Remote: 100,
},
}, },
// This entry will be added at log index =1, it // This entry will be added at log index =1, it
// is the parent entry we are looking for. // is the parent entry we are looking for.
{ {
HtlcIndex: 2, HtlcIndex: 2,
addCommitHeightLocal: 100, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 0, Local: 100,
Remote: 0,
},
}, },
}, },
remoteEntries: nil, remoteEntries: nil,
@@ -8495,16 +8511,20 @@ func TestFetchParent(t *testing.T) {
remoteEntries: []*paymentDescriptor{ remoteEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
addCommitHeightLocal: 100, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 0, Local: 100,
Remote: 0,
},
}, },
// This entry will be added at log index =1, it // This entry will be added at log index =1, it
// is the parent entry we are looking for. // is the parent entry we are looking for.
{ {
HtlcIndex: 2, HtlcIndex: 2,
addCommitHeightLocal: 100, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 100, Local: 100,
Remote: 100,
},
}, },
}, },
whoseCommitChain: lntypes.Remote, whoseCommitChain: lntypes.Remote,
@@ -8518,16 +8538,20 @@ func TestFetchParent(t *testing.T) {
localEntries: []*paymentDescriptor{ localEntries: []*paymentDescriptor{
// This entry will be added at log index =0. // This entry will be added at log index =0.
{ {
HtlcIndex: 1, HtlcIndex: 1,
addCommitHeightLocal: 0, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 100, Local: 0,
Remote: 100,
},
}, },
// This entry will be added at log index =1, it // This entry will be added at log index =1, it
// is the parent entry we are looking for. // is the parent entry we are looking for.
{ {
HtlcIndex: 2, HtlcIndex: 2,
addCommitHeightLocal: 100, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: 100, Local: 100,
Remote: 100,
},
}, },
}, },
remoteEntries: nil, remoteEntries: nil,
@@ -8728,10 +8752,12 @@ func TestEvaluateView(t *testing.T) {
mutateState: true, mutateState: true,
ourHtlcs: []*paymentDescriptor{ ourHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
EntryType: Add, EntryType: Add,
addCommitHeightLocal: addHeight, addCommitHeights: lntypes.Dual[uint64]{
Local: addHeight,
},
}, },
}, },
theirHtlcs: []*paymentDescriptor{ theirHtlcs: []*paymentDescriptor{
@@ -8763,10 +8789,12 @@ func TestEvaluateView(t *testing.T) {
mutateState: false, mutateState: false,
ourHtlcs: []*paymentDescriptor{ ourHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
EntryType: Add, EntryType: Add,
addCommitHeightLocal: addHeight, addCommitHeights: lntypes.Dual[uint64]{
Local: addHeight,
},
}, },
}, },
theirHtlcs: []*paymentDescriptor{ theirHtlcs: []*paymentDescriptor{
@@ -8813,16 +8841,20 @@ func TestEvaluateView(t *testing.T) {
}, },
theirHtlcs: []*paymentDescriptor{ theirHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
EntryType: Add, EntryType: Add,
addCommitHeightLocal: addHeight, addCommitHeights: lntypes.Dual[uint64]{
Local: addHeight,
},
}, },
{ {
HtlcIndex: 1, HtlcIndex: 1,
Amount: htlcAddAmount, Amount: htlcAddAmount,
EntryType: Add, EntryType: Add,
addCommitHeightLocal: addHeight, addCommitHeights: lntypes.Dual[uint64]{
Local: addHeight,
},
}, },
}, },
expectedFee: feePerKw, expectedFee: feePerKw,
@@ -8857,10 +8889,12 @@ func TestEvaluateView(t *testing.T) {
}, },
theirHtlcs: []*paymentDescriptor{ theirHtlcs: []*paymentDescriptor{
{ {
HtlcIndex: 0, HtlcIndex: 0,
Amount: htlcAddAmount, Amount: htlcAddAmount,
EntryType: Add, EntryType: Add,
addCommitHeightLocal: addHeight, addCommitHeights: lntypes.Dual[uint64]{
Local: addHeight,
},
}, },
}, },
expectedFee: feePerKw, expectedFee: feePerKw,
@@ -9155,12 +9189,16 @@ func TestProcessFeeUpdate(t *testing.T) {
// set in the test. // set in the test.
heights := test.startHeights heights := test.startHeights
update := &paymentDescriptor{ update := &paymentDescriptor{
Amount: ourFeeUpdateAmt, Amount: ourFeeUpdateAmt,
addCommitHeightRemote: heights.remoteAdd, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightLocal: heights.localAdd, Local: heights.localAdd,
removeCommitHeightRemote: heights.remoteRemove, Remote: heights.remoteAdd,
removeCommitHeightLocal: heights.localRemove, },
EntryType: FeeUpdate, removeCommitHeights: lntypes.Dual[uint64]{
Local: heights.localRemove,
Remote: heights.remoteRemove,
},
EntryType: FeeUpdate,
} }
view := &HtlcView{ view := &HtlcView{
@@ -9183,10 +9221,10 @@ func TestProcessFeeUpdate(t *testing.T) {
func checkHeights(t *testing.T, update *paymentDescriptor, expected heights) { func checkHeights(t *testing.T, update *paymentDescriptor, expected heights) {
updateHeights := heights{ updateHeights := heights{
localAdd: update.addCommitHeightLocal, localAdd: update.addCommitHeights.Local,
localRemove: update.removeCommitHeightLocal, localRemove: update.removeCommitHeights.Local,
remoteAdd: update.addCommitHeightRemote, remoteAdd: update.addCommitHeights.Remote,
remoteRemove: update.removeCommitHeightRemote, remoteRemove: update.removeCommitHeights.Remote,
} }
if !reflect.DeepEqual(updateHeights, expected) { if !reflect.DeepEqual(updateHeights, expected) {
@@ -9551,12 +9589,16 @@ func TestProcessAddRemoveEntry(t *testing.T) {
heights := test.startHeights heights := test.startHeights
update := &paymentDescriptor{ update := &paymentDescriptor{
Amount: updateAmount, Amount: updateAmount,
addCommitHeightLocal: heights.localAdd, addCommitHeights: lntypes.Dual[uint64]{
addCommitHeightRemote: heights.remoteAdd, Local: heights.localAdd,
removeCommitHeightLocal: heights.localRemove, Remote: heights.remoteAdd,
removeCommitHeightRemote: heights.remoteRemove, },
EntryType: test.updateType, removeCommitHeights: lntypes.Dual[uint64]{
Local: heights.localRemove,
Remote: heights.remoteRemove,
},
EntryType: test.updateType,
} }
var ( var (
+3 -4
View File
@@ -6,6 +6,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models" "github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
) )
@@ -165,16 +166,14 @@ type paymentDescriptor struct {
// which included this HTLC on either the remote or local commitment // which included this HTLC on either the remote or local commitment
// chain. This value is used to determine when an HTLC is fully // chain. This value is used to determine when an HTLC is fully
// "locked-in". // "locked-in".
addCommitHeightRemote uint64 addCommitHeights lntypes.Dual[uint64]
addCommitHeightLocal uint64
// removeCommitHeight[Remote|Local] encodes the height of the // removeCommitHeight[Remote|Local] encodes the height of the
// commitment which removed the parent pointer of this // commitment which removed the parent pointer of this
// paymentDescriptor either due to a timeout or a settle. Once both // paymentDescriptor either due to a timeout or a settle. Once both
// these heights are below the tail of both chains, the log entries can // these heights are below the tail of both chains, the log entries can
// safely be removed. // safely be removed.
removeCommitHeightRemote uint64 removeCommitHeights lntypes.Dual[uint64]
removeCommitHeightLocal uint64
// OnionBlob is an opaque blob which is used to complete multi-hop // OnionBlob is an opaque blob which is used to complete multi-hop
// routing. // routing.
+4 -5
View File
@@ -153,6 +153,7 @@ func compactLogs(ourLog, theirLog *updateLog,
nextA = e.Next() nextA = e.Next()
htlc := e.Value htlc := e.Value
rmvHeights := htlc.removeCommitHeights
// We skip Adds, as they will be removed along with the // We skip Adds, as they will be removed along with the
// fail/settles below. // fail/settles below.
@@ -162,9 +163,7 @@ func compactLogs(ourLog, theirLog *updateLog,
// If the HTLC hasn't yet been removed from either // If the HTLC hasn't yet been removed from either
// chain, the skip it. // chain, the skip it.
if htlc.removeCommitHeightRemote == 0 || if rmvHeights.Remote == 0 || rmvHeights.Local == 0 {
htlc.removeCommitHeightLocal == 0 {
continue continue
} }
@@ -172,8 +171,8 @@ func compactLogs(ourLog, theirLog *updateLog,
// is at least the height in which the HTLC was // is at least the height in which the HTLC was
// removed, then evict the settle/timeout entry along // removed, then evict the settle/timeout entry along
// with the original add entry. // with the original add entry.
if remoteChainTail >= htlc.removeCommitHeightRemote && if remoteChainTail >= rmvHeights.Remote &&
localChainTail >= htlc.removeCommitHeightLocal { localChainTail >= rmvHeights.Local {
// Fee updates have no parent htlcs, so we only // Fee updates have no parent htlcs, so we only
// remove the update itself. // remove the update itself.