GovernorUpgradeable.sol 13 KB

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