@@ -526,3 +526,154 @@ func TestHasPeer(t *testing.T) {
526
526
require .False (t , found )
527
527
require .Equal (t , peerStatusRestricted , access )
528
528
}
529
+
530
+ // TestAddPeerAccessInbound asserts the num of slots is correctly incremented
531
+ // only for a new inbound peer with restricted access.
532
+ func TestAddPeerAccessInbound (t * testing.T ) {
533
+ t .Parallel ()
534
+
535
+ // Create a testing accessMan.
536
+ a := & accessMan {
537
+ peerCounts : make (map [string ]channeldb.ChanCount ),
538
+ peerScores : make (map [string ]peerSlotStatus ),
539
+ }
540
+
541
+ // Create a testing key.
542
+ priv , err := btcec .NewPrivateKey ()
543
+ require .NoError (t , err )
544
+ pub := priv .PubKey ()
545
+ pubStr := string (pub .SerializeCompressed ())
546
+
547
+ // Add this peer as an inbound peer with peerStatusRestricted.
548
+ a .addPeerAccess (pub , peerStatusRestricted , true )
549
+
550
+ // Assert the accessMan's internal state.
551
+ //
552
+ // We expect to see one peer found in the score map, and one slot is
553
+ // taken, and this peer is not found in the counts map.
554
+ require .Len (t , a .peerScores , 1 )
555
+ require .Equal (t , int64 (1 ), a .numRestricted )
556
+ require .NotContains (t , a .peerCounts , pubStr )
557
+
558
+ // The peer should be found in the score map.
559
+ score , ok := a .peerScores [pubStr ]
560
+ require .True (t , ok )
561
+
562
+ expecedScore := peerSlotStatus {state : peerStatusRestricted }
563
+ require .Equal (t , expecedScore , score )
564
+
565
+ // Add this peer again, we expect the available slots to stay unchanged.
566
+ a .addPeerAccess (pub , peerStatusRestricted , true )
567
+
568
+ // Assert the internal state is not changed.
569
+ require .Len (t , a .peerScores , 1 )
570
+ require .Equal (t , int64 (1 ), a .numRestricted )
571
+ require .NotContains (t , a .peerCounts , pubStr )
572
+
573
+ // Reset the accessMan.
574
+ a = & accessMan {
575
+ peerCounts : make (map [string ]channeldb.ChanCount ),
576
+ peerScores : make (map [string ]peerSlotStatus ),
577
+ }
578
+
579
+ // Add this peer as an inbound peer with peerStatusTemporary.
580
+ a .addPeerAccess (pub , peerStatusTemporary , true )
581
+
582
+ // Assert the accessMan's internal state.
583
+ //
584
+ // We expect to see one peer found in the score map, and no slot is
585
+ // taken since this peer is not restricted.
586
+ require .Len (t , a .peerScores , 1 )
587
+ require .Equal (t , int64 (0 ), a .numRestricted )
588
+
589
+ // NOTE: in reality this is not possible as the peer must have been put
590
+ // into the map `peerCounts` before its perm can be upgraded.
591
+ require .NotContains (t , a .peerCounts , pubStr )
592
+
593
+ // The peer should be found in the score map.
594
+ score , ok = a .peerScores [pubStr ]
595
+ require .True (t , ok )
596
+
597
+ expecedScore = peerSlotStatus {state : peerStatusTemporary }
598
+ require .Equal (t , expecedScore , score )
599
+ }
600
+
601
+ // TestAddPeerAccessOutbound asserts that outbound peer is not restricted and
602
+ // its perm is upgraded when it has peerStatusRestricted.
603
+ func TestAddPeerAccessOutbound (t * testing.T ) {
604
+ t .Parallel ()
605
+
606
+ // Create a testing accessMan.
607
+ a := & accessMan {
608
+ peerCounts : make (map [string ]channeldb.ChanCount ),
609
+ peerScores : make (map [string ]peerSlotStatus ),
610
+ }
611
+
612
+ // Create a testing key.
613
+ priv , err := btcec .NewPrivateKey ()
614
+ require .NoError (t , err )
615
+ pub := priv .PubKey ()
616
+ pubStr := string (pub .SerializeCompressed ())
617
+
618
+ // Add this peer as an outbound peer with peerStatusRestricted.
619
+ a .addPeerAccess (pub , peerStatusRestricted , false )
620
+
621
+ // Assert the accessMan's internal state.
622
+ //
623
+ // We expect to see one peer found in the score map, and no slot is
624
+ // taken, and this peer is found in the counts map.
625
+ require .Len (t , a .peerScores , 1 )
626
+ require .Equal (t , int64 (0 ), a .numRestricted )
627
+ require .Contains (t , a .peerCounts , pubStr )
628
+
629
+ // The peer should be found in the score map.
630
+ score , ok := a .peerScores [pubStr ]
631
+ require .True (t , ok )
632
+
633
+ // Its perm should be upgraded to temporary.
634
+ expecedScore := peerSlotStatus {state : peerStatusTemporary }
635
+ require .Equal (t , expecedScore , score )
636
+
637
+ // The peer should be found in the peer counts map.
638
+ count , ok := a .peerCounts [pubStr ]
639
+ require .True (t , ok )
640
+
641
+ // The peer's count should be initialized correctly.
642
+ require .Zero (t , count .PendingOpenCount )
643
+ require .False (t , count .HasOpenOrClosedChan )
644
+
645
+ // Add this peer again, we expect the available slots to stay unchanged.
646
+ a .addPeerAccess (pub , peerStatusRestricted , true )
647
+
648
+ // Assert the internal state is not changed.
649
+ require .Len (t , a .peerScores , 1 )
650
+ require .Equal (t , int64 (0 ), a .numRestricted )
651
+ require .Contains (t , a .peerCounts , pubStr )
652
+
653
+ // Reset the accessMan.
654
+ a = & accessMan {
655
+ peerCounts : make (map [string ]channeldb.ChanCount ),
656
+ peerScores : make (map [string ]peerSlotStatus ),
657
+ }
658
+
659
+ // Add this peer as an inbound peer with peerStatusTemporary.
660
+ a .addPeerAccess (pub , peerStatusTemporary , true )
661
+
662
+ // Assert the accessMan's internal state.
663
+ //
664
+ // We expect to see one peer found in the score map, and no slot is
665
+ // taken since this peer is not restricted.
666
+ require .Len (t , a .peerScores , 1 )
667
+ require .Equal (t , int64 (0 ), a .numRestricted )
668
+
669
+ // NOTE: in reality this is not possible as the peer must have been put
670
+ // into the map `peerCounts` before its perm can be upgraded.
671
+ require .NotContains (t , a .peerCounts , pubStr )
672
+
673
+ // The peer should be found in the score map.
674
+ score , ok = a .peerScores [pubStr ]
675
+ require .True (t , ok )
676
+
677
+ expecedScore = peerSlotStatus {state : peerStatusTemporary }
678
+ require .Equal (t , expecedScore , score )
679
+ }
0 commit comments