TimelockController.sol 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (governance/TimelockController.sol)
  3. pragma solidity ^0.8.0;
  4. import "../access/AccessControl.sol";
  5. /**
  6. * @dev Contract module which acts as a timelocked controller. When set as the
  7. * owner of an `Ownable` smart contract, it enforces a timelock on all
  8. * `onlyOwner` maintenance operations. This gives time for users of the
  9. * controlled contract to exit before a potentially dangerous maintenance
  10. * operation is applied.
  11. *
  12. * By default, this contract is self administered, meaning administration tasks
  13. * have to go through the timelock process. The proposer (resp executor) role
  14. * is in charge of proposing (resp executing) operations. A common use case is
  15. * to position this {TimelockController} as the owner of a smart contract, with
  16. * a multisig or a DAO as the sole proposer.
  17. *
  18. * _Available since v3.3._
  19. */
  20. contract TimelockController is AccessControl {
  21. bytes32 public constant TIMELOCK_ADMIN_ROLE = keccak256("TIMELOCK_ADMIN_ROLE");
  22. bytes32 public constant PROPOSER_ROLE = keccak256("PROPOSER_ROLE");
  23. bytes32 public constant EXECUTOR_ROLE = keccak256("EXECUTOR_ROLE");
  24. bytes32 public constant CANCELLER_ROLE = keccak256("CANCELLER_ROLE");
  25. uint256 internal constant _DONE_TIMESTAMP = uint256(1);
  26. mapping(bytes32 => uint256) private _timestamps;
  27. uint256 private _minDelay;
  28. /**
  29. * @dev Emitted when a call is scheduled as part of operation `id`.
  30. */
  31. event CallScheduled(
  32. bytes32 indexed id,
  33. uint256 indexed index,
  34. address target,
  35. uint256 value,
  36. bytes data,
  37. bytes32 predecessor,
  38. uint256 delay
  39. );
  40. /**
  41. * @dev Emitted when a call is performed as part of operation `id`.
  42. */
  43. event CallExecuted(bytes32 indexed id, uint256 indexed index, address target, uint256 value, bytes data);
  44. /**
  45. * @dev Emitted when operation `id` is cancelled.
  46. */
  47. event Cancelled(bytes32 indexed id);
  48. /**
  49. * @dev Emitted when the minimum delay for future operations is modified.
  50. */
  51. event MinDelayChange(uint256 oldDuration, uint256 newDuration);
  52. /**
  53. * @dev Initializes the contract with a given `minDelay`, and a list of
  54. * initial proposers and executors. The proposers receive both the
  55. * proposer and the canceller role (for backward compatibility). The
  56. * executors receive the executor role.
  57. *
  58. * NOTE: At construction, both the deployer and the timelock itself are
  59. * administrators. This helps further configuration of the timelock by the
  60. * deployer. After configuration is done, it is recommended that the
  61. * deployer renounces its admin position and relies on timelocked
  62. * operations to perform future maintenance.
  63. */
  64. constructor(
  65. uint256 minDelay,
  66. address[] memory proposers,
  67. address[] memory executors
  68. ) {
  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. // deployer + self administration
  74. _setupRole(TIMELOCK_ADMIN_ROLE, _msgSender());
  75. _setupRole(TIMELOCK_ADMIN_ROLE, address(this));
  76. // register proposers and cancellers
  77. for (uint256 i = 0; i < proposers.length; ++i) {
  78. _setupRole(PROPOSER_ROLE, proposers[i]);
  79. _setupRole(CANCELLER_ROLE, proposers[i]);
  80. }
  81. // register executors
  82. for (uint256 i = 0; i < executors.length; ++i) {
  83. _setupRole(EXECUTOR_ROLE, executors[i]);
  84. }
  85. _minDelay = minDelay;
  86. emit MinDelayChange(0, minDelay);
  87. }
  88. /**
  89. * @dev Modifier to make a function callable only by a certain role. In
  90. * addition to checking the sender's role, `address(0)` 's role is also
  91. * considered. Granting a role to `address(0)` is equivalent to enabling
  92. * this role for everyone.
  93. */
  94. modifier onlyRoleOrOpenRole(bytes32 role) {
  95. if (!hasRole(role, address(0))) {
  96. _checkRole(role, _msgSender());
  97. }
  98. _;
  99. }
  100. /**
  101. * @dev Contract might receive/hold ETH as part of the maintenance process.
  102. */
  103. receive() external payable {}
  104. /**
  105. * @dev Returns whether an id correspond to a registered operation. This
  106. * includes both Pending, Ready and Done operations.
  107. */
  108. function isOperation(bytes32 id) public view virtual returns (bool pending) {
  109. return getTimestamp(id) > 0;
  110. }
  111. /**
  112. * @dev Returns whether an operation is pending or not.
  113. */
  114. function isOperationPending(bytes32 id) public view virtual returns (bool pending) {
  115. return getTimestamp(id) > _DONE_TIMESTAMP;
  116. }
  117. /**
  118. * @dev Returns whether an operation is ready or not.
  119. */
  120. function isOperationReady(bytes32 id) public view virtual returns (bool ready) {
  121. uint256 timestamp = getTimestamp(id);
  122. return timestamp > _DONE_TIMESTAMP && timestamp <= block.timestamp;
  123. }
  124. /**
  125. * @dev Returns whether an operation is done or not.
  126. */
  127. function isOperationDone(bytes32 id) public view virtual returns (bool done) {
  128. return getTimestamp(id) == _DONE_TIMESTAMP;
  129. }
  130. /**
  131. * @dev Returns the timestamp at with an operation becomes ready (0 for
  132. * unset operations, 1 for done operations).
  133. */
  134. function getTimestamp(bytes32 id) public view virtual returns (uint256 timestamp) {
  135. return _timestamps[id];
  136. }
  137. /**
  138. * @dev Returns the minimum delay for an operation to become valid.
  139. *
  140. * This value can be changed by executing an operation that calls `updateDelay`.
  141. */
  142. function getMinDelay() public view virtual returns (uint256 duration) {
  143. return _minDelay;
  144. }
  145. /**
  146. * @dev Returns the identifier of an operation containing a single
  147. * transaction.
  148. */
  149. function hashOperation(
  150. address target,
  151. uint256 value,
  152. bytes calldata data,
  153. bytes32 predecessor,
  154. bytes32 salt
  155. ) public pure virtual returns (bytes32 hash) {
  156. return keccak256(abi.encode(target, value, data, predecessor, salt));
  157. }
  158. /**
  159. * @dev Returns the identifier of an operation containing a batch of
  160. * transactions.
  161. */
  162. function hashOperationBatch(
  163. address[] calldata targets,
  164. uint256[] calldata values,
  165. bytes[] calldata payloads,
  166. bytes32 predecessor,
  167. bytes32 salt
  168. ) public pure virtual returns (bytes32 hash) {
  169. return keccak256(abi.encode(targets, values, payloads, predecessor, salt));
  170. }
  171. /**
  172. * @dev Schedule an operation containing a single transaction.
  173. *
  174. * Emits a {CallScheduled} event.
  175. *
  176. * Requirements:
  177. *
  178. * - the caller must have the 'proposer' role.
  179. */
  180. function schedule(
  181. address target,
  182. uint256 value,
  183. bytes calldata data,
  184. bytes32 predecessor,
  185. bytes32 salt,
  186. uint256 delay
  187. ) public virtual onlyRole(PROPOSER_ROLE) {
  188. bytes32 id = hashOperation(target, value, data, predecessor, salt);
  189. _schedule(id, delay);
  190. emit CallScheduled(id, 0, target, value, data, predecessor, delay);
  191. }
  192. /**
  193. * @dev Schedule an operation containing a batch of transactions.
  194. *
  195. * Emits one {CallScheduled} event per transaction in the batch.
  196. *
  197. * Requirements:
  198. *
  199. * - the caller must have the 'proposer' role.
  200. */
  201. function scheduleBatch(
  202. address[] calldata targets,
  203. uint256[] calldata values,
  204. bytes[] calldata payloads,
  205. bytes32 predecessor,
  206. bytes32 salt,
  207. uint256 delay
  208. ) public virtual onlyRole(PROPOSER_ROLE) {
  209. require(targets.length == values.length, "TimelockController: length mismatch");
  210. require(targets.length == payloads.length, "TimelockController: length mismatch");
  211. bytes32 id = hashOperationBatch(targets, values, payloads, predecessor, salt);
  212. _schedule(id, delay);
  213. for (uint256 i = 0; i < targets.length; ++i) {
  214. emit CallScheduled(id, i, targets[i], values[i], payloads[i], predecessor, delay);
  215. }
  216. }
  217. /**
  218. * @dev Schedule an operation that is to becomes valid after a given delay.
  219. */
  220. function _schedule(bytes32 id, uint256 delay) private {
  221. require(!isOperation(id), "TimelockController: operation already scheduled");
  222. require(delay >= getMinDelay(), "TimelockController: insufficient delay");
  223. _timestamps[id] = block.timestamp + delay;
  224. }
  225. /**
  226. * @dev Cancel an operation.
  227. *
  228. * Requirements:
  229. *
  230. * - the caller must have the 'canceller' role.
  231. */
  232. function cancel(bytes32 id) public virtual onlyRole(CANCELLER_ROLE) {
  233. require(isOperationPending(id), "TimelockController: operation cannot be cancelled");
  234. delete _timestamps[id];
  235. emit Cancelled(id);
  236. }
  237. /**
  238. * @dev Execute an (ready) operation containing a single transaction.
  239. *
  240. * Emits a {CallExecuted} event.
  241. *
  242. * Requirements:
  243. *
  244. * - the caller must have the 'executor' role.
  245. */
  246. // This function can reenter, but it doesn't pose a risk because _afterCall checks that the proposal is pending,
  247. // thus any modifications to the operation during reentrancy should be caught.
  248. // slither-disable-next-line reentrancy-eth
  249. function execute(
  250. address target,
  251. uint256 value,
  252. bytes calldata data,
  253. bytes32 predecessor,
  254. bytes32 salt
  255. ) public payable virtual onlyRoleOrOpenRole(EXECUTOR_ROLE) {
  256. bytes32 id = hashOperation(target, value, data, predecessor, salt);
  257. _beforeCall(id, predecessor);
  258. _call(id, 0, target, value, data);
  259. _afterCall(id);
  260. }
  261. /**
  262. * @dev Execute an (ready) operation containing a batch of transactions.
  263. *
  264. * Emits one {CallExecuted} event per transaction in the batch.
  265. *
  266. * Requirements:
  267. *
  268. * - the caller must have the 'executor' role.
  269. */
  270. function executeBatch(
  271. address[] calldata targets,
  272. uint256[] calldata values,
  273. bytes[] calldata payloads,
  274. bytes32 predecessor,
  275. bytes32 salt
  276. ) public payable virtual onlyRoleOrOpenRole(EXECUTOR_ROLE) {
  277. require(targets.length == values.length, "TimelockController: length mismatch");
  278. require(targets.length == payloads.length, "TimelockController: length mismatch");
  279. bytes32 id = hashOperationBatch(targets, values, payloads, predecessor, salt);
  280. _beforeCall(id, predecessor);
  281. for (uint256 i = 0; i < targets.length; ++i) {
  282. _call(id, i, targets[i], values[i], payloads[i]);
  283. }
  284. _afterCall(id);
  285. }
  286. /**
  287. * @dev Checks before execution of an operation's calls.
  288. */
  289. function _beforeCall(bytes32 id, bytes32 predecessor) private view {
  290. require(isOperationReady(id), "TimelockController: operation is not ready");
  291. require(predecessor == bytes32(0) || isOperationDone(predecessor), "TimelockController: missing dependency");
  292. }
  293. /**
  294. * @dev Checks after execution of an operation's calls.
  295. */
  296. function _afterCall(bytes32 id) private {
  297. require(isOperationReady(id), "TimelockController: operation is not ready");
  298. _timestamps[id] = _DONE_TIMESTAMP;
  299. }
  300. /**
  301. * @dev Execute an operation's call.
  302. *
  303. * Emits a {CallExecuted} event.
  304. */
  305. function _call(
  306. bytes32 id,
  307. uint256 index,
  308. address target,
  309. uint256 value,
  310. bytes calldata data
  311. ) private {
  312. (bool success, ) = target.call{value: value}(data);
  313. require(success, "TimelockController: underlying transaction reverted");
  314. emit CallExecuted(id, index, target, value, data);
  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. }