Skip to content

Commit

Permalink
fix broken permission and vk tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Trivo25 committed Oct 19, 2022
1 parent 9b6e2ad commit 0888294
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
52 changes: 29 additions & 23 deletions src/examples/zkapps/voting/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function testSet(
Field.zero,
Field.zero,
Field.zero,
false
true
);
console.log('checking that the tx is valid using default verification key');

Expand All @@ -95,25 +95,19 @@ export async function testSet(
);

console.log('changing verification key');

let { verificationKey } = await DummyContract.compile();

try {
let tx = await Mina.transaction(verificationKeySet.feePayer, () => {
let update = AccountUpdate.create(params.votingKey.toPublicKey());
update.update.verificationKey.value = {
await assertValidTx(
true,
() => {
let vkUpdate = AccountUpdate.createSigned(params.votingKey);

AccountUpdate.setValue(vkUpdate.update.verificationKey, {
...verificationKey,
hash: Field.fromString(verificationKey.hash),
};
});
await tx.prove();
await tx.send();
} catch (err: any) {
throw Error(`Transaction failed! Error: ${err}`);
}

console.log(
'producing proof against updated (invalid) verification key - expecting to fail'
});
},
verificationKeySet.feePayer
);

await assertValidTx(
Expand All @@ -124,10 +118,12 @@ export async function testSet(
Field.zero,
UInt64.from(15)
);
verificationKeySet.Local.addAccount(m.publicKey, m.balance.toString());

verificationKeySet.voting.voterRegistration(m);
},
verificationKeySet.feePayer,
'assert_equal'
'Invalid proof'
);

/*
Expand Down Expand Up @@ -177,17 +173,27 @@ export async function testSet(
permissionedSet.feePayer
);

console.log('trying to change permissions and invoking a method...');
console.log('trying to change permissions...');

await assertValidTx(
false,
true,
() => {
permissionedSet.voterContract.setPermissions({
let permUpdate = AccountUpdate.createSigned(params.voterKey);

AccountUpdate.setValue(permUpdate.update.permissions, {
...Permissions.default(),
setPermissions: Permissions.none(),
editSequenceState: Permissions.impossible(),
});
},
permissionedSet.feePayer
);

console.log('trying to invoke method with invalid permissions...');

await assertValidTx(
false,
() => {
let m = Member.from(
PrivateKey.random().toPublicKey(),
Field.zero,
Expand All @@ -198,7 +204,7 @@ export async function testSet(
permissionedSet.voting.voterRegistration(m);
},
permissionedSet.feePayer,
'sequence_state'
'sequenceEvents'
);

/*
Expand Down Expand Up @@ -442,7 +448,7 @@ export async function testSet(
voting.voterRegistration(v);
},
feePayer,
'assert_equal'
'rangeCheckHelper'
);

console.log('attempting to register a voter with too high balance...');
Expand All @@ -458,7 +464,7 @@ export async function testSet(
voting.voterRegistration(v);
},
feePayer,
'assert_equal'
'rangeCheckHelper'
);

console.log('attempting to register the same voter twice...');
Expand Down
20 changes: 9 additions & 11 deletions src/examples/zkapps/voting/voting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class Voting_ extends SmartContract {
editState: Permissions.proofOrSignature(),
editSequenceState: Permissions.proofOrSignature(),
incrementNonce: Permissions.proofOrSignature(),
setVerificationKey: Permissions.proofOrSignature(),
setVerificationKey: Permissions.none(),
setPermissions: Permissions.proofOrSignature(),
});
this.accumulatedVotes.set(Experimental.Reducer.initialActionsHash);
Expand Down Expand Up @@ -124,10 +124,9 @@ export class Voting_ extends SmartContract {
);

let balance = accountUpdate.account.balance.get();
balance
.gte(voterPreconditions.minMina)
.and(balance.lte(voterPreconditions.maxMina))
.assertTrue();

balance.assertGte(voterPreconditions.minMina);
balance.assertLte(voterPreconditions.maxMina);

let VoterContract: Membership_ = new Membership_(voterAddress);
let exists = VoterContract.addEntry(member);
Expand Down Expand Up @@ -164,10 +163,9 @@ export class Voting_ extends SmartContract {
);

let balance = accountUpdate.account.balance.get();
balance
.gte(candidatePreconditions.minMina)
.and(balance.lte(candidatePreconditions.maxMina))
.assertTrue();

balance.assertGte(candidatePreconditions.minMina);
balance.assertLte(candidatePreconditions.maxMina);

let CandidateContract: Membership_ = new Membership_(candidateAddress);
let exists = CandidateContract.addEntry(member);
Expand Down Expand Up @@ -200,14 +198,14 @@ export class Voting_ extends SmartContract {
*/
@method
vote(candidate: Member, voter: Member) {
/* let currentSlot = this.network.globalSlotSinceGenesis.get();
let currentSlot = this.network.globalSlotSinceGenesis.get();
this.network.globalSlotSinceGenesis.assertEquals(currentSlot);

// we can only vote in the election period time frame
currentSlot
.gte(electionPreconditions.startElection)
.and(currentSlot.lte(electionPreconditions.endElection))
.assertTrue(); */
.assertTrue();

// verifying that both the voter and the candidate are actually part of our member set
// ideally we would also verify a signature here, but ignoring that for now
Expand Down

0 comments on commit 0888294

Please sign in to comment.