GovernorTimelockControl.sol 5.5 KB

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