GovernorTimelockControlUpgradeable.sol 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0) (governance/extensions/GovernorTimelockControl.sol)
  3. pragma solidity ^0.8.0;
  4. import "./IGovernorTimelockUpgradeable.sol";
  5. import "../GovernorUpgradeable.sol";
  6. import "../TimelockControllerUpgradeable.sol";
  7. import "../../proxy/utils/Initializable.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. * _Available since v4.3._
  23. */
  24. abstract contract GovernorTimelockControlUpgradeable is Initializable, IGovernorTimelockUpgradeable, GovernorUpgradeable {
  25. TimelockControllerUpgradeable private _timelock;
  26. mapping(uint256 => bytes32) private _timelockIds;
  27. /**
  28. * @dev Emitted when the timelock controller used for proposal execution is modified.
  29. */
  30. event TimelockChange(address oldTimelock, address newTimelock);
  31. /**
  32. * @dev Set the timelock.
  33. */
  34. function __GovernorTimelockControl_init(TimelockControllerUpgradeable timelockAddress) internal onlyInitializing {
  35. __GovernorTimelockControl_init_unchained(timelockAddress);
  36. }
  37. function __GovernorTimelockControl_init_unchained(TimelockControllerUpgradeable timelockAddress) internal onlyInitializing {
  38. _updateTimelock(timelockAddress);
  39. }
  40. /**
  41. * @dev See {IERC165-supportsInterface}.
  42. */
  43. function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165Upgradeable, GovernorUpgradeable) returns (bool) {
  44. return interfaceId == type(IGovernorTimelockUpgradeable).interfaceId || super.supportsInterface(interfaceId);
  45. }
  46. /**
  47. * @dev Overriden version of the {Governor-state} function with added support for the `Queued` status.
  48. */
  49. function state(uint256 proposalId) public view virtual override(IGovernorUpgradeable, GovernorUpgradeable) returns (ProposalState) {
  50. ProposalState status = super.state(proposalId);
  51. if (status != ProposalState.Succeeded) {
  52. return status;
  53. }
  54. // core tracks execution, so we just have to check if successful proposal have been queued.
  55. bytes32 queueid = _timelockIds[proposalId];
  56. if (queueid == bytes32(0)) {
  57. return status;
  58. } else if (_timelock.isOperationDone(queueid)) {
  59. return ProposalState.Executed;
  60. } else if (_timelock.isOperationPending(queueid)) {
  61. return ProposalState.Queued;
  62. } else {
  63. return ProposalState.Canceled;
  64. }
  65. }
  66. /**
  67. * @dev Public accessor to check the address of the timelock
  68. */
  69. function timelock() public view virtual override returns (address) {
  70. return address(_timelock);
  71. }
  72. /**
  73. * @dev Public accessor to check the eta of a queued proposal
  74. */
  75. function proposalEta(uint256 proposalId) public view virtual override returns (uint256) {
  76. uint256 eta = _timelock.getTimestamp(_timelockIds[proposalId]);
  77. return eta == 1 ? 0 : eta; // _DONE_TIMESTAMP (1) should be replaced with a 0 value
  78. }
  79. /**
  80. * @dev Function to queue a proposal to the timelock.
  81. */
  82. function queue(
  83. address[] memory targets,
  84. uint256[] memory values,
  85. bytes[] memory calldatas,
  86. bytes32 descriptionHash
  87. ) public virtual override returns (uint256) {
  88. uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
  89. require(state(proposalId) == ProposalState.Succeeded, "Governor: proposal not successful");
  90. uint256 delay = _timelock.getMinDelay();
  91. _timelockIds[proposalId] = _timelock.hashOperationBatch(targets, values, calldatas, 0, descriptionHash);
  92. _timelock.scheduleBatch(targets, values, calldatas, 0, descriptionHash, delay);
  93. emit ProposalQueued(proposalId, block.timestamp + delay);
  94. return proposalId;
  95. }
  96. /**
  97. * @dev Overriden execute function that run the already queued proposal through the timelock.
  98. */
  99. function _execute(
  100. uint256, /* proposalId */
  101. address[] memory targets,
  102. uint256[] memory values,
  103. bytes[] memory calldatas,
  104. bytes32 descriptionHash
  105. ) internal virtual override {
  106. _timelock.executeBatch{value: msg.value}(targets, values, calldatas, 0, descriptionHash);
  107. }
  108. /**
  109. * @dev Overriden version of the {Governor-_cancel} function to cancel the timelocked proposal if it as already
  110. * been queued.
  111. */
  112. function _cancel(
  113. address[] memory targets,
  114. uint256[] memory values,
  115. bytes[] memory calldatas,
  116. bytes32 descriptionHash
  117. ) internal virtual override returns (uint256) {
  118. uint256 proposalId = super._cancel(targets, values, calldatas, descriptionHash);
  119. if (_timelockIds[proposalId] != 0) {
  120. _timelock.cancel(_timelockIds[proposalId]);
  121. delete _timelockIds[proposalId];
  122. }
  123. return proposalId;
  124. }
  125. /**
  126. * @dev Address through which the governor executes action. In this case, the timelock.
  127. */
  128. function _executor() internal view virtual override returns (address) {
  129. return address(_timelock);
  130. }
  131. /**
  132. * @dev Public endpoint to update the underlying timelock instance. Restricted to the timelock itself, so updates
  133. * must be proposed, scheduled, and executed through governance proposals.
  134. *
  135. * CAUTION: It is not recommended to change the timelock while there are other queued governance proposals.
  136. */
  137. function updateTimelock(TimelockControllerUpgradeable newTimelock) external virtual onlyGovernance {
  138. _updateTimelock(newTimelock);
  139. }
  140. function _updateTimelock(TimelockControllerUpgradeable newTimelock) private {
  141. emit TimelockChange(address(_timelock), address(newTimelock));
  142. _timelock = newTimelock;
  143. }
  144. /**
  145. * This empty reserved space is put in place to allow future versions to add new
  146. * variables without shifting down storage in the inheritance chain.
  147. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  148. */
  149. uint256[48] private __gap;
  150. }