TimelockController.sol 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "./AccessControl.sol";
  4. /**
  5. * @dev Contract module which acts as a timelocked controller. When set as the
  6. * owner of an `Ownable` smart contract, it enforces a timelock on all
  7. * `onlyOwner` maintenance operations. This gives time for users of the
  8. * controlled contract to exit before a potentially dangerous maintenance
  9. * operation is applied.
  10. *
  11. * By default, this contract is self administered, meaning administration tasks
  12. * have to go through the timelock process. The proposer (resp executor) role
  13. * is in charge of proposing (resp executing) operations. A common use case is
  14. * to position this {TimelockController} as the owner of a smart contract, with
  15. * a multisig or a DAO as the sole proposer.
  16. */
  17. contract TimelockController is AccessControl {
  18. bytes32 public constant TIMELOCK_ADMIN_ROLE = keccak256("TIMELOCK_ADMIN_ROLE");
  19. bytes32 public constant PROPOSER_ROLE = keccak256("PROPOSER_ROLE");
  20. bytes32 public constant EXECUTOR_ROLE = keccak256("EXECUTOR_ROLE");
  21. uint256 internal constant _DONE_TIMESTAMP = uint256(1);
  22. mapping(bytes32 => uint256) private _timestamps;
  23. uint256 private _minDelay;
  24. /**
  25. * @dev Emitted when a call is scheduled as part of operation `id`.
  26. */
  27. event CallScheduled(bytes32 indexed id, uint256 indexed index, address target, uint256 value, bytes data, bytes32 predecessor, uint256 delay);
  28. /**
  29. * @dev Emitted when a call is performed as part of operation `id`.
  30. */
  31. event CallExecuted(bytes32 indexed id, uint256 indexed index, address target, uint256 value, bytes data);
  32. /**
  33. * @dev Emitted when operation `id` is cancelled.
  34. */
  35. event Cancelled(bytes32 indexed id);
  36. /**
  37. * @dev Emitted when the minimum delay for future operations is modified.
  38. */
  39. event MinDelayChange(uint256 oldDuration, uint256 newDuration);
  40. /**
  41. * @dev Initializes the contract with a given `minDelay`.
  42. */
  43. constructor(uint256 minDelay, address[] memory proposers, address[] memory executors) {
  44. _setRoleAdmin(TIMELOCK_ADMIN_ROLE, TIMELOCK_ADMIN_ROLE);
  45. _setRoleAdmin(PROPOSER_ROLE, TIMELOCK_ADMIN_ROLE);
  46. _setRoleAdmin(EXECUTOR_ROLE, TIMELOCK_ADMIN_ROLE);
  47. // deployer + self administration
  48. _setupRole(TIMELOCK_ADMIN_ROLE, _msgSender());
  49. _setupRole(TIMELOCK_ADMIN_ROLE, address(this));
  50. // register proposers
  51. for (uint256 i = 0; i < proposers.length; ++i) {
  52. _setupRole(PROPOSER_ROLE, proposers[i]);
  53. }
  54. // register executors
  55. for (uint256 i = 0; i < executors.length; ++i) {
  56. _setupRole(EXECUTOR_ROLE, executors[i]);
  57. }
  58. _minDelay = minDelay;
  59. emit MinDelayChange(0, minDelay);
  60. }
  61. /**
  62. * @dev Modifier to make a function callable only by a certain role. In
  63. * addition to checking the sender's role, `address(0)` 's role is also
  64. * considered. Granting a role to `address(0)` is equivalent to enabling
  65. * this role for everyone.
  66. */
  67. modifier onlyRole(bytes32 role) {
  68. require(hasRole(role, _msgSender()) || hasRole(role, address(0)), "TimelockController: sender requires permission");
  69. _;
  70. }
  71. /**
  72. * @dev Contract might receive/hold ETH as part of the maintenance process.
  73. */
  74. receive() external payable {}
  75. /**
  76. * @dev Returns whether an operation is pending or not.
  77. */
  78. function isOperationPending(bytes32 id) public view returns (bool pending) {
  79. return _timestamps[id] > _DONE_TIMESTAMP;
  80. }
  81. /**
  82. * @dev Returns whether an operation is ready or not.
  83. */
  84. function isOperationReady(bytes32 id) public view returns (bool ready) {
  85. // solhint-disable-next-line not-rely-on-time
  86. return _timestamps[id] > _DONE_TIMESTAMP && _timestamps[id] <= block.timestamp;
  87. }
  88. /**
  89. * @dev Returns whether an operation is done or not.
  90. */
  91. function isOperationDone(bytes32 id) public view returns (bool done) {
  92. return _timestamps[id] == _DONE_TIMESTAMP;
  93. }
  94. /**
  95. * @dev Returns the timestamp at with an operation becomes ready (0 for
  96. * unset operations, 1 for done operations).
  97. */
  98. function getTimestamp(bytes32 id) public view returns (uint256 timestamp) {
  99. return _timestamps[id];
  100. }
  101. /**
  102. * @dev Returns the minimum delay for an operation to become valid.
  103. *
  104. * This value can be changed by executing an operation that calls `updateDelay`.
  105. */
  106. function getMinDelay() public view returns (uint256 duration) {
  107. return _minDelay;
  108. }
  109. /**
  110. * @dev Returns the identifier of an operation containing a single
  111. * transaction.
  112. */
  113. function hashOperation(address target, uint256 value, bytes calldata data, bytes32 predecessor, bytes32 salt) public pure returns (bytes32 hash) {
  114. return keccak256(abi.encode(target, value, data, predecessor, salt));
  115. }
  116. /**
  117. * @dev Returns the identifier of an operation containing a batch of
  118. * transactions.
  119. */
  120. function hashOperationBatch(address[] calldata targets, uint256[] calldata values, bytes[] calldata datas, bytes32 predecessor, bytes32 salt) public pure returns (bytes32 hash) {
  121. return keccak256(abi.encode(targets, values, datas, predecessor, salt));
  122. }
  123. /**
  124. * @dev Schedule an operation containing a single transaction.
  125. *
  126. * Emits a {CallScheduled} event.
  127. *
  128. * Requirements:
  129. *
  130. * - the caller must have the 'proposer' role.
  131. */
  132. function schedule(address target, uint256 value, bytes calldata data, bytes32 predecessor, bytes32 salt, uint256 delay) public virtual onlyRole(PROPOSER_ROLE) {
  133. bytes32 id = hashOperation(target, value, data, predecessor, salt);
  134. _schedule(id, delay);
  135. emit CallScheduled(id, 0, target, value, data, predecessor, delay);
  136. }
  137. /**
  138. * @dev Schedule an operation containing a batch of transactions.
  139. *
  140. * Emits one {CallScheduled} event per transaction in the batch.
  141. *
  142. * Requirements:
  143. *
  144. * - the caller must have the 'proposer' role.
  145. */
  146. function scheduleBatch(address[] calldata targets, uint256[] calldata values, bytes[] calldata datas, bytes32 predecessor, bytes32 salt, uint256 delay) public virtual onlyRole(PROPOSER_ROLE) {
  147. require(targets.length == values.length, "TimelockController: length mismatch");
  148. require(targets.length == datas.length, "TimelockController: length mismatch");
  149. bytes32 id = hashOperationBatch(targets, values, datas, predecessor, salt);
  150. _schedule(id, delay);
  151. for (uint256 i = 0; i < targets.length; ++i) {
  152. emit CallScheduled(id, i, targets[i], values[i], datas[i], predecessor, delay);
  153. }
  154. }
  155. /**
  156. * @dev Schedule an operation that is to becomes valid after a given delay.
  157. */
  158. function _schedule(bytes32 id, uint256 delay) private {
  159. require(_timestamps[id] == 0, "TimelockController: operation already scheduled");
  160. require(delay >= _minDelay, "TimelockController: insufficient delay");
  161. // solhint-disable-next-line not-rely-on-time
  162. _timestamps[id] = block.timestamp + delay;
  163. }
  164. /**
  165. * @dev Cancel an operation.
  166. *
  167. * Requirements:
  168. *
  169. * - the caller must have the 'proposer' role.
  170. */
  171. function cancel(bytes32 id) public virtual onlyRole(PROPOSER_ROLE) {
  172. require(isOperationPending(id), "TimelockController: operation cannot be cancelled");
  173. delete _timestamps[id];
  174. emit Cancelled(id);
  175. }
  176. /**
  177. * @dev Execute an (ready) operation containing a single transaction.
  178. *
  179. * Emits a {CallExecuted} event.
  180. *
  181. * Requirements:
  182. *
  183. * - the caller must have the 'executor' role.
  184. */
  185. function execute(address target, uint256 value, bytes calldata data, bytes32 predecessor, bytes32 salt) public payable virtual onlyRole(EXECUTOR_ROLE) {
  186. bytes32 id = hashOperation(target, value, data, predecessor, salt);
  187. _beforeCall(predecessor);
  188. _call(id, 0, target, value, data);
  189. _afterCall(id);
  190. }
  191. /**
  192. * @dev Execute an (ready) operation containing a batch of transactions.
  193. *
  194. * Emits one {CallExecuted} event per transaction in the batch.
  195. *
  196. * Requirements:
  197. *
  198. * - the caller must have the 'executor' role.
  199. */
  200. function executeBatch(address[] calldata targets, uint256[] calldata values, bytes[] calldata datas, bytes32 predecessor, bytes32 salt) public payable virtual onlyRole(EXECUTOR_ROLE) {
  201. require(targets.length == values.length, "TimelockController: length mismatch");
  202. require(targets.length == datas.length, "TimelockController: length mismatch");
  203. bytes32 id = hashOperationBatch(targets, values, datas, predecessor, salt);
  204. _beforeCall(predecessor);
  205. for (uint256 i = 0; i < targets.length; ++i) {
  206. _call(id, i, targets[i], values[i], datas[i]);
  207. }
  208. _afterCall(id);
  209. }
  210. /**
  211. * @dev Checks before execution of an operation's calls.
  212. */
  213. function _beforeCall(bytes32 predecessor) private view {
  214. require(predecessor == bytes32(0) || isOperationDone(predecessor), "TimelockController: missing dependency");
  215. }
  216. /**
  217. * @dev Checks after execution of an operation's calls.
  218. */
  219. function _afterCall(bytes32 id) private {
  220. require(isOperationReady(id), "TimelockController: operation is not ready");
  221. _timestamps[id] = _DONE_TIMESTAMP;
  222. }
  223. /**
  224. * @dev Execute an operation's call.
  225. *
  226. * Emits a {CallExecuted} event.
  227. */
  228. function _call(bytes32 id, uint256 index, address target, uint256 value, bytes calldata data) private {
  229. // solhint-disable-next-line avoid-low-level-calls
  230. (bool success,) = target.call{value: value}(data);
  231. require(success, "TimelockController: underlying transaction reverted");
  232. emit CallExecuted(id, index, target, value, data);
  233. }
  234. /**
  235. * @dev Changes the minimum timelock duration for future operations.
  236. *
  237. * Emits a {MinDelayChange} event.
  238. *
  239. * Requirements:
  240. *
  241. * - the caller must be the timelock itself. This can only be achieved by scheduling and later executing
  242. * an operation where the timelock is the target and the data is the ABI-encoded call to this function.
  243. */
  244. function updateDelay(uint256 newDelay) external virtual {
  245. require(msg.sender == address(this), "TimelockController: caller must be timelock");
  246. emit MinDelayChange(_minDelay, newDelay);
  247. _minDelay = newDelay;
  248. }
  249. }