def test_iter_around(routing_table, center_node_id): reference_node_id = NodeIDFactory.at_log_distance(center_node_id, 100) node_ids = tuple( NodeIDFactory.at_log_distance(reference_node_id, distance) for distance in (1, 2, 100, 200)) for node_id in node_ids: routing_table.update(node_id) assert tuple( routing_table.iter_nodes_around(reference_node_id)) == node_ids assert tuple(routing_table.iter_nodes_around(node_ids[0])) == node_ids assert tuple(routing_table.iter_nodes_around(node_ids[-1])) != node_ids
def test_get_nodes_at_log_distance(routing_table, center_node_id, bucket_size): nodes = tuple( NodeIDFactory.at_log_distance(center_node_id, 200) for _ in range(bucket_size)) farther_nodes = tuple( NodeIDFactory.at_log_distance(center_node_id, 201) for _ in range(5)) closer_nodes = tuple( NodeIDFactory.at_log_distance(center_node_id, 199) for _ in range(5)) for node_id in nodes + farther_nodes + closer_nodes: routing_table.update(node_id) assert set(routing_table.get_nodes_at_log_distance(200)) == set(nodes)
def test_update(routing_table, center_node_id): node_id_1 = NodeIDFactory.at_log_distance(center_node_id, 200) node_id_2 = NodeIDFactory.at_log_distance(center_node_id, 200) routing_table.update(node_id_1) routing_table.update(node_id_2) assert routing_table.get_nodes_at_log_distance(200) == (node_id_2, node_id_1) routing_table.update(node_id_2) assert routing_table.get_nodes_at_log_distance(200) == (node_id_2, node_id_1) routing_table.update(node_id_1) assert routing_table.get_nodes_at_log_distance(200) == (node_id_1, node_id_2)
async def test_ping_handler_updates_routing_table( ping_handler_service, inbound_message_channels, outbound_message_channels, local_enr, remote_enr, routing_table, ): distance = compute_log_distance(remote_enr.node_id, local_enr.node_id) other_node_id = NodeIDFactory.at_log_distance(local_enr.node_id, distance) routing_table.update(other_node_id) assert routing_table.get_nodes_at_log_distance(distance) == ( other_node_id, remote_enr.node_id, ) ping = PingMessageFactory() inbound_message = InboundMessageFactory( message=ping, sender_node_id=remote_enr.node_id, ) await inbound_message_channels[0].send(inbound_message) await wait_all_tasks_blocked() assert routing_table.get_nodes_at_log_distance(distance) == ( remote_enr.node_id, other_node_id, )
def test_add(routing_table, center_node_id): assert routing_table.get_nodes_at_log_distance(255) == () node_id_1 = NodeIDFactory.at_log_distance(center_node_id, 255) routing_table.update(node_id_1) assert routing_table.get_nodes_at_log_distance(255) == (node_id_1, ) node_id_2 = NodeIDFactory.at_log_distance(center_node_id, 255) routing_table.update(node_id_2) assert routing_table.get_nodes_at_log_distance(255) == (node_id_2, node_id_1) node_id_3 = NodeIDFactory.at_log_distance(center_node_id, 255) routing_table.update(node_id_3) assert routing_table.get_nodes_at_log_distance(255) == (node_id_2, node_id_1) node_id_4 = NodeIDFactory.at_log_distance(center_node_id, 1) routing_table.update(node_id_4) assert routing_table.get_nodes_at_log_distance(1) == (node_id_4, )
def test_least_recently_updated_distance(routing_table, center_node_id): with pytest.raises(ValueError): routing_table.get_least_recently_updated_log_distance() node_id_1 = NodeIDFactory.at_log_distance(center_node_id, 200) routing_table.update(node_id_1) assert routing_table.get_least_recently_updated_log_distance() == 200 node_id_2 = NodeIDFactory.at_log_distance(center_node_id, 100) routing_table.update(node_id_2) assert routing_table.get_least_recently_updated_log_distance() == 200 routing_table.update(node_id_1) assert routing_table.get_least_recently_updated_log_distance() == 100 routing_table.remove(node_id_1) assert routing_table.get_least_recently_updated_log_distance() == 100 routing_table.remove(node_id_2) with pytest.raises(ValueError): routing_table.get_least_recently_updated_log_distance()
def test_remove(routing_table, center_node_id): node_id_1 = NodeIDFactory.at_log_distance(center_node_id, 200) node_id_2 = NodeIDFactory.at_log_distance(center_node_id, 200) node_id_3 = NodeIDFactory.at_log_distance(center_node_id, 200) node_id_4 = NodeIDFactory.at_log_distance(center_node_id, 200) routing_table.update(node_id_1) routing_table.update(node_id_2) routing_table.update(node_id_3) routing_table.update(node_id_4) assert routing_table.get_nodes_at_log_distance(200) == (node_id_2, node_id_1) routing_table.remove( node_id_4) # remove from replacement cache, shouldn't appear again routing_table.remove(node_id_2) assert routing_table.get_nodes_at_log_distance(200) == (node_id_1, node_id_3) routing_table.remove(node_id_3) assert routing_table.get_nodes_at_log_distance(200) == (node_id_1, ) routing_table.remove(node_id_1) assert routing_table.get_nodes_at_log_distance(200) == () routing_table.remove(node_id_1) # shouldn't raise
def test_fill_bucket(routing_table, center_node_id, bucket_size): assert not routing_table.get_nodes_at_log_distance(200) for _ in range(2 * bucket_size): routing_table.update(NodeIDFactory.at_log_distance( center_node_id, 200)) assert len(routing_table.get_nodes_at_log_distance(200)) == bucket_size
async def test_rpc_tableInfo(make_request, routing_table): local_node_id = routing_table.center_node_id # 16/16 at furthest distance for _ in range(routing_table.bucket_size * 2): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 256)) # 16/8 at next bucket for _ in range(int(routing_table.bucket_size * 1.5)): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 255)) # 16/4 at next bucket for _ in range(int(routing_table.bucket_size * 1.25)): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 254)) # 16 in this one for _ in range(int(routing_table.bucket_size)): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 253)) # 8 in this one for _ in range(int(routing_table.bucket_size // 2)): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 252)) # 4 in this one for _ in range(int(routing_table.bucket_size // 4)): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 251)) table_info = await make_request("discv5_routingTableInfo") assert decode_hex(table_info["center_node_id"]) == routing_table.center_node_id assert table_info["bucket_size"] == routing_table.bucket_size assert table_info["num_buckets"] == routing_table.num_buckets assert len(table_info["buckets"]) == 6 bucket_256 = table_info["buckets"]["256"] bucket_255 = table_info["buckets"]["255"] bucket_254 = table_info["buckets"]["254"] bucket_253 = table_info["buckets"]["253"] bucket_252 = table_info["buckets"]["252"] bucket_251 = table_info["buckets"]["251"] assert bucket_256["idx"] == 256 assert bucket_256["is_full"] is True assert len(bucket_256["nodes"]) == routing_table.bucket_size assert len(bucket_256["replacement_cache"]) == routing_table.bucket_size assert bucket_255["idx"] == 255 assert bucket_255["is_full"] is True assert len(bucket_255["nodes"]) == routing_table.bucket_size assert len(bucket_255["replacement_cache"]) == routing_table.bucket_size // 2 assert bucket_254["idx"] == 254 assert bucket_254["is_full"] is True assert len(bucket_254["nodes"]) == routing_table.bucket_size assert len(bucket_254["replacement_cache"]) == routing_table.bucket_size // 4 assert bucket_253["idx"] == 253 assert bucket_253["is_full"] is True assert len(bucket_253["nodes"]) == routing_table.bucket_size assert not bucket_253["replacement_cache"] assert bucket_252["idx"] == 252 assert bucket_252["is_full"] is False assert len(bucket_252["nodes"]) == routing_table.bucket_size // 2 assert not bucket_252["replacement_cache"] assert bucket_251["idx"] == 251 assert bucket_251["is_full"] is False assert len(bucket_251["nodes"]) == routing_table.bucket_size // 4 assert not bucket_251["replacement_cache"]
async def test_rpc_tableInfo_web3(w3, routing_table, rpc_server): local_node_id = routing_table.center_node_id # 16/16 at furthest distance for _ in range(routing_table.bucket_size * 2): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 256)) # 16/8 at next bucket for _ in range(int(routing_table.bucket_size * 1.5)): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 255)) # 16/4 at next bucket for _ in range(int(routing_table.bucket_size * 1.25)): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 254)) # 16 in this one for _ in range(int(routing_table.bucket_size)): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 253)) # 8 in this one for _ in range(int(routing_table.bucket_size // 2)): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 252)) # 4 in this one for _ in range(int(routing_table.bucket_size // 4)): routing_table.update(NodeIDFactory.at_log_distance(local_node_id, 251)) table_info = await trio.to_thread.run_sync(w3.discv5.get_routing_table_info) assert table_info.center_node_id == routing_table.center_node_id assert table_info.bucket_size == routing_table.bucket_size assert table_info.num_buckets == routing_table.num_buckets assert len(table_info.buckets) == 6 bucket_256 = table_info.buckets[256] bucket_255 = table_info.buckets[255] bucket_254 = table_info.buckets[254] bucket_253 = table_info.buckets[253] bucket_252 = table_info.buckets[252] bucket_251 = table_info.buckets[251] assert bucket_256.idx == 256 assert bucket_256.is_full is True assert len(bucket_256.nodes) == routing_table.bucket_size assert len(bucket_256.replacement_cache) == routing_table.bucket_size assert bucket_255.idx == 255 assert bucket_255.is_full is True assert len(bucket_255.nodes) == routing_table.bucket_size assert len(bucket_255.replacement_cache) == routing_table.bucket_size // 2 assert bucket_254.idx == 254 assert bucket_254.is_full is True assert len(bucket_254.nodes) == routing_table.bucket_size assert len(bucket_254.replacement_cache) == routing_table.bucket_size // 4 assert bucket_253.idx == 253 assert bucket_253.is_full is True assert len(bucket_253.nodes) == routing_table.bucket_size assert not bucket_253.replacement_cache assert bucket_252.idx == 252 assert bucket_252.is_full is False assert len(bucket_252.nodes) == routing_table.bucket_size // 2 assert not bucket_253.replacement_cache assert bucket_251.idx == 251 assert bucket_251.is_full is False assert len(bucket_251.nodes) == routing_table.bucket_size // 4 assert not bucket_253.replacement_cache