|
| 1 | +import pytest |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +from p2p.peer import ( |
| 6 | + MsgBuffer, |
| 7 | +) |
| 8 | +from trinity.protocol.bcc.servers import BCCRequestServer |
| 9 | +from trinity.protocol.bcc.commands import ( |
| 10 | + BeaconBlocks, |
| 11 | +) |
| 12 | + |
| 13 | +from .helpers import ( |
| 14 | + get_fresh_chain_db, |
| 15 | + create_branch, |
| 16 | + get_directly_linked_peers_in_peer_pools, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +async def get_request_server_setup(request, event_loop, chain_db): |
| 21 | + alice, alice_peer_pool, bob, bob_peer_pool = await get_directly_linked_peers_in_peer_pools( |
| 22 | + request, |
| 23 | + event_loop, |
| 24 | + chain_db=chain_db, |
| 25 | + ) |
| 26 | + |
| 27 | + response_buffer = MsgBuffer() |
| 28 | + alice.add_subscriber(response_buffer) |
| 29 | + |
| 30 | + bob_request_server = BCCRequestServer(bob.context.chain_db, bob_peer_pool) |
| 31 | + asyncio.ensure_future(bob_request_server.run()) |
| 32 | + |
| 33 | + def finalizer(): |
| 34 | + event_loop.run_until_complete(bob_request_server.cancel()) |
| 35 | + |
| 36 | + return alice, response_buffer |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.asyncio |
| 40 | +async def test_get_single_block_by_slot(request, event_loop): |
| 41 | + chain_db = get_fresh_chain_db() |
| 42 | + alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) |
| 43 | + |
| 44 | + block_hash = chain_db.get_canonical_block_hash(0) |
| 45 | + alice.sub_proto.send_get_blocks(block_hash, 1) |
| 46 | + response = await response_buffer.msg_queue.get() |
| 47 | + |
| 48 | + assert isinstance(response.command, BeaconBlocks) |
| 49 | + assert response.payload == (chain_db.get_block_by_hash(block_hash),) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.asyncio |
| 53 | +async def test_get_single_block_by_hash(request, event_loop): |
| 54 | + chain_db = get_fresh_chain_db() |
| 55 | + alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) |
| 56 | + |
| 57 | + alice.sub_proto.send_get_blocks(0, 1) |
| 58 | + response = await response_buffer.msg_queue.get() |
| 59 | + |
| 60 | + assert isinstance(response.command, BeaconBlocks) |
| 61 | + assert response.payload == (chain_db.get_canonical_block_by_slot(0),) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.asyncio |
| 65 | +async def test_get_no_blocks(request, event_loop): |
| 66 | + chain_db = get_fresh_chain_db() |
| 67 | + alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) |
| 68 | + |
| 69 | + alice.sub_proto.send_get_blocks(0, 0) |
| 70 | + response = await response_buffer.msg_queue.get() |
| 71 | + |
| 72 | + assert isinstance(response.command, BeaconBlocks) |
| 73 | + assert len(response.payload) == 0 |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.asyncio |
| 77 | +async def test_get_unknown_block_by_slot(request, event_loop): |
| 78 | + chain_db = get_fresh_chain_db() |
| 79 | + alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) |
| 80 | + |
| 81 | + alice.sub_proto.send_get_blocks(100, 1) |
| 82 | + response = await response_buffer.msg_queue.get() |
| 83 | + |
| 84 | + assert isinstance(response.command, BeaconBlocks) |
| 85 | + assert len(response.payload) == 0 |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_get_unknown_block_by_hash(request, event_loop): |
| 90 | + chain_db = get_fresh_chain_db() |
| 91 | + alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) |
| 92 | + |
| 93 | + alice.sub_proto.send_get_blocks(b"\x00" * 32, 1) |
| 94 | + response = await response_buffer.msg_queue.get() |
| 95 | + |
| 96 | + assert isinstance(response.command, BeaconBlocks) |
| 97 | + assert len(response.payload) == 0 |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.asyncio |
| 101 | +async def test_get_canonical_block_range_by_slot(request, event_loop): |
| 102 | + chain_db = get_fresh_chain_db() |
| 103 | + base_branch = create_branch(3, root=chain_db.get_canonical_block_by_slot(0)) |
| 104 | + non_canonical_branch = create_branch(3, root=base_branch[-1]) |
| 105 | + canonical_branch = create_branch(4, root=base_branch[-1]) |
| 106 | + for branch in [base_branch, non_canonical_branch, canonical_branch]: |
| 107 | + chain_db.persist_block_chain(branch) |
| 108 | + |
| 109 | + alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) |
| 110 | + |
| 111 | + alice.sub_proto.send_get_blocks(2, 4) |
| 112 | + response = await response_buffer.msg_queue.get() |
| 113 | + |
| 114 | + assert isinstance(response.command, BeaconBlocks) |
| 115 | + assert len(response.payload) == 4 |
| 116 | + assert [block.slot for block in response.payload] == [2, 3, 4, 5] |
| 117 | + assert response.payload == base_branch[1:] + canonical_branch[:2] |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.asyncio |
| 121 | +async def test_get_canonical_block_range_by_hash(request, event_loop): |
| 122 | + chain_db = get_fresh_chain_db() |
| 123 | + base_branch = create_branch(3, root=chain_db.get_canonical_block_by_slot(0)) |
| 124 | + non_canonical_branch = create_branch(3, root=base_branch[-1]) |
| 125 | + canonical_branch = create_branch(4, root=base_branch[-1]) |
| 126 | + for branch in [base_branch, non_canonical_branch, canonical_branch]: |
| 127 | + chain_db.persist_block_chain(branch) |
| 128 | + |
| 129 | + alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) |
| 130 | + |
| 131 | + alice.sub_proto.send_get_blocks(base_branch[1].hash, 4) |
| 132 | + response = await response_buffer.msg_queue.get() |
| 133 | + |
| 134 | + assert isinstance(response.command, BeaconBlocks) |
| 135 | + assert len(response.payload) == 4 |
| 136 | + assert [block.slot for block in response.payload] == [2, 3, 4, 5] |
| 137 | + assert response.payload == base_branch[1:] + canonical_branch[:2] |
| 138 | + |
| 139 | + |
| 140 | +@pytest.mark.asyncio |
| 141 | +async def test_get_incomplete_canonical_block_range(request, event_loop): |
| 142 | + chain_db = get_fresh_chain_db() |
| 143 | + base_branch = create_branch(3, root=chain_db.get_canonical_block_by_slot(0)) |
| 144 | + non_canonical_branch = create_branch(3, root=base_branch[-1]) |
| 145 | + canonical_branch = create_branch(4, root=base_branch[-1]) |
| 146 | + for branch in [base_branch, non_canonical_branch, canonical_branch]: |
| 147 | + chain_db.persist_block_chain(branch) |
| 148 | + |
| 149 | + alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) |
| 150 | + |
| 151 | + alice.sub_proto.send_get_blocks(3, 10) |
| 152 | + response = await response_buffer.msg_queue.get() |
| 153 | + |
| 154 | + assert isinstance(response.command, BeaconBlocks) |
| 155 | + assert len(response.payload) == 5 |
| 156 | + assert [block.slot for block in response.payload] == [3, 4, 5, 6, 7] |
| 157 | + assert response.payload == base_branch[-1:] + canonical_branch |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.asyncio |
| 161 | +async def test_get_non_canonical_branch(request, event_loop): |
| 162 | + chain_db = get_fresh_chain_db() |
| 163 | + base_branch = create_branch(3, root=chain_db.get_canonical_block_by_slot(0)) |
| 164 | + non_canonical_branch = create_branch(3, root=base_branch[-1]) |
| 165 | + canonical_branch = create_branch(4, root=base_branch[-1]) |
| 166 | + for branch in [base_branch, non_canonical_branch, canonical_branch]: |
| 167 | + chain_db.persist_block_chain(branch) |
| 168 | + |
| 169 | + alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) |
| 170 | + |
| 171 | + alice.sub_proto.send_get_blocks(non_canonical_branch[1].hash, 3) |
| 172 | + response = await response_buffer.msg_queue.get() |
| 173 | + |
| 174 | + assert isinstance(response.command, BeaconBlocks) |
| 175 | + assert len(response.payload) == 1 |
| 176 | + assert [block.slot for block in response.payload] == [2] |
| 177 | + assert response.payload == (non_canonical_branch[1],) |
0 commit comments