TimelockController.sol 14 KB

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