lnwallet: consolidate loops in evaluateHTLCView
We had four for-loops in evaluateHTLCView that were exact mirror images of each other. By making use of the new ChannelParty and Dual facilities introduced in prior commits, we consolidate these into two for-loops.
This commit is contained in:
1 file changed
+57
-95
+57
-95
@@ -2905,6 +2905,7 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
|
|||||||
FeePerKw: view.FeePerKw,
|
FeePerKw: view.FeePerKw,
|
||||||
NextHeight: nextHeight,
|
NextHeight: nextHeight,
|
||||||
}
|
}
|
||||||
|
noUncommitted := lntypes.Dual[[]*paymentDescriptor]{}
|
||||||
|
|
||||||
// The fee rate of our view is always the last UpdateFee message from
|
// The fee rate of our view is always the last UpdateFee message from
|
||||||
// the channel's OpeningParty.
|
// the channel's OpeningParty.
|
||||||
@@ -2923,115 +2924,76 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
|
|||||||
// keep track of which entries we need to skip when creating the final
|
// keep track of which entries we need to skip when creating the final
|
||||||
// htlc view. We skip an entry whenever we find a settle or a timeout
|
// htlc view. We skip an entry whenever we find a settle or a timeout
|
||||||
// modifying an entry.
|
// modifying an entry.
|
||||||
skipUs := fn.NewSet[uint64]()
|
skip := lntypes.Dual[fn.Set[uint64]]{
|
||||||
skipThem := fn.NewSet[uint64]()
|
Local: fn.NewSet[uint64](),
|
||||||
|
Remote: fn.NewSet[uint64](),
|
||||||
// First we run through non-add entries in both logs, populating the
|
|
||||||
// skip sets.
|
|
||||||
for _, entry := range view.Updates.Local {
|
|
||||||
switch entry.EntryType {
|
|
||||||
// Skip adds for now. They will be processed below.
|
|
||||||
case Add:
|
|
||||||
continue
|
|
||||||
|
|
||||||
// Skip fee updates because we've already dealt with them above.
|
|
||||||
case FeeUpdate:
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
addEntry, err := lc.fetchParent(
|
|
||||||
entry, whoseCommitChain, lntypes.Remote,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, lntypes.Dual[[]*paymentDescriptor]{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
skipThem.Add(addEntry.HtlcIndex)
|
|
||||||
|
|
||||||
rmvHeight := entry.removeCommitHeights.GetForParty(
|
|
||||||
whoseCommitChain,
|
|
||||||
)
|
|
||||||
if rmvHeight == 0 {
|
|
||||||
processRemoveEntry(
|
|
||||||
entry, ourBalance, theirBalance,
|
|
||||||
lntypes.Remote,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do the same for our peer's updates.
|
parties := [2]lntypes.ChannelParty{lntypes.Local, lntypes.Remote}
|
||||||
for _, entry := range view.Updates.Remote {
|
for _, party := range parties {
|
||||||
switch entry.EntryType {
|
// First we run through non-add entries in both logs,
|
||||||
// Skip adds for now. They will be processed below.
|
// populating the skip sets.
|
||||||
case Add:
|
for _, entry := range view.Updates.GetForParty(party) {
|
||||||
continue
|
switch entry.EntryType {
|
||||||
|
// Skip adds for now. They will be processed below.
|
||||||
|
case Add:
|
||||||
|
continue
|
||||||
|
|
||||||
// Skip fee updates because we've already dealt with them above.
|
// Skip fee updates because we've already dealt with
|
||||||
case FeeUpdate:
|
// them above.
|
||||||
continue
|
case FeeUpdate:
|
||||||
}
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
addEntry, err := lc.fetchParent(
|
addEntry, err := lc.fetchParent(
|
||||||
entry, whoseCommitChain, lntypes.Local,
|
entry, whoseCommitChain, party.CounterParty(),
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, lntypes.Dual[[]*paymentDescriptor]{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
skipUs.Add(addEntry.HtlcIndex)
|
|
||||||
|
|
||||||
rmvHeight := entry.removeCommitHeights.GetForParty(
|
|
||||||
whoseCommitChain,
|
|
||||||
)
|
|
||||||
if rmvHeight == 0 {
|
|
||||||
processRemoveEntry(
|
|
||||||
entry, ourBalance, theirBalance, lntypes.Local,
|
|
||||||
)
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, noUncommitted, err
|
||||||
|
}
|
||||||
|
|
||||||
|
skipSet := skip.GetForParty(party.CounterParty())
|
||||||
|
skipSet.Add(addEntry.HtlcIndex)
|
||||||
|
|
||||||
|
rmvHeight := entry.removeCommitHeights.GetForParty(
|
||||||
|
whoseCommitChain,
|
||||||
|
)
|
||||||
|
if rmvHeight == 0 {
|
||||||
|
processRemoveEntry(
|
||||||
|
entry, ourBalance, theirBalance,
|
||||||
|
party.CounterParty(),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next we take a second pass through all the log entries, skipping any
|
// Next we take a second pass through all the log entries, skipping any
|
||||||
// settled HTLCs, and debiting the chain state balance due to any newly
|
// settled HTLCs, and debiting the chain state balance due to any newly
|
||||||
// added HTLCs.
|
// added HTLCs.
|
||||||
for _, entry := range view.Updates.Local {
|
for _, party := range parties {
|
||||||
isAdd := entry.EntryType == Add
|
for _, entry := range view.Updates.GetForParty(party) {
|
||||||
if skipUs.Contains(entry.HtlcIndex) || !isAdd {
|
isAdd := entry.EntryType == Add
|
||||||
continue
|
skipSet := skip.GetForParty(party)
|
||||||
}
|
if skipSet.Contains(entry.HtlcIndex) || !isAdd {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Skip the entries that have already had their add commit
|
// Skip the entries that have already had their add
|
||||||
// height set for this commit chain.
|
// commit height set for this commit chain.
|
||||||
addHeight := entry.addCommitHeights.GetForParty(
|
addHeight := entry.addCommitHeights.GetForParty(
|
||||||
whoseCommitChain,
|
whoseCommitChain,
|
||||||
)
|
)
|
||||||
if addHeight == 0 {
|
if addHeight == 0 {
|
||||||
processAddEntry(
|
processAddEntry(
|
||||||
entry, ourBalance, theirBalance, lntypes.Local,
|
entry, ourBalance, theirBalance, party,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
prevUpdates := newView.Updates.GetForParty(party)
|
||||||
|
newView.Updates.SetForParty(
|
||||||
|
party, append(prevUpdates, entry),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
newView.Updates.Local = append(newView.Updates.Local, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Again, we do the same for our peer's updates.
|
|
||||||
for _, entry := range view.Updates.Remote {
|
|
||||||
isAdd := entry.EntryType == Add
|
|
||||||
if skipThem.Contains(entry.HtlcIndex) || !isAdd {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip the entries that have already had their add commit
|
|
||||||
// height set for this commit chain.
|
|
||||||
addHeight := entry.addCommitHeights.GetForParty(
|
|
||||||
whoseCommitChain,
|
|
||||||
)
|
|
||||||
if addHeight == 0 {
|
|
||||||
processAddEntry(
|
|
||||||
entry, ourBalance, theirBalance, lntypes.Remote,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
newView.Updates.Remote = append(newView.Updates.Remote, entry)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a function that is capable of identifying whether or not the
|
// Create a function that is capable of identifying whether or not the
|
||||||
|
|||||||
Reference in new issue
Block a user