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

lightningnetwork / lnd / 15574102646

11 Jun 2025 01:44AM UTC coverage: 68.554% (+9.9%) from 58.637%
15574102646

Pull #9652

github

web-flow
Merge eb863e46a into 92a5d35cf
Pull Request #9652: lnwallet/chancloser: fix flake in TestRbfCloseClosingNegotiationLocal

11 of 12 new or added lines in 1 file covered. (91.67%)

7276 existing lines in 84 files now uncovered.

134508 of 196208 relevant lines covered (68.55%)

44569.29 hits per line

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

80.66
/autopilot/agent.go
1
package autopilot
2

3
import (
4
        "bytes"
5
        "context"
6
        "fmt"
7
        "math/rand"
8
        "net"
9
        "sync"
10
        "time"
11

12
        "github.com/btcsuite/btcd/btcec/v2"
13
        "github.com/btcsuite/btcd/btcutil"
14
        "github.com/davecgh/go-spew/spew"
15
        "github.com/lightningnetwork/lnd/fn/v2"
16
        "github.com/lightningnetwork/lnd/lnwire"
17
)
18

19
// Config couples all the items that an autopilot agent needs to function.
20
// All items within the struct MUST be populated for the Agent to be able to
21
// carry out its duties.
22
type Config struct {
23
        // Self is the identity public key of the Lightning Network node that
24
        // is being driven by the agent. This is used to ensure that we don't
25
        // accidentally attempt to open a channel with ourselves.
26
        Self *btcec.PublicKey
27

28
        // Heuristic is an attachment heuristic which will govern to whom we
29
        // open channels to, and also what those channels look like in terms of
30
        // desired capacity. The Heuristic will take into account the current
31
        // state of the graph, our set of open channels, and the amount of
32
        // available funds when determining how channels are to be opened.
33
        // Additionally, a heuristic make also factor in extra-graph
34
        // information in order to make more pertinent recommendations.
35
        Heuristic AttachmentHeuristic
36

37
        // ChanController is an interface that is able to directly manage the
38
        // creation, closing and update of channels within the network.
39
        ChanController ChannelController
40

41
        // ConnectToPeer attempts to connect to the peer using one of its
42
        // advertised addresses. The boolean returned signals whether the peer
43
        // was already connected.
44
        ConnectToPeer func(*btcec.PublicKey, []net.Addr) (bool, error)
45

46
        // DisconnectPeer attempts to disconnect the peer with the given public
47
        // key.
48
        DisconnectPeer func(*btcec.PublicKey) error
49

50
        // WalletBalance is a function closure that should return the current
51
        // available balance of the backing wallet.
52
        WalletBalance func() (btcutil.Amount, error)
53

54
        // Graph is an abstract channel graph that the Heuristic and the Agent
55
        // will use to make decisions w.r.t channel allocation and placement
56
        // within the graph.
57
        Graph ChannelGraph
58

59
        // Constraints is the set of constraints the autopilot must adhere to
60
        // when opening channels.
61
        Constraints AgentConstraints
62

63
        // TODO(roasbeef): add additional signals from fee rates and revenue of
64
        // currently opened channels
65
}
66

67
// channelState is a type that represents the set of active channels of the
68
// backing LN node that the Agent should be aware of. This type contains a few
69
// helper utility methods.
70
type channelState map[lnwire.ShortChannelID]LocalChannel
71

72
// Channels returns a slice of all the active channels.
73
func (c channelState) Channels() []LocalChannel {
66✔
74
        chans := make([]LocalChannel, 0, len(c))
66✔
75
        for _, channel := range c {
72✔
76
                chans = append(chans, channel)
6✔
77
        }
6✔
78
        return chans
66✔
79
}
80

81
// ConnectedNodes returns the set of nodes we currently have a channel with.
82
// This information is needed as we want to avoid making repeated channels with
83
// any node.
84
func (c channelState) ConnectedNodes() map[NodeID]struct{} {
32✔
85
        nodes := make(map[NodeID]struct{})
32✔
86
        for _, channels := range c {
32✔
UNCOV
87
                nodes[channels.Node] = struct{}{}
×
UNCOV
88
        }
×
89

90
        // TODO(roasbeef): add outgoing, nodes, allow incoming and outgoing to
91
        // per node
92
        //  * only add node is chan as funding amt set
93

94
        return nodes
32✔
95
}
96

