AccessControl.behavior.js 36 KB

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