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
This commit is contained in:
Keagan McClelland committed 2024-10-14 09:33:38 -06:00
1 parent 4b2a4e36ad
commit 51c28496a4
4 files changed
+23 -31

No files matched your search

+1 -1
View File
@@ -35,7 +35,7 @@ require (
github.com/lightningnetwork/lightning-onion v1.2.1-0.20240712235311-98bd56499dfb github.com/lightningnetwork/lightning-onion v1.2.1-0.20240712235311-98bd56499dfb
github.com/lightningnetwork/lnd/cert v1.2.2 github.com/lightningnetwork/lnd/cert v1.2.2
github.com/lightningnetwork/lnd/clock v1.1.1 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/healthcheck v1.2.5
github.com/lightningnetwork/lnd/kvdb v1.4.10 github.com/lightningnetwork/lnd/kvdb v1.4.10
github.com/lightningnetwork/lnd/queue v1.1.1 github.com/lightningnetwork/lnd/queue v1.1.1
+2 -2
View File
@@ -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/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 h1:OfR3/zcJd2RhH0RU+zX/77c0ZiOnIMsDIBjgjWdZgA0=
github.com/lightningnetwork/lnd/clock v1.1.1/go.mod h1:mGnAhPyjYZQJmebS7aevElXKTFDuO+uNFFfMXK1W8xQ= 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.2 h1:rVtmGW1cQTmYce2XdUbRcc5qLDxqu+aQ6IGRpyspakk=
github.com/lightningnetwork/lnd/fn v1.2.1/go.mod h1:SyFohpVrARPKH3XVAJZlXdVe+IwMYc4OMAvrDY32kw0= 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 h1:aTJy5xeBpcWgRtW/PGBDe+LMQEmNm/HQewlQx2jt7OA=
github.com/lightningnetwork/lnd/healthcheck v1.2.5/go.mod h1:G7Tst2tVvWo7cx6mSBEToQC5L1XOGxzZTPB29g9Rv2I= github.com/lightningnetwork/lnd/healthcheck v1.2.5/go.mod h1:G7Tst2tVvWo7cx6mSBEToQC5L1XOGxzZTPB29g9Rv2I=
github.com/lightningnetwork/lnd/kvdb v1.4.10 h1:vK89IVv1oVH9ubQWU+EmoCQFeVRaC8kfmOrqHbY5zoY= github.com/lightningnetwork/lnd/kvdb v1.4.10 h1:vK89IVv1oVH9ubQWU+EmoCQFeVRaC8kfmOrqHbY5zoY=
+15 -28
View File
@@ -2906,6 +2906,19 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
NextHeight: nextHeight, 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 // 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 // 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 // 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: case Add:
continue continue
// Process fee updates, updating the current feePerKw. // Skip fee updates because we've already dealt with them above.
case FeeUpdate: 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 continue
} }
@@ -2966,21 +2966,8 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance,
case Add: case Add:
continue continue
// Process fee updates, updating the current feePerKw. // Skip fee updates because we've already dealt with them above.
case FeeUpdate: 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 continue
} }
+5
View File
@@ -8650,6 +8650,7 @@ func TestEvaluateView(t *testing.T) {
name string name string
ourHtlcs []*paymentDescriptor ourHtlcs []*paymentDescriptor
theirHtlcs []*paymentDescriptor theirHtlcs []*paymentDescriptor
channelInitiator lntypes.ChannelParty
whoseCommitChain lntypes.ChannelParty whoseCommitChain lntypes.ChannelParty
mutateState bool mutateState bool
@@ -8679,6 +8680,7 @@ func TestEvaluateView(t *testing.T) {
}{ }{
{ {
name: "our fee update is applied", name: "our fee update is applied",
channelInitiator: lntypes.Local,
whoseCommitChain: lntypes.Local, whoseCommitChain: lntypes.Local,
mutateState: false, mutateState: false,
ourHtlcs: []*paymentDescriptor{ ourHtlcs: []*paymentDescriptor{
@@ -8696,6 +8698,7 @@ func TestEvaluateView(t *testing.T) {
}, },
{ {
name: "their fee update is applied", name: "their fee update is applied",
channelInitiator: lntypes.Remote,
whoseCommitChain: lntypes.Local, whoseCommitChain: lntypes.Local,
mutateState: false, mutateState: false,
ourHtlcs: []*paymentDescriptor{}, ourHtlcs: []*paymentDescriptor{},
@@ -8911,8 +8914,10 @@ func TestEvaluateView(t *testing.T) {
test := test test := test
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
isInitiator := test.channelInitiator == lntypes.Local
lc := LightningChannel{ lc := LightningChannel{
channelState: &channeldb.OpenChannel{ channelState: &channeldb.OpenChannel{
IsInitiator: isInitiator,
TotalMSatSent: 0, TotalMSatSent: 0,
TotalMSatReceived: 0, TotalMSatReceived: 0,
}, },