TimelockController.sol 15 KB

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