Skip to content

Commit

Permalink
Improve test debugging, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu committed Oct 20, 2023
1 parent 51e5164 commit 1533a2b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 36 deletions.
2 changes: 1 addition & 1 deletion ledger/benches/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function hello:

c.bench_function("Transaction::Deploy - verify", |b| {
let transaction = vm.deploy(&private_key, &program, Some(records[0].clone()), 600000, None, rng).unwrap();
b.iter(|| assert!(vm.verify_transaction(&transaction, None)))
b.iter(|| vm.check_transaction(&transaction, None).unwrap())
});
}

Expand Down
6 changes: 3 additions & 3 deletions ledger/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ finalize foo:
// Deploy.
let transaction = ledger.vm.deploy(&private_key, &program, credits, 0, None, rng).unwrap();
// Verify.
assert!(ledger.vm().verify_transaction(&transaction, None));
ledger.vm().check_transaction(&transaction, None).unwrap();

// Construct the next block.
let block =
Expand Down Expand Up @@ -287,7 +287,7 @@ finalize foo:
let transaction =
ledger.vm.execute(&private_key, ("dummy.aleo", "foo"), inputs, Some(sufficient_record), 0, None, rng).unwrap();
// Verify.
assert!(ledger.vm.verify_transaction(&transaction, None));
ledger.vm.check_transaction(&transaction, None).unwrap();
// Ensure that the ledger deems the transaction valid.
assert!(ledger.check_transaction_basic(&transaction, None).is_ok());
}
Expand Down Expand Up @@ -419,7 +419,7 @@ finalize foo:
// Deploy.
let transaction = ledger.vm.deploy(&private_key, &program, None, 0, None, rng).unwrap();
// Verify.
assert!(ledger.vm().verify_transaction(&transaction, None));
ledger.vm().check_transaction(&transaction, None).unwrap();

// Construct the next block.
let block =
Expand Down
2 changes: 1 addition & 1 deletion synthesizer/src/vm/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ finalize transfer_public:
.execute(&caller_private_key, (program_id, function_name), inputs.into_iter(), credits, 1, None, rng)
.unwrap();
// Verify.
assert!(vm.verify_transaction(&transaction, None));
vm.check_transaction(&transaction, None).unwrap();

// Return the transaction.
transaction
Expand Down
16 changes: 11 additions & 5 deletions synthesizer/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ function compute:
// Deploy.
let transaction = vm.deploy(&caller_private_key, &program, credits, 10, None, rng).unwrap();
// Verify.
assert!(vm.verify_transaction(&transaction, None));
vm.check_transaction(&transaction, None).unwrap();
// Return the transaction.
transaction
})
Expand Down Expand Up @@ -480,7 +480,7 @@ function compute:
// Construct the execute transaction.
let transaction = vm.execute_authorization(authorization, None, None, rng).unwrap();
// Verify.
assert!(vm.verify_transaction(&transaction, None));
vm.check_transaction(&transaction, None).unwrap();
// Return the transaction.
transaction
})
Expand Down Expand Up @@ -524,7 +524,7 @@ function compute:
.execute(&caller_private_key, ("credits.aleo", "transfer_public"), inputs, record, 0, None, rng)
.unwrap();
// Verify.
assert!(vm.verify_transaction(&transaction, None));
vm.check_transaction(&transaction, None).unwrap();
// Return the transaction.
transaction
})
Expand Down Expand Up @@ -562,15 +562,21 @@ function compute:

// Authorize the fee.
let authorization = vm
.authorize_fee_public(&caller_private_key, 100, 100, execution.to_execution_id().unwrap(), rng)
.authorize_fee_public(
&caller_private_key,
10_000_000,
100,
execution.to_execution_id().unwrap(),
rng,
)
.unwrap();
// Compute the fee.
let fee = vm.execute_fee_authorization(authorization, None, rng).unwrap();

// Construct the transaction.
let transaction = Transaction::from_execution(execution, Some(fee)).unwrap();
// Verify.
assert!(vm.verify_transaction(&transaction, None));
vm.check_transaction(&transaction, None).unwrap();
// Return the transaction.
transaction
})
Expand Down
38 changes: 13 additions & 25 deletions synthesizer/src/vm/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ macro_rules! ensure_is_unique {
}

impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
/// Returns `true` if the transaction is valid.
pub fn verify_transaction(&self, transaction: &Transaction<N>, rejected_id: Option<Field<N>>) -> bool {
self.check_transaction(transaction, rejected_id).map_err(|error| warn!("{error}")).is_ok()
}

