AccessControl.behavior.js 36 KB

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