TimelockController.sol 11 KB

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