Skip to content

Commit 3d3da79

Browse files
committed
refactor: deduplicate PDA derivation and fix naming
1 parent f0f13c2 commit 3d3da79

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

blackjack/encrypted-ixs/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ mod circuits {
8888
player_hand_size: u8,
8989
dealer_hand_size: u8,
9090
) -> (Enc<Shared, Hand>, bool) {
91-
let deck_array = deck_ctxt.to_arcis().unpack();
91+
let deck = deck_ctxt.to_arcis().unpack();
9292

9393
let mut player_hand = player_hand_ctxt.to_arcis().unpack();
9494

9595
let card_index = (player_hand_size + dealer_hand_size) as usize;
96-
player_hand[player_hand_size as usize] = deck_array[card_index];
96+
player_hand[player_hand_size as usize] = deck[card_index];
9797

9898
let is_bust = calculate_hand_value(&player_hand, player_hand_size + 1) > 21;
9999

voting/tests/voting.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ describe("Voting", () => {
157157

158158
// Cast votes for each poll with different outcomes
159159
const voteOutcomes = [true, false, true]; // Different outcomes for each poll
160+
let firstPollPDA: PublicKey;
161+
let firstVoterRecordPDA: PublicKey;
160162
for (let i = 0; i < POLL_IDS.length; i++) {
161163
const POLL_ID = POLL_IDS[i];
162164
const vote = BigInt(voteOutcomes[i]);
@@ -182,6 +184,11 @@ describe("Voting", () => {
182184
program.programId
183185
);
184186

187+
if (i === 0) {
188+
firstPollPDA = pollPDA;
189+
firstVoterRecordPDA = voterRecordPDA;
190+
}
191+
185192
const voteComputationOffset = new anchor.BN(randomBytes(8), "hex");
186193

187194
const queueVoteSig = await program.methods
@@ -234,22 +241,12 @@ describe("Voting", () => {
234241
}
235242

236243
// Test double-vote prevention: attempt to vote again on the first poll
244+
// Reuse firstPollPDA and firstVoterRecordPDA derived during the voting loop
237245
console.log("\n--- Testing double-vote prevention ---");
238246
const DOUBLE_VOTE_POLL_ID = POLL_IDS[0];
239247
const doubleVoteNonce = randomBytes(16);
240248
const doubleVoteCiphertext = cipher.encrypt([BigInt(true)], doubleVoteNonce);
241249

242-
const doubleVotePollIdBuffer = Buffer.alloc(4);
243-
doubleVotePollIdBuffer.writeUInt32LE(DOUBLE_VOTE_POLL_ID);
244-
const [doubleVotePollPDA] = PublicKey.findProgramAddressSync(
245-
[Buffer.from("poll"), owner.publicKey.toBuffer(), doubleVotePollIdBuffer],
246-
program.programId
247-
);
248-
const [doubleVoteVoterRecordPDA] = PublicKey.findProgramAddressSync(
249-
[Buffer.from("voter"), doubleVotePollPDA.toBuffer(), owner.publicKey.toBuffer()],
250-
program.programId
251-
);
252-
253250
const doubleVoteComputationOffset = new anchor.BN(randomBytes(8), "hex");
254251

255252
try {
@@ -277,8 +274,8 @@ describe("Voting", () => {
277274
Buffer.from(getCompDefAccOffset("vote")).readUInt32LE()
278275
),
279276
authority: owner.publicKey,
280-
pollAcc: doubleVotePollPDA,
281-
voterRecord: doubleVoteVoterRecordPDA,
277+
pollAcc: firstPollPDA,
278+
voterRecord: firstVoterRecordPDA,
282279
})
283280
.rpc({
284281
preflightCommitment: "confirmed",

0 commit comments

Comments
 (0)