GovernorTimelockControl.sol 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (governance/extensions/GovernorTimelockControl.sol)
  3. pragma solidity ^0.8.20;
  4. import {IGovernor, Governor} from "../Governor.sol";
  5. import {TimelockController} from "../TimelockController.sol";
  6. import {IERC165} from "../../interfaces/IERC165.sol";
  7. import {SafeCast} from "../../utils/math/SafeCast.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 and canceller) 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 from a proposal, unless executed via {Governor-relay}.
  16. *
  17. * WARNING: Setting up the TimelockController to have additional proposers or cancellers besides the governor is very
  18. * risky, as it grants them the ability to: 1) execute operations as the timelock, and thus possibly performing
  19. * operations or accessing funds that are expected to only be accessible through a vote, and 2) block governance
  20. * proposals that have been approved by the voters, effectively executing a Denial of Service attack.
  21. */
  22. abstract contract GovernorTimelockControl is Governor {
  23. TimelockController private _timelock;
  24. mapping(uint256 proposalId => 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 Overridden version of the {Governor-state} function that considers the status reported by the timelock.
  37. */
  38. function state(uint256 proposalId) public view virtual override returns (ProposalState) {
  39. ProposalState currentState = super.state(proposalId);
  40. if (currentState != ProposalState.Queued) {
  41. return currentState;
  42. }
  43. bytes32 queueid = _timelockIds[proposalId];
  44. if (_timelock.isOperationPending(queueid)) {
  45. return ProposalState.Queued;
  46. } else if (_timelock.isOperationDone(queueid)) {
  47. // This can happen if the proposal is executed directly on the timelock.
  48. return ProposalState.Executed;
  49. } else {
  50. // This can happen if the proposal is canceled directly on the timelock.
  51. return ProposalState.Canceled;
  52. }
  53. }
  54. /**
  55. * @dev Public accessor to check the address of the timelock
  56. */
  57. function timelock() public view virtual returns (address) {
  58. return address(_timelock);
  59. }
  60. /**
  61. * @dev See {IGovernor-proposalNeedsQueuing}.
  62. */
  63. function proposalNeedsQueuing(uint256) public view virtual override returns (bool) {
  64. return true;
  65. }
  66. /**
  67. * @dev Function to queue a proposal to the timelock.
  68. */
  69. function _queueOperations(
  70. uint256 proposalId,
  71. address[] memory targets,
  72. uint256[] memory values,
  73. bytes[] memory calldatas,
  74. bytes32 descriptionHash
  75. ) internal virtual override returns (uint48) {
  76. uint256 delay = _timelock.getMinDelay();
  77. bytes32 salt = _timelockSalt(descriptionHash);
  78. _timelockIds[proposalId] = _timelock.hashOperationBatch(targets, values, calldatas, 0, salt);
  79. _timelock.scheduleBatch(targets, values, calldatas, 0, salt, delay);
  80. return SafeCast.toUint48(block.timestamp + delay);
  81. }
  82. /**
  83. * @dev Overridden version of the {Governor-_executeOperations} function that runs the already queued proposal
  84. * through the timelock.
  85. */
  86. function _executeOperations(
  87. uint256 proposalId,
  88. address[] memory targets,
  89. uint256[] memory values,
  90. bytes[] memory calldatas,
  91. bytes32 descriptionHash
  92. ) internal virtual override {
  93. // execute
  94. _timelock.executeBatch{value: msg.value}(targets, values, calldatas, 0, _timelockSalt(descriptionHash));
  95. // cleanup for refund
  96. delete _timelockIds[proposalId];
  97. }
  98. /**
  99. * @dev Overridden version of the {Governor-_cancel} function to cancel the timelocked proposal if it has already
  100. * been queued.
  101. */
  102. // This function can reenter through the external call to the timelock, but we assume the timelock is trusted and
  103. // well behaved (according to TimelockController) and this will not happen.
  104. // slither-disable-next-line reentrancy-no-eth
  105. function _cancel(
  106. address[] memory targets,
  107. uint256[] memory values,
  108. bytes[] memory calldatas,
  109. bytes32 descriptionHash
  110. ) internal virtual override returns (uint256) {
  111. uint256 proposalId = super._cancel(targets, values, calldatas, descriptionHash);
  112. bytes32 timelockId = _timelockIds[proposalId];
  113. if (timelockId != 0) {
  114. // cancel
  115. _timelock.cancel(timelockId);
  116. // cleanup
  117. delete _timelockIds[proposalId];
  118. }
  119. return proposalId;
  120. }
  121. /**
  122. * @dev Address through which the governor executes action. In this case, the timelock.
  123. */
  124. function _executor() internal view virtual override returns (address) {
  125. return address(_timelock);
  126. }
  127. /**
  128. * @dev Public endpoint to update the underlying timelock instance. Restricted to the timelock itself, so updates
  129. * must be proposed, scheduled, and executed through governance proposals.
  130. *
  131. * CAUTION: It is not recommended to change the timelock while there are other queued governance proposals.
  132. */
  133. function updateTimelock(TimelockController newTimelock) external virtual onlyGovernance {
  134. _updateTimelock(newTimelock);
  135. }
  136. function _updateTimelock(TimelockController newTimelock) private {
  137. emit TimelockChange(address(_timelock), address(newTimelock));
  138. _timelock = newTimelock;
  139. }
  140. /**
  141. * @dev Computes the {TimelockController} operation salt.
  142. *
  143. * It is computed with the governor address itself to avoid collisions across governor instances using the
  144. * same timelock.
  145. */
  146. function _timelockSalt(bytes32 descriptionHash) private view returns (bytes32) {
  147. return bytes20(address(this)) ^ descriptionHash;
  148. }
  149. }