Skip to content

Commit

Permalink
test() add test for succeed/fails
Browse files Browse the repository at this point in the history
  • Loading branch information
immortal-tofu committed Sep 7, 2023
1 parent cd8420f commit 73e5b1c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 58 deletions.
7 changes: 2 additions & 5 deletions contracts/Governor/Comp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,8 @@ contract Comp is EIP712WithModifier {
* @notice Get the number of tokens
* @return reencrypted The number of tokens
*/
function getTotalSupply(
bytes32 publicKey,
bytes calldata signature
) public view onlySignedPublicKey(publicKey, signature) returns (bytes memory) {
return TFHE.reencrypt(totalSupply, publicKey, 0);
function getTotalSupply() public view returns (uint32) {
return TFHE.decrypt(totalSupply);
}

/**
Expand Down
16 changes: 4 additions & 12 deletions contracts/Governor/GovernorZama.sol
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ contract GovernorZama {
newProposal.targets = targets;
newProposal.values = values;
newProposal.signatures = signatures;
newProposal.forVotes = TFHE.asEuint32(0);
newProposal.againstVotes = TFHE.asEuint32(0);
newProposal.calldatas = calldatas;
newProposal.startBlock = startBlock;
newProposal.endBlock = endBlock;
newProposal.forVotes = euint32.wrap(0);
newProposal.againstVotes = euint32.wrap(0);
newProposal.canceled = false;
newProposal.executed = false;

Expand Down Expand Up @@ -372,18 +372,10 @@ contract GovernorZama {
require(receipt.hasVoted == false, "GovernorAlpha::_castVote: voter already voted");
euint32 votes = comp.getPriorVotes(voter, proposal.startBlock);

// can't do this, otherwise external actors will be able to deduce the votes of individual users.
// if (support) {
// proposal.forVotes = add256(proposal.forVotes, votes);
// } else {
// proposal.againstVotes = add256(proposal.againstVotes, votes);
// }

euint32 ct_one = TFHE.asEuint32(1);
euint32 ctOne = TFHE.asEuint32(1);

proposal.forVotes = TFHE.add(proposal.forVotes, TFHE.mul(votes, support));

proposal.forVotes = TFHE.add(proposal.forVotes, TFHE.mul(votes, TFHE.sub(ct_one, support)));
proposal.againstVotes = TFHE.add(proposal.againstVotes, TFHE.mul(votes, TFHE.sub(ctOne, support)));

receipt.hasVoted = true;
receipt.votes = votes;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"fhevm:start": "docker run -i -p 8545:8545 --rm --name fhevm ghcr.io/zama-ai/evmos-dev-node:v0.1.8",
"fhevm:stop": "docker rm -f fhevm",
"fhevm:restart": "fhevm:stop && fhevm:start",
"fhevm:faucet": "npm run fhevm:faucet:alice && npm run fhevm:faucet:bob && npm run fhevm:faucet:carol && npm run fhevm:faucet:dave",
"fhevm:faucet": "npm run fhevm:faucet:alice && sleep 5 && npm run fhevm:faucet:bob && sleep 5 && npm run fhevm:faucet:carol && sleep 5 && npm run fhevm:faucet:dave",
"fhevm:faucet:alice": "docker exec -i fhevm faucet $(npx hardhat task:getEthereumAddressAlice)",
"fhevm:faucet:bob": "docker exec -i fhevm faucet $(npx hardhat task:getEthereumAddressBob)",
"fhevm:faucet:carol": "docker exec -i fhevm faucet $(npx hardhat task:getEthereumAddressCarol)",
Expand Down Expand Up @@ -80,4 +80,4 @@
"dependencies": {
"@openzeppelin/contracts": "^4.9.2"
}
}
}
4 changes: 1 addition & 3 deletions test/governor/Comp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ describe('Comp', function () {
const balance = this.instances.alice.decrypt(this.contractAddress, encryptedBalance);
expect(balance).to.equal(1000);

const encryptedTotalSupply = await this.comp.getTotalSupply(token.publicKey, token.signature);
// Decrypt the total supply
const totalSupply = this.instances.alice.decrypt(this.contractAddress, encryptedTotalSupply);
const totalSupply = await this.comp.getTotalSupply();
expect(totalSupply).to.equal(1000);
});

Expand Down
80 changes: 44 additions & 36 deletions test/governor/GovernorZama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ import { deployGovernorZamaFixture } from './GovernorZama.fixture';
describe('GovernorZama', function () {
before(async function () {
this.signers = await getSigners();
this.comp = await deployCompFixture();
const instances = await createInstances(await this.comp.getAddress(), ethers, this.signers);
await this.comp.delegate(this.signers.alice.address);
const encryptedAmount = instances.alice.encrypt32(1000);
const transaction = await this.comp.initSupply(encryptedAmount);
transaction.wait();
});

beforeEach(async function () {
// Increase timeout for beforeEach
this.timeout(120000);

this.comp = await deployCompFixture();
const instances = await createInstances(await this.comp.getAddress(), ethers, this.signers);
const encryptedAmount = instances.alice.encrypt32(600);
const supply = await this.comp.initSupply(encryptedAmount);
supply.wait();
const encryptedAmountToTransfer = instances.alice.encrypt32(200);
const transfer1 = await this.comp['transfer(address,bytes)'](this.signers.bob.address, encryptedAmountToTransfer);
const transfer2 = await this.comp['transfer(address,bytes)'](this.signers.carol.address, encryptedAmountToTransfer);
await Promise.all([transfer1.wait(), transfer2.wait()]);

const { governor, timelock } = await deployGovernorZamaFixture(this.comp);
this.contractAddress = await governor.getAddress();
this.governor = governor;
Expand All @@ -31,25 +38,28 @@ describe('GovernorZama', function () {
const transaction2 = await this.governor.__acceptAdmin();

await Promise.all([transaction.wait(), transaction2.wait()]);
await this.comp.delegate(this.signers.alice);
await this.comp.connect(this.signers.bob).delegate(this.signers.bob);
await this.comp.connect(this.signers.carol).delegate(this.signers.carol);
});

// it('should propose a vote', async function () {
// const callDatas = [ethers.AbiCoder.defaultAbiCoder().encode(['address'], [this.signers.alice.address])];
// const tx = await this.governor.propose(
// [this.signers.alice],
// ['0'],
// ['getBalanceOf(address)'],
// callDatas,
// 0,
// 'do nothing',
// { gasLimit: 500000 },
// );
// await tx.wait();
// const proposalId = await this.governor.latestProposalIds(this.signers.alice.address);
// const proposals = await this.governor.proposals(proposalId);
// expect(proposals.id).to.equal(proposalId);
// expect(proposals.proposer).to.equal(this.signers.alice.address);
// }).timeout(120000);
it('should propose a vote', async function () {
const callDatas = [ethers.AbiCoder.defaultAbiCoder().encode(['address'], [this.signers.alice.address])];
const tx = await this.governor.propose(
[this.signers.alice],
['0'],
['getBalanceOf(address)'],
callDatas,
0,
'do nothing',
{ gasLimit: 500000 },
);
await tx.wait();
const proposalId = await this.governor.latestProposalIds(this.signers.alice.address);
const proposals = await this.governor.proposals(proposalId);
expect(proposals.id).to.equal(proposalId);
expect(proposals.proposer).to.equal(this.signers.alice.address);
}).timeout(120000);

it('should vote and return a Succeed', async function () {
const callDatas = [ethers.AbiCoder.defaultAbiCoder().encode(['address'], [this.signers.alice.address])];
Expand All @@ -58,7 +68,7 @@ describe('GovernorZama', function () {
['0'],
['getBalanceOf(address)'],
callDatas,
5,
4,
'do nothing',
{ gasLimit: 500000 },
);
Expand All @@ -69,23 +79,23 @@ describe('GovernorZama', function () {
await waitForBlock(proposals.startBlock + 1n);

// Cast some votes
const encryptedSupportAlice = this.instances.alice.encrypt32(1);
const txVoteAlice = await this.governor['castVote(uint256,bytes)'](proposalId, encryptedSupportAlice);
const encryptedSupportBob = this.instances.bob.encrypt32(1);
const txVoteBob = await this.governor
.connect(this.signers.bob)
['castVote(uint256,bytes)'](proposalId, encryptedSupportBob);
const encryptedSupportCarol = this.instances.carol.encrypt32(0);
['castVote(uint256,bytes)'](proposalId, encryptedSupportBob, { gasLimit: 500000 });

const encryptedSupportCarol = this.instances.carol.encrypt32(1);
const txVoteCarol = await this.governor
.connect(this.signers.carol)
['castVote(uint256,bytes)'](proposalId, encryptedSupportCarol);
await Promise.all([txVoteAlice.wait(), txVoteBob.wait(), txVoteCarol.wait()]);
['castVote(uint256,bytes)'](proposalId, encryptedSupportCarol, { gasLimit: 500000 });

await Promise.all([txVoteBob.wait(), txVoteCarol.wait()]);

await waitForBlock(proposals.endBlock + 1n);

const state = await this.governor.state(proposalId);
expect(state).to.equal(4n);
}).timeout(120000);
}).timeout(300000);

it('should vote and return a Defeated ', async function () {
const callDatas = [ethers.AbiCoder.defaultAbiCoder().encode(['address'], [this.signers.alice.address])];
Expand All @@ -94,7 +104,7 @@ describe('GovernorZama', function () {
['0'],
['getBalanceOf(address)'],
callDatas,
5,
4,
'do nothing',
{ gasLimit: 500000 },
);
Expand All @@ -105,16 +115,14 @@ describe('GovernorZama', function () {
await waitForBlock(proposals.startBlock + 1n);

// Cast some votes
const encryptedSupportAlice = this.instances.alice.encrypt32(0);
const txVoteAlice = await this.governor['castVote(uint256,bytes)'](proposalId, encryptedSupportAlice);
const encryptedSupportBob = this.instances.bob.encrypt32(0);
const txVoteBob = await this.governor
.connect(this.signers.bob)
['castVote(uint256,bytes)'](proposalId, encryptedSupportBob);
['castVote(uint256,bytes)'](proposalId, encryptedSupportBob, { gasLimit: 500000 });
const encryptedSupportCarol = this.instances.bob.encrypt32(1);
const txVoteCarol = await this.governor
.connect(this.signers.bob)
['castVote(uint256,bytes)'](proposalId, encryptedSupportCarol);
['castVote(uint256,bytes)'](proposalId, encryptedSupportCarol, { gasLimit: 500000 });
await Promise.all([txVoteAlice.wait(), txVoteBob.wait(), txVoteCarol.wait()]);

await waitForBlock(proposals.endBlock + 1n);
Expand Down

0 comments on commit 73e5b1c

Please sign in to comment.