Skip to content

Commit

Permalink
Merge pull request #303 from 0xPolygonHermez/feature/tests-pp
Browse files Browse the repository at this point in the history
Feature/tests pp
  • Loading branch information
krlosMata committed Jul 26, 2024
2 parents 1e3537b + a7e7955 commit 467ff2e
Show file tree
Hide file tree
Showing 4 changed files with 1,039 additions and 15 deletions.
10 changes: 9 additions & 1 deletion contracts/mocks/VerifierRollupHelperMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
pragma solidity 0.8.20;

import "../interfaces/IVerifierRollup.sol";
import "../v2/interfaces/ISP1Verifier.sol";

contract VerifierRollupHelperMock is IVerifierRollup {
contract VerifierRollupHelperMock is IVerifierRollup, ISP1Verifier {
function verifyProof(
bytes32[24] calldata proof,
uint256[1] memory pubSignals
) public pure override returns (bool) {
return true;
}

// SP1 interface
function verifyProof(
bytes32 programVKey,
bytes calldata publicValues,
bytes calldata proofBytes
) public pure {}
}
16 changes: 2 additions & 14 deletions contracts/v2/PolygonRollupManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ contract PolygonRollupManager is
} else {
rollup.batchNumToStateRoot[0] = initRoot;
}

// rollup type is 0, since it does not follow any rollup type
emit AddExistingRollup(
rollupID,
Expand Down Expand Up @@ -592,13 +593,6 @@ contract PolygonRollupManager is
revert UpdateToOldRollupTypeID();
}

if (
rollup.rollupVerifierType !=
rollupTypeMap[newRollupTypeID].rollupVerifierType
) {
revert UpdateNotCompatible();
}

_updateRollup(rollupContract, newRollupTypeID, new bytes(0));
}

Expand Down Expand Up @@ -654,13 +648,7 @@ contract PolygonRollupManager is

// Check rollup types
if (rollup.rollupVerifierType != newRollupType.rollupVerifierType) {
// Currently the transition from pessimistic to state transition is not allowed
if (rollup.rollupVerifierType == VerifierType.Pessimistic) {
revert RollupTypeObsolete();
}

// Update rollup verifier type
rollup.rollupVerifierType = newRollupType.rollupVerifierType;
revert UpdateNotCompatible();
}

// Update rollup parameters
Expand Down
163 changes: 163 additions & 0 deletions test/contractsv2/PolygonPessimisticConsensus.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* eslint-disable no-plusplus, no-await-in-loop */
import {expect} from "chai";
import {ethers, upgrades} from "hardhat";
import {Address, PolygonPessimisticConsensus} from "../../typechain-types";

