TimelockController.sol 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/utils/ERC721Holder.sol";
  6. import "../token/ERC1155/utils/ERC1155Holder.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, ERC721Holder, ERC1155Holder {
  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(
  136. bytes4 interfaceId
  137. ) public view virtual override(AccessControl, ERC1155Receiver) returns (bool) {
  138. return super.supportsInterface(interfaceId);
  139. }
  140. /**
  141. * @dev Returns whether an id correspond to a registered operation. This
  142. * includes both Pending, Ready and Done operations.
  143. */
  144. function isOperation(bytes32 id) public view virtual returns (bool) {
  145. return getTimestamp(id) > 0;
  146. }
  147. /**
  148. * @dev Returns whether an operation is pending or not. Note that a "pending" operation may also be "ready".
  149. */
  150. function isOperationPending(bytes32 id) public view virtual returns (bool) {
  151. return getTimestamp(id) > _DONE_TIMESTAMP;
  152. }
  153. /**
  154. * @dev Returns whether an operation is ready for execution. Note that a "ready" operation is also "pending".
  155. */
  156. function isOperationReady(bytes32 id) public view virtual returns (bool) {
  157. uint256 timestamp = getTimestamp(id);
  158. return timestamp > _DONE_TIMESTAMP && timestamp <= block.timestamp;
  159. }
  160. /**
  161. * @dev Returns whether an operation is done or not.
  162. */
  163. function isOperationDone(bytes32 id) public view virtual returns (bool) {
  164. return getTimestamp(id) == _DONE_TIMESTAMP;
  165. }
  166. /**
  167. * @dev Returns the timestamp at which an operation becomes ready (0 for
  168. * unset operations, 1 for done operations).
  169. */
  170. function getTimestamp(bytes32 id) public view virtual returns (uint256) {
  171. return _timestamps[id];
  172. }
  173. /**
  174. * @dev Returns the minimum delay for an operation to become valid.
  175. *
  176. * This value can be changed by executing an operation that calls `updateDelay`.
  177. */
  178. function getMinDelay() public view virtual returns (uint256) {
  179. return _minDelay;
  180. }
  181. /**
  182. * @dev Returns the identifier of an operation containing a single
  183. * transaction.
  184. */
  185. function hashOperation(
  186. address target,
  187. uint256 value,
  188. bytes calldata data,
  189. bytes32 predecessor,
  190. bytes32 salt
  191. ) public pure virtual returns (bytes32) {
  192. return keccak256(abi.encode(target, value, data, predecessor, salt));
  193. }
  194. /**
  195. * @dev Returns the identifier of an operation containing a batch of
  196. * transactions.
  197. */
  198. function hashOperationBatch(
  199. address[] calldata targets,
  200. uint256[] calldata values,
  201. bytes[] calldata payloads,
  202. bytes32 predecessor,
  203. bytes32 salt
  204. ) public pure virtual returns (bytes32) {
  205. return keccak256(abi.encode(targets, values, payloads, predecessor, salt));
  206. }
  207. /**
  208. * @dev Schedule an operation containing a single transaction.
  209. *
  210. * Emits {CallSalt} if salt is nonzero, and {CallScheduled}.
  211. *
  212. * Requirements:
  213. *
  214. * - the caller must have the 'proposer' role.
  215. */
  216. function schedule(
  217. address target,
  218. uint256 value,
  219. bytes calldata data,
  220. bytes32 predecessor,
  221. bytes32 salt,
  222. uint256 delay
  223. ) public virtual onlyRole(PROPOSER_ROLE) {
  224. bytes32 id = hashOperation(target, value, data, predecessor, salt);
  225. _schedule(id, delay);
  226. emit CallScheduled(id, 0, target, value, data, predecessor, delay);
  227. if (salt != bytes32(0)) {
  228. emit CallSalt(id, salt);
  229. }
  230. }
  231. /**
  232. * @dev Schedule an operation containing a batch of transactions.
  233. *
  234. * Emits {CallSalt} if salt is nonzero, and one {CallScheduled} event per transaction in the batch.
  235. *
  236. * Requirements:
  237. *
  238. * - the caller must have the 'proposer' role.
  239. */
  240. function scheduleBatch(
  241. address[] calldata targets,
  242. uint256[] calldata values,
  243. bytes[] calldata payloads,
  244. bytes32 predecessor,
  245. bytes32 salt,
  246. uint256 delay
  247. ) public virtual onlyRole(PROPOSER_ROLE) {
  248. if (targets.length != values.length || targets.length != payloads.length) {
  249. revert TimelockInvalidOperationLength(targets.length, payloads.length, values.length);
  250. }
  251. bytes32 id = hashOperationBatch(targets, values, payloads, predecessor, salt);
  252. _schedule(id, delay);
  253. for (uint256 i = 0; i < targets.length; ++i) {
  254. emit CallScheduled(id, i, targets[i], values[i], payloads[i], predecessor, delay);
  255. }
  256. if (salt != bytes32(0)) {
  257. emit CallSalt(id, salt);
  258. }
  259. }
  260. /**
  261. * @dev Schedule an operation that is to become valid after a given delay.
  262. */
  263. function _schedule(bytes32 id, uint256 delay) private {
  264. if (isOperation(id)) {
  265. revert TimelockUnexpectedOperationState(id, OperationState.Unset);
  266. }
  267. uint256 minDelay = getMinDelay();
  268. if (delay < minDelay) {
  269. revert TimelockInsufficientDelay(delay, minDelay);
  270. }
  271. _timestamps[id] = block.timestamp + delay;
  272. }
  273. /**
  274. * @dev Cancel an operation.
  275. *
  276. * Requirements:
  277. *
  278. * - the caller must have the 'canceller' role.
  279. */
  280. function cancel(bytes32 id) public virtual onlyRole(CANCELLER_ROLE) {
  281. if (!isOperationPending(id)) {
  282. revert TimelockUnexpectedOperationState(id, OperationState.Pending);
  283. }
  284. delete _timestamps[id];
  285. emit Cancelled(id);
  286. }
  287. /**
  288. * @dev Execute an (ready) operation containing a single transaction.
  289. *
  290. * Emits a {CallExecuted} event.
  291. *
  292. * Requirements:
  293. *
  294. * - the caller must have the 'executor' role.
  295. */
  296. // This function can reenter, but it doesn't pose a risk because _afterCall checks that the proposal is pending,
  297. // thus any modifications to the operation during reentrancy should be caught.
  298. // slither-disable-next-line reentrancy-eth
  299. function execute(
  300. address target,
  301. uint256 value,
  302. bytes calldata payload,
  303. bytes32 predecessor,
  304. bytes32 salt
  305. ) public payable virtual onlyRoleOrOpenRole(EXECUTOR_ROLE) {
  306. bytes32 id = hashOperation(target, value, payload, predecessor, salt);
  307. _beforeCall(id, predecessor);
  308. _execute(target, value, payload);
  309. emit CallExecuted(id, 0, target, value, payload);
  310. _afterCall(id);
  311. }
  312. /**
  313. * @dev Execute an (ready) operation containing a batch of transactions.
  314. *
  315. * Emits one {CallExecuted} event per transaction in the batch.
  316. *
  317. * Requirements:
  318. *
  319. * - the caller must have the 'executor' role.
  320. */
  321. // This function can reenter, but it doesn't pose a risk because _afterCall checks that the proposal is pending,
  322. // thus any modifications to the operation during reentrancy should be caught.
  323. // slither-disable-next-line reentrancy-eth
  324. function executeBatch(
  325. address[] calldata targets,
  326. uint256[] calldata values,
  327. bytes[] calldata payloads,
  328. bytes32 predecessor,
  329. bytes32 salt
  330. ) public payable virtual onlyRoleOrOpenRole(EXECUTOR_ROLE) {
  331. if (targets.length != values.length || targets.length != payloads.length) {
  332. revert TimelockInvalidOperationLength(targets.length, payloads.length, values.length);
  333. }
  334. bytes32 id = hashOperationBatch(targets, values, payloads, predecessor, salt);
  335. _beforeCall(id, predecessor);
  336. for (uint256 i = 0; i < targets.length; ++i) {
  337. address target = targets[i];
  338. uint256 value = values[i];
  339. bytes calldata payload = payloads[i];
  340. _execute(target, value, payload);
  341. emit CallExecuted(id, i, target, value, payload);
  342. }
  343. _afterCall(id);
  344. }
  345. /**
  346. * @dev Execute an operation's call.
  347. */
  348. function _execute(address target, uint256 value, bytes calldata data) internal virtual {
  349. (bool success, bytes memory returndata) = target.call{value: value}(data);
  350. Address.verifyCallResult(success, returndata);
  351. }
  352. /**
  353. * @dev Checks before execution of an operation's calls.
  354. */
  355. function _beforeCall(bytes32 id, bytes32 predecessor) private view {
  356. if (!isOperationReady(id)) {
  357. revert TimelockUnexpectedOperationState(id, OperationState.Ready);
  358. }
  359. if (predecessor != bytes32(0) && !isOperationDone(predecessor)) {
  360. revert TimelockUnexecutedPredecessor(predecessor);
  361. }
  362. }
  363. /**
  364. * @dev Checks after execution of an operation's calls.
  365. */
  366. function _afterCall(bytes32 id) private {
  367. if (!isOperationReady(id)) {
  368. revert TimelockUnexpectedOperationState(id, OperationState.Ready);
  369. }
  370. _timestamps[id] = _DONE_TIMESTAMP;
  371. }
  372. /**
  373. * @dev Changes the minimum timelock duration for future operations.
  374. *
  375. * Emits a {MinDelayChange} event.
  376. *
  377. * Requirements:
  378. *
  379. * - the caller must be the timelock itself. This can only be achieved by scheduling and later executing
  380. * an operation where the timelock is the target and the data is the ABI-encoded call to this function.
  381. */
  382. function updateDelay(uint256 newDelay) external virtual {
  383. if (msg.sender != address(this)) {
  384. revert TimelockUnauthorizedCaller(msg.sender);
  385. }
  386. emit MinDelayChange(_minDelay, newDelay);
  387. _minDelay = newDelay;
  388. }
  389. }