97
// Agent implements a closed-loop control system which seeks to autonomously
98
// optimize the allocation of satoshis within channels throughput the network's
99
// channel graph. An agent is configurable by swapping out different
100
// AttachmentHeuristic strategies. The agent uses external signals such as the
101
// wallet balance changing, or new channels being opened/closed for the local
102
// node as an indicator to re-examine its internal state, and the amount of
103
// available funds in order to make updated decisions w.r.t the channel graph.
104
// The Agent will automatically open, close, and splice in/out channel as
105
// necessary for it to step closer to its optimal state.
106
//
107
// TODO(roasbeef): prob re-word
108
type Agent struct {
109
        started sync.Once
110
        stopped sync.Once
111

112
        // cfg houses the configuration state of the Ant.
113
        cfg Config
114

115
        // chanState tracks the current set of open channels.
116
        chanState    channelState
117
        chanStateMtx sync.Mutex
118

119
        // stateUpdates is a channel that any external state updates that may
120
        // affect the heuristics of the agent will be sent over.
121
        stateUpdates chan interface{}
122

123
        // balanceUpdates is a channel where notifications about updates to the
124
        // wallet's balance will be sent. This channel will be buffered to
125
        // ensure we have at most one pending update of this type to handle at
126
        // a given time.
127
        balanceUpdates chan *balanceUpdate
128

129
        // nodeUpdates is a channel that changes to the graph node landscape
130
        // will be sent over. This channel will be buffered to ensure we have
131
        // at most one pending update of this type to handle at a given time.
132
        nodeUpdates chan *nodeUpdates
133

134
        // pendingOpenUpdates is a channel where updates about channel pending
135
        // opening will be sent. This channel will be buffered to ensure we
136
        // have at most one pending update of this type to handle at a given
137
        // time.
138
        pendingOpenUpdates chan *chanPendingOpenUpdate
139

140
        // chanOpenFailures is a channel where updates about channel open
141
        // failures will be sent. This channel will be buffered to ensure we
142
        // have at most one pending update of this type to handle at a given
143
        // time.
144
        chanOpenFailures chan *chanOpenFailureUpdate
145

146
        // heuristicUpdates is a channel where updates from active heuristics
147
        // will be sent.
148
        heuristicUpdates chan *heuristicUpdate
149

150
        // totalBalance is the total number of satoshis the backing wallet is
151
        // known to control at any given instance. This value will be updated
152
        // when the agent receives external balance update signals.
153
        totalBalance btcutil.Amount
154

155
        // failedNodes lists nodes that we've previously attempted to initiate
156
        // channels with, but didn't succeed.
157
        failedNodes map[NodeID]struct{}
158

159
        // pendingConns tracks the nodes that we are attempting to make
160
        // connections to. This prevents us from making duplicate connection
161
        // requests to the same node.
162
        pendingConns map[NodeID]struct{}
163

164
        // pendingOpens tracks the channels that we've requested to be
165
        // initiated, but haven't yet been confirmed as being fully opened.
166
        // This state is required as otherwise, we may go over our allotted
167
        // channel limit, or open multiple channels to the same node.
168
        pendingOpens map[NodeID]LocalChannel
169
        pendingMtx   sync.Mutex
170

171
        quit   chan struct{}
172
        wg     sync.WaitGroup
173
        cancel fn.Option[context.CancelFunc]
174
}
175

176
// New creates a new instance of the Agent instantiated using the passed
177
// configuration and initial channel state. The initial channel state slice
178
// should be populated with the set of Channels that are currently opened by
179
// the backing Lightning Node.
180
func New(cfg Config, initialState []LocalChannel) (*Agent, error) {
26✔
181
        a := &Agent{
26✔
182
                cfg:                cfg,
26✔
183
                chanState:          make(map[lnwire.ShortChannelID]LocalChannel),
26✔
184
                quit:               make(chan struct{}),
26✔
185
                stateUpdates:       make(chan interface{}),
26✔
186
                balanceUpdates:     make(chan *balanceUpdate, 1),
26✔
187
                nodeUpdates:        make(chan *nodeUpdates, 1),
26✔
188
                chanOpenFailures:   make(chan *chanOpenFailureUpdate, 1),
26✔
189
                heuristicUpdates:   make(chan *heuristicUpdate, 1),
26✔
190
                pendingOpenUpdates: make(chan *chanPendingOpenUpdate, 1),
26✔
191
                failedNodes:        make(map[NodeID]struct{}),
26✔
192
                pendingConns:       make(map[NodeID]struct{}),
26✔
193
                pendingOpens:       make(map[NodeID]LocalChannel),
26✔
194
        }
26✔
195

26✔
196
        for _, c := range initialState {
30✔
197
                a.chanState[c.ChanID] = c
4✔
198
        }
4✔
199

200
        return a, nil
26✔
201
}
202

203
// Start starts the agent along with any goroutines it needs to perform its
204
// normal duties.
205
func (a *Agent) Start() error {
26✔
206
        var err error
26✔
207
        a.started.Do(func() {
52✔
208
                ctx, cancel := context.WithCancel(context.Background())
26✔
209
                a.cancel = fn.Some(cancel)
26✔
210

26✔
211
                err = a.start(ctx)
26✔
212
        })
26✔
213
        return err
26✔
214
}
215

