-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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 a governor extension that implements a proposal guardian #5303
Open
Amxx
wants to merge
9
commits into
OpenZeppelin:master
Choose a base branch
from
Amxx:feature/governor/security_council
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6330d4c
Add a governor extension that implements a security council
Amxx 578543d
add tests
arr00 fb6e8b7
rename `GovernorSecurityCouncil` to `GovernorProposalGuardian`
arr00 d613cc8
update `GovernorProposalGuardian`
arr00 25eeb02
remove `.only`
arr00 8379051
Merge branch 'master' into feature/governor/security_council
arr00 5ba4596
use `getProposalId` instead of `hashProposal`
arr00 954b03c
Update contracts/governance/extensions/GovernorProposalGuardian.sol
arr00 e56c8b2
Apply suggestions from code review
arr00 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
75 changes: 75 additions & 0 deletions
75
contracts/governance/extensions/GovernorProposalGuardian.sol
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,75 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import {Governor} from "../Governor.sol"; | ||
|
||
/** | ||
* @dev Extension of {Governor} which adds a proposal guardian that can cancel proposals at any stage of their lifecycle. | ||
* | ||
* NOTE: if the proposal guardian is not configured, then proposers take this role for their proposals. | ||
*/ | ||
abstract contract GovernorProposalGuardian is Governor { | ||
address private _proposalGuardian; | ||
|
||
event ProposalGuardianSet(address oldProposalGuardian, address newProposalGuardian); | ||
|
||
/** | ||
* @dev Override {IGovernor-cancel} that implements the extended cancellation logic. | ||
* * The {proposalGuardian} can cancel any proposal at any point in the lifecycle. | ||
* * if no proposal guardian is set, the {proposalProposer} can cancel their proposals at any point in the lifecycle. | ||
* * if the proposal guardian is set, the {proposalProposer} keeps their default rights defined in {IGovernor-cancel} (calling `super`). | ||
*/ | ||
function cancel( | ||
address[] memory targets, | ||
uint256[] memory values, | ||
bytes[] memory calldatas, | ||
bytes32 descriptionHash | ||
) public virtual override returns (uint256) { | ||
address caller = _msgSender(); | ||
address guardian = proposalGuardian(); | ||
|
||
if (guardian == address(0)) { | ||
// if there is no proposal guardian | ||
// ... only the proposer can cancel | ||
// ... no restriction on when the proposer can cancel | ||
uint256 proposalId = getProposalId(targets, values, calldatas, descriptionHash); | ||
address proposer = proposalProposer(proposalId); | ||
if (caller != proposer) revert GovernorOnlyProposer(caller); | ||
return _cancel(targets, values, calldatas, descriptionHash); | ||
} else if (guardian == caller) { | ||
// if there is a proposal guardian, and the caller is the proposal guardian | ||
// ... just cancel | ||
return _cancel(targets, values, calldatas, descriptionHash); | ||
} else { | ||
// if there is a proposal guardian, and the caller is not the proposal guardian | ||
// ... apply default behavior | ||
return super.cancel(targets, values, calldatas, descriptionHash); | ||
} | ||
} | ||
|
||
/** | ||
* @dev Getter that returns the address of the proposal guardian. | ||
*/ | ||
function proposalGuardian() public view virtual returns (address) { | ||
return _proposalGuardian; | ||
} | ||
|
||
/** | ||
* @dev Update the proposal guardian's address. This operation can only be performed through a governance proposal. | ||
* | ||
* Emits a {ProposalGuardianSet} event. | ||
*/ | ||
function setProposalGuardian(address newProposalGuardian) public virtual onlyGovernance { | ||
_setProposalGuardian(newProposalGuardian); | ||
} | ||
|
||
/** | ||
* @dev Internal setter for the proposal guardian. | ||
* | ||
* Emits a {ProposalGuardianSet} event. | ||
*/ | ||
function _setProposalGuardian(address newProposalGuardian) internal virtual { | ||
emit ProposalGuardianSet(_proposalGuardian, newProposalGuardian); | ||
_proposalGuardian = newProposalGuardian; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
contracts/mocks/governance/GovernorProposalGuardianMock.sol
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,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {Governor} from "../../governance/Governor.sol"; | ||
import {GovernorSettings} from "../../governance/extensions/GovernorSettings.sol"; | ||
import {GovernorCountingSimple} from "../../governance/extensions/GovernorCountingSimple.sol"; | ||
import {GovernorVotesQuorumFraction} from "../../governance/extensions/GovernorVotesQuorumFraction.sol"; | ||
import {GovernorProposalGuardian} from "../../governance/extensions/GovernorProposalGuardian.sol"; | ||
|
||
abstract contract GovernorProposalGuardianMock is | ||
GovernorSettings, | ||
GovernorVotesQuorumFraction, | ||
GovernorCountingSimple, | ||
GovernorProposalGuardian | ||
{ | ||
function proposalThreshold() public view override(Governor, GovernorSettings) returns (uint256) { | ||
return super.proposalThreshold(); | ||
} | ||
|
||
function cancel( | ||
address[] memory targets, | ||
uint256[] memory values, | ||
bytes[] memory calldatas, | ||
bytes32 descriptionHash | ||
) public override(Governor, GovernorProposalGuardian) returns (uint256) { | ||
return super.cancel(targets, values, calldatas, descriptionHash); | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
test/governance/extensions/GovernorProposalGuardian.test.js
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,125 @@ | ||
const { ethers } = require('hardhat'); | ||
const { expect } = require('chai'); | ||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
const { impersonate } = require('../../helpers/account'); | ||
|
||
const { GovernorHelper } = require('../../helpers/governance'); | ||
const { ProposalState } = require('../../helpers/enums'); | ||
|
||
const TOKENS = [ | ||
{ Token: '$ERC20Votes', mode: 'blocknumber' }, | ||
{ Token: '$ERC20VotesTimestampMock', mode: 'timestamp' }, | ||
]; | ||
|
||
const name = 'Proposal Guardian Governor'; | ||
const version = '1'; | ||
const tokenName = 'MockToken'; | ||
const tokenSymbol = 'MTKN'; | ||
const tokenSupply = ethers.parseEther('100'); | ||
const votingDelay = 4n; | ||
const votingPeriod = 16n; | ||
const value = ethers.parseEther('1'); | ||
|
||
describe('GovernorProposalGuardian', function () { | ||
for (const { Token, mode } of TOKENS) { | ||
const fixture = async () => { | ||
const [owner, proposer, voter1, voter2, voter3, voter4, other] = await ethers.getSigners(); | ||
const receiver = await ethers.deployContract('CallReceiverMock'); | ||
|
||
const token = await ethers.deployContract(Token, [tokenName, tokenSymbol, tokenName, version]); | ||
const mock = await ethers.deployContract('$GovernorProposalGuardianMock', [ | ||
name, // name | ||
votingDelay, // initialVotingDelay | ||
votingPeriod, // initialVotingPeriod | ||
0n, // initialProposalThreshold | ||
token, // tokenAddress | ||
10n, // quorumNumeratorValue | ||
]); | ||
|
||
await impersonate(mock.target); | ||
await owner.sendTransaction({ to: mock, value }); | ||
await token.$_mint(owner, tokenSupply); | ||
|
||
const helper = new GovernorHelper(mock, mode); | ||
await helper.connect(owner).delegate({ token, to: voter1, value: ethers.parseEther('10') }); | ||
await helper.connect(owner).delegate({ token, to: voter2, value: ethers.parseEther('7') }); | ||
await helper.connect(owner).delegate({ token, to: voter3, value: ethers.parseEther('5') }); | ||
await helper.connect(owner).delegate({ token, to: voter4, value: ethers.parseEther('2') }); | ||
|
||
return { owner, proposer, voter1, voter2, voter3, voter4, other, receiver, token, mock, helper }; | ||
}; | ||
|
||
describe(`using ${Token}`, function () { | ||
beforeEach(async function () { | ||
Object.assign(this, await loadFixture(fixture)); | ||
|
||
// default proposal | ||
this.proposal = this.helper.setProposal( | ||
[ | ||
{ | ||
target: this.receiver.target, | ||
value, | ||
data: this.receiver.interface.encodeFunctionData('mockFunction'), | ||
}, | ||
], | ||
'<proposal description>', | ||
); | ||
}); | ||
|
||
it('deployment check', async function () { | ||
expect(await this.mock.name()).to.equal(name); | ||
expect(await this.mock.token()).to.equal(this.token); | ||
expect(await this.mock.votingDelay()).to.equal(votingDelay); | ||
expect(await this.mock.votingPeriod()).to.equal(votingPeriod); | ||
}); | ||
|
||
describe('set proposal guardian', function () { | ||
it('from governance', async function () { | ||
const governorSigner = await ethers.getSigner(this.mock.target); | ||
await expect(this.mock.connect(governorSigner).setProposalGuardian(this.voter1.address)) | ||
.to.emit(this.mock, 'ProposalGuardianSet') | ||
.withArgs(ethers.ZeroAddress, this.voter1.address); | ||
expect(this.mock.proposalGuardian()).to.eventually.equal(this.voter1.address); | ||
}); | ||
|
||
it('from non-governance', async function () { | ||
await expect(this.mock.setProposalGuardian(this.voter1.address)) | ||
.to.be.revertedWithCustomError(this.mock, 'GovernorOnlyExecutor') | ||
.withArgs(this.owner.address); | ||
}); | ||
}); | ||
|
||
describe('cancel proposal during active state', function () { | ||
beforeEach(async function () { | ||
await this.mock.$_setProposalGuardian(this.voter1.address); | ||
await this.helper.connect(this.proposer).propose(); | ||
await this.helper.waitForSnapshot(1n); | ||
expect(this.mock.state(this.proposal.id)).to.eventually.equal(ProposalState.Active); | ||
}); | ||
|
||
it('from proposal guardian', async function () { | ||
await expect(this.helper.connect(this.voter1).cancel()) | ||
.to.emit(this.mock, 'ProposalCanceled') | ||
.withArgs(this.proposal.id); | ||
}); | ||
|
||
it('from proposer when proposal guardian is non-zero', async function () { | ||
await expect(this.helper.connect(this.proposer).cancel()) | ||
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState') | ||
.withArgs( | ||
this.proposal.id, | ||
ProposalState.Active, | ||
GovernorHelper.proposalStatesToBitMap([ProposalState.Pending]), | ||
); | ||
}); | ||
|
||
it('from proposer when proposal guardian is zero', async function () { | ||
await this.mock.$_setProposalGuardian(ethers.ZeroAddress); | ||
await expect(this.helper.connect(this.proposer).cancel()) | ||
.to.emit(this.mock, 'ProposalCanceled') | ||
.withArgs(this.proposal.id); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |
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.
The default behavior in Governor is to only allow the proposer to cancel when the proposal is still pending. I don't think we want to allow proposers to cancel after that.
Also, if we want to keep the default behavior, wouldn't it be better to just?:
This would simplify this function imo
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.
return super.cancel(targets, values, calldatas, descriptionHash);
is already what we do in the last case ... so what you are proposing would be simplified further by doingNote that it would change the behavior! What we have right now is:
The changes would make it that if there is no guardian, the proposer is still restricted to only canceling during the propose phase.
What behavior do we want ?
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.
I shared a proposal down below. My main concern is that we're missing supper in the branches where there's no guardian and when there's a guardian.
For example, in this code, users could override
_validateCancel
and call super to 1) add the guardian behavior and 2) extend the canceler permissions: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.
In this solution, we will not call super for
_validateCancel
, which may also skip side effects. I don't see how it's materially better and I'm weary of adding another internal function.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.
_validateCancel
could be view though