Skip to content

Commit

Permalink
feat: check transactions in atomic_speculate
Browse files Browse the repository at this point in the history
  • Loading branch information
randomsleep committed Feb 25, 2024
1 parent 569cf5a commit 85a152b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
12 changes: 1 addition & 11 deletions ledger/src/check_next_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@

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<()> {
pub fn check_next_block<R: CryptoRng + Rng>(&self, block: &Block<N>, _rng: &mut R) -> Result<()> {
let height = block.height();

// Ensure the block hash does not already exist.
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
21 changes: 21 additions & 0 deletions synthesizer/src/vm/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use super::*;

use ledger_committee::{MAX_DELEGATORS, MIN_DELEGATOR_STAKE, MIN_VALIDATOR_STAKE};
use rand::{rngs::StdRng, SeedableRng};

impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
/// Speculates on the given list of transactions in the VM.
Expand Down Expand Up @@ -161,6 +162,18 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
Vec<(Transaction<N>, String)>,
Vec<FinalizeOperation<N>>,
)> {
// Ensure each transaction is well-formed.
let rng = &mut rand::thread_rng();
let rngs = (0..transactions.len()).map(|_| StdRng::from_seed(rng.gen())).collect::<Vec<_>>();
let transactions = transactions.collect::<Vec<_>>();
let check_fail_reasons = cfg_iter!(transactions)
.zip(rngs)
.flat_map(|(transaction, mut rng)| match self.check_transaction(transaction, None, &mut rng) {
Ok(_) => None,
Err(e) => Some((transaction.id(), format!("Check failed - {e}"))),
})
.collect::<IndexMap<_, _>>();

// Acquire the atomic lock, which is needed to ensure this function is not called concurrently
// with other `atomic_finalize!` macro calls, which will cause a `bail!` to be triggered erroneously.
// Note: This lock must be held for the entire scope of the call to `atomic_finalize!`.
Expand Down Expand Up @@ -245,6 +258,14 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {

// Finalize the transactions.
'outer: for transaction in transactions {
// Ensure each transaction is well-formed.
if let Some(fail_reason) = check_fail_reasons.get(&transaction.id()) {
// Store the aborted transaction.
aborted.push((transaction.clone(), fail_reason.clone()));
// Continue to the next transaction.
continue 'outer;
}

// Ensure the number of confirmed transactions does not exceed the maximum.
// Upon reaching the maximum number of confirmed transactions, all remaining transactions are aborted.
if confirmed.len() >= Self::MAXIMUM_CONFIRMED_TRANSACTIONS {
Expand Down

0 comments on commit 85a152b

Please sign in to comment.