216
func (a *Agent) start(ctx context.Context) error {
26✔
217
        rand.Seed(time.Now().Unix())
26✔
218
        log.Infof("Autopilot Agent starting")
26✔
219

26✔
220
        a.wg.Add(1)
26✔
221
        go a.controller(ctx)
26✔
222

26✔
223
        return nil
26✔
224
}
26✔
225

226
// Stop signals the Agent to gracefully shutdown. This function will block
227
// until all goroutines have exited.
228
func (a *Agent) Stop() error {
28✔
229
        var err error
28✔
230
        a.stopped.Do(func() {
54✔
231
                err = a.stop()
26✔
232
        })
26✔
233
        return err
28✔
234
}
235

236
func (a *Agent) stop() error {
26✔
237
        log.Infof("Autopilot Agent stopping")
26✔
238

26✔
239
        a.cancel.WhenSome(func(fn context.CancelFunc) { fn() })
52✔
240
        close(a.quit)
26✔
241
        a.wg.Wait()
26✔
242

26✔
243
        return nil
26✔
244
}
245

246
// balanceUpdate is a type of external state update that reflects an
247
// increase/decrease in the funds currently available to the wallet.
248
type balanceUpdate struct {
249
}
250

251
// nodeUpdates is a type of external state update that reflects an addition or
252
// modification in channel graph node membership.
253
type nodeUpdates struct{}
254

255
// chanOpenUpdate is a type of external state update that indicates a new
256
// channel has been opened, either by the Agent itself (within the main
257
// controller loop), or by an external user to the system.
258
type chanOpenUpdate struct {
259
        newChan LocalChannel
260
}
261

262
// chanPendingOpenUpdate is a type of external state update that indicates a new
263
// channel has been opened, either by the agent itself or an external subsystem,
264
// but is still pending.
265
type chanPendingOpenUpdate struct{}
266

267
// chanOpenFailureUpdate is a type of external state update that indicates
268
// a previous channel open failed, and that it might be possible to try again.
269
type chanOpenFailureUpdate struct{}
270

271
// heuristicUpdate is an update sent when one of the autopilot heuristics has
272
// changed, and prompts the agent to make a new attempt at opening more
273
// channels.
274
type heuristicUpdate struct {
275
        heuristic AttachmentHeuristic
276
}
277

278
// chanCloseUpdate is a type of external state update that indicates that the
279
// backing Lightning Node has closed a previously open channel.
280
type chanCloseUpdate struct {
281
        closedChans []lnwire.ShortChannelID
282
}
283

284
// OnBalanceChange is a callback that should be executed each time the balance
285
// of the backing wallet changes.
286
func (a *Agent) OnBalanceChange() {
30✔
287
        select {
30✔
288
        case a.balanceUpdates <- &balanceUpdate{}:
30✔
289
        default:
×
290
        }
291
}
292

293
// OnNodeUpdates is a callback that should be executed each time our channel
294
// graph has new nodes or their node announcements are updated.
295
func (a *Agent) OnNodeUpdates() {
4✔
296
        select {
4✔
297
        case a.nodeUpdates <- &nodeUpdates{}:
4✔
298
        default:
×
299
        }
300
}
301

302
// OnChannelOpen is a callback that should be executed each time a new channel
303
// is manually opened by the user or any system outside the autopilot agent.
304
func (a *Agent) OnChannelOpen(c LocalChannel) {
2✔
305
        a.wg.Add(1)
2✔
306
        go func() {
4✔
307
                defer a.wg.Done()
2✔
308

2✔
309
                select {
2✔
310
                case a.stateUpdates <- &chanOpenUpdate{newChan: c}:
2✔
UNCOV
311
                case <-a.quit:
×
312
                }
313
        }()
314
}
315

316
// OnChannelPendingOpen is a callback that should be executed each time a new
317
// channel is opened, either by the agent or an external subsystems, but is
318
// still pending.
319
func (a *Agent) OnChannelPendingOpen() {
46✔
320
        select {
46✔
321
        case a.pendingOpenUpdates <- &chanPendingOpenUpdate{}:
28✔
322
        default:
18✔
323
        }
324
}
325

326
// OnChannelOpenFailure is a callback that should be executed when the
327
// autopilot has attempted to open a channel, but failed. In this case we can
328
// retry channel creation with a different node.
329
func (a *Agent) OnChannelOpenFailure() {
2✔
330
        select {
2✔
331
        case a.chanOpenFailures <- &chanOpenFailureUpdate{}:
2✔
332
        default:
×
333
        }
334
}
335

336
// OnChannelClose is a callback that should be executed each time a prior
337
// channel has been closed for any reason. This includes regular
338
// closes, force closes, and channel breaches.
339
func (a *Agent) OnChannelClose(closedChans ...lnwire.ShortChannelID) {
2✔
340
        a.wg.Add(1)
2✔
341
        go func() {
4✔
342
                defer a.wg.Done()
2✔
343

2✔
344
                select {
2✔
345
                case a.stateUpdates <- &chanCloseUpdate{closedChans: closedChans}:
2✔
346
                case <-a.quit:
×
347
                }
348
        }()
349
}
350

