TimelockController.sol 14 KB

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