TimelockController.sol 16 KB

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