|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from trinity.protocol.eth.peer import ETHPeer |
| 7 | +from trinity.protocol.eth.commands import GetBlockHeaders, GetNodeData |
| 8 | +from trinity.protocol.eth.requests import HeaderRequest |
| 9 | + |
| 10 | +from tests.trinity.core.peer_helpers import ( |
| 11 | + get_directly_linked_peers, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +logger = logging.getLogger('testing.p2p.PeerSubscriber') |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.asyncio |
| 19 | +async def test_peer_subscriber_filters_messages(request, event_loop): |
| 20 | + peer, remote = await get_directly_linked_peers( |
| 21 | + request, |
| 22 | + event_loop, |
| 23 | + peer1_class=ETHPeer, |
| 24 | + peer2_class=ETHPeer, |
| 25 | + ) |
| 26 | + await peer.events.started.wait() |
| 27 | + |
| 28 | + with peer.collect_sub_proto_messages() as collector: |
| 29 | + assert collector in peer._subscribers |
| 30 | + remote.sub_proto.send_get_node_data([b'\x00' * 32]) |
| 31 | + remote.sub_proto.send_get_block_headers(HeaderRequest(0, 1, 0, False)) |
| 32 | + remote.sub_proto.send_get_node_data([b'\x00' * 32]) |
| 33 | + remote.sub_proto.send_get_block_headers(HeaderRequest(1, 1, 0, False)) |
| 34 | + remote.sub_proto.send_get_node_data([b'\x00' * 32]) |
| 35 | + await asyncio.sleep(0.01) |
| 36 | + |
| 37 | + assert collector not in peer._subscribers |
| 38 | + |
| 39 | + # yeild to let remote and peer transmit. |
| 40 | + |
| 41 | + all_messages = collector.get_messages() |
| 42 | + assert len(all_messages) == 5 |
| 43 | + |
| 44 | + assert isinstance(all_messages[0][1], GetNodeData) |
| 45 | + assert isinstance(all_messages[1][1], GetBlockHeaders) |
| 46 | + assert isinstance(all_messages[2][1], GetNodeData) |
| 47 | + assert isinstance(all_messages[3][1], GetBlockHeaders) |
| 48 | + assert isinstance(all_messages[4][1], GetNodeData) |
| 49 | + |
| 50 | + # make sure it isn't still collecting |
| 51 | + remote.sub_proto.send_get_block_headers(HeaderRequest(1, 1, 0, False)) |
| 52 | + |
| 53 | + await asyncio.sleep(0.01) |
| 54 | + |
| 55 | + assert len(collector.get_messages()) == 0 |
0 commit comments