refactor KadPeerInfo construction

This commit is contained in:
zixuanzh 2019-04-19 20:44:17 -04:00
parent 1512ae27a1
commit 69f9aa5f0e
4 changed files with 31 additions and 58 deletions

View File

@ -1,10 +1,7 @@
from collections import Counter
import logging
from multiaddr import Multiaddr
from libp2p.peer.id import ID
from libp2p.peer.peerdata import PeerData
from .kad_peerinfo import KadPeerInfo, KadPeerHeap
from .kad_peerinfo import KadPeerHeap, create_kad_peerinfo
from .utils import gather_dict
@ -186,14 +183,4 @@ class RPCFindResponse:
be set.
"""
nodelist = self.response[1] or []
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
return [create_kad_peerinfo(*nodeple) for nodeple in nodelist]

View File

@ -1,7 +1,11 @@
import heapq
import random
from operator import itemgetter
from multiaddr import Multiaddr
from libp2p.peer.peerinfo import PeerInfo
from libp2p.peer.id import ID
from libp2p.peer.peerdata import PeerData
from .utils import digest
P_IP = "ip4"
@ -126,3 +130,14 @@ class KadPeerHeap:
def get_uncontacted(self):
return [n for n in self if n.peer_id not in self.contacted]
def create_kad_peerinfo(raw_node_id=None, sender_ip=None, sender_port=None):
node_id = ID(raw_node_id) if raw_node_id else ID(digest(random.getrandbits(255)))
peer_data = None
if sender_ip and sender_port:
peer_data = PeerData() #pylint: disable=no-value-for-parameter
addr = [Multiaddr("/"+ P_IP +"/" + str(sender_ip) + "/"\
+ P_UDP + "/" + str(sender_port))]
peer_data.add_addrs(addr)
return KadPeerInfo(node_id, peer_data)

View File

@ -1,18 +1,14 @@
"""
Package for interacting on the network at a high level.
"""
import random
import pickle
import asyncio
import logging
from multiaddr import Multiaddr
from libp2p.peer.id import ID
from libp2p.peer.peerdata import PeerData
from .protocol import KademliaProtocol
from .utils import digest
from .storage import ForgetfulStorage
from .kad_peerinfo import KadPeerInfo
from .kad_peerinfo import create_kad_peerinfo
from .crawling import ValueSpiderCrawl
from .crawling import NodeSpiderCrawl
@ -42,8 +38,7 @@ class Server:
self.ksize = ksize
self.alpha = alpha
self.storage = storage or ForgetfulStorage()
new_node_id = ID(node_id) if node_id else ID(digest(random.getrandbits(255)))
self.node = KadPeerInfo(new_node_id, None)
self.node = create_kad_peerinfo(node_id)
self.transport = None
self.protocol = None
self.refresh_loop = None
@ -90,7 +85,7 @@ class Server:
"""
results = []
for node_id in self.protocol.get_refresh_ids():
node = KadPeerInfo(node_id, None)
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)
@ -135,11 +130,7 @@ class Server:
async def bootstrap_node(self, addr):
result = await self.protocol.ping(addr, self.node.peer_id)
node_id = ID(result[1])
peer_data = PeerData() #pylint: disable=no-value-for-parameter
addr = [Multiaddr("/ip4/" + str(addr[0]) + "/udp/" + str(addr[1]))]
peer_data.add_addrs(addr)
return KadPeerInfo(node_id, peer_data) if result[0] else None
return create_kad_peerinfo(result[1], addr[0], addr[1]) if result[0] else None
async def get(self, key):
"""
@ -154,7 +145,7 @@ class Server:
if self.storage.get(dkey) is not None:
return self.storage.get(dkey)
node = KadPeerInfo(ID(dkey))
node = create_kad_peerinfo(dkey)
nearest = self.protocol.router.find_neighbors(node)
if not nearest:
log.warning("There are no known neighbors to get key %s", key)
@ -180,7 +171,7 @@ class Server:
Set the given SHA1 digest key (bytes) to the given value in the
network.
"""
node = KadPeerInfo(ID(dkey))
node = create_kad_peerinfo(dkey)
nearest = self.protocol.router.find_neighbors(node)
if not nearest:

View File

@ -2,13 +2,10 @@ import random
import asyncio
import logging
from multiaddr import Multiaddr
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 create_kad_peerinfo
from .routing import RoutingTable
from .utils import digest
log = logging.getLogger(__name__) # pylint: disable=invalid-name
@ -49,21 +46,13 @@ class KademliaProtocol(RPCProtocol):
return sender
def rpc_ping(self, sender, nodeid):
node_id = ID(nodeid)
peer_data = PeerData() #pylint: disable=no-value-for-parameter
addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))]
peer_data.add_addrs(addr)
source = KadPeerInfo(node_id, peer_data)
source = create_kad_peerinfo(nodeid, sender[0], sender[1])
self.welcome_if_new(source)
return self.source_node.peer_id
def rpc_store(self, sender, nodeid, key, value):
node_id = ID(nodeid)
peer_data = PeerData() #pylint: disable=no-value-for-parameter
addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))]
peer_data.add_addrs(addr)
source = KadPeerInfo(node_id, peer_data)
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'",
@ -74,25 +63,16 @@ class KademliaProtocol(RPCProtocol):
def rpc_find_node(self, sender, nodeid, key):
log.info("finding neighbors of %i in local table",
int(nodeid.hex(), 16))
node_id = ID(nodeid)
peer_data = PeerData() #pylint: disable=no-value-for-parameter
addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))]
peer_data.add_addrs(addr)
source = KadPeerInfo(node_id, peer_data)
source = create_kad_peerinfo(nodeid, sender[0], sender[1])
# source = Node(nodeid, sender[0], sender[1])
self.welcome_if_new(source)
node = KadPeerInfo(ID(key))
node = create_kad_peerinfo(key)
neighbors = self.router.find_neighbors(node, exclude=source)
return list(map(tuple, neighbors))
def rpc_find_value(self, sender, nodeid, key):
node_id = ID(nodeid)
peer_data = PeerData() #pylint: disable=no-value-for-parameter
addr = [Multiaddr("/ip4/" + str(sender[0]) + "/udp/" + str(sender[1]))]
peer_data.add_addrs(addr)
source = KadPeerInfo(node_id, peer_data)
source = create_kad_peerinfo(nodeid, sender[0], sender[1])
self.welcome_if_new(source)
value = self.storage.get(key, None)
@ -141,7 +121,7 @@ class KademliaProtocol(RPCProtocol):
log.info("never seen %s before, adding to router", node)
for key, value in self.storage:
keynode = KadPeerInfo(ID(digest(key)))
keynode = create_kad_peerinfo(key)
neighbors = self.router.find_neighbors(keynode)
if neighbors:
last = neighbors[-1].distance_to(keynode)