Skip to content

Commit 5bc8a62

Browse files
committed
use v2.0.2 contract source
1 parent 97146fe commit 5bc8a62

File tree

9 files changed

+86
-100
lines changed

9 files changed

+86
-100
lines changed

scripts/deploy_contract.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EasyPrivateVotingContract } from "../src/artifacts/EasyPrivateVoting.js"
1+
import { PrivateVotingContract } from "../src/artifacts/PrivateVoting.js"
22
import { createLogger, PXE, Logger, SponsoredFeePaymentMethod, Fr } from "@aztec/aztec.js";
33
import { TokenContract } from "@aztec/noir-contracts.js/Token"
44
import { setupPXE } from "../src/utils/setup_pxe.js";
@@ -23,7 +23,7 @@ async function main() {
2323
logger.info('💰 Setting up sponsored fee payment contract...');
2424
const sponsoredFPC = await getSponsoredFPCInstance();
2525
logger.info(`💰 Sponsored FPC instance obtained at: ${sponsoredFPC.address}`);
26-
26+
2727
logger.info('📝 Registering sponsored FPC contract with PXE...');
2828
await pxe.registerContract({ instance: sponsoredFPC, artifact: SponsoredFPCContract.artifact });
2929
const sponsoredPaymentMethod = new SponsoredFeePaymentMethod(sponsoredFPC.address);
@@ -39,19 +39,19 @@ async function main() {
3939
// Deploy voting contract
4040
logger.info('🗳️ Starting voting contract deployment...');
4141
logger.info(`📋 Admin address for voting contract: ${address}`);
42-
43-
const deployTx = EasyPrivateVotingContract.deploy(wallet, address).send({
42+
43+
const deployTx = PrivateVotingContract.deploy(wallet, address).send({
4444
from: wallet.getAddress(),
4545
fee: { paymentMethod: sponsoredPaymentMethod }
4646
});
47-
47+
4848
logger.info('⏳ Waiting for deployment transaction to be mined...');
4949
const votingContract = await deployTx.deployed({ timeout: 120000 });
50-
50+
5151
logger.info(`🎉 Voting Contract deployed successfully!`);
5252
logger.info(`📍 Contract address: ${votingContract.address}`);
5353
logger.info(`👤 Admin address: ${address}`);
54-
54+
5555
// Verify deployment
5656
logger.info('🔍 Verifying contract deployment...');
5757
try {
@@ -61,11 +61,11 @@ async function main() {
6161
from: wallet.getAddress()
6262
});
6363
logger.info(`📊 Initial vote count for candidate 1: ${initialVoteCount}`);
64-
64+
6565
} catch (error) {
6666
logger.error(`❌ Contract verification failed: ${error}`);
6767
}
68-
68+
6969
logger.info('🏁 Deployment process completed successfully!');
7070
logger.info(`📋 Summary:`);
7171
logger.info(` - Contract Address: ${votingContract.address}`);

scripts/fees.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { foundry } from 'viem/chains'
66
import { mnemonicToAccount } from 'viem/accounts';
77
import { FeeJuiceContract } from "@aztec/noir-contracts.js/FeeJuice";
88
import { FPCContract } from "@aztec/noir-contracts.js/FPC";
9-
import { EasyPrivateVotingContract } from "../src/artifacts/EasyPrivateVoting.js"
9+
import { PrivateVotingContract } from "../src/artifacts/PrivateVoting.js"
1010
import { TokenContract } from "@aztec/noir-contracts.js/Token";
1111
// TODO: replace with import from aztec.js when published
1212
import { SponsoredFeePaymentMethod } from '@aztec/aztec.js/fee/testing'
@@ -67,7 +67,7 @@ async function main() {
6767
const paymentMethod = new SponsoredFeePaymentMethod(sponsoredFPC.address);
6868

6969
// Two arbitrary txs to make the L1 message available on L2
70-
const votingContract = await EasyPrivateVotingContract.deploy(wallet1, wallet1.getAddress()).send({
70+
const votingContract = await PrivateVotingContract.deploy(wallet1, wallet1.getAddress()).send({
7171
from: wallet1.getAddress(),
7272
fee: { paymentMethod }
7373
}).deployed();

scripts/interaction_existing_contract.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createLogger, Logger, SponsoredFeePaymentMethod, Fr, AztecAddress } from "@aztec/aztec.js";
2-
import { EasyPrivateVotingContract } from "../src/artifacts/EasyPrivateVoting.js";
2+
import { PrivateVotingContract } from "../src/artifacts/PrivateVoting.js";
33
import { SponsoredFPCContract } from "@aztec/noir-contracts.js/SponsoredFPC";
44
import { setupPXE } from "../src/utils/setup_pxe.js";
55
import { getSponsoredFPCInstance } from "../src/utils/sponsored_fpc.js";
@@ -29,7 +29,7 @@ async function main() {
2929
}
3030

3131
logger.info(`Connecting to voting contract at: ${contractAddress}`);
32-
const votingContract = await EasyPrivateVotingContract.at(
32+
const votingContract = await PrivateVotingContract.at(
3333
AztecAddress.fromString(contractAddress),
3434
wallet
3535
);

scripts/profile_deploy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EasyPrivateVotingContract } from "../src/artifacts/EasyPrivateVoting.js"
1+
import { PrivateVotingContract } from "../src/artifacts/PrivateVoting.js"
22
import { createLogger, PXE, Logger, SponsoredFeePaymentMethod, Fr } from "@aztec/aztec.js";
33
import { setupPXE } from "../src/utils/setup_pxe.js";
44
import { getSponsoredFPCInstance } from "../src/utils/sponsored_fpc.js";
@@ -21,7 +21,7 @@ async function main() {
2121
const wallet = await accountManager.getWallet();
2222
const address = await accountManager.getAddress();
2323

24-
const profileTx = await EasyPrivateVotingContract.deploy(wallet, address).profile({ profileMode: "full", from: address });
24+
const profileTx = await PrivateVotingContract.deploy(wallet, address).profile({ profileMode: "full", from: address });
2525
console.dir(profileTx, { depth: 2 });
2626
}
2727

src/main.nr

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ mod test;
22
use dep::aztec::macros::aztec;
33

44
#[aztec]
5-
pub contract EasyPrivateVoting {
5+
pub contract PrivateVoting {
66
use dep::aztec::{
77
keys::getters::get_public_keys,
88
macros::{functions::{initializer, internal, private, public, utility}, storage::storage},
99
};
10-
use dep::aztec::state_vars::{Map, PublicImmutable, PublicMutable};
1110
use dep::aztec::protocol_types::{
12-
address::{AztecAddress},
13-
traits::ToField,
14-
traits::Hash,
11+
address::AztecAddress,
12+
hash::poseidon2_hash,
13+
traits::{Hash, ToField},
1514
};
15+
use dep::aztec::state_vars::{Map, PublicImmutable, PublicMutable};
1616

1717
#[storage]
1818
struct Storage<Context> {
@@ -34,12 +34,13 @@ pub contract EasyPrivateVoting {
3434
#[private]
3535
// annotation to mark function as private and expose private context
3636
fn cast_vote(candidate: Field) {
37-
let msg_sender_npk_m_hash = get_public_keys(context.msg_sender()).npk_m.hash();
37+
let msg_sender_nullifier_public_key_message_hash =
38+
get_public_keys(context.msg_sender()).npk_m.hash();
3839

39-
let secret = context.request_nsk_app(msg_sender_npk_m_hash); // get secret key of caller of function
40-
let nullifier = std::hash::pedersen_hash([context.msg_sender().to_field(), secret]); // derive nullifier from sender and secret
40+
let secret = context.request_nsk_app(msg_sender_nullifier_public_key_message_hash); // get secret key of caller of function
41+
let nullifier = poseidon2_hash([context.msg_sender().to_field(), secret]); // derive nullifier from sender and secret
4142
context.push_nullifier(nullifier);
42-
EasyPrivateVoting::at(context.this_address()).add_to_tally_public(candidate).enqueue(
43+
PrivateVoting::at(context.this_address()).add_to_tally_public(candidate).enqueue(
4344
&mut context,
4445
);
4546
}

src/test/e2e/accounts.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EasyPrivateVotingContractArtifact, EasyPrivateVotingContract } from "../../artifacts/EasyPrivateVoting.js"
1+
import { PrivateVotingContractArtifact, PrivateVotingContract } from "../../artifacts/PrivateVoting.js"
22
import { AccountManager, AccountWallet, ContractDeployer, createLogger, Fr, PXE, TxStatus, getContractInstanceFromInstantiationParams, Logger } from "@aztec/aztec.js";
33
import { generateSchnorrAccounts } from "@aztec/accounts/testing"
44
import { getSchnorrAccount } from '@aztec/accounts/schnorr';
@@ -114,11 +114,11 @@ describe("Accounts", () => {
114114
}
115115

116116
// arbitrary transactions to progress 2 blocks, and have fee juice on Aztec ready to claim
117-
await EasyPrivateVotingContract.deploy(ownerWallet, ownerWallet.getAddress()).send({
117+
await PrivateVotingContract.deploy(ownerWallet, ownerWallet.getAddress()).send({
118118
from: ownerWallet.getAddress(),
119119
fee: { paymentMethod: sponsoredPaymentMethod }
120120
}).deployed(); // deploy contract with first funded wallet
121-
await EasyPrivateVotingContract.deploy(ownerWallet, ownerWallet.getAddress()).send({
121+
await PrivateVotingContract.deploy(ownerWallet, ownerWallet.getAddress()).send({
122122
from: ownerWallet.getAddress(),
123123
fee: { paymentMethod: sponsoredPaymentMethod }
124124
}).deployed(); // deploy contract with first funded wallet
@@ -153,7 +153,7 @@ describe("Accounts", () => {
153153

154154
it("Sponsored contract deployment", async () => {
155155
const salt = Fr.random();
156-
const VotingContractArtifact = EasyPrivateVotingContractArtifact
156+
const VotingContractArtifact = PrivateVotingContractArtifact
157157
const accounts = await Promise.all(
158158
(await generateSchnorrAccounts(2)).map(
159159
async a => await getSchnorrAccount(pxe, a.secret, a.signingKey, a.salt)

src/test/e2e/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EasyPrivateVotingContractArtifact, EasyPrivateVotingContract } from "../../artifacts/EasyPrivateVoting.js"
1+
import { PrivateVotingContractArtifact, PrivateVotingContract } from "../../artifacts/PrivateVoting.js"
22
import { AccountWallet, ContractDeployer, createLogger, Fr, PXE, TxStatus, getContractInstanceFromInstantiationParams, Logger, ContractInstanceWithAddress } from "@aztec/aztec.js";
33
import { generateSchnorrAccounts } from "@aztec/accounts/testing"
44
import { getSchnorrAccount } from '@aztec/accounts/schnorr';
@@ -62,7 +62,7 @@ describe("Voting", () => {
6262

6363
it("Deploys the contract", async () => {
6464
const salt = Fr.random();
65-
const VotingContractArtifact = EasyPrivateVotingContractArtifact
65+
const VotingContractArtifact = PrivateVotingContractArtifact
6666
const accounts = await Promise.all(
6767
(await generateSchnorrAccounts(2)).map(
6868
async a => await getSchnorrAccount(pxe, a.secret, a.signingKey, a.salt)
@@ -109,7 +109,7 @@ describe("Voting", () => {
109109
it("It casts a vote", async () => {
110110
const candidate = new Fr(1)
111111

112-
const contract = await EasyPrivateVotingContract.deploy(firstWallet, firstWallet.getAddress()).send({
112+
const contract = await PrivateVotingContract.deploy(firstWallet, firstWallet.getAddress()).send({
113113
from: firstWallet.getAddress(),
114114
fee: { paymentMethod: sponsoredPaymentMethod }
115115
}).deployed();
@@ -126,7 +126,7 @@ describe("Voting", () => {
126126
it("It should fail when trying to vote twice", async () => {
127127
const candidate = new Fr(1)
128128

129-
const votingContract = await EasyPrivateVotingContract.deploy(firstWallet, firstWallet.getAddress()).send({
129+
const votingContract = await PrivateVotingContract.deploy(firstWallet, firstWallet.getAddress()).send({
130130
from: firstWallet.getAddress(),
131131
fee: { paymentMethod: sponsoredPaymentMethod }
132132
}).deployed();

src/test/first.nr

Lines changed: 45 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,94 @@
11
use crate::test::utils;
2-
use dep::aztec::oracle::{execution::get_block_number, storage::storage_read};
32
use dep::aztec::protocol_types::storage::map::derive_storage_slot_in_map;
43

5-
use crate::EasyPrivateVoting;
4+
use crate::PrivateVoting;
65

76
#[test]
87
unconstrained fn test_initializer() {
9-
let (_, voting_contract_address, admin) = utils::setup();
8+
let (mut env, voting_contract_address, admin) = utils::setup();
109

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+
});
1514
}
1615

1716
#[test]
1817
unconstrained fn test_check_vote_status() {
19-
let (_, voting_contract_address, _) = utils::setup();
18+
let (mut env, voting_contract_address, _) = utils::setup();
2019

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+
});
2724
}
2825

2926
#[test]
3027
unconstrained fn test_end_vote() {
3128
let (env, voting_contract_address, admin) = utils::setup();
3229

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());
4131

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+
});
4236
}
4337

44-
#[test(should_fail)]
38+
#[test(should_fail_with = "Only admin can end votes")]
4539
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();
4741
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());
4944
}
5045

5146
#[test]
5247
unconstrained fn test_cast_vote() {
53-
let (env, voting_contract_address, _) = utils::setup();
48+
let (mut env, voting_contract_address, _) = utils::setup();
5449
let alice = env.create_light_account();
55-
let candidate = 1;
56-
env.call_private(alice, EasyPrivateVoting::at(voting_contract_address).cast_vote(candidate));
5750

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+
});
6761
}
6862

6963
#[test]
7064
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();
7266
let alice = env.create_light_account();
7367
let bob = env.create_light_account();
7468

7569
let candidate = 101;
7670

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));
8372

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));
8974

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+
});
9182
}
9283

93-
#[test(should_fail)]
84+
#[test(should_fail_with = "already exists")]
9485
unconstrained fn test_fail_vote_twice() {
95-
let (env, voting_contract_address, _) = utils::setup();
86+
let (mut env, voting_contract_address, _) = utils::setup();
9687
let alice = env.create_light_account();
9788

9889
let candidate = 101;
9990

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));
10292

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));
10594
}

src/test/utils.nr

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
use dep::aztec::{
2-
protocol_types::address::AztecAddress,
3-
test::helpers::test_environment::TestEnvironment,
2+
protocol_types::address::AztecAddress, test::helpers::test_environment::TestEnvironment,
43
};
54

6-
use crate::EasyPrivateVoting;
5+
use crate::PrivateVoting;
76

8-
pub unconstrained fn setup() -> (&mut TestEnvironment, AztecAddress, AztecAddress) {
7+
pub unconstrained fn setup() -> (TestEnvironment, AztecAddress, AztecAddress) {
98
let mut env = TestEnvironment::new();
109

11-
let admin = env.create_contract_account();
10+
let admin = env.create_light_account();
1211

13-
let initializer_call_interface = EasyPrivateVoting::interface().constructor(admin);
14-
let voting_contract = env.deploy("EasyPrivateVoting").with_public_initializer(
15-
admin,
16-
initializer_call_interface,
17-
);
12+
let initializer_call_interface = PrivateVoting::interface().constructor(admin);
13+
let voting_contract_address =
14+
env.deploy("PrivateVoting").with_public_initializer(admin, initializer_call_interface);
1815

19-
env.mine_block();
20-
(&mut env, voting_contract, admin)
16+
(env, voting_contract_address, admin)
2117
}

0 commit comments

Comments
 (0)