fix all tests
This commit is contained in:
parent
fb687dad09
commit
1512ae27a1
|
@ -1,8 +1,10 @@
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from .kad_peerinfo import KadPeerInfo, KadPeerHeap
|
from multiaddr import Multiaddr
|
||||||
from libp2p.peer.id import ID
|
from libp2p.peer.id import ID
|
||||||
|
from libp2p.peer.peerdata import PeerData
|
||||||
|
from .kad_peerinfo import KadPeerInfo, KadPeerHeap
|
||||||
from .utils import gather_dict
|
from .utils import gather_dict
|
||||||
|
|
||||||
|
|
||||||
|
@ -184,4 +186,14 @@ class RPCFindResponse:
|
||||||
be set.
|
be set.
|
||||||
"""
|
"""
|
||||||
nodelist = self.response[1] or []
|
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
|
||||||
|
|
|
@ -1,19 +1,28 @@
|
||||||
import heapq
|
import heapq
|
||||||
import multihash
|
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
from libp2p.peer.peerinfo import PeerInfo
|
from libp2p.peer.peerinfo import PeerInfo
|
||||||
|
from .utils import digest
|
||||||
|
|
||||||
|
P_IP = "ip4"
|
||||||
|
P_UDP = "udp"
|
||||||
|
|
||||||
class KadPeerInfo(PeerInfo):
|
class KadPeerInfo(PeerInfo):
|
||||||
def __init__(self, peer_id, peer_data=None):
|
def __init__(self, peer_id, peer_data=None):
|
||||||
super(KadPeerInfo, self).__init__(peer_id, peer_data)
|
super(KadPeerInfo, self).__init__(peer_id, peer_data)
|
||||||
print ("Kad Peer Info")
|
|
||||||
print (peer_id)
|
# pylint: disable=protected-access
|
||||||
print (peer_data)
|
self.peer_id = peer_id._id_str
|
||||||
sha1 = multihash.Func.sha1
|
self.long_id = int(digest(peer_id._id_str).hex(), 16)
|
||||||
mh_digest = multihash.digest(peer_id.pretty().encode('utf-8'), sha1)
|
|
||||||
self.peer_id = peer_id.pretty()
|
self.addrs = peer_data.get_addrs() if peer_data else None
|
||||||
self.long_id = int.from_bytes(mh_digest.encode(), byteorder='big')
|
|
||||||
|
# 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):
|
def same_home_as(self, node):
|
||||||
#TODO: handle more than one addr
|
#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.
|
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):
|
def __repr__(self):
|
||||||
return repr([self.long_id, str(self.addrs[0])])
|
return repr([self.long_id, self.ip, self.port])
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.addrs[0])
|
return "%s:%s" % (self.ip, str(self.port))
|
||||||
|
|
||||||
class KadPeerHeap:
|
class KadPeerHeap:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -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]
|
|
|
@ -49,9 +49,6 @@ class KademliaProtocol(RPCProtocol):
|
||||||
return sender
|
return sender
|
||||||
|
|
||||||
def rpc_ping(self, sender, nodeid):
|
def rpc_ping(self, sender, nodeid):
|
||||||
print ("RPC PING")
|
|
||||||
print (sender)
|
|
||||||
|
|
||||||
node_id = ID(nodeid)
|
node_id = ID(nodeid)
|
||||||
peer_data = PeerData() #pylint: disable=no-value-for-parameter
|
peer_data = PeerData() #pylint: disable=no-value-for-parameter
|
||||||
addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))]
|
addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))]
|
||||||
|
@ -62,9 +59,6 @@ class KademliaProtocol(RPCProtocol):
|
||||||
return self.source_node.peer_id
|
return self.source_node.peer_id
|
||||||
|
|
||||||
def rpc_store(self, sender, nodeid, key, value):
|
def rpc_store(self, sender, nodeid, key, value):
|
||||||
print ("RPC STORE")
|
|
||||||
print (sender)
|
|
||||||
|
|
||||||
node_id = ID(nodeid)
|
node_id = ID(nodeid)
|
||||||
peer_data = PeerData() #pylint: disable=no-value-for-parameter
|
peer_data = PeerData() #pylint: disable=no-value-for-parameter
|
||||||
addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))]
|
addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))]
|
||||||
|
@ -94,9 +88,6 @@ class KademliaProtocol(RPCProtocol):
|
||||||
return list(map(tuple, neighbors))
|
return list(map(tuple, neighbors))
|
||||||
|
|
||||||
def rpc_find_value(self, sender, nodeid, key):
|
def rpc_find_value(self, sender, nodeid, key):
|
||||||
print ("RPC_FIND_VALUE")
|
|
||||||
print (sender)
|
|
||||||
|
|
||||||
node_id = ID(nodeid)
|
node_id = ID(nodeid)
|
||||||
peer_data = PeerData() #pylint: disable=no-value-for-parameter
|
peer_data = PeerData() #pylint: disable=no-value-for-parameter
|
||||||
addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))]
|
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
|
is closer than the closest in that list, then store the key/value
|
||||||
on the new node (per section 2.5 of the paper)
|
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):
|
if not self.router.is_new_node(node):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -151,11 +151,7 @@ class RoutingTable:
|
||||||
self.buckets[index].remove_node(node)
|
self.buckets[index].remove_node(node)
|
||||||
|
|
||||||
def is_new_node(self, node):
|
def is_new_node(self, node):
|
||||||
print ("IN IS NEW NODE")
|
|
||||||
print (node)
|
|
||||||
print (self.buckets)
|
|
||||||
index = self.get_bucket_for(node)
|
index = self.get_bucket_for(node)
|
||||||
print (index)
|
|
||||||
return self.buckets[index].is_new_node(node)
|
return self.buckets[index].is_new_node(node)
|
||||||
|
|
||||||
def add_contact(self, 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.
|
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):
|
for index, bucket in enumerate(self.buckets):
|
||||||
print ("IN ENUMERATE")
|
|
||||||
print (index)
|
|
||||||
print (bucket)
|
|
||||||
print (bucket.range)
|
|
||||||
if node.long_id < bucket.range[1]:
|
if node.long_id < bucket.range[1]:
|
||||||
return index
|
return index
|
||||||
# we should never be here, but make linter happy
|
# we should never be here, but make linter happy
|
||||||
|
|
Loading…
Reference in New Issue
Block a user