AccessControlDefaultAdminRules.sol 14 KB

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