351
// OnHeuristicUpdate is a method called when a heuristic has been updated, to
352
// trigger the agent to do a new state assessment.
353
func (a *Agent) OnHeuristicUpdate(h AttachmentHeuristic) {
2✔
354
        select {
2✔
355
        case a.heuristicUpdates <- &heuristicUpdate{
356
                heuristic: h,
357
        }:
2✔
UNCOV
358
        default:
×
359
        }
360
}
361

362
// mergeNodeMaps merges the Agent's set of nodes that it already has active
363
// channels open to, with the other sets of nodes that should be removed from
364
// consideration during heuristic selection. This ensures that the Agent doesn't
365
// attempt to open any "duplicate" channels to the same node.
366
func mergeNodeMaps(c map[NodeID]LocalChannel,
367
        skips ...map[NodeID]struct{}) map[NodeID]struct{} {
32✔
368

32✔
369
        numNodes := len(c)
32✔
370
        for _, skip := range skips {
128✔
371
                numNodes += len(skip)
96✔
372
        }
96✔
373

374
        res := make(map[NodeID]struct{}, numNodes)
32✔
375
        for nodeID := range c {
60✔
376
                res[nodeID] = struct{}{}
28✔
377
        }
28✔
378
        for _, skip := range skips {
128✔
379
                for nodeID := range skip {
100✔
380
                        res[nodeID] = struct{}{}
4✔
381
                }
4✔
382
        }
383

384
        return res
32✔
385
}
386

387
// mergeChanState merges the Agent's set of active channels, with the set of
388
// channels awaiting confirmation. This ensures that the agent doesn't go over
389
// the prescribed channel limit or fund allocation limit.
390
func mergeChanState(pendingChans map[NodeID]LocalChannel,
391
        activeChans channelState) []LocalChannel {
66✔
392

66✔
393
        numChans := len(pendingChans) + len(activeChans)
66✔
394
        totalChans := make([]LocalChannel, 0, numChans)
66✔
395

66✔
396
        totalChans = append(totalChans, activeChans.Channels()...)
66✔
397

66✔
398
        for _, pendingChan := range pendingChans {
150✔
399
                totalChans = append(totalChans, pendingChan)
84✔
400
        }
84✔
401

402
        return totalChans
66✔
403
}
404

