AccessControl.behavior.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { bigint: 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.address, 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.address, this.defaultAdmin.address);
  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.address, 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.address, this.authorized.address);
  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.address, this.otherAdmin.address);
  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.address, this.otherAdmin.address);
  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.address, 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.address, 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.address, 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.address, 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.address);
  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. const acceptSchedule = (await time.clock.timestamp()) + this.delay;
  229. await time.forward.timestamp(acceptSchedule + 1n, false);
  230. await this.mock.connect(this.newDefaultAdmin).acceptDefaultAdminTransfer();
  231. const value = await this.mock[getter]();
  232. expect(value).to.equal(this.newDefaultAdmin.address);
  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.forward.timestamp(firstSchedule + fromSchedule);
  255. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  256. expect(newAdmin).to.equal(this.newDefaultAdmin.address);
  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.forward.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.forward.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.forward.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.address, 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.forward.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.address, acceptSchedule);
  364. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  365. expect(newAdmin).to.equal(this.newDefaultAdmin.address);
  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.forward.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.address);
  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.forward.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.forward.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.address, 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.address);
  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.forward.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.address);
  445. });
  446. describe('when caller is pending default admin and delay has passed', function () {
  447. beforeEach(async function () {
  448. await time.forward.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.address, this.newDefaultAdmin.address)
  455. .to.emit(this.mock, 'RoleGranted')
  456. .withArgs(DEFAULT_ADMIN_ROLE, this.newDefaultAdmin.address, this.newDefaultAdmin.address);
  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.address);
  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.forward.timestamp(this.acceptSchedule + fromSchedule, false);
  474. 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.address, 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.forward.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.forward.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.address);
  517. });
  518. });
  519. describe('when there is no pending default admin transfer', async 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. this.delayNotPassed = this.expectedSchedule;
  536. this.delayPassed = this.expectedSchedule + 1n;
  537. });
  538. it('reverts if caller is not default admin', async function () {
  539. await time.forward.timestamp(this.delayPassed, false);
  540. await expect(
  541. this.mock.connect(this.defaultAdmin).renounceRole(DEFAULT_ADMIN_ROLE, this.other),
  542. ).to.be.revertedWithCustomError(this.mock, 'AccessControlBadConfirmation');
  543. });
  544. it("renouncing the admin role when not an admin doesn't affect the schedule", async function () {
  545. await time.forward.timestamp(this.delayPassed, false);
  546. await this.mock.connect(this.other).renounceRole(DEFAULT_ADMIN_ROLE, this.other);
  547. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  548. expect(newAdmin).to.equal(ethers.ZeroAddress);
  549. expect(schedule).to.equal(this.expectedSchedule);
  550. });
  551. it('keeps defaultAdmin consistent with hasRole if another non-defaultAdmin user renounces the DEFAULT_ADMIN_ROLE', async function () {
  552. await time.forward.timestamp(this.delayPassed, false);
  553. // This passes because it's a noop
  554. await this.mock.connect(this.other).renounceRole(DEFAULT_ADMIN_ROLE, this.other);
  555. expect(await this.mock.hasRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin)).to.be.true;
  556. expect(await this.mock.defaultAdmin()).to.be.equal(this.defaultAdmin.address);
  557. });
  558. it('renounces role', async function () {
  559. await time.forward.timestamp(this.delayPassed, false);
  560. await expect(this.mock.connect(this.defaultAdmin).renounceRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin))
  561. .to.emit(this.mock, 'RoleRevoked')
  562. .withArgs(DEFAULT_ADMIN_ROLE, this.defaultAdmin.address, this.defaultAdmin.address);
  563. expect(await this.mock.hasRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin)).to.be.false;
  564. expect(await this.mock.defaultAdmin()).to.be.equal(ethers.ZeroAddress);
  565. expect(await this.mock.owner()).to.equal(ethers.ZeroAddress);
  566. const { newAdmin, schedule } = await this.mock.pendingDefaultAdmin();
  567. expect(newAdmin).to.equal(ethers.ZeroAddress);
  568. expect(schedule).to.equal(0);
  569. });
  570. it('allows to recover access using the internal _grantRole', async function () {
  571. await time.forward.timestamp(this.delayPassed, false);
  572. await this.mock.connect(this.defaultAdmin).renounceRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin);
  573. await expect(this.mock.connect(this.defaultAdmin).$_grantRole(DEFAULT_ADMIN_ROLE, this.other))
  574. .to.emit(this.mock, 'RoleGranted')
  575. .withArgs(DEFAULT_ADMIN_ROLE, this.other.address, this.defaultAdmin.address);
  576. });
  577. describe('schedule not passed', function () {
  578. for (const [fromSchedule, tag] of [
  579. [-1n, 'less'],
  580. [0n, 'equal'],
  581. ]) {
  582. it(`reverts if block.timestamp is ${tag} to schedule`, async function () {
  583. await time.forward.timestamp(this.delayNotPassed + fromSchedule, false);
  584. await expect(this.mock.connect(this.defaultAdmin).renounceRole(DEFAULT_ADMIN_ROLE, this.defaultAdmin))
  585. .to.be.revertedWithCustomError(this.mock, 'AccessControlEnforcedDefaultAdminDelay')
  586. .withArgs(this.expectedSchedule);
  587. });
  588. }
  589. });
  590. });
  591. describe('changes delay', function () {
  592. it('reverts if called by non default admin accounts', async function () {
  593. await expect(this.mock.connect(this.other).changeDefaultAdminDelay(time.duration.hours(4)))
  594. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  595. .withArgs(this.other.address, DEFAULT_ADMIN_ROLE);
  596. });
  597. for (const [delayDifference, delayChangeType] of [
  598. [time.duration.hours(-1), 'decreased'],
  599. [time.duration.hours(1), 'increased'],
  600. [time.duration.days(5), 'increased to more than 5 days'],
  601. ]) {
  602. describe(`when the delay is ${delayChangeType}`, function () {
  603. beforeEach(function () {
  604. this.newDefaultAdminDelay = this.delay + delayDifference;
  605. });
  606. it('begins the delay change to the new delay', async function () {
  607. // Calculate expected values
  608. const capWait = await this.mock.defaultAdminDelayIncreaseWait();
  609. const minWait = capWait < this.newDefaultAdminDelay ? capWait : this.newDefaultAdminDelay;
  610. const changeDelay =
  611. this.newDefaultAdminDelay <= this.delay ? this.delay - this.newDefaultAdminDelay : minWait;
  612. const nextBlockTimestamp = (await time.clock.timestamp()) + 1n;
  613. const effectSchedule = nextBlockTimestamp + changeDelay;
  614. await time.forward.timestamp(nextBlockTimestamp, false);
  615. // Begins the change
  616. await expect(this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(this.newDefaultAdminDelay))
  617. .to.emit(this.mock, 'DefaultAdminDelayChangeScheduled')
  618. .withArgs(this.newDefaultAdminDelay, effectSchedule);
  619. // Assert
  620. const { newDelay, schedule } = await this.mock.pendingDefaultAdminDelay();
  621. expect(newDelay).to.equal(this.newDefaultAdminDelay);
  622. expect(schedule).to.equal(effectSchedule);
  623. });
  624. describe('scheduling again', function () {
  625. beforeEach('schedule once', async function () {
  626. await this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(this.newDefaultAdminDelay);
  627. });
  628. for (const [fromSchedule, tag] of [
  629. [-1n, 'before'],
  630. [0n, 'exactly when'],
  631. [1n, 'after'],
  632. ]) {
  633. const passed = fromSchedule > 0;
  634. it(`succeeds ${tag} the delay schedule passes`, async function () {
  635. // Wait until schedule + fromSchedule
  636. const { schedule: firstSchedule } = await this.mock.pendingDefaultAdminDelay();
  637. const nextBlockTimestamp = firstSchedule + fromSchedule;
  638. await time.forward.timestamp(nextBlockTimestamp, false);
  639. // Calculate expected values
  640. const anotherNewDefaultAdminDelay = this.newDefaultAdminDelay + time.duration.hours(2);
  641. const capWait = await this.mock.defaultAdminDelayIncreaseWait();
  642. const minWait = capWait < anotherNewDefaultAdminDelay ? capWait : anotherNewDefaultAdminDelay;
  643. const effectSchedule = nextBlockTimestamp + minWait;
  644. // Default admin changes its mind and begins another delay change
  645. await expect(this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(anotherNewDefaultAdminDelay))
  646. .to.emit(this.mock, 'DefaultAdminDelayChangeScheduled')
  647. .withArgs(anotherNewDefaultAdminDelay, effectSchedule);
  648. // Assert
  649. const { newDelay, schedule } = await this.mock.pendingDefaultAdminDelay();
  650. expect(newDelay).to.equal(anotherNewDefaultAdminDelay);
  651. expect(schedule).to.equal(effectSchedule);
  652. });
  653. const emit = passed ? 'not emit' : 'emit';
  654. it(`should ${emit} a cancellation event ${tag} the delay schedule passes`, async function () {
  655. // Wait until schedule + fromSchedule
  656. const { schedule: firstSchedule } = await this.mock.pendingDefaultAdminDelay();
  657. await time.forward.timestamp(firstSchedule + fromSchedule, false);
  658. // Default admin changes its mind and begins another delay change
  659. const anotherNewDefaultAdminDelay = this.newDefaultAdminDelay + time.duration.hours(2);
  660. const expected = expect(
  661. this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(anotherNewDefaultAdminDelay),
  662. );
  663. if (passed) {
  664. await expected.to.not.emit(this.mock, 'DefaultAdminDelayChangeCanceled');
  665. } else {
  666. await expected.to.emit(this.mock, 'DefaultAdminDelayChangeCanceled');
  667. }
  668. });
  669. }
  670. });
  671. });
  672. }
  673. });
  674. describe('rollbacks a delay change', function () {
  675. it('reverts if called by non default admin accounts', async function () {
  676. await expect(this.mock.connect(this.other).rollbackDefaultAdminDelay())
  677. .to.be.revertedWithCustomError(this.mock, 'AccessControlUnauthorizedAccount')
  678. .withArgs(this.other.address, DEFAULT_ADMIN_ROLE);
  679. });
  680. describe('when there is a pending delay', function () {
  681. beforeEach('set pending delay', async function () {
  682. await this.mock.connect(this.defaultAdmin).changeDefaultAdminDelay(time.duration.hours(12));
  683. });
  684. for (const [fromSchedule, tag] of [
  685. [-1n, 'before'],
  686. [0n, 'exactly when'],
  687. [1n, 'after'],
  688. ]) {
  689. const passed = fromSchedule > 0;
  690. it(`resets pending delay and schedule ${tag} delay change schedule passes`, async function () {
  691. // Wait until schedule + fromSchedule
  692. const { schedule: firstSchedule } = await this.mock.pendingDefaultAdminDelay();
  693. await time.forward.timestamp(firstSchedule + fromSchedule, false);
  694. await this.mock.connect(this.defaultAdmin).rollbackDefaultAdminDelay();
  695. const { newDelay, schedule } = await this.mock.pendingDefaultAdminDelay();
  696. expect(newDelay).to.equal(0);
  697. expect(schedule).to.equal(0);
  698. });
  699. const emit = passed ? 'not emit' : 'emit';
  700. it(`should ${emit} a cancellation event ${tag} the delay schedule passes`, async function () {
  701. // Wait until schedule + fromSchedule
  702. const { schedule: firstSchedule } = await this.mock.pendingDefaultAdminDelay();
  703. await time.forward.timestamp(firstSchedule + fromSchedule, false);
  704. const expected = expect(this.mock.connect(this.defaultAdmin).rollbackDefaultAdminDelay());
  705. if (passed) {
  706. await expected.to.not.emit(this.mock, 'DefaultAdminDelayChangeCanceled');
  707. } else {
  708. await expected.to.emit(this.mock, 'DefaultAdminDelayChangeCanceled');
  709. }
  710. });
  711. }
  712. });
  713. describe('when there is no pending delay', function () {
  714. it('succeeds without changes', async function () {
  715. await this.mock.connect(this.defaultAdmin).rollbackDefaultAdminDelay();
  716. const { newDelay, schedule } = await this.mock.pendingDefaultAdminDelay();
  717. expect(newDelay).to.equal(0);
  718. expect(schedule).to.equal(0);
  719. });
  720. });
  721. });
  722. }
  723. module.exports = {
  724. DEFAULT_ADMIN_ROLE,
  725. shouldBehaveLikeAccessControl,
  726. shouldBehaveLikeAccessControlEnumerable,
  727. shouldBehaveLikeAccessControlDefaultAdminRules,
  728. };