Skip to content

Commit

Permalink
test: more
Browse files Browse the repository at this point in the history
  • Loading branch information
PacificYield committed Dec 2, 2024
1 parent 3f82edc commit 5ac37ce
Showing 1 changed file with 73 additions and 11 deletions.
84 changes: 73 additions & 11 deletions test/governance/GovernorAlphaZama.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ describe("GovernorAlphaZama", function () {

const tx = await this.comp.setGovernor(this.governorAddress);
await tx.wait();

this.VOTING_DELAY = await this.governor.VOTING_DELAY();
this.VOTING_PERIOD = await this.governor.VOTING_PERIOD();
this.TIMELOCK_DELAY = await this.timelock.delay();
});

it("can propose a vote that becomes active if votes match the token threshold", async function () {
Expand All @@ -60,11 +64,25 @@ describe("GovernorAlphaZama", function () {
this.compAddress,
);

const blockNumber = BigInt(await ethers.provider.getBlockNumber());

const tx = await this.governor
.connect(this.signers.bob)
.propose(targets, values, signatures, calldatas, description);

await tx.wait();
await expect(tx)
.to.emit(this.governor, "ProposalCreated")
.withArgs(
BigInt(1),
this.signers.bob.address,
targets,
values,
signatures,
calldatas,
blockNumber + this.VOTING_DELAY + BigInt(1), // @dev We add one since the transaction incremented the block number
blockNumber + this.VOTING_DELAY + this.VOTING_PERIOD + BigInt(1),
description,
);

const proposalId = await this.governor.latestProposalIds(this.signers.bob.address);
let proposalInfo = await this.governor.getProposalInfo(proposalId);
Expand Down Expand Up @@ -168,15 +186,23 @@ describe("GovernorAlphaZama", function () {
tx = await this.governor
.connect(this.signers.bob)
["castVote(uint256,bytes32,bytes)"](proposalId, encryptedVote.handles[0], encryptedVote.inputProof);
await tx.wait();

await expect(tx).to.emit(this.governor, "VoteCast").withArgs(
this.signers.bob,
BigInt(1), // @dev proposalId
);

input = this.instances.carol.createEncryptedInput(this.governorAddress, this.signers.carol.address);
input.addBool(true);
encryptedVote = await input.encrypt();
tx = await this.governor
.connect(this.signers.carol)
["castVote(uint256,bytes32,bytes)"](proposalId, encryptedVote.handles[0], encryptedVote.inputProof);
await tx.wait();

await expect(tx).to.emit(this.governor, "VoteCast").withArgs(
this.signers.carol,
BigInt(1), // @dev proposalId
);

// Bob/Carol can reeencrypt his/her receipt
let [hasVoted, support, votes] = await reencryptVoteReceipt(
Expand Down Expand Up @@ -211,6 +237,7 @@ describe("GovernorAlphaZama", function () {
// REQUEST DECRYPTION
tx = await this.governor.requestVoteDecryption(proposalId);
await tx.wait();

let proposalInfo = await this.governor.getProposalInfo(proposalId);
expect(proposalInfo.forVotes).to.be.eq(parseUnits(String(0), 6));
expect(proposalInfo.againstVotes).to.be.eq(parseUnits(String(0), 6));
Expand All @@ -225,19 +252,38 @@ describe("GovernorAlphaZama", function () {
// 7 ==> Succeeded
expect(proposalInfo.state).to.equal(7);

const block = await ethers.provider.getBlock(await ethers.provider.getBlockNumber());
let nextBlockTimestamp: BigInt;

if (block === null) {
throw "Block is null. Check RPC config.";
} else {
nextBlockTimestamp = BigInt(block.timestamp) + BigInt(30);
}

await ethers.provider.send("evm_setNextBlockTimestamp", [nextBlockTimestamp.toString()]);

// QUEUING
tx = await this.governor.queue(proposalId);
await tx.wait();
await expect(tx)
.to.emit(this.governor, "ProposalQueued")
.withArgs(
BigInt(1), // @dev proposalId,
nextBlockTimestamp + this.TIMELOCK_DELAY,
);

proposalInfo = await this.governor.getProposalInfo(proposalId);
// 8 ==> Queued
expect(proposalInfo.state).to.equal(8);
const eta = proposalInfo.eta;
expect(eta).to.equal(nextBlockTimestamp + this.TIMELOCK_DELAY);

// EXECUTE
await ethers.provider.send("evm_setNextBlockTimestamp", [eta.toString()]);
tx = await this.governor.execute(proposalId);
await tx.wait();
await expect(tx).to.emit(this.governor, "ProposalExecuted").withArgs(
BigInt(1), // @dev proposalId
);

proposalInfo = await this.governor.getProposalInfo(proposalId);
// 10 ==> Executed
Expand Down Expand Up @@ -299,6 +345,7 @@ describe("GovernorAlphaZama", function () {

// REQUEST DECRYPTION
tx = await this.governor.requestVoteDecryption(proposalId);

await tx.wait();
let proposalInfo = await this.governor.getProposalInfo(proposalId);
expect(proposalInfo.forVotes).to.be.eq(parseUnits(String(0), 6));
Expand All @@ -316,14 +363,14 @@ describe("GovernorAlphaZama", function () {
expect(proposalInfo.state).to.equal(6);
});

it("vote is rejected if forVotes < againstVotes", async function () {
it("vote is rejected if forVotes <= againstVotes", async function () {
const targets = [this.signers.bob.address];
const values = ["0"];
const signatures = ["getBalanceOf(address)"];
const calldatas = [ethers.AbiCoder.defaultAbiCoder().encode(["address"], [this.signers.bob.address])];
const description = "description";
const transferAmountFor = parseUnits(String(500_000), 6);
const transferAmountAgainst = parseUnits(String(500_000), 6) + BigInt(1);
const transferAmountAgainst = transferAmountFor;

// Bob and Carol receive 200k tokens and delegate to themselves.
await transferTokensAndDelegate(
Expand Down Expand Up @@ -422,7 +469,13 @@ describe("GovernorAlphaZama", function () {

it("only owner could queue setTimelockPendingAdmin then execute it, and then acceptTimelockAdmin", async function () {
const block = await ethers.provider.getBlock(await ethers.provider.getBlockNumber());
const expiry = block!.timestamp + 60 * 60 * 24 * 2 + 60;
let expiry;

if (block === null) {
throw "Block is null. Check RPC config.";
} else {
expiry = BigInt(block.timestamp) + this.TIMELOCK_DELAY + BigInt(1);
}

const tx = await this.governor.queueSetTimelockPendingAdmin(this.signers.bob, expiry);
await tx.wait();
Expand Down Expand Up @@ -453,8 +506,15 @@ describe("GovernorAlphaZama", function () {

const latestBlockNumber = await ethers.provider.getBlockNumber();
const block = await ethers.provider.getBlock(latestBlockNumber);
const expiry2 = block!.timestamp + 60 * 60 * 24 * 2 + 60;
const timeLockAdd = await this.timelock.getAddress();

let expiry2;
if (block === null) {
throw "Block is null. Check RPC config.";
} else {
expiry2 = BigInt(block.timestamp) + this.TIMELOCK_DELAY + BigInt(1);
}

const timeLockAdd = this.timelockAddress;
const callData = ethers.AbiCoder.defaultAbiCoder().encode(["address"], [this.governorAddress]);
const tx5 = await this.timelock
.connect(this.signers.bob)
Expand Down Expand Up @@ -851,7 +911,9 @@ describe("GovernorAlphaZama", function () {

// @dev Alice is the governor's owner.
tx = await this.governor.connect(this.signers.alice).cancel(proposalId);
await tx.wait();
await expect(tx).to.emit(this.governor, "ProposalCanceled").withArgs(
BigInt(1), // @dev proposalId
);

// 5 ==> Canceled
expect((await this.governor.getProposalInfo(proposalId)).state).to.equal(5);
Expand Down

0 comments on commit 5ac37ce

Please sign in to comment.