use std::{borrow::Borrow, sync::Arc};
use chrono::Duration;
use zebra_chain::{
block::{self, Block, ChainHistoryBlockTxAuthCommitmentHash, CommitmentError},
history_tree::HistoryTree,
parameters::{Network, NetworkUpgrade},
work::difficulty::CompactDifficulty,
};
use crate::{
service::{
block_iter::any_ancestor_blocks, check::difficulty::POW_ADJUSTMENT_BLOCK_SPAN,
finalized_state::ZebraDb, non_finalized_state::NonFinalizedState,
},
BoxError, SemanticallyVerifiedBlock, ValidateContextError,
};
use super::check;
#[allow(unused_imports)]
use crate::service::non_finalized_state::Chain;
pub(crate) mod anchors;
pub(crate) mod difficulty;
pub(crate) mod nullifier;
pub(crate) mod utxo;
pub use utxo::transparent_coinbase_spend;
#[cfg(test)]
mod tests;
pub(crate) use difficulty::AdjustedDifficulty;
#[tracing::instrument(skip(semantically_verified, finalized_tip_height, relevant_chain))]
pub(crate) fn block_is_valid_for_recent_chain<C>(
semantically_verified: &SemanticallyVerifiedBlock,
network: &Network,
finalized_tip_height: Option<block::Height>,
relevant_chain: C,
) -> Result<(), ValidateContextError>
where
C: IntoIterator,
C::Item: Borrow<Block>,
C::IntoIter: ExactSizeIterator,
{
let finalized_tip_height = finalized_tip_height
.expect("finalized state must contain at least one block to do contextual validation");
check::block_is_not_orphaned(finalized_tip_height, semantically_verified.height)?;
let relevant_chain: Vec<_> = relevant_chain
.into_iter()
.take(POW_ADJUSTMENT_BLOCK_SPAN)
.collect();
let Some(parent_block) = relevant_chain.first() else {
warn!(
?semantically_verified,
?finalized_tip_height,
"state must contain parent block to do contextual validation"
);
return Err(ValidateContextError::NotReadyToBeCommitted);
};
let parent_block = parent_block.borrow();
let parent_height = parent_block
.coinbase_height()
.expect("valid blocks have a coinbase height");
check::height_one_more_than_parent_height(parent_height, semantically_verified.height)?;
#[cfg(test)]
if relevant_chain.len() < POW_ADJUSTMENT_BLOCK_SPAN {
return Ok(());
}
#[cfg(not(test))]
if relevant_chain.is_empty() {
return Err(ValidateContextError::NotReadyToBeCommitted);
}
let relevant_data = relevant_chain.iter().map(|block| {
(
block.borrow().header.difficulty_threshold,
block.borrow().header.time,
)
});
let difficulty_adjustment =
AdjustedDifficulty::new_from_block(&semantically_verified.block, network, relevant_data);
check::difficulty_threshold_and_time_are_valid(
semantically_verified.block.header.difficulty_threshold,
difficulty_adjustment,
)?;
Ok(())
}
#[tracing::instrument(skip(block, history_tree))]
pub(crate) fn block_commitment_is_valid_for_chain_history(
block: Arc<Block>,
network: &Network,
history_tree: &HistoryTree,
) -> Result<(), ValidateContextError> {
match block.commitment(network)? {
block::Commitment::PreSaplingReserved(_)
| block::Commitment::FinalSaplingRoot(_)
| block::Commitment::ChainHistoryActivationReserved => {
Ok(())
}
block::Commitment::ChainHistoryRoot(actual_history_tree_root) => {
let history_tree_root = history_tree
.hash()
.expect("the history tree of the previous block must exist since the current block has a ChainHistoryRoot");
if actual_history_tree_root == history_tree_root {
Ok(())
} else {
Err(ValidateContextError::InvalidBlockCommitment(
CommitmentError::InvalidChainHistoryRoot {
actual: actual_history_tree_root.into(),
expected: history_tree_root.into(),
},
))
}
}
block::Commitment::ChainHistoryBlockTxAuthCommitment(actual_hash_block_commitments) => {
let history_tree_root = history_tree
.hash()
.or_else(|| {
(NetworkUpgrade::Heartwood.activation_height(network)
== block.coinbase_height())
.then_some(block::CHAIN_HISTORY_ACTIVATION_RESERVED.into())
})
.expect(
"the history tree of the previous block must exist \
since the current block has a ChainHistoryBlockTxAuthCommitment",
);
let auth_data_root = block.auth_data_root();
let hash_block_commitments = ChainHistoryBlockTxAuthCommitmentHash::from_commitments(
&history_tree_root,
&auth_data_root,
);
if actual_hash_block_commitments == hash_block_commitments {
Ok(())
} else {
Err(ValidateContextError::InvalidBlockCommitment(
CommitmentError::InvalidChainHistoryBlockTxAuthCommitment {
actual: actual_hash_block_commitments.into(),
expected: hash_block_commitments.into(),
},
))
}
}
}
}
fn block_is_not_orphaned(
finalized_tip_height: block::Height,
candidate_height: block::Height,
) -> Result<(), ValidateContextError> {
if candidate_height <= finalized_tip_height {
Err(ValidateContextError::OrphanedBlock {
candidate_height,
finalized_tip_height,
})
} else {
Ok(())
}
}
fn height_one_more_than_parent_height(
parent_height: block::Height,
candidate_height: block::Height,
) -> Result<(), ValidateContextError> {
if parent_height + 1 != Some(candidate_height) {
Err(ValidateContextError::NonSequentialBlock {
candidate_height,
parent_height,
})
} else {
Ok(())
}
}
fn difficulty_threshold_and_time_are_valid(
difficulty_threshold: CompactDifficulty,
difficulty_adjustment: AdjustedDifficulty,
) -> Result<(), ValidateContextError> {
let candidate_height = difficulty_adjustment.candidate_height();
let candidate_time = difficulty_adjustment.candidate_time();
let network = difficulty_adjustment.network();
let median_time_past = difficulty_adjustment.median_time_past();
let block_time_max =
median_time_past + Duration::seconds(difficulty::BLOCK_MAX_TIME_SINCE_MEDIAN.into());
let genesis_height = NetworkUpgrade::Genesis
.activation_height(&network)
.expect("Zebra always has a genesis height available");
if candidate_time <= median_time_past && candidate_height != genesis_height {
Err(ValidateContextError::TimeTooEarly {
candidate_time,
median_time_past,
})?
}
if network.is_max_block_time_enforced(candidate_height) && candidate_time > block_time_max {
Err(ValidateContextError::TimeTooLate {
candidate_time,
block_time_max,
})?
}
let expected_difficulty = difficulty_adjustment.expected_difficulty_threshold();
if difficulty_threshold != expected_difficulty {
Err(ValidateContextError::InvalidDifficultyThreshold {
difficulty_threshold,
expected_difficulty,
})?
}
Ok(())
}
pub(crate) fn legacy_chain<I>(
nu5_activation_height: block::Height,
ancestors: I,
network: &Network,
max_legacy_chain_blocks: usize,
) -> Result<(), BoxError>
where
I: Iterator<Item = Arc<Block>>,
{
let mut ancestors = ancestors.peekable();
let tip_height = ancestors.peek().and_then(|block| block.coinbase_height());
for (index, block) in ancestors.enumerate() {
if block
.coinbase_height()
.expect("valid blocks have coinbase heights")
< nu5_activation_height
{
return Ok(());
}
if index >= max_legacy_chain_blocks {
return Err(format!(
"could not find any transactions in recent blocks: \
checked {index} blocks back from {:?}",
tip_height.expect("database contains valid blocks"),
)
.into());
}
block
.check_transaction_network_upgrade_consistency(network)
.map_err(|error| {
format!("inconsistent network upgrade found in transaction: {error:?}")
})?;
let has_network_upgrade = block
.transactions
.iter()
.find_map(|trans| trans.network_upgrade())
.is_some();
if has_network_upgrade {
return Ok(());
}
}
Ok(())
}
pub(crate) fn initial_contextual_validity(
finalized_state: &ZebraDb,
non_finalized_state: &NonFinalizedState,
semantically_verified: &SemanticallyVerifiedBlock,
) -> Result<(), ValidateContextError> {
let relevant_chain = any_ancestor_blocks(
non_finalized_state,
finalized_state,
semantically_verified.block.header.previous_block_hash,
);
check::block_is_valid_for_recent_chain(
semantically_verified,
&non_finalized_state.network,
finalized_state.finalized_tip_height(),
relevant_chain,
)?;
check::nullifier::no_duplicates_in_finalized_chain(semantically_verified, finalized_state)?;
Ok(())
}