AccessControl.behavior.js 36 KB

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