describe("PolygonPessimisticConsensus", () => {
let deployer: any;
let trustedSequencer: any;
let admin: any;

let PolygonPPConsensusContract: PolygonPessimisticConsensus;

const gerManagerAddress = "0xA00000000000000000000000000000000000000A" as unknown as Address;
const polTokenAddress = "0xB00000000000000000000000000000000000000B" as unknown as Address;
const rollupManagerAddress = "0xC00000000000000000000000000000000000000C" as unknown as Address;
const bridgeAddress = "0xD00000000000000000000000000000000000000D" as unknown as Address;

const urlSequencer = "http://zkevm-json-rpc:8123";
const networkName = "zkevm";
const networkID = 1;

// Native token will be ether
const gasTokenAddress = ethers.ZeroAddress;

beforeEach("Deploy contract", async () => {
upgrades.silenceWarnings();

// load signers
[deployer, trustedSequencer, admin] = await ethers.getSigners();

// deploy consensus
// create polygonPessimisticConsensus implementation
const ppConsensusFactory = await ethers.getContractFactory("PolygonPessimisticConsensus");
PolygonPPConsensusContract = await ppConsensusFactory.deploy(
gerManagerAddress,
polTokenAddress,
bridgeAddress,
rollupManagerAddress
);
await PolygonPPConsensusContract.waitForDeployment();
});

it("should check the initalized parameters", async () => {
// initialize zkEVM using non admin address
await expect(
PolygonPPConsensusContract.initialize(
admin.address,
trustedSequencer.address,
networkID,
gasTokenAddress,
urlSequencer,
networkName
)
).to.be.revertedWithCustomError(PolygonPPConsensusContract, "OnlyRollupManager");

// initialize using rollup manager
await ethers.provider.send("hardhat_impersonateAccount", [rollupManagerAddress]);
const rolllupManagerSigner = await ethers.getSigner(rollupManagerAddress as any);
await PolygonPPConsensusContract.connect(rolllupManagerSigner).initialize(
admin.address,
trustedSequencer.address,
networkID,
gasTokenAddress,
urlSequencer,
networkName,
{gasPrice: 0}
);

expect(await PolygonPPConsensusContract.admin()).to.be.equal(admin.address);
expect(await PolygonPPConsensusContract.trustedSequencer()).to.be.equal(trustedSequencer.address);
expect(await PolygonPPConsensusContract.trustedSequencerURL()).to.be.equal(urlSequencer);
expect(await PolygonPPConsensusContract.networkName()).to.be.equal(networkName);
expect(await PolygonPPConsensusContract.gasTokenAddress()).to.be.equal(gasTokenAddress);

// initialize again
await expect(
PolygonPPConsensusContract.connect(rolllupManagerSigner).initialize(
admin.address,
trustedSequencer.address,
networkID,
gasTokenAddress,
urlSequencer,
networkName,
{gasPrice: 0}
)
).to.be.revertedWith("Initializable: contract is already initialized");
});

it("should check admin functions", async () => {
// initialize using rollup manager
await ethers.provider.send("hardhat_impersonateAccount", [rollupManagerAddress]);
const rolllupManagerSigner = await ethers.getSigner(rollupManagerAddress as any);
await PolygonPPConsensusContract.connect(rolllupManagerSigner).initialize(
admin.address,
trustedSequencer.address,
networkID,
gasTokenAddress,
urlSequencer,
networkName,
{gasPrice: 0}
);

// setTrustedSequencer
await expect(PolygonPPConsensusContract.setTrustedSequencer(deployer.address)).to.be.revertedWithCustomError(
PolygonPPConsensusContract,
"OnlyAdmin"
);

await expect(PolygonPPConsensusContract.connect(admin).setTrustedSequencer(deployer.address))
.to.emit(PolygonPPConsensusContract, "SetTrustedSequencer")
.withArgs(deployer.address);

// setTrustedSequencerURL
await expect(PolygonPPConsensusContract.setTrustedSequencerURL("0x1253")).to.be.revertedWithCustomError(
PolygonPPConsensusContract,
"OnlyAdmin"
);
await expect(PolygonPPConsensusContract.connect(admin).setTrustedSequencerURL("0x1253"))
.to.emit(PolygonPPConsensusContract, "SetTrustedSequencerURL")
.withArgs("0x1253");

// transferAdminRole & acceptAdminRole
await expect(PolygonPPConsensusContract.connect(admin).transferAdminRole(deployer.address))
.to.emit(PolygonPPConsensusContract, "TransferAdminRole")
.withArgs(deployer.address);

await expect(PolygonPPConsensusContract.connect(admin).acceptAdminRole()).to.be.revertedWithCustomError(
PolygonPPConsensusContract,
"OnlyPendingAdmin"
);

await expect(PolygonPPConsensusContract.connect(deployer).acceptAdminRole())
.to.emit(PolygonPPConsensusContract, "AcceptAdminRole")
.withArgs(deployer.address);
});

it("should check getConsensusHash", async () => {
// initialize using rollup manager
await ethers.provider.send("hardhat_impersonateAccount", [rollupManagerAddress]);
const rolllupManagerSigner = await ethers.getSigner(rollupManagerAddress as any);
await PolygonPPConsensusContract.connect(rolllupManagerSigner).initialize(
admin.address,
trustedSequencer.address,
networkID,
gasTokenAddress,
urlSequencer,
networkName,
{gasPrice: 0}
);

// pessimistic constant CONSENSUS_TYPE = 0;
const CONSENSUS_TYPE = 0;
const consensusHashJs = ethers.solidityPackedKeccak256(
["uint32", "address"],
[CONSENSUS_TYPE, trustedSequencer.address]
);

// getConsensusHash
const resGetConsensusHash = await PolygonPPConsensusContract.getConsensusHash();

expect(resGetConsensusHash).to.be.equal(consensusHashJs);
});
});
Loading

0 comments on commit 467ff2e

Please sign in to comment.