lnwallet: bring processFeeUpdate in line with process[Add|Remove]Entry

This commit redoes the API and semantics of processFeeUpdate to make
it consistent with the semantics of it's sister functions. This is
part of an ongoing series of commits to remove mutateState arguments
pervasively from the codebase.

As with the previous commit this makes state mutation the caller's
responsibility. This temporarily increases code duplication at the
call-sites, but this will unlock other refactor opportunities.
This commit is contained in:
Keagan McClelland committed 2024-10-11 11:25:58 +02:00
1 parent 49add0f57b
commit 05347c8392
2 files changed
+64 -35

No files matched your search

+42 -32
View File
@@ -2927,10 +2927,26 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
// Process fee updates, updating the current feePerKw.
case FeeUpdate:
processFeeUpdate(
entry, nextHeight, whoseCommitChain,
mutateState, newView,
h := entry.addCommitHeights.GetForParty(
whoseCommitChain,
)
if h == 0 {
processFeeUpdate(
entry, &newView.FeePerKw, nextHeight,
whoseCommitChain,
)
if mutateState {
entry.addCommitHeights.SetForParty(
whoseCommitChain, nextHeight,
)
entry.removeCommitHeights.SetForParty(
whoseCommitChain, nextHeight,
)
}
}
continue
}
@@ -2975,10 +2991,26 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
// Process fee updates, updating the current feePerKw.
case FeeUpdate:
processFeeUpdate(
entry, nextHeight, whoseCommitChain,
mutateState, newView,
h := entry.addCommitHeights.GetForParty(
whoseCommitChain,
)
if h == 0 {
processFeeUpdate(
entry, &newView.FeePerKw, nextHeight,
whoseCommitChain,
)
if mutateState {
entry.addCommitHeights.SetForParty(
whoseCommitChain, nextHeight,
)
entry.removeCommitHeights.SetForParty(
whoseCommitChain, nextHeight,
)
}
}
continue
}
@@ -3176,35 +3208,13 @@ func processRemoveEntry(htlc *paymentDescriptor, ourBalance,
// processFeeUpdate processes a log update that updates the current commitment
// fee.
func processFeeUpdate(feeUpdate *paymentDescriptor, nextHeight uint64,
whoseCommitChain lntypes.ChannelParty, mutateState bool,
view *HtlcView) {
// Fee updates are applied for all commitments after they are
// sent/received, so we consider them being added and removed at the
// same height.
var addHeight *uint64
var removeHeight *uint64
if whoseCommitChain.IsRemote() {
addHeight = &feeUpdate.addCommitHeights.Remote
removeHeight = &feeUpdate.removeCommitHeights.Remote
} else {
addHeight = &feeUpdate.addCommitHeights.Local
removeHeight = &feeUpdate.removeCommitHeights.Local
}
if *addHeight != 0 {
return
}
func processFeeUpdate(feeUpdate *paymentDescriptor,
feeRef *chainfee.SatPerKWeight, nextHeight uint64,
whoseCommitChain lntypes.ChannelParty) {
// If the update wasn't already locked in, update the current fee rate
// to reflect this update.
view.FeePerKw = chainfee.SatPerKWeight(feeUpdate.Amount.ToSatoshis())
if mutateState {
*addHeight = nextHeight
*removeHeight = nextHeight
}
*feeRef = chainfee.SatPerKWeight(feeUpdate.Amount.ToSatoshis())
}
// generateRemoteHtlcSigJobs generates a series of HTLC signature jobs for the
+22 -3
View File
@@ -9204,11 +9204,30 @@ func TestProcessFeeUpdate(t *testing.T) {
view := &HtlcView{
FeePerKw: chainfee.SatPerKWeight(feePerKw),
}
processFeeUpdate(
update, nextHeight, test.whoseCommitChain,
test.mutate, view,
h := update.addCommitHeights.GetForParty(
test.whoseCommitChain,
)
if h == 0 {
processFeeUpdate(
update, &view.FeePerKw, nextHeight,
test.whoseCommitChain,
)
if test.mutate {
update.addCommitHeights.SetForParty(
test.whoseCommitChain,
nextHeight,
)
update.removeCommitHeights.SetForParty(
test.whoseCommitChain,
nextHeight,
)
}
}
if view.FeePerKw != test.expectedFee {
t.Fatalf("expected fee: %v, got: %v",
test.expectedFee, feePerKw)