Skip to content
This repository was archived by the owner on Apr 13, 2022. It is now read-only.

Commit 90d55c1

Browse files
committed
[refer #393] Add more comments
1 parent d7efdbf commit 90d55c1

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/main/scala/scorex/core/network/peer/PeerManager.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ object PeerManager {
149149
sc: ScorexContext): Map[InetSocketAddress, PeerInfo] = knownPeers
150150
}
151151

152+
// Extract /16 IPv4 prefix (IP group)
152153
private def getIpGroup(addr : InetSocketAddress) : Int = {
153154
val ip = addr.getAddress.getAddress
154155
val group = ((ip(0) & 0xFF) << 8) | (ip(1) & 0xFF)
@@ -160,12 +161,15 @@ object PeerManager {
160161
override def choose(knownPeers: Map[InetSocketAddress, PeerInfo],
161162
blacklistedPeers: Seq[InetAddress],
162163
sc: ScorexContext): Option[PeerInfo] = {
164+
// First of all try to establish connections to the hosts from different IP group
165+
// It will complicate eclipse attacks
163166
val excludedGroups = excludedPeers.flatMap(_.peerSpec.address).map(getIpGroup(_)).toSet
164167
val allCandidates = knownPeers.values.filterNot { p =>
165168
excludedPeers.exists(_.peerSpec.address == p.peerSpec.address) ||
166169
blacklistedPeers.exists(addr => p.peerSpec.address.map(_.getAddress).contains(addr))
167170
}.toSeq
168171
val preferredCandidates = allCandidates.filterNot(_.peerSpec.address.fold(true)(addr => excludedGroups.contains(getIpGroup(addr))))
172+
// If it is not possible to connect to the node from different IP group, then try to connect somewhere
169173
val candidates = if (preferredCandidates.nonEmpty) preferredCandidates else allCandidates
170174
if (candidates.nonEmpty) Some(candidates(Random.nextInt(candidates.size)))
171175
else None

0 commit comments

Comments
 (0)