forked from compound-finance/compound-governance
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cancel logic and tests #36
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9fc6c3e
Add cancel logic and tests
garyghayrat 374175d
Use `_getProposalId` instead of `hashProposal` to prep for upcoming c…
garyghayrat 218f8ff
Rename and cleanup test
garyghayrat d6f6e65
Use helpers to set whitelist accounts and remove voting weight
garyghayrat 82ac1b9
Clean up test
garyghayrat ae944db
Update natspec
garyghayrat cc14c23
docstring nit
wildmolasses File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
pragma solidity 0.8.26; | ||
|
||
import {ProposalTest} from "contracts/test/helpers/ProposalTest.sol"; | ||
import {IGovernor} from "@openzeppelin/contracts/governance/IGovernor.sol"; | ||
import {CompoundGovernor} from "contracts/CompoundGovernor.sol"; | ||
|
||
contract CompoundGovernorCancelTest is ProposalTest { | ||
function _buildAnEmptyProposal() private pure returns (Proposal memory _proposal) { | ||
address[] memory _targets = new address[](1); | ||
uint256[] memory _values = new uint256[](1); | ||
bytes[] memory _calldatas = new bytes[](1); | ||
_proposal = Proposal(_targets, _values, _calldatas, "An Empty Proposal"); | ||
} | ||
|
||
function _getProposalId(Proposal memory _proposal) private view returns (uint256) { | ||
return governor.hashProposal( | ||
_proposal.targets, _proposal.values, _proposal.calldatas, keccak256(bytes(_proposal.description)) | ||
); | ||
} | ||
|
||
function _setWhitelistedProposer(address _proposer) private { | ||
vm.prank(whitelistGuardian); | ||
governor.setWhitelistAccountExpiration(_proposer, block.timestamp + 2_000_000); | ||
} | ||
|
||
function _removeDelegateeVotingWeight(address _proposer) private { | ||
vm.mockCall( | ||
address(token), | ||
abi.encodeWithSelector(token.getPriorVotes.selector, _proposer, block.number - 1), | ||
abi.encode(0) // Return 0 as the new voting weight | ||
); | ||
} | ||
|
||
function testFuzz_ProposerCanCancelItsOwnProposal(uint256 _randomIndex) public { | ||
_randomIndex = bound(_randomIndex, 0, _majorDelegates.length - 1); | ||
address _proposer = _majorDelegates[_randomIndex]; | ||
Proposal memory _proposal = _buildAnEmptyProposal(); | ||
uint256 _proposalId = _getProposalId(_proposal); | ||
_submitPassAndQueueProposal(_proposer, _proposal); | ||
|
||
vm.prank(_proposer); | ||
governor.cancel( | ||
_proposal.targets, _proposal.values, _proposal.calldatas, keccak256(bytes(_proposal.description)) | ||
); | ||
vm.assertEq(uint256(governor.state(_proposalId)), uint256(IGovernor.ProposalState.Canceled)); | ||
} | ||
|
||
function testFuzz_ProposalGuardianCanCancelAnyProposal(uint256 _randomIndex) public { | ||
_randomIndex = bound(_randomIndex, 0, _majorDelegates.length - 1); | ||
address _proposer = _majorDelegates[_randomIndex]; | ||
Proposal memory _proposal = _buildAnEmptyProposal(); | ||
uint256 _proposalId = _getProposalId(_proposal); | ||
_submitPassAndQueueProposal(_proposer, _proposal); | ||
|
||
vm.prank(proposalGuardian.account); | ||
governor.cancel( | ||
_proposal.targets, _proposal.values, _proposal.calldatas, keccak256(bytes(_proposal.description)) | ||
); | ||
vm.assertEq(uint256(governor.state(_proposalId)), uint256(IGovernor.ProposalState.Canceled)); | ||
} | ||
|
||
function testFuzz_AnyoneCanCancelAProposalBelowThreshold(address _caller, uint256 _randomIndex) public { | ||
vm.assume(_caller != PROXY_ADMIN_ADDRESS); | ||
_randomIndex = bound(_randomIndex, 0, _majorDelegates.length - 1); | ||
address _proposer = _majorDelegates[_randomIndex]; | ||
Proposal memory _proposal = _buildAnEmptyProposal(); | ||
uint256 _proposalId = _getProposalId(_proposal); | ||
_submitPassAndQueueProposal(_proposer, _proposal); | ||
_removeDelegateeVotingWeight(_proposer); | ||
|
||
vm.prank(_caller); | ||
governor.cancel( | ||
_proposal.targets, _proposal.values, _proposal.calldatas, keccak256(bytes(_proposal.description)) | ||
); | ||
vm.assertEq(uint256(governor.state(_proposalId)), uint256(IGovernor.ProposalState.Canceled)); | ||
} | ||
|
||
function testFuzz_WhitelistGuardianCanCancelWhitelistedProposalBelowThreshold(uint256 _randomIndex) public { | ||
_randomIndex = bound(_randomIndex, 0, _majorDelegates.length - 1); | ||
address _proposer = _majorDelegates[_randomIndex]; | ||
Proposal memory _proposal = _buildAnEmptyProposal(); | ||
uint256 _proposalId = _getProposalId(_proposal); | ||
_setWhitelistedProposer(_proposer); | ||
_submitPassAndQueueProposal(_proposer, _proposal); | ||
_removeDelegateeVotingWeight(_proposer); | ||
|
||
vm.prank(whitelistGuardian); | ||
governor.cancel( | ||
_proposal.targets, _proposal.values, _proposal.calldatas, keccak256(bytes(_proposal.description)) | ||
); | ||
vm.assertEq(uint256(governor.state(_proposalId)), uint256(IGovernor.ProposalState.Canceled)); | ||
} | ||
|
||
function testFuzz_RevertIf_NonProposerOrGuardianCancelsProposalAboveThreshold(address _caller, uint256 _randomIndex) | ||
public | ||
{ | ||
_randomIndex = bound(_randomIndex, 0, _majorDelegates.length - 1); | ||
address _proposer = _majorDelegates[_randomIndex]; | ||
vm.assume(_caller != proposalGuardian.account && _caller != _proposer && _caller != PROXY_ADMIN_ADDRESS); | ||
Proposal memory _proposal = _buildAnEmptyProposal(); | ||
_submitPassAndQueueProposal(_proposer, _proposal); | ||
|
||
vm.prank(_caller); | ||
vm.expectRevert( | ||
abi.encodeWithSelector( | ||
CompoundGovernor.Unauthorized.selector, bytes32("Proposer above proposalThreshold"), _caller | ||
) | ||
); | ||
governor.cancel( | ||
_proposal.targets, _proposal.values, _proposal.calldatas, keccak256(bytes(_proposal.description)) | ||
); | ||
} | ||
|
||
function testFuzz_RevertIf_NonWhitelistGuardianCancelsWhitelistedProposalBelowThreshold( | ||
address _caller, | ||
uint256 _randomIndex | ||
) public { | ||
_randomIndex = bound(_randomIndex, 0, _majorDelegates.length - 1); | ||
address _proposer = _majorDelegates[_randomIndex]; | ||
vm.assume( | ||
_caller != whitelistGuardian && _caller != _proposer && _caller != proposalGuardian.account | ||
&& _caller != PROXY_ADMIN_ADDRESS | ||
); | ||
|
||
Proposal memory _proposal = _buildAnEmptyProposal(); | ||
_setWhitelistedProposer(_proposer); | ||
_submitPassAndQueueProposal(_proposer, _proposal); | ||
_removeDelegateeVotingWeight(_proposer); | ||
|
||
vm.prank(_caller); | ||
vm.expectRevert( | ||
abi.encodeWithSelector(CompoundGovernor.Unauthorized.selector, bytes32("Not whitelistGuardian"), _caller) | ||
); | ||
governor.cancel( | ||
_proposal.targets, _proposal.values, _proposal.calldatas, keccak256(bytes(_proposal.description)) | ||
); | ||
} | ||
|
||
function testFuzz_RevertIf_NonProposerOrGuardianCancelsWhitelistedProposalAboveThreshold( | ||
address _caller, | ||
uint256 _randomIndex | ||
) public { | ||
_randomIndex = bound(_randomIndex, 0, _majorDelegates.length - 1); | ||
address _proposer = _majorDelegates[_randomIndex]; | ||
vm.assume(_caller != _proposer && _caller != proposalGuardian.account && _caller != PROXY_ADMIN_ADDRESS); | ||
Proposal memory _proposal = _buildAnEmptyProposal(); | ||
vm.prank(whitelistGuardian); | ||
governor.setWhitelistAccountExpiration(_proposer, block.timestamp + 2_000_000); | ||
vm.assertTrue(governor.isWhitelisted(_proposer)); | ||
|
||
_submitPassAndQueueProposal(_proposer, _proposal); | ||
|
||
vm.prank(_caller); | ||
vm.expectRevert( | ||
abi.encodeWithSelector( | ||
CompoundGovernor.Unauthorized.selector, bytes32("Proposer above proposalThreshold"), _caller | ||
) | ||
); | ||
governor.cancel( | ||
_proposal.targets, _proposal.values, _proposal.calldatas, keccak256(bytes(_proposal.description)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: this will also have to be reproduced for the
cancel(uint proposalId)
method in #3.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
created an issue so we don't forget #37