Governor.sol 11 KB

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