Skip to content

Commit ea70103

Browse files
committed
lnwallet: inline and remove process[Add|Remove]Entry
This commit observes that processAddEntry and processRemoveEntry are only invoked at a single call-site. Here we inline them at their call-sites, which will unlock further simplifications of the code that will allow us to remove pointer mutations in favor of explicit expression oriented programming. We also delete the tests associated with these functions, the overall functionality is implicitly tested by the TestEvaluateHTLCView tests.
1 parent 6a8d640 commit ea70103

File tree

2 files changed

+51
-495
lines changed

2 files changed

+51
-495
lines changed

lnwallet/channel.go

+51-64
Original file line numberDiff line numberDiff line change
@@ -2957,10 +2957,44 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
29572957
rmvHeights := &entry.removeCommitHeights
29582958
rmvHeight := rmvHeights.GetForParty(whoseCommitChain)
29592959
if rmvHeight == 0 {
2960-
processRemoveEntry(
2961-
entry, ourBalance, theirBalance,
2962-
party.CounterParty(),
2963-
)
2960+
switch {
2961+
// If an incoming HTLC is being settled, then
2962+
// this means that we've received the preimage
2963+
// either from another subsystem, or the
2964+
// upstream peer in the route. Therefore, we
2965+
// increase our balance by the HTLC amount.
2966+
case party.CounterParty() == lntypes.Remote &&
2967+
entry.EntryType == Settle:
2968+
2969+
*ourBalance += entry.Amount
2970+
2971+
// Otherwise, this HTLC is being failed out,
2972+
// therefore the value of the HTLC should
2973+
// return to the remote party.
2974+
case party.CounterParty() == lntypes.Remote &&
2975+
entry.EntryType != Settle:
2976+
2977+
*theirBalance += entry.Amount
2978+
2979+
// If an outgoing HTLC is being settled, then
2980+
// this means that the downstream party
2981+
// resented the preimage or learned of it via a
2982+
// downstream peer. In either case, we credit
2983+
// their settled value with the value of the
2984+
// HTLC.
2985+
case party.CounterParty() == lntypes.Local &&
2986+
entry.EntryType == Settle:
2987+
2988+
*theirBalance += entry.Amount
2989+
2990+
// Otherwise, one of our outgoing HTLC's has
2991+
// timed out, so the value of the HTLC should
2992+
// be returned to our settled balance.
2993+
case party.CounterParty() == lntypes.Local &&
2994+
entry.EntryType != Settle:
2995+
2996+
*ourBalance += entry.Amount
2997+
}
29642998
}
29652999
}
29663000
}
@@ -2980,10 +3014,19 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
29803014
addHeights := &entry.addCommitHeights
29813015
addHeight := addHeights.GetForParty(whoseCommitChain)
29823016
if addHeight == 0 {
2983-
processAddEntry(
2984-
entry, ourBalance, theirBalance,
2985-
party,
2986-
)
3017+
if party == lntypes.Remote {
3018+
// If this is a new incoming
3019+
// (un-committed) HTLC, then we need
3020+
// to update their balance accordingly
3021+
// by subtracting the amount of the
3022+
// HTLC that are funds pending.
3023+
*theirBalance -= entry.Amount
3024+
} else {
3025+
// Similarly, we need to debit our
3026+
// balance if this is an out going HTLC
3027+
// to reflect the pending balance.
3028+
*ourBalance -= entry.Amount
3029+
}
29873030
}
29883031
}
29893032

@@ -3071,62 +3114,6 @@ func (lc *LightningChannel) fetchParent(entry *paymentDescriptor,
30713114
return addEntry, nil
30723115
}
30733116

3074-
// processAddEntry evaluates the effect of an add entry within the HTLC log.
3075-
// If the HTLC hasn't yet been committed in either chain, then the height it
3076-
// was committed is updated. Keeping track of this inclusion height allows us to
3077-
// later compact the log once the change is fully committed in both chains.
3078-
func processAddEntry(htlc *paymentDescriptor, ourBalance,
3079-
theirBalance *lnwire.MilliSatoshi, originator lntypes.ChannelParty) {
3080-
3081-
if originator == lntypes.Remote {
3082-
// If this is a new incoming (un-committed) HTLC, then we need
3083-
// to update their balance accordingly by subtracting the
3084-
// amount of the HTLC that are funds pending.
3085-
*theirBalance -= htlc.Amount
3086-
} else {
3087-
// Similarly, we need to debit our balance if this is an out
3088-
// going HTLC to reflect the pending balance.
3089-
*ourBalance -= htlc.Amount
3090-
}
3091-
}
3092-
3093-
// processRemoveEntry processes a log entry which settles or times out a
3094-
// previously added HTLC. If the removal entry has already been processed, it
3095-
// is skipped.
3096-
func processRemoveEntry(htlc *paymentDescriptor, ourBalance,
3097-
theirBalance *lnwire.MilliSatoshi, originator lntypes.ChannelParty) {
3098-
3099-
switch {
3100-
// If an incoming HTLC is being settled, then this means that we've
3101-
// received the preimage either from another subsystem, or the
3102-
// upstream peer in the route. Therefore, we increase our balance by
3103-
// the HTLC amount.
3104-
case originator == lntypes.Remote && htlc.EntryType == Settle:
3105-
*ourBalance += htlc.Amount
3106-
3107-
// Otherwise, this HTLC is being failed out, therefore the value of the
3108-
// HTLC should return to the remote party.
3109-
case originator == lntypes.Remote &&
3110-
(htlc.EntryType == Fail || htlc.EntryType == MalformedFail):
3111-
3112-
*theirBalance += htlc.Amount
3113-
3114-
// If an outgoing HTLC is being settled, then this means that the
3115-
// downstream party resented the preimage or learned of it via a
3116-
// downstream peer. In either case, we credit their settled value with
3117-
// the value of the HTLC.
3118-
case originator == lntypes.Local && htlc.EntryType == Settle:
3119-
*theirBalance += htlc.Amount
3120-
3121-
// Otherwise, one of our outgoing HTLC's has timed out, so the value of
3122-
// the HTLC should be returned to our settled balance.
3123-
case originator == lntypes.Local &&
3124-
(htlc.EntryType == Fail || htlc.EntryType == MalformedFail):
3125-
3126-
*ourBalance += htlc.Amount
3127-
}
3128-
}
3129-
31303117
// generateRemoteHtlcSigJobs generates a series of HTLC signature jobs for the
31313118
// sig pool, along with a channel that if closed, will cancel any jobs after
31323119
// they have been submitted to the sigPool. This method is to be used when

0 commit comments

Comments
 (0)