spec: simplify cache_state

The `latest_block_root` input argument was unnecessary as we were always setting it to something
almost equivalent to `state.latest_block_root` anyway, and more importantly, it was messing up the
caching of the state root. Previously it was possible for the function to update the state's latest
block root, and then hash the outdated block root that was passed in as an argument.
This commit is contained in:
Michael Sproul
2019-04-08 14:37:01 +10:00
parent b801303374
commit 32547373e5
3 changed files with 8 additions and 22 deletions

View File

@@ -11,12 +11,8 @@ pub enum Error {
/// Advances a state forward by one slot, performing per-epoch processing if required.
///
/// Spec v0.5.0
pub fn per_slot_processing(
state: &mut BeaconState,
latest_block_header: &BeaconBlockHeader,
spec: &ChainSpec,
) -> Result<(), Error> {
cache_state(state, latest_block_header, spec)?;
pub fn per_slot_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
cache_state(state, spec)?;
if (state.slot + 1) % spec.slots_per_epoch == 0 {
per_epoch_processing(state, spec)?;
@@ -27,11 +23,7 @@ pub fn per_slot_processing(
Ok(())
}
fn cache_state(
state: &mut BeaconState,
latest_block_header: &BeaconBlockHeader,
spec: &ChainSpec,
) -> Result<(), Error> {
fn cache_state(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
let previous_slot_state_root = Hash256::from_slice(&state.tree_hash_root()[..]);
// Note: increment the state slot here to allow use of our `state_root` and `block_root`
@@ -46,7 +38,7 @@ fn cache_state(
state.latest_block_header.state_root = previous_slot_state_root
}
let latest_block_root = Hash256::from_slice(&latest_block_header.tree_hash_root()[..]);
let latest_block_root = Hash256::from_slice(&state.latest_block_header.tree_hash_root()[..]);
state.set_block_root(previous_slot, latest_block_root, spec)?;
// Set the state slot back to what it should be.

View File

@@ -105,8 +105,7 @@ fn run_state_transition_test(test_name: &str) {
let mut state = test_case.initial_state.clone();
for (j, block) in test_case.blocks.iter().enumerate() {
while block.slot > state.slot {
let latest_block_header = state.latest_block_header.clone();
per_slot_processing(&mut state, &latest_block_header, &test_case.config).unwrap();
per_slot_processing(&mut state, &test_case.config).unwrap();
}
let res = per_block_processing(&mut state, &block, &test_case.config);
if res.is_err() {