AccessControlDefaultAdminRules.sol 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0) (access/AccessControlDefaultAdminRules.sol)
  3. pragma solidity ^0.8.0;
  4. import "./AccessControl.sol";
  5. import "./IAccessControlDefaultAdminRules.sol";
  6. import "../utils/math/SafeCast.sol";
  7. import "../interfaces/IERC5313.sol";
  8. /**
  9. * @dev Extension of {AccessControl} that allows specifying special rules to manage
  10. * the `DEFAULT_ADMIN_ROLE` holder, which is a sensitive role with special permissions
  11. * over other roles that may potentially have privileged rights in the system.
  12. *
  13. * If a specific role doesn't have an admin role assigned, the holder of the
  14. * `DEFAULT_ADMIN_ROLE` will have the ability to grant it and revoke it.
  15. *
  16. * This contract implements the following risk mitigations on top of {AccessControl}:
  17. *
  18. * * Only one account holds the `DEFAULT_ADMIN_ROLE` since deployment until it's potentially renounced.
  19. * * Enforces a 2-step process to transfer the `DEFAULT_ADMIN_ROLE` to another account.
  20. * * Enforces a configurable delay between the two steps, with the ability to cancel before the transfer is accepted.
  21. * * The delay can be changed by scheduling, see {changeDefaultAdminDelay}.
  22. * * It is not possible to use another role to manage the `DEFAULT_ADMIN_ROLE`.
  23. *
  24. * Example usage:
  25. *
  26. * ```solidity
  27. * contract MyToken is AccessControlDefaultAdminRules {
  28. * constructor() AccessControlDefaultAdminRules(
  29. * 3 days,
  30. * msg.sender // Explicit initial `DEFAULT_ADMIN_ROLE` holder
  31. * ) {}
  32. * }
  33. * ```
  34. *
  35. * _Available since v4.9._
  36. */
  37. abstract contract AccessControlDefaultAdminRules is IAccessControlDefaultAdminRules, IERC5313, AccessControl {
  38. // pending admin pair read/written together frequently
  39. address private _pendingDefaultAdmin;
  40. uint48 private _pendingDefaultAdminSchedule; // 0 == unset
  41. uint48 private _currentDelay;
  42. address private _currentDefaultAdmin;
  43. // pending delay pair read/written together frequently
  44. uint48 private _pendingDelay;
  45. uint48 private _pendingDelaySchedule; // 0 == unset
  46. /**
  47. * @dev Sets the initial values for {defaultAdminDelay} and {defaultAdmin} address.
  48. */
  49. constructor(uint48 initialDelay, address initialDefaultAdmin) {
  50. _currentDelay = initialDelay;
  51. _grantRole(DEFAULT_ADMIN_ROLE, initialDefaultAdmin);
  52. }
  53. /**
  54. * @dev See {IERC165-supportsInterface}.
  55. */
  56. function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
  57. return interfaceId == type(IAccessControlDefaultAdminRules).interfaceId || super.supportsInterface(interfaceId);
  58. }
  59. /**
  60. * @dev See {IERC5313-owner}.
  61. */
  62. function owner() public view virtual returns (address) {
  63. return defaultAdmin();
  64. }
  65. ///
  66. /// Override AccessControl role management
  67. ///
  68. /**
  69. * @dev See {AccessControl-grantRole}. Reverts for `DEFAULT_ADMIN_ROLE`.
  70. */
  71. function grantRole(bytes32 role, address account) public virtual override(AccessControl, IAccessControl) {
  72. require(role != DEFAULT_ADMIN_ROLE, "AccessControl: can't directly grant default admin role");
  73. super.grantRole(role, account);
  74. }
  75. /**
  76. * @dev See {AccessControl-revokeRole}. Reverts for `DEFAULT_ADMIN_ROLE`.
  77. */
  78. function revokeRole(bytes32 role, address account) public virtual override(AccessControl, IAccessControl) {
  79. require(role != DEFAULT_ADMIN_ROLE, "AccessControl: can't directly revoke default admin role");
  80. super.revokeRole(role, account);
  81. }
  82. /**
  83. * @dev See {AccessControl-renounceRole}.
  84. *
  85. * For the `DEFAULT_ADMIN_ROLE`, it only allows renouncing in two steps by first calling
  86. * {beginDefaultAdminTransfer} to the `address(0)`, so it's required that the {pendingDefaultAdmin} schedule
  87. * has also passed when calling this function.
  88. *
  89. * After its execution, it will not be possible to call `onlyRole(DEFAULT_ADMIN_ROLE)` functions.
  90. *
  91. * NOTE: Renouncing `DEFAULT_ADMIN_ROLE` will leave the contract without a {defaultAdmin},
  92. * thereby disabling any functionality that is only available for it, and the possibility of reassigning a
  93. * non-administrated role.
  94. */
  95. function renounceRole(bytes32 role, address account) public virtual override(AccessControl, IAccessControl) {
  96. if (role == DEFAULT_ADMIN_ROLE) {
  97. (address newDefaultAdmin, uint48 schedule) = pendingDefaultAdmin();
  98. require(
  99. newDefaultAdmin == address(0) && _isScheduleSet(schedule) && _hasSchedulePassed(schedule),
  100. "AccessControl: only can renounce in two delayed steps"
  101. );
  102. }
  103. super.renounceRole(role, account);
  104. }
  105. /**
  106. * @dev See {AccessControl-_grantRole}.
  107. *
  108. * For `DEFAULT_ADMIN_ROLE`, it only allows granting if there isn't already a {defaultAdmin} or if the
  109. * role has been previously renounced.
  110. *
  111. * NOTE: Exposing this function through another mechanism may make the `DEFAULT_ADMIN_ROLE`
  112. * assignable again. Make sure to guarantee this is the expected behavior in your implementation.
  113. */
  114. function _grantRole(bytes32 role, address account) internal virtual override {
  115. if (role == DEFAULT_ADMIN_ROLE) {
  116. require(defaultAdmin() == address(0), "AccessControl: default admin already granted");
  117. _currentDefaultAdmin = account;
  118. }
  119. super._grantRole(role, account);
  120. }
  121. /**
  122. * @dev See {AccessControl-_revokeRole}.
  123. */
  124. function _revokeRole(bytes32 role, address account) internal virtual override {
  125. if (role == DEFAULT_ADMIN_ROLE) {
  126. delete _currentDefaultAdmin;
  127. }
  128. super._revokeRole(role, account);
  129. }
  130. /**
  131. * @dev See {AccessControl-_setRoleAdmin}. Reverts for `DEFAULT_ADMIN_ROLE`.
  132. */
  133. function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual override {
  134. require(role != DEFAULT_ADMIN_ROLE, "AccessControl: can't violate default admin rules");
  135. super._setRoleAdmin(role, adminRole);
  136. }
  137. ///
  138. /// AccessControlDefaultAdminRules accessors
  139. ///
  140. /**
  141. * @inheritdoc IAccessControlDefaultAdminRules
  142. */
  143. function defaultAdmin() public view virtual returns (address) {
  144. return _currentDefaultAdmin;
  145. }
  146. /**
  147. * @inheritdoc IAccessControlDefaultAdminRules
  148. */
  149. function pendingDefaultAdmin() public view virtual returns (address newAdmin, uint48 schedule) {
  150. return (_pendingDefaultAdmin, _pendingDefaultAdminSchedule);
  151. }
  152. /**
  153. * @inheritdoc IAccessControlDefaultAdminRules
  154. */
  155. function defaultAdminDelay() public view virtual returns (uint48) {
  156. uint48 schedule = _pendingDelaySchedule;
  157. return (_isScheduleSet(schedule) && _hasSchedulePassed(schedule)) ? _pendingDelay : _currentDelay;
  158. }
  159. /**
  160. * @inheritdoc IAccessControlDefaultAdminRules
  161. */
  162. function pendingDefaultAdminDelay() public view virtual returns (uint48 newDelay, uint48 schedule) {
  163. schedule = _pendingDelaySchedule;
  164. return (_isScheduleSet(schedule) && !_hasSchedulePassed(schedule)) ? (_pendingDelay, schedule) : (0, 0);
  165. }
  166. /**
  167. * @inheritdoc IAccessControlDefaultAdminRules
  168. */
  169. function defaultAdminDelayIncreaseWait() public view virtual returns (uint48) {
  170. return 5 days;
  171. }
  172. ///
  173. /// AccessControlDefaultAdminRules public and internal setters for defaultAdmin/pendingDefaultAdmin
  174. ///
  175. /**
  176. * @inheritdoc IAccessControlDefaultAdminRules
  177. */
  178. function beginDefaultAdminTransfer(address newAdmin) public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
  179. _beginDefaultAdminTransfer(newAdmin);
  180. }
  181. /**
  182. * @dev See {beginDefaultAdminTransfer}.
  183. *
  184. * Internal function without access restriction.
  185. */
  186. function _beginDefaultAdminTransfer(address newAdmin) internal virtual {
  187. uint48 newSchedule = SafeCast.toUint48(block.timestamp) + defaultAdminDelay();
  188. _setPendingDefaultAdmin(newAdmin, newSchedule);
  189. emit DefaultAdminTransferScheduled(newAdmin, newSchedule);
  190. }
  191. /**
  192. * @inheritdoc IAccessControlDefaultAdminRules
  193. */
  194. function cancelDefaultAdminTransfer() public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
  195. _cancelDefaultAdminTransfer();
  196. }
  197. /**
  198. * @dev See {cancelDefaultAdminTransfer}.
  199. *
  200. * Internal function without access restriction.
  201. */
  202. function _cancelDefaultAdminTransfer() internal virtual {
  203. _setPendingDefaultAdmin(address(0), 0);
  204. }
  205. /**
  206. * @inheritdoc IAccessControlDefaultAdminRules
  207. */
  208. function acceptDefaultAdminTransfer() public virtual {
  209. (address newDefaultAdmin, ) = pendingDefaultAdmin();
  210. require(_msgSender() == newDefaultAdmin, "AccessControl: pending admin must accept");
  211. _acceptDefaultAdminTransfer();
  212. }
  213. /**
  214. * @dev See {acceptDefaultAdminTransfer}.
  215. *
  216. * Internal function without access restriction.
  217. */
  218. function _acceptDefaultAdminTransfer() internal virtual {
  219. (address newAdmin, uint48 schedule) = pendingDefaultAdmin();
  220. require(_isScheduleSet(schedule) && _hasSchedulePassed(schedule), "AccessControl: transfer delay not passed");
  221. _revokeRole(DEFAULT_ADMIN_ROLE, defaultAdmin());
  222. _grantRole(DEFAULT_ADMIN_ROLE, newAdmin);
  223. delete _pendingDefaultAdmin;
  224. delete _pendingDefaultAdminSchedule;
  225. }
  226. ///
  227. /// AccessControlDefaultAdminRules public and internal setters for defaultAdminDelay/pendingDefaultAdminDelay
  228. ///
  229. /**
  230. * @inheritdoc IAccessControlDefaultAdminRules
  231. */
  232. function changeDefaultAdminDelay(uint48 newDelay) public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
  233. _changeDefaultAdminDelay(newDelay);
  234. }
  235. /**
  236. * @dev See {changeDefaultAdminDelay}.
  237. *
  238. * Internal function without access restriction.
  239. */
  240. function _changeDefaultAdminDelay(uint48 newDelay) internal virtual {
  241. uint48 newSchedule = SafeCast.toUint48(block.timestamp) + _delayChangeWait(newDelay);
  242. _setPendingDelay(newDelay, newSchedule);
  243. emit DefaultAdminDelayChangeScheduled(newDelay, newSchedule);
  244. }
  245. /**
  246. * @inheritdoc IAccessControlDefaultAdminRules
  247. */
  248. function rollbackDefaultAdminDelay() public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
  249. _rollbackDefaultAdminDelay();
  250. }
  251. /**
  252. * @dev See {rollbackDefaultAdminDelay}.
  253. *
  254. * Internal function without access restriction.
  255. */
  256. function _rollbackDefaultAdminDelay() internal virtual {
  257. _setPendingDelay(0, 0);
  258. }
  259. /**
  260. * @dev Returns the amount of seconds to wait after the `newDelay` will
  261. * become the new {defaultAdminDelay}.
  262. *
  263. * The value returned guarantees that if the delay is reduced, it will go into effect
  264. * after a wait that honors the previously set delay.
  265. *
  266. * See {defaultAdminDelayIncreaseWait}.
  267. */
  268. function _delayChangeWait(uint48 newDelay) internal view virtual returns (uint48) {
  269. uint48 currentDelay = defaultAdminDelay();
  270. // When increasing the delay, we schedule the delay change to occur after a period of "new delay" has passed, up
  271. // to a maximum given by defaultAdminDelayIncreaseWait, by default 5 days. For example, if increasing from 1 day
  272. // to 3 days, the new delay will come into effect after 3 days. If increasing from 1 day to 10 days, the new
  273. // delay will come into effect after 5 days. The 5 day wait period is intended to be able to fix an error like
  274. // using milliseconds instead of seconds.
  275. //
  276. // When decreasing the delay, we wait the difference between "current delay" and "new delay". This guarantees
  277. // that an admin transfer cannot be made faster than "current delay" at the time the delay change is scheduled.
  278. // For example, if decreasing from 10 days to 3 days, the new delay will come into effect after 7 days.
  279. return
  280. newDelay > currentDelay
  281. ? uint48(Math.min(newDelay, defaultAdminDelayIncreaseWait())) // no need to safecast, both inputs are uint48
  282. : currentDelay - newDelay;
  283. }
  284. ///
  285. /// Private setters
  286. ///
  287. /**
  288. * @dev Setter of the tuple for pending admin and its schedule.
  289. *
  290. * May emit a DefaultAdminTransferCanceled event.
  291. */
  292. function _setPendingDefaultAdmin(address newAdmin, uint48 newSchedule) private {
  293. (, uint48 oldSchedule) = pendingDefaultAdmin();
  294. _pendingDefaultAdmin = newAdmin;
  295. _pendingDefaultAdminSchedule = newSchedule;
  296. // An `oldSchedule` from `pendingDefaultAdmin()` is only set if it hasn't been accepted.
  297. if (_isScheduleSet(oldSchedule)) {
  298. // Emit for implicit cancellations when another default admin was scheduled.
  299. emit DefaultAdminTransferCanceled();
  300. }
  301. }
  302. /**
  303. * @dev Setter of the tuple for pending delay and its schedule.
  304. *
  305. * May emit a DefaultAdminDelayChangeCanceled event.
  306. */
  307. function _setPendingDelay(uint48 newDelay, uint48 newSchedule) private {
  308. uint48 oldSchedule = _pendingDelaySchedule;
  309. if (_isScheduleSet(oldSchedule)) {
  310. if (_hasSchedulePassed(oldSchedule)) {
  311. // Materialize a virtual delay
  312. _currentDelay = _pendingDelay;
  313. } else {
  314. // Emit for implicit cancellations when another delay was scheduled.
  315. emit DefaultAdminDelayChangeCanceled();
  316. }
  317. }
  318. _pendingDelay = newDelay;
  319. _pendingDelaySchedule = newSchedule;
  320. }
  321. ///
  322. /// Private helpers
  323. ///
  324. /**
  325. * @dev Defines if an `schedule` is considered set. For consistency purposes.
  326. */
  327. function _isScheduleSet(uint48 schedule) private pure returns (bool) {
  328. return schedule != 0;
  329. }
  330. /**
  331. * @dev Defines if an `schedule` is considered passed. For consistency purposes.
  332. */
  333. function _hasSchedulePassed(uint48 schedule) private view returns (bool) {
  334. return schedule < block.timestamp;
  335. }
  336. }