replace id with peer_id

This commit is contained in:
zixuanzh 2019-04-19 15:26:30 -04:00
parent 9ddbd18ded
commit c03f2f63d2

View File

@ -34,24 +34,24 @@ class KBucket:
two = KBucket(midpoint + 1, self.range[1], self.ksize) two = KBucket(midpoint + 1, self.range[1], self.ksize)
for node in self.nodes.values(): for node in self.nodes.values():
bucket = one if node.long_id <= midpoint else two bucket = one if node.long_id <= midpoint else two
bucket.nodes[node.id] = node bucket.nodes[node.peer_id] = node
return (one, two) return (one, two)
def remove_node(self, node): def remove_node(self, node):
if node.id not in self.nodes: if node.peer_id not in self.nodes:
return return
# delete node, and see if we can add a replacement # delete node, and see if we can add a replacement
del self.nodes[node.id] del self.nodes[node.peer_id]
if self.replacement_nodes: if self.replacement_nodes:
newnode = self.replacement_nodes.pop() newnode = self.replacement_nodes.pop()
self.nodes[newnode.id] = newnode self.nodes[newnode.peer_id] = newnode
def has_in_range(self, node): def has_in_range(self, node):
return self.range[0] <= node.long_id <= self.range[1] return self.range[0] <= node.long_id <= self.range[1]
def is_new_node(self, node): def is_new_node(self, node):
return node.id not in self.nodes return node.peer_id not in self.nodes
def add_node(self, node): def add_node(self, node):
""" """
@ -61,11 +61,11 @@ class KBucket:
If the bucket is full, keep track of node in a replacement list, If the bucket is full, keep track of node in a replacement list,
per section 4.1 of the paper. per section 4.1 of the paper.
""" """
if node.id in self.nodes: if node.peer_id in self.nodes:
del self.nodes[node.id] del self.nodes[node.peer_id]
self.nodes[node.id] = node self.nodes[node.peer_id] = node
elif len(self) < self.ksize: elif len(self) < self.ksize:
self.nodes[node.id] = node self.nodes[node.peer_id] = node
else: else:
self.replacement_nodes.push(node) self.replacement_nodes.push(node)
return False return False
@ -73,7 +73,7 @@ class KBucket:
def depth(self): def depth(self):
vals = self.nodes.values() vals = self.nodes.values()
sprefix = shared_prefix([bytes_to_bit_string(n.id) for n in vals]) sprefix = shared_prefix([bytes_to_bit_string(n.peer_id) for n in vals])
return len(sprefix) return len(sprefix)
def head(self): def head(self):
@ -185,7 +185,7 @@ class RoutingTable:
nodes = [] nodes = []
for neighbor in TableTraverser(self, node): for neighbor in TableTraverser(self, node):
notexcluded = exclude is None or not neighbor.same_home_as(exclude) notexcluded = exclude is None or not neighbor.same_home_as(exclude)
if neighbor.id != node.id and notexcluded: if neighbor.peer_id != node.peer_id and notexcluded:
heapq.heappush(nodes, (node.distance_to(neighbor), neighbor)) heapq.heappush(nodes, (node.distance_to(neighbor), neighbor))
if len(nodes) == k: if len(nodes) == k:
break break