mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
run black w/ extended line length
This commit is contained in:
committed by
Kevin Mai-Husan Chia
parent
905dfa9a8d
commit
7477b29508
@ -52,12 +52,7 @@ def initialize_default_kademlia_router(ksize=20, alpha=3, id_opt=None, storage=N
|
||||
|
||||
|
||||
def initialize_default_swarm(
|
||||
id_opt=None,
|
||||
transport_opt=None,
|
||||
muxer_opt=None,
|
||||
sec_opt=None,
|
||||
peerstore_opt=None,
|
||||
disc_opt=None,
|
||||
id_opt=None, transport_opt=None, muxer_opt=None, sec_opt=None, peerstore_opt=None, disc_opt=None
|
||||
):
|
||||
"""
|
||||
initialize swarm when no swarm is passed in
|
||||
|
||||
@ -68,9 +68,7 @@ class BasicHost(IHost):
|
||||
addrs.append(addr.encapsulate(p2p_part))
|
||||
return addrs
|
||||
|
||||
def set_stream_handler(
|
||||
self, protocol_id: str, stream_handler: StreamHandlerFn
|
||||
) -> bool:
|
||||
def set_stream_handler(self, protocol_id: str, stream_handler: StreamHandlerFn) -> bool:
|
||||
"""
|
||||
set stream handler for host
|
||||
:param protocol_id: protocol id used on stream
|
||||
|
||||
@ -38,9 +38,7 @@ class IHost(ABC):
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def set_stream_handler(
|
||||
self, protocol_id: str, stream_handler: StreamHandlerFn
|
||||
) -> bool:
|
||||
def set_stream_handler(self, protocol_id: str, stream_handler: StreamHandlerFn) -> bool:
|
||||
"""
|
||||
set stream handler for host
|
||||
:param protocol_id: protocol id used on stream
|
||||
|
||||
@ -114,9 +114,7 @@ class ValueSpiderCrawl(SpiderCrawl):
|
||||
"""
|
||||
value_counts = Counter(values)
|
||||
if len(value_counts) != 1:
|
||||
log.warning(
|
||||
"Got multiple values for key %i: %s", self.node.xor_id, str(values)
|
||||
)
|
||||
log.warning("Got multiple values for key %i: %s", self.node.xor_id, str(values))
|
||||
value = value_counts.most_common(1)[0][0]
|
||||
|
||||
peer = self.nearest_without_value.popleft()
|
||||
|
||||
@ -49,9 +49,7 @@ class KadPeerInfo(PeerInfo):
|
||||
|
||||
def encode(self):
|
||||
return (
|
||||
str(self.peer_id_bytes)
|
||||
+ "\n"
|
||||
+ str("/ip4/" + str(self.ip) + "/udp/" + str(self.port))
|
||||
str(self.peer_id_bytes) + "\n" + str("/ip4/" + str(self.ip) + "/udp/" + str(self.port))
|
||||
)
|
||||
|
||||
|
||||
@ -139,17 +137,11 @@ class KadPeerHeap:
|
||||
|
||||
|
||||
def create_kad_peerinfo(node_id_bytes=None, sender_ip=None, sender_port=None):
|
||||
node_id = (
|
||||
ID(node_id_bytes) if node_id_bytes else ID(digest(random.getrandbits(255)))
|
||||
)
|
||||
node_id = ID(node_id_bytes) if node_id_bytes else ID(digest(random.getrandbits(255)))
|
||||
peer_data = None
|
||||
if sender_ip and sender_port:
|
||||
peer_data = PeerData()
|
||||
addr = [
|
||||
Multiaddr(
|
||||
"/" + P_IP + "/" + str(sender_ip) + "/" + P_UDP + "/" + str(sender_port)
|
||||
)
|
||||
]
|
||||
addr = [Multiaddr("/" + P_IP + "/" + str(sender_ip) + "/" + P_UDP + "/" + str(sender_port))]
|
||||
peer_data.add_addrs(addr)
|
||||
|
||||
return KadPeerInfo(node_id, peer_data)
|
||||
|
||||
@ -62,9 +62,7 @@ class KademliaServer:
|
||||
Provide interface="::" to accept ipv6 address
|
||||
"""
|
||||
loop = asyncio.get_event_loop()
|
||||
listen = loop.create_datagram_endpoint(
|
||||
self._create_protocol, local_addr=(interface, port)
|
||||
)
|
||||
listen = loop.create_datagram_endpoint(self._create_protocol, local_addr=(interface, port))
|
||||
log.info("Node %i listening on %s:%i", self.node.xor_id, interface, port)
|
||||
self.transport, self.protocol = await listen
|
||||
# finally, schedule refreshing table
|
||||
@ -85,9 +83,7 @@ class KademliaServer:
|
||||
for node_id in self.protocol.get_refresh_ids():
|
||||
node = create_kad_peerinfo(node_id)
|
||||
nearest = self.protocol.router.find_neighbors(node, self.alpha)
|
||||
spider = NodeSpiderCrawl(
|
||||
self.protocol, node, nearest, self.ksize, self.alpha
|
||||
)
|
||||
spider = NodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)
|
||||
results.append(spider.find())
|
||||
|
||||
# do our crawling
|
||||
@ -122,9 +118,7 @@ class KademliaServer:
|
||||
cos = list(map(self.bootstrap_node, addrs))
|
||||
gathered = await asyncio.gather(*cos)
|
||||
nodes = [node for node in gathered if node is not None]
|
||||
spider = NodeSpiderCrawl(
|
||||
self.protocol, self.node, nodes, self.ksize, self.alpha
|
||||
)
|
||||
spider = NodeSpiderCrawl(self.protocol, self.node, nodes, self.ksize, self.alpha)
|
||||
return await spider.find()
|
||||
|
||||
async def bootstrap_node(self, addr):
|
||||
|
||||
@ -50,9 +50,7 @@ class KademliaProtocol(RPCProtocol):
|
||||
source = create_kad_peerinfo(nodeid, sender[0], sender[1])
|
||||
|
||||
self.welcome_if_new(source)
|
||||
log.debug(
|
||||
"got a store request from %s, storing '%s'='%s'", sender, key.hex(), value
|
||||
)
|
||||
log.debug("got a store request from %s, storing '%s'='%s'", sender, key.hex(), value)
|
||||
self.storage[key] = value
|
||||
return True
|
||||
|
||||
@ -82,9 +80,7 @@ class KademliaProtocol(RPCProtocol):
|
||||
we store a map of content_id to peer_id (non xor)
|
||||
"""
|
||||
if nodeid == provider_id:
|
||||
log.info(
|
||||
"adding provider %s for key %s in local table", provider_id, str(key)
|
||||
)
|
||||
log.info("adding provider %s for key %s in local table", provider_id, str(key))
|
||||
self.storage[key] = provider_id
|
||||
return True
|
||||
return False
|
||||
@ -135,9 +131,7 @@ class KademliaProtocol(RPCProtocol):
|
||||
|
||||
async def call_add_provider(self, node_to_ask, key, provider_id):
|
||||
address = (node_to_ask.ip, node_to_ask.port)
|
||||
result = await self.add_provider(
|
||||
address, self.source_node.peer_id_bytes, key, provider_id
|
||||
)
|
||||
result = await self.add_provider(address, self.source_node.peer_id_bytes, key, provider_id)
|
||||
|
||||
return self.handle_call_response(result, node_to_ask)
|
||||
|
||||
|
||||
@ -40,9 +40,7 @@ class INetwork(ABC):
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def set_stream_handler(
|
||||
self, protocol_id: str, stream_handler: StreamHandlerFn
|
||||
) -> bool:
|
||||
def set_stream_handler(self, protocol_id: str, stream_handler: StreamHandlerFn) -> bool:
|
||||
"""
|
||||
:param protocol_id: protocol id used on stream
|
||||
:param stream_handler: a stream handler instance
|
||||
|
||||
@ -69,9 +69,7 @@ class Swarm(INetwork):
|
||||
def get_peer_id(self) -> ID:
|
||||
return self.self_id
|
||||
|
||||
def set_stream_handler(
|
||||
self, protocol_id: str, stream_handler: StreamHandlerFn
|
||||
) -> bool:
|
||||
def set_stream_handler(self, protocol_id: str, stream_handler: StreamHandlerFn) -> bool:
|
||||
"""
|
||||
:param protocol_id: protocol id used on stream
|
||||
:param stream_handler: a stream handler instance
|
||||
@ -199,9 +197,7 @@ class Swarm(INetwork):
|
||||
|
||||
# Per, https://discuss.libp2p.io/t/multistream-security/130, we first secure
|
||||
# the conn and then mux the conn
|
||||
secured_conn = await self.upgrader.upgrade_security(
|
||||
raw_conn, peer_id, False
|
||||
)
|
||||
secured_conn = await self.upgrader.upgrade_security(raw_conn, peer_id, False)
|
||||
muxed_conn = self.upgrader.upgrade_connection(
|
||||
secured_conn, self.generic_protocol_handler, peer_id
|
||||
)
|
||||
|
||||
@ -25,9 +25,7 @@ def info_from_p2p_addr(addr: multiaddr.Multiaddr) -> PeerInfo:
|
||||
|
||||
parts = addr.split()
|
||||
if not parts:
|
||||
raise InvalidAddrError(
|
||||
f"`parts`={parts} should at least have a protocol `P_P2P`"
|
||||
)
|
||||
raise InvalidAddrError(f"`parts`={parts} should at least have a protocol `P_P2P`")
|
||||
|
||||
p2p_part = parts[-1]
|
||||
last_protocol_code = p2p_part.protocols()[0].code
|
||||
|
||||
@ -66,9 +66,7 @@ class FloodSub(IPubsubRouter):
|
||||
"""
|
||||
|
||||
peers_gen = self._get_peers_to_send(
|
||||
pubsub_msg.topicIDs,
|
||||
msg_forwarder=msg_forwarder,
|
||||
origin=ID(pubsub_msg.from_id),
|
||||
pubsub_msg.topicIDs, msg_forwarder=msg_forwarder, origin=ID(pubsub_msg.from_id)
|
||||
)
|
||||
rpc_msg = rpc_pb2.RPC(publish=[pubsub_msg])
|
||||
for peer_id in peers_gen:
|
||||
|
||||
@ -156,9 +156,7 @@ class GossipSub(IPubsubRouter):
|
||||
self.mcache.put(pubsub_msg)
|
||||
|
||||
peers_gen = self._get_peers_to_send(
|
||||
pubsub_msg.topicIDs,
|
||||
msg_forwarder=msg_forwarder,
|
||||
origin=ID(pubsub_msg.from_id),
|
||||
pubsub_msg.topicIDs, msg_forwarder=msg_forwarder, origin=ID(pubsub_msg.from_id)
|
||||
)
|
||||
rpc_msg = rpc_pb2.RPC(publish=[pubsub_msg])
|
||||
for peer_id in peers_gen:
|
||||
@ -353,9 +351,7 @@ class GossipSub(IPubsubRouter):
|
||||
if num_fanout_peers_in_topic < self.degree:
|
||||
# Select D - |fanout[topic]| peers from peers.gossipsub[topic] - fanout[topic]
|
||||
selected_peers = self._get_in_topic_gossipsub_peers_from_minus(
|
||||
topic,
|
||||
self.degree - num_fanout_peers_in_topic,
|
||||
self.fanout[topic],
|
||||
topic, self.degree - num_fanout_peers_in_topic, self.fanout[topic]
|
||||
)
|
||||
# Add the peers to fanout[topic]
|
||||
self.fanout[topic].extend(selected_peers)
|
||||
@ -374,9 +370,7 @@ class GossipSub(IPubsubRouter):
|
||||
|
||||
for peer in peers_to_emit_ihave_to:
|
||||
# TODO: this line is a monster, can hopefully be simplified
|
||||
if (
|
||||
topic not in self.mesh or (peer not in self.mesh[topic])
|
||||
) and (
|
||||
if (topic not in self.mesh or (peer not in self.mesh[topic])) and (
|
||||
topic not in self.fanout or (peer not in self.fanout[topic])
|
||||
):
|
||||
msg_id_strs = [str(msg_id) for msg_id in msg_ids]
|
||||
@ -396,10 +390,7 @@ class GossipSub(IPubsubRouter):
|
||||
topic, self.degree, []
|
||||
)
|
||||
for peer in peers_to_emit_ihave_to:
|
||||
if (
|
||||
peer not in self.mesh[topic]
|
||||
and peer not in self.fanout[topic]
|
||||
):
|
||||
if peer not in self.mesh[topic] and peer not in self.fanout[topic]:
|
||||
|
||||
msg_id_strs = [str(msg) for msg in msg_ids]
|
||||
await self.emit_ihave(topic, msg_id_strs, peer)
|
||||
@ -439,19 +430,13 @@ class GossipSub(IPubsubRouter):
|
||||
self, topic: str, num_to_select: int, minus: Sequence[ID]
|
||||
) -> List[ID]:
|
||||
gossipsub_peers_in_topic = [
|
||||
peer_id
|
||||
for peer_id in self.pubsub.peer_topics[topic]
|
||||
if peer_id in self.peers_gossipsub
|
||||
peer_id for peer_id in self.pubsub.peer_topics[topic] if peer_id in self.peers_gossipsub
|
||||
]
|
||||
return self.select_from_minus(
|
||||
num_to_select, gossipsub_peers_in_topic, list(minus)
|
||||
)
|
||||
return self.select_from_minus(num_to_select, gossipsub_peers_in_topic, list(minus))
|
||||
|
||||
# RPC handlers
|
||||
|
||||
async def handle_ihave(
|
||||
self, ihave_msg: rpc_pb2.Message, sender_peer_id: ID
|
||||
) -> None:
|
||||
async def handle_ihave(self, ihave_msg: rpc_pb2.Message, sender_peer_id: ID) -> None:
|
||||
"""
|
||||
Checks the seen set and requests unknown messages with an IWANT message.
|
||||
"""
|
||||
@ -475,9 +460,7 @@ class GossipSub(IPubsubRouter):
|
||||
if msg_ids_wanted:
|
||||
await self.emit_iwant(msg_ids_wanted, sender_peer_id)
|
||||
|
||||
async def handle_iwant(
|
||||
self, iwant_msg: rpc_pb2.Message, sender_peer_id: ID
|
||||
) -> None:
|
||||
async def handle_iwant(self, iwant_msg: rpc_pb2.Message, sender_peer_id: ID) -> None:
|
||||
"""
|
||||
Forwards all request messages that are present in mcache to the requesting peer.
|
||||
"""
|
||||
@ -512,9 +495,7 @@ class GossipSub(IPubsubRouter):
|
||||
# 4) And write the packet to the stream
|
||||
await peer_stream.write(rpc_msg)
|
||||
|
||||
async def handle_graft(
|
||||
self, graft_msg: rpc_pb2.Message, sender_peer_id: ID
|
||||
) -> None:
|
||||
async def handle_graft(self, graft_msg: rpc_pb2.Message, sender_peer_id: ID) -> None:
|
||||
topic: str = graft_msg.topicID
|
||||
|
||||
# Add peer to mesh for topic
|
||||
@ -525,9 +506,7 @@ class GossipSub(IPubsubRouter):
|
||||
# Respond with PRUNE if not subscribed to the topic
|
||||
await self.emit_prune(topic, sender_peer_id)
|
||||
|
||||
async def handle_prune(
|
||||
self, prune_msg: rpc_pb2.Message, sender_peer_id: ID
|
||||
) -> None:
|
||||
async def handle_prune(self, prune_msg: rpc_pb2.Message, sender_peer_id: ID) -> None:
|
||||
topic: str = prune_msg.topicID
|
||||
|
||||
# Remove peer from mesh for topic, if peer is in topic
|
||||
@ -589,9 +568,7 @@ class GossipSub(IPubsubRouter):
|
||||
|
||||
await self.emit_control_message(control_msg, to_peer)
|
||||
|
||||
async def emit_control_message(
|
||||
self, control_msg: rpc_pb2.ControlMessage, to_peer: ID
|
||||
) -> None:
|
||||
async def emit_control_message(self, control_msg: rpc_pb2.ControlMessage, to_peer: ID) -> None:
|
||||
# Add control message to packet
|
||||
packet: rpc_pb2.RPC = rpc_pb2.RPC()
|
||||
packet.control.CopyFrom(control_msg)
|
||||
|
||||
@ -105,9 +105,7 @@ class Pubsub:
|
||||
"""
|
||||
packet = rpc_pb2.RPC()
|
||||
for topic_id in self.my_topics:
|
||||
packet.subscriptions.extend(
|
||||
[rpc_pb2.RPC.SubOpts(subscribe=True, topicid=topic_id)]
|
||||
)
|
||||
packet.subscriptions.extend([rpc_pb2.RPC.SubOpts(subscribe=True, topicid=topic_id)])
|
||||
return packet.SerializeToString()
|
||||
|
||||
async def continuously_read_stream(self, stream: INetStream) -> None:
|
||||
|
||||
@ -29,9 +29,7 @@ class InsecureTransport(ISecureTransport):
|
||||
insecure_conn = InsecureConn(conn, self.transport_id)
|
||||
return insecure_conn
|
||||
|
||||
async def secure_outbound(
|
||||
self, conn: "IRawConnection", peer_id: "ID"
|
||||
) -> ISecureConn:
|
||||
async def secure_outbound(self, conn: "IRawConnection", peer_id: "ID") -> ISecureConn:
|
||||
"""
|
||||
Secure the connection, either locally or by communicating with opposing node via conn,
|
||||
for an inbound connection (i.e. we are the initiator)
|
||||
|
||||
@ -25,9 +25,7 @@ class ISecureTransport(ABC):
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def secure_outbound(
|
||||
self, conn: "IRawConnection", peer_id: "ID"
|
||||
) -> "ISecureConn":
|
||||
async def secure_outbound(self, conn: "IRawConnection", peer_id: "ID") -> "ISecureConn":
|
||||
"""
|
||||
Secure the connection, either locally or by communicating with opposing node via conn,
|
||||
for an inbound connection (i.e. we are the initiator)
|
||||
|
||||
@ -61,9 +61,7 @@ class SecurityMultistream(ABC):
|
||||
|
||||
return secure_conn
|
||||
|
||||
async def secure_outbound(
|
||||
self, conn: "IRawConnection", peer_id: "ID"
|
||||
) -> "ISecureConn":
|
||||
async def secure_outbound(self, conn: "IRawConnection", peer_id: "ID") -> "ISecureConn":
|
||||
"""
|
||||
Secure the connection, either locally or by communicating with opposing node via conn,
|
||||
for an inbound connection (i.e. we are the initiator)
|
||||
@ -78,9 +76,7 @@ class SecurityMultistream(ABC):
|
||||
|
||||
return secure_conn
|
||||
|
||||
async def select_transport(
|
||||
self, conn: "IRawConnection", initiator: bool
|
||||
) -> "ISecureTransport":
|
||||
async def select_transport(self, conn: "IRawConnection", initiator: bool) -> "ISecureTransport":
|
||||
"""
|
||||
Select a transport that both us and the node on the
|
||||
other end of conn support and agree on
|
||||
|
||||
@ -26,16 +26,12 @@ class SimpleSecurityTransport(ISecureTransport):
|
||||
incoming = (await conn.read()).decode()
|
||||
|
||||
if incoming != self.key_phrase:
|
||||
raise Exception(
|
||||
"Key phrase differed between nodes. Expected " + self.key_phrase
|
||||
)
|
||||
raise Exception("Key phrase differed between nodes. Expected " + self.key_phrase)
|
||||
|
||||
secure_conn = SimpleSecureConn(conn, self.key_phrase)
|
||||
return secure_conn
|
||||
|
||||
async def secure_outbound(
|
||||
self, conn: "IRawConnection", peer_id: "ID"
|
||||
) -> "ISecureConn":
|
||||
async def secure_outbound(self, conn: "IRawConnection", peer_id: "ID") -> "ISecureConn":
|
||||
"""
|
||||
Secure the connection, either locally or by communicating with opposing node via conn,
|
||||
for an inbound connection (i.e. we are the initiator)
|
||||
@ -49,9 +45,7 @@ class SimpleSecurityTransport(ISecureTransport):
|
||||
await asyncio.sleep(0)
|
||||
|
||||
if incoming != self.key_phrase:
|
||||
raise Exception(
|
||||
"Key phrase differed between nodes. Expected " + self.key_phrase
|
||||
)
|
||||
raise Exception("Key phrase differed between nodes. Expected " + self.key_phrase)
|
||||
|
||||
secure_conn = SimpleSecureConn(conn, self.key_phrase)
|
||||
return secure_conn
|
||||
|
||||
@ -157,9 +157,7 @@ class Mplex(IMuxedConn):
|
||||
try:
|
||||
header = await decode_uvarint_from_stream(self.raw_conn.reader, timeout)
|
||||
length = await decode_uvarint_from_stream(self.raw_conn.reader, timeout)
|
||||
message = await asyncio.wait_for(
|
||||
self.raw_conn.reader.read(length), timeout=timeout
|
||||
)
|
||||
message = await asyncio.wait_for(self.raw_conn.reader.read(length), timeout=timeout)
|
||||
except asyncio.TimeoutError:
|
||||
return None, None, None
|
||||
|
||||
|
||||
@ -50,9 +50,7 @@ class MplexStream(IMuxedStream):
|
||||
"""
|
||||
# TODO error handling with timeout
|
||||
# TODO understand better how mutexes are used from go repo
|
||||
await self.mplex_conn.send_message(
|
||||
get_flag(self.initiator, "CLOSE"), None, self.stream_id
|
||||
)
|
||||
await self.mplex_conn.send_message(get_flag(self.initiator, "CLOSE"), None, self.stream_id)
|
||||
|
||||
remote_lock = ""
|
||||
async with self.stream_lock:
|
||||
|
||||
@ -25,9 +25,7 @@ class TCP(ITransport):
|
||||
:return: return True if successful
|
||||
"""
|
||||
self.server = await asyncio.start_server(
|
||||
self.handler,
|
||||
maddr.value_for_protocol("ip4"),
|
||||
maddr.value_for_protocol("tcp"),
|
||||
self.handler, maddr.value_for_protocol("ip4"), maddr.value_for_protocol("tcp")
|
||||
)
|
||||
socket = self.server.sockets[0]
|
||||
self.multiaddrs.append(_multiaddr_from_socket(socket))
|
||||
|
||||
Reference in New Issue
Block a user