Skip to content

Commit 7870760

Browse files
authored
Merge pull request ethereum#625 from pipermerriam/piper/provide-all-connected-peers-to-candidate-backends
Candidate backend API now provides all connected remotes
2 parents d4bac63 + dcb41c1 commit 7870760

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

p2p/peer_backend.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from abc import ABC, abstractmethod
22
from typing import (
3-
Iterable,
3+
Set,
4+
Tuple,
45
)
56

67
from lahja import (
@@ -24,7 +25,7 @@ class BasePeerBackend(ABC):
2425
@abstractmethod
2526
async def get_peer_candidates(self,
2627
num_requested: int,
27-
num_connected_peers: int) -> Iterable[Node]:
28+
connected_remotes: Set[Node]) -> Tuple[Node, ...]:
2829
pass
2930

3031

@@ -37,12 +38,16 @@ def __init__(self, event_bus: Endpoint) -> None:
3738

3839
async def get_peer_candidates(self,
3940
num_requested: int,
40-
num_connected_peers: int) -> Iterable[Node]:
41+
connected_remotes: Set[Node]) -> Tuple[Node, ...]:
4142
response = await self.event_bus.request(
4243
PeerCandidatesRequest(num_requested),
4344
TO_DISCOVERY_BROADCAST_CONFIG,
4445
)
45-
return response.candidates
46+
return tuple(
47+
candidate
48+
for candidate in response.candidates
49+
if candidate not in connected_remotes
50+
)
4651

4752

4853
class BootnodesPeerBackend(BasePeerBackend):
@@ -51,13 +56,17 @@ def __init__(self, event_bus: Endpoint) -> None:
5156

5257
async def get_peer_candidates(self,
5358
num_requested: int,
54-
num_connected_peers: int) -> Iterable[Node]:
55-
if num_connected_peers == 0:
59+
connected_remotes: Set[Node]) -> Tuple[Node, ...]:
60+
if len(connected_remotes) == 0:
5661
response = await self.event_bus.request(
5762
RandomBootnodeRequest(),
5863
TO_DISCOVERY_BROADCAST_CONFIG
5964
)
6065

61-
return response.candidates
66+
return tuple(
67+
candidate
68+
for candidate in response.candidates
69+
if candidate not in connected_remotes
70+
)
6271
else:
6372
return ()

p2p/peer_pool.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,13 @@ async def _add_peers_from_backend(self, backend: BasePeerBackend) -> None:
161161
available_slots = self.max_peers - len(self)
162162

163163
try:
164+
connected_remotes = {
165+
peer.remote for peer in self.connected_nodes.values()
166+
}
164167
candidates = await self.wait(
165168
backend.get_peer_candidates(
166169
num_requested=available_slots,
167-
num_connected_peers=len(self),
170+
connected_remotes=connected_remotes,
168171
),
169172
timeout=REQUEST_PEER_CANDIDATE_TIMEOUT,
170173
)

0 commit comments

Comments
 (0)