Governor.sol 11 KB

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