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

lightningnetwork / lnd / 16217691934

11 Jul 2025 10:19AM UTC coverage: 67.331% (+0.006%) from 67.325%
16217691934

Pull #10068

github

web-flow
Merge d6378e50e into 6b326152d
Pull Request #10068: multi: let all V1Store `ForEach*` methods take a `reset` call-back

137 of 188 new or added lines in 18 files covered. (72.87%)

66 existing lines in 18 files now uncovered.

135368 of 201048 relevant lines covered (67.33%)

21713.13 hits per line

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

80.16
/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 {
34✔
74
        chans := make([]LocalChannel, 0, len(c))
34✔
75
        for _, channel := range c {
37✔
76
                chans = append(chans, channel)
3✔
77
        }
3✔
78
        return chans
34✔
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 {
34✔
392

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

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

34✔
398
        for _, pendingChan := range pendingChans {
85✔
399
                totalChans = append(totalChans, pendingChan)
51✔
400
        }
51✔
401

402
        return totalChans
34✔
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() {
44✔
421
                newBalance, err := a.cfg.WalletBalance()
31✔
422
                if err != nil {
31✔
423
                        log.Warnf("unable to update wallet balance: %v", err)
×
424
                        return
×
425
                }
×
426

427
                a.totalBalance = newBalance
31✔
428
        }
429

430
        // TODO(roasbeef): add 10-minute wake up timer
431
        for {
60✔
432
                select {
47✔
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:
14✔
477
                        updateBalance()
14✔
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:
8✔
511
                        return
8✔
512

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

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

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

34✔
530
                // Now that we've updated our internal state, we'll consult our
34✔
531
                // channel attachment heuristic to determine if we can open
34✔
532
                // up any additional channels while staying within our
34✔
533
                // constraints.
34✔
534
                availableFunds, numChans := a.cfg.Constraints.ChannelBudget(
34✔
535
                        totalChans, a.totalBalance,
34✔
536
                )
34✔
537
                switch {
34✔
538
                case numChans == 0:
18✔
539
                        continue
18✔
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✔
NEW
643
        }, func() {
×
NEW
644
                nodes = nil
×
NEW
645
                addresses = nil
×
646
        }); err != nil {
×
647
                return fmt.Errorf("unable to get graph nodes: %w", err)
×
648
        }
×
649

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

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

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

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

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

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

689
                chanCandidates[nID] = &AttachmentDirective{
25✔
690
                        NodeID:  nID,
25✔
691
                        ChanAmt: chanSize,
25✔
692
                        Addrs:   addresses[nID],
25✔
693
                }
25✔
694
        }
695

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

701
        log.Infof("Attempting to execute channel attachment "+
11✔
702
                "directives: %v", spew.Sdump(chanCandidates))
11✔
703

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

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

25✔
733
                a.wg.Add(1)
25✔
734
                go a.executeDirective(*chanCandidate)
25✔
735
        }
736
        return nil
11✔
737
}
738

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

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

755
        connected := make(chan bool)
25✔
756
        errChan := make(chan error)
25✔
757

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

774
                select {
22✔
775
                case connected <- alreadyConnected:
22✔
776
                case <-a.quit:
×
777
                        return
×
778
                }
779
        }()
780

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

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

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

1✔
800
                // Finally, we'll trigger the agent to select new peers to
1✔
801
                // connect to.
1✔
802
                a.OnChannelOpenFailure()
1✔
803

1✔
804
                return
1✔
805
        }
1✔
806

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

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

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

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

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

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

×
866
                // Trigger the agent to re-evaluate everything and possibly
×
867
                // retry with a different node.
×
868
                a.OnChannelOpenFailure()
×
869

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

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

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