Turn a chain length check into an assert

This commit is contained in:
teor 2020-11-30 10:47:07 +10:00
parent fb2f2a97f2
commit d007c76488
1 changed files with 19 additions and 16 deletions

View File

@ -23,9 +23,9 @@ pub(crate) mod difficulty;
/// The relevant chain is an iterator over the ancestors of `block`, starting /// The relevant chain is an iterator over the ancestors of `block`, starting
/// with its parent block. /// with its parent block.
/// ///
/// Panics if the finalized state is empty. /// # Panics
/// ///
/// Skips the difficulty adjustment check if the state contains less than 28 /// If the state contains less than 28
/// (`POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN`) blocks. /// (`POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN`) blocks.
#[tracing::instrument( #[tracing::instrument(
name = "contextual_validation", name = "contextual_validation",
@ -53,6 +53,11 @@ where
.into_iter() .into_iter()
.take(MAX_CONTEXT_BLOCKS) .take(MAX_CONTEXT_BLOCKS)
.collect(); .collect();
assert_eq!(
relevant_chain.len(),
POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN,
"state must contain enough blocks to do contextual validation"
);
let parent_block = relevant_chain let parent_block = relevant_chain
.get(0) .get(0)
@ -63,20 +68,18 @@ where
.expect("valid blocks have a coinbase height"); .expect("valid blocks have a coinbase height");
check::height_one_more_than_parent_height(parent_height, prepared.height)?; check::height_one_more_than_parent_height(parent_height, prepared.height)?;
if relevant_chain.len() >= POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN { let relevant_data = relevant_chain.iter().map(|block| {
let relevant_data = relevant_chain.iter().map(|block| { (
( block.borrow().header.difficulty_threshold,
block.borrow().header.difficulty_threshold, block.borrow().header.time,
block.borrow().header.time, )
) });
}); let expected_difficulty =
let expected_difficulty = AdjustedDifficulty::new_from_block(&prepared.block, network, relevant_data);
AdjustedDifficulty::new_from_block(&prepared.block, network, relevant_data); check::difficulty_threshold_is_valid(
check::difficulty_threshold_is_valid( prepared.block.header.difficulty_threshold,
prepared.block.header.difficulty_threshold, expected_difficulty,
expected_difficulty, )?;
)?;
}
// TODO: other contextual validation design and implementation // TODO: other contextual validation design and implementation
Ok(()) Ok(())