GovernorTimelockControl.sol 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (governance/extensions/GovernorTimelockControl.sol)
  3. pragma solidity ^0.8.19;
  4. import {IGovernorTimelock} from "./IGovernorTimelock.sol";
  5. import {IGovernor, Governor} from "../Governor.sol";
  6. import {TimelockController} from "../TimelockController.sol";
  7. import {IERC165} from "../../interfaces/IERC165.sol";
  8. /**
  9. * @dev Extension of {Governor} that binds the execution process to an instance of {TimelockController}. This adds a
  10. * delay, enforced by the {TimelockController} to all successful proposal (in addition to the voting duration). The
  11. * {Governor} needs the proposer (and ideally the executor) roles for the {Governor} to work properly.
  12. *
  13. * Using this model means the proposal will be operated by the {TimelockController} and not by the {Governor}. Thus,
  14. * the assets and permissions must be attached to the {TimelockController}. Any asset sent to the {Governor} will be
  15. * inaccessible.
  16. *
  17. * WARNING: Setting up the TimelockController to have additional proposers besides the governor is very risky, as it
  18. * grants them powers that they must be trusted or known not to use: 1) {onlyGovernance} functions like {relay} are
  19. * available to them through the timelock, and 2) approved governance proposals can be blocked by them, effectively
  20. * executing a Denial of Service attack. This risk will be mitigated in a future release.
  21. */
  22. abstract contract GovernorTimelockControl is IGovernorTimelock, Governor {
  23. TimelockController private _timelock;
  24. mapping(uint256 => bytes32) private _timelockIds;
  25. /**
  26. * @dev Emitted when the timelock controller used for proposal execution is modified.
  27. */
  28. event TimelockChange(address oldTimelock, address newTimelock);
  29. /**
  30. * @dev Set the timelock.
  31. */
  32. constructor(TimelockController timelockAddress) {
  33. _updateTimelock(timelockAddress);
  34. }
  35. /**
  36. * @dev See {IERC165-supportsInterface}.
  37. */
  38. function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, Governor) returns (bool) {
  39. return interfaceId == type(IGovernorTimelock).interfaceId || super.supportsInterface(interfaceId);
  40. }
  41. /**
  42. * @dev Overridden version of the {Governor-state} function with added support for the `Queued` state.
  43. */
  44. function state(uint256 proposalId) public view virtual override(IGovernor, Governor) returns (ProposalState) {
  45. ProposalState currentState = super.state(proposalId);
  46. if (currentState != ProposalState.Succeeded) {
  47. return currentState;
  48. }
  49. // core tracks execution, so we just have to check if successful proposal have been queued.
  50. bytes32 queueid = _timelockIds[proposalId];
  51. if (queueid == bytes32(0)) {
  52. return currentState;
  53. } else if (_timelock.isOperationPending(queueid)) {
  54. return ProposalState.Queued;
  55. } else if (_timelock.isOperationDone(queueid)) {
  56. // This can happen if the proposal is executed directly on the timelock.
  57. return ProposalState.Executed;
  58. } else {
  59. // This can happen if the proposal is canceled directly on the timelock.
  60. return ProposalState.Canceled;
  61. }
  62. }
  63. /**
  64. * @dev Public accessor to check the address of the timelock
  65. */
  66. function timelock() public view virtual override returns (address) {
  67. return address(_timelock);
  68. }
  69. /**
  70. * @dev Public accessor to check the eta of a queued proposal
  71. */
  72. function proposalEta(uint256 proposalId) public view virtual override returns (uint256) {
  73. uint256 eta = _timelock.getTimestamp(_timelockIds[proposalId]);
  74. return eta == 1 ? 0 : eta; // _DONE_TIMESTAMP (1) should be replaced with a 0 value
  75. }
  76. /**
  77. * @dev Function to queue a proposal to the timelock.
  78. */
  79. function queue(
  80. address[] memory targets,
  81. uint256[] memory values,
  82. bytes[] memory calldatas,
  83. bytes32 descriptionHash
  84. ) public virtual override returns (uint256) {
  85. uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
  86. ProposalState currentState = state(proposalId);
  87. if (currentState != ProposalState.Succeeded) {
  88. revert GovernorUnexpectedProposalState(
  89. proposalId,
  90. currentState,
  91. _encodeStateBitmap(ProposalState.Succeeded)
  92. );
  93. }
  94. uint256 delay = _timelock.getMinDelay();
  95. _timelockIds[proposalId] = _timelock.hashOperationBatch(targets, values, calldatas, 0, descriptionHash);
  96. _timelock.scheduleBatch(targets, values, calldatas, 0, descriptionHash, delay);
  97. emit ProposalQueued(proposalId, block.timestamp + delay);
  98. return proposalId;
  99. }
  100. /**
  101. * @dev Overridden execute function that run the already queued proposal through the timelock.
  102. */
  103. function _execute(
  104. uint256 proposalId,
  105. address[] memory targets,
  106. uint256[] memory values,
  107. bytes[] memory calldatas,
  108. bytes32 descriptionHash
  109. ) internal virtual override {
  110. // execute
  111. _timelock.executeBatch{value: msg.value}(targets, values, calldatas, 0, descriptionHash);
  112. // cleanup for refund
  113. delete _timelockIds[proposalId];
  114. }
  115. /**
  116. * @dev Overridden version of the {Governor-_cancel} function to cancel the timelocked proposal if it as already
  117. * been queued.
  118. */
  119. // This function can reenter through the external call to the timelock, but we assume the timelock is trusted and
  120. // well behaved (according to TimelockController) and this will not happen.
  121. // slither-disable-next-line reentrancy-no-eth
  122. function _cancel(
  123. address[] memory targets,
  124. uint256[] memory values,
  125. bytes[] memory calldatas,
  126. bytes32 descriptionHash
  127. ) internal virtual override returns (uint256) {
  128. uint256 proposalId = super._cancel(targets, values, calldatas, descriptionHash);
  129. bytes32 timelockId = _timelockIds[proposalId];
  130. if (timelockId != 0) {
  131. // cancel
  132. _timelock.cancel(timelockId);
  133. // cleanup
  134. delete _timelockIds[proposalId];
  135. }
  136. return proposalId;
  137. }
  138. /**
  139. * @dev Address through which the governor executes action. In this case, the timelock.
  140. */
  141. function _executor() internal view virtual override returns (address) {
  142. return address(_timelock);
  143. }
  144. /**
  145. * @dev Public endpoint to update the underlying timelock instance. Restricted to the timelock itself, so updates
  146. * must be proposed, scheduled, and executed through governance proposals.
  147. *
  148. * CAUTION: It is not recommended to change the timelock while there are other queued governance proposals.
  149. */
  150. function updateTimelock(TimelockController newTimelock) external virtual onlyGovernance {
  151. _updateTimelock(newTimelock);
  152. }
  153. function _updateTimelock(TimelockController newTimelock) private {
  154. emit TimelockChange(address(_timelock), address(newTimelock));
  155. _timelock = newTimelock;
  156. }
  157. }