forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent_test.go
1350 lines (1141 loc) · 38 KB
/
agent_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package autopilot
import (
"errors"
"fmt"
"net"
"sync"
"testing"
"time"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
"github.com/stretchr/testify/require"
)
type moreChansResp struct {
numMore uint32
amt btcutil.Amount
}
type moreChanArg struct {
chans []LocalChannel
balance btcutil.Amount
}
type mockConstraints struct {
moreChansResps chan moreChansResp
moreChanArgs chan moreChanArg
quit chan struct{}
}
func (m *mockConstraints) ChannelBudget(chans []LocalChannel,
balance btcutil.Amount) (btcutil.Amount, uint32) {
if m.moreChanArgs != nil {
moreChan := moreChanArg{
chans: chans,
balance: balance,
}
select {
case m.moreChanArgs <- moreChan:
case <-m.quit:
return 0, 0
}
}
select {
case resp := <-m.moreChansResps:
return resp.amt, resp.numMore
case <-m.quit:
return 0, 0
}
}
func (m *mockConstraints) MaxPendingOpens() uint16 {
return 10
}
func (m *mockConstraints) MinChanSize() btcutil.Amount {
return 1e7
}
func (m *mockConstraints) MaxChanSize() btcutil.Amount {
return 1e8
}
var _ AgentConstraints = (*mockConstraints)(nil)
type mockHeuristic struct {
nodeScoresResps chan map[NodeID]*NodeScore
nodeScoresArgs chan directiveArg
quit chan struct{}
}
type directiveArg struct {
graph ChannelGraph
amt btcutil.Amount
chans []LocalChannel
nodes map[NodeID]struct{}
}
func (m *mockHeuristic) Name() string {
return "mock"
}
func (m *mockHeuristic) NodeScores(g ChannelGraph, chans []LocalChannel,
chanSize btcutil.Amount, nodes map[NodeID]struct{}) (
map[NodeID]*NodeScore, error) {
if m.nodeScoresArgs != nil {
directive := directiveArg{
graph: g,
amt: chanSize,
chans: chans,
nodes: nodes,
}
select {
case m.nodeScoresArgs <- directive:
case <-m.quit:
return nil, errors.New("exiting")
}
}
select {
case resp := <-m.nodeScoresResps:
return resp, nil
case <-m.quit:
return nil, errors.New("exiting")
}
}
var _ AttachmentHeuristic = (*mockHeuristic)(nil)
type openChanIntent struct {
target *btcec.PublicKey
amt btcutil.Amount
private bool
}
type mockChanController struct {
openChanSignals chan openChanIntent
private bool
}
func (m *mockChanController) OpenChannel(target *btcec.PublicKey,
amt btcutil.Amount) error {
m.openChanSignals <- openChanIntent{
target: target,
amt: amt,
private: m.private,
}
return nil
}
func (m *mockChanController) CloseChannel(chanPoint *wire.OutPoint) error {
return nil
}
var _ ChannelController = (*mockChanController)(nil)
type testContext struct {
constraints *mockConstraints
heuristic *mockHeuristic
chanController ChannelController
graph testGraph
agent *Agent
walletBalance btcutil.Amount
quit chan struct{}
sync.Mutex
}
func setup(t *testing.T, initialChans []LocalChannel) *testContext {
t.Helper()
// First, we'll create all the dependencies that we'll need in order to
// create the autopilot agent.
self, err := randKey()
require.NoError(t, err, "unable to generate key")
quit := make(chan struct{})
heuristic := &mockHeuristic{
nodeScoresArgs: make(chan directiveArg),
nodeScoresResps: make(chan map[NodeID]*NodeScore),
quit: quit,
}
constraints := &mockConstraints{
moreChansResps: make(chan moreChansResp),
moreChanArgs: make(chan moreChanArg),
quit: quit,
}
chanController := &mockChanController{
openChanSignals: make(chan openChanIntent, 10),
}
memGraph, _ := newMemChanGraph(t)
// We'll keep track of the funds available to the agent, to make sure
// it correctly uses this value when querying the ChannelBudget.
var availableFunds btcutil.Amount = 10 * btcutil.SatoshiPerBitcoin
ctx := &testContext{
constraints: constraints,
heuristic: heuristic,
chanController: chanController,
graph: memGraph,
walletBalance: availableFunds,
quit: quit,
}
// With the dependencies we created, we can now create the initial
// agent itself.
testCfg := Config{
Self: self,
Heuristic: heuristic,
ChanController: chanController,
WalletBalance: func() (btcutil.Amount, error) {
ctx.Lock()
defer ctx.Unlock()
return ctx.walletBalance, nil
},
ConnectToPeer: func(*btcec.PublicKey, []net.Addr) (bool, error) {
return false, nil
},
DisconnectPeer: func(*btcec.PublicKey) error {
return nil
},
Graph: memGraph,
Constraints: constraints,
}
agent, err := New(testCfg, initialChans)
require.NoError(t, err, "unable to create agent")
ctx.agent = agent
// With the autopilot agent and all its dependencies we'll start the
// primary controller goroutine.
if err := agent.Start(); err != nil {
t.Fatalf("unable to start agent: %v", err)
}
t.Cleanup(func() {
// We must close quit before agent.Stop(), to make sure
// ChannelBudget won't block preventing the agent from exiting.
close(quit)
agent.Stop()
})
return ctx
}
// respondMoreChans consumes the moreChanArgs element and responds to the agent
// with the given moreChansResp.
func respondMoreChans(t *testing.T, testCtx *testContext, resp moreChansResp) {
t.Helper()
// The agent should now query the heuristic.
select {
case <-testCtx.constraints.moreChanArgs:
case <-time.After(time.Second * 3):
t.Fatalf("heuristic wasn't queried in time")
}
// We'll send the response.
select {
case testCtx.constraints.moreChansResps <- resp:
case <-time.After(time.Second * 10):
t.Fatalf("response wasn't sent in time")
}
}
// respondMoreChans consumes the nodeScoresArgs element and responds to the
// agent with the given node scores.
func respondNodeScores(t *testing.T, testCtx *testContext,
resp map[NodeID]*NodeScore) {
t.Helper()
// Send over an empty list of attachment directives, which should cause
// the agent to return to waiting on a new signal.
select {
case <-testCtx.heuristic.nodeScoresArgs:
case <-time.After(time.Second * 3):
t.Fatalf("node scores weren't queried in time")
}
select {
case testCtx.heuristic.nodeScoresResps <- resp:
case <-time.After(time.Second * 10):
t.Fatalf("node scores were not sent in time")
}
}
// TestAgentChannelOpenSignal tests that upon receipt of a chanOpenUpdate, then
// agent modifies its local state accordingly, and reconsults the heuristic.
func TestAgentChannelOpenSignal(t *testing.T) {
t.Parallel()
testCtx := setup(t, nil)
// We'll send an initial "no" response to advance the agent past its
// initial check.
respondMoreChans(t, testCtx, moreChansResp{0, 0})
// Next we'll signal a new channel being opened by the backing LN node,
// with a capacity of 1 BTC.
newChan := LocalChannel{
ChanID: randChanID(),
Balance: btcutil.SatoshiPerBitcoin,
}
testCtx.agent.OnChannelOpen(newChan)
// The agent should now query the heuristic in order to determine its
// next action as it local state has now been modified.
respondMoreChans(t, testCtx, moreChansResp{0, 0})
// At this point, the local state of the agent should
// have also been updated to reflect that the LN node
// now has an additional channel with one BTC.
if _, ok := testCtx.agent.chanState[newChan.ChanID]; !ok {
t.Fatalf("internal channel state wasn't updated")
}
// There shouldn't be a call to the Select method as we've returned
// "false" for NeedMoreChans above.
select {
// If this send success, then Select was erroneously called and the
// test should be failed.
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{}:
t.Fatalf("Select was called but shouldn't have been")
// This is the correct path as Select should've be called.
default:
}
}
// TestAgentHeuristicUpdateSignal tests that upon notification about a
// heuristic update, the agent reconsults the heuristic.
func TestAgentHeuristicUpdateSignal(t *testing.T) {
t.Parallel()
testCtx := setup(t, nil)
pub, err := testCtx.graph.addRandNode()
require.NoError(t, err, "unable to generate key")
// We'll send an initial "no" response to advance the agent past its
// initial check.
respondMoreChans(t, testCtx, moreChansResp{0, 0})
// Next we'll signal that one of the heuristics have been updated.
testCtx.agent.OnHeuristicUpdate(testCtx.heuristic)
// The update should trigger the agent to ask for a channel budget.so
// we'll respond that there is a budget for opening 1 more channel.
respondMoreChans(t, testCtx,
moreChansResp{
numMore: 1,
amt: 1 * btcutil.SatoshiPerBitcoin,
},
)
// At this point, the agent should now be querying the heuristic for
// scores. We'll respond.
nodeID := NewNodeID(pub)
scores := map[NodeID]*NodeScore{
nodeID: {
NodeID: nodeID,
Score: 0.5,
},
}
respondNodeScores(t, testCtx, scores)
// Finally, this should result in the agent opening a channel.
chanController := testCtx.chanController.(*mockChanController)
select {
case <-chanController.openChanSignals:
case <-time.After(time.Second * 10):
t.Fatalf("channel not opened in time")
}
}
// A mockFailingChanController always fails to open a channel.
type mockFailingChanController struct {
}
func (m *mockFailingChanController) OpenChannel(target *btcec.PublicKey,
amt btcutil.Amount) error {
return errors.New("failure")
}
func (m *mockFailingChanController) CloseChannel(chanPoint *wire.OutPoint) error {
return nil
}
var _ ChannelController = (*mockFailingChanController)(nil)
// TestAgentChannelFailureSignal tests that if an autopilot channel fails to
// open, the agent is signalled to make a new decision.
func TestAgentChannelFailureSignal(t *testing.T) {
t.Parallel()
testCtx := setup(t, nil)
testCtx.chanController = &mockFailingChanController{}
node, err := testCtx.graph.addRandNode()
require.NoError(t, err, "unable to add node")
// First ensure the agent will attempt to open a new channel. Return
// that we need more channels, and have 5BTC to use.
respondMoreChans(t, testCtx, moreChansResp{1, 5 * btcutil.SatoshiPerBitcoin})
// At this point, the agent should now be querying the heuristic to
// request attachment directives, return a fake so the agent will
// attempt to open a channel.
var fakeDirective = &NodeScore{
NodeID: NewNodeID(node),
Score: 0.5,
}
respondNodeScores(
t, testCtx, map[NodeID]*NodeScore{
NewNodeID(node): fakeDirective,
},
)
// At this point the agent will attempt to create a channel and fail.
// Now ensure that the controller loop is re-executed.
respondMoreChans(t, testCtx, moreChansResp{1, 5 * btcutil.SatoshiPerBitcoin})
respondNodeScores(t, testCtx, map[NodeID]*NodeScore{})
}
// TestAgentChannelCloseSignal ensures that once the agent receives an outside
// signal of a channel belonging to the backing LN node being closed, then it
// will query the heuristic to make its next decision.
func TestAgentChannelCloseSignal(t *testing.T) {
t.Parallel()
// We'll start the agent with two channels already being active.
initialChans := []LocalChannel{
{
ChanID: randChanID(),
Balance: btcutil.SatoshiPerBitcoin,
},
{
ChanID: randChanID(),
Balance: btcutil.SatoshiPerBitcoin * 2,
},
}
testCtx := setup(t, initialChans)
// We'll send an initial "no" response to advance the agent past its
// initial check.
respondMoreChans(t, testCtx, moreChansResp{0, 0})
// Next, we'll close both channels which should force the agent to
// re-query the heuristic.
testCtx.agent.OnChannelClose(initialChans[0].ChanID, initialChans[1].ChanID)
// The agent should now query the heuristic in order to determine its
// next action as it local state has now been modified.
respondMoreChans(t, testCtx, moreChansResp{0, 0})
// At this point, the local state of the agent should
// have also been updated to reflect that the LN node
// has no existing open channels.
if len(testCtx.agent.chanState) != 0 {
t.Fatalf("internal channel state wasn't updated")
}
// There shouldn't be a call to the Select method as we've returned
// "false" for NeedMoreChans above.
select {
// If this send success, then Select was erroneously called and the
// test should be failed.
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{}:
t.Fatalf("Select was called but shouldn't have been")
// This is the correct path as Select should've be called.
default:
}
}
// TestAgentBalanceUpdateIncrease ensures that once the agent receives an
// outside signal concerning a balance update, then it will re-query the
// heuristic to determine its next action.
func TestAgentBalanceUpdate(t *testing.T) {
t.Parallel()
testCtx := setup(t, nil)
// We'll send an initial "no" response to advance the agent past its
// initial check.
respondMoreChans(t, testCtx, moreChansResp{0, 0})
// Next we'll send a new balance update signal to the agent, adding 5
// BTC to the amount of available funds.
testCtx.Lock()
testCtx.walletBalance += btcutil.SatoshiPerBitcoin * 5
testCtx.Unlock()
testCtx.agent.OnBalanceChange()
// The agent should now query the heuristic in order to determine its
// next action as it local state has now been modified.
respondMoreChans(t, testCtx, moreChansResp{0, 0})
// At this point, the local state of the agent should
// have also been updated to reflect that the LN node
// now has an additional 5BTC available.
if testCtx.agent.totalBalance != testCtx.walletBalance {
t.Fatalf("expected %v wallet balance "+
"instead have %v", testCtx.agent.totalBalance,
testCtx.walletBalance)
}
// There shouldn't be a call to the Select method as we've returned
// "false" for NeedMoreChans above.
select {
// If this send success, then Select was erroneously called and the
// test should be failed.
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{}:
t.Fatalf("Select was called but shouldn't have been")
// This is the correct path as Select should've be called.
default:
}
}
// TestAgentImmediateAttach tests that if an autopilot agent is created, and it
// has enough funds available to create channels, then it does so immediately.
func TestAgentImmediateAttach(t *testing.T) {
t.Parallel()
testCtx := setup(t, nil)
const numChans = 5
// We'll generate 5 mock directives so it can progress within its loop.
directives := make(map[NodeID]*NodeScore)
nodeKeys := make(map[NodeID]struct{})
for i := 0; i < numChans; i++ {
pub, err := testCtx.graph.addRandNode()
if err != nil {
t.Fatalf("unable to generate key: %v", err)
}
nodeID := NewNodeID(pub)
directives[nodeID] = &NodeScore{
NodeID: nodeID,
Score: 0.5,
}
nodeKeys[nodeID] = struct{}{}
}
// The very first thing the agent should do is query the NeedMoreChans
// method on the passed heuristic. So we'll provide it with a response
// that will kick off the main loop.
respondMoreChans(t, testCtx,
moreChansResp{
numMore: numChans,
amt: 5 * btcutil.SatoshiPerBitcoin,
},
)
// At this point, the agent should now be querying the heuristic to
// requests attachment directives. With our fake directives created,
// we'll now send then to the agent as a return value for the Select
// function.
respondNodeScores(t, testCtx, directives)
// Finally, we should receive 5 calls to the OpenChannel method with
// the exact same parameters that we specified within the attachment
// directives.
chanController := testCtx.chanController.(*mockChanController)
for i := 0; i < numChans; i++ {
select {
case openChan := <-chanController.openChanSignals:
if openChan.amt != btcutil.SatoshiPerBitcoin {
t.Fatalf("invalid chan amt: expected %v, got %v",
btcutil.SatoshiPerBitcoin, openChan.amt)
}
nodeID := NewNodeID(openChan.target)
_, ok := nodeKeys[nodeID]
if !ok {
t.Fatalf("unexpected key: %v, not found",
nodeID)
}
delete(nodeKeys, nodeID)
case <-time.After(time.Second * 10):
t.Fatalf("channel not opened in time")
}
}
}
// TestAgentPrivateChannels ensure that only requests for private channels are
// sent if set.
func TestAgentPrivateChannels(t *testing.T) {
t.Parallel()
testCtx := setup(t, nil)
// The chanController should be initialized such that all of its open
// channel requests are for private channels.
testCtx.chanController.(*mockChanController).private = true
const numChans = 5
// We'll generate 5 mock directives so the pubkeys will be found in the
// agent's graph, and it can progress within its loop.
directives := make(map[NodeID]*NodeScore)
for i := 0; i < numChans; i++ {
pub, err := testCtx.graph.addRandNode()
if err != nil {
t.Fatalf("unable to generate key: %v", err)
}
directives[NewNodeID(pub)] = &NodeScore{
NodeID: NewNodeID(pub),
Score: 0.5,
}
}
// The very first thing the agent should do is query the NeedMoreChans
// method on the passed heuristic. So we'll provide it with a response
// that will kick off the main loop. We'll send over a response
// indicating that it should establish more channels, and give it a
// budget of 5 BTC to do so.
resp := moreChansResp{
numMore: numChans,
amt: 5 * btcutil.SatoshiPerBitcoin,
}
respondMoreChans(t, testCtx, resp)
// At this point, the agent should now be querying the heuristic to
// requests attachment directives. With our fake directives created,
// we'll now send then to the agent as a return value for the Select
// function.
respondNodeScores(t, testCtx, directives)
// Finally, we should receive 5 calls to the OpenChannel method, each
// specifying that it's for a private channel.
chanController := testCtx.chanController.(*mockChanController)
for i := 0; i < numChans; i++ {
select {
case openChan := <-chanController.openChanSignals:
if !openChan.private {
t.Fatal("expected open channel request to be private")
}
case <-time.After(10 * time.Second):
t.Fatal("channel not opened in time")
}
}
}
// TestAgentPendingChannelState ensures that the agent properly factors in its
// pending channel state when making decisions w.r.t if it needs more channels
// or not, and if so, who is eligible to open new channels to.
func TestAgentPendingChannelState(t *testing.T) {
t.Parallel()
testCtx := setup(t, nil)
// We'll only return a single directive for a pre-chosen node.
nodeKey, err := testCtx.graph.addRandNode()
require.NoError(t, err, "unable to generate key")
nodeID := NewNodeID(nodeKey)
nodeDirective := &NodeScore{
NodeID: nodeID,
Score: 0.5,
}
// Once again, we'll start by telling the agent as part of its first
// query, that it needs more channels and has 3 BTC available for
// attachment. We'll send over a response indicating that it should
// establish more channels, and give it a budget of 1 BTC to do so.
respondMoreChans(t, testCtx,
moreChansResp{
numMore: 1,
amt: btcutil.SatoshiPerBitcoin,
},
)
respondNodeScores(t, testCtx,
map[NodeID]*NodeScore{
nodeID: nodeDirective,
},
)
// A request to open the channel should've also been sent.
chanController := testCtx.chanController.(*mockChanController)
select {
case openChan := <-chanController.openChanSignals:
chanAmt := testCtx.constraints.MaxChanSize()
if openChan.amt != chanAmt {
t.Fatalf("invalid chan amt: expected %v, got %v",
chanAmt, openChan.amt)
}
if !openChan.target.IsEqual(nodeKey) {
t.Fatalf("unexpected key: expected %x, got %x",
nodeKey.SerializeCompressed(),
openChan.target.SerializeCompressed())
}
case <-time.After(time.Second * 10):
t.Fatalf("channel wasn't opened in time")
}
// Now, in order to test that the pending state was properly updated,
// we'll trigger a balance update in order to trigger a query to the
// heuristic.
testCtx.Lock()
testCtx.walletBalance += 0.4 * btcutil.SatoshiPerBitcoin
testCtx.Unlock()
testCtx.agent.OnBalanceChange()
// The heuristic should be queried, and the argument for the set of
// channels passed in should include the pending channels that
// should've been created above.
select {
// The request that we get should include a pending channel for the
// one that we just created, otherwise the agent isn't properly
// updating its internal state.
case req := <-testCtx.constraints.moreChanArgs:
chanAmt := testCtx.constraints.MaxChanSize()
if len(req.chans) != 1 {
t.Fatalf("should include pending chan in current "+
"state, instead have %v chans", len(req.chans))
}
if req.chans[0].Balance != chanAmt {
t.Fatalf("wrong chan balance: expected %v, got %v",
req.chans[0].Balance, chanAmt)
}
if req.chans[0].Node != nodeID {
t.Fatalf("wrong node ID: expected %x, got %x",
nodeID, req.chans[0].Node[:])
}
case <-time.After(time.Second * 10):
t.Fatalf("need more chans wasn't queried in time")
}
// We'll send across a response indicating that it *does* need more
// channels.
select {
case testCtx.constraints.moreChansResps <- moreChansResp{1, btcutil.SatoshiPerBitcoin}:
case <-time.After(time.Second * 10):
t.Fatalf("need more chans wasn't queried in time")
}
// The response above should prompt the agent to make a query to the
// Select method. The arguments passed should reflect the fact that the
// node we have a pending channel to, should be ignored.
select {
case req := <-testCtx.heuristic.nodeScoresArgs:
if len(req.chans) == 0 {
t.Fatalf("expected to skip %v nodes, instead "+
"skipping %v", 1, len(req.chans))
}
if req.chans[0].Node != nodeID {
t.Fatalf("pending node not included in skip arguments")
}
case <-time.After(time.Second * 10):
t.Fatalf("select wasn't queried in time")
}
}
// TestAgentPendingOpenChannel ensures that the agent queries its heuristic once
// it detects a channel is pending open. This allows the agent to use its own
// change outputs that have yet to confirm for funding transactions.
func TestAgentPendingOpenChannel(t *testing.T) {
t.Parallel()
testCtx := setup(t, nil)
// We'll send an initial "no" response to advance the agent past its
// initial check.
respondMoreChans(t, testCtx, moreChansResp{0, 0})
// Next, we'll signal that a new channel has been opened, but it is
// still pending.
testCtx.agent.OnChannelPendingOpen()
// The agent should now query the heuristic in order to determine its
// next action as its local state has now been modified.
respondMoreChans(t, testCtx, moreChansResp{0, 0})
// There shouldn't be a call to the Select method as we've returned
// "false" for NeedMoreChans above.
select {
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{}:
t.Fatalf("Select was called but shouldn't have been")
default:
}
}
// TestAgentOnNodeUpdates tests that the agent will wake up in response to the
// OnNodeUpdates signal. This is useful in ensuring that autopilot is always
// pulling in the latest graph updates into its decision making. It also
// prevents the agent from stalling after an initial attempt that finds no nodes
// in the graph.
func TestAgentOnNodeUpdates(t *testing.T) {
t.Parallel()
testCtx := setup(t, nil)
// We'll send an initial "yes" response to advance the agent past its
// initial check. This will cause it to try to get directives from an
// empty graph.
respondMoreChans(
t, testCtx,
moreChansResp{
numMore: 2,
amt: testCtx.walletBalance,
},
)
// Send over an empty list of attachment directives, which should cause
// the agent to return to waiting on a new signal.
respondNodeScores(t, testCtx, map[NodeID]*NodeScore{})
// Simulate more nodes being added to the graph by informing the agent
// that we have node updates.
testCtx.agent.OnNodeUpdates()
// In response, the agent should wake up and see if it needs more
// channels. Since we haven't done anything, we will send the same
// response as before since we are still trying to open channels.
respondMoreChans(
t, testCtx,
moreChansResp{
numMore: 2,
amt: testCtx.walletBalance,
},
)
// Again the agent should pull in the next set of attachment directives.
// It's not important that this list is also empty, so long as the node
// updates signal is causing the agent to make this attempt.
respondNodeScores(t, testCtx, map[NodeID]*NodeScore{})
}
// TestAgentSkipPendingConns asserts that the agent will not try to make
// duplicate connection requests to the same node, even if the attachment
// heuristic instructs the agent to do so. It also asserts that the agent
// stops tracking the pending connection once it finishes. Note that in
// practice, a failed connection would be inserted into the skip map passed to
// the attachment heuristic, though this does not assert that case.
func TestAgentSkipPendingConns(t *testing.T) {
t.Parallel()
testCtx := setup(t, nil)
connect := make(chan chan error)
testCtx.agent.cfg.ConnectToPeer = func(*btcec.PublicKey, []net.Addr) (bool, error) {
errChan := make(chan error)
select {
case connect <- errChan:
case <-testCtx.quit:
return false, errors.New("quit")
}
select {
case err := <-errChan:
return false, err
case <-testCtx.quit:
return false, errors.New("quit")
}
}
// We'll only return a single directive for a pre-chosen node.
nodeKey, err := testCtx.graph.addRandNode()
require.NoError(t, err, "unable to generate key")
nodeID := NewNodeID(nodeKey)
nodeDirective := &NodeScore{
NodeID: nodeID,
Score: 0.5,
}
// We'll also add a second node to the graph, to keep the first one
// company.
nodeKey2, err := testCtx.graph.addRandNode()
require.NoError(t, err, "unable to generate key")
nodeID2 := NewNodeID(nodeKey2)
// We'll send an initial "yes" response to advance the agent past its
// initial check. This will cause it to try to get directives from the
// graph.
respondMoreChans(t, testCtx,
moreChansResp{
numMore: 1,
amt: testCtx.walletBalance,
},
)
// Both nodes should be part of the arguments.
select {
case req := <-testCtx.heuristic.nodeScoresArgs:
if len(req.nodes) != 2 {
t.Fatalf("expected %v nodes, instead "+
"had %v", 2, len(req.nodes))
}
if _, ok := req.nodes[nodeID]; !ok {
t.Fatalf("node not included in arguments")
}
if _, ok := req.nodes[nodeID2]; !ok {
t.Fatalf("node not included in arguments")
}
case <-time.After(time.Second * 10):
t.Fatalf("select wasn't queried in time")
}
// Respond with a scored directive. We skip node2 for now, implicitly
// giving it a zero-score.
select {
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{
NewNodeID(nodeKey): nodeDirective,
}:
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
// The agent should attempt connection to the node.
var errChan chan error
select {
case errChan = <-connect:
case <-time.After(time.Second * 10):
t.Fatalf("agent did not attempt connection")
}
// Signal the agent to go again, now that we've tried to connect.
testCtx.agent.OnNodeUpdates()
// The heuristic again informs the agent that we need more channels.
respondMoreChans(t, testCtx,
moreChansResp{
numMore: 1,
amt: testCtx.walletBalance,
},
)
// Since the node now has a pending connection, it should be skipped
// and not part of the nodes attempting to be scored.
select {
case req := <-testCtx.heuristic.nodeScoresArgs:
if len(req.nodes) != 1 {
t.Fatalf("expected %v nodes, instead "+
"had %v", 1, len(req.nodes))
}
if _, ok := req.nodes[nodeID2]; !ok {
t.Fatalf("node not included in arguments")
}
case <-time.After(time.Second * 10):
t.Fatalf("select wasn't queried in time")
}
// Respond with an emtpty score set.
select {
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{}:
case <-time.After(time.Second * 10):
t.Fatalf("heuristic wasn't queried in time")
}
// The agent should not attempt any connection, since no nodes were
// scored.
select {
case <-connect:
t.Fatalf("agent should not have attempted connection")
case <-time.After(time.Second * 3):
}
// Now, timeout the original request, which should still be waiting for
// a response.
select {
case errChan <- fmt.Errorf("connection timeout"):
case <-time.After(time.Second * 10):
t.Fatalf("agent did not receive connection timeout")
}
// The agent will now retry since the last connection attempt failed.
// The heuristic again informs the agent that we need more channels.
respondMoreChans(t, testCtx,
moreChansResp{
numMore: 1,
amt: testCtx.walletBalance,
},
)
// The node should now be marked as "failed", which should make it
// being skipped during scoring. Again check that it won't be among the
// score request.
select {
case req := <-testCtx.heuristic.nodeScoresArgs:
if len(req.nodes) != 1 {
t.Fatalf("expected %v nodes, instead "+
"had %v", 1, len(req.nodes))
}
if _, ok := req.nodes[nodeID2]; !ok {
t.Fatalf("node not included in arguments")
}
case <-time.After(time.Second * 10):
t.Fatalf("select wasn't queried in time")
}
// Send a directive for the second node.
nodeDirective2 := &NodeScore{
NodeID: nodeID2,
Score: 0.5,
}
select {
case testCtx.heuristic.nodeScoresResps <- map[NodeID]*NodeScore{
nodeID2: nodeDirective2,
}:
case <-time.After(time.Second * 10):