def test_setup(): """ nodes connect to any peer and do a lookup for them selfs """ proto = get_wired_protocol() wire = proto.wire other = routing_table() # lookup self proto.bootstrap(nodes=[other.this_node]) msg = wire.poll(other.this_node) assert msg == ('find_node', proto.routing.this_node, proto.routing.this_node.pubkey) assert wire.poll(other.this_node) is None assert wire.messages == [] # respond with neighbours closest = other.neighbours(kademlia.Node(msg[2])) assert len(closest) == kademlia.k_bucket_size proto.recv_neighbours(random_node(), closest) # expect another lookup msg = wire.poll(closest[0]) assert msg == ('find_node', proto.routing.this_node, proto.routing.this_node.pubkey) # and pings for all nodes for node in closest: msg = wire.poll(node) assert msg[0] == 'ping' # nothing else assert wire.messages == []
def test_find_node_timeout(): proto = get_wired_protocol() other = routing_table() wire = proto.wire # lookup self proto.bootstrap(nodes=[other.this_node]) msg = wire.poll(other.this_node) assert msg == ('find_node', proto.routing.this_node, proto.routing.this_node.pubkey) assert wire.poll(other.this_node) is None assert wire.messages == [] # do timeout time.sleep(kademlia.k_request_timeout) # respond with neighbours closest = other.neighbours(kademlia.Node(msg[2])) assert len(closest) == kademlia.k_bucket_size proto.recv_neighbours(random_node(), closest) # expect pings, but no other lookup msg = wire.poll(closest[0]) assert msg[0] == 'ping' assert wire.poll(closest[0]) is None wire.empty() assert wire.messages == []
def test_split(): node = random_node() routing = kademlia.RoutingTable(node) assert len(routing.buckets) == 1 # create very close node for i in range(kademlia.k_bucket_size): node = kademlia.Node(int_to_big_endian(node.id + 1)) assert routing.buckets[0].in_range(node) routing.add_node(node) assert len(routing.buckets) == 1 assert len(routing.buckets[0]) == kademlia.k_bucket_size node = kademlia.Node(int_to_big_endian(node.id + 1)) assert routing.buckets[0].in_range(node) routing.add_node(node) assert len(routing.buckets[0]) <= kademlia.k_bucket_size assert len(routing.buckets) <= 512
def random_node(): return kademlia.Node(random_pubkey())