@@ -6,6 +6,8 @@ import akka.actor.ActorSystem
6
6
import akka .testkit .TestProbe
7
7
import akka .util .ByteString
8
8
9
+ import scala .collection .mutable
10
+
9
11
import org .scalatest .flatspec .AnyFlatSpec
10
12
import org .scalatest .matchers .should .Matchers
11
13
import org .scalatest .prop .TableFor3
@@ -74,14 +76,14 @@ class PeersClientSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyC
74
76
}
75
77
}
76
78
77
- it should " determine the next best peer (same as bestPeer when lru set is empty)" in {
79
+ it should " determine the next best peer (same as bestPeer when lru set is empty)" in {
78
80
forAll(table) { (peerInfoMap, expectedPeer, _) =>
79
- PeersClient .nextBestPeer(peerInfoMap) shouldEqual expectedPeer
81
+ PeersClient .nextBestPeer(peerInfoMap, Set .empty ) shouldEqual expectedPeer
80
82
}
81
83
}
82
84
83
- it should " determine the next best peer when lru is used " in {
84
- val table = Table [Map [PeerId , PeerWithInfo ], Option [Peer ], Option [Peer ], String ](
85
+ it should " determine the next best peer with a different best block each time " in {
86
+ val table = Table [Map [PeerId , PeerWithInfo ], Option [PeerWithInfo ], Option [Peer ], String ](
85
87
(" PeerInfo map" , " Used best peer" , " Expected best peer" , " Scenario info (selected peer)" ),
86
88
(
87
89
Map (),
@@ -100,40 +102,31 @@ class PeersClientSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyC
100
102
peer1.id -> PeerWithInfo (peer1, peerInfo(0 , 100 , fork = false )),
101
103
peer2.id -> PeerWithInfo (peer2, peerInfo(0 , 50 , fork = true ))
102
104
),
103
- Some (peer2),
105
+ Some (PeerWithInfo ( peer2, peerInfo( 0 , 50 , fork = true )) ),
104
106
None ,
105
- " Peer2 with lower TD but following the ETC fork"
107
+ " Peer2 with lower TD but following the ETC fork, peer2 is already used "
106
108
),
107
109
(
108
110
Map (peer1.id -> PeerWithInfo (peer1, peerInfo(0 , 100 )), peer2.id -> PeerWithInfo (peer2, peerInfo(0 , 101 ))),
109
- Some (peer2),
110
- Some (peer1),
111
- " Peer2 with higher TD"
112
- ),
113
- (
114
- Map (
115
- peer1.id -> PeerWithInfo (peer1, peerInfo(0 , 100 )),
116
- peer2.id -> PeerWithInfo (peer2, peerInfo(0 , 101 )),
117
- peer3.id -> PeerWithInfo (peer3, peerInfo(1 , 50 ))
118
- ),
119
- Some (peer3),
120
- Some (peer2),
121
- " Peer3 with lower TD but higher checkpoint number"
111
+ Some (PeerWithInfo (peer2, peerInfo(0 , 101 ))),
112
+ None ,
113
+ " Both peer are used"
122
114
),
123
115
(
124
116
Map (
125
117
peer1.id -> PeerWithInfo (peer1, peerInfo(0 , 100 )),
126
- peer2.id -> PeerWithInfo (peer2, peerInfo(4 , 101 )),
127
- peer3.id -> PeerWithInfo (peer3, peerInfo(4 , 50 ))
118
+ peer2.id -> PeerWithInfo (peer2, peerInfo(1 , 50 )),
119
+ peer3.id -> PeerWithInfo (peer3, peerInfo(0 , 80 ).copy(bestBlockHash = ByteString ( " differenthash " ) ))
128
120
),
129
- Some (peer2),
121
+ Some (PeerWithInfo ( peer2, peerInfo( 1 , 50 )) ),
130
122
Some (peer3),
131
- " Peer2 with equal checkpoint number and higher TD "
123
+ " Peer2 with lower TD but higher checkpoint number, peer 1 and 2 are used "
132
124
)
133
125
)
134
- forAll(table) { (peerInfoMap, usedPeer, expectedPeer, _) =>
135
- usedPeer.map(PeersClient .activeFetchingNodes.add)
136
- PeersClient .nextBestPeer(peerInfoMap) shouldEqual expectedPeer
126
+ val activeFetchingNodes : mutable.Set [PeerWithInfo ] = mutable.Set .empty
127
+ forAll(table) { (peerInfoMap, usedPeerWithInfo, expectedPeer, _) =>
128
+ usedPeerWithInfo.map(activeFetchingNodes.add)
129
+ PeersClient .nextBestPeer(peerInfoMap, activeFetchingNodes.toSet) shouldEqual expectedPeer
137
130
}
138
131
}
139
132
0 commit comments