TimelockController.sol 15 KB

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