TimelockController.sol 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (governance/TimelockController.sol)
  3. pragma solidity ^0.8.19;
  4. import "../access/AccessControl.sol";
  5. import "../token/ERC721/IERC721Receiver.sol";
  6. import "../token/ERC1155/IERC1155Receiver.sol";
  7. import "../utils/Address.sol";
  8. /**
  9. * @dev Contract module which acts as a timelocked controller. When set as the
  10. * owner of an `Ownable` smart contract, it enforces a timelock on all
  11. * `onlyOwner` maintenance operations. This gives time for users of the
  12. * controlled contract to exit before a potentially dangerous maintenance
  13. * operation is applied.
  14. *
  15. * By default, this contract is self administered, meaning administration tasks
  16. * have to go through the timelock process. The proposer (resp executor) role
  17. * is in charge of proposing (resp executing) operations. A common use case is
  18. * to position this {TimelockController} as the owner of a smart contract, with
  19. * a multisig or a DAO as the sole proposer.
  20. *
  21. * _Available since v3.3._
  22. */
  23. contract TimelockController is AccessControl, IERC721Receiver, IERC1155Receiver {
  24. bytes32 public constant PROPOSER_ROLE = keccak256("PROPOSER_ROLE");
  25. bytes32 public constant EXECUTOR_ROLE = keccak256("EXECUTOR_ROLE");
  26. bytes32 public constant CANCELLER_ROLE = keccak256("CANCELLER_ROLE");
  27. uint256 internal constant _DONE_TIMESTAMP = uint256(1);
  28. mapping(bytes32 => uint256) private _timestamps;
  29. uint256 private _minDelay;
  30. enum OperationState {
  31. Unset,
  32. Pending,
  33. Ready,
  34. Done
  35. }
  36. /**
  37. * @dev Mismatch between the parameters length for a operation call.
  38. */
  39. error TimelockInvalidOperationLength(uint256 targets, uint256 payloads, uint256 values);
  40. /**
  41. * @dev The schedule operation doesn't met the minimum delay.
  42. */
  43. error TimelockInsufficientDelay(uint256 delay, uint256 minDelay);
  44. /**
  45. * @dev The current state of an operation is not the required.
  46. */
  47. error TimelockIncorrectState(bytes32 operationId, OperationState expected);
  48. /**
  49. * @dev A call to a target failed. The target may have reverted.
  50. */
  51. error TimelockFailedCall();
  52. /**
  53. * @dev The predecessor to an operation not yet done.
  54. */
  55. error TimelockMissingPredecessor(bytes32 predecessorId);
  56. /**
  57. * @dev The caller account is not authorized.
  58. */
  59. error TimelockUnauthorizedCaller(address caller);
  60. /**
  61. * @dev Emitted when a call is scheduled as part of operation `id`.
  62. */
  63. event CallScheduled(
  64. bytes32 indexed id,
  65. uint256 indexed index,
  66. address target,
  67. uint256 value,
  68. bytes data,
  69. bytes32 predecessor,
  70. uint256 delay
  71. );
  72. /**
  73. * @dev Emitted when a call is performed as part of operation `id`.
  74. */
  75. event CallExecuted(bytes32 indexed id, uint256 indexed index, address target, uint256 value, bytes data);
  76. /**
  77. * @dev Emitted when new proposal is scheduled with non-zero salt.
  78. */
  79. event CallSalt(bytes32 indexed id, bytes32 salt);
  80. /**
  81. * @dev Emitted when operation `id` is cancelled.
  82. */
  83. event Cancelled(bytes32 indexed id);
  84. /**
  85. * @dev Emitted when the minimum delay for future operations is modified.
  86. */
  87. event MinDelayChange(uint256 oldDuration, uint256 newDuration);
  88. /**
  89. * @dev Initializes the contract with the following parameters:
  90. *
  91. * - `minDelay`: initial minimum delay for operations
  92. * - `proposers`: accounts to be granted proposer and canceller roles
  93. * - `executors`: accounts to be granted executor role
  94. * - `admin`: optional account to be granted admin role; disable with zero address
  95. *
  96. * IMPORTANT: The optional admin can aid with initial configuration of roles after deployment
  97. * without being subject to delay, but this role should be subsequently renounced in favor of
  98. * administration through timelocked proposals. Previous versions of this contract would assign
  99. * this admin to the deployer automatically and should be renounced as well.
  100. */
  101. constructor(uint256 minDelay, address[] memory proposers, address[] memory executors, address admin) {
  102. // self administration
  103. _grantRole(DEFAULT_ADMIN_ROLE, address(this));
  104. // optional admin
  105. if (admin != address(0)) {
  106. _grantRole(DEFAULT_ADMIN_ROLE, admin);
  107. }
  108. // register proposers and cancellers
  109. for (uint256 i = 0; i < proposers.length; ++i) {
  110. _grantRole(PROPOSER_ROLE, proposers[i]);
  111. _grantRole(CANCELLER_ROLE, proposers[i]);
  112. }
  113. // register executors
  114. for (uint256 i = 0; i < executors.length; ++i) {
  115. _grantRole(EXECUTOR_ROLE, executors[i]);
  116. }
  117. _minDelay = minDelay;
  118. emit MinDelayChange(0, minDelay);
  119. }
  120. /**
  121. * @dev Modifier to make a function callable only by a certain role. In
  122. * addition to checking the sender's role, `address(0)` 's role is also
  123. * considered. Granting a role to `address(0)` is equivalent to enabling
  124. * this role for everyone.
  125. */
  126. modifier onlyRoleOrOpenRole(bytes32 role) {
  127. if (!hasRole(role, address(0))) {
  128. _checkRole(role, _msgSender());
  129. }
  130. _;
  131. }
  132. /**
  133. * @dev Contract might receive/hold ETH as part of the maintenance process.
  134. */
  135. receive() external payable {}
  136. /**
  137. * @dev See {IERC165-supportsInterface}.
  138. */
  139. function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, AccessControl) returns (bool) {
  140. return interfaceId == type(IERC1155Receiver).interfaceId || super.supportsInterface(interfaceId);
  141. }
  142. /**
  143. * @dev Returns whether an id correspond to a registered operation. This
  144. * includes both Pending, Ready and Done operations.
  145. */
  146. function isOperation(bytes32 id) public view virtual returns (bool) {
  147. return getTimestamp(id) > 0;
  148. }
  149. /**
  150. * @dev Returns whether an operation is pending or not. Note that a "pending" operation may also be "ready".
  151. */
  152. function isOperationPending(bytes32 id) public view virtual returns (bool) {
  153. return getTimestamp(id) > _DONE_TIMESTAMP;
  154. }
  155. /**
  156. * @dev Returns whether an operation is ready for execution. Note that a "ready" operation is also "pending".
  157. */
  158. function isOperationReady(bytes32 id) public view virtual returns (bool) {
  159. uint256 timestamp = getTimestamp(id);
  160. return timestamp > _DONE_TIMESTAMP && timestamp <= block.timestamp;
  161. }
  162. /**
  163. * @dev Returns whether an operation is done or not.
  164. */
  165. function isOperationDone(bytes32 id) public view virtual returns (bool) {
  166. return getTimestamp(id) == _DONE_TIMESTAMP;
  167. }
  168. /**
  169. * @dev Returns the timestamp at which an operation becomes ready (0 for
  170. * unset operations, 1 for done operations).
  171. */
  172. function getTimestamp(bytes32 id) public view virtual returns (uint256) {
  173. return _timestamps[id];
  174. }
  175. /**
  176. * @dev Returns the minimum delay for an operation to become valid.
  177. *
  178. * This value can be changed by executing an operation that calls `updateDelay`.
  179. */
  180. function getMinDelay() public view virtual returns (uint256) {
  181. return _minDelay;
  182. }
  183. /**
  184. * @dev Returns the identifier of an operation containing a single
  185. * transaction.
  186. */
  187. function hashOperation(
  188. address target,
  189. uint256 value,
  190. bytes calldata data,
  191. bytes32 predecessor,
  192. bytes32 salt
  193. ) public pure virtual returns (bytes32) {
  194. return keccak256(abi.encode(target, value, data, predecessor, salt));
  195. }
  196. /**
  197. * @dev Returns the identifier of an operation containing a batch of
  198. * transactions.
  199. */
  200. function hashOperationBatch(
  201. address[] calldata targets,
  202. uint256[] calldata values,
  203. bytes[] calldata payloads,
  204. bytes32 predecessor,
  205. bytes32 salt
  206. ) public pure virtual returns (bytes32) {
  207. return keccak256(abi.encode(targets, values, payloads, predecessor, salt));
  208. }
  209. /**
  210. * @dev Schedule an operation containing a single transaction.
  211. *
  212. * Emits {CallSalt} if salt is nonzero, and {CallScheduled}.
  213. *
  214. * Requirements:
  215. *
  216. * - the caller must have the 'proposer' role.
  217. */
  218. function schedule(
  219. address target,
  220. uint256 value,
  221. bytes calldata data,
  222. bytes32 predecessor,
  223. bytes32 salt,
  224. uint256 delay
  225. ) public virtual onlyRole(PROPOSER_ROLE) {
  226. bytes32 id = hashOperation(target, value, data, predecessor, salt);
  227. _schedule(id, delay);
  228. emit CallScheduled(id, 0, target, value, data, predecessor, delay);
  229. if (salt != bytes32(0)) {
  230. emit CallSalt(id, salt);
  231. }
  232. }
  233. /**
  234. * @dev Schedule an operation containing a batch of transactions.
  235. *
  236. * Emits {CallSalt} if salt is nonzero, and one {CallScheduled} event per transaction in the batch.
  237. *
  238. * Requirements:
  239. *
  240. * - the caller must have the 'proposer' role.
  241. */
  242. function scheduleBatch(
  243. address[] calldata targets,
  244. uint256[] calldata values,
  245. bytes[] calldata payloads,
  246. bytes32 predecessor,
  247. bytes32 salt,
  248. uint256 delay
  249. ) public virtual onlyRole(PROPOSER_ROLE) {
  250. if (targets.length != values.length || targets.length != payloads.length) {
  251. revert TimelockInvalidOperationLength(targets.length, payloads.length, values.length);
  252. }
  253. bytes32 id = hashOperationBatch(targets, values, payloads, predecessor, salt);
  254. _schedule(id, delay);
  255. for (uint256 i = 0; i < targets.length; ++i) {
  256. emit CallScheduled(id, i, targets[i], values[i], payloads[i], predecessor, delay);
  257. }
  258. if (salt != bytes32(0)) {
  259. emit CallSalt(id, salt);
  260. }
  261. }
  262. /**
  263. * @dev Schedule an operation that is to become valid after a given delay.
  264. */
  265. function _schedule(bytes32 id, uint256 delay) private {
  266. if (isOperation(id)) {
  267. revert TimelockIncorrectState(id, OperationState.Unset);
  268. }
  269. uint256 minDelay = getMinDelay();
  270. if (delay < minDelay) {
  271. revert TimelockInsufficientDelay(delay, minDelay);
  272. }
  273. _timestamps[id] = block.timestamp + delay;
  274. }
  275. /**
  276. * @dev Cancel an operation.
  277. *
  278. * Requirements:
  279. *
  280. * - the caller must have the 'canceller' role.
  281. */
  282. function cancel(bytes32 id) public virtual onlyRole(CANCELLER_ROLE) {
  283. if (!isOperationPending(id)) {
  284. revert TimelockIncorrectState(id, OperationState.Pending);
  285. }
  286. delete _timestamps[id];
  287. emit Cancelled(id);
  288. }
  289. /**
  290. * @dev Execute an (ready) operation containing a single transaction.
  291. *
  292. * Emits a {CallExecuted} event.
  293. *
  294. * Requirements:
  295. *
  296. * - the caller must have the 'executor' role.
  297. */
  298. // This function can reenter, but it doesn't pose a risk because _afterCall checks that the proposal is pending,
  299. // thus any modifications to the operation during reentrancy should be caught.
  300. // slither-disable-next-line reentrancy-eth
  301. function execute(
  302. address target,
  303. uint256 value,
  304. bytes calldata payload,
  305. bytes32 predecessor,
  306. bytes32 salt
  307. ) public payable virtual onlyRoleOrOpenRole(EXECUTOR_ROLE) {
  308. bytes32 id = hashOperation(target, value, payload, predecessor, salt);
  309. _beforeCall(id, predecessor);
  310. _execute(target, value, payload);
  311. emit CallExecuted(id, 0, target, value, payload);
  312. _afterCall(id);
  313. }
  314. /**
  315. * @dev Execute an (ready) operation containing a batch of transactions.
  316. *
  317. * Emits one {CallExecuted} event per transaction in the batch.
  318. *
  319. * Requirements:
  320. *
  321. * - the caller must have the 'executor' role.
  322. */
  323. // This function can reenter, but it doesn't pose a risk because _afterCall checks that the proposal is pending,
  324. // thus any modifications to the operation during reentrancy should be caught.
  325. // slither-disable-next-line reentrancy-eth
  326. function executeBatch(
  327. address[] calldata targets,
  328. uint256[] calldata values,
  329. bytes[] calldata payloads,
  330. bytes32 predecessor,
  331. bytes32 salt
  332. ) public payable virtual onlyRoleOrOpenRole(EXECUTOR_ROLE) {
  333. if (targets.length != values.length || targets.length != payloads.length) {
  334. revert TimelockInvalidOperationLength(targets.length, payloads.length, values.length);
  335. }
  336. bytes32 id = hashOperationBatch(targets, values, payloads, predecessor, salt);
  337. _beforeCall(id, predecessor);
  338. for (uint256 i = 0; i < targets.length; ++i) {
  339. address target = targets[i];
  340. uint256 value = values[i];
  341. bytes calldata payload = payloads[i];
  342. _execute(target, value, payload);
  343. emit CallExecuted(id, i, target, value, payload);
  344. }
  345. _afterCall(id);
  346. }
  347. /**
  348. * @dev Execute an operation's call.
  349. */
  350. function _execute(address target, uint256 value, bytes calldata data) internal virtual {
  351. (bool success, ) = target.call{value: value}(data);
  352. if (!success) {
  353. revert TimelockFailedCall();
  354. }
  355. }
  356. /**
  357. * @dev Checks before execution of an operation's calls.
  358. */
  359. function _beforeCall(bytes32 id, bytes32 predecessor) private view {
  360. if (!isOperationReady(id)) {
  361. revert TimelockIncorrectState(id, OperationState.Ready);
  362. }
  363. if (predecessor != bytes32(0) && !isOperationDone(predecessor)) {
  364. revert TimelockMissingPredecessor(predecessor);
  365. }
  366. }
  367. /**
  368. * @dev Checks after execution of an operation's calls.
  369. */
  370. function _afterCall(bytes32 id) private {
  371. if (!isOperationReady(id)) {
  372. revert TimelockIncorrectState(id, OperationState.Ready);
  373. }
  374. _timestamps[id] = _DONE_TIMESTAMP;
  375. }
  376. /**
  377. * @dev Changes the minimum timelock duration for future operations.
  378. *
  379. * Emits a {MinDelayChange} event.
  380. *
  381. * Requirements:
  382. *
  383. * - the caller must be the timelock itself. This can only be achieved by scheduling and later executing
  384. * an operation where the timelock is the target and the data is the ABI-encoded call to this function.
  385. */
  386. function updateDelay(uint256 newDelay) external virtual {
  387. if (msg.sender != address(this)) {
  388. revert TimelockUnauthorizedCaller(msg.sender);
  389. }
  390. emit MinDelayChange(_minDelay, newDelay);
  391. _minDelay = newDelay;
  392. }
  393. /**
  394. * @dev See {IERC721Receiver-onERC721Received}.
  395. */
  396. function onERC721Received(address, address, uint256, bytes memory) public virtual override returns (bytes4) {
  397. return this.onERC721Received.selector;
  398. }
  399. /**
  400. * @dev See {IERC1155Receiver-onERC1155Received}.
  401. */
  402. function onERC1155Received(
  403. address,
  404. address,
  405. uint256,
  406. uint256,
  407. bytes memory
  408. ) public virtual override returns (bytes4) {
  409. return this.onERC1155Received.selector;
  410. }
  411. /**
  412. * @dev See {IERC1155Receiver-onERC1155BatchReceived}.
  413. */
  414. function onERC1155BatchReceived(
  415. address,
  416. address,
  417. uint256[] memory,
  418. uint256[] memory,
  419. bytes memory
  420. ) public virtual override returns (bytes4) {
  421. return this.onERC1155BatchReceived.selector;
  422. }
  423. }