AccessControl.behavior.js 37 KB

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