diff --git a/src/test/first.nr b/src/test/first.nr index 18b2a0b..9e1a3f8 100644 --- a/src/test/first.nr +++ b/src/test/first.nr @@ -1,50 +1,33 @@ use crate::test::utils; - -use dep::aztec::test::{helpers::{cheatcodes, test_environment::TestEnvironment}}; -use dep::aztec::protocol_types::storage::map::derive_storage_slot_in_map; -use dep::aztec::note::note_getter::{MAX_NOTES_PER_PAGE, view_notes}; -use dep::aztec::note::note_viewer_options::NoteViewerOptions; -use dep::aztec::hash::compute_secret_hash; - -use dep::aztec::{oracle::{execution::{get_block_number, get_contract_address}, storage::storage_read}}; - +use dep::aztec::{ + hash::compute_secret_hash, + note::note_getter::{MAX_NOTES_PER_PAGE, view_notes}, + note::note_viewer_options::NoteViewerOptions, + protocol_types::storage::map::derive_storage_slot_in_map, + oracle::{execution::{get_block_number, get_contract_address}, storage::storage_read}, +}; +use dep::aztec::test::helpers::{cheatcodes, test_environment::TestEnvironment}; use crate::EasyPrivateVoting; #[test] unconstrained fn test_initializer() { let (_, voting_contract_address, admin) = utils::setup(); - - let block_number = get_block_number(); - let admin_slot = EasyPrivateVoting::storage_layout().admin.slot; - let admin_storage_value = storage_read(voting_contract_address, admin_slot, block_number); - assert(admin_storage_value == admin, "Vote ended should be false"); + check_storage_value(voting_contract_address, EasyPrivateVoting::storage_layout().admin.slot, admin, "Admin should match setup value"); } #[test] unconstrained fn test_check_vote_status() { let (_, voting_contract_address, _) = utils::setup(); - - let vote_ended_expected: bool = false; - - let block_number = get_block_number(); - let status_slot = EasyPrivateVoting::storage_layout().vote_ended.slot; - let vote_ended_read: bool = storage_read(voting_contract_address, status_slot, block_number); - assert(vote_ended_expected == vote_ended_read, "Vote ended should be false"); + check_storage_value(voting_contract_address, EasyPrivateVoting::storage_layout().vote_ended.slot, false, "Vote ended should be false at initialization"); } #[test] unconstrained fn test_end_vote() { let (env, voting_contract_address, admin) = utils::setup(); - env.impersonate(admin); - EasyPrivateVoting::at(voting_contract_address).end_vote().call(&mut env.public()); - - let vote_ended_expected = true; - let block_number = get_block_number(); - let status_slot = EasyPrivateVoting::storage_layout().vote_ended.slot; - let vote_ended_read: bool = storage_read(voting_contract_address, status_slot, block_number); - assert(vote_ended_expected == vote_ended_read, "Vote ended should be true"); + EasyPrivateVoting::at(voting_contract_address).end_vote().call(&mut env.public()); + check_storage_value(voting_contract_address, EasyPrivateVoting::storage_layout().vote_ended.slot, true, "Vote ended should be true after end_vote call"); } #[test(should_fail)] @@ -59,45 +42,16 @@ unconstrained fn test_fail_end_vote_by_non_admin() { #[test] unconstrained fn test_cast_vote() { let (env, voting_contract_address, _) = utils::setup(); - let alice = env.create_account(); - env.impersonate(alice); - - let candidate = 1; - env.advance_block_by(6); - EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate).call(&mut env.private()); - - // Read vote count from storage - let block_number = get_block_number(); - let tally_slot = EasyPrivateVoting::storage_layout().tally.slot; - let candidate_tally_slot = derive_storage_slot_in_map(tally_slot, candidate); - let vote_count: u32 = storage_read(voting_contract_address, candidate_tally_slot, block_number); - - assert(vote_count == 1, "vote tally should be incremented"); + cast_vote_and_check_tally(&env, voting_contract_address, env.create_account(), 1, 1); } #[test] unconstrained fn test_cast_vote_with_separate_accounts() { let (env, voting_contract_address, _) = utils::setup(); - let alice = env.create_account(); - let bob = env.create_account(); let candidate = 101; - - env.impersonate(alice); - env.advance_block_by(1); - EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate).call(&mut env.private()); - - env.impersonate(bob); - env.advance_block_by(1); - EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate).call(&mut env.private()); - - // Read vote count from storage - let block_number = get_block_number(); - let tally_slot = EasyPrivateVoting::storage_layout().tally.slot; - let candidate_tally_slot = derive_storage_slot_in_map(tally_slot, candidate); - let vote_count: u32 = storage_read(voting_contract_address, candidate_tally_slot, block_number); - - assert(vote_count == 2, "vote tally should be 2"); + cast_vote_and_check_tally(&env, voting_contract_address, env.create_account(), candidate, 1); + cast_vote_and_check_tally(&env, voting_contract_address, env.create_account(), candidate, 2); } #[test(should_fail)] @@ -106,12 +60,28 @@ unconstrained fn test_fail_vote_twice() { let alice = env.create_account(); let candidate = 101; - - env.impersonate(alice); - env.advance_block_by(1); - EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate).call(&mut env.private()); + cast_vote(&env, voting_contract_address, alice, candidate); + cast_vote(&env, voting_contract_address, alice, candidate); +} + +// Helper function to check storage values +fn check_storage_value(contract_address: Address, slot: Slot, expected: T, error_message: &str) { + let block_number = get_block_number(); + let value: T = storage_read(contract_address, slot, block_number); + assert_eq!(value, expected, "{}", error_message); +} + +// Helper function for voting and tally check +fn cast_vote_and_check_tally(env: &TestEnvironment, contract_address: Address, account: Account, candidate: u32, expected_tally: u32) { + cast_vote(env, contract_address, account, candidate); + let tally_slot = EasyPrivateVoting::storage_layout().tally.slot; + let candidate_tally_slot = derive_storage_slot_in_map(tally_slot, candidate); + check_storage_value(contract_address, candidate_tally_slot, expected_tally, "Vote tally mismatch"); +} - // Vote again as alice +// Helper function for voting +fn cast_vote(env: &TestEnvironment, contract_address: Address, account: Account, candidate: u32) { + env.impersonate(account); env.advance_block_by(1); - EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate).call(&mut env.private()); + EasyPrivateVoting::at(contract_address).cast_vote(candidate).call(&mut env.private()); }