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

lightningnetwork / lnd / 14193549836

01 Apr 2025 10:40AM UTC coverage: 69.046% (+0.007%) from 69.039%
14193549836

Pull #9665

github

web-flow
Merge e8825f209 into b01f4e514
Pull Request #9665: kvdb: bump etcd libs to v3.5.12

133439 of 193262 relevant lines covered (69.05%)

22119.45 hits per line

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

85.05
/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) {
45✔
56

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

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

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

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

80
        return manager, nil
45✔
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 {
146✔
89

146✔
90
        link, err := b.getLink(cid)
146✔
91
        if err != nil {
150✔
92
                // If the link isn't online, then we'll report that it has
4✔
93
                // zero bandwidth.
4✔
94
                log.Warnf("ShortChannelID=%v: link not found: %v", cid, err)
4✔
95
                return 0
4✔
96
        }
4✔
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() {
149✔
102
                log.Warnf("ShortChannelID=%v: not eligible to forward", cid)
4✔
103
                return 0
4✔
104
        }
4✔
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 {
144✔
110
                // bandwidth is the available bandwidth for the channel as
144✔
111
                // reported by the external traffic shaper. If the external
144✔
112
                // traffic shaper is not handling the channel, this value will
144✔
113
                // be fn.None
144✔
114
                bandwidth fn.Option[lnwire.MilliSatoshi]
144✔
115

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

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

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

135
        result, err := fn.MapOptionZ(
144✔
136
                b.trafficShaper,
144✔
137
                func(s htlcswitch.AuxTrafficShaper) fn.Result[bandwidthResult] {
285✔
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 "+
×
143
                                        "auxiliary bandwidth: %w", err))
×
144
                        }
×
145

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

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

×
171
                return 0
×
172
        }
×
173

174
        htlcAmount := result.htlcAmount.UnwrapOr(amount)
144✔
175

144✔
176
        // If our link isn't currently in a state where it can add another
144✔
177
        // outgoing htlc, treat the link as unusable.
144✔
178
        if err := link.MayAddOutgoingHtlc(htlcAmount); err != nil {
148✔
179
                log.Warnf("ShortChannelID=%v: cannot add outgoing "+
4✔
180
                        "htlc with amount %v: %v", cid, htlcAmount, err)
4✔
181
                return 0
4✔
182
        }
4✔
183

184
        // If the external traffic shaper determined the bandwidth, we'll return
185
        // that value, even if it is zero (which would mean no bandwidth is
186
        // available on that channel).
187
        reportedBandwidth := result.bandwidth.UnwrapOr(linkBandwidth)
143✔
188

143✔
189
        return reportedBandwidth
143✔
190
}
191

192
// availableChanBandwidth returns the total available bandwidth for a channel
193
// and a bool indicating whether the channel hint was found. If the channel is
194
// unavailable, a zero amount is returned.
195
func (b *bandwidthManager) availableChanBandwidth(channelID uint64,
196
        amount lnwire.MilliSatoshi) (lnwire.MilliSatoshi, bool) {
147✔
197

147✔
198
        shortID := lnwire.NewShortChanIDFromInt(channelID)
147✔
199
        _, ok := b.localChans[shortID]
147✔
200
        if !ok {
148✔
201
                return 0, false
1✔
202
        }
1✔
203

204
        return b.getBandwidth(shortID, amount), true
146✔
205
}
206

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