Skip to content

Commit 704be78

Browse files
author
cms5347
committed
Changed lookups to go from one node to a random key, instead of just between two nodes
1 parent 70afb2d commit 704be78

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

RunSimulation.py

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
# The rate at which we will disable nodes
2020
node_disable_n = int(sys.argv[4])
21-
print node_disable_n
2221
sim = Simulation(seed, network_size)
2322

2423

node.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
import math
44
import hashlib
55

6+
7+
def make_hash(id_str):
8+
"""
9+
Make a hash which is truncated to the correct bit length
10+
based on kademliaConstants.bit_string_size.
11+
"""
12+
hash_id = int(hashlib.sha1(id_str).hexdigest(), 16)
13+
hash_id = hash_id % int(math.pow(2, kademliaConstants.bit_string_size))
14+
return hash_id
15+
616
class Node(object):
717
"""
818
Implementation of a Kademlia node object.
@@ -28,8 +38,7 @@ def __init__(self, node_name, random):
2838
# responsible for storing the RTT
2939

3040
# Get the hash and truncate to the correct number of bits
31-
self.id = int(hashlib.sha1(self.node_name).hexdigest(), 16)
32-
self.id = self.id % int(math.pow(2, kademliaConstants.bit_string_size))
41+
self.id = make_hash(node_name)
3342

3443
self.kBuckets = list()
3544
self.disabled = False # Whether or not this node is accessible

simulation.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
# can be used to run different simulations
44

55
import random
6-
6+
import hashlib
7+
import math
78
import kademliaConstants
8-
from node import Node
9+
from node import Node, make_hash
910

1011

1112
class Simulation:
@@ -32,12 +33,15 @@ def perform_node_lookup(self):
3233
Perform a node lookup between two random nodes, return the
3334
time that it took to complete the lookup.
3435
"""
35-
nodes = self.rand.sample(self.nodes, 2)
36+
node = self.rand.choice(self.nodes)
3637

37-
target_node = nodes[1]
38-
start_node = nodes[0]
38+
# Generate a random key to look for, use random_IP as a random
39+
# string generator
40+
target_key = hashlib.sha1(self.random_IP()).hexdigest()
41+
target_key = int(target_key, 16)
42+
target_key = target_key % (int(math.pow(2, kademliaConstants.bit_string_size)))
3943

40-
start_node.lookup_node(target_node.id)
44+
node.lookup_node(target_key)
4145

4246

4347

0 commit comments

Comments
 (0)