use super::*;
use rand::{rngs::StdRng, SeedableRng};
impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
pub fn check_next_block<R: CryptoRng + Rng>(&self, block: &Block<N>, rng: &mut R) -> Result<()> {
let height = block.height();
if self.contains_block_hash(&block.hash())? {
bail!("Block hash '{}' already exists in the ledger", block.hash())
}
if self.contains_block_height(block.height())? {
bail!("Block height '{height}' already exists in the ledger")
}
if let Some(solutions) = block.solutions() {
for puzzle_commitment in solutions.puzzle_commitments() {
if self.contains_puzzle_commitment(puzzle_commitment)? {
bail!("Puzzle commitment {puzzle_commitment} already exists in the ledger");
}
}
}
let transactions = block.transactions().iter().collect::<Vec<_>>();
let rngs = (0..transactions.len()).map(|_| StdRng::from_seed(rng.gen())).collect::<Vec<_>>();
cfg_iter!(transactions).zip(rngs).try_for_each(|(transaction, mut rng)| {
self.check_transaction_basic(*transaction, transaction.to_rejected_id()?, &mut rng)
.map_err(|e| anyhow!("Invalid transaction found in the transactions list: {e}"))
})?;
{
}
let state = FinalizeGlobalState::new::<N>(
block.round(),
block.height(),
block.cumulative_weight(),
block.cumulative_proof_target(),
block.previous_hash(),
)?;
let ratified_finalize_operations =
self.vm.check_speculate(state, block.ratifications(), block.solutions(), block.transactions())?;
block.verify(
&self.latest_block(),
self.latest_state_root(),
&self.latest_committee()?,
self.coinbase_puzzle(),
&self.latest_epoch_challenge()?,
OffsetDateTime::now_utc().unix_timestamp(),
ratified_finalize_operations,
)?;
Ok(())
}
}