Skip to content

Commit

Permalink
hack to make server stay alive!
Browse files Browse the repository at this point in the history
  • Loading branch information
Trivo25 committed Apr 16, 2023
1 parent 0fa7f63 commit 5605155
Show file tree
Hide file tree
Showing 7 changed files with 646 additions and 48 deletions.
35 changes: 12 additions & 23 deletions src/create_rollup.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/* eslint-disable no-unused-vars */
import {
DeployArgs,
Experimental,
Field,
isReady,
method,
PrivateKey,
Signature,
SmartContract,
state,
State,
Permissions,
Proof,
} from 'snarkyjs';
import { Field, isReady, SmartContract } from 'snarkyjs';
import { NetworkState, RollupState } from './proof_system/state_transition.js';
import { Prover } from './proof_system/prover.js';
import { DepositStore } from './lib/data_store/DepositStore.js';
Expand Down Expand Up @@ -49,21 +36,22 @@ async function zkRollup(

await userContract.compile();

console.log(1);
let { RollupProver, ProofClass, PublicInputType } = Prover(userContract);
let compiledProver = await RollupProver.compile();

console.log(2);
const RollupZkapp = RollupContract(feePayer, RollupProver);

console.log(3);
let compiledContract = await RollupZkapp.compile();

console.log(4);
let accountStore = new AccountStore();
let depositStore = new DepositStore();

let transactionPool: Transaction[] = [];
let transactionHistory: Transaction[] = [];

let root = accountStore.getMerkleRoot()!.toString()!;

let accountRoot = accountStore.getMerkleRoot()!;
let depositRoot = depositStore.getMerkleRoot()!;
// this is also just temporary
let globalState: GlobalState = {
accountTree: accountStore,
Expand All @@ -73,14 +61,14 @@ async function zkRollup(
state: {
// represents the actual on-chain state
committed: new RollupState({
accountDbCommitment: Field(0),
pendingDepositsCommitment: Field(0),
accountDbCommitment: accountRoot,
pendingDepositsCommitment: depositRoot,
network: NetworkState.empty(),
}),
// represents the current rollup state
current: new RollupState({
accountDbCommitment: Field(0),
pendingDepositsCommitment: Field(0),
accountDbCommitment: accountRoot,
pendingDepositsCommitment: depositRoot,
network: NetworkState.empty(),
}),
},
Expand All @@ -93,6 +81,7 @@ async function zkRollup(
await rollup.start(port);
logger.log(`Graphql server running on http://localhost:${port}/graphql`);
console.error('Not further implemented');
return new Promise(() => {});
},
async deploy() {
throw Error('Not implemented');
Expand Down
39 changes: 30 additions & 9 deletions src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
Field,
PrivateKey,
Mina,
Experimental,
Proof,
UInt64,
Permission,
ZkappPublicInput,
} from 'snarkyjs';
import { Account } from './proof_system/account';
import {
Expand All @@ -30,6 +32,22 @@ await MyContract.compile();
let Local = Mina.LocalBlockchain();
Mina.setActiveInstance(Local);

function RollupClient({
sequencer,
mina,
}: {
sequencer: string;
mina: string;
}) {
return {
Network: Mina.Network(mina),
sendTx: async (
tx: Mina.Transaction,
proofs: (Proof<ZkappPublicInput> | undefined)[]
) => {},
};
}

let feePayerKey = Local.testAccounts[0].privateKey;
let feePayer = Local.testAccounts[0].publicKey;
let zkappKey = PrivateKey.random();
Expand All @@ -46,15 +64,10 @@ await tx.sign([feePayerKey, zkappKey]).send();

tx = await Mina.transaction(feePayer, () => {
zkapp.update(Field(1));
let u = AccountUpdate.create(zkappAddress);
u.send({
to: PrivateKey.random().toPublicKey(),
amount: 10,
});
u.requireSignature();
});
await tx.sign([feePayerKey, zkappKey]);
let [p1] = await tx.prove();
let pis = await tx.prove();
let [p1] = pis;

class ContractProof extends MyContract.Proof() {}

Expand All @@ -63,6 +76,13 @@ proof.verify();

let update = tx.transaction.accountUpdates[0];

let { Network, sendTx } = RollupClient({
sequencer: '',
mina: '',
});

await sendTx(tx, pis);

/* const RollupProver = Experimental.ZkProgram({
publicInput: Field,
Expand Down Expand Up @@ -97,13 +117,14 @@ let acc = Account.empty();
acc.publicKey = update.body.publicKey;
acc.tokenId = update.body.tokenId;
acc.balance.total = UInt64.from(500);

acc.permissions.editState = Permission.proof();
const State = new RollupState({
accountDbCommitment: Field(0),
pendingDepositsCommitment: Field(0),
network: NetworkState.empty(),
});

console.log('verifier');
const verifier = getVerifier(MyContract);
verifier(
new StateTransition({ source: State, target: State }),
Expand Down
15 changes: 11 additions & 4 deletions src/proof_system/state_transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,24 @@ function getVerifier(Contract: typeof SmartContract) {
account: Account
) {
const body = accountUpdate.body;

console.log(1);
// verify proof and check that the hash matches
proof.verify();
accountUpdate.hash().assertEquals(proof.publicInput.accountUpdate);
console.log(2);

// check that the public key and tokenid matches
body.publicKey.assertEquals(account.publicKey);
body.tokenId.assertEquals(account.tokenId);
console.log(3);

// check that the account has enough balance, if the account is supposed to transfer funds
let balanceChangeValid = Circuit.if(
body.balanceChange.sgn.isPositive(),
Bool(true),
account.balance.total.greaterThanOrEqual(body.balanceChange.magnitude)
);

balanceChangeValid.assertTrue('Not enough balance');

// TODO: Events, will skip for now
Expand Down Expand Up @@ -257,10 +260,14 @@ function getVerifier(Contract: typeof SmartContract) {

// TODO: verify pseudo dynamically what type of auth is given
const authIsProof = Bool(true);
isTypeProof(account.permissions.editState).assertEquals(
authIsProof,
'Authorization is of type proofm but AccountUpdate had different authorization'
);

isTypeProof(account.permissions.editState).assertEquals(authIsProof);

isTypeImpossible(account.permissions.editState).assertFalse();
isTypeImpossible(account.permissions.editState).assertFalse(
'Authorization is impossible'
);

// TODO: continue
};
Expand Down
6 changes: 2 additions & 4 deletions src/rollup_operator/services/setupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ function setupService(
typeDefs: Schema,
resolvers,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
}) as any;
});

return {
async start(port: number) {
await graphql.start();
graphql.applyMiddleware({ app });
await new Promise<void>((resolve) =>
httpServer.listen({ port }, resolve)
);
httpServer.listen({ port });
},
};
}
Loading

0 comments on commit 5605155

Please sign in to comment.