Exemple #1
0
async def test_sync_from_old_head(request, event_loop, event_bus):
    genesis = BeaconBlockFactory()
    alice_branch = (genesis, ) + BeaconBlockFactory.create_branch(length=49,
                                                                  root=genesis)
    bob_branch = alice_branch + BeaconBlockFactory.create_branch(
        length=50, root=alice_branch[-1])
    async with get_sync_setup(request, event_loop, event_bus, alice_branch,
                              bob_branch) as (alice, bob):
        assert_synced(alice, bob, bob_branch)
Exemple #2
0
async def test_reorg_sync(request, event_loop, event_bus):
    genesis = BeaconBlockFactory()
    alice_branch = (genesis, ) + BeaconBlockFactory.create_branch(
        length=49, root=genesis, state_root=b"\x11" * 32)
    bob_branch = (genesis, ) + BeaconBlockFactory.create_branch(
        length=99, root=genesis, state_root=b"\x22" * 32)

    async with get_sync_setup(request, event_loop, event_bus, alice_branch,
                              bob_branch) as (alice, bob):
        assert_synced(alice, bob, bob_branch)
Exemple #3
0
async def test_reorg_sync(request, event_loop, event_bus, genesis_state):
    genesis_block = get_genesis_block(genesis_state.hash_tree_root,
                                      BeaconBlock)
    alice_branch = (genesis_block, ) + BeaconBlockFactory.create_branch(
        length=49, root=genesis_block, state_root=b"\x11" * 32)
    bob_branch = (genesis_block, ) + BeaconBlockFactory.create_branch(
        length=99, root=genesis_block, state_root=b"\x22" * 32)

    async with get_sync_setup(request, event_loop, event_bus, genesis_state,
                              alice_branch, bob_branch) as (alice, bob):
        assert_synced(alice, bob, bob_branch)
Exemple #4
0
async def test_sync_from_old_head(request, event_loop, event_bus,
                                  genesis_state):
    genesis_block = get_genesis_block(genesis_state.hash_tree_root,
                                      BeaconBlock)
    alice_branch = (genesis_block, ) + BeaconBlockFactory.create_branch(
        length=49, root=genesis_block)
    bob_branch = alice_branch + BeaconBlockFactory.create_branch(
        length=50, root=alice_branch[-1])
    async with get_sync_setup(request, event_loop, event_bus, genesis_state,
                              alice_branch, bob_branch) as (alice, bob):
        assert_synced(alice, bob, bob_branch)
Exemple #5
0
async def test_sync_when_already_at_best_head(request, event_loop, event_bus):
    genesis = BeaconBlockFactory()
    alice_branch = (genesis, ) + BeaconBlockFactory.create_branch(
        length=99, root=genesis, state_root=b"\x11" * 32)
    bob_branch = (genesis, ) + BeaconBlockFactory.create_branch(
        length=50, root=genesis, state_root=b"\x22" * 32)

    async with get_sync_setup(request, event_loop, event_bus, alice_branch,
                              bob_branch) as (alice, bob):
        alice_head = alice.chain.get_canonical_head()
        assert alice_head.slot == 99
        for correct_block in alice_branch:
            slot = correct_block.slot
            alice_block = alice.chain.get_canonical_block_by_slot(slot)
            assert alice_block == correct_block
Exemple #6
0
async def test_sync_skipped_slots(request, event_loop, event_bus):
    genesis = BeaconBlockFactory()
    alice_branch = (genesis, ) + BeaconBlockFactory.create_branch(length=0,
                                                                  root=genesis)
    bob_branch = (genesis, ) + BeaconBlockFactory.create_branch_by_slots(
        slots=tuple(range(4, 99)), root=genesis)
    assert bob_branch[0].slot == 0
    assert bob_branch[1].slot == 4
    async with get_sync_setup(request, event_loop, event_bus, alice_branch,
                              bob_branch) as (alice, bob):
        assert_synced(alice, bob, bob_branch)
Exemple #7
0
async def test_sync_skipped_slots(request, event_loop, event_bus,
                                  genesis_state):
    genesis_block = get_genesis_block(genesis_state.hash_tree_root,
                                      BeaconBlock)
    alice_branch = (genesis_block, ) + BeaconBlockFactory.create_branch(
        length=0, root=genesis_block)
    bob_branch = (genesis_block, ) + BeaconBlockFactory.create_branch_by_slots(
        slots=tuple(range(4, 99)), root=genesis_block)
    assert bob_branch[0].slot == 0
    assert bob_branch[1].slot == 4
    async with get_sync_setup(request, event_loop, event_bus, genesis_state,
                              alice_branch, bob_branch) as (alice, bob):
        assert_synced(alice, bob, bob_branch)
Exemple #8
0
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]