-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: TXE detects duplicate nullifiers (#10764)
This functionality was lost in a refactor. Added tests to verify
- Loading branch information
Showing
6 changed files
with
187 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
noir-projects/noir-contracts/contracts/easy_private_voting_contract/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod test; | ||
use dep::aztec::macros::aztec; | ||
|
||
#[aztec] | ||
|
111 changes: 111 additions & 0 deletions
111
noir-projects/noir-contracts/contracts/easy_private_voting_contract/src/test/first.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use crate::test::utils; | ||
use dep::aztec::oracle::{execution::get_block_number, storage::storage_read}; | ||
use dep::aztec::protocol_types::storage::map::derive_storage_slot_in_map; | ||
|
||
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"); | ||
} | ||
|
||
#[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"); | ||
} | ||
|
||
#[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"); | ||
} | ||
|
||
#[test(should_fail)] | ||
unconstrained fn test_fail_end_vote_by_non_admin() { | ||
let (env, voting_contract_address, _) = utils::setup(); | ||
let alice = env.create_account(); | ||
|
||
env.impersonate(alice); | ||
EasyPrivateVoting::at(voting_contract_address).end_vote().call(&mut env.public()); | ||
} | ||
|
||
#[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"); | ||
} | ||
|
||
#[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"); | ||
} | ||
|
||
#[test(should_fail)] | ||
unconstrained fn test_fail_vote_twice() { | ||
let (env, voting_contract_address, _) = utils::setup(); | ||
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()); | ||
|
||
// Vote again as alice | ||
env.advance_block_by(1); | ||
EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate).call(&mut env.private()); | ||
} |
2 changes: 2 additions & 0 deletions
2
noir-projects/noir-contracts/contracts/easy_private_voting_contract/src/test/mod.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod first; | ||
mod utils; |
21 changes: 21 additions & 0 deletions
21
noir-projects/noir-contracts/contracts/easy_private_voting_contract/src/test/utils.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use dep::aztec::{ | ||
note::{note_getter::{MAX_NOTES_PER_PAGE, view_notes}, note_viewer_options::NoteViewerOptions}, | ||
prelude::AztecAddress, | ||
protocol_types::storage::map::derive_storage_slot_in_map, | ||
test::helpers::test_environment::TestEnvironment, | ||
}; | ||
|
||
use crate::EasyPrivateVoting; | ||
|
||
pub fn setup() -> (&mut TestEnvironment, AztecAddress, AztecAddress) { | ||
let mut env = TestEnvironment::new(); | ||
|
||
let admin = env.create_account(); | ||
|
||
let initializer_call_interface = EasyPrivateVoting::interface().constructor(admin); | ||
let voting_contract = env.deploy_self("EasyPrivateVoting").with_public_void_initializer( | ||
initializer_call_interface, | ||
); | ||
// std::println(voting_contract); | ||
(&mut env, voting_contract.to_address(), admin) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters