AccessControl.behavior.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const time = require('../helpers/time');
  4. const { shouldSupportInterfaces } = require('../utils/introspection/SupportsInterface.behavior');
  5. const DEFAULT_ADMIN_ROLE = ethers.ZeroHash;
  6. const ROLE = ethers.id('ROLE');
  7. const OTHER_ROLE = ethers.id('OTHER_ROLE');
  8. function shouldBehaveLikeAccessControl() {
  9. beforeEach(async function () {
  10. [this.authorized, this.other, this.otherAdmin] = this.accounts;
  11. });
  12. shouldSupportInterfaces(['AccessControl']);
  13. describe('default admin', function () {
  14. it('deployer has default admin role', async function () {
  15. expect(await this.mock.hasRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin)).to.be.true;
  16. });
  17. it("other roles's admin is the default admin role", async function () {
  18. expect(await this.mock.getRoleAdmin(ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  19. });
  20. it("default admin role's admin is itself", async function () {
  21. expect(await this.mock.getRoleAdmin(DEFAULT_ADMIN_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  22. });
  23. });
  24. describe('granting', function () {
  25. beforeEach(async function () {
  26. await this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.authorized);
  27. });
  28. it('non-admin cannot grant role to other accounts', async function () {
  29. await expect(this.mock.connect(this.other).grantRole(ROLE, this.authorized))
  30. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  31. .withArgs(this.other, DEFAULT_ADMIN_ROLE);
  32. });
  33. it('accounts can be granted a role multiple times', async function () {
  34. await this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.authorized);
  35. expect(this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.authorized)).to.not.emit(
  36. this.mock,
  37. 'RoleGranted',
  38. );
  39. });
  40. });
  41. describe('revoking', function () {
  42. it('roles that are not had can be revoked', async function () {
  43. expect(await this.mock.hasRole(ROLE, this.authorized)).to.be.false;
  44. await expect(this.mock.connect(this.defaultAdmin).revokeRole(ROLE, this.authorized)).to.not.emit(
  45. this.mock,
  46. 'RoleRevoked',
  47. );
  48. });
  49. describe('with granted role', function () {
  50. beforeEach(async function () {
  51. await this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.authorized);
  52. });
  53. it('admin can revoke role', async function () {
  54. await expect(this.mock.connect(this.defaultAdmin).revokeRole(ROLE, this.authorized))
  55. .to.emit(this.mock, 'RoleRevoked')
  56. .withArgs(ROLE, this.authorized, this.defaultAdmin);
  57. expect(await this.mock.hasRole(ROLE, this.authorized)).to.be.false;
  58. });
  59. it('non-admin cannot revoke role', async function () {
  60. await expect(this.mock.connect(this.other).revokeRole(ROLE, this.authorized))
  61. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  62. .withArgs(this.other, DEFAULT_ADMIN_ROLE);
  63. });
  64. it('a role can be revoked multiple times', async function () {
  65. await this.mock.connect(this.defaultAdmin).revokeRole(ROLE, this.authorized);
  66. expect(this.mock.connect(this.defaultAdmin).revokeRole(ROLE, this.authorized)).to.not.emit(
  67. this.mock,
  68. 'RoleRevoked',
  69. );
  70. });
  71. });
  72. });
  73. describe('renouncing', function () {
  74. it('roles that are not had can be renounced', async function () {
  75. await expect(this.mock.connect(this.authorized).renounceRole(ROLE, this.authorized)).to.not.emit(
  76. this.mock,
  77. 'RoleRevoked',
  78. );
  79. });
  80. describe('with granted role', function () {
  81. beforeEach(async function () {
  82. await this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.authorized);
  83. });
  84. it('bearer can renounce role', async function () {
  85. await expect(this.mock.connect(this.authorized).renounceRole(ROLE, this.authorized))
  86. .to.emit(this.mock, 'RoleRevoked')
  87. .withArgs(ROLE, this.authorized, this.authorized);
  88. expect(await this.mock.hasRole(ROLE, this.authorized)).to.be.false;
  89. });
  90. it('only the sender can renounce their roles', async function () {
  91. expect(this.mock.connect(this.defaultAdmin).renounceRole(ROLE, this.authorized)).to.be.revertedWithCustomError(
  92. this.mock,
  93. 'AccessControlBadConfirmation',
  94. );
  95. });
  96. it('a role can be renounced multiple times', async function () {
  97. await this.mock.connect(this.authorized).renounceRole(ROLE, this.authorized);
  98. await expect(this.mock.connect(this.authorized).renounceRole(ROLE, this.authorized)).not.to.emit(
  99. this.mock,
  100. 'RoleRevoked',
  101. );
  102. });
  103. });
  104. });
  105. describe('setting role admin', function () {
  106. beforeEach(async function () {
  107. await expect(this.mock.$_setRoleAdmin(ROLE, OTHER_ROLE))
  108. .to.emit(this.mock, 'RoleAdminChanged')
  109. .withArgs(ROLE, DEFAULT_ADMIN_ROLE, OTHER_ROLE);
  110. await this.mock.connect(this.defaultAdmin).grantRole(OTHER_ROLE, this.otherAdmin);
  111. });
  112. it("a role's admin role can be changed", async function () {
  113. expect(await this.mock.getRoleAdmin(ROLE)).to.equal(OTHER_ROLE);
  114. });
  115. it('the new admin can grant roles', async function () {
  116. await expect(this.mock.connect(this.otherAdmin).grantRole(ROLE, this.authorized))
  117. .to.emit(this.mock, 'RoleGranted')
  118. .withArgs(ROLE, this.authorized, this.otherAdmin);
  119. });
  120. it('the new admin can revoke roles', async function () {
  121. await this.mock.connect(this.otherAdmin).grantRole(ROLE, this.authorized);
  122. await expect(this.mock.connect(this.otherAdmin).revokeRole(ROLE, this.authorized))
  123. .to.emit(this.mock, 'RoleRevoked')
  124. .withArgs(ROLE, this.authorized, this.otherAdmin);
  125. });
  126. it("a role's previous admins no longer grant roles", async function () {
  127. await expect(this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.authorized))
  128. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  129. .withArgs(this.defaultAdmin, OTHER_ROLE);
  130. });
  131. it("a role's previous admins no longer revoke roles", async function () {
  132. await expect(this.mock.connect(this.defaultAdmin).revokeRole(ROLE, this.authorized))
  133. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  134. .withArgs(this.defaultAdmin, OTHER_ROLE);
  135. });
  136. });
  137. describe('onlyRole modifier', function () {
  138. beforeEach(async function () {
  139. await this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.authorized);
  140. });
  141. it('do not revert if sender has role', async function () {
  142. await this.mock.connect(this.authorized).$_checkRole(ROLE);
  143. });
  144. it("revert if sender doesn't have role #1", async function () {
  145. await expect(this.mock.connect(this.other).$_checkRole(ROLE))
  146. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  147. .withArgs(this.other, ROLE);
  148. });
  149. it("revert if sender doesn't have role #2", async function () {
  150. await expect(this.mock.connect(this.authorized).$_checkRole(OTHER_ROLE))
  151. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  152. .withArgs(this.authorized, OTHER_ROLE);
  153. });
  154. });
  155. describe('internal functions', function () {
  156. describe('_grantRole', function () {
  157. it('return true if the account does not have the role', async function () {
  158. await expect(this.mock.$_grantRole(ROLE, this.authorized))
  159. .to.emit(this.mock, 'return$_grantRole')
  160. .withArgs(true);
  161. });
  162. it('return false if the account has the role', async function () {
  163. await this.mock.$_grantRole(ROLE, this.authorized);
  164. await expect(this.mock.$_grantRole(ROLE, this.authorized))
  165. .to.emit(this.mock, 'return$_grantRole')
  166. .withArgs(false);
  167. });
  168. });
  169. describe('_revokeRole', function () {
  170. it('return true if the account has the role', async function () {
  171. await this.mock.$_grantRole(ROLE, this.authorized);
  172. await expect(this.mock.$_revokeRole(ROLE, this.authorized))
  173. .to.emit(this.mock, 'return$_revokeRole')
  174. .withArgs(true);
  175. });
  176. it('return false if the account does not have the role', async function () {
  177. await expect(this.mock.$_revokeRole(ROLE, this.authorized))
  178. .to.emit(this.mock, 'return$_revokeRole')
  179. .withArgs(false);
  180. });
  181. });
  182. });
  183. }
  184. function shouldBehaveLikeAccessControlEnumerable() {
  185. beforeEach(async function () {
  186. [this.authorized, this.other, this.otherAdmin, this.otherAuthorized] = this.accounts;
  187. });
  188. shouldSupportInterfaces(['AccessControlEnumerable']);
  189. describe('enumerating', function () {
  190. it('role bearers can be enumerated', async function () {
  191. await this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.authorized);
  192. await this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.other);
  193. await this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.otherAuthorized);
  194. await this.mock.connect(this.defaultAdmin).revokeRole(ROLE, this.other);
  195. const expectedMembers = [this.authorized.address, this.otherAuthorized.address];
  196. const memberCount = await this.mock.getRoleMemberCount(ROLE);
  197. const members = [];
  198. for (let i = 0; i < memberCount; ++i) {
  199. members.push(await this.mock.getRoleMember(ROLE, i));
  200. }
  201. expect(memberCount).to.equal(expectedMembers.length);
  202. expect(members).to.deep.equal(expectedMembers);
  203. expect(await this.mock.getRoleMembers(ROLE)).to.deep.equal(expectedMembers);
  204. });
  205. it('role enumeration should be in sync after renounceRole call', async function () {
  206. expect(await this.mock.getRoleMemberCount(ROLE)).to.equal(0);
  207. await this.mock.connect(this.defaultAdmin).grantRole(ROLE, this.defaultAdmin);
  208. expect(await this.mock.getRoleMemberCount(ROLE)).to.equal(1);
  209. await this.mock.connect(this.defaultAdmin).renounceRole(ROLE, this.defaultAdmin);
  210. expect(await this.mock.getRoleMemberCount(ROLE)).to.equal(0);
  211. });
  212. });
  213. }
  214. function shouldBehaveLikeAccessControlDefaultAdminRules() {
  215. shouldSupportInterfaces(['AccessControlDefaultAdminRules']);
  216. beforeEach(async function () {
  217. [this.newDefaultAdmin, this.other] = this.accounts;
  218. });
  219. for (const getter of ['owner', 'defaultAdmin']) {
  220. describe(`${getter}()`, function () {
  221. it('has a default set to the initial default admin', async function () {
  222. const value = await this.mock[getter]();
  223. expect(value).to.equal(this.defaultAdmin);
  224. expect(await this.mock.hasRole(DEFAULT_ADMIN_ROLE, value)).to.be.true;
  225. });
  226. it('changes if the default admin changes', async function () {
  227. // Starts an admin transfer
  228. await this.mock.connect(this.defaultAdmin).beginDefaultAdminTransfer(this.newDefaultAdmin);
  229. // Wait for acceptance
  230. await time.increaseBy.timestamp(this.delay + 1n, false);
  231. await this.mock.connect(this.newDefaultAdmin).acceptDefaultAdminTransfer();
  232. const value = await this.mock[getter]();
  233. expect(value).to.equal(this.newDefaultAdmin);
  234. });
  235. });
  236. }
  237. describe('pendingDefaultAdmin()', function () {
  238. it('returns 0 if no pending default admin transfer', async function () {
  239. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  240. expect(newAdmin).to.equal(ethers.ZeroAddress);
  241. expect(schedule).to.equal(0);
  242. });
  243. describe('when there is a scheduled default admin transfer', function () {
  244. beforeEach('begins admin transfer', async function () {
  245. await this.mock.connect(this.defaultAdmin).beginDefaultAdminTransfer(this.newDefaultAdmin);
  246. });
  247. for (const [fromSchedule, tag] of [
  248. [-1n, 'before'],
  249. [0n, 'exactly when'],
  250. [1n, 'after'],
  251. ]) {
  252. it(`returns pending admin and schedule ${tag} it passes if not accepted`, async function () {
  253. // Wait until schedule + fromSchedule
  254. const { schedule: firstSchedule } = await this.mock.pendingDefaultAdmin();
  255. await time.increaseTo.timestamp(firstSchedule + fromSchedule);
  256. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  257. expect(newAdmin).to.equal(this.newDefaultAdmin);
  258. expect(schedule).to.equal(firstSchedule);
  259. });
  260. }
  261. it('returns 0 after schedule passes and the transfer was accepted', async function () {
  262. // Wait after schedule
  263. const { schedule: firstSchedule } = await this.mock.pendingDefaultAdmin();
  264. await time.increaseTo.timestamp(firstSchedule + 1n, false);
  265. // Accepts
  266. await this.mock.connect(this.newDefaultAdmin).acceptDefaultAdminTransfer();
  267. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  268. expect(newAdmin).to.equal(ethers.ZeroAddress);
  269. expect(schedule).to.equal(0);
  270. });
  271. });
  272. });
  273. describe('defaultAdminDelay()', function () {
  274. it('returns the current delay', async function () {
  275. expect(await this.mock.defaultAdminDelay()).to.equal(this.delay);
  276. });
  277. describe('when there is a scheduled delay change', function () {
  278. const newDelay = 0x1337n; // Any change
  279. beforeEach('begins delay change', async function () {
  280. await this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(newDelay);
  281. });
  282. for (const [fromSchedule, tag, expectNew, delayTag] of [
  283. [-1n, 'before', false, 'old'],
  284. [0n, 'exactly when', false, 'old'],
  285. [1n, 'after', true, 'new'],
  286. ]) {
  287. it(`returns ${delayTag} delay ${tag} delay schedule passes`, async function () {
  288. // Wait until schedule + fromSchedule
  289. const { schedule } = await this.mock.pendingDefaultAdminDelay();
  290. await time.increaseTo.timestamp(schedule + fromSchedule);
  291. const currentDelay = await this.mock.defaultAdminDelay();
  292. expect(currentDelay).to.equal(expectNew ? newDelay : this.delay);
  293. });
  294. }
  295. });
  296. });
  297. describe('pendingDefaultAdminDelay()', function () {
  298. it('returns 0 if not set', async function () {
  299. const { newDelay, schedule } = await this.mock.pendingDefaultAdminDelay();
  300. expect(newDelay).to.equal(0);
  301. expect(schedule).to.equal(0);
  302. });
  303. describe('when there is a scheduled delay change', function () {
  304. const newDelay = 0x1337n; // Any change
  305. beforeEach('begins admin transfer', async function () {
  306. await this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(newDelay);
  307. });
  308. for (const [fromSchedule, tag, expectedDelay, delayTag, expectZeroSchedule] of [
  309. [-1n, 'before', newDelay, 'new'],
  310. [0n, 'exactly when', newDelay, 'new'],
  311. [1n, 'after', 0, 'zero', true],
  312. ]) {
  313. it(`returns ${delayTag} delay ${tag} delay schedule passes`, async function () {
  314. // Wait until schedule + fromSchedule
  315. const { schedule: firstSchedule } = await this.mock.pendingDefaultAdminDelay();
  316. await time.increaseTo.timestamp(firstSchedule + fromSchedule);
  317. const { newDelay, schedule } = await this.mock.pendingDefaultAdminDelay();
  318. expect(newDelay).to.equal(expectedDelay);
  319. expect(schedule).to.equal(expectZeroSchedule ? 0 : firstSchedule);
  320. });
  321. }
  322. });
  323. });
  324. describe('defaultAdminDelayIncreaseWait()', function () {
  325. it('should return 5 days (default)', async function () {
  326. expect(await this.mock.defaultAdminDelayIncreaseWait()).to.equal(time.duration.days(5));
  327. });
  328. });
  329. it('should revert if granting default admin role', async function () {
  330. await expect(
  331. this.mock.connect(this.defaultAdmin).grantRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin),
  332. ).to.be.revertedWithCustomError(this.mock, 'AccessControlEnforcedDefaultAdminRules');
  333. });
  334. it('should revert if revoking default admin role', async function () {
  335. await expect(
  336. this.mock.connect(this.defaultAdmin).revokeRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin),
  337. ).to.be.revertedWithCustomError(this.mock, 'AccessControlEnforcedDefaultAdminRules');
  338. });
  339. it("should revert if defaultAdmin's admin is changed", async function () {
  340. await expect(this.mock.$_setRoleAdmin(DEFAULT_ADMIN_ROLE, OTHER_ROLE)).to.be.revertedWithCustomError(
  341. this.mock,
  342. 'AccessControlEnforcedDefaultAdminRules',
  343. );
  344. });
  345. it('should not grant the default admin role twice', async function () {
  346. await expect(this.mock.$_grantRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin)).to.be.revertedWithCustomError(
  347. this.mock,
  348. 'AccessControlEnforcedDefaultAdminRules',
  349. );
  350. });
  351. describe('begins a default admin transfer', function () {
  352. it('reverts if called by non default admin accounts', async function () {
  353. await expect(this.mock.connect(this.other).beginDefaultAdminTransfer(this.newDefaultAdmin))
  354. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  355. .withArgs(this.other, DEFAULT_ADMIN_ROLE);
  356. });
  357. describe('when there is no pending delay nor pending admin transfer', function () {
  358. it('should set pending default admin and schedule', async function () {
  359. const nextBlockTimestamp = (await time.clock.timestamp()) + 1n;
  360. const acceptSchedule = nextBlockTimestamp + this.delay;
  361. await time.increaseTo.timestamp(nextBlockTimestamp, false); // set timestamp but don't mine the block yet
  362. await expect(this.mock.connect(this.defaultAdmin).beginDefaultAdminTransfer(this.newDefaultAdmin))
  363. .to.emit(this.mock, 'DefaultAdminTransferScheduled')
  364. .withArgs(this.newDefaultAdmin, acceptSchedule);
  365. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  366. expect(newAdmin).to.equal(this.newDefaultAdmin);
  367. expect(schedule).to.equal(acceptSchedule);
  368. });
  369. });
  370. describe('when there is a pending admin transfer', function () {
  371. beforeEach('sets a pending default admin transfer', async function () {
  372. await this.mock.connect(this.defaultAdmin).beginDefaultAdminTransfer(this.newDefaultAdmin);
  373. this.acceptSchedule = (await time.clock.timestamp()) + this.delay;
  374. });
  375. for (const [fromSchedule, tag] of [
  376. [-1n, 'before'],
  377. [0n, 'exactly when'],
  378. [1n, 'after'],
  379. ]) {
  380. it(`should be able to begin a transfer again ${tag} acceptSchedule passes`, async function () {
  381. // Wait until schedule + fromSchedule
  382. await time.increaseTo.timestamp(this.acceptSchedule + fromSchedule, false);
  383. // defaultAdmin changes its mind and begin again to another address
  384. await expect(this.mock.connect(this.defaultAdmin).beginDefaultAdminTransfer(this.other)).to.emit(
  385. this.mock,
  386. 'DefaultAdminTransferCanceled', // Cancellation is always emitted since it was never accepted
  387. );
  388. const newSchedule = (await time.clock.timestamp()) + this.delay;
  389. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  390. expect(newAdmin).to.equal(this.other);
  391. expect(schedule).to.equal(newSchedule);
  392. });
  393. }
  394. it('should not emit a cancellation event if the new default admin accepted', async function () {
  395. // Wait until the acceptSchedule has passed
  396. await time.increaseTo.timestamp(this.acceptSchedule + 1n, false);
  397. // Accept and restart
  398. await this.mock.connect(this.newDefaultAdmin).acceptDefaultAdminTransfer();
  399. await expect(this.mock.connect(this.newDefaultAdmin).beginDefaultAdminTransfer(this.other)).to.not.emit(
  400. this.mock,
  401. 'DefaultAdminTransferCanceled',
  402. );
  403. });
  404. });
  405. describe('when there is a pending delay', function () {
  406. const newDelay = time.duration.hours(3);
  407. beforeEach('schedule a delay change', async function () {
  408. await this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(newDelay);
  409. ({ schedule: this.effectSchedule } = await this.mock.pendingDefaultAdminDelay());
  410. });
  411. for (const [fromSchedule, schedulePassed, expectNewDelay] of [
  412. [-1n, 'before', false],
  413. [0n, 'exactly when', false],
  414. [1n, 'after', true],
  415. ]) {
  416. it(`should set the ${
  417. expectNewDelay ? 'new' : 'old'
  418. } delay and apply it to next default admin transfer schedule ${schedulePassed} effectSchedule passed`, async function () {
  419. // Wait until the expected fromSchedule time
  420. const nextBlockTimestamp = this.effectSchedule + fromSchedule;
  421. await time.increaseTo.timestamp(nextBlockTimestamp, false);
  422. // Start the new default admin transfer and get its schedule
  423. const expectedDelay = expectNewDelay ? newDelay : this.delay;
  424. const expectedAcceptSchedule = nextBlockTimestamp + expectedDelay;
  425. await expect(this.mock.connect(this.defaultAdmin).beginDefaultAdminTransfer(this.newDefaultAdmin))
  426. .to.emit(this.mock, 'DefaultAdminTransferScheduled')
  427. .withArgs(this.newDefaultAdmin, expectedAcceptSchedule);
  428. // Check that the schedule corresponds with the new delay
  429. const { newAdmin, schedule: transferSchedule } = await this.mock.pendingDefaultAdmin();
  430. expect(newAdmin).to.equal(this.newDefaultAdmin);
  431. expect(transferSchedule).to.equal(expectedAcceptSchedule);
  432. });
  433. }
  434. });
  435. });
  436. describe('accepts transfer admin', function () {
  437. beforeEach(async function () {
  438. await this.mock.connect(this.defaultAdmin).beginDefaultAdminTransfer(this.newDefaultAdmin);
  439. this.acceptSchedule = (await time.clock.timestamp()) + this.delay;
  440. });
  441. it('should revert if caller is not pending default admin', async function () {
  442. await time.increaseTo.timestamp(this.acceptSchedule + 1n, false);
  443. await expect(this.mock.connect(this.other).acceptDefaultAdminTransfer())
  444. .to.be.revertedWithCustomError(this.mock, 'AccessControlInvalidDefaultAdmin')
  445. .withArgs(this.other);
  446. });
  447. describe('when caller is pending default admin and delay has passed', function () {
  448. beforeEach(async function () {
  449. await time.increaseTo.timestamp(this.acceptSchedule + 1n, false);
  450. });
  451. it('accepts a transfer and changes default admin', async function () {
  452. // Emit events
  453. await expect(this.mock.connect(this.newDefaultAdmin).acceptDefaultAdminTransfer())
  454. .to.emit(this.mock, 'RoleRevoked')
  455. .withArgs(DEFAULT_ADMIN_ROLE, this.defaultAdmin, this.newDefaultAdmin)
  456. .to.emit(this.mock, 'RoleGranted')
  457. .withArgs(DEFAULT_ADMIN_ROLE, this.newDefaultAdmin, this.newDefaultAdmin);
  458. // Storage changes
  459. expect(await this.mock.hasRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin)).to.be.false;
  460. expect(await this.mock.hasRole(DEFAULT_ADMIN_ROLE, this.newDefaultAdmin)).to.be.true;
  461. expect(await this.mock.owner()).to.equal(this.newDefaultAdmin);
  462. // Resets pending default admin and schedule
  463. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  464. expect(newAdmin).to.equal(ethers.ZeroAddress);
  465. expect(schedule).to.equal(0);
  466. });
  467. });
  468. describe('schedule not passed', function () {
  469. for (const [fromSchedule, tag] of [
  470. [-1n, 'less'],
  471. [0n, 'equal'],
  472. ]) {
  473. it(`should revert if block.timestamp is ${tag} to schedule`, async function () {
  474. await time.increaseTo.timestamp(this.acceptSchedule + fromSchedule, false);
  475. expect(this.mock.connect(this.newDefaultAdmin).acceptDefaultAdminTransfer())
  476. .to.be.revertedWithCustomError(this.mock, 'AccessControlEnforcedDefaultAdminDelay')
  477. .withArgs(this.acceptSchedule);
  478. });
  479. }
  480. });
  481. });
  482. describe('cancels a default admin transfer', function () {
  483. it('reverts if called by non default admin accounts', async function () {
  484. await expect(this.mock.connect(this.other).cancelDefaultAdminTransfer())
  485. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  486. .withArgs(this.other, DEFAULT_ADMIN_ROLE);
  487. });
  488. describe('when there is a pending default admin transfer', function () {
  489. beforeEach(async function () {
  490. await this.mock.connect(this.defaultAdmin).beginDefaultAdminTransfer(this.newDefaultAdmin);
  491. this.acceptSchedule = (await time.clock.timestamp()) + this.delay;
  492. });
  493. for (const [fromSchedule, tag] of [
  494. [-1n, 'before'],
  495. [0n, 'exactly when'],
  496. [1n, 'after'],
  497. ]) {
  498. it(`resets pending default admin and schedule ${tag} transfer schedule passes`, async function () {
  499. // Advance until passed delay
  500. await time.increaseTo.timestamp(this.acceptSchedule + fromSchedule, false);
  501. await expect(this.mock.connect(this.defaultAdmin).cancelDefaultAdminTransfer()).to.emit(
  502. this.mock,
  503. 'DefaultAdminTransferCanceled',
  504. );
  505. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  506. expect(newAdmin).to.equal(ethers.ZeroAddress);
  507. expect(schedule).to.equal(0);
  508. });
  509. }
  510. it('should revert if the previous default admin tries to accept', async function () {
  511. await this.mock.connect(this.defaultAdmin).cancelDefaultAdminTransfer();
  512. // Advance until passed delay
  513. await time.increaseTo.timestamp(this.acceptSchedule + 1n, false);
  514. // Previous pending default admin should not be able to accept after cancellation.
  515. await expect(this.mock.connect(this.newDefaultAdmin).acceptDefaultAdminTransfer())
  516. .to.be.revertedWithCustomError(this.mock, 'AccessControlInvalidDefaultAdmin')
  517. .withArgs(this.newDefaultAdmin);
  518. });
  519. });
  520. describe('when there is no pending default admin transfer', async function () {
  521. it('should succeed without changes', async function () {
  522. await expect(this.mock.connect(this.defaultAdmin).cancelDefaultAdminTransfer()).to.not.emit(
  523. this.mock,
  524. 'DefaultAdminTransferCanceled',
  525. );
  526. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  527. expect(newAdmin).to.equal(ethers.ZeroAddress);
  528. expect(schedule).to.equal(0);
  529. });
  530. });
  531. });
  532. describe('renounces admin', function () {
  533. beforeEach(async function () {
  534. await this.mock.connect(this.defaultAdmin).beginDefaultAdminTransfer(ethers.ZeroAddress);
  535. this.expectedSchedule = (await time.clock.timestamp()) + this.delay;
  536. });
  537. it('reverts if caller is not default admin', async function () {
  538. await time.increaseBy.timestamp(this.delay + 1n, false);
  539. await expect(
  540. this.mock.connect(this.defaultAdmin).renounceRole(DEFAULT_ADMIN_ROLE, this.other),
  541. ).to.be.revertedWithCustomError(this.mock, 'AccessControlBadConfirmation');
  542. });
  543. it("renouncing the admin role when not an admin doesn't affect the schedule", async function () {
  544. await time.increaseBy.timestamp(this.delay + 1n, false);
  545. await this.mock.connect(this.other).renounceRole(DEFAULT_ADMIN_ROLE, this.other);
  546. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  547. expect(newAdmin).to.equal(ethers.ZeroAddress);
  548. expect(schedule).to.equal(this.expectedSchedule);
  549. });
  550. it('keeps defaultAdmin consistent with hasRole if another non-defaultAdmin user renounces the DEFAULT_ADMIN_ROLE', async function () {
  551. await time.increaseBy.timestamp(this.delay + 1n, false);
  552. // This passes because it's a noop
  553. await this.mock.connect(this.other).renounceRole(DEFAULT_ADMIN_ROLE, this.other);
  554. expect(await this.mock.hasRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin)).to.be.true;
  555. expect(await this.mock.defaultAdmin()).to.equal(this.defaultAdmin);
  556. });
  557. it('renounces role', async function () {
  558. await time.increaseBy.timestamp(this.delay + 1n, false);
  559. await expect(this.mock.connect(this.defaultAdmin).renounceRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin))
  560. .to.emit(this.mock, 'RoleRevoked')
  561. .withArgs(DEFAULT_ADMIN_ROLE, this.defaultAdmin, this.defaultAdmin);
  562. expect(await this.mock.hasRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin)).to.be.false;
  563. expect(await this.mock.defaultAdmin()).to.equal(ethers.ZeroAddress);
  564. expect(await this.mock.owner()).to.equal(ethers.ZeroAddress);
  565. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  566. expect(newAdmin).to.equal(ethers.ZeroAddress);
  567. expect(schedule).to.equal(0);
  568. });
  569. it('allows to recover access using the internal _grantRole', async function () {
  570. await time.increaseBy.timestamp(this.delay + 1n, false);
  571. await this.mock.connect(this.defaultAdmin).renounceRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin);
  572. await expect(this.mock.connect(this.defaultAdmin).$_grantRole(DEFAULT_ADMIN_ROLE, this.other))
  573. .to.emit(this.mock, 'RoleGranted')
  574. .withArgs(DEFAULT_ADMIN_ROLE, this.other, this.defaultAdmin);
  575. });
  576. describe('schedule not passed', function () {
  577. for (const [fromSchedule, tag] of [
  578. [-1n, 'less'],
  579. [0n, 'equal'],
  580. ]) {
  581. it(`reverts if block.timestamp is ${tag} to schedule`, async function () {
  582. await time.increaseBy.timestamp(this.delay + fromSchedule, false);
  583. await expect(this.mock.connect(this.defaultAdmin).renounceRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin))
  584. .to.be.revertedWithCustomError(this.mock, 'AccessControlEnforcedDefaultAdminDelay')
  585. .withArgs(this.expectedSchedule);
  586. });
  587. }
  588. });
  589. });
  590. describe('changes delay', function () {
  591. it('reverts if called by non default admin accounts', async function () {
  592. await expect(this.mock.connect(this.other).changeDefaultAdminDelay(time.duration.hours(4)))
  593. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  594. .withArgs(this.other, DEFAULT_ADMIN_ROLE);
  595. });
  596. for (const [delayDifference, delayChangeType] of [
  597. [time.duration.hours(-1), 'decreased'],
  598. [time.duration.hours(1), 'increased'],
  599. [time.duration.days(5), 'increased to more than 5 days'],
  600. ]) {
  601. describe(`when the delay is ${delayChangeType}`, function () {
  602. beforeEach(function () {
  603. this.newDefaultAdminDelay = this.delay + delayDifference;
  604. });
  605. it('begins the delay change to the new delay', async function () {
  606. // Calculate expected values
  607. const capWait = await this.mock.defaultAdminDelayIncreaseWait();
  608. const minWait = capWait < this.newDefaultAdminDelay ? capWait : this.newDefaultAdminDelay;
  609. const changeDelay =
  610. this.newDefaultAdminDelay <= this.delay ? this.delay - this.newDefaultAdminDelay : minWait;
  611. const nextBlockTimestamp = (await time.clock.timestamp()) + 1n;
  612. const effectSchedule = nextBlockTimestamp + changeDelay;
  613. await time.increaseTo.timestamp(nextBlockTimestamp, false);
  614. // Begins the change
  615. await expect(this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(this.newDefaultAdminDelay))
  616. .to.emit(this.mock, 'DefaultAdminDelayChangeScheduled')
  617. .withArgs(this.newDefaultAdminDelay, effectSchedule);
  618. // Assert
  619. const { newDelay, schedule } = await this.mock.pendingDefaultAdminDelay();
  620. expect(newDelay).to.equal(this.newDefaultAdminDelay);
  621. expect(schedule).to.equal(effectSchedule);
  622. });
  623. describe('scheduling again', function () {
  624. beforeEach('schedule once', async function () {
  625. await this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(this.newDefaultAdminDelay);
  626. });
  627. for (const [fromSchedule, tag] of [
  628. [-1n, 'before'],
  629. [0n, 'exactly when'],
  630. [1n, 'after'],
  631. ]) {
  632. const passed = fromSchedule > 0;
  633. it(`succeeds ${tag} the delay schedule passes`, async function () {
  634. // Wait until schedule + fromSchedule
  635. const { schedule: firstSchedule } = await this.mock.pendingDefaultAdminDelay();
  636. const nextBlockTimestamp = firstSchedule + fromSchedule;
  637. await time.increaseTo.timestamp(nextBlockTimestamp, false);
  638. // Calculate expected values
  639. const anotherNewDefaultAdminDelay = this.newDefaultAdminDelay + time.duration.hours(2);
  640. const capWait = await this.mock.defaultAdminDelayIncreaseWait();
  641. const minWait = capWait < anotherNewDefaultAdminDelay ? capWait : anotherNewDefaultAdminDelay;
  642. const effectSchedule = nextBlockTimestamp + minWait;
  643. // Default admin changes its mind and begins another delay change
  644. await expect(this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(anotherNewDefaultAdminDelay))
  645. .to.emit(this.mock, 'DefaultAdminDelayChangeScheduled')
  646. .withArgs(anotherNewDefaultAdminDelay, effectSchedule);
  647. // Assert
  648. const { newDelay, schedule } = await this.mock.pendingDefaultAdminDelay();
  649. expect(newDelay).to.equal(anotherNewDefaultAdminDelay);
  650. expect(schedule).to.equal(effectSchedule);
  651. });
  652. const emit = passed ? 'not emit' : 'emit';
  653. it(`should ${emit} a cancellation event ${tag} the delay schedule passes`, async function () {
  654. // Wait until schedule + fromSchedule
  655. const { schedule: firstSchedule } = await this.mock.pendingDefaultAdminDelay();
  656. await time.increaseTo.timestamp(firstSchedule + fromSchedule, false);
  657. // Default admin changes its mind and begins another delay change
  658. const anotherNewDefaultAdminDelay = this.newDefaultAdminDelay + time.duration.hours(2);
  659. const expected = expect(
  660. this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(anotherNewDefaultAdminDelay),
  661. );
  662. if (passed) {
  663. await expected.to.not.emit(this.mock, 'DefaultAdminDelayChangeCanceled');
  664. } else {
  665. await expected.to.emit(this.mock, 'DefaultAdminDelayChangeCanceled');
  666. }
  667. });
  668. }
  669. });
  670. });
  671. }
  672. });
  673. describe('rollbacks a delay change', function () {
  674. it('reverts if called by non default admin accounts', async function () {
  675. await expect(this.mock.connect(this.other).rollbackDefaultAdminDelay())
  676. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  677. .withArgs(this.other, DEFAULT_ADMIN_ROLE);
  678. });
  679. describe('when there is a pending delay', function () {
  680. beforeEach('set pending delay', async function () {
  681. await this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(time.duration.hours(12));
  682. });
  683. for (const [fromSchedule, tag] of [
  684. [-1n, 'before'],
  685. [0n, 'exactly when'],
  686. [1n, 'after'],
  687. ]) {
  688. const passed = fromSchedule > 0;
  689. it(`resets pending delay and schedule ${tag} delay change schedule passes`, async function () {
  690. // Wait until schedule + fromSchedule
  691. const { schedule: firstSchedule } = await this.mock.pendingDefaultAdminDelay();
  692. await time.increaseTo.timestamp(firstSchedule + fromSchedule, false);
  693. await this.mock.connect(this.defaultAdmin).rollbackDefaultAdminDelay();
  694. const { newDelay, schedule } = await this.mock.pendingDefaultAdminDelay();
  695. expect(newDelay).to.equal(0);
  696. expect(schedule).to.equal(0);
  697. });
  698. const emit = passed ? 'not emit' : 'emit';
  699. it(`should ${emit} a cancellation event ${tag} the delay schedule passes`, async function () {
  700. // Wait until schedule + fromSchedule
  701. const { schedule: firstSchedule } = await this.mock.pendingDefaultAdminDelay();
  702. await time.increaseTo.timestamp(firstSchedule + fromSchedule, false);
  703. const expected = expect(this.mock.connect(this.defaultAdmin).rollbackDefaultAdminDelay());
  704. if (passed) {
  705. await expected.to.not.emit(this.mock, 'DefaultAdminDelayChangeCanceled');
  706. } else {
  707. await expected.to.emit(this.mock, 'DefaultAdminDelayChangeCanceled');
  708. }
  709. });
  710. }
  711. });
  712. describe('when there is no pending delay', function () {
  713. it('succeeds without changes', async function () {
  714. await this.mock.connect(this.defaultAdmin).rollbackDefaultAdminDelay();
  715. const { newDelay, schedule } = await this.mock.pendingDefaultAdminDelay();
  716. expect(newDelay).to.equal(0);
  717. expect(schedule).to.equal(0);
  718. });
  719. });
  720. });
  721. }
  722. module.exports = {
  723. DEFAULT_ADMIN_ROLE,
  724. shouldBehaveLikeAccessControl,
  725. shouldBehaveLikeAccessControlEnumerable,
  726. shouldBehaveLikeAccessControlDefaultAdminRules,
  727. };