AccessControlDefaultAdminRules.sol 14 KB

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