Governor.sol 13 KB

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