Skip to content

Instantly share code, notes, and snippets.

@rizary
Created March 28, 2022 23:31
Show Gist options
  • Select an option

  • Save rizary/32f4b026d2a4f70132695b0419271ebd to your computer and use it in GitHub Desktop.

Select an option

Save rizary/32f4b026d2a4f70132695b0419271ebd to your computer and use it in GitHub Desktop.
// Andika: this is the recursive proof system
@proofSystem
class RollupProof extends ProofWithInput<RollupStateTransition> {
// Andika: first, it has to process the deposit from L1
@branch static processDeposit(
pending: MerkleStack<RollupDeposit>, // deposit queue
accountDb: AccountDb // L2 account
): RollupProof {
let before = new RollupState(pending.commitment, accountDb.commitment()); // current L2 state
let deposit = pending.pop(); // get deposit from queue
let [{ isSome }, mem] = accountDb.get(deposit.publicKey);
isSome.assertEquals(false); // Andika: make sure that the account does not already exist
// Andika: 2. Create account on L2
let account = new RollupAccount(
UInt64.zero,
UInt32.zero,
deposit.publicKey
);
accountDb.set(mem, account);
let after = new RollupState(pending.commitment, accountDb.commitment()); // new L2 state
// Andika: finally, this function generate and return proof of L2 state update from deposit
return new RollupProof(new RollupStateTransition(before, after));
}
// Andika: 3. Process L2 transaction
@branch static transaction(
t: RollupTransaction,
s: Signature,
pending: MerkleStack<RollupDeposit>,
accountDb: AccountDb
): RollupProof {
s.verify(t.sender, t.toFields()).assertEquals(true); // verify signature
let stateBefore = new RollupState(
pending.commitment,
accountDb.commitment()
);
// Andika: 4. Get sender account from database
let [senderAccount, senderPos] = accountDb.get(t.sender);
senderAccount.isSome.assertEquals(true); // assure account exists
senderAccount.value.nonce.assertEquals(t.nonce); // assure transaction nonce is validity
// Andika: 5. Send the transaction
senderAccount.value.balance = senderAccount.value.balance.sub(t.amount);
senderAccount.value.nonce = senderAccount.value.nonce.add(1);
// Andika: 6. Update the account info
accountDb.set(senderPos, senderAccount.value);
// Andika: 7. transaction receive
let [receiverAccount, receiverPos] = accountDb.get(t.receiver);
receiverAccount.value.balance = receiverAccount.value.balance.add(t.amount);
accountDb.set(receiverPos, receiverAccount.value);
let stateAfter = new RollupState(
pending.commitment,
accountDb.commitment()
);
// Andika: finally, this function generate and return proof of L2 state update from transaction
return new RollupProof(new RollupStateTransition(stateBefore, stateAfter));
}
// Andika: last, it merge two proofs into one
@branch static merge(p1: RollupProof, p2: RollupProof): RollupProof {
p1.publicInput.target.assertEquals(p2.publicInput.source);
// Andika: finally, it generate and return proof of total state transition
return new RollupProof(
new RollupStateTransition(p1.publicInput.source, p2.publicInput.target)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment