Skip to content

Commit

Permalink
test: Adjustments for custom errors
Browse files Browse the repository at this point in the history
  • Loading branch information
PacificYield committed Nov 12, 2024
1 parent cebc56a commit af473e1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
59 changes: 41 additions & 18 deletions test/governance/CompoundTimelock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ describe("CompoundTimelock", function () {
});

it("non-timelock account could not call setPendingAdmin", async function () {
await expect(this.timelock.setPendingAdmin(this.signers.bob)).to.be.revertedWith(
"Timelock::setPendingAdmin: Call must come from Timelock.",
await expect(this.timelock.setPendingAdmin(this.signers.bob)).to.be.revertedWithCustomError(
this.timelock,
"SenderIsNotTimelock",
);
});

it("non-timelock account could not call setDelay", async function () {
await expect(this.timelock.setDelay(60 * 60 * 24 * 3)).to.be.revertedWith(
"Timelock::setDelay: Call must come from Timelock.",
await expect(this.timelock.setDelay(60 * 60 * 24 * 3)).to.be.revertedWithCustomError(
this.timelock,
"SenderIsNotTimelock",
);
});

Expand All @@ -47,10 +49,10 @@ describe("CompoundTimelock", function () {
await ethers.provider.send("evm_increaseTime", ["0x2a33c"]);
await expect(
this.timelock.executeTransaction(timeLockAdd, 0, "setDelay(uint256)", callData1, expiry),
).to.be.revertedWith("Timelock::executeTransaction: Transaction execution reverted.");
).to.be.revertedWithCustomError(this.timelock, "ExecutionReverted");
await expect(
this.timelock.executeTransaction(timeLockAdd, 0, "setDelay(uint256)", callData2, expiry),
).to.be.revertedWith("Timelock::executeTransaction: Transaction execution reverted.");
).to.be.revertedWithCustomError(this.timelock, "ExecutionReverted");
await this.timelock.executeTransaction(timeLockAdd, 0, "setDelay(uint256)", callData3, expiry);
expect(await this.timelock.delay()).to.equal(60 * 60 * 24 * 20);
}
Expand All @@ -63,22 +65,22 @@ describe("CompoundTimelock", function () {
const timeLockAdd = await this.timelock.getAddress();
const callData = ethers.AbiCoder.defaultAbiCoder().encode(["uint256"], [60 * 60 * 24 * 20]); // OK

const tx = await this.timelock.queueTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry);
let tx = await this.timelock.queueTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry);
await tx.wait();

await expect(
this.timelock.connect(this.signers.bob).cancelTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry),
).to.throw;
).to.be.revertedWithCustomError(this.timelock, "SenderIsNotAdmin");

const tx2 = await this.timelock.cancelTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry);
await tx2.wait();
tx = await this.timelock.cancelTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry);
await tx.wait();

if (network.name === "hardhat") {
// hardhat cheatcodes are available only in mocked mode
await ethers.provider.send("evm_increaseTime", ["0x2a33c"]);
await expect(
this.timelock.executeTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry),
).to.be.revertedWith("Timelock::executeTransaction: Transaction hasn't been queued.");
).to.be.revertedWithCustomError(this.timelock, "TransactionNotQueued");
}
});

Expand All @@ -90,14 +92,21 @@ describe("CompoundTimelock", function () {
const timeLockAdd = await this.timelock.getAddress();
const callData = ethers.AbiCoder.defaultAbiCoder().encode(["uint256"], [60 * 60 * 24 * 20]); // OK

// Bob is not the admin.
await expect(
this.timelock.connect(this.signers.bob).queueTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry),
).to.throw;

await expect(this.timelock.queueTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiryTooShort)).to
.throw;
).to.be.revertedWithCustomError(this.timelock, "SenderIsNotTimelock");

const tx = await this.timelock.queueTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry);
// The expiry is too short.
await expect(
this.timelock
.connect(this.signers.alice)
.queueTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiryTooShort),
).to.be.revertedWithCustomError(this.timelock, "TransactionTooEarlyForQueuing");

const tx = await this.timelock
.connect(this.signers.alice)
.queueTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry);
await tx.wait();
});

Expand All @@ -117,13 +126,13 @@ describe("CompoundTimelock", function () {
this.timelock
.connect(this.signers.bob)
.executeTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry),
).to.be.revertedWith("Timelock::executeTransaction: Call must come from admin.");
).to.be.revertedWithCustomError(this.timelock, "SenderIsNotAdmin");

const idSnapshot = await ethers.provider.send("evm_snapshot");
await ethers.provider.send("evm_increaseTime", ["0xffffff"]);
await expect(
this.timelock.executeTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry),
).to.be.revertedWith("Timelock::executeTransaction: Transaction is stale.");
).to.be.revertedWithCustomError(this.timelock, "TransactionTooLateForExecution");

await ethers.provider.send("evm_revert", [idSnapshot]); // roll back time to previous snapshot, before the grace period
const tx2 = await this.timelock.executeTransaction(timeLockAdd, 0, "setDelay(uint256)", callData, expiry);
Expand Down Expand Up @@ -151,4 +160,18 @@ describe("CompoundTimelock", function () {
expect(await this.timelock.delay()).to.equal(60 * 60 * 24 * 20);
}
});

it("could not deploy timelock contract if delay is below 2 days or above 31 days", async function () {
const timelockFactory = await ethers.getContractFactory("CompoundTimelock");

if (network.name === "hardhat") {
await expect(
timelockFactory.connect(this.signers.alice).deploy(this.signers.alice.address, 60 * 60 * 24 * 1),
).to.be.revertedWithCustomError(this.timelock, "DelayBelowMinimumDelay");

await expect(
timelockFactory.connect(this.signers.alice).deploy(this.signers.alice.address, 60 * 60 * 24 * 31),
).to.be.revertedWithCustomError(this.timelock, "DelayAboveMaximumDelay");
}
});
});
29 changes: 8 additions & 21 deletions test/governance/GovernorAlphaZama.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,19 +418,6 @@ describe("GovernorAlphaZama", function () {
expect(proposalInfo.state).to.equal(6);
});

it("could not deploy timelock contract if delay is below 2 days or above 31 days", async function () {
const timelockFactory = await ethers.getContractFactory("CompoundTimelock");

if (network.name === "hardhat") {
await expect(
timelockFactory.connect(this.signers.alice).deploy(this.signers.alice.address, 60 * 60 * 24 * 1),
).to.be.revertedWith("Timelock::constructor: Delay must exceed minimum delay."); // 1 day < 2 days
await expect(
timelockFactory.connect(this.signers.alice).deploy(this.signers.alice.address, 60 * 60 * 24 * 31),
).to.be.revertedWith("Timelock::setDelay: Delay must not exceed maximum delay."); // 31 days > 30 days
}
});

it("only owner could queue setTimelockPendingAdmin then execute it, and then acceptTimelockAdmin", async function () {
const latestBlockNumber = await ethers.provider.getBlockNumber();
const block = await ethers.provider.getBlock(latestBlockNumber);
Expand All @@ -441,9 +428,9 @@ describe("GovernorAlphaZama", function () {

if (network.name === "hardhat") {
// hardhat cheatcodes are available only in mocked mode
await expect(this.governor.executeSetTimelockPendingAdmin(this.signers.bob, expiry)).to.be.revertedWith(
"Timelock::executeTransaction: Transaction hasn't surpassed time lock.",
);
await expect(
this.governor.executeSetTimelockPendingAdmin(this.signers.bob, expiry),
).to.be.revertedWithCustomError(this.timelock, "TransactionTooEarlyForExecution");

await expect(
this.governor.connect(this.signers.carol).queueSetTimelockPendingAdmin(this.signers.bob, expiry),
Expand All @@ -458,9 +445,7 @@ describe("GovernorAlphaZama", function () {
const tx3 = await this.governor.executeSetTimelockPendingAdmin(this.signers.bob, expiry);
await tx3.wait();

await expect(this.timelock.acceptAdmin()).to.be.revertedWith(
"Timelock::acceptAdmin: Call must come from pendingAdmin.",
);
await expect(this.timelock.acceptAdmin()).to.be.revertedWithCustomError(this.timelock, "SenderIsNotPendingAdmin");

const tx4 = await this.timelock.connect(this.signers.bob).acceptAdmin();
await tx4.wait();
Expand All @@ -479,6 +464,7 @@ describe("GovernorAlphaZama", function () {
const tx6 = await this.timelock
.connect(this.signers.bob)
.executeTransaction(timeLockAdd, 0, "setPendingAdmin(address)", callData, expiry2);

await tx6.wait();

await expect(this.governor.connect(this.signers.bob).acceptTimelockAdmin()).to.be.revertedWithCustomError(
Expand Down Expand Up @@ -1069,8 +1055,9 @@ describe("GovernorAlphaZama", function () {
await ethers.provider.send("evm_setNextBlockTimestamp", [deadlineExecutionTransaction.toString()]);
await mineNBlocks(1);

await expect(this.governor.execute(proposalId)).to.be.revertedWith(
"Timelock::executeTransaction: Transaction is stale.",
await expect(this.governor.execute(proposalId)).to.be.revertedWithCustomError(
this.timelock,
"TransactionTooLateForExecution",
);

proposalInfo = await this.governor.getProposalInfo(proposalId);
Expand Down

0 comments on commit af473e1

Please sign in to comment.