refactor peerinfo

This commit is contained in:
zixuanzh 2019-04-19 16:29:15 -04:00
parent c03f2f63d2
commit fb687dad09
4 changed files with 30 additions and 5 deletions

View File

@ -1,12 +1,19 @@
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
class KadPeerInfo(PeerInfo): class KadPeerInfo(PeerInfo):
def __init__(self, peer_id, peer_data): def __init__(self, peer_id, peer_data=None):
super(KadPeerInfo, self).__init__(peer_id, peer_data) super(KadPeerInfo, self).__init__(peer_id, peer_data)
self.long_id = int(peer_id.hex(), 16) 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')
def same_home_as(self, node): def same_home_as(self, node):
#TODO: handle more than one addr #TODO: handle more than one addr

View File

@ -2,8 +2,10 @@ import random
import asyncio import asyncio
import logging import logging
from multiaddr import Multiaddr
from rpcudp.protocol import RPCProtocol from rpcudp.protocol import RPCProtocol
from libp2p.peer.id import ID
from libp2p.peer.peerdata import PeerData
from .kad_peerinfo import KadPeerInfo from .kad_peerinfo import KadPeerInfo
from .routing import RoutingTable from .routing import RoutingTable
from .utils import digest from .utils import digest
@ -143,6 +145,10 @@ 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

View File

@ -151,7 +151,11 @@ 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):
@ -174,7 +178,15 @@ 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

View File

@ -7,9 +7,9 @@ from .peerdata import PeerData
class PeerInfo: class PeerInfo:
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
def __init__(self, peer_id, peer_data): def __init__(self, peer_id, peer_data=None):
self.peer_id = peer_id self.peer_id = peer_id
self.addrs = peer_data.get_addrs() self.addrs = peer_data.get_addrs() if peer_data else None
def info_from_p2p_addr(addr): def info_from_p2p_addr(addr):