-
Notifications
You must be signed in to change notification settings - Fork 78
[ETCM-178] Disallow duplicated connections and connections to self #763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ef84bec
[ETCM-178] Disallow repeated connections and connections to self
2e1fc09
[ETCM-178] Added further lazy vals to ConnectedPeers
e4c50d4
Merge branch 'develop' of github.com:input-output-hk/mantis into etcm…
eb91bfe
Merge branch 'develop' into etcm-178-remove-repeated-connections
mirkoAlic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/scala/io/iohk/ethereum/network/ConnectedPeers.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package io.iohk.ethereum.network | ||
|
|
||
| import java.net.InetSocketAddress | ||
|
|
||
| import akka.actor.ActorRef | ||
| import akka.util.ByteString | ||
|
|
||
| case class ConnectedPeers( | ||
| private val incomingPendingPeers: Map[PeerId, Peer], | ||
| private val outgoingPendingPeers: Map[PeerId, Peer], | ||
| private val handshakedPeers: Map[PeerId, Peer] | ||
| ) { | ||
|
|
||
| // FIXME: Kept only for compatibility purposes, should eventually be removed | ||
| lazy val peers: Map[PeerId, Peer] = outgoingPendingPeers ++ handshakedPeers | ||
|
|
||
| private lazy val allPeers: Map[PeerId, Peer] = outgoingPendingPeers ++ handshakedPeers ++ incomingPendingPeers | ||
|
|
||
| private lazy val allPeersRemoteAddresses: Set[InetSocketAddress] = allPeers.values.map(_.remoteAddress).toSet | ||
| def isConnectionHandled(remoteAddress: InetSocketAddress): Boolean = | ||
| allPeersRemoteAddresses.contains(remoteAddress) | ||
|
|
||
| /* | ||
| We have the node id of our outgoing pending peers so we could use that in our checks, by rejecting a peer that | ||
| handshaked to us with the same node id. | ||
| However, with checking the node id of only handshaked peers we prioritize handshaked peers over pending ones, | ||
| in the above mentioned case the repeated pending peer connection will eventually die out | ||
| */ | ||
| private lazy val handshakedPeersNodeIds: Set[ByteString] = handshakedPeers.values.flatMap(_.nodeId).toSet | ||
| def hasHandshakedWith(nodeId: ByteString): Boolean = | ||
| handshakedPeersNodeIds.contains(nodeId) | ||
|
|
||
| lazy val incomingPendingPeersCount: Int = incomingPendingPeers.size | ||
| lazy val incomingHandshakedPeersCount: Int = handshakedPeers.count { case (_, p) => p.incomingConnection } | ||
| lazy val outgoingPeersCount: Int = peers.count { case (_, p) => !p.incomingConnection } | ||
|
|
||
| lazy val handshakedPeersCount: Int = handshakedPeers.size | ||
| lazy val pendingPeersCount: Int = incomingPendingPeersCount + outgoingPendingPeers.size | ||
|
|
||
| def getPeer(peerId: PeerId): Option[Peer] = peers.get(peerId) | ||
|
|
||
| def addNewPendingPeer(pendingPeer: Peer): ConnectedPeers = { | ||
| if (pendingPeer.incomingConnection) | ||
| copy(incomingPendingPeers = incomingPendingPeers + (pendingPeer.id -> pendingPeer)) | ||
| else | ||
| copy(outgoingPendingPeers = outgoingPendingPeers + (pendingPeer.id -> pendingPeer)) | ||
| } | ||
|
|
||
| def promotePeerToHandshaked(peerAfterHandshake: Peer): ConnectedPeers = { | ||
| if (peerAfterHandshake.incomingConnection) | ||
| copy( | ||
| incomingPendingPeers = incomingPendingPeers - peerAfterHandshake.id, | ||
| handshakedPeers = handshakedPeers + (peerAfterHandshake.id -> peerAfterHandshake) | ||
| ) | ||
| else | ||
| copy( | ||
| outgoingPendingPeers = outgoingPendingPeers - peerAfterHandshake.id, | ||
| handshakedPeers = handshakedPeers + (peerAfterHandshake.id -> peerAfterHandshake) | ||
| ) | ||
| } | ||
|
|
||
| def removeTerminatedPeer(peerRef: ActorRef): (Iterable[PeerId], ConnectedPeers) = { | ||
| val peersId = allPeers.collect { case (id, peer) if peer.ref == peerRef => id } | ||
|
|
||
| ( | ||
| peersId, | ||
| ConnectedPeers(incomingPendingPeers -- peersId, outgoingPendingPeers -- peersId, handshakedPeers -- peersId) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| object ConnectedPeers { | ||
| def empty: ConnectedPeers = ConnectedPeers(Map.empty, Map.empty, Map.empty) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we have
lazy valwith all handshaked peer id to avoid this traversing every time ?