Skip to content

Commit

Permalink
Merge pull request #406 from o1-labs/feature/address-variable
Browse files Browse the repository at this point in the history
Make address a variable
  • Loading branch information
mitschabaude authored Sep 14, 2022
2 parents 1cde74a + dab42a1 commit b24540a
Show file tree
Hide file tree
Showing 28 changed files with 347 additions and 228 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- **Breaking change:** Rename the `Party` class to `AccountUpdate`
- **Breaking change:** Rename the `Party` class to `AccountUpdate`. Also, rename other occurrences of "party" to "account update". https://github.com/o1-labs/snarkyjs/pull/393
- **Breaking change:** Don't require the account address as input to `SmartContract.compile()`, `SmartContract.digest()` and `SmartContract.analyzeMethods()` https://github.com/o1-labs/snarkyjs/pull/406
- This works because the address / public key is now a variable in the method circuit; it used to be a constant

## [0.5.2](https://github.com/o1-labs/snarkyjs/compare/55c8ea0...4f0dd40)

Expand Down
1 change: 1 addition & 0 deletions run-integration-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
./run src/examples/zkapps/reducer/reducer_composite.ts || exit 1
./run src/examples/zkapps/composability.ts || exit 1
./run src/examples/zkapps/token_with_proofs.ts || exit 1
./run src/examples/zkapps/dex/run.ts || exit 1
2 changes: 1 addition & 1 deletion src/examples/circuit_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ await isReady;
let address = PrivateKey.random().toPublicKey();