405
// controller implements the closed-loop control system of the Agent. The
406
// controller will make a decision w.r.t channel placement within the graph
407
// based on: its current internal state of the set of active channels open,
408
// and external state changes as a result of decisions it makes w.r.t channel
409
// allocation, or attributes affecting its control loop being updated by the
410
// backing Lightning Node.
411
func (a *Agent) controller(ctx context.Context) {
26✔
412
        defer a.wg.Done()
26✔
413

26✔
414
        // We'll start off by assigning our starting balance, and injecting
26✔
415
        // that amount as an initial wake up to the main controller goroutine.
26✔
416
        a.OnBalanceChange()
26✔
417

26✔
418
        // TODO(roasbeef): do we in fact need to maintain order?
26✔
419
        //  * use sync.Cond if so
26✔
420
        updateBalance := func() {
86✔
421
                newBalance, err := a.cfg.WalletBalance()
60✔
422
                if err != nil {
60✔
UNCOV
423
                        log.Warnf("unable to update wallet balance: %v", err)
×
424
                        return
×
425
                }
×
426

427
                a.totalBalance = newBalance
60✔
428
        }
429

430
        // TODO(roasbeef): add 10-minute wake up timer
431
        for {
118✔
432
                select {
92✔
433
                // A new external signal has arrived. We'll use this to update
434
                // our internal state, then determine if we should trigger a
435
                // channel state modification (open/close, splice in/out).
436
                case signal := <-a.stateUpdates:
4✔
437
                        log.Infof("Processing new external signal")
4✔
438

4✔
439
                        switch update := signal.(type) {
4✔
440
                        // A new channel has been opened successfully. This was
441
                        // either opened by the Agent, or an external system
442
                        // that is able to drive the Lightning Node.
443
                        case *chanOpenUpdate:
2✔
444
                                log.Debugf("New channel successfully opened, "+
2✔
445
                                        "updating state with: %v",
2✔
446
                                        spew.Sdump(update.newChan))
2✔
447

2✔
448
                                newChan := update.newChan
2✔
449
                                a.chanStateMtx.Lock()
2✔
450
                                a.chanState[newChan.ChanID] = newChan
2✔
451
                                a.chanStateMtx.Unlock()
2✔
452

2✔
453
                                a.pendingMtx.Lock()
2✔
454
                                delete(a.pendingOpens, newChan.Node)
2✔
455
                                a.pendingMtx.Unlock()
2✔
456

2✔
457
                                updateBalance()
2✔
458
                        // A channel has been closed, this may free up an
459
                        // available slot, triggering a new channel update.
460
                        case *chanCloseUpdate:
2✔
461
                                log.Debugf("Applying closed channel "+
2✔
462
                                        "updates: %v",
2✔
463
                                        spew.Sdump(update.closedChans))
2✔
464

2✔
465
                                a.chanStateMtx.Lock()
2✔
466
                                for _, closedChan := range update.closedChans {
6✔
467
                                        delete(a.chanState, closedChan)
4✔
468
                                }
4✔
469
                                a.chanStateMtx.Unlock()
2✔
470

2✔
471
                                updateBalance()
2✔
472
                        }
473

474
                // A new channel has been opened by the agent or an external
475
                // subsystem, but is still pending confirmation.
476
                case <-a.pendingOpenUpdates:
25✔
477
                        updateBalance()
25✔
478

479
                // The balance of the backing wallet has changed, if more funds
480
                // are now available, we may attempt to open up an additional
481
                // channel, or splice in funds to an existing one.
482
                case <-a.balanceUpdates:
29✔
483
                        log.Debug("Applying external balance state update")
29✔
484

29✔
485
                        updateBalance()
29✔
486

487
                // The channel we tried to open previously failed for whatever
488
                // reason.
489
                case <-a.chanOpenFailures:
2✔
490
                        log.Debug("Retrying after previous channel open " +
2✔
491
                                "failure.")
2✔
492

2✔
493
                        updateBalance()
2✔
494

495
                // New nodes have been added to the graph or their node
496
                // announcements have been updated. We will consider opening
497
                // channels to these nodes if we haven't stabilized.
498
                case <-a.nodeUpdates:
4✔
499
                        log.Debugf("Node updates received, assessing " +
4✔
500
                                "need for more channels")
4✔
501

502
                // Any of the deployed heuristics has been updated, check
503
                // whether we have new channel candidates available.
504
                case upd := <-a.heuristicUpdates:
2✔
505
                        log.Debugf("Heuristic %v updated, assessing need for "+
2✔
506
                                "more channels", upd.heuristic.Name())
2✔
507

508
                // The agent has been signalled to exit, so we'll bail out
509
                // immediately.
510
                case <-a.quit:
10✔
511
                        return
10✔
512

513
                case <-ctx.Done():
16✔
514
                        return
16✔
515
                }
516

517
                a.pendingMtx.Lock()
66✔
518
                log.Debugf("Pending channels: %v", spew.Sdump(a.pendingOpens))
66✔
519
                a.pendingMtx.Unlock()
66✔
520

66✔
521
                // With all the updates applied, we'll obtain a set of the
66✔
522
                // current active channels (confirmed channels), and also
66✔
523
                // factor in our set of unconfirmed channels.
66✔
524
                a.chanStateMtx.Lock()
66✔
525
                a.pendingMtx.Lock()
66✔
526
                totalChans := mergeChanState(a.pendingOpens, a.chanState)
66✔
527
                a.pendingMtx.Unlock()
66✔
528
                a.chanStateMtx.Unlock()
66✔
529

66✔
530
                // Now that we've updated our internal state, we'll consult our
66✔
531
                // channel attachment heuristic to determine if we can open
66✔
532
                // up any additional channels while staying within our
66✔
533
                // constraints.
66✔
534
                availableFunds, numChans := a.cfg.Constraints.ChannelBudget(
66✔
535
                        totalChans, a.totalBalance,
66✔
536
                )
66✔
537
                switch {
66✔
538
                case numChans == 0:
34✔
539
                        continue
34✔
540

541
                // If the amount is too small, we don't want to attempt opening
542
                // another channel.
543
                case availableFunds == 0:
×
544
                        continue
×
545
                case availableFunds < a.cfg.Constraints.MinChanSize():
×
UNCOV
546
                        continue
×
547
                }
548

549
                log.Infof("Triggering attachment directive dispatch, "+
32✔
550
                        "total_funds=%v", a.totalBalance)
32✔
551

32✔
552
                err := a.openChans(ctx, availableFunds, numChans, totalChans)
32✔
553
                if err != nil {
34✔
554
                        log.Errorf("Unable to open channels: %v", err)
2✔
555
                }
2✔
556
        }
557
}
558

