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

lightningnetwork / lnd / 15951470896

29 Jun 2025 04:23AM UTC coverage: 67.594% (-0.01%) from 67.606%
15951470896

Pull #9751

github

web-flow
Merge 599d9b051 into 6290edf14
Pull Request #9751: multi: update Go to 1.23.10 and update some packages

135088 of 199851 relevant lines covered (67.59%)

21909.44 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 {
32✔
74
        chans := make([]LocalChannel, 0, len(c))
32✔
75
        for _, channel := range c {
35✔
76
                chans = append(chans, channel)
3✔
77
        }
3✔
78
        return chans
32✔
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{} {
16✔
85
        nodes := make(map[NodeID]struct{})
16✔
86
        for _, channels := range c {
16✔
87
                nodes[channels.Node] = struct{}{}
×
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
16✔
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) {
13✔
181
        a := &Agent{
13✔
182
                cfg:                cfg,
13✔
183
                chanState:          make(map[lnwire.ShortChannelID]LocalChannel),
13✔
184
                quit:               make(chan struct{}),
13✔
185
                stateUpdates:       make(chan interface{}),
13✔
186
                balanceUpdates:     make(chan *balanceUpdate, 1),
13✔
187
                nodeUpdates:        make(chan *nodeUpdates, 1),
13✔
188
                chanOpenFailures:   make(chan *chanOpenFailureUpdate, 1),
13✔
189
                heuristicUpdates:   make(chan *heuristicUpdate, 1),
13✔
190
                pendingOpenUpdates: make(chan *chanPendingOpenUpdate, 1),
13✔
191
                failedNodes:        make(map[NodeID]struct{}),
13✔
192
                pendingConns:       make(map[NodeID]struct{}),
13✔
193
                pendingOpens:       make(map[NodeID]LocalChannel),
13✔
194
        }
13✔
195

13✔
196
        for _, c := range initialState {
15✔
197
                a.chanState[c.ChanID] = c
2✔
198
        }
2✔
199

200
        return a, nil
13✔
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 {
13✔
206
        var err error
13✔
207
        a.started.Do(func() {
26✔
208
                ctx, cancel := context.WithCancel(context.Background())
13✔
209
                a.cancel = fn.Some(cancel)
13✔
210

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

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

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

13✔
223
        return nil
13✔
224
}
13✔
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 {
14✔
229
        var err error
14✔
230
        a.stopped.Do(func() {
27✔
231
                err = a.stop()
13✔
232
        })
13✔
233
        return err
14✔
234
}
235

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

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

13✔
243
        return nil
13✔
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() {
15✔
287
        select {
15✔
288
        case a.balanceUpdates <- &balanceUpdate{}:
15✔
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() {
2✔
296
        select {
2✔
297
        case a.nodeUpdates <- &nodeUpdates{}:
2✔
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) {
1✔
305
        a.wg.Add(1)
1✔
306
        go func() {
2✔
307
                defer a.wg.Done()
1✔
308

1✔
309
                select {
1✔
310
                case a.stateUpdates <- &chanOpenUpdate{newChan: c}:
1✔
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() {
23✔
320
        select {
23✔
321
        case a.pendingOpenUpdates <- &chanPendingOpenUpdate{}:
14✔
322
        default:
9✔
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() {
1✔
330
        select {
1✔
331
        case a.chanOpenFailures <- &chanOpenFailureUpdate{}:
1✔
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) {
1✔
340
        a.wg.Add(1)
1✔
341
        go func() {
2✔
342
                defer a.wg.Done()
1✔
343

1✔
344
                select {
1✔
345
                case a.stateUpdates <- &chanCloseUpdate{closedChans: closedChans}:
1✔
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) {
1✔
354
        select {
1✔
355
        case a.heuristicUpdates <- &heuristicUpdate{
356
                heuristic: h,
357
        }:
1✔
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{} {
16✔
368

16✔
369
        numNodes := len(c)
16✔
370
        for _, skip := range skips {
64✔
371
                numNodes += len(skip)
48✔
372
        }
48✔
373

374
        res := make(map[NodeID]struct{}, numNodes)
16✔
375
        for nodeID := range c {
30✔
376
                res[nodeID] = struct{}{}
14✔
377
        }
14✔
378
        for _, skip := range skips {
64✔
379
                for nodeID := range skip {
50✔
380
                        res[nodeID] = struct{}{}
2✔
381
                }
2✔
382
        }
383

384
        return res
16✔
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 {
32✔
392

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

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

32✔
398
        for _, pendingChan := range pendingChans {
69✔
399
                totalChans = append(totalChans, pendingChan)
37✔
400
        }
37✔
401

402
        return totalChans
32✔
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) {
13✔
412
        defer a.wg.Done()
13✔
413

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

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

427
                a.totalBalance = newBalance
29✔
428
        }
429

430
        // TODO(roasbeef): add 10-minute wake up timer
431
        for {
58✔
432
                select {
45✔
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:
2✔
437
                        log.Infof("Processing new external signal")
2✔
438

2✔
439
                        switch update := signal.(type) {
2✔
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:
1✔
444
                                log.Debugf("New channel successfully opened, "+
1✔
445
                                        "updating state with: %v",
1✔
446
                                        spew.Sdump(update.newChan))
1✔
447

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

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

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

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

1✔
471
                                updateBalance()
1✔
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:
12✔
477
                        updateBalance()
12✔
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:
14✔
483
                        log.Debug("Applying external balance state update")
14✔
484

14✔
485
                        updateBalance()
14✔
486

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

1✔
493
                        updateBalance()
1✔
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:
2✔
499
                        log.Debugf("Node updates received, assessing " +
2✔
500
                                "need for more channels")
2✔
501

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

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

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

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

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

32✔
530
                // Now that we've updated our internal state, we'll consult our
32✔
531
                // channel attachment heuristic to determine if we can open
32✔
532
                // up any additional channels while staying within our
32✔
533
                // constraints.
32✔
534
                availableFunds, numChans := a.cfg.Constraints.ChannelBudget(
32✔
535
                        totalChans, a.totalBalance,
32✔
536
                )
32✔
537
                switch {
32✔
538
                case numChans == 0:
16✔
539
                        continue
16✔
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():
×
546
                        continue
×
547
                }
548

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

16✔
552
                err := a.openChans(ctx, availableFunds, numChans, totalChans)
16✔
553
                if err != nil {
17✔
554
                        log.Errorf("Unable to open channels: %v", err)
1✔
555
                }
1✔
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 {
16✔
563

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

570
        if chanSize < a.cfg.Constraints.MinChanSize() {
16✔
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()
16✔
579
        connectedNodes := a.chanState.ConnectedNodes()
16✔
580
        a.chanStateMtx.Unlock()
16✔
581

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

586
        a.pendingMtx.Lock()
16✔
587

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

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

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

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

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

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

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

82✔
616
                // If we come across ourselves, them we'll continue in
82✔
617
                // order to avoid attempting to make a channel with
82✔
618
                // ourselves.
82✔
619
                if bytes.Equal(nID[:], selfPubBytes) {
82✔
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()
82✔
627
                if len(addrs) == 0 {
82✔
628
                        log.Tracef("Skipping node %x since no addresses known",
×
629
                                nID[:])
×
630
                        return nil
×
631
                }
×
632
                addresses[nID] = addrs
82✔
633

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

641
                nodes[nID] = struct{}{}
66✔
642
                return nil
66✔
643
        }); err != nil {
×
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)
16✔
650
        scores, err := a.cfg.Heuristic.NodeScores(
16✔
651
                ctx, a.cfg.Graph, totalChans, chanSize, nodes,
16✔
652
        )
16✔
653
        if err != nil {
17✔
654
                return fmt.Errorf("unable to calculate node scores : %w", err)
1✔
655
        }
1✔
656

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

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

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

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

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

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

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

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

11✔
701
        // Before proceeding, check to see if we have any slots
11✔
702
        // available to open channels. If there are any, we will attempt
11✔
703
        // to dispatch the retrieved directives since we can't be
11✔
704
        // certain which ones may actually succeed. If too many
11✔
705
        // connections succeed, they will be ignored and made
11✔
706
        // available to future heuristic selections.
11✔
707
        a.pendingMtx.Lock()
11✔
708
        defer a.pendingMtx.Unlock()
11✔
709
        if uint16(len(a.pendingOpens)) >= a.cfg.Constraints.MaxPendingOpens() {
11✔
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
×
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 {
36✔
722
                // Skip candidates which we are already trying
25✔
723
                // to establish a connection with.
25✔
724
                nodeID := chanCandidate.NodeID
25✔
725
                if _, ok := a.pendingConns[nodeID]; ok {
25✔
726
                        continue
×
727
                }
728
                a.pendingConns[nodeID] = struct{}{}
25✔
729

25✔
730
                a.wg.Add(1)
25✔
731
                go a.executeDirective(*chanCandidate)
25✔
732
        }
733
        return nil
11✔
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) {
25✔
741
        defer a.wg.Done()
25✔
742

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

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

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

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

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

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

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

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

1✔
801
                return
1✔
802
        }
1✔
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()
22✔
810
        if uint16(len(a.pendingOpens)) >= a.cfg.Constraints.MaxPendingOpens() {
22✔
811
                // Since we've reached our max number of pending opens, we'll
×
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
×
818
                        // from a.pendingConns so that it can be retried in the
×
819
                        // future.
×
820
                        delete(a.pendingConns, nodeID)
×
821
                        a.pendingMtx.Unlock()
×
822
                        return
×
823
                }
×
824

825
                err = a.cfg.DisconnectPeer(pub)
×
826
                if err != nil {
×
827
                        log.Warnf("Unable to disconnect peer %x: %v",
×
828
                                pub.SerializeCompressed(), err)
×
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)
22✔
843
        a.pendingOpens[nodeID] = LocalChannel{
22✔
844
                Balance: directive.ChanAmt,
22✔
845
                Node:    nodeID,
22✔
846
        }
22✔
847
        a.pendingMtx.Unlock()
22✔
848

22✔
849
        // We can then begin the funding workflow with this peer.
22✔
850
        err = a.cfg.ChanController.OpenChannel(pub, directive.ChanAmt)
22✔
851
        if err != nil {
22✔
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{}{}
×
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

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

874
                err = a.cfg.DisconnectPeer(pub)
×
875
                if err != nil {
×
876
                        log.Warnf("Unable to disconnect peer %x: %v",
×
877
                                pub.SerializeCompressed(), err)
×
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()
22✔
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