TimelockControllerUpgradeable.sol 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. __TimelockController_init_unchained(minDelay, proposers, executors);
  61. }
  62. function __TimelockController_init_unchained(
  63. uint256 minDelay,
  64. address[] memory proposers,
  65. address[] memory executors
  66. ) internal onlyInitializing {
  67. _setRoleAdmin(TIMELOCK_ADMIN_ROLE, TIMELOCK_ADMIN_ROLE);
  68. _setRoleAdmin(PROPOSER_ROLE, TIMELOCK_ADMIN_ROLE);
  69. _setRoleAdmin(EXECUTOR_ROLE, TIMELOCK_ADMIN_ROLE);
  70. // deployer + self administration
  71. _setupRole(TIMELOCK_ADMIN_ROLE, _msgSender());
  72. _setupRole(TIMELOCK_ADMIN_ROLE, address(this));
  73. // register proposers
  74. for (uint256 i = 0; i < proposers.length; ++i) {
  75. _setupRole(PROPOSER_ROLE, proposers[i]);
  76. }
  77. // register executors
  78. for (uint256 i = 0; i < executors.length; ++i) {
  79. _setupRole(EXECUTOR_ROLE, executors[i]);
  80. }
  81. _minDelay = minDelay;
  82. emit MinDelayChange(0, minDelay);
  83. }
  84. /**
  85. * @dev Modifier to make a function callable only by a certain role. In
  86. * addition to checking the sender's role, `address(0)` 's role is also
  87. * considered. Granting a role to `address(0)` is equivalent to enabling
  88. * this role for everyone.
  89. */
  90. modifier onlyRoleOrOpenRole(bytes32 role) {
  91. if (!hasRole(role, address(0))) {
  92. _checkRole(role, _msgSender());
  93. }
  94. _;
  95. }
  96. /**
  97. * @dev Contract might receive/hold ETH as part of the maintenance process.
  98. */
  99. receive() external payable {}
  100. /**
  101. * @dev Returns whether an id correspond to a registered operation. This
  102. * includes both Pending, Ready and Done operations.
  103. */
  104. function isOperation(bytes32 id) public view virtual returns (bool pending) {
  105. return getTimestamp(id) > 0;
  106. }
  107. /**
  108. * @dev Returns whether an operation is pending or not.
  109. */
  110. function isOperationPending(bytes32 id) public view virtual returns (bool pending) {
  111. return getTimestamp(id) > _DONE_TIMESTAMP;
  112. }
  113. /**
  114. * @dev Returns whether an operation is ready or not.
  115. */
  116. function isOperationReady(bytes32 id) public view virtual returns (bool ready) {
  117. uint256 timestamp = getTimestamp(id);
  118. return timestamp > _DONE_TIMESTAMP && timestamp <= block.timestamp;
  119. }
  120. /**
  121. * @dev Returns whether an operation is done or not.
  122. */
  123. function isOperationDone(bytes32 id) public view virtual returns (bool done) {
  124. return getTimestamp(id) == _DONE_TIMESTAMP;
  125. }
  126. /**
  127. * @dev Returns the timestamp at with an operation becomes ready (0 for
  128. * unset operations, 1 for done operations).
  129. */
  130. function getTimestamp(bytes32 id) public view virtual returns (uint256 timestamp) {
  131. return _timestamps[id];
  132. }
  133. /**
  134. * @dev Returns the minimum delay for an operation to become valid.
  135. *
  136. * This value can be changed by executing an operation that calls `updateDelay`.
  137. */
  138. function getMinDelay() public view virtual returns (uint256 duration) {
  139. return _minDelay;
  140. }
  141. /**
  142. * @dev Returns the identifier of an operation containing a single
  143. * transaction.
  144. */
  145. function hashOperation(
  146. address target,
  147. uint256 value,
  148. bytes calldata data,
  149. bytes32 predecessor,
  150. bytes32 salt
  151. ) public pure virtual returns (bytes32 hash) {
  152. return keccak256(abi.encode(target, value, data, predecessor, salt));
  153. }
  154. /**
  155. * @dev Returns the identifier of an operation containing a batch of
  156. * transactions.
  157. */
  158. function hashOperationBatch(
  159. address[] calldata targets,
  160. uint256[] calldata values,
  161. bytes[] calldata datas,
  162. bytes32 predecessor,
  163. bytes32 salt
  164. ) public pure virtual returns (bytes32 hash) {
  165. return keccak256(abi.encode(targets, values, datas, predecessor, salt));
  166. }
  167. /**
  168. * @dev Schedule an operation containing a single transaction.
  169. *
  170. * Emits a {CallScheduled} event.
  171. *
  172. * Requirements:
  173. *
  174. * - the caller must have the 'proposer' role.
  175. */
  176. function schedule(
  177. address target,
  178. uint256 value,
  179. bytes calldata data,
  180. bytes32 predecessor,
  181. bytes32 salt,
  182. uint256 delay
  183. ) public virtual onlyRole(PROPOSER_ROLE) {
  184. bytes32 id = hashOperation(target, value, data, predecessor, salt);
  185. _schedule(id, delay);
  186. emit CallScheduled(id, 0, target, value, data, predecessor, delay);
  187. }
  188. /**
  189. * @dev Schedule an operation containing a batch of transactions.
  190. *
  191. * Emits one {CallScheduled} event per transaction in the batch.
  192. *
  193. * Requirements:
  194. *
  195. * - the caller must have the 'proposer' role.
  196. */
  197. function scheduleBatch(
  198. address[] calldata targets,
  199. uint256[] calldata values,
  200. bytes[] calldata datas,
  201. bytes32 predecessor,
  202. bytes32 salt,
  203. uint256 delay
  204. ) public virtual onlyRole(PROPOSER_ROLE) {
  205. require(targets.length == values.length, "TimelockController: length mismatch");
  206. require(targets.length == datas.length, "TimelockController: length mismatch");
  207. bytes32 id = hashOperationBatch(targets, values, datas, predecessor, salt);
  208. _schedule(id, delay);
  209. for (uint256 i = 0; i < targets.length; ++i) {
  210. emit CallScheduled(id, i, targets[i], values[i], datas[i], predecessor, delay);
  211. }
  212. }
  213. /**
  214. * @dev Schedule an operation that is to becomes valid after a given delay.
  215. */
  216. function _schedule(bytes32 id, uint256 delay) private {
  217. require(!isOperation(id), "TimelockController: operation already scheduled");
  218. require(delay >= getMinDelay(), "TimelockController: insufficient delay");
  219. _timestamps[id] = block.timestamp + delay;
  220. }
  221. /**
  222. * @dev Cancel an operation.
  223. *
  224. * Requirements:
  225. *
  226. * - the caller must have the 'proposer' role.
  227. */
  228. function cancel(bytes32 id) public virtual onlyRole(PROPOSER_ROLE) {
  229. require(isOperationPending(id), "TimelockController: operation cannot be cancelled");
  230. delete _timestamps[id];
  231. emit Cancelled(id);
  232. }
  233. /**
  234. * @dev Execute an (ready) operation containing a single transaction.
  235. *
  236. * Emits a {CallExecuted} event.
  237. *
  238. * Requirements:
  239. *
  240. * - the caller must have the 'executor' role.
  241. */
  242. function execute(
  243. address target,
  244. uint256 value,
  245. bytes calldata data,
  246. bytes32 predecessor,
  247. bytes32 salt
  248. ) public payable virtual onlyRoleOrOpenRole(EXECUTOR_ROLE) {
  249. bytes32 id = hashOperation(target, value, data, predecessor, salt);
  250. _beforeCall(id, predecessor);
  251. _call(id, 0, target, value, data);
  252. _afterCall(id);
  253. }
  254. /**
  255. * @dev Execute an (ready) operation containing a batch of transactions.
  256. *
  257. * Emits one {CallExecuted} event per transaction in the batch.
  258. *
  259. * Requirements:
  260. *
  261. * - the caller must have the 'executor' role.
  262. */
  263. function executeBatch(
  264. address[] calldata targets,
  265. uint256[] calldata values,
  266. bytes[] calldata datas,
  267. bytes32 predecessor,
  268. bytes32 salt
  269. ) public payable virtual onlyRoleOrOpenRole(EXECUTOR_ROLE) {
  270. require(targets.length == values.length, "TimelockController: length mismatch");
  271. require(targets.length == datas.length, "TimelockController: length mismatch");
  272. bytes32 id = hashOperationBatch(targets, values, datas, predecessor, salt);
  273. _beforeCall(id, predecessor);
  274. for (uint256 i = 0; i < targets.length; ++i) {
  275. _call(id, i, targets[i], values[i], datas[i]);
  276. }
  277. _afterCall(id);
  278. }
  279. /**
  280. * @dev Checks before execution of an operation's calls.
  281. */
  282. function _beforeCall(bytes32 id, bytes32 predecessor) private view {
  283. require(isOperationReady(id), "TimelockController: operation is not ready");
  284. require(predecessor == bytes32(0) || isOperationDone(predecessor), "TimelockController: missing dependency");
  285. }
  286. /**
  287. * @dev Checks after execution of an operation's calls.
  288. */
  289. function _afterCall(bytes32 id) private {
  290. require(isOperationReady(id), "TimelockController: operation is not ready");
  291. _timestamps[id] = _DONE_TIMESTAMP;
  292. }
  293. /**
  294. * @dev Execute an operation's call.
  295. *
  296. * Emits a {CallExecuted} event.
  297. */
  298. function _call(
  299. bytes32 id,
  300. uint256 index,
  301. address target,
  302. uint256 value,
  303. bytes calldata data
  304. ) private {
  305. (bool success, ) = target.call{value: value}(data);
  306. require(success, "TimelockController: underlying transaction reverted");
  307. emit CallExecuted(id, index, target, value, data);
  308. }
  309. /**
  310. * @dev Changes the minimum timelock duration for future operations.
  311. *
  312. * Emits a {MinDelayChange} event.
  313. *
  314. * Requirements:
  315. *
  316. * - the caller must be the timelock itself. This can only be achieved by scheduling and later executing
  317. * an operation where the timelock is the target and the data is the ABI-encoded call to this function.
  318. */
  319. function updateDelay(uint256 newDelay) external virtual {
  320. require(msg.sender == address(this), "TimelockController: caller must be timelock");
  321. emit MinDelayChange(_minDelay, newDelay);
  322. _minDelay = newDelay;
  323. }
  324. /**
  325. * @dev This empty reserved space is put in place to allow future versions to add new
  326. * variables without shifting down storage in the inheritance chain.
  327. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  328. */
  329. uint256[48] private __gap;
  330. }