TimelockController.sol 11 KB

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