GovernorTimelockControl.sol 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "./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 Overriden version of the {Governor-state} function with added support for the `Queued` status.
  44. */
  45. function state(uint256 proposalId) public view virtual override(IGovernor, Governor) returns (ProposalState) {
  46. ProposalState status = super.state(proposalId);
  47. if (status != ProposalState.Succeeded) {
  48. return status;
  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 status;
  54. } else if (_timelock.isOperationDone(queueid)) {
  55. return ProposalState.Executed;
  56. } else if (_timelock.isOperationPending(queueid)) {
  57. return ProposalState.Queued;
  58. } else {
  59. return ProposalState.Canceled;
  60. }
  61. }
  62. /**
  63. * @dev Public accessor to check the address of the timelock
  64. */
  65. function timelock() public view virtual override returns (address) {
  66. return address(_timelock);
  67. }
  68. /**
  69. * @dev Public accessor to check the eta of a queued proposal
  70. */
  71. function proposalEta(uint256 proposalId) public view virtual override returns (uint256) {
  72. uint256 eta = _timelock.getTimestamp(_timelockIds[proposalId]);
  73. return eta == 1 ? 0 : eta; // _DONE_TIMESTAMP (1) should be replaced with a 0 value
  74. }
  75. /**
  76. * @dev Function to queue a proposal to the timelock.
  77. */
  78. function queue(
  79. address[] memory targets,
  80. uint256[] memory values,
  81. bytes[] memory calldatas,
  82. bytes32 descriptionHash
  83. ) public virtual override returns (uint256) {
  84. uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
  85. require(state(proposalId) == ProposalState.Succeeded, "Governor: proposal not successful");
  86. uint256 delay = _timelock.getMinDelay();
  87. _timelockIds[proposalId] = _timelock.hashOperationBatch(targets, values, calldatas, 0, descriptionHash);
  88. _timelock.scheduleBatch(targets, values, calldatas, 0, descriptionHash, delay);
  89. emit ProposalQueued(proposalId, block.timestamp + delay);
  90. return proposalId;
  91. }
  92. /**
  93. * @dev Overriden execute function that run the already queued proposal through the timelock.
  94. */
  95. function _execute(
  96. uint256, /* proposalId */
  97. address[] memory targets,
  98. uint256[] memory values,
  99. bytes[] memory calldatas,
  100. bytes32 descriptionHash
  101. ) internal virtual override {
  102. _timelock.executeBatch{value: msg.value}(targets, values, calldatas, 0, descriptionHash);
  103. }
  104. /**
  105. * @dev Overriden version of the {Governor-_cancel} function to cancel the timelocked proposal if it as already
  106. * been queued.
  107. */
  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. if (_timelockIds[proposalId] != 0) {
  116. _timelock.cancel(_timelockIds[proposalId]);
  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. }