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

lightningnetwork / lnd / 16096991976

06 Jul 2025 08:15AM UTC coverage: 57.802% (-0.001%) from 57.803%
16096991976

Pull #10042

github

web-flow
Merge 98a97f211 into ff32e90d1
Pull Request #10042: routing: add logs to debug potential payment sending issue

11 of 21 new or added lines in 2 files covered. (52.38%)

29 existing lines in 7 files now uncovered.

98502 of 170414 relevant lines covered (57.8%)

1.79 hits per line

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

74.34
/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) {
3✔
56

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

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

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

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

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

3✔
90
        link, err := b.getLink(cid)
3✔
91
        if err != nil {
6✔
92
                // If the link isn't online, then we'll report that it has
3✔
93
                // zero bandwidth.
3✔
94
                log.Debugf("ShortChannelID=%v: link not found (channel peer "+
3✔
95
                        "is probably offline): %v", cid, err)
3✔
96

3✔
97
                return 0
3✔
98
        }
3✔
99

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

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

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

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

3✔
132
                bandwidthErr = func(err error) fn.Result[bandwidthResult] {
3✔
133
                        return fn.Err[bandwidthResult](err)
×
134
                }
×
135
        )
136

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

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

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

×
173
                return 0
×
174
        }
×
175

176
        htlcAmount := result.htlcAmount.UnwrapOr(amount)
3✔
177

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

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

3✔
191
        return reportedBandwidth
3✔
192
}
193

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

3✔
200
        shortID := lnwire.NewShortChanIDFromInt(channelID)
3✔
201
        _, ok := b.localChans[shortID]
3✔
202
        if !ok {
3✔
NEW
203
                log.Warnf("ShortChannelID=%v: not found in the local channels "+
×
NEW
204
                        "map of the bandwidth manager, reporting 0 bandwidth "+
×
NEW
205
                        "for this channel", shortID)
×
NEW
206

×
207
                return 0, false
×
208
        }
×
209

210
        return b.getBandwidth(shortID, amount), true
3✔
211
}
212

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