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

lightningnetwork / lnd / 16683051882

01 Aug 2025 07:03PM UTC coverage: 54.949% (-12.1%) from 67.047%
16683051882

Pull #9455

github

web-flow
Merge 3f1f50be8 into 37523b6cb
Pull Request #9455: discovery+lnwire: add support for DNS host name in NodeAnnouncement msg

144 of 226 new or added lines in 7 files covered. (63.72%)

23852 existing lines in 290 files now uncovered.

108751 of 197912 relevant lines covered (54.95%)

22080.83 hits per line

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

85.32
/routing/bandwidth.go
1
package routing
2

3
import (
4
        "fmt"
5

6
        "github.com/lightningnetwork/lnd/fn/v2"
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) {
42✔
56

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

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

141✔
73
                        return nil
141✔
74
                }, func() {
141✔
UNCOV
75
                        clear(manager.localChans)
×
UNCOV
76
                },
×
77
        )
78
        if err != nil {
42✔
79
                return nil, err
×
80
        }
×
81

82
        return manager, nil
42✔
83
}
84

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

143✔
92
        link, err := b.getLink(cid)
143✔
93
        if err != nil {
144✔
94
                return 0, fmt.Errorf("error querying switch for link: %w", err)
1✔
95
        }
1✔
96

97
        // If the link is found within the switch, but it isn't yet eligible
98
        // to forward any HTLCs, then we'll treat it as if it isn't online in
99
        // the first place.
100
        if !link.EligibleToForward() {
143✔
101
                return 0, fmt.Errorf("link not eligible to forward")
1✔
102
        }
1✔
103

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

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

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

141✔
128
                bandwidthErr = func(err error) fn.Result[bandwidthResult] {
141✔
129
                        return fn.Err[bandwidthResult](err)
×
130
                }
×
131
        )
132

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

144
                        // If the external traffic shaper is not handling the
145
                        // channel, we'll just return the original bandwidth and
146
                        // no custom amount.
147
                        if !auxBandwidth.IsHandled {
282✔
148
                                return fn.Ok(bandwidthResult{})
141✔
149
                        }
141✔
150

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

170
        htlcAmount := result.htlcAmount.UnwrapOr(amount)
141✔
171

141✔
172
        // If our link isn't currently in a state where it can add another
141✔
173
        // outgoing htlc, treat the link as unusable.
141✔
174
        if err := link.MayAddOutgoingHtlc(htlcAmount); err != nil {
142✔
175
                return 0, fmt.Errorf("cannot add outgoing htlc to channel %v "+
1✔
176
                        "with amount %v: %w", cid, htlcAmount, err)
1✔
177
        }
1✔
178

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

140✔
184
        return reportedBandwidth, nil
140✔
185
}
186

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

144✔
193
        shortID := lnwire.NewShortChanIDFromInt(channelID)
144✔
194
        _, ok := b.localChans[shortID]
144✔
195
        if !ok {
145✔
196
                return 0, false
1✔
197
        }
1✔
198

199
        bandwidth, err := b.getBandwidth(shortID, amount)
143✔
200
        if err != nil {
146✔
201
                log.Warnf("failed to get bandwidth for channel %v: %v",
3✔
202
                        shortID, err)
3✔
203

3✔
204
                return 0, true
3✔
205
        }
3✔
206

207
        return bandwidth, true
140✔
208
}
209

210
// firstHopCustomBlob returns the custom blob for the first hop of the payment,
211
// if available.
212
func (b *bandwidthManager) firstHopCustomBlob() fn.Option[tlv.Blob] {
36✔
213
        return b.firstHopBlob
36✔
214
}
36✔
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