async def test_peer_subscriber_filters_messages(request, event_loop): peer, remote = await get_directly_linked_peers( request, event_loop, peer1_class=ETHPeer, peer2_class=ETHPeer, ) header_subscriber = HeadersSubscriber() all_subscriber = AllSubscriber() peer.add_subscriber(header_subscriber) peer.add_subscriber(all_subscriber) remote.sub_proto.send_get_node_data(NodeDataRequest([b'\x00' * 32])) remote.sub_proto.send_get_block_headers(HeaderRequest(0, 1, 0, False)) remote.sub_proto.send_get_node_data(NodeDataRequest([b'\x00' * 32])) remote.sub_proto.send_get_block_headers(HeaderRequest(1, 1, 0, False)) remote.sub_proto.send_get_node_data(NodeDataRequest([b'\x00' * 32])) # yeild to let remote and peer transmit. await asyncio.sleep(0.01) assert header_subscriber.queue_size == 2 assert all_subscriber.queue_size == 5
def test_node_data_request_with_fully_invalid_response(): node_keys, nodes = mk_node_data(10) request = NodeDataRequest(node_keys) # construct a unique set of other nodes other_nodes = tuple(set(mk_node() for _ in range(10)).difference(nodes)) other_node_data = tuple((keccak(node), node) for node in other_nodes) with pytest.raises(ValidationError): request.validate_response(other_node_data)
def test_node_data_request_with_partial_response(): node_keys, nodes = mk_node_data(10) request = NodeDataRequest(node_keys) node_data = tuple(zip(node_keys, nodes)) request.validate_response(node_data[3:]) request.validate_response(node_data[:3]) request.validate_response((node_data[1], node_data[8], node_data[4]))
async def test_peer_subscriber_filters_messages(request, event_loop): peer, remote = await get_directly_linked_peers( request, event_loop, peer1_class=ETHPeer, peer2_class=ETHPeer, ) await peer.events.started.wait() with peer.collect_sub_proto_messages() as collector: assert collector in peer._subscribers remote.sub_proto.send_get_node_data(NodeDataRequest([b'\x00' * 32])) remote.sub_proto.send_get_block_headers(HeaderRequest(0, 1, 0, False)) remote.sub_proto.send_get_node_data(NodeDataRequest([b'\x00' * 32])) remote.sub_proto.send_get_block_headers(HeaderRequest(1, 1, 0, False)) remote.sub_proto.send_get_node_data(NodeDataRequest([b'\x00' * 32])) await asyncio.sleep(0.01) assert collector not in peer._subscribers # yield to let remote and peer transmit. all_messages = collector.get_messages() assert len(all_messages) == 5 assert isinstance(all_messages[0][1], GetNodeData) assert isinstance(all_messages[1][1], GetBlockHeaders) assert isinstance(all_messages[2][1], GetNodeData) assert isinstance(all_messages[3][1], GetBlockHeaders) assert isinstance(all_messages[4][1], GetNodeData) # make sure it isn't still collecting remote.sub_proto.send_get_block_headers(HeaderRequest(1, 1, 0, False)) await asyncio.sleep(0.01) assert len(collector.get_messages()) == 0
def test_node_data_request_with_full_response(): node_keys, nodes = mk_node_data(10) request = NodeDataRequest(node_keys) node_data = tuple(zip(node_keys, nodes)) request.validate_response(node_data)
def test_node_data_request_empty_response_is_valid(): node_keys, _ = mk_node_data(10) request = NodeDataRequest(node_keys) request.validate_response(tuple())