async def get_sync_setup( request, event_loop, event_bus, genesis_state, alice_branch, bob_branch, sync_timeout=6, ): alice_chaindb = AsyncBeaconChainDBFactory() bob_chaindb = AsyncBeaconChainDBFactory() peer_pair = ConnectionPairFactory( alice_chaindb=alice_chaindb, alice_branch=alice_branch, bob_chaindb=bob_chaindb, bob_branch=bob_branch, genesis_state=genesis_state, ) async with peer_pair as (alice, bob): alice_syncer = BeaconChainSyncerFactory( chain_db__db=alice.chain.chaindb.db, peer_pool=alice.handshaked_peers) try: await asyncio.wait_for(alice_syncer.run(), timeout=sync_timeout) except asyncio.TimeoutError: # After sync is cancelled, return to let the caller do assertions about the state pass finally: yield alice, bob
async def get_fake_chain() -> FakeChain: genesis_config = Eth2GenesisConfig(MINIMAL_SERENITY_CONFIG) chain_db = AsyncBeaconChainDBFactory(genesis_config=genesis_config) genesis_block = SignedBeaconBlockFactory() chain_db.persist_block(genesis_block, SerenitySignedBeaconBlock, HigherSlotScoring()) return FakeChain(base_db=chain_db.db, genesis_config=genesis_config)
async def get_sync_setup(request, event_loop, event_bus, alice_branch, bob_branch): alice_chaindb = AsyncBeaconChainDBFactory(blocks=alice_branch) bob_chaindb = AsyncBeaconChainDBFactory(blocks=bob_branch) peer_pair = ConnectionPairFactory(alice_chaindb=alice_chaindb, bob_chaindb=bob_chaindb) async with peer_pair as (alice, bob): alice_syncer = BeaconChainSyncerFactory( chain_db__db=alice.chain.chaindb.db, peer_pool=alice.handshaked_peers) asyncio.ensure_future(alice_syncer.run()) def finalizer(): event_loop.run_until_complete(alice_syncer.cancel()) request.addfinalizer(finalizer) await alice_syncer.events.finished.wait() yield alice, bob
async def get_sync_setup( request, event_loop, event_bus, genesis_state, alice_branch, bob_branch, sync_timeout=6, ): alice_chaindb = AsyncBeaconChainDBFactory() bob_chaindb = AsyncBeaconChainDBFactory() peer_pair = ConnectionPairFactory( alice_chaindb=alice_chaindb, alice_branch=alice_branch, bob_chaindb=bob_chaindb, bob_branch=bob_branch, genesis_state=genesis_state, alice_event_bus=event_bus, handshake=False, ) async with peer_pair as (alice, bob): alice_syncer = BeaconChainSyncerFactory( chain_db__db=alice.chain.chaindb.db, peer_pool=alice.handshaked_peers, event_bus=event_bus, ) try: task = asyncio.ensure_future(alice_syncer.run()) # sync will start when alice request_status await alice.request_status(bob.peer_id) # Wait sync to complete await asyncio.wait_for(task, timeout=sync_timeout) except asyncio.TimeoutError: # After sync is cancelled, return to let the caller do assertions about the state pass finally: yield alice, bob
async def test_get_single_block_by_root(request, event_loop, event_bus): block = BeaconBlockFactory() chain_db = AsyncBeaconChainDBFactory(blocks=(block, )) async with get_request_server_setup(request, event_loop, event_bus, chain_db) as (alice, response_buffer): alice.sub_proto.send_get_blocks(block.slot, 1, request_id=5) response = await response_buffer.msg_queue.get() assert isinstance(response.command, BeaconBlocks) assert response.payload == { "request_id": 5, "encoded_blocks": (ssz.encode(block), ), }
async def test_get_canonical_block_range_by_root(request, event_loop, event_bus): chain_db = AsyncBeaconChainDBFactory(blocks=()) genesis = BeaconBlockFactory() base_branch = BeaconBlockFactory.create_branch(3, root=genesis) non_canonical_branch = BeaconBlockFactory.create_branch( 3, root=base_branch[-1], state_root=b"\x00" * 32, ) canonical_branch = BeaconBlockFactory.create_branch( 4, root=base_branch[-1], state_root=b"\x11" * 32, ) for branch in [[genesis], base_branch, non_canonical_branch, canonical_branch]: scorings = (higher_slot_scoring for block in branch) await chain_db.coro_persist_block_chain(branch, BeaconBlock, scorings) async with get_request_server_setup(request, event_loop, event_bus, chain_db) as (alice, response_buffer): alice.sub_proto.send_get_blocks(base_branch[1].signing_root, 4, request_id=5) response = await response_buffer.msg_queue.get() assert isinstance(response.command, BeaconBlocks) assert response.payload["request_id"] == 5 blocks = tuple( ssz.decode(block, BeaconBlock) for block in response.payload["encoded_blocks"]) assert len(blocks) == 4 assert [block.slot for block in blocks ] == [genesis.slot + s for s in [2, 3, 4, 5]] assert blocks == base_branch[1:] + canonical_branch[:2]
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_fake_chain() -> FakeChain: genesis_config = Eth2GenesisConfig(XIAO_LONG_BAO_CONFIG) chain_db = AsyncBeaconChainDBFactory(genesis_config=genesis_config) return FakeChain(base_db=chain_db.db, attestation_pool=TempPool(), genesis_config=genesis_config)
async def get_fake_chain() -> FakeChain: genesis_config = Eth2GenesisConfig(MINIMAL_SERENITY_CONFIG) chain_db = AsyncBeaconChainDBFactory(genesis_config=genesis_config) return FakeChain(base_db=chain_db.db, genesis_config=genesis_config)