/// Verifies the transaction in the VM. On failure, returns an error.
#[inline]
pub fn check_transaction(&self, transaction: &Transaction<N>, rejected_id: Option<Field<N>>) -> Result<()> {
Expand Down Expand Up @@ -308,20 +303,17 @@ mod tests {
// Fetch a deployment transaction.
let deployment_transaction = crate::vm::test_helpers::sample_deployment_transaction(rng);
// Ensure the transaction verifies.
assert!(vm.check_transaction(&deployment_transaction, None).is_ok());
assert!(vm.verify_transaction(&deployment_transaction, None));
vm.check_transaction(&deployment_transaction, None).unwrap();

// Fetch an execution transaction.
let execution_transaction = crate::vm::test_helpers::sample_execution_transaction_with_private_fee(rng);
// Ensure the transaction verifies.
assert!(vm.check_transaction(&execution_transaction, None).is_ok());
assert!(vm.verify_transaction(&execution_transaction, None));
vm.check_transaction(&execution_transaction, None).unwrap();

// Fetch an execution transaction.
let execution_transaction = crate::vm::test_helpers::sample_execution_transaction_with_public_fee(rng);
// Ensure the transaction verifies.
assert!(vm.check_transaction(&execution_transaction, None).is_ok());
assert!(vm.verify_transaction(&execution_transaction, None));
vm.check_transaction(&execution_transaction, None).unwrap();
}

#[test]
Expand All @@ -336,12 +328,12 @@ mod tests {
let deployment = vm.deploy_raw(&program, rng).unwrap();

// Ensure the deployment is valid.
assert!(vm.check_deployment_internal(&deployment).is_ok());
vm.check_deployment_internal(&deployment).unwrap();

// Ensure that deserialization doesn't break the transaction verification.
let serialized_deployment = deployment.to_string();
let deployment_transaction: Deployment<CurrentNetwork> = serde_json::from_str(&serialized_deployment).unwrap();
assert!(vm.check_deployment_internal(&deployment_transaction).is_ok());
vm.check_deployment_internal(&deployment_transaction).unwrap();
}

#[test]
Expand All @@ -361,13 +353,13 @@ mod tests {
// Ensure the proof exists.
assert!(execution.proof().is_some());
// Verify the execution.
assert!(vm.check_execution_internal(&execution).is_ok());
vm.check_execution_internal(&execution).unwrap();

// Ensure that deserialization doesn't break the transaction verification.
let serialized_execution = execution.to_string();
let recovered_execution: Execution<CurrentNetwork> =
serde_json::from_str(&serialized_execution).unwrap();
assert!(vm.check_execution_internal(&recovered_execution).is_ok());
vm.check_execution_internal(&recovered_execution).unwrap();
}
_ => panic!("Expected an execution transaction"),
}
Expand All @@ -393,12 +385,12 @@ mod tests {
// Ensure the proof exists.
assert!(fee.proof().is_some());
// Verify the fee.
assert!(vm.check_fee_internal(&fee, execution_id).is_ok());
vm.check_fee_internal(&fee, execution_id).unwrap();

// Ensure that deserialization doesn't break the transaction verification.
let serialized_fee = fee.to_string();
let recovered_fee: Fee<CurrentNetwork> = serde_json::from_str(&serialized_fee).unwrap();
assert!(vm.check_fee_internal(&recovered_fee, execution_id).is_ok());
vm.check_fee_internal(&recovered_fee, execution_id).unwrap();
}
_ => panic!("Expected an execution with a fee"),
}
Expand All @@ -418,18 +410,15 @@ mod tests {

// Fetch a valid execution transaction with a private fee.
let valid_transaction = crate::vm::test_helpers::sample_execution_transaction_with_private_fee(rng);
assert!(vm.check_transaction(&valid_transaction, None).is_ok());
assert!(vm.verify_transaction(&valid_transaction, None));
vm.check_transaction(&valid_transaction, None).unwrap();

// Fetch a valid execution transaction with a public fee.
let valid_transaction = crate::vm::test_helpers::sample_execution_transaction_with_public_fee(rng);
assert!(vm.check_transaction(&valid_transaction, None).is_ok());
assert!(vm.verify_transaction(&valid_transaction, None));
vm.check_transaction(&valid_transaction, None).unwrap();

// Fetch an valid execution transaction with no fee.
let valid_transaction = crate::vm::test_helpers::sample_execution_transaction_without_fee(rng);
assert!(vm.check_transaction(&valid_transaction, None).is_ok());
assert!(vm.verify_transaction(&valid_transaction, None));
vm.check_transaction(&valid_transaction, None).unwrap();
}

#[test]
Expand Down Expand Up @@ -525,8 +514,7 @@ mod tests {
vm.execute(&caller_private_key, ("testing.aleo", "initialize"), inputs, credits, 10, None, rng).unwrap();

// Verify.
assert!(vm.check_transaction(&transaction, None).is_ok());
assert!(vm.verify_transaction(&transaction, None));
vm.check_transaction(&transaction, None).unwrap();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion synthesizer/tests/test_vm_execute_and_finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn run_test(test: &ProgramTest) -> serde_yaml::Mapping {
};

// Attempt to verify the transaction.
let verified = vm.verify_transaction(&transaction, None);
let verified = vm.check_transaction(&transaction, None).is_ok();
// Store the verification result.
result.insert(serde_yaml::Value::String("verified".to_string()), serde_yaml::Value::Bool(verified));

Expand Down

0 comments on commit 1533a2b

Please sign in to comment.