Пример #1
0
def test_routingtable_split_bucket():
    table = kademlia.RoutingTable(random_node())
    assert len(table.buckets) == 1
    old_bucket = table.buckets[0]
    table.split_bucket(0)
    assert len(table.buckets) == 2
    assert old_bucket not in table.buckets
Пример #2
0
def test_routingtable_remove_node():
    table = kademlia.RoutingTable(random_node())
    node1 = random_node()
    assert table.add_node(node1) is None
    assert node1 in table

    table.remove_node(node1)

    assert node1 not in table
Пример #3
0
def test_routingtable_add_node():
    table = kademlia.RoutingTable(random_node())
    for i in range(table.buckets[0].k):
        # As long as the bucket is not full, the new node is added to the bucket and None is
        # returned.
        assert table.add_node(random_node()) is None
        assert len(table.buckets) == 1
        assert len(table) == i + 1
    assert table.buckets[0].is_full
    # Now that the bucket is full, an add_node() should cause it to be split.
    assert table.add_node(random_node()) is None
Пример #4
0
def test_routingtable_get_random_nodes():
    table = kademlia.RoutingTable(random_node())
    for i in range(100):
        assert table.add_node(random_node()) is None

    nodes = list(table.get_random_nodes(50))
    assert len(nodes) == 50
    assert len(set(nodes)) == 50

    # If we ask for more nodes than what the routing table contains, we'll get only what the
    # routing table contains, without duplicates.
    nodes = list(table.get_random_nodes(200))
    assert len(nodes) == 100
    assert len(set(nodes)) == 100
Пример #5
0
 def __init__(self, privkey: datatypes.PrivateKey,
              address: kademlia.Address,
              bootstrap_nodes: Tuple[kademlia.Node, ...],
              cancel_token: CancelToken) -> None:
     self.privkey = privkey
     self.address = address
     self.bootstrap_nodes = bootstrap_nodes
     self.this_node = kademlia.Node(self.pubkey, address)
     self.routing = kademlia.RoutingTable(self.this_node)
     self.pong_callbacks = CallbackManager()
     self.ping_callbacks = CallbackManager()
     self.neighbours_callbacks = CallbackManager()
     self.parity_pong_tokens: Dict[Hash32, Hash32] = {}
     self.cancel_token = cancel_token
Пример #6
0
def test_routingtable_neighbours():
    table = kademlia.RoutingTable(random_node())
    for i in range(1000):
        assert table.add_node(random_node()) is None
        assert i == len(table) - 1

    for i in range(100):
        node = random_node()
        nearest_bucket = table.buckets_by_distance_to(node.id)[0]
        if not nearest_bucket.nodes:
            continue
        # Change nodeid to something that is in this bucket's range.
        node_a = nearest_bucket.nodes[0]
        node_b = random_node(node_a.id + 1)
        assert node_a == table.neighbours(node_b.id)[0]
Пример #7
0
def test_routingtable_add_node_error():
    table = kademlia.RoutingTable(random_node())
    with pytest.raises(ValueError):
        table.add_node(random_node(kademlia.k_max_node_id + 1))
Пример #8
0
def test_routingtable_add_node_error():
    table = kademlia.RoutingTable(NodeFactory())
    with pytest.raises(ValueError):
        table.add_node(NodeFactory.with_nodeid(kademlia.k_max_node_id + 1))