async def test_get_blocks_from_fork_chain_by_root(fork_chain_block_slots, slot_of_requested_blocks, expected_block_slots): fork_chain_blocks = BeaconBlockFactory.create_branch_by_slots( fork_chain_block_slots) mock_root_to_block_db = { block.signing_root: block for block in fork_chain_blocks } class Chain: def get_block_by_root(self, root): if root in mock_root_to_block_db: return mock_root_to_block_db[root] else: raise BlockNotFound requested_blocks = get_blocks_from_fork_chain_by_root( chain=Chain(), start_slot=slot_of_requested_blocks[0], peer_head_block=fork_chain_blocks[-1], slot_of_requested_blocks=slot_of_requested_blocks, ) expected_blocks = [ block for block in fork_chain_blocks if block.slot in expected_block_slots ] assert len(requested_blocks) == len(expected_blocks) assert set(requested_blocks) == set(expected_blocks)
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)
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)
async def test_get_blocks_from_canonical_chain_by_slot( db_block_slots, slot_of_requested_blocks, expected_block_slots): chain = BeaconBlockFactory.create_branch_by_slots(db_block_slots) # Mock up block database mock_slot_to_block_db = {block.slot: block for block in chain} class Chain: def get_canonical_block_by_slot(self, slot): if slot in mock_slot_to_block_db: return mock_slot_to_block_db[slot] else: raise BlockNotFound result_blocks = get_blocks_from_canonical_chain_by_slot( chain=Chain(), slot_of_requested_blocks=slot_of_requested_blocks) expected_blocks = [ mock_slot_to_block_db[slot] for slot in expected_block_slots ] assert len(result_blocks) == len(expected_blocks) assert set(result_blocks) == set(expected_blocks)