Skip to content

Commit

Permalink
Move tx verification logic into check_speculate
Browse files Browse the repository at this point in the history
  • Loading branch information
raychu86 committed Mar 13, 2024
1 parent 2783732 commit f83d18b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
14 changes: 2 additions & 12 deletions ledger/src/check_next_block.rs
Expand Up @@ -14,8 +14,6 @@

use super::*;

use rand::{rngs::StdRng, SeedableRng};

impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
/// Checks the given block is valid next block.
pub fn check_next_block<R: CryptoRng + Rng>(&self, block: &Block<N>, rng: &mut R) -> Result<()> {
Expand All @@ -38,14 +36,6 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
}
}

// Ensure each transaction is well-formed and unique.
let transactions = block.transactions();
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}"))
})?;

// TODO (howardwu): Remove this after moving the total supply into credits.aleo.
{
// // Retrieve the latest total supply.
Expand Down Expand Up @@ -78,9 +68,9 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
block.previous_hash(),
)?;

// Ensure speculation over the unconfirmed transactions is correct.
// Ensure speculation over the unconfirmed transactions is correct and ensure each transaction is well-formed and unique.
let ratified_finalize_operations =
self.vm.check_speculate(state, block.ratifications(), block.solutions(), block.transactions())?;
self.vm.check_speculate(state, block.ratifications(), block.solutions(), block.transactions(), rng)?;

// Retrieve the committee lookback.
let committee_lookback = {
Expand Down
16 changes: 15 additions & 1 deletion synthesizer/src/vm/finalize.rs
Expand Up @@ -20,6 +20,8 @@ use rand::{rngs::StdRng, SeedableRng};

impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
/// Speculates on the given list of transactions in the VM.
/// This function aborts all transactions that are not are well-formed or unique.
///
///
/// Returns the confirmed transactions, aborted transaction IDs,
/// and finalize operations from pre-ratify and post-ratify.
Expand Down Expand Up @@ -111,18 +113,30 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
}

/// Checks the speculation on the given transactions in the VM.
/// This function also ensure that the given transactions are well-formed and unique.
///
/// Returns the finalize operations from pre-ratify and post-ratify.
#[inline]
pub fn check_speculate(
pub fn check_speculate<R: Rng + CryptoRng>(
&self,
state: FinalizeGlobalState,
ratifications: &Ratifications<N>,
solutions: &Solutions<N>,
transactions: &Transactions<N>,
rng: &mut R,
) -> Result<Vec<FinalizeOperation<N>>> {
let timer = timer!("VM::check_speculate");

// Ensure each transaction is well-formed and unique.
// NOTE: We perform the transaction checks here prior to `atomic_speculate` because we must
// ensure that the `Fee` transactions are valid. We can't unify the transaction checks in `atomic_speculate`
// because we run speculation on the unconfirmed variant of the transactions.
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(transaction, transaction.to_rejected_id()?, &mut rng)
.map_err(|e| anyhow!("Invalid transaction found in the transactions list: {e}"))
})?;

// Reconstruct the candidate ratifications to verify the speculation.
let candidate_ratifications = ratifications.iter().cloned().collect::<Vec<_>>();
// Reconstruct the unconfirmed transactions to verify the speculation.
Expand Down

0 comments on commit f83d18b

Please sign in to comment.