GovernorTimelockControl.sol 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.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) 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. * NOTE: `AccessManager` does not support scheduling more than one operation with the same target and calldata at
  23. * the same time. See {AccessManager-schedule} for a workaround.
  24. */
  25. abstract contract GovernorTimelockControl is Governor {
  26. TimelockController private _timelock;
  27. mapping(uint256 proposalId => bytes32) private _timelockIds;
  28. /**
  29. * @dev Emitted when the timelock controller used for proposal execution is modified.
  30. */
  31. event TimelockChange(address oldTimelock, address newTimelock);
  32. /**
  33. * @dev Set the timelock.
  34. */
  35. constructor(TimelockController timelockAddress) {
  36. _updateTimelock(timelockAddress);
  37. }
  38. /**
  39. * @dev Overridden version of the {Governor-state} function that considers the status reported by the timelock.
  40. */
  41. function state(uint256 proposalId) public view virtual override returns (ProposalState) {
  42. ProposalState currentState = super.state(proposalId);
  43. if (currentState != ProposalState.Queued) {
  44. return currentState;
  45. }
  46. bytes32 queueid = _timelockIds[proposalId];
  47. if (_timelock.isOperationPending(queueid)) {
  48. return ProposalState.Queued;
  49. } else if (_timelock.isOperationDone(queueid)) {
  50. // This can happen if the proposal is executed directly on the timelock.
  51. return ProposalState.Executed;
  52. } else {
  53. // This can happen if the proposal is canceled directly on the timelock.
  54. return ProposalState.Canceled;
  55. }
  56. }
  57. /**
  58. * @dev Public accessor to check the address of the timelock
  59. */
  60. function timelock() public view virtual returns (address) {
  61. return address(_timelock);
  62. }
  63. /**
  64. * @dev See {IGovernor-proposalNeedsQueuing}.
  65. */
  66. function proposalNeedsQueuing(uint256) public view virtual override returns (bool) {
  67. return true;
  68. }
  69. /**
  70. * @dev Function to queue a proposal to the timelock.
  71. */
  72. function _queueOperations(
  73. uint256 proposalId,
  74. address[] memory targets,
  75. uint256[] memory values,
  76. bytes[] memory calldatas,
  77. bytes32 descriptionHash
  78. ) internal virtual override returns (uint48) {
  79. uint256 delay = _timelock.getMinDelay();
  80. bytes32 salt = _timelockSalt(descriptionHash);
  81. _timelockIds[proposalId] = _timelock.hashOperationBatch(targets, values, calldatas, 0, salt);
  82. _timelock.scheduleBatch(targets, values, calldatas, 0, salt, delay);
  83. return SafeCast.toUint48(block.timestamp + delay);
  84. }
  85. /**
  86. * @dev Overridden version of the {Governor-_executeOperations} function that runs the already queued proposal
  87. * through the timelock.
  88. */
  89. function _executeOperations(
  90. uint256 proposalId,
  91. address[] memory targets,
  92. uint256[] memory values,
  93. bytes[] memory calldatas,
  94. bytes32 descriptionHash
  95. ) internal virtual override {
  96. // execute
  97. _timelock.executeBatch{value: msg.value}(targets, values, calldatas, 0, _timelockSalt(descriptionHash));
  98. // cleanup for refund
  99. delete _timelockIds[proposalId];
  100. }
  101. /**
  102. * @dev Overridden version of the {Governor-_cancel} function to cancel the timelocked proposal if it has already
  103. * been queued.
  104. */
  105. // This function can reenter through the external call to the timelock, but we assume the timelock is trusted and
  106. // well behaved (according to TimelockController) and this will not happen.
  107. // slither-disable-next-line reentrancy-no-eth
  108. function _cancel(
  109. address[] memory targets,
  110. uint256[] memory values,
  111. bytes[] memory calldatas,
  112. bytes32 descriptionHash
  113. ) internal virtual override returns (uint256) {
  114. uint256 proposalId = super._cancel(targets, values, calldatas, descriptionHash);
  115. bytes32 timelockId = _timelockIds[proposalId];
  116. if (timelockId != 0) {
  117. // cancel
  118. _timelock.cancel(timelockId);
  119. // cleanup
  120. delete _timelockIds[proposalId];
  121. }
  122. return proposalId;
  123. }
  124. /**
  125. * @dev Address through which the governor executes action. In this case, the timelock.
  126. */
  127. function _executor() internal view virtual override returns (address) {
  128. return address(_timelock);
  129. }
  130. /**
  131. * @dev Public endpoint to update the underlying timelock instance. Restricted to the timelock itself, so updates
  132. * must be proposed, scheduled, and executed through governance proposals.
  133. *
  134. * CAUTION: It is not recommended to change the timelock while there are other queued governance proposals.
  135. */
  136. function updateTimelock(TimelockController newTimelock) external virtual onlyGovernance {
  137. _updateTimelock(newTimelock);
  138. }
  139. function _updateTimelock(TimelockController newTimelock) private {
  140. emit TimelockChange(address(_timelock), address(newTimelock));
  141. _timelock = newTimelock;
  142. }
  143. /**
  144. * @dev Computes the {TimelockController} operation salt.
  145. *
  146. * It is computed with the governor address itself to avoid collisions across governor instances using the
  147. * same timelock.
  148. */
  149. function _timelockSalt(bytes32 descriptionHash) private view returns (bytes32) {
  150. return bytes20(address(this)) ^ descriptionHash;
  151. }
  152. }