GovernorTimelockControl.sol 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.3.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 cancelers 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. /// @inheritdoc IGovernor
  61. function proposalNeedsQueuing(uint256) public view virtual override returns (bool) {
  62. return true;
  63. }
  64. /**
  65. * @dev Function to queue a proposal to the timelock.
  66. */
  67. function _queueOperations(
  68. uint256 proposalId,
  69. address[] memory targets,
  70. uint256[] memory values,
  71. bytes[] memory calldatas,
  72. bytes32 descriptionHash
  73. ) internal virtual override returns (uint48) {
  74. uint256 delay = _timelock.getMinDelay();
  75. bytes32 salt = _timelockSalt(descriptionHash);
  76. _timelockIds[proposalId] = _timelock.hashOperationBatch(targets, values, calldatas, 0, salt);
  77. _timelock.scheduleBatch(targets, values, calldatas, 0, salt, delay);
  78. return SafeCast.toUint48(block.timestamp + delay);
  79. }
  80. /**
  81. * @dev Overridden version of the {Governor-_executeOperations} function that runs the already queued proposal
  82. * through the timelock.
  83. */
  84. function _executeOperations(
  85. uint256 proposalId,
  86. address[] memory targets,
  87. uint256[] memory values,
  88. bytes[] memory calldatas,
  89. bytes32 descriptionHash
  90. ) internal virtual override {
  91. // execute
  92. _timelock.executeBatch{value: msg.value}(targets, values, calldatas, 0, _timelockSalt(descriptionHash));
  93. // cleanup for refund
  94. delete _timelockIds[proposalId];
  95. }
  96. /**
  97. * @dev Overridden version of the {Governor-_cancel} function to cancel the timelocked proposal if it has already
  98. * been queued.
  99. */
  100. // This function can reenter through the external call to the timelock, but we assume the timelock is trusted and
  101. // well behaved (according to TimelockController) and this will not happen.
  102. // slither-disable-next-line reentrancy-no-eth
  103. function _cancel(
  104. address[] memory targets,
  105. uint256[] memory values,
  106. bytes[] memory calldatas,
  107. bytes32 descriptionHash
  108. ) internal virtual override returns (uint256) {
  109. uint256 proposalId = super._cancel(targets, values, calldatas, descriptionHash);
  110. bytes32 timelockId = _timelockIds[proposalId];
  111. if (timelockId != 0) {
  112. // cancel
  113. _timelock.cancel(timelockId);
  114. // cleanup
  115. delete _timelockIds[proposalId];
  116. }
  117. return proposalId;
  118. }
  119. /**
  120. * @dev Address through which the governor executes action. In this case, the timelock.
  121. */
  122. function _executor() internal view virtual override returns (address) {
  123. return address(_timelock);
  124. }
  125. /**
  126. * @dev Public endpoint to update the underlying timelock instance. Restricted to the timelock itself, so updates
  127. * must be proposed, scheduled, and executed through governance proposals.
  128. *
  129. * CAUTION: It is not recommended to change the timelock while there are other queued governance proposals.
  130. */
  131. function updateTimelock(TimelockController newTimelock) external virtual onlyGovernance {
  132. _updateTimelock(newTimelock);
  133. }
  134. function _updateTimelock(TimelockController newTimelock) private {
  135. emit TimelockChange(address(_timelock), address(newTimelock));
  136. _timelock = newTimelock;
  137. }
  138. /**
  139. * @dev Computes the {TimelockController} operation salt.
  140. *
  141. * It is computed with the governor address itself to avoid collisions across governor instances using the
  142. * same timelock.
  143. */
  144. function _timelockSalt(bytes32 descriptionHash) private view returns (bytes32) {
  145. return bytes20(address(this)) ^ descriptionHash;
  146. }
  147. }