TimelockControllerUpgradeable.sol 12 KB

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