@@ -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,
111112func (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,12 @@ 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 (
184+ commitment , htlcView ,
185+ )
186+ if err != nil {
187+ return 0 , err
188+ }
183189
184190 // If the HTLC carries asset units (keysend, forwarding), then there's
185191 // no need to do any RFQ related math. We can directly compare the asset
@@ -304,12 +310,34 @@ func (s *AuxTrafficShaper) paymentBandwidthRfqMath(htlc *rfqmsg.Htlc,
304310
305311// ComputeLocalBalance combines the given commitment state with the HtlcView to
306312// produce the available local balance with accuracy.
307- func ComputeLocalBalance (commitment * cmsg.Commitment ) uint64 {
308- // Let's get the current local asset balance of the channel as reported
309- // by the latest commitment.
313+ func ComputeLocalBalance (commitment * cmsg.Commitment ,
314+ htlcView lnwallet.AuxHtlcView ) (uint64 , * DecodedView , error ) {
315+
316+ // Set the htlcView next height to 0. This is needed because the
317+ // following helper `ComputeLocalBalance` will use that height to add or
318+ // remove HTLCs that have a matching addHeight. The HTLCs that are not
319+ // yet part of the commitment have an addHeight of 0, so that's the
320+ // height we want to filter by here.
321+ htlcView .NextHeight = 0
322+
323+ // Let's get the current local and remote asset balances of the channel
324+ // as reported by the latest commitment.
310325 localBalance := cmsg .OutputSum (commitment .LocalOutputs ())
326+ remoteBalance := cmsg .OutputSum (commitment .RemoteOutputs ())
327+
328+ // With the help of the latest HtlcView, let's calculate a more precise
329+ // local balance. This is useful in order to not forward HTLCs that may
330+ // never be settled. Other HTLCs that may also call into this method are
331+ // not yet registered to the commitment, so we need to account for them
332+ // manually.
333+ computedLocal , _ , decodedView , _ , err := ComputeView (
334+ localBalance , remoteBalance , lntypes .Local , htlcView ,
335+ )
336+ if err != nil {
337+ return 0 , nil , err
338+ }
311339
312- return localBalance
340+ return computedLocal , decodedView , nil
313341}
314342
315343// ProduceHtlcExtraData is a function that, based on the previous custom record
0 commit comments