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:
2 files changed
+64
-35
No files matched your search
+42
-32
@@ -2927,10 +2927,26 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
|
|||||||
|
|
||||||
// Process fee updates, updating the current feePerKw.
|
// Process fee updates, updating the current feePerKw.
|
||||||
case FeeUpdate:
|
case FeeUpdate:
|
||||||
processFeeUpdate(
|
h := entry.addCommitHeights.GetForParty(
|
||||||
entry, nextHeight, whoseCommitChain,
|
whoseCommitChain,
|
||||||
mutateState, newView,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if h == 0 {
|
||||||
|
processFeeUpdate(
|
||||||
|
entry, &newView.FeePerKw, nextHeight,
|
||||||
|
whoseCommitChain,
|
||||||
|
)
|
||||||
|
|
||||||
|
if mutateState {
|
||||||
|
entry.addCommitHeights.SetForParty(
|
||||||
|
whoseCommitChain, nextHeight,
|
||||||
|
)
|
||||||
|
|
||||||
|
entry.removeCommitHeights.SetForParty(
|
||||||
|
whoseCommitChain, nextHeight,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2975,10 +2991,26 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
|
|||||||
|
|
||||||
// Process fee updates, updating the current feePerKw.
|
// Process fee updates, updating the current feePerKw.
|
||||||
case FeeUpdate:
|
case FeeUpdate:
|
||||||
processFeeUpdate(
|
h := entry.addCommitHeights.GetForParty(
|
||||||
entry, nextHeight, whoseCommitChain,
|
whoseCommitChain,
|
||||||
mutateState, newView,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if h == 0 {
|
||||||
|
processFeeUpdate(
|
||||||
|
entry, &newView.FeePerKw, nextHeight,
|
||||||
|
whoseCommitChain,
|
||||||
|
)
|
||||||
|
|
||||||
|
if mutateState {
|
||||||
|
entry.addCommitHeights.SetForParty(
|
||||||
|
whoseCommitChain, nextHeight,
|
||||||
|
)
|
||||||
|
|
||||||
|
entry.removeCommitHeights.SetForParty(
|
||||||
|
whoseCommitChain, nextHeight,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3176,35 +3208,13 @@ func processRemoveEntry(htlc *paymentDescriptor, ourBalance,
|
|||||||
|
|
||||||
// processFeeUpdate processes a log update that updates the current commitment
|
// processFeeUpdate processes a log update that updates the current commitment
|
||||||
// fee.
|
// fee.
|
||||||
func processFeeUpdate(feeUpdate *paymentDescriptor, nextHeight uint64,
|
func processFeeUpdate(feeUpdate *paymentDescriptor,
|
||||||
whoseCommitChain lntypes.ChannelParty, mutateState bool,
|
feeRef *chainfee.SatPerKWeight, nextHeight uint64,
|
||||||
view *HtlcView) {
|
whoseCommitChain lntypes.ChannelParty) {
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the update wasn't already locked in, update the current fee rate
|
// If the update wasn't already locked in, update the current fee rate
|
||||||
// to reflect this update.
|
// to reflect this update.
|
||||||
view.FeePerKw = chainfee.SatPerKWeight(feeUpdate.Amount.ToSatoshis())
|
*feeRef = chainfee.SatPerKWeight(feeUpdate.Amount.ToSatoshis())
|
||||||
|
|
||||||
if mutateState {
|
|
||||||
*addHeight = nextHeight
|
|
||||||
*removeHeight = nextHeight
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateRemoteHtlcSigJobs generates a series of HTLC signature jobs for the
|
// generateRemoteHtlcSigJobs generates a series of HTLC signature jobs for the
|
||||||
|
|||||||
@@ -9204,11 +9204,30 @@ func TestProcessFeeUpdate(t *testing.T) {
|
|||||||
view := &HtlcView{
|
view := &HtlcView{
|
||||||
FeePerKw: chainfee.SatPerKWeight(feePerKw),
|
FeePerKw: chainfee.SatPerKWeight(feePerKw),
|
||||||
}
|
}
|
||||||
processFeeUpdate(
|
|
||||||
update, nextHeight, test.whoseCommitChain,
|
h := update.addCommitHeights.GetForParty(
|
||||||
test.mutate, view,
|
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 {
|
if view.FeePerKw != test.expectedFee {
|
||||||
t.Fatalf("expected fee: %v, got: %v",
|
t.Fatalf("expected fee: %v, got: %v",
|
||||||
test.expectedFee, feePerKw)
|
test.expectedFee, feePerKw)
|
||||||
|
|||||||
Reference in new issue
Block a user