GovernorTimelockControl.sol 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0-rc.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 introduces the risk that
  17. * approved governance proposals could be blocked by the other proposers, effectively executing a Denial of Service attack,
  18. * and therefore blocking access to governance-controlled assets.
  19. *
  20. * _Available since v4.3._
  21. */
  22. abstract contract GovernorTimelockControl is IGovernorTimelock, Governor {
  23. TimelockController private _timelock;
  24. mapping(uint256 => 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 See {IERC165-supportsInterface}.
  37. */
  38. function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, Governor) returns (bool) {
  39. return interfaceId == type(IGovernorTimelock).interfaceId || super.supportsInterface(interfaceId);
  40. }
  41. /**
  42. * @dev Overriden version of the {Governor-state} function with added support for the `Queued` status.
  43. */
  44. function state(uint256 proposalId) public view virtual override(IGovernor, Governor) returns (ProposalState) {
  45. ProposalState status = super.state(proposalId);
  46. if (status != ProposalState.Succeeded) {
  47. return status;
  48. }
  49. // core tracks execution, so we just have to check if successful proposal have been queued.
  50. bytes32 queueid = _timelockIds[proposalId];
  51. if (queueid == bytes32(0)) {
  52. return status;
  53. } else if (_timelock.isOperationDone(queueid)) {
  54. return ProposalState.Executed;
  55. } else if (_timelock.isOperationPending(queueid)) {
  56. return ProposalState.Queued;
  57. } else {
  58. return ProposalState.Canceled;
  59. }
  60. }
  61. /**
  62. * @dev Public accessor to check the address of the timelock
  63. */
  64. function timelock() public view virtual override returns (address) {
  65. return address(_timelock);
  66. }
  67. /**
  68. * @dev Public accessor to check the eta of a queued proposal
  69. */
  70. function proposalEta(uint256 proposalId) public view virtual override returns (uint256) {
  71. uint256 eta = _timelock.getTimestamp(_timelockIds[proposalId]);
  72. return eta == 1 ? 0 : eta; // _DONE_TIMESTAMP (1) should be replaced with a 0 value
  73. }
  74. /**
  75. * @dev Function to queue a proposal to the timelock.
  76. */
  77. function queue(
  78. address[] memory targets,
  79. uint256[] memory values,
  80. bytes[] memory calldatas,
  81. bytes32 descriptionHash
  82. ) public virtual override returns (uint256) {
  83. uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
  84. require(state(proposalId) == ProposalState.Succeeded, "Governor: proposal not successful");
  85. uint256 delay = _timelock.getMinDelay();
  86. _timelockIds[proposalId] = _timelock.hashOperationBatch(targets, values, calldatas, 0, descriptionHash);
  87. _timelock.scheduleBatch(targets, values, calldatas, 0, descriptionHash, delay);
  88. emit ProposalQueued(proposalId, block.timestamp + delay);
  89. return proposalId;
  90. }
  91. /**
  92. * @dev Overriden execute function that run the already queued proposal through the timelock.
  93. */
  94. function _execute(
  95. uint256, /* proposalId */
  96. address[] memory targets,
  97. uint256[] memory values,
  98. bytes[] memory calldatas,
  99. bytes32 descriptionHash
  100. ) internal virtual override {
  101. _timelock.executeBatch{value: msg.value}(targets, values, calldatas, 0, descriptionHash);
  102. }
  103. /**
  104. * @dev Overriden version of the {Governor-_cancel} function to cancel the timelocked proposal if it as already
  105. * been queued.
  106. */
  107. function _cancel(
  108. address[] memory targets,
  109. uint256[] memory values,
  110. bytes[] memory calldatas,
  111. bytes32 descriptionHash
  112. ) internal virtual override returns (uint256) {
  113. uint256 proposalId = super._cancel(targets, values, calldatas, descriptionHash);
  114. if (_timelockIds[proposalId] != 0) {
  115. _timelock.cancel(_timelockIds[proposalId]);
  116. delete _timelockIds[proposalId];
  117. }
  118. return proposalId;
  119. }
  120. /**
  121. * @dev Address through which the governor executes action. In this case, the timelock.
  122. */
  123. function _executor() internal view virtual override returns (address) {
  124. return address(_timelock);
  125. }
  126. /**
  127. * @dev Public endpoint to update the underlying timelock instance. Restricted to the timelock itself, so updates
  128. * must be proposed, scheduled and executed using the {Governor} workflow.
  129. */
  130. function updateTimelock(TimelockController newTimelock) external virtual onlyGovernance {
  131. _updateTimelock(newTimelock);
  132. }
  133. function _updateTimelock(TimelockController newTimelock) private {
  134. emit TimelockChange(address(_timelock), address(newTimelock));
  135. _timelock = newTimelock;
  136. }
  137. }