GovernorUpgradeable.sol 14 KB

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