Skip to content

Commit

Permalink
add gas report
Browse files Browse the repository at this point in the history
  • Loading branch information
invocamanman committed Jan 19, 2023
1 parent 1360df2 commit a5f7c1a
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 21 deletions.
6 changes: 3 additions & 3 deletions deployment/genesis.json

Large diffs are not rendered by default.

125 changes: 125 additions & 0 deletions gas_report.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ module.exports = {
}
},
gasReporter: {
currency: 'USD',
coinmarketcap: process.env.COINMARKETCAP_KEY,
enabled: !!process.env.REPORT_GAS,
outputFile: process.env.REPORT_GAS_FILE ? "./gas_report.md" : null,
noColors: process.env.REPORT_GAS_FILE ? true : false
},
etherscan: {
apiKey: `${process.env.ETHERSCAN_API_KEY}`,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"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",
"coverage":"npx hardhat coverage",
"gas:report":"REPORT_GAS=true npx hardhat test"
"gas:report":"REPORT_GAS=true npx hardhat test",
"gas:report:file":"REPORT_GAS=true REPORT_GAS_FILE=true npx hardhat test"
}
}
25 changes: 15 additions & 10 deletions test/contracts/globalExitRootManagerL2.test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
const { expect } = require('chai');
const { ethers, upgrades } = require('hardhat');
const { ethers } = require('hardhat');

const zero32bytes = '0x0000000000000000000000000000000000000000000000000000000000000000';

