TimelockController.sol 14 KB

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