TimelockController.sol 14 KB

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