Add partial support for admin_peers JSON-RPC API #1491
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Implement ``admin_peers`` JSON-RPC API |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
from typing import Tuple, Iterable, Dict | ||
|
||
from typing import Tuple, Iterable, Dict, Sequence | ||
|
||
from eth.constants import GENESIS_BLOCK_NUMBER | ||
from eth_typing import BlockNumber | ||
from eth_utils import encode_hex, to_dict | ||
|
||
from lahja import EndpointAPI | ||
|
||
from p2p.kademlia import Node | ||
|
@@ -14,11 +16,14 @@ | |
from trinity.constants import TO_NETWORKING_BROADCAST_CONFIG | ||
from trinity.protocol.common.events import ( | ||
ConnectToNodeCommand, | ||
GetProtocolCapabilitiesRequest | ||
GetConnectedPeersRequest, | ||
GetProtocolCapabilitiesRequest, | ||
PeerInfo, | ||
) | ||
from trinity.rpc.modules import Eth1ChainRPCModule | ||
from trinity.rpc.typing import RpcProtocolResponse, RpcNodeInfoResponse | ||
from trinity.server import BOUND_IP | ||
from trinity.rpc.typing import RpcPeerResponse | ||
from trinity._utils.version import construct_trinity_client_identifier | ||
|
||
|
||
|
@@ -91,3 +96,26 @@ async def _generate_protocol_info( | |
} | ||
for protocol, version in protocols | ||
} | ||
|
||
def _format_peer(self, peer_info: PeerInfo) -> RpcPeerResponse: | ||
session = peer_info.session | ||
return { | ||
'enode': session.remote.uri(), | ||
'id': str(session.id), | ||
'name': peer_info.client_version_string, | ||
'caps': [f"{protocol}/{version}" for protocol, version in peer_info.capabilities], | ||
'network': { | ||
'localAddress': f"{BOUND_IP}:{self.trinity_config.port}", | ||
'remoteAddress': f"{session.remote.address.ip}:{session.remote.address.tcp_port}", | ||
'inbound': peer_info.inbound | ||
} | ||
} | ||
|
||
async def peers(self) -> Sequence[RpcPeerResponse]: | ||
|
||
response = await self.event_bus.request(GetConnectedPeersRequest()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pipermerriam So, our There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be very reasonable to have a new object returned that aggregates information about the session like: class PeerInfo(NamedTuple):
session: SessionAPI
capabilities: Capabilities
... |
||
|
||
return tuple( | ||
self._format_peer(peer_info) | ||
for peer_info in response.peers | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import Sequence, Any | ||
|
||
from p2p.abc import SessionAPI | ||
|
||
try: | ||
import factory | ||
except ImportError: | ||
raise ImportError( | ||
"The p2p.tools.factories module requires the `factory_boy` and `faker` libraries." | ||
) | ||
|
||
|
||
from trinity.protocol.common.events import GetConnectedPeersResponse, PeerInfo | ||
|
||
|
||
class GetConnectedPeersResponseFactory(factory.Factory): | ||
class Meta: | ||
model = GetConnectedPeersResponse | ||
|
||
@classmethod | ||
def from_sessions(cls, | ||
sessions: Sequence[SessionAPI], | ||
*args: Any, | ||
**kwargs: Any) -> GetConnectedPeersResponse: | ||
return GetConnectedPeersResponse(tuple( | ||
PeerInfo( | ||
session=session, | ||
capabilities=(), | ||
client_version_string='unknown', | ||
inbound=False | ||
) for session in sessions | ||
)) |
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.
Lol, it looks like you implemented pretty much the exact thing I typed up 👍