Skip to content

Commit

Permalink
refactor(zkapps): update import paths and improve code readability
Browse files Browse the repository at this point in the history
- refactor: change import paths from 'experimental-zkapp-offchain-storage' to 'experimental-offchain-zkapp-storage' for better naming consistency
- refactor: replace 'Field.zero' with 'Field(0)' for better readability
- refactor: replace 'assertGt' with 'assertGreaterThan' for better readability
- refactor: replace 'setPermissions' with 'account.permissions.set' for better readability
- refactor: remove unnecessary 'SignedMessageBoard.test.ts' file
- refactor: clean up unnecessary imports and await calls
- refactor: update transaction signing and sending process for better readability
- refactor: update 'loopUntilAccountExists' and 'makeAndSendTransaction' functions for better readability
- refactor: update 'zkAppNeedsInitialization' function for better readability

These changes were made to improve the readability and maintainability of the code.
  • Loading branch information
MartinMinkov committed Aug 25, 2023
1 parent 0a619fb commit 1fc95e3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import {

import {
OffChainStorage,
Update,
MerkleWitness8,
} from 'experimental-zkapp-offchain-storage';
} from 'experimental-offchain-zkapp-storage';

export class NumberTreeContract extends SmartContract {
@state(PublicKey) storageServerPublicKey = State<PublicKey>();
Expand All @@ -33,7 +32,7 @@ export class NumberTreeContract extends SmartContract {

@method initState(storageServerPublicKey: PublicKey) {
this.storageServerPublicKey.set(storageServerPublicKey);
this.storageNumber.set(Field.zero);
this.storageNumber.set(Field(0));

const emptyTreeRoot = new MerkleTree(8).getRoot();
this.storageTreeRoot.set(emptyTreeRoot);
Expand All @@ -60,7 +59,7 @@ export class NumberTreeContract extends SmartContract {
let newLeaf = [num];

// newLeaf can be a function of the existing leaf
newLeaf[0].assertGt(leaf[0]);
newLeaf[0].assertGreaterThan(leaf[0]);

const updates = [
{
Expand All @@ -85,4 +84,3 @@ export class NumberTreeContract extends SmartContract {
this.storageNumber.set(storedNewRootNumber);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
OffChainStorage,
Update,
MerkleWitness8,
} from 'experimental-zkapp-offchain-storage';
} from 'experimental-offchain-zkapp-storage';

export class SignedMessageBoard extends SmartContract {
@state(PublicKey) storageServerPublicKey = State<PublicKey>();
Expand All @@ -26,15 +26,15 @@ export class SignedMessageBoard extends SmartContract {

deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
});
}

@method initState(storageServerPublicKey: PublicKey) {
this.storageServerPublicKey.set(storageServerPublicKey);
this.storageNumber.set(Field.zero);
this.storageNumber.set(Field(0));

const emptyTreeRoot = new MerkleTree(8).getRoot();
this.storageTreeRoot.set(emptyTreeRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,10 @@ import { NumberTreeContract } from './NumberTreeContract.js';
import {
OffChainStorage,
MerkleWitness8,
} from 'experimental-zkapp-offchain-storage';
} from 'experimental-offchain-zkapp-storage';
import fs from 'fs';

import {
Mina,
isReady,
PublicKey,
PrivateKey,
AccountUpdate,
Group,
Character,
CircuitString,
Signature,
Field,
Bool,
shutdown,
} from 'snarkyjs';
import { Mina, PrivateKey, AccountUpdate, Field, Bool } from 'snarkyjs';

import { makeAndSendTransaction, loopUntilAccountExists } from './utils.js';

Expand All @@ -28,8 +15,6 @@ const NodeXMLHttpRequest =

const useLocal = true;

await isReady;

// ----------------------------------------

const transactionFee = 100_000_000;
Expand Down Expand Up @@ -90,10 +75,10 @@ if (useLocal) {
zkapp.initState(serverPublicKey);
});
transaction.sign([zkappPrivateKey, feePayerKey]);
await transaction.prove()
await transaction.prove();
await transaction.send();
} else {
let zkAppAccount = await loopUntilAccountExists({
await loopUntilAccountExists({
account: zkappPrivateKey.toPublicKey(),
eachTimeNotExist: () =>
console.log('waiting for zkApp account to be deployed...'),
Expand Down Expand Up @@ -130,8 +115,8 @@ async function updateTree() {
priorLeafNumber = idx2fields.get(index)![0];
newLeafNumber = priorLeafNumber.add(3);
} else {
priorLeafNumber = Field.zero;
newLeafNumber = Field.one;
priorLeafNumber = Field(0);
newLeafNumber = Field(1);
}

// update the leaf, and save it in the storage server
Expand Down Expand Up @@ -175,9 +160,8 @@ async function updateTree() {
doUpdate();
}
);

updateTransaction.sign([zkappPrivateKey, feePayerKey]);
await updateTransaction.prove()
await updateTransaction.prove();
await updateTransaction.send();
} else {
await makeAndSendTransaction({
Expand All @@ -198,5 +182,3 @@ for (;;) {
}

//---------------------------

await shutdown();
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { SignedMessageBoard } from './SignedMessageBoard.js';
import { OffChainStorage, MerkleWitness8 } from 'experimental-zkapp-offchain-storage';
import {
OffChainStorage,
MerkleWitness8,
} from 'experimental-offchain-zkapp-storage';

import {
Mina,
isReady,
PublicKey,
PrivateKey,
AccountUpdate,
Expand All @@ -12,18 +14,14 @@ import {
CircuitString,
Signature,
Bool,
shutdown,
} from 'snarkyjs';

import XMLHttpRequestTs from 'xmlhttprequest-ts';
const NodeXMLHttpRequest =
XMLHttpRequestTs.XMLHttpRequest as any as typeof XMLHttpRequest;

async function main() {
await isReady;

// ----------------------------------------

const transactionFee = 100_000_000;

const treeHeight = 8;
Expand All @@ -32,6 +30,7 @@ async function main() {
Mina.setActiveInstance(Local);

const feePayerKey = Local.testAccounts[0].privateKey;
const feePayerPublicKey = feePayerKey.toPublicKey();

const zkappPrivateKey = PrivateKey.random();
const zkappPublicKey = zkappPrivateKey.toPublicKey();
Expand All @@ -47,13 +46,12 @@ async function main() {

const zkapp = new SignedMessageBoard(zkappPublicKey);

const transaction = await Mina.transaction(feePayerKey, () => {
AccountUpdate.fundNewAccount(feePayerKey);
const transaction = await Mina.transaction(feePayerPublicKey, () => {
AccountUpdate.fundNewAccount(feePayerPublicKey);
zkapp.deploy({ zkappKey: zkappPrivateKey });
zkapp.initState(serverPublicKey);
zkapp.sign(zkappPrivateKey);
});

transaction.sign([feePayerKey, zkappPrivateKey]);
await transaction.send();

// ----------------------------------------
Expand Down Expand Up @@ -84,7 +82,7 @@ async function main() {

const publicKeyFields = fields.slice(0, 2);
priorLeafSigner = PublicKey.fromGroup(
new Group(publicKeyFields[0], publicKeyFields[1])
new Group({ x: publicKeyFields[0], y: publicKeyFields[1] })
);

const messageFields = fields.slice(2);
Expand Down Expand Up @@ -112,7 +110,7 @@ async function main() {

// update the smart contract
const updateTransaction = await Mina.transaction(
{ feePayerKey, fee: transactionFee },
{ sender: feePayerPublicKey, fee: transactionFee },
() => {
zkapp!.update(
priorLeafMessage,
Expand All @@ -125,18 +123,13 @@ async function main() {
storedNewStorageNumber,
storedNewStorageSignature
);
zkapp.sign(zkappPrivateKey);
}
);

updateTransaction.sign([feePayerKey, zkappPrivateKey]);
await updateTransaction.send();

console.log('root updated to', zkapp.storageTreeRoot.get().toString());

//---------------------------

await shutdown();
}

main();

Loading

0 comments on commit 1fc95e3

Please sign in to comment.