GovernorTimelock.sol 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {IGovernorTimelock} from "./IGovernorTimelock.sol";
  4. import {IGovernor, Governor} from "../Governor.sol";
  5. import {IManaged} from "../../access/manager/IManaged.sol";
  6. import {IAuthority, safeCanCall} from "../../access/manager/IAuthority.sol";
  7. import {IAccessManager} from "../../access/manager/IAccessManager.sol";
  8. import {Address} from "../../utils/Address.sol";
  9. import {Math} from "../../utils/math/Math.sol";
  10. /**
  11. * @dev TODO
  12. *
  13. * _Available since v5.0._
  14. */
  15. abstract contract GovernorTimelock is IGovernorTimelock, Governor {
  16. struct ExecutionDetail {
  17. address authority;
  18. uint32 delay;
  19. }
  20. mapping(uint256 => ExecutionDetail[]) private _executionDetails;
  21. mapping(uint256 => uint256) private _proposalEta;
  22. /**
  23. * @dev Overridden version of the {Governor-state} function with added support for the `Queued` and `Expired` state.
  24. */
  25. function state(uint256 proposalId) public view virtual override(IGovernor, Governor) returns (ProposalState) {
  26. ProposalState currentState = super.state(proposalId);
  27. if (currentState == ProposalState.Succeeded && proposalEta(proposalId) != 0) {
  28. return ProposalState.Queued;
  29. } else {
  30. return currentState;
  31. }
  32. }
  33. /**
  34. * @dev Public accessor to check the eta of a queued proposal.
  35. */
  36. function proposalEta(uint256 proposalId) public view virtual override returns (uint256) {
  37. return _proposalEta[proposalId];
  38. }
  39. /**
  40. * @dev Public accessor to check the execution details.
  41. */
  42. function proposalExecutionDetails(uint256 proposalId) public view returns (ExecutionDetail[] memory) {
  43. return _executionDetails[proposalId];
  44. }
  45. /**
  46. * @dev See {IGovernor-propose}
  47. */
  48. function propose(
  49. address[] memory targets,
  50. uint256[] memory values,
  51. bytes[] memory calldatas,
  52. string memory description
  53. ) public virtual override(IGovernor, Governor) returns (uint256) {
  54. uint256 proposalId = super.propose(targets, values, calldatas, description);
  55. ExecutionDetail[] storage details = _executionDetails[proposalId];
  56. for (uint256 i = 0; i < targets.length; ++i) {
  57. details.push(_detectExecutionDetails(targets[i], bytes4(calldatas[i])));
  58. }
  59. return proposalId;
  60. }
  61. /**
  62. * @dev Function to queue a proposal to the timelock.
  63. *
  64. * NOTE: execution delay is estimated based on the delay information retrieved in {proposal}. This value may be
  65. * off if the delay were updated during the vote.
  66. */
  67. function queue(
  68. address[] memory targets,
  69. uint256[] memory values,
  70. bytes[] memory calldatas,
  71. bytes32 descriptionHash
  72. ) public virtual override returns (uint256) {
  73. uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
  74. ProposalState currentState = state(proposalId);
  75. if (currentState != ProposalState.Succeeded) {
  76. revert GovernorUnexpectedProposalState(
  77. proposalId,
  78. currentState,
  79. _encodeStateBitmap(ProposalState.Succeeded)
  80. );
  81. }
  82. ExecutionDetail[] storage details = _executionDetails[proposalId];
  83. ExecutionDetail memory detail;
  84. uint32 setback = 0;
  85. for (uint256 i = 0; i < targets.length; ++i) {
  86. detail = details[i];
  87. if (detail.authority != address(0)) {
  88. IAccessManager(detail.authority).schedule(targets[i], calldatas[i]);
  89. }
  90. setback = uint32(Math.max(setback, detail.delay)); // cast is safe, both parameters are uint32
  91. }
  92. uint256 eta = block.timestamp + setback;
  93. _proposalEta[proposalId] = eta;
  94. return eta;
  95. }
  96. /**
  97. * @dev See {IGovernor-_execute}
  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. ExecutionDetail[] storage details = _executionDetails[proposalId];
  107. ExecutionDetail memory detail;
  108. // TODO: enforce ETA (might include some _defaultDelaySeconds that are not enforced by any authority)
  109. for (uint256 i = 0; i < targets.length; ++i) {
  110. detail = details[i];
  111. if (detail.authority != address(0)) {
  112. IAccessManager(detail.authority).relay{value: values[i]}(targets[i], calldatas[i]);
  113. } else {
  114. (bool success, bytes memory returndata) = targets[i].call{value: values[i]}(calldatas[i]);
  115. Address.verifyCallResult(success, returndata);
  116. }
  117. }
  118. delete _executionDetails[proposalId];
  119. }
  120. /**
  121. * @dev See {IGovernor-_cancel}
  122. */
  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. ExecutionDetail[] storage details = _executionDetails[proposalId];
  131. ExecutionDetail memory detail;
  132. for (uint256 i = 0; i < targets.length; ++i) {
  133. detail = details[i];
  134. if (detail.authority != address(0)) {
  135. IAccessManager(detail.authority).cancel(address(this), targets[i], calldatas[i]);
  136. }
  137. }
  138. delete _executionDetails[proposalId];
  139. return proposalId;
  140. }
  141. /**
  142. * @dev Default delay to apply to function calls that are not (scheduled and) executed through an AccessManager.
  143. *
  144. * NOTE: execution delays are processed by the AccessManager contracts. We expect these to always be in seconds.
  145. * Therefore, the default delay that is enforced for calls that don't go through an access manager is also in
  146. * seconds, regardless of the Governor's clock mode.
  147. */
  148. function _defaultDelaySeconds() internal view virtual returns (uint32) {
  149. return 0;
  150. }
  151. /**
  152. * @dev Check if the execution of a call needs to be performed through an AccessManager and what delay should be
  153. * applied to this call.
  154. *
  155. * Returns { manager: address(0), delay: _defaultDelaySeconds() } if:
  156. * - target does not have code
  157. * - target does not implement IManaged
  158. * - calling canCall on the target's manager returns a 0 delay
  159. * - calling canCall on the target's manager reverts
  160. * Otherwise (calling canCall on the target's manager returns a non 0 delay), return the address of the
  161. * AccessManager to use, and the delay for this call.
  162. */
  163. function _detectExecutionDetails(address target, bytes4 selector) private view returns (ExecutionDetail memory) {
  164. bool success;
  165. bytes memory returndata;
  166. // Get authority
  167. (success, returndata) = target.staticcall(abi.encodeCall(IManaged.authority, ()));
  168. if (success && returndata.length >= 0x20) {
  169. address authority = abi.decode(returndata, (address));
  170. // Check if governor can call, and try to detect a delay
  171. (bool authorized, uint32 delay) = safeCanCall(authority, address(this), target, selector);
  172. // If direct call is not authorized, and delayed call is possible
  173. if (!authorized && delay > 0) {
  174. return ExecutionDetail({authority: authority, delay: delay});
  175. }
  176. }
  177. return ExecutionDetail({authority: address(0), delay: _defaultDelaySeconds()});
  178. }
  179. }