Governor.sol 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0) (governance/Governor.sol)
  3. pragma solidity ^0.8.0;
  4. import "../utils/cryptography/ECDSA.sol";
  5. import "../utils/cryptography/draft-EIP712.sol";
  6. import "../utils/introspection/ERC165.sol";
  7. import "../utils/math/SafeCast.sol";
  8. import "../utils/Address.sol";
  9. import "../utils/Context.sol";
  10. import "../utils/Timers.sol";
  11. import "./IGovernor.sol";
  12. /**
  13. * @dev Core of the governance system, designed to be extended though various modules.
  14. *
  15. * This contract is abstract and requires several function to be implemented in various modules:
  16. *
  17. * - A counting module must implement {quorum}, {_quorumReached}, {_voteSucceeded} and {_countVote}
  18. * - A voting module must implement {getVotes}
  19. * - Additionanly, the {votingPeriod} must also be implemented
  20. *
  21. * _Available since v4.3._
  22. */
  23. abstract contract Governor is Context, ERC165, EIP712, IGovernor {
  24. using SafeCast for uint256;
  25. using Timers for Timers.BlockNumber;
  26. bytes32 public constant BALLOT_TYPEHASH = keccak256("Ballot(uint256 proposalId,uint8 support)");
  27. struct ProposalCore {
  28. Timers.BlockNumber voteStart;
  29. Timers.BlockNumber voteEnd;
  30. bool executed;
  31. bool canceled;
  32. }
  33. string private _name;
  34. mapping(uint256 => ProposalCore) private _proposals;
  35. /**
  36. * @dev Restrict access of functions to the governance executor, which may be the Governor itself or a timelock
  37. * contract, as specified by {_executor}. This generally means that function with this modifier must be voted on and
  38. * executed through the governance protocol.
  39. */
  40. modifier onlyGovernance() {
  41. require(_msgSender() == _executor(), "Governor: onlyGovernance");
  42. _;
  43. }
  44. /**
  45. * @dev Sets the value for {name} and {version}
  46. */
  47. constructor(string memory name_) EIP712(name_, version()) {
  48. _name = name_;
  49. }
  50. /**
  51. * @dev Function to receive ETH that will be handled by the governor (disabled if executor is a third party contract)
  52. */
  53. receive() external payable virtual {
  54. require(_executor() == address(this));
  55. }
  56. /**
  57. * @dev See {IERC165-supportsInterface}.
  58. */
  59. function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC165) returns (bool) {
  60. return interfaceId == type(IGovernor).interfaceId || super.supportsInterface(interfaceId);
  61. }
  62. /**
  63. * @dev See {IGovernor-name}.
  64. */
  65. function name() public view virtual override returns (string memory) {
  66. return _name;
  67. }
  68. /**
  69. * @dev See {IGovernor-version}.
  70. */
  71. function version() public view virtual override returns (string memory) {
  72. return "1";
  73. }
  74. /**
  75. * @dev See {IGovernor-hashProposal}.
  76. *
  77. * The proposal id is produced by hashing the RLC encoded `targets` array, the `values` array, the `calldatas` array
  78. * and the descriptionHash (bytes32 which itself is the keccak256 hash of the description string). This proposal id
  79. * can be produced from the proposal data which is part of the {ProposalCreated} event. It can even be computed in
  80. * advance, before the proposal is submitted.
  81. *
  82. * Note that the chainId and the governor address are not part of the proposal id computation. Consequently, the
  83. * same proposal (with same operation and same description) will have the same id if submitted on multiple governors
  84. * accross multiple networks. This also means that in order to execute the same operation twice (on the same
  85. * governor) the proposer will have to change the description in order to avoid proposal id conflicts.
  86. */
  87. function hashProposal(
  88. address[] memory targets,
  89. uint256[] memory values,
  90. bytes[] memory calldatas,
  91. bytes32 descriptionHash
  92. ) public pure virtual override returns (uint256) {
  93. return uint256(keccak256(abi.encode(targets, values, calldatas, descriptionHash)));
  94. }
  95. /**
  96. * @dev See {IGovernor-state}.
  97. */
  98. function state(uint256 proposalId) public view virtual override returns (ProposalState) {
  99. ProposalCore storage proposal = _proposals[proposalId];
  100. if (proposal.executed) {
  101. return ProposalState.Executed;
  102. }
  103. if (proposal.canceled) {
  104. return ProposalState.Canceled;
  105. }
  106. uint256 snapshot = proposalSnapshot(proposalId);
  107. if (snapshot == 0) {
  108. revert("Governor: unknown proposal id");
  109. }
  110. if (snapshot >= block.number) {
  111. return ProposalState.Pending;
  112. }
  113. uint256 deadline = proposalDeadline(proposalId);
  114. if (deadline >= block.number) {
  115. return ProposalState.Active;
  116. }
  117. if (_quorumReached(proposalId) && _voteSucceeded(proposalId)) {
  118. return ProposalState.Succeeded;
  119. } else {
  120. return ProposalState.Defeated;
  121. }
  122. }
  123. /**
  124. * @dev See {IGovernor-proposalSnapshot}.
  125. */
  126. function proposalSnapshot(uint256 proposalId) public view virtual override returns (uint256) {
  127. return _proposals[proposalId].voteStart.getDeadline();
  128. }
  129. /**
  130. * @dev See {IGovernor-proposalDeadline}.
  131. */
  132. function proposalDeadline(uint256 proposalId) public view virtual override returns (uint256) {
  133. return _proposals[proposalId].voteEnd.getDeadline();
  134. }
  135. /**
  136. * @dev Part of the Governor Bravo's interface: _"The number of votes required in order for a voter to become a proposer"_.
  137. */
  138. function proposalThreshold() public view virtual returns (uint256) {
  139. return 0;
  140. }
  141. /**
  142. * @dev Amount of votes already cast passes the threshold limit.
  143. */
  144. function _quorumReached(uint256 proposalId) internal view virtual returns (bool);
  145. /**
  146. * @dev Is the proposal successful or not.
  147. */
  148. function _voteSucceeded(uint256 proposalId) internal view virtual returns (bool);
  149. /**
  150. * @dev Register a vote with a given support and voting weight.
  151. *
  152. * Note: Support is generic and can represent various things depending on the voting system used.
  153. */
  154. function _countVote(
  155. uint256 proposalId,
  156. address account,
  157. uint8 support,
  158. uint256 weight
  159. ) internal virtual;
  160. /**
  161. * @dev See {IGovernor-propose}.
  162. */
  163. function propose(
  164. address[] memory targets,
  165. uint256[] memory values,
  166. bytes[] memory calldatas,
  167. string memory description
  168. ) public virtual override returns (uint256) {
  169. require(
  170. getVotes(msg.sender, block.number - 1) >= proposalThreshold(),
  171. "GovernorCompatibilityBravo: proposer votes below proposal threshold"
  172. );
  173. uint256 proposalId = hashProposal(targets, values, calldatas, keccak256(bytes(description)));
  174. require(targets.length == values.length, "Governor: invalid proposal length");
  175. require(targets.length == calldatas.length, "Governor: invalid proposal length");
  176. require(targets.length > 0, "Governor: empty proposal");
  177. ProposalCore storage proposal = _proposals[proposalId];
  178. require(proposal.voteStart.isUnset(), "Governor: proposal already exists");
  179. uint64 snapshot = block.number.toUint64() + votingDelay().toUint64();
  180. uint64 deadline = snapshot + votingPeriod().toUint64();
  181. proposal.voteStart.setDeadline(snapshot);
  182. proposal.voteEnd.setDeadline(deadline);
  183. emit ProposalCreated(
  184. proposalId,
  185. _msgSender(),
  186. targets,
  187. values,
  188. new string[](targets.length),
  189. calldatas,
  190. snapshot,
  191. deadline,
  192. description
  193. );
  194. return proposalId;
  195. }
  196. /**
  197. * @dev See {IGovernor-execute}.
  198. */
  199. function execute(
  200. address[] memory targets,
  201. uint256[] memory values,
  202. bytes[] memory calldatas,
  203. bytes32 descriptionHash
  204. ) public payable virtual override returns (uint256) {
  205. uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
  206. ProposalState status = state(proposalId);
  207. require(
  208. status == ProposalState.Succeeded || status == ProposalState.Queued,
  209. "Governor: proposal not successful"
  210. );
  211. _proposals[proposalId].executed = true;
  212. emit ProposalExecuted(proposalId);
  213. _execute(proposalId, targets, values, calldatas, descriptionHash);
  214. return proposalId;
  215. }
  216. /**
  217. * @dev Internal execution mechanism. Can be overriden to implement different execution mechanism
  218. */
  219. function _execute(
  220. uint256, /* proposalId */
  221. address[] memory targets,
  222. uint256[] memory values,
  223. bytes[] memory calldatas,
  224. bytes32 /*descriptionHash*/
  225. ) internal virtual {
  226. string memory errorMessage = "Governor: call reverted without message";
  227. for (uint256 i = 0; i < targets.length; ++i) {
  228. (bool success, bytes memory returndata) = targets[i].call{value: values[i]}(calldatas[i]);
  229. Address.verifyCallResult(success, returndata, errorMessage);
  230. }
  231. }
  232. /**
  233. * @dev Internal cancel mechanism: locks up the proposal timer, preventing it from being re-submitted. Marks it as
  234. * canceled to allow distinguishing it from executed proposals.
  235. *
  236. * Emits a {IGovernor-ProposalCanceled} event.
  237. */
  238. function _cancel(
  239. address[] memory targets,
  240. uint256[] memory values,
  241. bytes[] memory calldatas,
  242. bytes32 descriptionHash
  243. ) internal virtual returns (uint256) {
  244. uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
  245. ProposalState status = state(proposalId);
  246. require(
  247. status != ProposalState.Canceled && status != ProposalState.Expired && status != ProposalState.Executed,
  248. "Governor: proposal not active"
  249. );
  250. _proposals[proposalId].canceled = true;
  251. emit ProposalCanceled(proposalId);
  252. return proposalId;
  253. }
  254. /**
  255. * @dev See {IGovernor-castVote}.
  256. */
  257. function castVote(uint256 proposalId, uint8 support) public virtual override returns (uint256) {
  258. address voter = _msgSender();
  259. return _castVote(proposalId, voter, support, "");
  260. }
  261. /**
  262. * @dev See {IGovernor-castVoteWithReason}.
  263. */
  264. function castVoteWithReason(
  265. uint256 proposalId,
  266. uint8 support,
  267. string calldata reason
  268. ) public virtual override returns (uint256) {
  269. address voter = _msgSender();
  270. return _castVote(proposalId, voter, support, reason);
  271. }
  272. /**
  273. * @dev See {IGovernor-castVoteBySig}.
  274. */
  275. function castVoteBySig(
  276. uint256 proposalId,
  277. uint8 support,
  278. uint8 v,
  279. bytes32 r,
  280. bytes32 s
  281. ) public virtual override returns (uint256) {
  282. address voter = ECDSA.recover(
  283. _hashTypedDataV4(keccak256(abi.encode(BALLOT_TYPEHASH, proposalId, support))),
  284. v,
  285. r,
  286. s
  287. );
  288. return _castVote(proposalId, voter, support, "");
  289. }
  290. /**
  291. * @dev Internal vote casting mechanism: Check that the vote is pending, that it has not been cast yet, retrieve
  292. * voting weight using {IGovernor-getVotes} and call the {_countVote} internal function.
  293. *
  294. * Emits a {IGovernor-VoteCast} event.
  295. */
  296. function _castVote(
  297. uint256 proposalId,
  298. address account,
  299. uint8 support,
  300. string memory reason
  301. ) internal virtual returns (uint256) {
  302. ProposalCore storage proposal = _proposals[proposalId];
  303. require(state(proposalId) == ProposalState.Active, "Governor: vote not currently active");
  304. uint256 weight = getVotes(account, proposal.voteStart.getDeadline());
  305. _countVote(proposalId, account, support, weight);
  306. emit VoteCast(account, proposalId, support, weight, reason);
  307. return weight;
  308. }
  309. /**
  310. * @dev Relays a transaction or function call to an arbitrary target. In cases where the governance executor
  311. * is some contract other than the governor itself, like when using a timelock, this function can be invoked
  312. * in a governance proposal to recover tokens or Ether that was sent to the governor contract by mistake.
  313. * Note that if the executor is simply the governor itself, use of `relay` is redundant.
  314. */
  315. function relay(
  316. address target,
  317. uint256 value,
  318. bytes calldata data
  319. ) external virtual onlyGovernance {
  320. Address.functionCallWithValue(target, data, value);
  321. }
  322. /**
  323. * @dev Address through which the governor executes action. Will be overloaded by module that execute actions
  324. * through another contract such as a timelock.
  325. */
  326. function _executor() internal view virtual returns (address) {
  327. return address(this);
  328. }
  329. }