GovernorUpgradeable.sol 13 KB

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