From 9562cb2a46be5618b304c75860ba33679be199b3 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Wed, 31 Jul 2019 23:50:53 +0800 Subject: [PATCH] Rename: `KadPeerInfo.peer_id` to `KadPeerInfo.peer_id_bytes` --- libp2p/kademlia/crawling.py | 4 ++-- libp2p/kademlia/kad_peerinfo.py | 20 ++++++++++---------- libp2p/kademlia/network.py | 12 ++++++------ libp2p/kademlia/protocol.py | 16 ++++++++-------- libp2p/kademlia/routing.py | 22 +++++++++++----------- tests/kademlia/test_providers.py | 2 +- tests/routing/test_kad_peer_router.py | 6 +++--- 7 files changed, 41 insertions(+), 41 deletions(-) diff --git a/libp2p/kademlia/crawling.py b/libp2p/kademlia/crawling.py index 702271e..0e43a9b 100644 --- a/libp2p/kademlia/crawling.py +++ b/libp2p/kademlia/crawling.py @@ -61,7 +61,7 @@ class SpiderCrawl: dicts = {} for peer in self.nearest.get_uncontacted()[:count]: - dicts[peer.peer_id] = rpcmethod(peer, self.node) + dicts[peer.peer_id_bytes] = rpcmethod(peer, self.node) self.nearest.mark_contacted(peer) found = await gather_dict(dicts) return await self._nodes_found(found) @@ -125,7 +125,7 @@ class ValueSpiderCrawl(SpiderCrawl): peer = self.nearest_without_value.popleft() if peer: - await self.protocol.call_store(peer, self.node.peer_id, value) + await self.protocol.call_store(peer, self.node.peer_id_bytes, value) return value diff --git a/libp2p/kademlia/kad_peerinfo.py b/libp2p/kademlia/kad_peerinfo.py index f1bffce..a66d5f7 100644 --- a/libp2p/kademlia/kad_peerinfo.py +++ b/libp2p/kademlia/kad_peerinfo.py @@ -16,7 +16,7 @@ class KadPeerInfo(PeerInfo): def __init__(self, peer_id, peer_data=None): super(KadPeerInfo, self).__init__(peer_id, peer_data) - self.peer_id = peer_id.to_bytes() + self.peer_id_bytes = peer_id.to_bytes() self.xor_id = peer_id.xor_id self.addrs = peer_data.get_addrs() if peer_data else None @@ -38,17 +38,17 @@ class KadPeerInfo(PeerInfo): """ Enables use of Node as a tuple - i.e., tuple(node) works. """ - return iter([self.peer_id, self.ip, self.port]) + return iter([self.peer_id_bytes, self.ip, self.port]) def __repr__(self): - return repr([self.xor_id, self.ip, self.port, self.peer_id]) + return repr([self.xor_id, self.ip, self.port, self.peer_id_bytes]) def __str__(self): return "%s:%s" % (self.ip, str(self.port)) def encode(self): return ( - str(self.peer_id) + str(self.peer_id_bytes) + "\n" + str("/ip4/" + str(self.ip) + "/udp/" + str(self.port)) ) @@ -84,13 +84,13 @@ class KadPeerHeap: return nheap = [] for distance, node in self.heap: - if node.peer_id not in peers: + if node.peer_id_bytes not in peers: heapq.heappush(nheap, (distance, node)) self.heap = nheap def get_node(self, node_id): for _, node in self.heap: - if node.peer_id == node_id: + if node.peer_id_bytes == node_id: return node return None @@ -98,10 +98,10 @@ class KadPeerHeap: return len(self.get_uncontacted()) == 0 def get_ids(self): - return [n.peer_id for n in self] + return [n.peer_id_bytes for n in self] def mark_contacted(self, node): - self.contacted.add(node.peer_id) + self.contacted.add(node.peer_id_bytes) def popleft(self): return heapq.heappop(self.heap)[1] if self else None @@ -129,12 +129,12 @@ class KadPeerHeap: def __contains__(self, node): for _, other in self.heap: - if node.peer_id == other.peer_id: + if node.peer_id_bytes == other.peer_id_bytes: return True return False def get_uncontacted(self): - return [n for n in self if n.peer_id not in self.contacted] + return [n for n in self if n.peer_id_bytes not in self.contacted] 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))) diff --git a/libp2p/kademlia/network.py b/libp2p/kademlia/network.py index f8b9d7a..09a3463 100644 --- a/libp2p/kademlia/network.py +++ b/libp2p/kademlia/network.py @@ -130,7 +130,7 @@ class KademliaServer: return await spider.find() async def bootstrap_node(self, addr): - result = await self.protocol.ping(addr, self.node.peer_id) + result = await self.protocol.ping(addr, self.node.peer_id_bytes) return create_kad_peerinfo(result[1], addr[0], addr[1]) if result[0] else None async def get(self, key): @@ -170,7 +170,7 @@ class KademliaServer: """ neighbors = self.protocol.router.find_neighbors(self.node) return [ - await self.protocol.call_add_provider(n, key, self.node.peer_id) + await self.protocol.call_add_provider(n, key, self.node.peer_id_bytes) for n in neighbors ] @@ -212,10 +212,10 @@ class KademliaServer: """ log.info("Saving state to %s", fname) data = { - "ksize": self.ksize, - "alpha": self.alpha, - "id": self.node.peer_id, - "neighbors": self.bootstrappable_neighbors(), + 'ksize': self.ksize, + 'alpha': self.alpha, + 'id': self.node.peer_id_bytes, + 'neighbors': self.bootstrappable_neighbors() } if not data["neighbors"]: log.warning("No known neighbors, so not writing to cache.") diff --git a/libp2p/kademlia/protocol.py b/libp2p/kademlia/protocol.py index 1c99579..afbef44 100644 --- a/libp2p/kademlia/protocol.py +++ b/libp2p/kademlia/protocol.py @@ -44,7 +44,7 @@ class KademliaProtocol(RPCProtocol): source = create_kad_peerinfo(nodeid, sender[0], sender[1]) self.welcome_if_new(source) - return self.source_node.peer_id + return self.source_node.peer_id_bytes def rpc_store(self, sender, nodeid, key, value): source = create_kad_peerinfo(nodeid, sender[0], sender[1]) @@ -106,39 +106,39 @@ class KademliaProtocol(RPCProtocol): keynode = create_kad_peerinfo(key) neighbors = self.router.find_neighbors(keynode) for neighbor in neighbors: - if neighbor.peer_id != record: - providers.append(neighbor.peer_id) + if neighbor.peer_id_bytes != record: + providers.append(neighbor.peer_id_bytes) return providers async def call_find_node(self, node_to_ask, node_to_find): address = (node_to_ask.ip, node_to_ask.port) result = await self.find_node( - address, self.source_node.peer_id, node_to_find.peer_id + address, self.source_node.peer_id_bytes, node_to_find.peer_id_bytes ) return self.handle_call_response(result, node_to_ask) async def call_find_value(self, node_to_ask, node_to_find): address = (node_to_ask.ip, node_to_ask.port) result = await self.find_value( - address, self.source_node.peer_id, node_to_find.peer_id + address, self.source_node.peer_id_bytes, node_to_find.peer_id_bytes ) return self.handle_call_response(result, node_to_ask) async def call_ping(self, node_to_ask): address = (node_to_ask.ip, node_to_ask.port) - result = await self.ping(address, self.source_node.peer_id) + result = await self.ping(address, self.source_node.peer_id_bytes) return self.handle_call_response(result, node_to_ask) async def call_store(self, node_to_ask, key, value): address = (node_to_ask.ip, node_to_ask.port) - result = await self.store(address, self.source_node.peer_id, key, value) + result = await self.store(address, self.source_node.peer_id_bytes, key, value) return self.handle_call_response(result, node_to_ask) 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, key, provider_id + address, self.source_node.peer_id_bytes, key, provider_id ) return self.handle_call_response(result, node_to_ask) diff --git a/libp2p/kademlia/routing.py b/libp2p/kademlia/routing.py index 6aba4ca..0179155 100644 --- a/libp2p/kademlia/routing.py +++ b/libp2p/kademlia/routing.py @@ -35,24 +35,24 @@ class KBucket: two = KBucket(midpoint + 1, self.range[1], self.ksize) for node in self.nodes.values(): bucket = one if node.xor_id <= midpoint else two - bucket.nodes[node.peer_id] = node + bucket.nodes[node.peer_id_bytes] = node return (one, two) def remove_node(self, node): - if node.peer_id not in self.nodes: + if node.peer_id_bytes not in self.nodes: return # delete node, and see if we can add a replacement - del self.nodes[node.peer_id] + del self.nodes[node.peer_id_bytes] if self.replacement_nodes: newnode = self.replacement_nodes.pop() - self.nodes[newnode.peer_id] = newnode + self.nodes[newnode.peer_id_bytes] = newnode def has_in_range(self, node): return self.range[0] <= node.xor_id <= self.range[1] def is_new_node(self, node): - return node.peer_id not in self.nodes + return node.peer_id_bytes not in self.nodes def add_node(self, node): """ @@ -62,11 +62,11 @@ class KBucket: If the bucket is full, keep track of node in a replacement list, per section 4.1 of the paper. """ - if node.peer_id in self.nodes: - del self.nodes[node.peer_id] - self.nodes[node.peer_id] = node + if node.peer_id_bytes in self.nodes: + del self.nodes[node.peer_id_bytes] + self.nodes[node.peer_id_bytes] = node elif len(self) < self.ksize: - self.nodes[node.peer_id] = node + self.nodes[node.peer_id_bytes] = node else: self.replacement_nodes.push(node) return False @@ -74,7 +74,7 @@ class KBucket: def depth(self): vals = self.nodes.values() - sprefix = shared_prefix([bytes_to_bit_string(n.peer_id) for n in vals]) + sprefix = shared_prefix([bytes_to_bit_string(n.peer_id_bytes) for n in vals]) return len(sprefix) def head(self): @@ -186,7 +186,7 @@ class RoutingTable: nodes = [] for neighbor in TableTraverser(self, node): notexcluded = exclude is None or not neighbor.same_home_as(exclude) - if neighbor.peer_id != node.peer_id and notexcluded: + if neighbor.peer_id_bytes != node.peer_id_bytes and notexcluded: heapq.heappush(nodes, (node.distance_to(neighbor), neighbor)) if len(nodes) == k: break diff --git a/tests/kademlia/test_providers.py b/tests/kademlia/test_providers.py index 9723cc1..f8ed1bf 100644 --- a/tests/kademlia/test_providers.py +++ b/tests/kademlia/test_providers.py @@ -28,4 +28,4 @@ async def test_example(): first_providers = first_tuple[1] # [b'\xf9\xa1\xf5\x10a\xe5\xe0F'] first_provider = first_providers[0] - assert node_b.node.peer_id == first_provider + assert node_b.node.peer_id_bytes == first_provider diff --git a/tests/routing/test_kad_peer_router.py b/tests/routing/test_kad_peer_router.py index 3e5c19d..3b2edcc 100644 --- a/tests/routing/test_kad_peer_router.py +++ b/tests/routing/test_kad_peer_router.py @@ -18,7 +18,7 @@ async def test_simple_two_nodes(): await node_a.set(node_a_kad_peerinfo.xor_id, repr(node_a_kad_peerinfo)) router = KadmeliaPeerRouter(node_b) - returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id)) + returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id_bytes)) print(repr(returned_info)) print(repr(node_a_kad_peerinfo)) assert repr(returned_info) == repr(node_a_kad_peerinfo) @@ -42,7 +42,7 @@ async def test_simple_three_nodes(): await node_a.set(node_a_kad_peerinfo.xor_id, repr(node_a_kad_peerinfo)) router = KadmeliaPeerRouter(node_c) - returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id)) + returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id_bytes)) assert str(returned_info) == str(node_a_kad_peerinfo) @@ -70,5 +70,5 @@ async def test_simple_four_nodes(): await node_b.set(node_a_kad_peerinfo.xor_id, repr(node_a_kad_peerinfo)) router = KadmeliaPeerRouter(node_d) - returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id)) + returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id_bytes)) assert str(returned_info) == str(node_a_kad_peerinfo)