Skip to content

Commit 7381f4b

Browse files
authored
Merge pull request lightningnetwork#9687 from GeorgeTsagk/aux-trff-shpr-htlcview
`AuxTrafficShaper.PaymentBandwidth` uses HTLC view
2 parents 06f1ef4 + d0ef248 commit 7381f4b

File tree

6 files changed

+69
-15
lines changed

6 files changed

+69
-15
lines changed

htlcswitch/interfaces.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/lightningnetwork/lnd/graph/db/models"
1111
"github.com/lightningnetwork/lnd/invoices"
1212
"github.com/lightningnetwork/lnd/lntypes"
13+
"github.com/lightningnetwork/lnd/lnwallet"
1314
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
1415
"github.com/lightningnetwork/lnd/lnwire"
1516
"github.com/lightningnetwork/lnd/record"
@@ -516,8 +517,8 @@ type AuxTrafficShaper interface {
516517
// is a custom channel that should be handled by the traffic shaper, the
517518
// ShouldHandleTraffic method should be called first.
518519
PaymentBandwidth(htlcBlob, commitmentBlob fn.Option[tlv.Blob],
519-
linkBandwidth,
520-
htlcAmt lnwire.MilliSatoshi) (lnwire.MilliSatoshi, error)
520+
linkBandwidth, htlcAmt lnwire.MilliSatoshi,
521+
htlcView lnwallet.AuxHtlcView) (lnwire.MilliSatoshi, error)
521522

522523
// IsCustomHTLC returns true if the HTLC carries the set of relevant
523524
// custom records to put it under the purview of the traffic shaper,

