Skip to content

Commit

Permalink
add test ConsensusEcdsa
Browse files Browse the repository at this point in the history
  • Loading branch information
laisolizq committed Jan 15, 2025
1 parent 1196834 commit 190fedf
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 2 deletions.
4 changes: 2 additions & 2 deletions contracts/v2/lib/ALBaseConsensus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ abstract contract ALBaseConsensus is IALConsensusBase, Initializable {
//////////////////

/**
* @notice Allow the admin to set a new trusted sequencer
* @param newConsensusVKey Address of the new trusted sequencer
* @notice Allow the admin to set a new consensus vkey
* @param newConsensusVKey bytes32, new consensus verification key
*/
function setConsensusVKey(bytes32 newConsensusVKey) external onlyAdmin {
consensusVKey = newConsensusVKey;
Expand Down
165 changes: 165 additions & 0 deletions test/contractsv2/ConsensusEcdsa.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/* eslint-disable no-plusplus, no-await-in-loop */
import {expect} from "chai";
import {ethers, upgrades} from "hardhat";
import {Address, ConsensusEcdsa} from "../../typechain-types";

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

let consensusEcdsaContract: ConsensusEcdsa;

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 consensusVKey = "0x1122334455667788990011223344556677889900112233445566778899001122";

// 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 consensusEcdsaFactory = await ethers.getContractFactory("ConsensusEcdsa");
consensusEcdsaContract = await upgrades.deployProxy(consensusEcdsaFactory, [], {
initializer: false,
constructorArgs: [rollupManagerAddress],
unsafeAllow: ["constructor", "state-variable-immutable"],
});

await consensusEcdsaContract.waitForDeployment();
});

it("should check the initalized parameters", async () => {

// initialize zkEVM using non admin address
await expect(
consensusEcdsaContract.initialize(
consensusVKey,
admin.address,
trustedSequencer.address,
gasTokenAddress,
urlSequencer,
networkName
)
).to.be.revertedWithCustomError(consensusEcdsaContract, "OnlyRollupManager");

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

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

// initialize again
await expect(
consensusEcdsaContract.connect(rollupManagerSigner).initialize(
consensusVKey,
admin.address,
trustedSequencer.address,
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 rollupManagerSigner = await ethers.getSigner(rollupManagerAddress as any);
await consensusEcdsaContract.connect(rollupManagerSigner).initialize(
consensusVKey,
admin.address,
trustedSequencer.address,
gasTokenAddress,
urlSequencer,
networkName,
{gasPrice: 0}
);

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

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

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

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

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

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

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

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

// getConsensusHash
const resGetConsensusHash = await consensusEcdsaContract.getConsensusHash("0x");

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

0 comments on commit 190fedf

Please sign in to comment.