Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
invocamanman committed Jan 18, 2023
1 parent de3430a commit 1360df2
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .solcover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
skipFiles: ['mocks', 'interfaces']
};
4 changes: 2 additions & 2 deletions compiled-contracts/PolygonZkEVM.json

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions compiled-contracts/PolygonZkEVMBridge.json

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions compiled-contracts/PolygonZkEVMBridgeMock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions compiled-contracts/PolygonZkEVMMock.json

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions deployment/genesis-gen.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

{
"defaultChainId": 1000,
"defaultChainId": 0,
"sequencerAddress": "0xC949254d682D8c9ad5682521675b8F43b102aec4",
"genesis": [
{
Expand All @@ -23,7 +23,6 @@
"value": "0",
"gasLimit": 10000000,
"gasPrice": "0",
"chainId": 1000,
"paramsDeploy": {
"types": [
"address"
Expand All @@ -41,7 +40,6 @@
"value": "0",
"gasLimit": 10000000,
"gasPrice": "0",
"chainId": 1000,
"contractName": "PolygonZkEVMBridge"
},
{
Expand All @@ -51,7 +49,6 @@
"value": "0",
"gasLimit": 10000000,
"gasPrice": "0",
"chainId": 1000,
"function": "initialize",
"paramsFunction": [
1,
Expand Down
11 changes: 8 additions & 3 deletions deployment/genesis.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions docker/scripts/genesis_docker.json

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions docker/scripts/genesis_gen_docker.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

{
"defaultChainId": 1000,
"defaultChainId": 0,
"sequencerAddress": "0xC949254d682D8c9ad5682521675b8F43b102aec4",
"genesis": [
{
Expand Down Expand Up @@ -29,7 +29,6 @@
"value": "0",
"gasLimit": 10000000,
"gasPrice": "0",
"chainId": 1000,
"paramsDeploy": {
"types": [
"address"
Expand All @@ -47,7 +46,6 @@
"value": "0",
"gasLimit": 10000000,
"gasPrice": "0",
"chainId": 1000,
"contractName": "PolygonZkEVMBridge"
},
{
Expand All @@ -57,7 +55,6 @@
"value": "0",
"gasLimit": 10000000,
"gasPrice": "0",
"chainId": 1000,
"function": "initialize",
"paramsFunction": [
1,
Expand Down
2 changes: 1 addition & 1 deletion docs/PolygonZkEVM.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Verify batches internal function
) internal
```
Internal function to consolidate the state automatically once sequence or verify batches are called
It trys to consolidate the first and the middle pending state
It trys to consolidate the first and the middle pending state in the queue



Expand Down
4 changes: 2 additions & 2 deletions docs/lib/DepositContract.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Add a new leaf to the merkle tree
function verifyMerkleProof(
bytes32 leafHash,
bytes32[] smtProof,
uint64 index,
uint32 index,
bytes32 root
) public returns (bool)
```
Expand All @@ -43,7 +43,7 @@ Verify merkle proof
| :--- | :--- | :------------------------------------------------------------------- |
|`leafHash` | bytes32 | Leaf hash
|`smtProof` | bytes32[] | Smt proof
|`index` | uint64 | Index of the leaf
|`index` | uint32 | Index of the leaf
|`root` | bytes32 | Merkle root

### getLeafValue
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
"compile": "npx hardhat compile",
"docker:contracts": "./docker/scripts/deploy-docker.sh",
"push:docker:contracts": "docker push hermeznetwork/geth-zkevm-contracts",
"update:genesis": "node src/create-genesis.js --gen ../deployment/genesis-gen.json --out ../deployment/genesis.json && node src/create-genesis.js --gen ../docker/scripts/genesis_gen_docker.json --out ../docker/scripts/genesis_docker.json"
"update:genesis": "node src/create-genesis.js --gen ../deployment/genesis-gen.json --out ../deployment/genesis.json && node src/create-genesis.js --gen ../docker/scripts/genesis_gen_docker.json --out ../docker/scripts/genesis_docker.json",
"coverage":"npx hardhat coverage",
"gas:report":"REPORT_GAS=true npx hardhat test"
}
}
20 changes: 3 additions & 17 deletions src/create-genesis.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,14 @@ async function main() {
let customRawTx;
const address = genesis.find((o) => o.address === currentTx.from);
const wallet = new ethers.Wallet(address.pvtKey);
if (tx.chainId === 0) {
if (tx.chainId === 0 || tx.chainId === undefined) {
const signData = ethers.utils.RLP.encode([
processorUtils.toHexStringRlp(Scalar.e(tx.nonce)),
processorUtils.toHexStringRlp(tx.gasPrice),
processorUtils.toHexStringRlp(tx.gasLimit),
processorUtils.toHexStringRlp(tx.to),
processorUtils.toHexStringRlp(tx.value),
processorUtils.toHexStringRlp(tx.data),
processorUtils.toHexStringRlp(tx.chainId),
'0x',
'0x',
]);
const digest = ethers.utils.keccak256(signData);
const signingKey = new ethers.utils.SigningKey(address.pvtKey);
Expand Down Expand Up @@ -198,21 +195,10 @@ async function main() {

genesisOutput.root = smtUtils.h4toString(batch.currentStateRoot);
genesisOutput.genesis = accountsOutput;
genesisOutput.rawTxs = rawTxs;

const genesisOutputPath = path.join(__dirname, outPath);
await fs.writeFileSync(genesisOutputPath, JSON.stringify(genesisOutput, null, 2));

if (argv.update) {
const updatePath = (typeof argv.update === 'undefined') ? undefined : argv.update;

if (!fs.existsSync(updatePath)) {
throw new Error('Update file does not exist');
}

const testVectors = JSON.parse(fs.readFileSync(updatePath));
testVectors[0].genesis = genesisOutput.genesis;
testVectors[0].expectedOldRoot = genesisOutput.root;
await fs.writeFileSync(updatePath, JSON.stringify(testVectors, null, 2));
}
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
Expand Down
114 changes: 101 additions & 13 deletions test/contracts/proofOfEfficiency.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-plusplus, no-await-in-loop */
const { expect } = require('chai');
const { ethers, upgrades } = require('hardhat');

Expand Down Expand Up @@ -86,7 +87,7 @@ describe('Polygon ZK-EVM', () => {
);

// fund sequencer address with Matic tokens
await maticTokenContract.transfer(trustedSequencer.address, ethers.utils.parseEther('100'));
await maticTokenContract.transfer(trustedSequencer.address, ethers.utils.parseEther('1000'));
});

it('should check the constructor parameters', async () => {
Expand Down Expand Up @@ -179,6 +180,23 @@ describe('Polygon ZK-EVM', () => {
).to.emit(polygonZkEVMContract, 'SetPendingStateTimeout').withArgs(newPendingStateTimeoutDefault);
expect(await polygonZkEVMContract.pendingStateTimeout()).to.be.equal(newPendingStateTimeoutDefault);

// setMultiplierBatchFee
const newMultiplierBatchFee = 1023;
await expect(polygonZkEVMContract.connect(admin).setMultiplierBatchFee(newMultiplierBatchFee + 1))
.to.be.revertedWith('PolygonZkEVM::setMultiplierBatchFee: newMultiplierBatchFee incorrect range');

await expect(
polygonZkEVMContract.connect(admin).setMultiplierBatchFee(newMultiplierBatchFee),
).to.emit(polygonZkEVMContract, 'SetMultiplierBatchFee').withArgs(newMultiplierBatchFee);
expect(await polygonZkEVMContract.multiplierBatchFee()).to.be.equal(newMultiplierBatchFee);

// setVeryBatchTimeTarget
const newVeryBatchTimeTarget = 100;
await expect(
polygonZkEVMContract.connect(admin).setVeryBatchTimeTarget(newVeryBatchTimeTarget),
).to.emit(polygonZkEVMContract, 'SetVeryBatchTimeTarget').withArgs(newVeryBatchTimeTarget);
expect(await polygonZkEVMContract.veryBatchTimeTarget()).to.be.equal(newVeryBatchTimeTarget);

// setAdmin
await expect(polygonZkEVMContract.setAdmin(deployer.address))
.to.be.revertedWith('PolygonZkEVM::onlyAdmin: Only admin');
Expand Down Expand Up @@ -1112,7 +1130,6 @@ describe('Polygon ZK-EVM', () => {

it('should test the pending state properly', async () => {
const l2txData = '0x123456';
const maticAmount = await polygonZkEVMContract.getCurrentBatchFee();
const currentTimestamp = (await ethers.provider.getBlock()).timestamp;

const batchesForSequence = 5;
Expand All @@ -1130,8 +1147,8 @@ describe('Polygon ZK-EVM', () => {
maticTokenContract.connect(trustedSequencer).approve(polygonZkEVMContract.address, maticTokenInitialBalance),
).to.emit(maticTokenContract, 'Approval');

// Make 10 sequences of 5 batches, with 1 minut timestamp difference
for (let i = 0; i < 10; i++) {
// Make 20 sequences of 5 batches, with 1 minut timestamp difference
for (let i = 0; i < 20; i++) {
await expect(polygonZkEVMContract.connect(trustedSequencer).sequenceBatches(sequencesArray))
.to.emit(polygonZkEVMContract, 'SequenceBatches');
}
Expand All @@ -1147,9 +1164,6 @@ describe('Polygon ZK-EVM', () => {
];
const proofC = ['0', '0'];

const sequencedBatchData = await polygonZkEVMContract.sequencedBatches(batchesForSequence);
const { sequencedTimestamp } = sequencedBatchData;

let currentPendingState = 0;
let currentNumBatch = 0;
let newBatch = currentNumBatch + batchesForSequence;
Expand Down Expand Up @@ -1243,6 +1257,11 @@ describe('Polygon ZK-EVM', () => {
expect(currentPendingState).to.be.equal(await polygonZkEVMContract.lastPendingState());
expect(0).to.be.equal(await polygonZkEVMContract.lastPendingStateConsolidated());

// Check consolidated state
let currentVerifiedBatch = newBatch;
expect(currentVerifiedBatch).to.be.equal(await polygonZkEVMContract.lastVerifiedBatch());
expect(newStateRoot).to.be.equal(await polygonZkEVMContract.batchNumToStateRoot(currentVerifiedBatch));

await expect(
polygonZkEVMContract.connect(trustedAggregator).trustedVerifyBatches(
1,
Expand Down Expand Up @@ -1400,8 +1419,10 @@ describe('Polygon ZK-EVM', () => {
const firstPendingState = await polygonZkEVMContract.pendingStateTransitions(1);
await ethers.provider.send('evm_setNextBlockTimestamp', [firstPendingState.timestamp.toNumber() + pendingStateTimeoutDefault]);

let currentPendingConsolidated = 0;
currentNumBatch = newBatch;
newBatch += batchesForSequence;

await expect(
polygonZkEVMContract.connect(aggregator1).verifyBatches(
currentPendingState,
Expand All @@ -1416,12 +1437,12 @@ describe('Polygon ZK-EVM', () => {
).to.emit(polygonZkEVMContract, 'VerifyBatches')
.withArgs(newBatch, newStateRoot, aggregator1.address)
.to.emit(polygonZkEVMContract, 'ConsolidatePendingState')
.withArgs(firstPendingState.lastVerifiedBatch, newStateRoot, 1);
.withArgs(firstPendingState.lastVerifiedBatch, newStateRoot, ++currentPendingConsolidated);

verifyTimestamp = (await ethers.provider.getBlock()).timestamp;
currentPendingState++;
expect(currentPendingState).to.be.equal(await polygonZkEVMContract.lastPendingState());
expect(1).to.be.equal(await polygonZkEVMContract.lastPendingStateConsolidated());
expect(currentPendingConsolidated).to.be.equal(await polygonZkEVMContract.lastPendingStateConsolidated());

currentPendingStateData = await polygonZkEVMContract.pendingStateTransitions(currentPendingState);
expect(verifyTimestamp).to.be.equal(currentPendingStateData.timestamp);
Expand All @@ -1430,10 +1451,77 @@ describe('Polygon ZK-EVM', () => {
expect(newStateRoot).to.be.equal(currentPendingStateData.stateRoot);

// Check state consolidated
currentVerifiedBatch += batchesForSequence;
expect(currentVerifiedBatch).to.be.equal(await polygonZkEVMContract.lastVerifiedBatch());
expect(newStateRoot).to.be.equal(await polygonZkEVMContract.batchNumToStateRoot(currentVerifiedBatch));


// Consolidate using verifyBatches
const firstPendingState = await polygonZkEVMContract.pendingStateTransitions(1);
await ethers.provider.send('evm_setNextBlockTimestamp', [firstPendingState.timestamp.toNumber() + pendingStateTimeoutDefault]);
// Consolidate using sendBatches
const secondPendingState = await polygonZkEVMContract.pendingStateTransitions(2);
await ethers.provider.send('evm_setNextBlockTimestamp', [secondPendingState.timestamp.toNumber() + pendingStateTimeoutDefault]);

await expect(polygonZkEVMContract.connect(trustedSequencer).sequenceBatches(sequencesArray))
.to.emit(polygonZkEVMContract, 'SequenceBatches')
.to.emit(polygonZkEVMContract, 'ConsolidatePendingState')
.withArgs(secondPendingState.lastVerifiedBatch, newStateRoot, ++currentPendingConsolidated);

expect(currentPendingState).to.be.equal(await polygonZkEVMContract.lastPendingState());
expect(currentPendingConsolidated).to.be.equal(await polygonZkEVMContract.lastPendingStateConsolidated());

// Check state consolidated
currentVerifiedBatch += batchesForSequence;
expect(currentVerifiedBatch).to.be.equal(await polygonZkEVMContract.lastVerifiedBatch());
expect(newStateRoot).to.be.equal(await polygonZkEVMContract.batchNumToStateRoot(currentVerifiedBatch));

// Put a lot of pending states and check that half of them are consoldiated
for (let i = 0; i < 8; i++) {
currentNumBatch = newBatch;
newBatch += batchesForSequence;
await expect(
polygonZkEVMContract.connect(aggregator1).verifyBatches(
currentPendingState,
currentNumBatch,
newBatch,
newLocalExitRoot,
newStateRoot,
proofA,
proofB,
proofC,
),
).to.emit(polygonZkEVMContract, 'VerifyBatches')
.withArgs(newBatch, newStateRoot, aggregator1.address);

currentPendingState++;
}

expect(currentPendingState).to.be.equal(await polygonZkEVMContract.lastPendingState());

currentPendingConsolidated = await polygonZkEVMContract.lastPendingStateConsolidated();
const lastPendingState = await polygonZkEVMContract.pendingStateTransitions(currentPendingState);
await ethers.provider.send('evm_setNextBlockTimestamp', [lastPendingState.timestamp.toNumber() + pendingStateTimeoutDefault]);

// call verify batches and check that half of them are consolidated
expect(currentPendingState).to.be.equal(await polygonZkEVMContract.lastPendingState());
expect(currentPendingConsolidated).to.be.equal(await polygonZkEVMContract.lastPendingStateConsolidated());

const nextPendingConsolidated = Number(currentPendingConsolidated) + 1;
const nextConsolidatedStateNum = nextPendingConsolidated + Number(Math.floor((currentPendingState - nextPendingConsolidated) / 2));
const nextConsolidatedState = await polygonZkEVMContract.pendingStateTransitions(nextConsolidatedStateNum);

await expect(polygonZkEVMContract.connect(trustedSequencer).sequenceBatches(sequencesArray))
.to.emit(polygonZkEVMContract, 'SequenceBatches')
.to.emit(polygonZkEVMContract, 'ConsolidatePendingState')
.withArgs(nextConsolidatedState.lastVerifiedBatch, newStateRoot, nextConsolidatedStateNum);

// Put pendingState to 0 and check that the pending state is clear after verifyBatches

await expect(
polygonZkEVMContract.connect(admin).setPendingStateTimeout(0),
).to.emit(polygonZkEVMContract, 'SetPendingStateTimeout').withArgs(0);

it('Test batch fees properly', async () => {
});

it('Test overridePendingState properly', async () => {
});
});
});

0 comments on commit 1360df2

Please sign in to comment.