TimelockController.sol 14 KB

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