AccessControl.behavior.js 37 KB

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