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

lightningnetwork / lnd / 11170835610

03 Oct 2024 10:41PM UTC coverage: 49.188% (-9.6%) from 58.738%
11170835610

push

github

web-flow
Merge pull request #9154 from ziggie1984/master

multi: bump btcd version.

3 of 6 new or added lines in 6 files covered. (50.0%)

26110 existing lines in 428 files now uncovered.

97359 of 197934 relevant lines covered (49.19%)

1.04 hits per line

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

54.01
/routing/bandwidth.go
1
package routing
2

3
import (
4
        "fmt"
5

6
        "github.com/lightningnetwork/lnd/channeldb"
7
        "github.com/lightningnetwork/lnd/fn"
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
// TlvTrafficShaper is an interface that allows the sender to determine if a
33
// payment should be carried by a channel based on the TLV records that may be
34
// present in the `update_add_htlc` message or the channel commitment itself.
35
type TlvTrafficShaper interface {
36
        AuxHtlcModifier
37

38
        // ShouldHandleTraffic is called in order to check if the channel
39
        // identified by the provided channel ID may have external mechanisms
40
        // that would allow it to carry out the payment.
41
        ShouldHandleTraffic(cid lnwire.ShortChannelID,
42
                fundingBlob fn.Option[tlv.Blob]) (bool, error)
43

44
        // PaymentBandwidth returns the available bandwidth for a custom channel
45
        // decided by the given channel aux blob and HTLC blob. A return value
46
        // of 0 means there is no bandwidth available. To find out if a channel
47
        // is a custom channel that should be handled by the traffic shaper, the
48
        // HandleTraffic method should be called first.
49
        PaymentBandwidth(htlcBlob, commitmentBlob fn.Option[tlv.Blob],
50
                linkBandwidth,
51
                htlcAmt lnwire.MilliSatoshi) (lnwire.MilliSatoshi, error)
52
}
53

54
// AuxHtlcModifier is an interface that allows the sender to modify the outgoing
55
// HTLC of a payment by changing the amount or the wire message tlv records.
56
type AuxHtlcModifier interface {
57
        // ProduceHtlcExtraData is a function that, based on the previous extra
58
        // data blob of an HTLC, may produce a different blob or modify the
59
        // amount of bitcoin this htlc should carry.
60
        ProduceHtlcExtraData(totalAmount lnwire.MilliSatoshi,
61
                htlcCustomRecords lnwire.CustomRecords) (lnwire.MilliSatoshi,
62
                lnwire.CustomRecords, error)
63
}
64

65
// getLinkQuery is the function signature used to lookup a link.
66
type getLinkQuery func(lnwire.ShortChannelID) (
67
        htlcswitch.ChannelLink, error)
68

69
// bandwidthManager is an implementation of the bandwidthHints interface which
70
// uses the link lookup provided to query the link for our latest local channel
71
// balances.
72
type bandwidthManager struct {
73
        getLink       getLinkQuery
74
        localChans    map[lnwire.ShortChannelID]struct{}
75
        firstHopBlob  fn.Option[tlv.Blob]
76
        trafficShaper fn.Option[TlvTrafficShaper]
77
}
78

79
// newBandwidthManager creates a bandwidth manager for the source node provided
80
// which is used to obtain hints from the lower layer w.r.t the available
81
// bandwidth of edges on the network. Currently, we'll only obtain bandwidth
82
// hints for the edges we directly have open ourselves. Obtaining these hints
83
// allows us to reduce the number of extraneous attempts as we can skip channels
84
// that are inactive, or just don't have enough bandwidth to carry the payment.
85
func newBandwidthManager(graph Graph, sourceNode route.Vertex,
86
        linkQuery getLinkQuery, firstHopBlob fn.Option[tlv.Blob],
87
        trafficShaper fn.Option[TlvTrafficShaper]) (*bandwidthManager, error) {
2✔
88

2✔
89
        manager := &bandwidthManager{
2✔
90
                getLink:       linkQuery,
2✔
91
                localChans:    make(map[lnwire.ShortChannelID]struct{}),
2✔
92
                firstHopBlob:  firstHopBlob,
2✔
93
                trafficShaper: trafficShaper,
2✔
94
        }
2✔
95

2✔
96
        // First, we'll collect the set of outbound edges from the target
2✔
97
        // source node and add them to our bandwidth manager's map of channels.
2✔
98
        err := graph.ForEachNodeChannel(sourceNode,
2✔
99
                func(channel *channeldb.DirectedChannel) error {
4✔
100
                        shortID := lnwire.NewShortChanIDFromInt(
2✔
101
                                channel.ChannelID,
2✔
102
                        )
2✔
103
                        manager.localChans[shortID] = struct{}{}
2✔
104

2✔
105
                        return nil
2✔
106
                })
2✔
107

108
        if err != nil {
2✔
109
                return nil, err
×
110
        }
×
111

112
        return manager, nil
2✔
113
}
114

115
// getBandwidth queries the current state of a link and gets its currently
116
// available bandwidth. Note that this function assumes that the channel being
117
// queried is one of our local channels, so any failure to retrieve the link
118
// is interpreted as the link being offline.
119
func (b *bandwidthManager) getBandwidth(cid lnwire.ShortChannelID,
120
        amount lnwire.MilliSatoshi) lnwire.MilliSatoshi {
2✔
121

2✔
122
        link, err := b.getLink(cid)
2✔
123
        if err != nil {
2✔
UNCOV
124
                // If the link isn't online, then we'll report that it has
×
UNCOV
125
                // zero bandwidth.
×
UNCOV
126
                log.Warnf("ShortChannelID=%v: link not found: %v", cid, err)
×
UNCOV
127
                return 0
×
UNCOV
128
        }
×
129

130
        // If the link is found within the switch, but it isn't yet eligible
131
        // to forward any HTLCs, then we'll treat it as if it isn't online in
132
        // the first place.
133
        if !link.EligibleToForward() {
2✔
UNCOV
134
                log.Warnf("ShortChannelID=%v: not eligible to forward", cid)
×
UNCOV
135
                return 0
×
UNCOV
136
        }
×
137

138
        // bandwidthResult is an inline type that we'll use to pass the
139
        // bandwidth result from the external traffic shaper to the main logic
140
        // below.
141
        type bandwidthResult struct {
2✔
142
                // bandwidth is the available bandwidth for the channel as
2✔
143
                // reported by the external traffic shaper. If the external
2✔
144
                // traffic shaper is not handling the channel, this value will
2✔
145
                // be fn.None
2✔
146
                bandwidth fn.Option[lnwire.MilliSatoshi]
2✔
147

2✔
148
                // htlcAmount is the amount we're going to use to check if we
2✔
149
                // can add another HTLC to the channel. If the external traffic
2✔
150
                // shaper is handling the channel, we'll use 0 to just sanity
2✔
151
                // check the number of HTLCs on the channel, since we don't know
2✔
152
                // the actual HTLC amount that will be sent.
2✔
153
                htlcAmount fn.Option[lnwire.MilliSatoshi]
2✔
154
        }
2✔
155

2✔
156
        var (
2✔
157
                // We will pass the link bandwidth to the external traffic
2✔
158
                // shaper. This is the current best estimate for the available
2✔
159
                // bandwidth for the link.
2✔
160
                linkBandwidth = link.Bandwidth()
2✔
161

2✔
162
                bandwidthErr = func(err error) fn.Result[bandwidthResult] {
2✔
163
                        return fn.Err[bandwidthResult](err)
×
164
                }
×
165
        )
166

167
        result, err := fn.MapOptionZ(
2✔
168
                b.trafficShaper,
2✔
169
                func(ts TlvTrafficShaper) fn.Result[bandwidthResult] {
2✔
UNCOV
170
                        fundingBlob := link.FundingCustomBlob()
×
UNCOV
171
                        shouldHandle, err := ts.ShouldHandleTraffic(
×
UNCOV
172
                                cid, fundingBlob,
×
UNCOV
173
                        )
×
UNCOV
174
                        if err != nil {
×
175
                                return bandwidthErr(fmt.Errorf("traffic "+
×
176
                                        "shaper failed to decide whether to "+
×
177
                                        "handle traffic: %w", err))
×
178
                        }
×
179

UNCOV
180
                        log.Debugf("ShortChannelID=%v: external traffic "+
×
UNCOV
181
                                "shaper is handling traffic: %v", cid,
×
UNCOV
182
                                shouldHandle)
×
UNCOV
183

×
UNCOV
184
                        // If this channel isn't handled by the external traffic
×
UNCOV
185
                        // shaper, we'll return early.
×
UNCOV
186
                        if !shouldHandle {
×
187
                                return fn.Ok(bandwidthResult{})
×
188
                        }
×
189

190
                        // Ask for a specific bandwidth to be used for the
191
                        // channel.
UNCOV
192
                        commitmentBlob := link.CommitmentCustomBlob()
×
UNCOV
193
                        auxBandwidth, err := ts.PaymentBandwidth(
×
UNCOV
194
                                b.firstHopBlob, commitmentBlob, linkBandwidth,
×
UNCOV
195
                                amount,
×
UNCOV
196
                        )
×
UNCOV
197
                        if err != nil {
×
198
                                return bandwidthErr(fmt.Errorf("failed to get "+
×
199
                                        "bandwidth from external traffic "+
×
200
                                        "shaper: %w", err))
×
201
                        }
×
202

UNCOV
203
                        log.Debugf("ShortChannelID=%v: external traffic "+
×
UNCOV
204
                                "shaper reported available bandwidth: %v", cid,
×
UNCOV
205
                                auxBandwidth)
×
UNCOV
206

×
UNCOV
207
                        // We don't know the actual HTLC amount that will be
×
UNCOV
208
                        // sent using the custom channel. But we'll still want
×
UNCOV
209
                        // to make sure we can add another HTLC, using the
×
UNCOV
210
                        // MayAddOutgoingHtlc method below. Passing 0 into that
×
UNCOV
211
                        // method will use the minimum HTLC value for the
×
UNCOV
212
                        // channel, which is okay to just check we don't exceed
×
UNCOV
213
                        // the max number of HTLCs on the channel. A proper
×
UNCOV
214
                        // balance check is done elsewhere.
×
UNCOV
215
                        return fn.Ok(bandwidthResult{
×
UNCOV
216
                                bandwidth:  fn.Some(auxBandwidth),
×
UNCOV
217
                                htlcAmount: fn.Some[lnwire.MilliSatoshi](0),
×
UNCOV
218
                        })
×
219
                },
220
        ).Unpack()
221
        if err != nil {
2✔
222
                log.Errorf("ShortChannelID=%v: failed to get bandwidth from "+
×
223
                        "external traffic shaper: %v", cid, err)
×
224

×
225
                return 0
×
226
        }
×
227

228
        htlcAmount := result.htlcAmount.UnwrapOr(amount)
2✔
229

2✔
230
        // If our link isn't currently in a state where it can add another
2✔
231
        // outgoing htlc, treat the link as unusable.
2✔
232
        if err := link.MayAddOutgoingHtlc(htlcAmount); err != nil {
4✔
233
                log.Warnf("ShortChannelID=%v: cannot add outgoing "+
2✔
234
                        "htlc with amount %v: %v", cid, htlcAmount, err)
2✔
235
                return 0
2✔
236
        }
2✔
237

238
        // If the external traffic shaper determined the bandwidth, we'll return
239
        // that value, even if it is zero (which would mean no bandwidth is
240
        // available on that channel).
241
        reportedBandwidth := result.bandwidth.UnwrapOr(linkBandwidth)
2✔
242

2✔
243
        return reportedBandwidth
2✔
244
}
245

246
// availableChanBandwidth returns the total available bandwidth for a channel
247
// and a bool indicating whether the channel hint was found. If the channel is
248
// unavailable, a zero amount is returned.
249
func (b *bandwidthManager) availableChanBandwidth(channelID uint64,
250
        amount lnwire.MilliSatoshi) (lnwire.MilliSatoshi, bool) {
2✔
251

2✔
252
        shortID := lnwire.NewShortChanIDFromInt(channelID)
2✔
253
        _, ok := b.localChans[shortID]
2✔
254
        if !ok {
2✔
UNCOV
255
                return 0, false
×
UNCOV
256
        }
×
257

258
        return b.getBandwidth(shortID, amount), true
2✔
259
}
260

261
// firstHopCustomBlob returns the custom blob for the first hop of the payment,
262
// if available.
263
func (b *bandwidthManager) firstHopCustomBlob() fn.Option[tlv.Blob] {
2✔
264
        return b.firstHopBlob
2✔
265
}
2✔
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