htlcswitch/link.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,6 +3505,7 @@ func (l *channelLink) AuxBandwidth(amount lnwire.MilliSatoshi,
35053505
commitmentBlob := l.CommitmentCustomBlob()
35063506
auxBandwidth, err := ts.PaymentBandwidth(
35073507
htlcBlob, commitmentBlob, l.Bandwidth(), amount,
3508+
l.channel.FetchLatestAuxHTLCView(),
35083509
)
35093510
if err != nil {
35103511
return fn.Err[OptionalBandwidth](fmt.Errorf("failed to get "+

lnwallet/aux_leaf_store.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ type CommitDiffAuxInput struct {
133133
// UnfilteredView is the unfiltered, original HTLC view of the channel.
134134
// Unfiltered in this context means that the view contains all HTLCs,
135135
// including the canceled ones.
136-
UnfilteredView *HtlcView
136+
UnfilteredView AuxHtlcView
137137

138138
// WhoseCommit denotes whose commitment transaction we are computing the
139139
// diff for.
@@ -177,9 +177,8 @@ type AuxLeafStore interface {
177177
// correspond to the passed aux blob, and an existing channel
178178
// commitment.
179179
FetchLeavesFromCommit(chanState AuxChanState,
180-
commit channeldb.ChannelCommitment,
181-
keyRing CommitmentKeyRing, whoseCommit lntypes.ChannelParty,
182-
) fn.Result[CommitDiffAuxResult]
180+
commit channeldb.ChannelCommitment, keyRing CommitmentKeyRing,
181+
whoseCommit lntypes.ChannelParty) fn.Result[CommitDiffAuxResult]
183182

184183
// FetchLeavesFromRevocation attempts to fetch the auxiliary leaves
185184
// from a channel revocation that stores balance + blob information.
@@ -206,7 +205,7 @@ func auxLeavesFromView(leafStore AuxLeafStore, chanState *channeldb.OpenChannel,
206205
return leafStore.FetchLeavesFromView(CommitDiffAuxInput{
207206
ChannelState: NewAuxChanState(chanState),
208207
PrevBlob: blob,
209-
UnfilteredView: originalView,
208+
UnfilteredView: newAuxHtlcView(originalView),
210209
WhoseCommit: whoseCommit,
211210
OurBalance: ourBalance,
212211
TheirBalance: theirBalance,
@@ -227,13 +226,15 @@ func updateAuxBlob(leafStore AuxLeafStore, chanState *channeldb.OpenChannel,
227226
return fn.MapOptionZ(
228227
prevBlob, func(blob tlv.Blob) fn.Result[fn.Option[tlv.Blob]] {
229228
return leafStore.ApplyHtlcView(CommitDiffAuxInput{
230-
ChannelState: NewAuxChanState(chanState),
231-
PrevBlob: blob,
232-
UnfilteredView: nextViewUnfiltered,
233-
WhoseCommit: whoseCommit,
234-
OurBalance: ourBalance,
235-
TheirBalance: theirBalance,
236-
KeyRing: keyRing,
229+
ChannelState: NewAuxChanState(chanState),
230+
PrevBlob: blob,
231+
UnfilteredView: newAuxHtlcView(
232+
nextViewUnfiltered,
233+
),
234+
WhoseCommit: whoseCommit,
235+
OurBalance: ourBalance,
236+
TheirBalance: theirBalance,
237+
KeyRing: keyRing,
237238
})
238239
},
239240
)

lnwallet/aux_signer.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/lightningnetwork/lnd/fn/v2"
66
"github.com/lightningnetwork/lnd/input"
77
"github.com/lightningnetwork/lnd/lntypes"
8+
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
89
"github.com/lightningnetwork/lnd/lnwire"
910
"github.com/lightningnetwork/lnd/tlv"
1011
)
@@ -13,6 +14,37 @@ import (
1314
// signatures within the custom data for an existing HTLC.
1415
var htlcCustomSigType tlv.TlvType65543
1516

17+
// AuxHtlcView is a struct that contains a safe copy of an HTLC view that can
18+
// be used by aux components.
19+
type AuxHtlcView struct {
20+
// NextHeight is the height of the commitment transaction that will be
21+
// created using this view.
22+
NextHeight uint64
23+
24+
// Updates is a Dual of the Local and Remote HTLCs.
25+
Updates lntypes.Dual[[]AuxHtlcDescriptor]
26+
27+
// FeePerKw is the fee rate in sat/kw of the commitment transaction.
28+
FeePerKw chainfee.SatPerKWeight
29+
}
30+
31+
// newAuxHtlcView creates a new safe copy of the HTLC view that can be used by
32+
// aux components.
33+
//
34+
// NOTE: This function should only be called while holding the channel's read
35+
// lock, since the underlying local/remote payment descriptors are accessed
36+
// directly.
37+
func newAuxHtlcView(v *HtlcView) AuxHtlcView {
38+
return AuxHtlcView{
39+
NextHeight: v.NextHeight,
40+
Updates: lntypes.Dual[[]AuxHtlcDescriptor]{
41+
Local: fn.Map(v.Updates.Local, newAuxHtlcDescriptor),
42+
Remote: fn.Map(v.Updates.Remote, newAuxHtlcDescriptor),
43+
},
44+
FeePerKw: v.FeePerKw,
45+
}
46+
}
47+
1648
// AuxHtlcDescriptor is a struct that contains the information needed to sign or
1749
// verify an HTLC for custom channels.
1850
type AuxHtlcDescriptor struct {
@@ -99,6 +131,9 @@ func (a *AuxHtlcDescriptor) RemoveHeight(
99131

100132
// newAuxHtlcDescriptor creates a new AuxHtlcDescriptor from a payment
101133
// descriptor.
134+
//
135+
// NOTE: This function should only be called while holding the channel's read
136+
// lock, since the underlying payment descriptors are accessed directly.
102137
func newAuxHtlcDescriptor(p *paymentDescriptor) AuxHtlcDescriptor {
103138
return AuxHtlcDescriptor{
104139
ChanID: p.ChanID,

lnwallet/channel.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,6 +2704,20 @@ func (v *HtlcView) AuxTheirUpdates() []AuxHtlcDescriptor {
27042704
return fn.Map(v.Updates.Remote, newAuxHtlcDescriptor)
27052705
}
27062706

2707+
// FetchLatestAuxHTLCView returns the latest HTLC view of the lightning channel
2708+
// as a safe copy that can be used outside the wallet code in concurrent access.
2709+
func (lc *LightningChannel) FetchLatestAuxHTLCView() AuxHtlcView {
2710+
// This read lock is important, because we access both the local and
2711+
// remote log indexes as well as the underlying payment descriptors of
2712+
// the HTLCs when creating the view.
2713+
lc.RLock()
2714+
defer lc.RUnlock()
2715+
2716+
return newAuxHtlcView(lc.fetchHTLCView(
2717+
lc.updateLogs.Remote.logIndex, lc.updateLogs.Local.logIndex,
2718+
))
2719+
}
2720+
27072721
// fetchHTLCView returns all the candidate HTLC updates which should be
27082722
// considered for inclusion within a commitment based on the passed HTLC log
27092723
// indexes.

routing/bandwidth_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/go-errors/errors"
88
"github.com/lightningnetwork/lnd/fn/v2"
99
"github.com/lightningnetwork/lnd/htlcswitch"
10+
"github.com/lightningnetwork/lnd/lnwallet"
1011
"github.com/lightningnetwork/lnd/lnwire"
1112
"github.com/lightningnetwork/lnd/tlv"
1213
"github.com/stretchr/testify/require"
@@ -150,7 +151,8 @@ func (*mockTrafficShaper) ShouldHandleTraffic(_ lnwire.ShortChannelID,
150151
// is a custom channel that should be handled by the traffic shaper, the
151152
// HandleTraffic method should be called first.
152153
func (*mockTrafficShaper) PaymentBandwidth(_, _ fn.Option[tlv.Blob],
153-
linkBandwidth, _ lnwire.MilliSatoshi) (lnwire.MilliSatoshi, error) {
154+
linkBandwidth, _ lnwire.MilliSatoshi,
155+
_ lnwallet.AuxHtlcView) (lnwire.MilliSatoshi, error) {
154156

155157
return linkBandwidth, nil
156158
}

0 commit comments

Comments
 (0)