async def get_request_server_setup(request, event_loop, chain_db): genesis = await chain_db.coro_get_canonical_block_by_slot( SERENITY_CONFIG.GENESIS_SLOT, BeaconBlock, ) alice_chain_db = await get_chain_db((genesis, )) alice, alice_peer_pool, bob, bob_peer_pool = await get_directly_linked_peers_in_peer_pools( request, event_loop, alice_chain_db=alice_chain_db, bob_chain_db=chain_db, ) response_buffer = MsgBuffer() alice.add_subscriber(response_buffer) bob_request_server = BCCRequestServer(bob.context.chain_db, bob_peer_pool) asyncio.ensure_future(bob_request_server.run()) def finalizer(): event_loop.run_until_complete(bob_request_server.cancel()) request.addfinalizer(finalizer) return alice, response_buffer
async def get_request_server_setup(request, event_loop, event_bus, chain_db): genesis = await chain_db.coro_get_canonical_block_by_slot( SERENITY_CONFIG.GENESIS_SLOT, BeaconBlock, ) alice_chain_db = await get_chain_db((genesis, )) alice, alice_peer_pool, bob, bob_peer_pool = await get_directly_linked_peers_in_peer_pools( request, event_loop, alice_chain_db=alice_chain_db, bob_chain_db=chain_db, bob_peer_pool_event_bus=event_bus, ) response_buffer = MsgBuffer() alice.add_subscriber(response_buffer) async with run_peer_pool_event_server(event_bus, bob_peer_pool, handler_type=BCCPeerPoolEventServer): bob_request_server = BCCRequestServer(event_bus, TO_NETWORKING_BROADCAST_CONFIG, bob.context.chain_db) asyncio.ensure_future(bob_request_server.run()) await event_bus.wait_until_all_endpoints_subscribed_to( GetBeaconBlocksEvent) def finalizer(): event_loop.run_until_complete(bob_request_server.cancel()) request.addfinalizer(finalizer) yield alice, response_buffer
async def test_send_multiple_attestations(request, event_loop): alice, bob = await get_directly_linked_peers(request, event_loop) msg_buffer = MsgBuffer() bob.add_subscriber(msg_buffer) attestation_records = tuple( AttestationRecord( data=AttestationData( slot=0, shard=1, beacon_block_hash=ZERO_HASH32, epoch_boundary_hash=ZERO_HASH32, shard_block_hash=ZERO_HASH32, latest_crosslink_hash=ZERO_HASH32, justified_slot=0, justified_block_hash=ZERO_HASH32, ), participation_bitfield=b"\x00\x00\x00", custody_bitfield=b"\x00\x00\x00", ) for shard in range(10)) alice.sub_proto.send_attestation_records(attestation_records) message = await msg_buffer.msg_queue.get() assert isinstance(message.command, AttestationRecords) assert message.payload == attestation_records
async def test_bcc_receive_request_block_by_root(request, event_loop, event_bus): async with get_peer_and_receive_server( request, event_loop, event_bus, ) as (alice, alice_req_server, bob_recv_server, bob_msg_queue): alice_msg_buffer = MsgBuffer() alice.add_subscriber(alice_msg_buffer) blocks = get_blocks(bob_recv_server, num_blocks=1) # test: request from bob is issued and received by alice bob_recv_server._request_block_from_peers(blocks[0].signing_root) req = await alice_msg_buffer.msg_queue.get() assert req.payload['block_slot_or_root'] == blocks[0].signing_root # test: alice responds to the bob's request await alice_req_server.db.coro_persist_block( blocks[0], SerenityBeaconBlock, higher_slot_scoring, ) bob_recv_server._request_block_from_peers(blocks[0].signing_root) msg_block = await bob_msg_queue.get() assert blocks[0] == parse_resp_block_msg(msg_block)
async def test_bcc_receive_server_broadcast_block(request, event_loop, monkeypatch): alice, _, bob_recv_server, _ = await get_peer_and_receive_server( request, event_loop, ) block_non_orphan, block_orphan = get_blocks(bob_recv_server, num_blocks=2) alice_msg_buffer = MsgBuffer() alice.add_subscriber(alice_msg_buffer) # test: with `from_peer=alice`, bob broadcasts to all peers except alice. Therefore, alice # should fail to receive the block. bob_peers = bob_recv_server._peer_pool.connected_nodes.values() assert len(bob_peers) == 1 alice_in_bobs_peer_pool = tuple(bob_peers)[0] # NOTE: couldn't use `alice` directly, `from_peer` should be a `BCCPeer` in its PeerPool bob_recv_server._broadcast_block(block_orphan, from_peer=alice_in_bobs_peer_pool) with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(alice_msg_buffer.msg_queue.get(), 0.1) # test: with `from_peer=None` it broadcasts the block to all bob's peers. Try the orphan block # first. bob_recv_server._broadcast_block(block_orphan, from_peer=None) msg_block_orphan = await alice_msg_buffer.msg_queue.get() block_orphan_received = parse_new_block_msg(msg_block_orphan.payload) assert block_orphan_received.signing_root == block_orphan.signing_root # test: Try the non-orphan block. bob_recv_server._broadcast_block(block_non_orphan, from_peer=None) msg_block_orphan = await alice_msg_buffer.msg_queue.get() block_non_orphan_received = parse_new_block_msg(msg_block_orphan.payload) assert block_non_orphan_received.signing_root == block_non_orphan.signing_root
async def test_send_no_attestations(request, event_loop): alice, bob = await get_directly_linked_peers(request, event_loop) msg_buffer = MsgBuffer() bob.add_subscriber(msg_buffer) alice.sub_proto.send_attestation_records(()) message = await msg_buffer.msg_queue.get() assert isinstance(message.command, AttestationRecords) assert message.payload == ()
async def get_command_setup(request, event_loop): alice, bob = await get_directly_linked_peers( request, event_loop, alice_chain_db=await get_genesis_chain_db(), bob_chain_db=await get_genesis_chain_db(), ) msg_buffer = MsgBuffer() bob.add_subscriber(msg_buffer) return alice, msg_buffer
async def test_send_get_blocks_by_hash(request, event_loop): alice, bob = await get_directly_linked_peers(request, event_loop) msg_buffer = MsgBuffer() bob.add_subscriber(msg_buffer) alice.sub_proto.send_get_blocks(b"\x33" * 32, 15) message = await msg_buffer.msg_queue.get() assert isinstance(message.command, GetBeaconBlocks) assert message.payload == { "block_slot_or_hash": b"\x33" * 32, "max_blocks": 15, }
async def test_bcc_receive_server_with_request_server(request, event_loop, event_bus): async with get_peer_and_receive_server( request, event_loop, event_bus, ) as (alice, alice_req_server, bob_recv_server, bob_msg_queue): alice_msg_buffer = MsgBuffer() alice.add_subscriber(alice_msg_buffer) blocks = get_blocks(bob_recv_server, num_blocks=3) await alice_req_server.db.coro_persist_block( blocks[0], SerenityBeaconBlock, higher_slot_scoring, ) await alice_req_server.db.coro_persist_block( blocks[1], SerenityBeaconBlock, higher_slot_scoring, ) await alice_req_server.db.coro_persist_block( blocks[2], SerenityBeaconBlock, higher_slot_scoring, ) # test: alice send `blocks[2]` to bob, and bob should be able to # get `blocks[1]` and `blocks[0]` later through the requests. assert not bob_recv_server._is_block_seen(blocks[0]) assert not bob_recv_server._is_block_seen(blocks[1]) assert not bob_recv_server._is_block_seen(blocks[2]) alice.sub_proto.send_new_block(block=blocks[2]) # bob receives new block `blocks[2]` assert blocks[2] == parse_new_block_msg(await bob_msg_queue.get()) # bob requests for `blocks[1]`, and alice receives the request req_1 = await alice_msg_buffer.msg_queue.get() assert req_1.payload['block_slot_or_root'] == blocks[1].signing_root # bob receives the response block `blocks[1]` assert blocks[1] == parse_resp_block_msg(await bob_msg_queue.get()) # bob requests for `blocks[0]`, and alice receives the request req_0 = await alice_msg_buffer.msg_queue.get() assert req_0.payload['block_slot_or_root'] == blocks[0].signing_root # bob receives the response block `blocks[0]` assert blocks[0] == parse_resp_block_msg(await bob_msg_queue.get()) assert bob_recv_server._is_block_root_in_db(blocks[0].signing_root) assert bob_recv_server._is_block_root_in_db(blocks[1].signing_root) assert bob_recv_server._is_block_root_in_db(blocks[2].signing_root)
async def get_request_server_setup(request, event_loop, chain_db): alice, alice_peer_pool, bob, bob_peer_pool = await get_directly_linked_peers_in_peer_pools( request, event_loop, chain_db=chain_db, ) response_buffer = MsgBuffer() alice.add_subscriber(response_buffer) bob_request_server = BCCRequestServer(bob.context.chain_db, bob_peer_pool) asyncio.ensure_future(bob_request_server.run()) def finalizer(): event_loop.run_until_complete(bob_request_server.cancel()) request.addfinalizer(finalizer) return alice, response_buffer
async def test_send_single_block(request, event_loop): alice, bob = await get_directly_linked_peers(request, event_loop) msg_buffer = MsgBuffer() bob.add_subscriber(msg_buffer) block = BaseBeaconBlock( slot=1, parent_root=ZERO_HASH32, state_root=ZERO_HASH32, randao_reveal=ZERO_HASH32, candidate_pow_receipt_root=ZERO_HASH32, signature=(0, 0), body=empty_body(), ) alice.sub_proto.send_blocks((block, )) message = await msg_buffer.msg_queue.get() assert isinstance(message.command, BeaconBlocks) assert message.payload == (block, )
async def test_send_single_block(request, event_loop): alice, bob = await get_directly_linked_peers(request, event_loop) msg_buffer = MsgBuffer() bob.add_subscriber(msg_buffer) block = BaseBeaconBlock( slot=1, randao_reveal=ZERO_HASH32, candidate_pow_receipt_root=ZERO_HASH32, ancestor_hashes=[ZERO_HASH32] * 32, state_root=ZERO_HASH32, attestations=[], specials=[], proposer_signature=None, ) alice.sub_proto.send_blocks((block, )) message = await msg_buffer.msg_queue.get() assert isinstance(message.command, BeaconBlocks) assert message.payload == (block, )
async def get_request_server_setup(request, event_loop, event_bus, chain_db): genesis = await chain_db.coro_get_canonical_block_by_slot( SERENITY_CONFIG.GENESIS_SLOT, BeaconBlock, ) alice_chain_db = AsyncBeaconChainDBFactory(blocks=(genesis, )) alice_context = BeaconContextFactory(chain_db=alice_chain_db) bob_context = BeaconContextFactory(chain_db=chain_db) peer_pair = BCCPeerPairFactory( alice_peer_context=alice_context, bob_peer_context=bob_context, event_bus=event_bus, ) async with peer_pair as (alice, bob): async with BCCPeerPoolFactory.run_for_peer( bob) as bob_peer_pool: # noqa: E501 response_buffer = MsgBuffer() alice.add_subscriber(response_buffer) async with run_peer_pool_event_server( event_bus, bob_peer_pool, handler_type=BCCPeerPoolEventServer): bob_request_server = BCCRequestServer( event_bus, TO_NETWORKING_BROADCAST_CONFIG, bob_context.chain_db) asyncio.ensure_future(bob_request_server.run()) await event_bus.wait_until_all_endpoints_subscribed_to( GetBeaconBlocksEvent) def finalizer(): event_loop.run_until_complete(bob_request_server.cancel()) request.addfinalizer(finalizer) yield alice, response_buffer
async def get_command_setup(): async with BCCPeerPairFactory() as (alice, bob): msg_buffer = MsgBuffer() bob.add_subscriber(msg_buffer) yield alice, msg_buffer