• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12187193240

05 Dec 2024 08:04PM UTC coverage: 58.933% (-0.02%) from 58.951%
12187193240

push

github

web-flow
Merge pull request #9333 from guggero/aux-traffic-shaper-refactor

[custom channels]: refactor AuxTrafficManager to be used for forwarding as well

30 of 81 new or added lines in 7 files covered. (37.04%)

57 existing lines in 14 files now uncovered.

133458 of 226459 relevant lines covered (58.93%)

19547.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.46
/routing/bandwidth.go
1
package routing
2

3
import (
4
        "fmt"
5

6
        "github.com/lightningnetwork/lnd/fn"
7
        graphdb "github.com/lightningnetwork/lnd/graph/db"
8
        "github.com/lightningnetwork/lnd/htlcswitch"
9
        "github.com/lightningnetwork/lnd/lnwire"
10
        "github.com/lightningnetwork/lnd/routing/route"
11
        "github.com/lightningnetwork/lnd/tlv"
12
)
13

14
// bandwidthHints provides hints about the currently available balance in our
15
// channels.
16
type bandwidthHints interface {
17
        // availableChanBandwidth returns the total available bandwidth for a
18
        // channel and a bool indicating whether the channel hint was found.
19
        // The amount parameter is used to validate the outgoing htlc amount
20
        // that we wish to add to the channel against its flow restrictions. If
21
        // a zero amount is provided, the minimum htlc value for the channel
22
        // will be used. If the channel is unavailable, a zero amount is
23
        // returned.
24
        availableChanBandwidth(channelID uint64,
25
                amount lnwire.MilliSatoshi) (lnwire.MilliSatoshi, bool)
26

27
        // firstHopCustomBlob returns the custom blob for the first hop of the
28
        // payment, if available.
29
        firstHopCustomBlob() fn.Option[tlv.Blob]
30
}
31

32
// getLinkQuery is the function signature used to lookup a link.
33
type getLinkQuery func(lnwire.ShortChannelID) (
34
        htlcswitch.ChannelLink, error)
35

36
// bandwidthManager is an implementation of the bandwidthHints interface which
37
// uses the link lookup provided to query the link for our latest local channel
38
// balances.
39
type bandwidthManager struct {
40
        getLink       getLinkQuery
41
        localChans    map[lnwire.ShortChannelID]struct{}
42
        firstHopBlob  fn.Option[tlv.Blob]
43
        trafficShaper fn.Option[htlcswitch.AuxTrafficShaper]
44
}
45

46
// newBandwidthManager creates a bandwidth manager for the source node provided
47
// which is used to obtain hints from the lower layer w.r.t the available
48
// bandwidth of edges on the network. Currently, we'll only obtain bandwidth
49
// hints for the edges we directly have open ourselves. Obtaining these hints
50
// allows us to reduce the number of extraneous attempts as we can skip channels
51
// that are inactive, or just don't have enough bandwidth to carry the payment.
52
func newBandwidthManager(graph Graph, sourceNode route.Vertex,
53
        linkQuery getLinkQuery, firstHopBlob fn.Option[tlv.Blob],
54
        ts fn.Option[htlcswitch.AuxTrafficShaper]) (*bandwidthManager,
55
        error) {
46✔
56

46✔
57
        manager := &bandwidthManager{
46✔
58
                getLink:       linkQuery,
46✔
59
                localChans:    make(map[lnwire.ShortChannelID]struct{}),
46✔
60
                firstHopBlob:  firstHopBlob,
46✔
61
                trafficShaper: ts,
46✔
62
        }
46✔
63

46✔
64
        // First, we'll collect the set of outbound edges from the target
46✔
65
        // source node and add them to our bandwidth manager's map of channels.
46✔
66
        err := graph.ForEachNodeChannel(sourceNode,
46✔
67
                func(channel *graphdb.DirectedChannel) error {
191✔
68
                        shortID := lnwire.NewShortChanIDFromInt(
145✔
69
                                channel.ChannelID,
145✔
70
                        )
145✔
71
                        manager.localChans[shortID] = struct{}{}
145✔
72

145✔
73
                        return nil
145✔
74
                })
145✔
75

76
        if err != nil {
46✔
77
                return nil, err
×
78
        }
×
79

80
        return manager, nil
46✔
81
}
82