describe('Global Exit Root L2', () => {
let PolygonZkEVMBridge;
let polygonZkEVMGlobalExitRoot;
const PolygonZkEVMAddress = ethers.constants.AddressZero;
let deployer;

beforeEach('Deploy contracts', async () => {
const networkIDRollup = 1;

// load signers
const deployer = (await ethers.getSigners())[0];
[deployer, PolygonZkEVMBridge] = await ethers.getSigners();

// deploy PolygonZkEVMBridge
const polygonZkEVMBridgeFactory = await ethers.getContractFactory('PolygonZkEVMBridge');
PolygonZkEVMBridge = await upgrades.deployProxy(polygonZkEVMBridgeFactory, [], { initializer: false });
// deploy global exit root manager
const PolygonZkEVMGlobalExitRootFactory = await ethers.getContractFactory('PolygonZkEVMGlobalExitRootL2Mock', deployer);
polygonZkEVMGlobalExitRoot = await PolygonZkEVMGlobalExitRootFactory.deploy(PolygonZkEVMBridge.address);

await PolygonZkEVMBridge.initialize(networkIDRollup, polygonZkEVMGlobalExitRoot.address, PolygonZkEVMAddress);
});

it('should check the constructor parameters', async () => {
expect(await polygonZkEVMGlobalExitRoot.bridgeAddress()).to.be.equal(PolygonZkEVMBridge.address);
expect(await polygonZkEVMGlobalExitRoot.lastRollupExitRoot()).to.be.equal(zero32bytes);
});

it('should update root and check global exit root', async () => {
const newRootRollup = ethers.utils.hexlify(ethers.utils.randomBytes(32));

await expect(polygonZkEVMGlobalExitRoot.updateExitRoot(newRootRollup))
.to.be.revertedWith('PolygonZkEVMGlobalExitRootL2::updateExitRoot: Only PolygonZkEVMBridge');

// Update root from the rollup
await polygonZkEVMGlobalExitRoot.connect(PolygonZkEVMBridge).updateExitRoot(newRootRollup);

expect(await polygonZkEVMGlobalExitRoot.lastRollupExitRoot()).to.be.equal(newRootRollup);
});

it('should update root and check the storage position matches', async () => {
// Check global exit root
const newRoot = ethers.utils.hexlify(ethers.utils.randomBytes(32));
Expand Down
199 changes: 194 additions & 5 deletions test/contracts/proofOfEfficiency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('Polygon ZK-EVM', () => {
expect(await polygonZkEVMContract.rollupVerifier()).to.be.equal(verifierContract.address);
expect(await polygonZkEVMContract.bridgeAddress()).to.be.equal(polygonZkEVMBridgeContract.address);

expect(await polygonZkEVMContract.owner()).to.be.equal(deployer.address);
expect(await polygonZkEVMContract.admin()).to.be.equal(admin.address);
expect(await polygonZkEVMContract.chainID()).to.be.equal(chainID);
expect(await polygonZkEVMContract.trustedSequencer()).to.be.equal(trustedSequencer.address);
Expand Down Expand Up @@ -1513,15 +1514,203 @@ describe('Polygon ZK-EVM', () => {
.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 () => {
});
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 = 0;
expect(currentPendingState).to.be.equal(await polygonZkEVMContract.lastPendingState());
expect(0).to.be.equal(await polygonZkEVMContract.lastPendingStateConsolidated());

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

it('Activate emergency state due halt timeout', async () => {
const l2txData = '0x123456';
const maticAmount = await polygonZkEVMContract.getCurrentBatchFee();
const currentTimestamp = (await ethers.provider.getBlock()).timestamp;

const sequence = {
transactions: l2txData,
globalExitRoot: ethers.constants.HashZero,
timestamp: ethers.BigNumber.from(currentTimestamp),
minForcedTimestamp: 0,
};

// Approve tokens
await expect(
maticTokenContract.connect(trustedSequencer).approve(polygonZkEVMContract.address, maticAmount),
).to.emit(maticTokenContract, 'Approval');

// Sequence batch
const lastBatchSequenced = 1;
await expect(polygonZkEVMContract.connect(trustedSequencer).sequenceBatches([sequence]))
.to.emit(polygonZkEVMContract, 'SequenceBatches')
.withArgs(lastBatchSequenced);

const sequencedTimestmap = Number((await polygonZkEVMContract.sequencedBatches(1)).sequencedTimestamp);
const haltTimeout = Number(await polygonZkEVMContract.HALT_AGGREGATION_TIMEOUT());

// Try to activate the emergency state

// Check batch is not sequenced
await expect(polygonZkEVMContract.connect(aggregator1).activateEmergencyState(2))
.to.be.revertedWith('PolygonZkEVM::activateEmergencyState: Batch not sequenced or not end of sequence');

// Check batch is already verified
await polygonZkEVMContract.setVerifiedBatch(1);
await expect(polygonZkEVMContract.connect(aggregator1).activateEmergencyState(1))
.to.be.revertedWith('PolygonZkEVM::activateEmergencyState: Batch already verified');
await polygonZkEVMContract.setVerifiedBatch(0);

// check timeout is not expired
await expect(polygonZkEVMContract.connect(aggregator1).activateEmergencyState(1))
.to.be.revertedWith('PolygonZkEVM::activateEmergencyState: Aggregation halt timeout is not expired');

await ethers.provider.send('evm_setNextBlockTimestamp', [sequencedTimestmap + haltTimeout]);

// Succesfully acitvate emergency state
await expect(polygonZkEVMContract.connect(aggregator1).activateEmergencyState(1))
.to.emit(polygonZkEVMContract, 'EmergencyStateActivated');
});

it('Test overridePendingState properly', async () => {
const l2txData = '0x123456';
const currentTimestamp = (await ethers.provider.getBlock()).timestamp;

const batchesForSequence = 5;
const sequence = {
transactions: l2txData,
globalExitRoot: ethers.constants.HashZero,
timestamp: currentTimestamp,
minForcedTimestamp: 0,
};
const sequencesArray = Array(batchesForSequence).fill(sequence);
// Array(5).fill("girl", 0);

// Approve lots of tokens
await expect(
maticTokenContract.connect(trustedSequencer).approve(polygonZkEVMContract.address, maticTokenInitialBalance),
).to.emit(maticTokenContract, 'Approval');

// 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');
}
await ethers.provider.send('evm_increaseTime', [60]);

// Forge first sequence with verifyBAtches
const newLocalExitRoot = '0x0000000000000000000000000000000000000000000000000000000000000001';
const newStateRoot = '0x0000000000000000000000000000000000000000000000000000000000000002';
const proofA = ['0', '0'];
const proofB = [
['0', '0'],
['0', '0'],
];
const proofC = ['0', '0'];

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

// Verify batch 2 batches
await expect(
polygonZkEVMContract.connect(aggregator1).verifyBatches(
currentPendingState,
currentNumBatch,
newBatch,
newLocalExitRoot,
newStateRoot,
proofA,
proofB,
proofC,
),
).to.emit(polygonZkEVMContract, 'VerifyBatches')
.withArgs(newBatch, newStateRoot, aggregator1.address);

// verify second sequence
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++;
const finalPendingState = 2;

await expect(
polygonZkEVMContract.connect(aggregator1).overridePendingState(
currentPendingState,
finalPendingState,
currentNumBatch,
newBatch,
newLocalExitRoot,
newStateRoot,
proofA,
proofB,
proofC,
),
).to.be.revertedWith('PolygonZkEVM::onlyTrustedAggregator: Only trusted aggregator');

await expect(
polygonZkEVMContract.connect(aggregator1).overridePendingState(
currentPendingState,
finalPendingState,
currentNumBatch,
newBatch,
newLocalExitRoot,
newStateRoot,
proofA,
proofB,
proofC,
),
).to.be.revertedWith('PolygonZkEVM::_proveDistinctPendingState: finalPendingStateNum incorrect');

await expect(
polygonZkEVMContract.connect(aggregator1).overridePendingState(
currentPendingState,
finalPendingState,
currentNumBatch,
newBatch,
newLocalExitRoot,
newStateRoot,
proofA,
proofB,
proofC,
),
).to.be.revertedWith('PolygonZkEVM::_proveDistinctPendingState: finalPendingStateNum incorrect');
});

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

0 comments on commit a5f7c1a

Please sign in to comment.