diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 13a421458..2157afe03 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -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 diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 997f0c22d..77630d57a 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -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)