예제 #1
0
def routing_table(num_nodes=1000):
    node = random_node()
    routing = kademlia.RoutingTable(node)
    for i in range(num_nodes):
        routing.add_node(random_node())
        assert len(routing.buckets) <= i + 2
    assert len(routing.buckets) <= 512
    assert i == num_nodes - 1
    return routing
예제 #2
0
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
예제 #3
0
def create_json_bucket_test():
    doc = \
        """
    'node_ids': hex encoded ids in order in which they were added to the routing table
    'buckets' : buckets sorted asc by range
    """
    num_nodes = 100
    data = dict(node_ids=[], buckets=[], comment=doc)
    node = random_node()
    routing = kademlia.RoutingTable(node)
    data['local_node_id'] = hex(node.id)
    for r in [random_node() for i in range(num_nodes)]:
        routing.add_node(r)
        data['node_ids'].append(hex(r.id))

    for b in routing.buckets:
        jb = dict(start=hex(b.start), end=hex(b.end), node_ids=[hex(n.id) for n in b.nodes])
        data['buckets'].append(jb)

    return json.dumps(data, indent=2)