GovernorTimelockControl.sol 7.0 KB

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