示例#1
0
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
示例#2
0
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
示例#3
0
async def get_peer_and_receive_server(request, event_loop) -> Tuple[
        BCCPeer, BCCRequestServer, BCCReceiveServer, asyncio.Queue]:
    alice_chain = await get_fake_chain()
    bob_chain = await get_fake_chain()

    (
        alice, alice_peer_pool, bob, bob_peer_pool
    ) = await helpers.get_directly_linked_peers_in_peer_pools(
        request,
        event_loop,
        alice_chain_db=alice_chain.chaindb,
        bob_chain_db=bob_chain.chaindb,
    )

    msg_queue = asyncio.Queue()
    orig_handle_msg = BCCReceiveServer._handle_msg

    # Inject a queue to each `BCCReceiveServer`, which puts the message passed to `_handle_msg` to
    # the queue, right after every `_handle_msg` finishes.
    # This is crucial to make the test be able to wait until `_handle_msg` finishes.
    async def _handle_msg(self, base_peer, cmd, msg):
        task = asyncio.ensure_future(orig_handle_msg(self, base_peer, cmd, msg))

        def enqueue_msg(future, msg):
            msg_queue.put_nowait(msg)
        task.add_done_callback(functools.partial(enqueue_msg, msg=msg))
        await task
    BCCReceiveServer._handle_msg = _handle_msg

    alice_req_server = BCCRequestServer(
        db=alice_chain.chaindb,
        peer_pool=alice_peer_pool,
    )
    bob_recv_server = BCCReceiveServer(chain=bob_chain, peer_pool=bob_peer_pool)

    asyncio.ensure_future(alice_req_server.run())
    asyncio.ensure_future(bob_recv_server.run())
    await alice_req_server.events.started.wait()
    await bob_recv_server.events.started.wait()

    def finalizer():
        event_loop.run_until_complete(alice_req_server.cancel())
        event_loop.run_until_complete(bob_recv_server.cancel())

    request.addfinalizer(finalizer)

    return alice, alice_req_server, bob_recv_server, msg_queue
示例#4
0
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
示例#5
0
async def get_sync_setup(request,
                         event_loop,
                         event_bus,
                         alice_chain_db,
                         bob_chain_db,
                         genesis_config=SERENITY_GENESIS_CONFIG):
    alice_context = BeaconContextFactory(chain_db=alice_chain_db)
    bob_context = BeaconContextFactory(chain_db=bob_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(
                alice) as alice_peer_pool, BCCPeerPoolFactory.run_for_peer(
                    bob) as bob_peer_pool:  # noqa: E501

            bob_request_server = BCCRequestServer(
                event_bus, TO_NETWORKING_BROADCAST_CONFIG,
                bob.context.chain_db)

            alice_syncer = BeaconChainSyncer(
                alice_chain_db,
                alice_peer_pool,
                SimpleWriterBlockImporter(alice_chain_db),
                genesis_config,
            )
            async with run_peer_pool_event_server(
                    event_bus, bob_peer_pool,
                    handler_type=BCCPeerPoolEventServer):

                asyncio.ensure_future(bob_request_server.run())
                asyncio.ensure_future(alice_syncer.run())

                def finalizer():
                    event_loop.run_until_complete(alice_syncer.cancel())
                    event_loop.run_until_complete(bob_request_server.cancel())

                request.addfinalizer(finalizer)
                await alice_syncer.events.finished.wait()
                yield alice_syncer
示例#6
0
async def get_sync_setup(request, event_loop, alice_chain_db, bob_chain_db):
    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=bob_chain_db,
    )

    bob_request_server = BCCRequestServer(bob.context.chain_db, bob_peer_pool)
    alice_syncer = BeaconChainSyncer(alice_chain_db, alice_peer_pool)

    asyncio.ensure_future(bob_request_server.run())
    asyncio.ensure_future(alice_syncer.run())

    def finalizer():
        event_loop.run_until_complete(alice_syncer.cancel())
        event_loop.run_until_complete(bob_request_server.cancel())

    request.addfinalizer(finalizer)
    return alice_syncer
示例#7
0
async def get_sync_setup(request,
                         event_loop,
                         event_bus,
                         alice_chain_db,
                         bob_chain_db,
                         genesis_config=SERENITY_GENESIS_CONFIG):
    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=bob_chain_db,
        bob_peer_pool_event_bus=event_bus,
    )

    bob_request_server = BCCRequestServer(event_bus,
                                          TO_NETWORKING_BROADCAST_CONFIG,
                                          bob.context.chain_db)

    alice_syncer = BeaconChainSyncer(
        alice_chain_db,
        alice_peer_pool,
        SimpleWriterBlockImporter(alice_chain_db),
        genesis_config,
    )
    async with run_peer_pool_event_server(event_bus,
                                          bob_peer_pool,
                                          handler_type=BCCPeerPoolEventServer):

        asyncio.ensure_future(bob_request_server.run())
        asyncio.ensure_future(alice_syncer.run())

        def finalizer():
            event_loop.run_until_complete(alice_syncer.cancel())
            event_loop.run_until_complete(bob_request_server.cancel())

        request.addfinalizer(finalizer)
        await alice_syncer.events.finished.wait()
        yield alice_syncer
示例#8
0
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