diff --git a/contracts/Governor/Comp.sol b/contracts/Governor/Comp.sol index 7ed98bfe..929c0932 100644 --- a/contracts/Governor/Comp.sol +++ b/contracts/Governor/Comp.sol @@ -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); } /** diff --git a/contracts/Governor/GovernorZama.sol b/contracts/Governor/GovernorZama.sol index f1cc51c6..37e0fb46 100644 --- a/contracts/Governor/GovernorZama.sol +++ b/contracts/Governor/GovernorZama.sol @@ -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; @@ -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; diff --git a/package.json b/package.json index 177948ff..95cae3e7 100644 --- a/package.json +++ b/package.json @@ -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)", @@ -80,4 +80,4 @@ "dependencies": { "@openzeppelin/contracts": "^4.9.2" } -} \ No newline at end of file +} diff --git a/test/governor/Comp.ts b/test/governor/Comp.ts index 985535b9..24942e02 100644 --- a/test/governor/Comp.ts +++ b/test/governor/Comp.ts @@ -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); }); diff --git a/test/governor/GovernorZama.ts b/test/governor/GovernorZama.ts index bdc39154..aa068a81 100644 --- a/test/governor/GovernorZama.ts +++ b/test/governor/GovernorZama.ts @@ -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; @@ -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])]; @@ -58,7 +68,7 @@ describe('GovernorZama', function () { ['0'], ['getBalanceOf(address)'], callDatas, - 5, + 4, 'do nothing', { gasLimit: 500000 }, ); @@ -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])]; @@ -94,7 +104,7 @@ describe('GovernorZama', function () { ['0'], ['getBalanceOf(address)'], callDatas, - 5, + 4, 'do nothing', { gasLimit: 500000 }, ); @@ -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);