async def test_read_pbmsg_safe_valid(msg_bytes): s = MockReaderWriter() await write_unsigned_varint(s, len(msg_bytes)) s.write(msg_bytes) # reset the offset back to the beginning s.seek(0, 0) pb_msg = p2pd_pb.Response() await read_pbmsg_safe(s, pb_msg) assert pb_msg.SerializeToString() == msg_bytes
async def disconnect(self, peer_id: PeerID) -> None: disconnect_req = p2pd_pb.DisconnectRequest(peer=peer_id.to_bytes()) req = p2pd_pb.Request( type=p2pd_pb.Request.DISCONNECT, disconnect=disconnect_req ) reader, writer = await self.daemon_connector.open_connection() await write_pbmsg(writer, req) resp = p2pd_pb.Response() # type: ignore await read_pbmsg_safe(reader, resp) writer.close() raise_if_failed(resp)
async def list_peers(self) -> Tuple[PeerInfo, ...]: req = p2pd_pb.Request(type=p2pd_pb.Request.LIST_PEERS) reader, writer = await self.daemon_connector.open_connection() await write_pbmsg(writer, req) resp = p2pd_pb.Response() # type: ignore await read_pbmsg_safe(reader, resp) writer.close() raise_if_failed(resp) peers = tuple(PeerInfo.from_protobuf(pinfo) for pinfo in resp.peers) return peers
async def connect(self, peer_id: PeerID, maddrs: Iterable[Multiaddr]) -> None: reader, writer = await self.daemon_connector.open_connection() maddrs_bytes = [i.to_bytes() for i in maddrs] connect_req = p2pd_pb.ConnectRequest( peer=peer_id.to_bytes(), addrs=maddrs_bytes ) req = p2pd_pb.Request(type=p2pd_pb.Request.CONNECT, connect=connect_req) await write_pbmsg(writer, req) resp = p2pd_pb.Response() # type: ignore await read_pbmsg_safe(reader, resp) writer.close() raise_if_failed(resp)
async def identify(self) -> Tuple[PeerID, Tuple[Multiaddr, ...]]: reader, writer = await self.daemon_connector.open_connection() req = p2pd_pb.Request(type=p2pd_pb.Request.IDENTIFY) await write_pbmsg(writer, req) resp = p2pd_pb.Response() # type: ignore await read_pbmsg_safe(reader, resp) writer.close() raise_if_failed(resp) peer_id_bytes = resp.identify.id maddrs_bytes = resp.identify.addrs maddrs = tuple(Multiaddr(maddr_bytes) for maddr_bytes in maddrs_bytes) peer_id = PeerID(peer_id_bytes) return peer_id, maddrs
async def stream_handler(self, proto: str, handler_cb: StreamHandler) -> None: reader, writer = await self.daemon_connector.open_connection() listen_path_maddr_bytes = self.listen_maddr.to_bytes() stream_handler_req = p2pd_pb.StreamHandlerRequest( addr=listen_path_maddr_bytes, proto=[proto] ) req = p2pd_pb.Request( type=p2pd_pb.Request.STREAM_HANDLER, streamHandler=stream_handler_req ) await write_pbmsg(writer, req) resp = p2pd_pb.Response() # type: ignore await read_pbmsg_safe(reader, resp) writer.close() raise_if_failed(resp) # if success, add the handler to the dict self.handlers[proto] = handler_cb
async def stream_open( self, peer_id: PeerID, protocols: Sequence[str] ) -> Tuple[StreamInfo, asyncio.StreamReader, asyncio.StreamWriter]: reader, writer = await self.daemon_connector.open_connection() stream_open_req = p2pd_pb.StreamOpenRequest( peer=peer_id.to_bytes(), proto=list(protocols) ) req = p2pd_pb.Request( type=p2pd_pb.Request.STREAM_OPEN, streamOpen=stream_open_req ) await write_pbmsg(writer, req) resp = p2pd_pb.Response() # type: ignore await read_pbmsg_safe(reader, resp) raise_if_failed(resp) pb_stream_info = resp.streamInfo stream_info = StreamInfo.from_protobuf(pb_stream_info) return stream_info, reader, writer
def test_raise_if_failed_not_raises(): resp = p2pd_pb.Response() resp.type = p2pd_pb.Response.OK raise_if_failed(resp)
def test_raise_if_failed_raises(): resp = p2pd_pb.Response() resp.type = p2pd_pb.Response.ERROR with pytest.raises(ControlFailure): raise_if_failed(resp)
def test_control_client_ctor_default_listen_maddr(): c = ControlClient(daemon_connector=DaemonConnector()) assert c.listen_maddr == Multiaddr(ControlClient.DEFAULT_LISTEN_MADDR) @pytest.mark.parametrize( "msg_bytes", ( p2pd_pb.Response( type=p2pd_pb.Response.Type.OK, identify=p2pd_pb.IdentifyResponse( id=PeerID.from_base58( 'QmT7WhTne9zBLfAgAJt9aiZ8jZ5BxJGowRubxsHYmnyzUd').to_bytes( ), addrs=[ Multiaddr('/p2p-circuit').to_bytes(), Multiaddr('/ip4/127.0.0.1/tcp/51126').to_bytes(), Multiaddr('/ip4/192.168.10.135/tcp/51126').to_bytes(), Multiaddr('/ip6/::1/tcp/51127').to_bytes() ])).SerializeToString(), p2pd_pb.Response( type=p2pd_pb.Response.Type.OK, identify=p2pd_pb.IdentifyResponse( id=PeerID.from_base58( 'QmcQFt2MFfCZ9AxzUCNrk4k7TtMdZZvAAteaA6tHpBKdrk').to_bytes( ), addrs=[ Multiaddr('/p2p-circuit').to_bytes(), Multiaddr('/ip4/127.0.0.1/tcp/51493').to_bytes(), Multiaddr('/ip4/192.168.10.135/tcp/51493').to_bytes(),