Skip to content

Commit

Permalink
removed mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
williambannas committed Jun 20, 2018
1 parent 25c0e29 commit 60ea395
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
13 changes: 5 additions & 8 deletions p2p/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,11 @@ def is_full(self) -> bool:
return len(self) >= self.max_peers

def is_valid_connection_candidate(self, candidate: Node) -> bool:
max_matching_ip = 2
matching_ip = 0
for peer in self.connected_nodes.values():
if candidate.address.ip == peer.remote.address.ip:
matching_ip += 1
if (matching_ip > max_matching_ip):
return False
return True
# connect to no more then 2 nodes with the same IP
nodes_by_ip = groupby(
operator.attrgetter('remote.address.ip'), self.connected_nodes.values())
matching_ip_nodes = nodes_by_ip.get(candidate.address.ip, [])
return len(matching_ip_nodes) <= 2

def get_nodes_to_connect(self) -> Generator[Node, None, None]:
for node in self.discovery.get_random_nodes(self.max_peers):
Expand Down
28 changes: 12 additions & 16 deletions p2p/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,25 +355,21 @@ async def _receive_handshake(
inbound=True,
)

total_peers_inbound = 0.0
total_peers = 0.0

for current_peers in self.peer_pool.connected_nodes.values():
total_peers += 1
if current_peers.inbound:
total_peers_inbound += 1
# make sure to have atleast 1/4 outbound connections

if self.peer_pool.is_full:
peer.disconnect(DisconnectReason.too_many_peers)
elif total_peers > 1 and total_peers_inbound // total_peers > 0.75:
peer.disconnect(DisconnectReason.useless_peer)
elif not self.peer_pool.is_valid_connection_candidate(peer.remote):
peer.disconnect(DisconnectReason.useless_peer)
else:
# We use self.wait() here as a workaround for
# https://github.com/ethereum/py-evm/issues/670.
await self.wait(self.do_handshake(peer))
total_peers = len(self.peer_pool.connected_nodes)
inbound_peers = len(
[peer for peer in self.peer_pool.connected_nodes.values() if peer.inbound])
if total_peers > 1 and inbound_peers / total_peers > 0.75:
# make sure to have at least 1/4 outbound connections
peer.disconnect(DisconnectReason.useless_peer)
elif not self.peer_pool.is_valid_connection_candidate(peer.remote):
peer.disconnect(DisconnectReason.useless_peer)
else:
# We use self.wait() here as a workaround for
# https://github.com/ethereum/py-evm/issues/670.
await self.wait(self.do_handshake(peer))

async def do_handshake(self, peer: BasePeer) -> None:
await peer.do_p2p_handshake(),
Expand Down
3 changes: 3 additions & 0 deletions tests/p2p/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class MockPeerPool():
def is_valid_connection_candidate(self, node):
return True

def __len__(self):
return len(self.connected_nodes)


def get_server(privkey, address, peer_class):
base_db = MemoryDB()
Expand Down

0 comments on commit 60ea395

Please sign in to comment.