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

lightningnetwork / lnd / 11216766535

07 Oct 2024 01:37PM UTC coverage: 57.817% (-1.0%) from 58.817%
11216766535

Pull #9148

github

ProofOfKeags
lnwire: remove kickoff feerate from propose/commit
Pull Request #9148: DynComms [2/n]: lnwire: add authenticated wire messages for Dyn*

571 of 879 new or added lines in 16 files covered. (64.96%)

23253 existing lines in 251 files now uncovered.

99022 of 171268 relevant lines covered (57.82%)

38420.67 hits per line

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

86.13
/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 lnwire.MilliSatoshi) (lnwire.MilliSatoshi, error)
51
}
52

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

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

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

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

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

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

141✔
104
                        return nil
141✔
105
                })
141✔
106

141✔
107
        if err != nil {
108
                return nil, err
42✔
109
        }
×
UNCOV
110

×
111
        return manager, nil
112
}
42✔
113

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

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

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

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

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

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

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

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

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

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

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

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

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

×
223
                return 0
×
224
        }
×
UNCOV
225

×
UNCOV
226
        htlcAmount := result.htlcAmount.UnwrapOr(amount)
×
227

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

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

241
        return reportedBandwidth
140✔
242
}
140✔
243

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

250
        shortID := lnwire.NewShortChanIDFromInt(channelID)
144✔
251
        _, ok := b.localChans[shortID]
144✔
252
        if !ok {
144✔
253
                return 0, false
144✔
254
        }
145✔
255

1✔
256
        return b.getBandwidth(shortID, amount), true
1✔
257
}
258

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