From 1512ae27a17f915eaeb04fc4fb0ebd8f24fa9b7b Mon Sep 17 00:00:00 2001 From: zixuanzh Date: Fri, 19 Apr 2019 20:00:23 -0400 Subject: [PATCH] fix all tests --- libp2p/kademlia/crawling.py | 16 ++++- libp2p/kademlia/kad_peerinfo.py | 31 +++++---- libp2p/kademlia/node.py | 113 -------------------------------- libp2p/kademlia/protocol.py | 13 ---- libp2p/kademlia/routing.py | 12 ---- 5 files changed, 34 insertions(+), 151 deletions(-) delete mode 100644 libp2p/kademlia/node.py diff --git a/libp2p/kademlia/crawling.py b/libp2p/kademlia/crawling.py index a1756af..2c6a618 100644 --- a/libp2p/kademlia/crawling.py +++ b/libp2p/kademlia/crawling.py @@ -1,8 +1,10 @@ from collections import Counter import logging -from .kad_peerinfo import KadPeerInfo, KadPeerHeap +from multiaddr import Multiaddr from libp2p.peer.id import ID +from libp2p.peer.peerdata import PeerData +from .kad_peerinfo import KadPeerInfo, KadPeerHeap from .utils import gather_dict @@ -184,4 +186,14 @@ class RPCFindResponse: be set. """ nodelist = self.response[1] or [] - return [KadPeerInfo(ID(*nodeple)) for nodeple in nodelist] + output = [] + for nodeple in nodelist: + #TODO check if nodeple is of the right format + # node_id, ip, port + node_id = ID(nodeple[0]) + peer_data = PeerData() #pylint: disable=no-value-for-parameter + addr = [Multiaddr("/ip4/" + str(nodeple[1]) + "/udp/" + str(nodeple[2]))] + peer_data.add_addrs(addr) + output.append(KadPeerInfo(node_id, peer_data)) + + return output diff --git a/libp2p/kademlia/kad_peerinfo.py b/libp2p/kademlia/kad_peerinfo.py index 6524fc2..91ba939 100644 --- a/libp2p/kademlia/kad_peerinfo.py +++ b/libp2p/kademlia/kad_peerinfo.py @@ -1,19 +1,28 @@ import heapq -import multihash + from operator import itemgetter from libp2p.peer.peerinfo import PeerInfo +from .utils import digest +P_IP = "ip4" +P_UDP = "udp" class KadPeerInfo(PeerInfo): def __init__(self, peer_id, peer_data=None): super(KadPeerInfo, self).__init__(peer_id, peer_data) - print ("Kad Peer Info") - print (peer_id) - print (peer_data) - sha1 = multihash.Func.sha1 - mh_digest = multihash.digest(peer_id.pretty().encode('utf-8'), sha1) - self.peer_id = peer_id.pretty() - self.long_id = int.from_bytes(mh_digest.encode(), byteorder='big') + + # pylint: disable=protected-access + self.peer_id = peer_id._id_str + self.long_id = int(digest(peer_id._id_str).hex(), 16) + + self.addrs = peer_data.get_addrs() if peer_data else None + + # pylint: disable=invalid-name + self.ip = self.addrs[0].value_for_protocol(P_IP)\ + if peer_data else None + self.port = int(self.addrs[0].value_for_protocol(P_UDP))\ + if peer_data else None + def same_home_as(self, node): #TODO: handle more than one addr @@ -29,13 +38,13 @@ class KadPeerInfo(PeerInfo): """ Enables use of Node as a tuple - i.e., tuple(node) works. """ - return iter([self.peer_id.pretty(), str(self.addrs[0])]) + return iter([self.peer_id, self.ip, self.port]) def __repr__(self): - return repr([self.long_id, str(self.addrs[0])]) + return repr([self.long_id, self.ip, self.port]) def __str__(self): - return str(self.addrs[0]) + return "%s:%s" % (self.ip, str(self.port)) class KadPeerHeap: """ diff --git a/libp2p/kademlia/node.py b/libp2p/kademlia/node.py deleted file mode 100644 index 251b5d8..0000000 --- a/libp2p/kademlia/node.py +++ /dev/null @@ -1,113 +0,0 @@ -# from operator import itemgetter -# import heapq - - -# class Node: -# def __init__(self, node_id, ip=None, port=None): -# self.id = node_id # pylint: disable=invalid-name -# self.ip = ip # pylint: disable=invalid-name -# self.port = port -# self.long_id = int(node_id.hex(), 16) - -# def same_home_as(self, node): -# return self.ip == node.ip and self.port == node.port - -# def distance_to(self, node): -# """ -# Get the distance between this node and another. -# """ -# return self.long_id ^ node.long_id - -# def __iter__(self): -# """ -# Enables use of Node as a tuple - i.e., tuple(node) works. -# """ -# return iter([self.id, self.ip, self.port]) - -# def __repr__(self): -# return repr([self.long_id, self.ip, self.port]) - -# def __str__(self): -# return "%s:%s" % (self.ip, str(self.port)) - - -# class NodeHeap: -# """ -# A heap of nodes ordered by distance to a given node. -# """ -# def __init__(self, node, maxsize): -# """ -# Constructor. - -# @param node: The node to measure all distnaces from. -# @param maxsize: The maximum size that this heap can grow to. -# """ -# self.node = node -# self.heap = [] -# self.contacted = set() -# self.maxsize = maxsize - -# def remove(self, peers): -# """ -# Remove a list of peer ids from this heap. Note that while this -# heap retains a constant visible size (based on the iterator), it's -# actual size may be quite a bit larger than what's exposed. Therefore, -# removal of nodes may not change the visible size as previously added -# nodes suddenly become visible. -# """ -# peers = set(peers) -# if not peers: -# return -# nheap = [] -# for distance, node in self.heap: -# if node.id not in peers: -# heapq.heappush(nheap, (distance, node)) -# self.heap = nheap - -# def get_node(self, node_id): -# for _, node in self.heap: -# if node.id == node_id: -# return node -# return None - -# def have_contacted_all(self): -# return len(self.get_uncontacted()) == 0 - -# def get_ids(self): -# return [n.id for n in self] - -# def mark_contacted(self, node): -# self.contacted.add(node.id) - -# def popleft(self): -# return heapq.heappop(self.heap)[1] if self else None - -# def push(self, nodes): -# """ -# Push nodes onto heap. - -# @param nodes: This can be a single item or a C{list}. -# """ -# if not isinstance(nodes, list): -# nodes = [nodes] - -# for node in nodes: -# if node not in self: -# distance = self.node.distance_to(node) -# heapq.heappush(self.heap, (distance, node)) - -# def __len__(self): -# return min(len(self.heap), self.maxsize) - -# def __iter__(self): -# nodes = heapq.nsmallest(self.maxsize, self.heap) -# return iter(map(itemgetter(1), nodes)) - -# def __contains__(self, node): -# for _, other in self.heap: -# if node.id == other.id: -# return True -# return False - -# def get_uncontacted(self): -# return [n for n in self if n.id not in self.contacted] diff --git a/libp2p/kademlia/protocol.py b/libp2p/kademlia/protocol.py index d1936f5..11400c0 100644 --- a/libp2p/kademlia/protocol.py +++ b/libp2p/kademlia/protocol.py @@ -49,9 +49,6 @@ class KademliaProtocol(RPCProtocol): return sender def rpc_ping(self, sender, nodeid): - print ("RPC PING") - print (sender) - node_id = ID(nodeid) peer_data = PeerData() #pylint: disable=no-value-for-parameter addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))] @@ -62,9 +59,6 @@ class KademliaProtocol(RPCProtocol): return self.source_node.peer_id def rpc_store(self, sender, nodeid, key, value): - print ("RPC STORE") - print (sender) - node_id = ID(nodeid) peer_data = PeerData() #pylint: disable=no-value-for-parameter addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))] @@ -94,9 +88,6 @@ class KademliaProtocol(RPCProtocol): return list(map(tuple, neighbors)) def rpc_find_value(self, sender, nodeid, key): - print ("RPC_FIND_VALUE") - print (sender) - node_id = ID(nodeid) peer_data = PeerData() #pylint: disable=no-value-for-parameter addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))] @@ -145,10 +136,6 @@ class KademliaProtocol(RPCProtocol): is closer than the closest in that list, then store the key/value on the new node (per section 2.5 of the paper) """ - - print ("Welcome if new") - print (node) - if not self.router.is_new_node(node): return diff --git a/libp2p/kademlia/routing.py b/libp2p/kademlia/routing.py index e4b49d0..7d97494 100644 --- a/libp2p/kademlia/routing.py +++ b/libp2p/kademlia/routing.py @@ -151,11 +151,7 @@ class RoutingTable: self.buckets[index].remove_node(node) def is_new_node(self, node): - print ("IN IS NEW NODE") - print (node) - print (self.buckets) index = self.get_bucket_for(node) - print (index) return self.buckets[index].is_new_node(node) def add_contact(self, node): @@ -178,15 +174,7 @@ class RoutingTable: """ Get the index of the bucket that the given node would fall into. """ - print ("IN GET BUKCKET FOR") - print (node) - print (node.long_id) - print (self.buckets) for index, bucket in enumerate(self.buckets): - print ("IN ENUMERATE") - print (index) - print (bucket) - print (bucket.range) if node.long_id < bucket.range[1]: return index # we should never be here, but make linter happy