def test_per_slot_transition( chaindb, genesis_block, genesis_state, fixture_sm_class, config, state_slot, fork_choice_scoring, empty_attestation_pool, keymap, ): chaindb.persist_block(genesis_block, SerenityBeaconBlock, fork_choice_scoring) chaindb.persist_state(genesis_state) state = genesis_state # Create a block block = create_mock_block( state=state, config=config, state_machine=fixture_sm_class(chaindb, empty_attestation_pool, genesis_block.slot), block_class=SerenityBeaconBlock, parent_block=genesis_block, keymap=keymap, slot=state_slot, ) # Store in chaindb chaindb.persist_block(block, SerenityBeaconBlock, fork_choice_scoring) # Get state machine instance sm = fixture_sm_class(chaindb, empty_attestation_pool, block.slot) # Get state transition instance st = sm.state_transition_class(sm.config) updated_state = st.apply_state_transition(state, future_slot=state.slot + 1) # Ensure that slot gets increased by 1 assert updated_state.slot == state.slot + 1 # block_roots roots_index = (updated_state.slot - 1) % st.config.SLOTS_PER_HISTORICAL_ROOT assert updated_state.block_roots[roots_index] == block.parent_root # state_roots assert updated_state.state_roots[roots_index] == state.hash_tree_root # historical_roots if updated_state.slot % st.config.SLOTS_PER_HISTORICAL_ROOT == 0: historical_batch = HistoricalBatch(block_roots=state.block_roots, state_roots=state.state_roots) assert updated_state.historical_roots[ -1] == historical_batch.hash_tree_root else: assert updated_state.historical_roots == state.historical_roots
def test_per_slot_transition(base_db, genesis_block, genesis_state, fixture_sm_class, config, state_slot, keymap): chaindb = BeaconChainDB(base_db) chaindb.persist_block(genesis_block, SerenityBeaconBlock) chaindb.persist_state(genesis_state) state = genesis_state # Create a block block = create_mock_block( state=state, config=config, state_machine=fixture_sm_class( chaindb, genesis_block, ), block_class=SerenityBeaconBlock, parent_block=genesis_block, keymap=keymap, slot=state_slot, ) # Store in chaindb chaindb.persist_block(block, SerenityBeaconBlock) # Get state machine instance sm = fixture_sm_class( chaindb, block, ) # Get state transition instance st = sm.state_transition_class(sm.config) # NOTE: we want to run both functions, however they are run independently # so we have two function calls updated_state = st.cache_state(state) updated_state = st.per_slot_transition(updated_state) # Ensure that slot gets increased by 1 assert updated_state.slot == state.slot + 1 # latest_block_roots latest_block_roots_index = (updated_state.slot - 1) % st.config.SLOTS_PER_HISTORICAL_ROOT assert updated_state.latest_block_roots[ latest_block_roots_index] == block.previous_block_root # historical_roots if updated_state.slot % st.config.SLOTS_PER_HISTORICAL_ROOT == 0: historical_batch = HistoricalBatch( block_roots=state.latest_block_roots, state_roots=state.latest_state_roots, slots_per_historical_root=config.SLOTS_PER_HISTORICAL_ROOT, ) assert updated_state.historical_roots[ -1] == historical_batch.hash_tree_root else: assert updated_state.historical_roots == state.historical_roots
def _compute_next_historical_roots(state: BeaconState, config: Eth2Config) -> Tuple[Hash32, ...]: next_epoch = state.next_epoch(config.SLOTS_PER_EPOCH) new_historical_roots = state.historical_roots if next_epoch % (config.SLOTS_PER_HISTORICAL_ROOT // config.SLOTS_PER_EPOCH) == 0: historical_batch = HistoricalBatch( block_roots=state.block_roots, state_roots=state.state_roots, ) new_historical_roots = state.historical_roots + (historical_batch.root,) return new_historical_roots
def _update_historical_roots(state: BeaconState, next_epoch: Epoch, config: Eth2Config) -> BeaconState: updated_historical_roots = state.historical_roots epochs_per_historical_root = config.SLOTS_PER_HISTORICAL_ROOT // config.SLOTS_PER_EPOCH should_update_historical_roots = next_epoch % epochs_per_historical_root == 0 if should_update_historical_roots: historical_batch = HistoricalBatch( block_roots=state.latest_block_roots, state_roots=state.latest_state_roots, ) updated_historical_roots += (historical_batch.root, ) return state.copy(historical_roots=updated_historical_roots)
def test_per_slot_transition( chaindb_at_genesis, genesis_block, genesis_state, fixture_sm_class, config, state_slot, fork_choice_scoring, keymap, ): chaindb = chaindb_at_genesis sm = fixture_sm_class(chaindb) state = genesis_state updated_state, _ = sm.apply_state_transition(state, future_slot=state.slot + 1) # Ensure that slot gets increased by 1 assert updated_state.slot == state.slot + 1 # block_roots roots_index = (updated_state.slot - 1) % sm.config.SLOTS_PER_HISTORICAL_ROOT assert (updated_state.block_roots[roots_index] == genesis_block.message.hash_tree_root) # state_roots assert updated_state.state_roots[roots_index] == state.hash_tree_root # historical_roots if updated_state.slot % sm.config.SLOTS_PER_HISTORICAL_ROOT == 0: historical_batch = HistoricalBatch(block_roots=state.block_roots, state_roots=state.state_roots) assert updated_state.historical_roots[ -1] == historical_batch.hash_tree_root else: assert updated_state.historical_roots == state.historical_roots