TimelockController.sol 14 KB

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