AccessControl.behavior.js 36 KB

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