-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #361 from o1-labs/feature/zkapp-b-test
- Loading branch information
Showing
11 changed files
with
1,324 additions
and
63 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
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,169 @@ | ||
import { | ||
DeployArgs, | ||
Experimental, | ||
Field, | ||
Permissions, | ||
Mina, | ||
AccountUpdate, | ||
PrivateKey, | ||
SmartContract, | ||
} from 'snarkyjs'; | ||
import { VotingAppParams } from './factory.js'; | ||
|
||
import { Membership_ } from './membership.js'; | ||
|
||
import { Voting_ } from './voting.js'; | ||
|
||
class InvalidContract extends SmartContract { | ||
deploy(args: DeployArgs) { | ||
super.deploy(args); | ||
this.setPermissions({ | ||
...Permissions.default(), | ||
editState: Permissions.none(), | ||
editSequenceState: Permissions.none(), | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Function used to deploy a set of contracts for a given set of preconditions | ||
* @param feePayer the private key used to pay the fees | ||
* @param contracts A set of contracts to deploy | ||
* @param params A set of preconditions and parameters | ||
* @param voterRoot the initial root of the voter store | ||
* @param candidateRoot the initial root of the voter store | ||
* @param votesRoot the initial root of the votes store | ||
*/ | ||
export async function deployContracts( | ||
contracts: { | ||
voterContract: Membership_; | ||
candidateContract: Membership_; | ||
voting: Voting_; | ||
}, | ||
params: VotingAppParams, | ||
voterRoot: Field, | ||
candidateRoot: Field, | ||
votesRoot: Field, | ||
proofsEnabled: boolean = false | ||
): Promise<{ | ||
voterContract: Membership_; | ||
candidateContract: Membership_; | ||
voting: Voting_; | ||
Local: any; | ||
feePayer: PrivateKey; | ||
}> { | ||
let Local = Mina.LocalBlockchain({ | ||
proofsEnabled, | ||
}); | ||
Mina.setActiveInstance(Local); | ||
|
||
let feePayer = Local.testAccounts[0].privateKey; | ||
let { voterContract, candidateContract, voting } = contracts; | ||
|
||
console.log('deploying set of 3 contracts'); | ||
try { | ||
let tx = await Mina.transaction(feePayer, () => { | ||
AccountUpdate.fundNewAccount(feePayer, { | ||
initialBalance: Mina.accountCreationFee().add( | ||
Mina.accountCreationFee() | ||
), | ||
}); | ||
|
||
voting.deploy({ zkappKey: params.votingKey }); | ||
voting.committedVotes.set(votesRoot); | ||
voting.accumulatedVotes.set(Experimental.Reducer.initialActionsHash); | ||
|
||
candidateContract.deploy({ zkappKey: params.candidateKey }); | ||
candidateContract.committedMembers.set(candidateRoot); | ||
candidateContract.accumulatedMembers.set( | ||
Experimental.Reducer.initialActionsHash | ||
); | ||
|
||
voterContract.deploy({ zkappKey: params.voterKey }); | ||
voterContract.committedMembers.set(voterRoot); | ||
voterContract.accumulatedMembers.set( | ||
Experimental.Reducer.initialActionsHash | ||
); | ||
}); | ||
await tx.send(); | ||
} catch (err: any) { | ||
throw Error(err); | ||
} | ||
|
||
console.log('successfully deployed contracts'); | ||
return { voterContract, candidateContract, voting, feePayer, Local }; | ||
} | ||
|
||
/** | ||
* Function used to deploy a set of **invalid** membership contracts for a given set of preconditions | ||
* @param feePayer the private key used to pay the fees | ||
* @param contracts A set of contracts to deploy | ||
* @param params A set of preconditions and parameters | ||
* @param voterRoot the initial root of the voter store | ||
* @param candidateRoot the initial root of the voter store | ||
* @param votesRoot the initial root of the votes store | ||
*/ | ||
export async function deployInvalidContracts( | ||
contracts: { | ||
voterContract: Membership_; | ||
candidateContract: Membership_; | ||
voting: Voting_; | ||
}, | ||
params: VotingAppParams, | ||
voterRoot: Field, | ||
candidateRoot: Field, | ||
votesRoot: Field | ||
): Promise<{ | ||
voterContract: Membership_; | ||
candidateContract: Membership_; | ||
voting: Voting_; | ||
Local: any; | ||
feePayer: PrivateKey; | ||
}> { | ||
let Local = Mina.LocalBlockchain({ | ||
proofsEnabled: false, | ||
}); | ||
Mina.setActiveInstance(Local); | ||
|
||
let feePayer = Local.testAccounts[0].privateKey; | ||
let { voterContract, candidateContract, voting } = contracts; | ||
|
||
console.log('deploying set of 3 contracts'); | ||
try { | ||
let tx = await Mina.transaction(feePayer, () => { | ||
AccountUpdate.fundNewAccount(feePayer, { | ||
initialBalance: Mina.accountCreationFee().add( | ||
Mina.accountCreationFee() | ||
), | ||
}); | ||
|
||
voting.deploy({ zkappKey: params.votingKey }); | ||
voting.committedVotes.set(votesRoot); | ||
voting.accumulatedVotes.set(Experimental.Reducer.initialActionsHash); | ||
|
||
// invalid contracts | ||
|
||
let invalidCandidateContract = new InvalidContract( | ||
params.candidateKey.toPublicKey() | ||
); | ||
|
||
invalidCandidateContract.deploy({ zkappKey: params.candidateKey }); | ||
|
||
candidateContract = invalidCandidateContract as Membership_; | ||
|
||
let invalidVoterContract = new InvalidContract( | ||
params.voterKey.toPublicKey() | ||
); | ||
|
||
invalidVoterContract.deploy({ zkappKey: params.voterKey }); | ||
|
||
voterContract = invalidVoterContract as Membership_; | ||
}); | ||
await tx.send(); | ||
} catch (err: any) { | ||
throw Error(err); | ||
} | ||
|
||
console.log('successfully deployed contracts'); | ||
return { voterContract, candidateContract, voting, feePayer, Local }; | ||
} |
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,33 @@ | ||
import { | ||
Field, | ||
SmartContract, | ||
state, | ||
State, | ||
method, | ||
DeployArgs, | ||
Permissions, | ||
} from 'snarkyjs'; | ||
|
||
export class DummyContract extends SmartContract { | ||
@state(Field) sum = State<Field>(); | ||
|
||
deploy(args: DeployArgs) { | ||
super.deploy(args); | ||
this.setPermissions({ | ||
...Permissions.default(), | ||
editState: Permissions.proofOrSignature(), | ||
editSequenceState: Permissions.proofOrSignature(), | ||
setPermissions: Permissions.proofOrSignature(), | ||
setVerificationKey: Permissions.proofOrSignature(), | ||
incrementNonce: Permissions.proofOrSignature(), | ||
}); | ||
this.sum.set(Field.zero); | ||
} | ||
|
||
/** | ||
* Method used to add two variables together. | ||
*/ | ||
@method add(x: Field, y: Field) { | ||
this.sum.set(x.add(y)); | ||
} | ||
} |
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,12 @@ | ||
import { CircuitValue, prop, UInt32 } from 'snarkyjs'; | ||
|
||
export default class ElectionPreconditions extends CircuitValue { | ||
@prop startElection: UInt32; | ||
@prop endElection: UInt32; | ||
|
||
constructor(startElection: UInt32, endElection: UInt32) { | ||
super(); | ||
this.startElection = startElection; | ||
this.endElection = endElection; | ||
} | ||
} |
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
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,18 @@ | ||
import { CircuitValue, prop, UInt64 } from 'snarkyjs'; | ||
|
||
export default class ParticipantPreconditions extends CircuitValue { | ||
@prop minMinaVote: UInt64; | ||
@prop minMinaCandidate: UInt64; | ||
@prop maxMinaCandidate: UInt64; | ||
|
||
constructor( | ||
minMinaVote: UInt64, | ||
minMinaCandidate: UInt64, | ||
maxMinaCandidate: UInt64 | ||
) { | ||
super(); | ||
this.minMinaVote = minMinaVote; | ||
this.minMinaCandidate = minMinaCandidate; | ||
this.maxMinaCandidate = maxMinaCandidate; | ||
} | ||
} |
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
Oops, something went wrong.