TimelockController.sol 11 KB

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