83
// getBandwidth queries the current state of a link and gets its currently
84
// available bandwidth. Note that this function assumes that the channel being
85
// queried is one of our local channels, so any failure to retrieve the link
86
// is interpreted as the link being offline.
87
func (b *bandwidthManager) getBandwidth(cid lnwire.ShortChannelID,
88
        amount lnwire.MilliSatoshi) lnwire.MilliSatoshi {
147✔
89

147✔
90
        link, err := b.getLink(cid)
147✔
91
        if err != nil {
152✔
92
                // If the link isn't online, then we'll report that it has
5✔
93
                // zero bandwidth.
5✔
94
                log.Warnf("ShortChannelID=%v: link not found: %v", cid, err)
5✔
95
                return 0
5✔
96
        }
5✔
97

98
        // If the link is found within the switch, but it isn't yet eligible
99
        // to forward any HTLCs, then we'll treat it as if it isn't online in
100
        // the first place.
101
        if !link.EligibleToForward() {
151✔
102
                log.Warnf("ShortChannelID=%v: not eligible to forward", cid)
5✔
103
                return 0
5✔
104
        }
5✔
105

106
        // bandwidthResult is an inline type that we'll use to pass the
107
        // bandwidth result from the external traffic shaper to the main logic
108
        // below.
109
        type bandwidthResult struct {
145✔
110
                // bandwidth is the available bandwidth for the channel as
145✔
111
                // reported by the external traffic shaper. If the external
145✔
112
                // traffic shaper is not handling the channel, this value will
145✔
113
                // be fn.None
145✔
114
                bandwidth fn.Option[lnwire.MilliSatoshi]
145✔
115

145✔
116
                // htlcAmount is the amount we're going to use to check if we
145✔
117
                // can add another HTLC to the channel. If the external traffic
145✔
118
                // shaper is handling the channel, we'll use 0 to just sanity
145✔
119
                // check the number of HTLCs on the channel, since we don't know
145✔
120
                // the actual HTLC amount that will be sent.
145✔
121
                htlcAmount fn.Option[lnwire.MilliSatoshi]
145✔
122
        }
145✔
123

145✔
124
        var (
145✔
125
                // We will pass the link bandwidth to the external traffic
145✔
126
                // shaper. This is the current best estimate for the available
145✔
127
                // bandwidth for the link.
145✔
128
                linkBandwidth = link.Bandwidth()
145✔
129

145✔
130
                bandwidthErr = func(err error) fn.Result[bandwidthResult] {
145✔
131
                        return fn.Err[bandwidthResult](err)
×
132
                }
×
133
        )
134

135
        result, err := fn.MapOptionZ(
145✔
136
                b.trafficShaper,
145✔
137
                func(s htlcswitch.AuxTrafficShaper) fn.Result[bandwidthResult] {
286✔
138
                        auxBandwidth, err := link.AuxBandwidth(
141✔
139
                                amount, cid, b.firstHopBlob, s,
141✔
140
                        ).Unpack()
141✔
141
                        if err != nil {
141✔
142
                                return bandwidthErr(fmt.Errorf("failed to get "+
×
NEW
143
                                        "auxiliary bandwidth: %w", err))
×
144
                        }
×
145

146
                        // We don't know the actual HTLC amount that will be
147
                        // sent using the custom channel. But we'll still want
148
                        // to make sure we can add another HTLC, using the
149
                        // MayAddOutgoingHtlc method below. Passing 0 into that
150
                        // method will use the minimum HTLC value for the
151
                        // channel, which is okay to just check we don't exceed
152
                        // the max number of HTLCs on the channel. A proper
153
                        // balance check is done elsewhere.
154
                        return fn.Ok(bandwidthResult{
141✔
155
                                bandwidth:  auxBandwidth,
141✔
156
                                htlcAmount: fn.Some[lnwire.MilliSatoshi](0),
141✔
157
                        })
141✔
158
                },
159
        ).Unpack()
160
        if err != nil {
145✔
161
                log.Errorf("ShortChannelID=%v: failed to get bandwidth from "+
×
162
                        "external traffic shaper: %v", cid, err)
×
163

×
164
                return 0
×
165
        }
×
166

167
        htlcAmount := result.htlcAmount.UnwrapOr(amount)
145✔
168

145✔
169
        // If our link isn't currently in a state where it can add another
145✔
170
        // outgoing htlc, treat the link as unusable.
145✔
171
        if err := link.MayAddOutgoingHtlc(htlcAmount); err != nil {
150✔
172
                log.Warnf("ShortChannelID=%v: cannot add outgoing "+
5✔
173
                        "htlc with amount %v: %v", cid, htlcAmount, err)
5✔
174
                return 0
5✔
175
        }
5✔
176

177
        // If the external traffic shaper determined the bandwidth, we'll return
178
        // that value, even if it is zero (which would mean no bandwidth is
179
        // available on that channel).
180
        reportedBandwidth := result.bandwidth.UnwrapOr(linkBandwidth)
144✔
181

144✔
182
        return reportedBandwidth
144✔
183
}
184

185
// availableChanBandwidth returns the total available bandwidth for a channel
186
// and a bool indicating whether the channel hint was found. If the channel is
187
// unavailable, a zero amount is returned.
188
func (b *bandwidthManager) availableChanBandwidth(channelID uint64,
189
        amount lnwire.MilliSatoshi) (lnwire.MilliSatoshi, bool) {
148✔
190

148✔
191
        shortID := lnwire.NewShortChanIDFromInt(channelID)
148✔
192
        _, ok := b.localChans[shortID]
148✔
193
        if !ok {
149✔
194
                return 0, false
1✔
195
        }
1✔
196

197
        return b.getBandwidth(shortID, amount), true
147✔
198
}
199

200
// firstHopCustomBlob returns the custom blob for the first hop of the payment,
201
// if available.
202
func (b *bandwidthManager) firstHopCustomBlob() fn.Option[tlv.Blob] {
40✔
203
        return b.firstHopBlob
40✔
204
}
40✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc