forked from rollupnc/RollupNC
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
batch rollupnc#1 proven integrity on L2
- Loading branch information
Showing
5 changed files
with
187 additions
and
222 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,142 @@ | ||
const { deployments, ethers } = require('hardhat') | ||
const { solidity } = require("ethereum-waffle"); | ||
const { buildEddsa, buildPoseidon } = require('circomlibjs') | ||
const chai = require("chai").use(solidity) | ||
const { initializeContracts, generateAccounts, L2Account } = require('./utils') | ||
const { IncrementalMerkleTree } = require('@zk-kit/incremental-merkle-tree'); | ||
const { expect } = require('chai'); | ||
|
||
describe("Test rollup deposits", async () => { | ||
let eddsa, poseidon, F; | ||
let signers, accounts; | ||
let rollup; | ||
let zeroCache; | ||
before(async () => { | ||
|
||
// initial | ||
signers = await ethers.getSigners(); | ||
poseidon = await buildPoseidon(); | ||
eddsa = await buildEddsa(); | ||
F = poseidon.F; | ||
|
||
// generate zero cache | ||
const depths = [4, 2]; | ||
zeroCache = [BigInt(0)]; | ||
for (let i = 1; i <= depths[0]; i++) { | ||
const root = zeroCache[i - 1]; | ||
const internalNode = poseidon([root, root]) | ||
zeroCache.push(F.toObject(internalNode)); | ||
} | ||
rollup = await initializeContracts(zeroCache); | ||
// set accounts | ||
accounts = await generateAccounts(poseidon, eddsa); | ||
}) | ||
describe('Deposits', async () => { | ||
describe('Batch #1', async () => { | ||
it('Deposit #0 (0 ADDRESS)', async () => { | ||
// check deposit fn execution logic | ||
const tx = rollup.deposit([0, 0], 0, 0, { from: accounts.coordinator.L1.address }); | ||
await expect(tx).to.emit(rollup, 'RequestDeposit').withArgs([0, 0], 0, 0); | ||
// check deposit queue | ||
const expectedRoot = L2Account.emptyRoot(poseidon); | ||
const depositRoot = F.toObject((await rollup.describeDeposits())._leaves[0]); | ||
expect(expectedRoot).to.be.equal(depositRoot); | ||
}) | ||
it('Deposit #1 (COORDINATOR ADDRESS)', async () => { | ||
// check deposit fn execution logic | ||
const l2Pubkey = accounts.coordinator.L2.pubkey.map(point => F.toObject(point)); | ||
const tx = rollup.deposit(l2Pubkey, 0, 0, { from: accounts.coordinator.L1.address }); | ||
await expect(tx).to.emit(rollup, 'RequestDeposit').withArgs(l2Pubkey, 0, 0); | ||
// check deposit queue | ||
const data = [...l2Pubkey, 0, 0, 0]; | ||
const leafRoot = F.toObject(poseidon(data)); | ||
const sibling = L2Account.emptyRoot(poseidon); | ||
const expectedRoot = F.toObject(poseidon([sibling, leafRoot])); | ||
const depositRoot = F.toObject((await rollup.describeDeposits())._leaves[0]); | ||
expect(expectedRoot).to.be.equal(depositRoot); | ||
}) | ||
it('Deposit #2 (Alice)', async () => { | ||
// check deposit fn execution logic | ||
const l2Pubkey = accounts.alice.L2.pubkey.map(point => F.toObject(point)); | ||
const tx = rollup.connect(accounts.alice.L1).deposit(l2Pubkey, 20, 1, { value: 20 }); | ||
await expect(tx).to.emit(rollup, 'RequestDeposit').withArgs(l2Pubkey, 20, 1); | ||
accounts.alice.L2.credit(BigInt(20)); | ||
// check deposit queue | ||
const expectedRoot = accounts.alice.L2.root; | ||
const depositRoot = F.toObject((await rollup.describeDeposits())._leaves[1]); | ||
expect(expectedRoot).to.be.equal(depositRoot); | ||
}) | ||
it('Deposit #3 (Bob)', async () => { | ||
// check deposit fn execution logic | ||
const l2Pubkey = accounts.bob.L2.pubkey.map(point => F.toObject(point)); | ||
const tx = rollup.connect(accounts.bob.L1).deposit(l2Pubkey, 15, 1, { value: 15 }); | ||
await expect(tx).to.emit(rollup, 'RequestDeposit').withArgs(l2Pubkey, 15, 1); | ||
accounts.bob.L2.credit(BigInt(15)); | ||
// check deposit queue | ||
const coordinatorPubkey = accounts.coordinator.L2.pubkey.map(point => F.toObject(point)); | ||
const coordinatorLeaf = F.toObject(poseidon([...coordinatorPubkey, 0, 0, 0])); | ||
const sibling = F.toObject(poseidon([L2Account.emptyRoot(poseidon), coordinatorLeaf])) | ||
const current = F.toObject(poseidon([accounts.alice.L2.root, accounts.bob.L2.root])); | ||
const expectedRoot = F.toObject(poseidon([sibling, current])); | ||
const depositRoot = F.toObject((await rollup.describeDeposits())._leaves[0]); | ||
expect(expectedRoot).to.be.equal(depositRoot); | ||
}) | ||
it('Process Batch #1 (4 new balance leaves)', async () => { | ||
// construct expected values | ||
const emptyLeaf = L2Account.emptyRoot(poseidon) | ||
const coordinatorPubkey = accounts.coordinator.L2.pubkey.map(point => F.toObject(point)); | ||
const coordinatorLeaf = F.toObject(poseidon([...coordinatorPubkey, 0, 0, 0])); | ||
const expectTree = new IncrementalMerkleTree(poseidon, 4, 0); | ||
expectTree.insert(emptyLeaf); | ||
expectTree.insert(coordinatorLeaf); | ||
expectTree.insert(accounts.alice.L2.root); | ||
expectTree.insert(accounts.bob.L2.root); | ||
const expected = { | ||
oldRoot: zeroCache[zeroCache.length - 1], | ||
newRoot: F.toObject(expectTree.root) | ||
} | ||
// construct transaction | ||
const position = [0, 0]; | ||
const proof = [zeroCache[2], zeroCache[3]]; | ||
const tx = rollup.connect(accounts.coordinator.L1).processDeposits(2, position, proof); | ||
// verify execution integrity | ||
await expect(tx).to.emit(rollup, "ConfirmDeposit").withArgs( | ||
expected.oldRoot, | ||
expected.newRoot, | ||
4 | ||
); | ||
}) | ||
|
||
}) | ||
describe('Batch #2', async () => { | ||
xit('Deposit #4 (Charlie)', async () => { | ||
// check deposit fn execution logic | ||
const l2Pubkey = accounts.charlie.L2.pubkey.map(point => F.toObject(point)); | ||
const tx = rollup.connect(accounts.charlie.L1).deposit(l2Pubkey, 500, 1, { value: 500 }); | ||
await expect(tx).to.emit(rollup, 'RequestDeposit').withArgs(l2Pubkey, 500, 1); | ||
accounts.charlie.L2.credit(BigInt(500)); | ||
// check deposit queue | ||
const expectedRoot = accounts.charlie.L2.root; | ||
const depositRoot = F.toObject((await rollup.describeDeposits())._leaves[0]); | ||
expect(expectedRoot).to.be.equal(depositRoot); | ||
}) | ||
xit('Deposit #5 (David)', async () => { | ||
// check deposit fn execution logic | ||
const l2Pubkey = accounts.david.L2.pubkey.map(point => F.toObject(point)); | ||
const tx = rollup.connect(accounts.david.L1).deposit(l2Pubkey, 499, 1, { value: 500 }); | ||
await expect(tx).to.emit(rollup, 'RequestDeposit').withArgs(l2Pubkey, 499, 1); | ||
accounts.david.L2.credit(BigInt(499)); | ||
// check deposit queue | ||
const expectedRoot = F.toObject(poseidon[ | ||
accounts.charlie.L2.root, | ||
accounts.david.L2.root | ||
]) | ||
const depositRoot = F.toObject((await rollup.describeDeposits())._leaves[0]); | ||
expect(expectedRoot).to.be.equal(depositRoot); | ||
}) | ||
xit('Process Batch #2 (2 new balance leaves', async () => { | ||
|
||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.