Replace a chain length assertion with a NotReadyToBeCommitted error (#7072)

This commit is contained in:
teor 2023-06-27 16:50:35 +10:00 committed by GitHub
parent 3af03c3971
commit 941be2965c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 11 deletions

View File

@ -51,7 +51,7 @@ pub struct CommitSemanticallyVerifiedError(#[from] ValidateContextError);
#[non_exhaustive]
#[allow(missing_docs)]
pub enum ValidateContextError {
#[error("block parent not found in any chain")]
#[error("block parent not found in any chain, or not enough blocks in chain")]
#[non_exhaustive]
NotReadyToBeCommitted,

View File

@ -80,19 +80,25 @@ where
.expect("valid blocks have a coinbase height");
check::height_one_more_than_parent_height(parent_height, prepared.height)?;
// skip this check during tests if we don't have enough blocks in the chain
#[cfg(test)]
if relevant_chain.len() < POW_ADJUSTMENT_BLOCK_SPAN {
// skip this check during tests if we don't have enough blocks in the chain
// process_queued also checks the chain length, so we can skip this assertion during testing
// (tests that want to check this code should use the correct number of blocks)
//
// TODO: accept a NotReadyToBeCommitted error in those tests instead
#[cfg(test)]
return Ok(());
// In production, blocks without enough context are invalid.
//
// The BlockVerifierRouter makes sure that the first 1 million blocks (or more) are
// checkpoint verified. The state queues and block write task make sure that blocks are
// committed in strict height order. But this function is only called on semantically
// verified blocks, so there will be at least 1 million blocks in the state when it is
// called. So this error should never happen.
#[cfg(not(test))]
return Err(ValidateContextError::NotReadyToBeCommitted);
}
// process_queued also checks the chain length, so we can skip this assertion during testing
// (tests that want to check this code should use the correct number of blocks)
assert_eq!(
relevant_chain.len(),
POW_ADJUSTMENT_BLOCK_SPAN,
"state must contain enough blocks to do proof of work contextual validation, \
and validation must receive the exact number of required blocks"
);
let relevant_data = relevant_chain.iter().map(|block| {
(