Skip to content

Commit

Permalink
Reorder checks
Browse files Browse the repository at this point in the history
  • Loading branch information
raychu86 committed Jan 20, 2024
1 parent 10dfb72 commit abc2053
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions synthesizer/src/vm/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
let mut deployments = IndexSet::new();
// Initialize a counter for the confirmed transaction index.
let mut counter = 0u32;
// Initialize a list of created transition IDs.
let mut transition_ids: IndexSet<N::TransitionID> = IndexSet::new();
// Initialize a list of spent input IDs.
let mut input_ids: IndexSet<Field<N>> = IndexSet::new();
// Initialize a list of created output IDs.
let mut output_ids: IndexSet<Field<N>> = IndexSet::new();
// Initialize a list of created transition IDs.
let mut transition_ids: IndexSet<N::TransitionID> = IndexSet::new();

// Finalize the transactions.
'outer: for transaction in transactions {
Expand All @@ -239,6 +239,19 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
continue 'outer;
}

// Ensure that the transaction is not producing a duplicate transition.
for transition_id in transaction.transition_ids() {
// If the transition ID is already produced in this block or previous blocks, abort the transaction.
if transition_ids.contains(transition_id)
|| self.transition_store().contains_transition_id(transition_id).unwrap_or(true)
{
// Store the aborted transaction.
aborted.push((transaction.clone(), format!("Duplicate transition {transition_id}")));
// Continue to the next transaction.
continue 'outer;
}
}

// Ensure that the transaction is not double-spending an input.
for input_id in transaction.input_ids() {
// If the input ID is already spent in this block or previous blocks, abort the transaction.
Expand All @@ -265,19 +278,6 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
}
}

// Ensure that the transaction is not producing a duplicate transition.
for transition_id in transaction.transition_ids() {
// If the transition ID is already produced in this block or previous blocks, abort the transaction.
if transition_ids.contains(transition_id)
|| self.transition_store().contains_transition_id(transition_id).unwrap_or(true)
{
// Store the aborted transaction.
aborted.push((transaction.clone(), format!("Duplicate transition {transition_id}")));
// Continue to the next transaction.
continue 'outer;
}
}

// Process the transaction in an isolated atomic batch.
// - If the transaction succeeds, the finalize operations are stored.
// - If the transaction fails, the atomic batch is aborted and no finalize operations are stored.
Expand Down Expand Up @@ -391,12 +391,12 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
match outcome {
// If the transaction succeeded, store it and continue to the next transaction.
Ok(confirmed_transaction) => {
// Add the transition IDs to the set of produced transition IDs.
transition_ids.extend(confirmed_transaction.transaction().transition_ids());
// Add the input IDs to the set of spent input IDs.
input_ids.extend(confirmed_transaction.transaction().input_ids());
// Add the output IDs to the set of produced output IDs.
output_ids.extend(confirmed_transaction.transaction().output_ids());
// Add the transition IDs to the set of produced transition IDs.
transition_ids.extend(confirmed_transaction.transaction().transition_ids());
// Store the confirmed transaction.
confirmed.push(confirmed_transaction);
// Increment the transaction index counter.
Expand Down

0 comments on commit abc2053

Please sign in to comment.