|
1 | 1 | use crate::test::utils; |
2 | | -use dep::aztec::oracle::{execution::get_block_number, storage::storage_read}; |
3 | 2 | use dep::aztec::protocol_types::storage::map::derive_storage_slot_in_map; |
4 | 3 |
|
5 | | -use crate::EasyPrivateVoting; |
| 4 | +use crate::PrivateVoting; |
6 | 5 |
|
7 | 6 | #[test] |
8 | 7 | unconstrained fn test_initializer() { |
9 | | - let (_, voting_contract_address, admin) = utils::setup(); |
| 8 | + let (mut env, voting_contract_address, admin) = utils::setup(); |
10 | 9 |
|
11 | | - let block_number = get_block_number(); |
12 | | - let admin_slot = EasyPrivateVoting::storage_layout().admin.slot; |
13 | | - let admin_storage_value = storage_read(voting_contract_address, admin_slot, block_number); |
14 | | - assert(admin_storage_value == admin, "Admin should be correctly stored"); |
| 10 | + env.public_context_at(voting_contract_address, |context| { |
| 11 | + let current_admin = context.storage_read(PrivateVoting::storage_layout().admin.slot); |
| 12 | + assert_eq(current_admin, admin); |
| 13 | + }); |
15 | 14 | } |
16 | 15 |
|
17 | 16 | #[test] |
18 | 17 | unconstrained fn test_check_vote_status() { |
19 | | - let (_, voting_contract_address, _) = utils::setup(); |
| 18 | + let (mut env, voting_contract_address, _) = utils::setup(); |
20 | 19 |
|
21 | | - let vote_ended_expected: bool = false; |
22 | | - |
23 | | - let block_number = get_block_number(); |
24 | | - let status_slot = EasyPrivateVoting::storage_layout().vote_ended.slot; |
25 | | - let vote_ended_read: bool = storage_read(voting_contract_address, status_slot, block_number); |
26 | | - assert(vote_ended_expected == vote_ended_read, "Vote ended should be false"); |
| 20 | + env.public_context_at(voting_contract_address, |context| { |
| 21 | + let vote_ended = context.storage_read(PrivateVoting::storage_layout().vote_ended.slot); |
| 22 | + assert_eq(vote_ended, false); |
| 23 | + }); |
27 | 24 | } |
28 | 25 |
|
29 | 26 | #[test] |
30 | 27 | unconstrained fn test_end_vote() { |
31 | 28 | let (env, voting_contract_address, admin) = utils::setup(); |
32 | 29 |
|
33 | | - env.call_public(admin, EasyPrivateVoting::at(voting_contract_address).end_vote()); |
34 | | - |
35 | | - let vote_ended_expected = true; |
36 | | - |
37 | | - let block_number = get_block_number(); |
38 | | - let status_slot = EasyPrivateVoting::storage_layout().vote_ended.slot; |
39 | | - let vote_ended_read: bool = storage_read(voting_contract_address, status_slot, block_number); |
40 | | - assert(vote_ended_expected == vote_ended_read, "Vote ended should be true"); |
| 30 | + env.call_public(admin, PrivateVoting::at(voting_contract_address).end_vote()); |
41 | 31 |
|
| 32 | + env.public_context_at(voting_contract_address, |context| { |
| 33 | + let vote_ended = context.storage_read(PrivateVoting::storage_layout().vote_ended.slot); |
| 34 | + assert_eq(vote_ended, true); |
| 35 | + }); |
42 | 36 | } |
43 | 37 |
|
44 | | -#[test(should_fail)] |
| 38 | +#[test(should_fail_with = "Only admin can end votes")] |
45 | 39 | unconstrained fn test_fail_end_vote_by_non_admin() { |
46 | | - let (env, voting_contract_address, _) = utils::setup(); |
| 40 | + let (mut env, voting_contract_address, _) = utils::setup(); |
47 | 41 | let alice = env.create_light_account(); |
48 | | - env.call_public(alice, EasyPrivateVoting::at(voting_contract_address).end_vote()); |
| 42 | + |
| 43 | + env.call_public(alice, PrivateVoting::at(voting_contract_address).end_vote()); |
49 | 44 | } |
50 | 45 |
|
51 | 46 | #[test] |
52 | 47 | unconstrained fn test_cast_vote() { |
53 | | - let (env, voting_contract_address, _) = utils::setup(); |
| 48 | + let (mut env, voting_contract_address, _) = utils::setup(); |
54 | 49 | let alice = env.create_light_account(); |
55 | | - let candidate = 1; |
56 | | - env.call_private(alice, EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate)); |
57 | 50 |
|
58 | | - env.mine_block(); |
59 | | - |
60 | | - // Read vote count from storage |
61 | | - let block_number = get_block_number(); |
62 | | - let tally_slot = EasyPrivateVoting::storage_layout().tally.slot; |
63 | | - let candidate_tally_slot = derive_storage_slot_in_map(tally_slot, candidate); |
64 | | - let vote_count: u32 = storage_read(voting_contract_address, candidate_tally_slot, block_number); |
65 | | - |
66 | | - assert(vote_count == 1, "vote tally should be incremented"); |
| 51 | + let candidate = 1; |
| 52 | + env.call_private(alice, PrivateVoting::at(voting_contract_address).cast_vote(candidate)); |
| 53 | + |
| 54 | + env.public_context_at(voting_contract_address, |context| { |
| 55 | + let vote_count = context.storage_read(derive_storage_slot_in_map( |
| 56 | + PrivateVoting::storage_layout().tally.slot, |
| 57 | + candidate, |
| 58 | + )); |
| 59 | + assert_eq(vote_count, 1); |
| 60 | + }); |
67 | 61 | } |
68 | 62 |
|
69 | 63 | #[test] |
70 | 64 | unconstrained fn test_cast_vote_with_separate_accounts() { |
71 | | - let (env, voting_contract_address, _) = utils::setup(); |
| 65 | + let (mut env, voting_contract_address, _) = utils::setup(); |
72 | 66 | let alice = env.create_light_account(); |
73 | 67 | let bob = env.create_light_account(); |
74 | 68 |
|
75 | 69 | let candidate = 101; |
76 | 70 |
|
77 | | - env.call_private(alice, EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate)); |
78 | | - env.mine_block(); |
79 | | - |
80 | | - env.call_private(bob, EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate)); |
81 | | - env.mine_block(); |
82 | | - |
| 71 | + env.call_private(alice, PrivateVoting::at(voting_contract_address).cast_vote(candidate)); |
83 | 72 |
|
84 | | - // Read vote count from storage |
85 | | - let block_number = get_block_number(); |
86 | | - let tally_slot = EasyPrivateVoting::storage_layout().tally.slot; |
87 | | - let candidate_tally_slot = derive_storage_slot_in_map(tally_slot, candidate); |
88 | | - let vote_count: u32 = storage_read(voting_contract_address, candidate_tally_slot, block_number); |
| 73 | + let _ = env.call_private(bob, PrivateVoting::at(voting_contract_address).cast_vote(candidate)); |
89 | 74 |
|
90 | | - assert(vote_count == 2, "vote tally should be 2"); |
| 75 | + env.public_context_at(voting_contract_address, |context| { |
| 76 | + let vote_count = context.storage_read(derive_storage_slot_in_map( |
| 77 | + PrivateVoting::storage_layout().tally.slot, |
| 78 | + candidate, |
| 79 | + )); |
| 80 | + assert_eq(vote_count, 2); |
| 81 | + }); |
91 | 82 | } |
92 | 83 |
|
93 | | -#[test(should_fail)] |
| 84 | +#[test(should_fail_with = "already exists")] |
94 | 85 | unconstrained fn test_fail_vote_twice() { |
95 | | - let (env, voting_contract_address, _) = utils::setup(); |
| 86 | + let (mut env, voting_contract_address, _) = utils::setup(); |
96 | 87 | let alice = env.create_light_account(); |
97 | 88 |
|
98 | 89 | let candidate = 101; |
99 | 90 |
|
100 | | - env.call_private(alice, EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate)); |
101 | | - env.mine_block(); |
| 91 | + env.call_private(alice, PrivateVoting::at(voting_contract_address).cast_vote(candidate)); |
102 | 92 |
|
103 | | - // Vote again as alice |
104 | | - env.call_private(alice, EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate)); |
| 93 | + env.call_private(alice, PrivateVoting::at(voting_contract_address).cast_vote(candidate)); |
105 | 94 | } |
0 commit comments