559
// openChans queries the agent's heuristic for a set of channel candidates, and
560
// attempts to open channels to them.
561
func (a *Agent) openChans(ctx context.Context, availableFunds btcutil.Amount,
562
        numChans uint32, totalChans []LocalChannel) error {
32✔
563

32✔
564
        // As channel size we'll use the maximum channel size available.
32✔
565
        chanSize := a.cfg.Constraints.MaxChanSize()
32✔
566
        if availableFunds < chanSize {
32✔
UNCOV
567
                chanSize = availableFunds
×
568
        }
×
569

570
        if chanSize < a.cfg.Constraints.MinChanSize() {
32✔
571
                return fmt.Errorf("not enough funds available to open a " +
×
572
                        "single channel")
×
573
        }
×
574

575
        // We're to attempt an attachment so we'll obtain the set of
576
        // nodes that we currently have channels with so we avoid
577
        // duplicate edges.
578
        a.chanStateMtx.Lock()
32✔
579
        connectedNodes := a.chanState.ConnectedNodes()
32✔
580
        a.chanStateMtx.Unlock()
32✔
581

32✔
582
        for nID := range connectedNodes {
32✔
583
                log.Tracef("Skipping node %x with open channel", nID[:])
×
584
        }
×
585

586
        a.pendingMtx.Lock()
32✔
587

32✔
588
        for nID := range a.pendingOpens {
60✔
589
                log.Tracef("Skipping node %x with pending channel open", nID[:])
28✔
590
        }
28✔
591

592
        for nID := range a.pendingConns {
34✔
593
                log.Tracef("Skipping node %x with pending connection", nID[:])
2✔
594
        }
2✔
595

596
        for nID := range a.failedNodes {
34✔
597
                log.Tracef("Skipping failed node %v", nID[:])
2✔
598
        }
2✔
599

600
        nodesToSkip := mergeNodeMaps(a.pendingOpens,
32✔
601
                a.pendingConns, connectedNodes, a.failedNodes,
32✔
602
        )
32✔
603

32✔
604
        a.pendingMtx.Unlock()
32✔
605

32✔
606
        // Gather the set of all nodes in the graph, except those we
32✔
607
        // want to skip.
32✔
608
        selfPubBytes := a.cfg.Self.SerializeCompressed()
32✔
609
        nodes := make(map[NodeID]struct{})
32✔
610
        addresses := make(map[NodeID][]net.Addr)
32✔
611
        if err := a.cfg.Graph.ForEachNode(ctx, func(_ context.Context,
32✔
612
                node Node) error {
196✔
613

164✔
614
                nID := NodeID(node.PubKey())
164✔
615

164✔
616
                // If we come across ourselves, them we'll continue in
164✔
617
                // order to avoid attempting to make a channel with
164✔
618
                // ourselves.
164✔
619
                if bytes.Equal(nID[:], selfPubBytes) {
164✔
620
                        log.Tracef("Skipping self node %x", nID[:])
×
621
                        return nil
×
622
                }
×
623

624
                // If the node has no known addresses, we cannot connect to it,
625
                // so we'll skip it.
626
                addrs := node.Addrs()
164✔
627
                if len(addrs) == 0 {
164✔
UNCOV
628
                        log.Tracef("Skipping node %x since no addresses known",
×
629
                                nID[:])
×
630
                        return nil
×
631
                }
×
632
                addresses[nID] = addrs
164✔
633

164✔
634
                // Additionally, if this node is in the blacklist, then
164✔
635
                // we'll skip it.
164✔
636
                if _, ok := nodesToSkip[nID]; ok {
196✔
637
                        log.Tracef("Skipping blacklisted node %x", nID[:])
32✔
638
                        return nil
32✔
639
                }
32✔
640

641
                nodes[nID] = struct{}{}
132✔
642
                return nil
132✔
643
        }); err != nil {
×
UNCOV
644
                return fmt.Errorf("unable to get graph nodes: %w", err)
×
645
        }
×
646

647
        // Use the heuristic to calculate a score for each node in the
648
        // graph.
649
        log.Debugf("Scoring %d nodes for chan_size=%v", len(nodes), chanSize)
32✔
650
        scores, err := a.cfg.Heuristic.NodeScores(
32✔
651
                ctx, a.cfg.Graph, totalChans, chanSize, nodes,
32✔
652
        )
32✔
653
        if err != nil {
34✔
654
                return fmt.Errorf("unable to calculate node scores : %w", err)
2✔
655
        }
2✔
656

657
        log.Debugf("Got scores for %d nodes", len(scores))
30✔
658

30✔
659
        // Now use the score to make a weighted choice which nodes to attempt
30✔
660
        // to open channels to.
30✔
661
        scores, err = chooseN(numChans, scores)
30✔
662
        if err != nil {
30✔
663
                return fmt.Errorf("unable to make weighted choice: %w",
×
664
                        err)
×
665
        }
×
666

667
        chanCandidates := make(map[NodeID]*AttachmentDirective)
30✔
668
        for nID := range scores {
82✔
669
                log.Tracef("Creating attachment directive for chosen node %x",
52✔
670
                        nID[:])
52✔
671

52✔
672
                // Track the available funds we have left.
52✔
673
                if availableFunds < chanSize {
56✔
674
                        chanSize = availableFunds
4✔
675
                }
4✔
676
                availableFunds -= chanSize
52✔
677

52✔
678
                // If we run out of funds, we can break early.
52✔
679
                if chanSize < a.cfg.Constraints.MinChanSize() {
54✔
680
                        log.Tracef("Chan size %v too small to satisfy min "+
2✔
681
                                "channel size %v, breaking", chanSize,
2✔
682
                                a.cfg.Constraints.MinChanSize())
2✔
683
                        break
2✔
684
                }
685

686
                chanCandidates[nID] = &AttachmentDirective{
50✔
687
                        NodeID:  nID,
50✔
688
                        ChanAmt: chanSize,
50✔
689
                        Addrs:   addresses[nID],
50✔
690
                }
50✔
691
        }
692

693
        if len(chanCandidates) == 0 {
38✔
694
                log.Infof("No eligible candidates to connect to")
8✔
695
                return nil
8✔
696
        }
8✔
697

698
        log.Infof("Attempting to execute channel attachment "+
22✔
699
                "directives: %v", spew.Sdump(chanCandidates))
22✔
700

22✔
701
        // Before proceeding, check to see if we have any slots
22✔
702
        // available to open channels. If there are any, we will attempt
22✔
703
        // to dispatch the retrieved directives since we can't be
22✔
704
        // certain which ones may actually succeed. If too many
22✔
705
        // connections succeed, they will be ignored and made
22✔
706
        // available to future heuristic selections.
22✔
707
        a.pendingMtx.Lock()
22✔
708
        defer a.pendingMtx.Unlock()
22✔
709
        if uint16(len(a.pendingOpens)) >= a.cfg.Constraints.MaxPendingOpens() {
22✔
710
                log.Debugf("Reached cap of %v pending "+
×
711
                        "channel opens, will retry "+
×
712
                        "after success/failure",
×
713
                        a.cfg.Constraints.MaxPendingOpens())
×
714
                return nil
×
UNCOV
715
        }
×
716

717
        // For each recommended attachment directive, we'll launch a
718
        // new goroutine to attempt to carry out the directive. If any
719
        // of these succeed, then we'll receive a new state update,
720
        // taking us back to the top of our controller loop.
721
        for _, chanCandidate := range chanCandidates {
72✔
722
                // Skip candidates which we are already trying
50✔
723
                // to establish a connection with.
50✔
724
                nodeID := chanCandidate.NodeID
50✔
725
                if _, ok := a.pendingConns[nodeID]; ok {
50✔
UNCOV
726
                        continue
×
727
                }
728
                a.pendingConns[nodeID] = struct{}{}
50✔
729

50✔
730
                a.wg.Add(1)
50✔
731
                go a.executeDirective(*chanCandidate)
50✔
732
        }
733
        return nil
22✔
734
}
735

736
// executeDirective attempts to connect to the channel candidate specified by
737
// the given attachment directive, and open a channel of the given size.
738
//
739
// NOTE: MUST be run as a goroutine.
740
func (a *Agent) executeDirective(directive AttachmentDirective) {
50✔
741
        defer a.wg.Done()
50✔
742

50✔
743
        // We'll start out by attempting to connect to the peer in order to
50✔
744
        // begin the funding workflow.
50✔
745
        nodeID := directive.NodeID
50✔
746
        pub, err := btcec.ParsePubKey(nodeID[:])
50✔
747
        if err != nil {
50✔
748
                log.Errorf("Unable to parse pubkey %x: %v", nodeID, err)
×
749
                return
×
750
        }
×
751

752
        connected := make(chan bool)
50✔
753
        errChan := make(chan error)
50✔
754

50✔
755
        // To ensure a call to ConnectToPeer doesn't block the agent from
50✔
756
        // shutting down, we'll launch it in a non-waitgrouped goroutine, that
50✔
757
        // will signal when a result is returned.
50✔
758
        // TODO(halseth): use DialContext to cancel on transport level.
50✔
759
        go func() {
100✔
760
                alreadyConnected, err := a.cfg.ConnectToPeer(
50✔
761
                        pub, directive.Addrs,
50✔
762
                )
50✔
763
                if err != nil {
56✔
764
                        select {
6✔
765
                        case errChan <- err:
2✔
766
                        case <-a.quit:
4✔
767
                        }
768
                        return
6✔
769
                }
770

771
                select {
44✔
772
                case connected <- alreadyConnected:
44✔
UNCOV
773
                case <-a.quit:
×
774
                        return
×
775
                }
776
        }()
777

778
        var alreadyConnected bool
50✔
779
        select {
50✔
780
        case alreadyConnected = <-connected:
44✔
781
        case err = <-errChan:
2✔
782
        case <-a.quit:
4✔
783
                return
4✔
784
        }
785

786
        if err != nil {
48✔
787
                log.Warnf("Unable to connect to %x: %v",
2✔
788
                        pub.SerializeCompressed(), err)
2✔
789

2✔
790
                // Since we failed to connect to them, we'll mark them as
2✔
791
                // failed so that we don't attempt to connect to them again.
2✔
792
                a.pendingMtx.Lock()
2✔
793
                delete(a.pendingConns, nodeID)
2✔
794
                a.failedNodes[nodeID] = struct{}{}
2✔
795
                a.pendingMtx.Unlock()
2✔
796

2✔
797
                // Finally, we'll trigger the agent to select new peers to
2✔
798
                // connect to.
2✔
799
                a.OnChannelOpenFailure()
2✔
800

2✔
801
                return
2✔
802
        }
2✔
803

804
        // The connection was successful, though before progressing we must
805
        // check that we have not already met our quota for max pending open
806
        // channels. This can happen if multiple directives were spawned but
807
        // fewer slots were available, and other successful attempts finished
808
        // first.
809
        a.pendingMtx.Lock()
44✔
810
        if uint16(len(a.pendingOpens)) >= a.cfg.Constraints.MaxPendingOpens() {
44✔
811
                // Since we've reached our max number of pending opens, we'll
×
UNCOV
812
                // disconnect this peer and exit. However, if we were
×
813
                // previously connected to them, then we'll make sure to
×
814
                // maintain the connection alive.
×
815
                if alreadyConnected {
×
816
                        // Since we succeeded in connecting, we won't add this
×
817
                        // peer to the failed nodes map, but we will remove it
×
UNCOV
818
                        // from a.pendingConns so that it can be retried in the
×
UNCOV
819
                        // future.
×
UNCOV
820
                        delete(a.pendingConns, nodeID)
×
UNCOV
821
                        a.pendingMtx.Unlock()
×
822
                        return
×
823
                }
×
824

UNCOV
825
                err = a.cfg.DisconnectPeer(pub)
×
UNCOV
826
                if err != nil {
×
UNCOV
827
                        log.Warnf("Unable to disconnect peer %x: %v",
×
UNCOV
828
                                pub.SerializeCompressed(), err)
×
UNCOV
829
                }
×
830

831
                // Now that we have disconnected, we can remove this node from
832
                // our pending conns map, permitting subsequent connection
833
                // attempts.
834
                delete(a.pendingConns, nodeID)
×
835
                a.pendingMtx.Unlock()
×
836
                return
×
837
        }
838

839
        // If we were successful, we'll track this peer in our set of pending
840
        // opens. We do this here to ensure we don't stall on selecting new
841
        // peers if the connection attempt happens to take too long.
842
        delete(a.pendingConns, nodeID)
44✔
843
        a.pendingOpens[nodeID] = LocalChannel{
44✔
844
                Balance: directive.ChanAmt,
44✔
845
                Node:    nodeID,
44✔
846
        }
44✔
847
        a.pendingMtx.Unlock()
44✔
848

44✔
849
        // We can then begin the funding workflow with this peer.
44✔
850
        err = a.cfg.ChanController.OpenChannel(pub, directive.ChanAmt)
44✔
851
        if err != nil {
44✔
852
                log.Warnf("Unable to open channel to %x of %v: %v",
×
853
                        pub.SerializeCompressed(), directive.ChanAmt, err)
×
854

×
855
                // As the attempt failed, we'll clear the peer from the set of
×
856
                // pending opens and mark them as failed so we don't attempt to
×
857
                // open a channel to them again.
×
858
                a.pendingMtx.Lock()
×
859
                delete(a.pendingOpens, nodeID)
×
860
                a.failedNodes[nodeID] = struct{}{}
×
UNCOV
861
                a.pendingMtx.Unlock()
×
862

×
863
                // Trigger the agent to re-evaluate everything and possibly
×
864
                // retry with a different node.
×
865
                a.OnChannelOpenFailure()
×
866

×
UNCOV
867
                // Finally, we should also disconnect the peer if we weren't
×
UNCOV
868
                // already connected to them beforehand by an external
×
UNCOV
869
                // subsystem.
×
UNCOV
870
                if alreadyConnected {
×
UNCOV
871
                        return
×
UNCOV
872
                }
×
873

874
                err = a.cfg.DisconnectPeer(pub)
×
UNCOV
875
                if err != nil {
×
UNCOV
876
                        log.Warnf("Unable to disconnect peer %x: %v",
×
UNCOV
877
                                pub.SerializeCompressed(), err)
×
UNCOV
878
                }
×
879
        }
880

881
        // Since the channel open was successful and is currently pending,
882
        // we'll trigger the autopilot agent to query for more peers.
883
        // TODO(halseth): this triggers a new loop before all the new channels
884
        // are added to the pending channels map. Should add before executing
885
        // directive in goroutine?
886
        a.OnChannelPendingOpen()
44✔
887
}
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