console.log('compile...');
await MyContract.compile(address);
await MyContract.compile();
// should work
console.log('prove...');
let tx = await Mina.transaction(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/examples/deploy/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ await isReady;
let zkappKey = PrivateKey.random();
let zkappAddress = zkappKey.toPublicKey();

let { verificationKey } = await SimpleZkapp.compile(zkappAddress);
let { verificationKey } = await SimpleZkapp.compile();
storeArtifact(SimpleZkapp, { verificationKey });

shutdown();
Expand Down
2 changes: 1 addition & 1 deletion src/examples/local_events_zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ let zkapp = new SimpleZkapp(zkappAddress);

if (doProofs) {
console.log('compile');
await SimpleZkapp.compile(zkappAddress);
await SimpleZkapp.compile();
}

console.log('deploy');
Expand Down
2 changes: 1 addition & 1 deletion src/examples/set_local_preconditions_zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ let zkapp = new SimpleZkapp(zkappAddress);

if (doProofs) {
console.log('compile');
await SimpleZkapp.compile(zkappAddress);
await SimpleZkapp.compile();
}

console.log('deploy');
Expand Down
2 changes: 1 addition & 1 deletion src/examples/simple_zkapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let initialState = Field(1);
let zkapp = new SimpleZkapp(zkappAddress);

console.log('compile');
await SimpleZkapp.compile(zkappAddress);
await SimpleZkapp.compile();

console.log('deploy');
let tx = await Mina.transaction(feePayerKey, () => {
Expand Down
2 changes: 1 addition & 1 deletion src/examples/simple_zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ let zkapp = new SimpleZkapp(zkappAddress);

if (doProofs) {
console.log('compile');
await SimpleZkapp.compile(zkappAddress);
await SimpleZkapp.compile();
}

console.log('deploy');
Expand Down
2 changes: 1 addition & 1 deletion src/examples/simple_zkapp_berkeley.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let initialState = Field(1);
// compile the SmartContract to get the verification key (if deploying) or cache the provers (if updating)
// this can take a while...
console.log('Compiling smart contract...');
let { verificationKey } = await SimpleZkapp.compile(zkappAddress);
let { verificationKey } = await SimpleZkapp.compile();

// check if the zkapp is already deployed, based on whether the account exists and its first zkapp state is != 0
let zkapp = new SimpleZkapp(zkappAddress);
Expand Down
6 changes: 2 additions & 4 deletions src/examples/simple_zkapp_with_proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ let zkappAddress2 = zkappKey2.toPublicKey();

// compile and prove trivial zkapp
console.log('compile (trivial zkapp)');
let { verificationKey: trivialVerificationKey } = await TrivialZkapp.compile(
zkappAddress2
);
let { verificationKey: trivialVerificationKey } = await TrivialZkapp.compile();
// TODO: should we have a simpler API for zkapp proving without
// submitting transactions? or is this an irrelevant use case?
// would also improve the return type -- `Proof` instead of `(Proof | undefined)[]`
Expand All @@ -79,7 +77,7 @@ trivialProof = await testJsonRoundtripAndVerify(
);

console.log('compile');
let { verificationKey } = await NotSoSimpleZkapp.compile(zkappAddress);
let { verificationKey } = await NotSoSimpleZkapp.compile();

let zkapp = new NotSoSimpleZkapp(zkappAddress);

Expand Down
2 changes: 1 addition & 1 deletion src/examples/sudoku/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let zkapp = new SudokuZkapp(zkappAddress);
let sudoku = generateSudoku(0.5);

console.log('Deploying Sudoku...');
await SudokuZkapp.compile(zkappAddress);
await SudokuZkapp.compile();
let tx = await Mina.transaction(account1, () => {
AccountUpdate.fundNewAccount(account1);
let zkapp = new SudokuZkapp(zkappAddress);
Expand Down
6 changes: 3 additions & 3 deletions src/examples/zkapps/composability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ let incrementerZkapp = new Incrementer(incrementerAddress);

if (doProofs) {
console.log('compile (incrementer)');
await Incrementer.compile(incrementerAddress);
await Incrementer.compile();
console.log('compile (adder)');
await Adder.compile(adderAddress);
await Adder.compile();
console.log('compile (caller)');
await Caller.compile(zkappAddress);
await Caller.compile();
}

console.log('deploy');
Expand Down
80 changes: 23 additions & 57 deletions src/examples/zkapps/dex/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@ import {
tokenIds,
} from './dex.js';

/**
* TODOs
*
* - make address a variable, or make smart contract store multiple vks/provers, one per address & tokenId
* to fix proving for X and Y token contracts
* - get rid of nonce increments
*
*/

await isReady;
let doProofs = false;
let doProofs = true;

let Local = Mina.LocalBlockchain();
Mina.setActiveInstance(Local);
Expand All @@ -28,75 +19,50 @@ let [{ privateKey: feePayerKey }] = Local.testAccounts;
let tx;

// analyze methods for quick error feedback
TokenContract.analyzeMethods(addresses.tokenX);
TokenContract.analyzeMethods(addresses.tokenY);
DexTokenHolder.analyzeMethods(addresses.dex, tokenIds.X);
DexTokenHolder.analyzeMethods(addresses.dex, tokenIds.Y);
Dex.analyzeMethods(addresses.dex);
TokenContract.analyzeMethods();
DexTokenHolder.analyzeMethods();
Dex.analyzeMethods();

if (doProofs) {
// compile & deploy all 5 zkApps
console.log('compile (token X)...');
await TokenContract.compile(addresses.tokenX);
// console.log('compile (token Y)...');
// await TokenContract.compile(addresses.tokenY);
console.log('compile (dex token holder X)...');
await DexTokenHolder.compile(addresses.dex, tokenIds.X);
// console.log('compile (dex token holder Y)...');
// await DexTokenHolder.compile(addresses.dex, tokenIds.Y);
console.log('compile (token)...');
await TokenContract.compile();
console.log('compile (dex token holder)...');
await DexTokenHolder.compile();
console.log('compile (dex main contract)...');
await Dex.compile(addresses.dex);
await Dex.compile();
}
let tokenX = new TokenContract(addresses.tokenX);
let tokenY = new TokenContract(addresses.tokenY);
let dex = new Dex(addresses.dex);
let dexX = new DexTokenHolder(addresses.dex, tokenIds.X);
let dexY = new DexTokenHolder(addresses.dex, tokenIds.Y);

console.log('deploy (x5)...');
tx = await Mina.transaction({ feePayerKey, fee: accountFee.mul(1) }, () => {
// fund 2 new accounts, and fund token contracts so each can create 1 token account
console.log('deploy & init token contracts...');
tx = await Mina.transaction({ feePayerKey }, () => {
// pay fees for creating 2 token contract accounts, and fund them so each can create 1 account themselves
let feePayerUpdate = AccountUpdate.createSigned(feePayerKey);
feePayerUpdate.balance.subInPlace(accountFee.mul(3));
feePayerUpdate.balance.subInPlace(accountFee.mul(2));
feePayerUpdate.send({ to: addresses.tokenX, amount: accountFee });
feePayerUpdate.send({ to: addresses.tokenY, amount: accountFee });

tokenX.deploy();
// tokenY.deploy();
// tokenX.deployZkapp(addresses.dex);
// tokenY.deployZkapp(addresses.dex);
dex.deploy();

// initialize tokens
// tokenX.init();
// tokenY.init();
tokenY.deploy();
tokenX.init();
tokenY.init();
});

await tx.prove();
tx.sign([keys.tokenX, keys.tokenY, keys.dex]);
console.log(tx.toJSON());
tx.sign([keys.tokenX, keys.tokenY]);
tx.send();

console.log('deploy tokens...');
console.log('deploy dex contracts...');
tx = await Mina.transaction(feePayerKey, () => {
// fund 5 new accounts
AccountUpdate.createSigned(feePayerKey).balance.subInPlace(
Mina.accountCreationFee().mul(1)
);
// initialize tokens
tokenX.init();
if (!doProofs) tokenX.sign();
// tokenY.init();

// tokenX.deploy();
// tokenY.deploy();
// pay fees for creating 3 dex accounts
AccountUpdate.createSigned(feePayerKey).balance.subInPlace(accountFee.mul(3));
dex.deploy();
tokenX.deployZkapp(addresses.dex);
if (!doProofs) tokenX.sign();
// tokenY.deployZkapp(addresses.dex);
// dex.deploy();
tokenY.deployZkapp(addresses.dex);
});
console.log(tx.toJSON());

await tx.prove();
tx.sign([keys.dex, keys.tokenX]);
tx.sign([keys.dex]);
tx.send();
2 changes: 1 addition & 1 deletion src/examples/zkapps/merkle_tree/merkle_zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ initialCommitment = Tree.getRoot();
let leaderboardZkApp = new Leaderboard(zkappAddress);
console.log('Deploying leaderboard..');
if (doProofs) {
await Leaderboard.compile(zkappAddress);
await Leaderboard.compile();
}
let tx = await Mina.transaction(feePayer, () => {
AccountUpdate.fundNewAccount(feePayer, { initialBalance });
Expand Down
2 changes: 1 addition & 1 deletion src/examples/zkapps/reducer/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ let zkappAddress = zkappKey.toPublicKey();
let zkapp = new CounterZkapp(zkappAddress);
if (doProofs) {
console.log('compile');
await CounterZkapp.compile(zkappAddress);
await CounterZkapp.compile();
}

console.log('deploy');
Expand Down
2 changes: 1 addition & 1 deletion src/examples/zkapps/reducer/reducer_composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ let zkappAddress = zkappKey.toPublicKey();
let zkapp = new CounterZkapp(zkappAddress);
if (doProofs) {
console.log('compile');
await CounterZkapp.compile(zkappAddress);
await CounterZkapp.compile();
}

console.log('deploy');
Expand Down
9 changes: 6 additions & 3 deletions src/examples/zkapps/simple_and_counter_zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ class SimpleZkapp extends SmartContract {
callerAddress.assertEquals(privilegedAddress);

// assert that the caller nonce is 0, and increment the nonce - this way, payout can only happen once
let callerAccountUpdate = Experimental.createChildAccountUpdate(this.self, callerAddress);
let callerAccountUpdate = Experimental.createChildAccountUpdate(
this.self,
callerAddress
);
callerAccountUpdate.account.nonce.assertEquals(UInt32.zero);
callerAccountUpdate.body.incrementNonce = Bool(true);

Expand Down Expand Up @@ -167,8 +170,8 @@ let counterZkapp = new CounterZkapp(counterZkappAddress);

if (doProofs) {
console.log('compile');
await SimpleZkapp.compile(zkappAddress);
await CounterZkapp.compile(counterZkappAddress);
await SimpleZkapp.compile();
await CounterZkapp.compile();
}

console.log('deploy');
Expand Down
6 changes: 3 additions & 3 deletions src/examples/zkapps/token_with_proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ console.log('feePayer', feePayer.toPublicKey().toBase58());
console.log('-------------------------------------------');

console.log('compile (TokenContract)');
await TokenContract.compile(tokenZkAppAddress);
await TokenContract.compile();
console.log('compile (ZkAppB)');
await ZkAppB.compile(zkAppBAddress, tokenId);
await ZkAppB.compile();
console.log('compile (ZkAppC)');
await ZkAppC.compile(zkAppCAddress, tokenId);
await ZkAppC.compile();

console.log('deploy tokenZkApp');
tx = await Local.transaction(feePayer, () => {
Expand Down
Loading

0 comments on commit b24540a

Please sign in to comment.