From 51c28496a4c27d08e599a4482052046e7ce273a1 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Wed, 24 Jul 2024 15:52:41 -0700 Subject: [PATCH] lnwallet: simplify fee calculation in evaluateHTLCView This commit simplifies how we compute the commitment fee rate based off of the live updates. Prior to this commit we processed all of the FeeUpdate paymentDescriptors of both ChannelParty's. Now we only process the last FeeUpdate of the OpeningParty --- go.mod | 2 +- go.sum | 4 ++-- lnwallet/channel.go | 43 ++++++++++++++-------------------------- lnwallet/channel_test.go | 5 +++++ 4 files changed, 23 insertions(+), 31 deletions(-) diff --git a/go.mod b/go.mod index 60af5fb3d..3a559abb6 100644 --- a/go.mod +++ b/go.mod @@ -35,7 +35,7 @@ require ( github.com/lightningnetwork/lightning-onion v1.2.1-0.20240712235311-98bd56499dfb github.com/lightningnetwork/lnd/cert v1.2.2 github.com/lightningnetwork/lnd/clock v1.1.1 - github.com/lightningnetwork/lnd/fn v1.2.1 + github.com/lightningnetwork/lnd/fn v1.2.2 github.com/lightningnetwork/lnd/healthcheck v1.2.5 github.com/lightningnetwork/lnd/kvdb v1.4.10 github.com/lightningnetwork/lnd/queue v1.1.1 diff --git a/go.sum b/go.sum index 312b22578..ea51f3e95 100644 --- a/go.sum +++ b/go.sum @@ -453,8 +453,8 @@ github.com/lightningnetwork/lnd/cert v1.2.2 h1:71YK6hogeJtxSxw2teq3eGeuy4rHGKcFf github.com/lightningnetwork/lnd/cert v1.2.2/go.mod h1:jQmFn/Ez4zhDgq2hnYSw8r35bqGVxViXhX6Cd7HXM6U= github.com/lightningnetwork/lnd/clock v1.1.1 h1:OfR3/zcJd2RhH0RU+zX/77c0ZiOnIMsDIBjgjWdZgA0= github.com/lightningnetwork/lnd/clock v1.1.1/go.mod h1:mGnAhPyjYZQJmebS7aevElXKTFDuO+uNFFfMXK1W8xQ= -github.com/lightningnetwork/lnd/fn v1.2.1 h1:pPsVGrwi9QBwdLJzaEGK33wmiVKOxs/zc8H7+MamFf0= -github.com/lightningnetwork/lnd/fn v1.2.1/go.mod h1:SyFohpVrARPKH3XVAJZlXdVe+IwMYc4OMAvrDY32kw0= +github.com/lightningnetwork/lnd/fn v1.2.2 h1:rVtmGW1cQTmYce2XdUbRcc5qLDxqu+aQ6IGRpyspakk= +github.com/lightningnetwork/lnd/fn v1.2.2/go.mod h1:SyFohpVrARPKH3XVAJZlXdVe+IwMYc4OMAvrDY32kw0= github.com/lightningnetwork/lnd/healthcheck v1.2.5 h1:aTJy5xeBpcWgRtW/PGBDe+LMQEmNm/HQewlQx2jt7OA= github.com/lightningnetwork/lnd/healthcheck v1.2.5/go.mod h1:G7Tst2tVvWo7cx6mSBEToQC5L1XOGxzZTPB29g9Rv2I= github.com/lightningnetwork/lnd/kvdb v1.4.10 h1:vK89IVv1oVH9ubQWU+EmoCQFeVRaC8kfmOrqHbY5zoY= diff --git a/lnwallet/channel.go b/lnwallet/channel.go index b66b14780..906c37e79 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -2906,6 +2906,19 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance, NextHeight: nextHeight, } + // The fee rate of our view is always the last UpdateFee message from + // the channel's OpeningParty. + openerUpdates := view.Updates.GetForParty(lc.channelState.Initiator()) + feeUpdates := fn.Filter(func(u *paymentDescriptor) bool { + return u.EntryType == FeeUpdate + }, openerUpdates) + lastFeeUpdate := fn.Last(feeUpdates) + lastFeeUpdate.WhenSome(func(pd *paymentDescriptor) { + newView.FeePerKw = chainfee.SatPerKWeight( + pd.Amount.ToSatoshis(), + ) + }) + // We use two maps, one for the local log and one for the remote log to // 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 @@ -2921,21 +2934,8 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance, case Add: continue - // Process fee updates, updating the current feePerKw. + // Skip fee updates because we've already dealt with them above. case FeeUpdate: - h := entry.addCommitHeights.GetForParty( - whoseCommitChain, - ) - - if h == 0 { - // If the update wasn't already locked in, - // update the current fee rate to reflect this - // update. - newView.FeePerKw = chainfee.SatPerKWeight( - entry.Amount.ToSatoshis(), - ) - } - continue } @@ -2966,21 +2966,8 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance, case Add: continue - // Process fee updates, updating the current feePerKw. + // Skip fee updates because we've already dealt with them above. case FeeUpdate: - h := entry.addCommitHeights.GetForParty( - whoseCommitChain, - ) - - if h == 0 { - // If the update wasn't already locked in, - // update the current fee rate to reflect this - // update. - newView.FeePerKw = chainfee.SatPerKWeight( - entry.Amount.ToSatoshis(), - ) - } - continue } diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index dd352dab1..62eb01e3e 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -8650,6 +8650,7 @@ func TestEvaluateView(t *testing.T) { name string ourHtlcs []*paymentDescriptor theirHtlcs []*paymentDescriptor + channelInitiator lntypes.ChannelParty whoseCommitChain lntypes.ChannelParty mutateState bool @@ -8679,6 +8680,7 @@ func TestEvaluateView(t *testing.T) { }{ { name: "our fee update is applied", + channelInitiator: lntypes.Local, whoseCommitChain: lntypes.Local, mutateState: false, ourHtlcs: []*paymentDescriptor{ @@ -8696,6 +8698,7 @@ func TestEvaluateView(t *testing.T) { }, { name: "their fee update is applied", + channelInitiator: lntypes.Remote, whoseCommitChain: lntypes.Local, mutateState: false, ourHtlcs: []*paymentDescriptor{}, @@ -8911,8 +8914,10 @@ func TestEvaluateView(t *testing.T) { test := test t.Run(test.name, func(t *testing.T) { + isInitiator := test.channelInitiator == lntypes.Local lc := LightningChannel{ channelState: &channeldb.OpenChannel{ + IsInitiator: isInitiator, TotalMSatSent: 0, TotalMSatReceived: 0, },