Beispiel #1
0
async def test_fast_syncer(request, event_loop, chaindb_fresh, chaindb_20):
    client_peer, server_peer = await get_directly_linked_peers(
        request, event_loop, ETHPeer, FakeAsyncHeaderDB(chaindb_fresh.db),
        ETHPeer, FakeAsyncHeaderDB(chaindb_20.db))
    client_peer_pool = MockPeerPoolWithConnectedPeers([client_peer])
    client = FastChainSyncer(chaindb_fresh, client_peer_pool)
    server = RegularChainSyncer(FrontierTestChain(chaindb_20.db), chaindb_20,
                                MockPeerPoolWithConnectedPeers([server_peer]))
    asyncio.ensure_future(server.run())

    def finalizer():
        event_loop.run_until_complete(server.cancel())
        # Yield control so that server.run() returns, otherwise asyncio will complain.
        event_loop.run_until_complete(asyncio.sleep(0.1))

    request.addfinalizer(finalizer)

    # FastChainSyncer.run() will return as soon as it's caught up with the peer.
    await asyncio.wait_for(client.run(), timeout=2)

    head = chaindb_fresh.get_canonical_head()
    assert head == chaindb_20.get_canonical_head()

    # Now download the state for the chain's head.
    state_downloader = StateDownloader(chaindb_fresh.db, head.state_root,
                                       client_peer_pool)
    await asyncio.wait_for(state_downloader.run(), timeout=2)

    assert head.state_root in chaindb_fresh.db
Beispiel #2
0
async def test_regular_syncer(request, event_loop, chaindb_fresh, chaindb_20):
    client_peer, server_peer = await get_directly_linked_peers(
        request, event_loop,
        ETHPeer, FakeAsyncHeaderDB(chaindb_fresh.db),
        ETHPeer, FakeAsyncHeaderDB(chaindb_20.db))
    client = RegularChainSyncer(
        FrontierTestChain(chaindb_fresh.db),
        chaindb_fresh,
        MockPeerPoolWithConnectedPeers([client_peer]))
    server = RegularChainSyncer(
        FrontierTestChain(chaindb_20.db),
        chaindb_20,
        MockPeerPoolWithConnectedPeers([server_peer]))
    asyncio.ensure_future(server.run())

    def finalizer():
        event_loop.run_until_complete(asyncio.gather(client.cancel(), server.cancel()))
        # Yield control so that client/server.run() returns, otherwise asyncio will complain.
        event_loop.run_until_complete(asyncio.sleep(0.1))
    request.addfinalizer(finalizer)

    asyncio.ensure_future(client.run())

    await wait_for_head(client.db, server.db.get_canonical_head())
    head = client.db.get_canonical_head()
    assert head.state_root in client.db.db
Beispiel #3
0
async def test_peer_pool_iter(request, event_loop):
    peer1, _ = await get_directly_linked_peers(request, event_loop)
    peer2, _ = await get_directly_linked_peers(request, event_loop)
    peer3, _ = await get_directly_linked_peers(request, event_loop)
    pool = MockPeerPoolWithConnectedPeers([peer1, peer2, peer3])
    peers = list([peer async for peer in pool])

    assert len(peers) == 3
    assert peer1 in peers
    assert peer2 in peers
    assert peer3 in peers

    peers = []
    asyncio.ensure_future(
        peer2.disconnect(DisconnectReason.disconnect_requested))
    async for peer in pool:
        peers.append(peer)

    assert len(peers) == 2
    assert peer1 in peers
    assert peer2 not in peers
    assert peer3 in peers
Beispiel #4
0
async def get_lightchain_with_peers(request, event_loop, server_peer_headerdb):
    """Return a MainnetLightPeerChain instance with a client/server peer pair.

    The server is a LESPeerServer instance that can be used to send Announce and BlockHeaders
    messages, and the client will be registered with the LightPeerChain so that a sync
    request is added to the LightPeerChain's queue every time a new Announce message is received.
    """
    headerdb = get_fresh_mainnet_headerdb()
    client, server = await get_client_and_server_peer_pair(
        request, event_loop, headerdb, server_peer_headerdb)

    light_chain = MainnetLightPeerChain(
        headerdb, MockPeerPoolWithConnectedPeers([client]))
    asyncio.ensure_future(light_chain.run())
    await asyncio.sleep(
        0)  # Yield control to give the LightPeerChain a chance to start

    def finalizer():
        event_loop.run_until_complete(light_chain.cancel())

    request.addfinalizer(finalizer)
    return light_chain, client, server