Skip to content

Commit 3df87fe

Browse files
committed
tapchannel: PaymentBandwidth uses AuxHtlcView
We now make use of the AuxHtlcView to calculate a more precise value of our local balance. This is using the ComputeView helper in order to manually process HTLC adds from our side, providing us with the correct value of local balance. We also add the DecodedView as a return value of the helper function, but this will only be used in the next commit for verbose logging.
1 parent cd72533 commit 3df87fe

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

tapchannel/aux_traffic_shaper.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/lightninglabs/taproot-assets/rfqmsg"
1515
cmsg "github.com/lightninglabs/taproot-assets/tapchannelmsg"
1616
lfn "github.com/lightningnetwork/lnd/fn/v2"
17+
"github.com/lightningnetwork/lnd/lntypes"
1718
"github.com/lightningnetwork/lnd/lnwallet"
1819
"github.com/lightningnetwork/lnd/lnwire"
1920
"github.com/lightningnetwork/lnd/tlv"
@@ -111,7 +112,7 @@ func (s *AuxTrafficShaper) ShouldHandleTraffic(_ lnwire.ShortChannelID,
111112
func (s *AuxTrafficShaper) PaymentBandwidth(htlcBlob,
112113
commitmentBlob lfn.Option[tlv.Blob], linkBandwidth,
113114
htlcAmt lnwire.MilliSatoshi,
114-
_ lnwallet.AuxHtlcView) (lnwire.MilliSatoshi, error) {
115+
htlcView lnwallet.AuxHtlcView) (lnwire.MilliSatoshi, error) {
115116

116117
// If the commitment or HTLC blob is not set, we don't have any
117118
// information about the channel and cannot determine the available
@@ -179,7 +180,10 @@ func (s *AuxTrafficShaper) PaymentBandwidth(htlcBlob,
179180
// never be settled. Other HTLCs that may also call into this method are
180181
// not yet registered to the commitment, so we need to account for them
181182
// manually.
182-
computedLocal := ComputeLocalBalance(*commitment)
183+
computedLocal, _, err := ComputeLocalBalance(*commitment, htlcView)
184+
if err != nil {
185+
return 0, err
186+
}
183187

184188
// If the HTLC carries asset units (keysend, forwarding), then there's
185189
// no need to do any RFQ related math. We can directly compare the asset
@@ -305,12 +309,34 @@ func (s *AuxTrafficShaper) paymentBandwidth(htlc *rfqmsg.Htlc,
305309

306310
// ComputeLocalBalance combines the given commitment state with the HtlcView to
307311
// produce the available local balance with accuracy.
308-
func ComputeLocalBalance(commitment cmsg.Commitment) uint64 {
309-
// Let's get the current local asset balance of the channel as reported
310-
// by the latest commitment.
312+
func ComputeLocalBalance(commitment cmsg.Commitment,
313+
htlcView lnwallet.AuxHtlcView) (uint64, *DecodedView, error) {
314+
315+
// Set the htlcView next height to 0. This is needed because the
316+
// following helper `ComputeLocalBalance` will use that height to add or
317+
// remove HTLCs that have a matching addHeight. The HTLCs that are not
318+
// yet part of the commitment have an addHeight of 0, so that's the
319+
// height we want to filter by here.
320+
htlcView.NextHeight = 0
321+
322+
// Let's get the current local and remote asset balances of the channel
323+
// as reported by the latest commitment.
311324
localBalance := cmsg.OutputSum(commitment.LocalOutputs())
325+
remoteBalance := cmsg.OutputSum(commitment.RemoteOutputs())
326+
327+
// With the help of the latest HtlcView, let's calculate a more precise
328+
// local balance. This is useful in order to not forward HTLCs that may
329+
// never be settled. Other HTLCs that may also call into this method are
330+
// not yet registered to the commitment, so we need to account for them
331+
// manually.
332+
computedLocal, _, decodedView, _, err := ComputeView(
333+
localBalance, remoteBalance, lntypes.Local, htlcView,
334+
)
335+
if err != nil {
336+
return 0, nil, err
337+
}
312338

313-
return localBalance
339+
return computedLocal, decodedView, nil
314340
}
315341

316342
// ProduceHtlcExtraData is a function that, based on the previous custom record

0 commit comments

Comments
 (0)