AccessControl.behavior.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 delay ${tag} delay schedule 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 delay 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. let from;
  427. beforeEach(async function () {
  428. await time.setNextBlockTimestamp(acceptSchedule.addn(1));
  429. from = newDefaultAdmin;
  430. });
  431. it('accepts a transfer and changes default admin', async function () {
  432. const receipt = await this.accessControl.acceptDefaultAdminTransfer({ from });
  433. // Storage changes
  434. expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, defaultAdmin)).to.be.false;
  435. expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, newDefaultAdmin)).to.be.true;
  436. expect(await this.accessControl.owner()).to.equal(newDefaultAdmin);
  437. // Emit events
  438. expectEvent(receipt, 'RoleRevoked', {
  439. role: DEFAULT_ADMIN_ROLE,
  440. account: defaultAdmin,
  441. });
  442. expectEvent(receipt, 'RoleGranted', {
  443. role: DEFAULT_ADMIN_ROLE,
  444. account: newDefaultAdmin,
  445. });
  446. // Resets pending default admin and schedule
  447. const { newAdmin, schedule } = await this.accessControl.pendingDefaultAdmin();
  448. expect(newAdmin).to.equal(constants.ZERO_ADDRESS);
  449. expect(schedule).to.be.bignumber.equal(ZERO);
  450. });
  451. });
  452. describe('schedule not passed', function () {
  453. for (const [fromSchedule, tag] of [
  454. [-1, 'less'],
  455. [0, 'equal'],
  456. ]) {
  457. it(`should revert if block.timestamp is ${tag} to schedule`, async function () {
  458. await time.setNextBlockTimestamp(acceptSchedule.toNumber() + fromSchedule);
  459. await expectRevert(
  460. this.accessControl.acceptDefaultAdminTransfer({ from: newDefaultAdmin }),
  461. `${errorPrefix}: transfer delay not passed`,
  462. );
  463. });
  464. }
  465. });
  466. });
  467. describe('cancels a default admin transfer', function () {
  468. it('reverts if called by non default admin accounts', async function () {
  469. await expectRevert(
  470. this.accessControl.cancelDefaultAdminTransfer({ from: other }),
  471. `${errorPrefix}: account ${other.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`,
  472. );
  473. });
  474. describe('when there is a pending default admin transfer', function () {
  475. let acceptSchedule;
  476. beforeEach(async function () {
  477. await this.accessControl.beginDefaultAdminTransfer(newDefaultAdmin, { from: defaultAdmin });
  478. acceptSchedule = web3.utils.toBN(await time.latest()).add(delay);
  479. });
  480. for (const [fromSchedule, tag] of [
  481. [-1, 'before'],
  482. [0, 'exactly when'],
  483. [1, 'after'],
  484. ]) {
  485. it(`resets pending default admin and schedule ${tag} transfer schedule passes`, async function () {
  486. // Advance until passed delay
  487. await time.setNextBlockTimestamp(acceptSchedule.toNumber() + fromSchedule);
  488. const receipt = await this.accessControl.cancelDefaultAdminTransfer({ from: defaultAdmin });
  489. const { newAdmin, schedule } = await this.accessControl.pendingDefaultAdmin();
  490. expect(newAdmin).to.equal(constants.ZERO_ADDRESS);
  491. expect(schedule).to.be.bignumber.equal(ZERO);
  492. expectEvent(receipt, 'DefaultAdminTransferCanceled');
  493. });
  494. }
  495. it('should revert if the previous default admin tries to accept', async function () {
  496. await this.accessControl.cancelDefaultAdminTransfer({ from: defaultAdmin });
  497. // Advance until passed delay
  498. await time.setNextBlockTimestamp(acceptSchedule.addn(1));
  499. // Previous pending default admin should not be able to accept after cancellation.
  500. await expectRevert(
  501. this.accessControl.acceptDefaultAdminTransfer({ from: newDefaultAdmin }),
  502. `${errorPrefix}: pending admin must accept`,
  503. );
  504. });
  505. });
  506. describe('when there is no pending default admin transfer', async function () {
  507. it('should succeed without changes', async function () {
  508. const receipt = await this.accessControl.cancelDefaultAdminTransfer({ from: defaultAdmin });
  509. const { newAdmin, schedule } = await this.accessControl.pendingDefaultAdmin();
  510. expect(newAdmin).to.equal(constants.ZERO_ADDRESS);
  511. expect(schedule).to.be.bignumber.equal(ZERO);
  512. expectNoEvent(receipt, 'DefaultAdminTransferCanceled');
  513. });
  514. });
  515. });
  516. describe('renounces admin', function () {
  517. let delayPassed;
  518. let from = defaultAdmin;
  519. beforeEach(async function () {
  520. await this.accessControl.beginDefaultAdminTransfer(constants.ZERO_ADDRESS, { from });
  521. delayPassed = web3.utils
  522. .toBN(await time.latest())
  523. .add(delay)
  524. .addn(1);
  525. });
  526. it('reverts if caller is not default admin', async function () {
  527. await time.setNextBlockTimestamp(delayPassed);
  528. await expectRevert(
  529. this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, other, { from }),
  530. `${errorPrefix}: can only renounce roles for self`,
  531. );
  532. });
  533. it('renounces role', async function () {
  534. await time.setNextBlockTimestamp(delayPassed);
  535. const receipt = await this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, from, { from });
  536. expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, defaultAdmin)).to.be.false;
  537. expect(await this.accessControl.hasRole(constants.ZERO_ADDRESS, defaultAdmin)).to.be.false;
  538. expectEvent(receipt, 'RoleRevoked', {
  539. role: DEFAULT_ADMIN_ROLE,
  540. account: from,
  541. });
  542. expect(await this.accessControl.owner()).to.equal(constants.ZERO_ADDRESS);
  543. });
  544. it('allows to recover access using the internal _grantRole', async function () {
  545. await time.setNextBlockTimestamp(delayPassed);
  546. await this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, from, { from });
  547. const grantRoleReceipt = await this.accessControl.$_grantRole(DEFAULT_ADMIN_ROLE, other);
  548. expectEvent(grantRoleReceipt, 'RoleGranted', {
  549. role: DEFAULT_ADMIN_ROLE,
  550. account: other,
  551. });
  552. });
  553. describe('schedule not passed', function () {
  554. let delayNotPassed;
  555. beforeEach(function () {
  556. delayNotPassed = delayPassed.subn(1);
  557. });
  558. for (const [fromSchedule, tag] of [
  559. [-1, 'less'],
  560. [0, 'equal'],
  561. ]) {
  562. it(`reverts if block.timestamp is ${tag} to schedule`, async function () {
  563. await time.setNextBlockTimestamp(delayNotPassed.toNumber() + fromSchedule);
  564. await expectRevert(
  565. this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, defaultAdmin, { from }),
  566. `${errorPrefix}: only can renounce in two delayed steps`,
  567. );
  568. });
  569. }
  570. });
  571. });
  572. describe('changes delay', function () {
  573. it('reverts if called by non default admin accounts', async function () {
  574. await expectRevert(
  575. this.accessControl.changeDefaultAdminDelay(time.duration.hours(4), {
  576. from: other,
  577. }),
  578. `${errorPrefix}: account ${other.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`,
  579. );
  580. });
  581. for (const [newDefaultAdminDelay, delayChangeType] of [
  582. [web3.utils.toBN(delay).subn(time.duration.hours(1)), 'decreased'],
  583. [web3.utils.toBN(delay).addn(time.duration.hours(1)), 'increased'],
  584. [web3.utils.toBN(delay).addn(time.duration.days(5)), 'increased to more than 5 days'],
  585. ]) {
  586. describe(`when the delay is ${delayChangeType}`, function () {
  587. it('begins the delay change to the new delay', async function () {
  588. // Begins the change
  589. const receipt = await this.accessControl.changeDefaultAdminDelay(newDefaultAdminDelay, {
  590. from: defaultAdmin,
  591. });
  592. // Calculate expected values
  593. const cap = await this.accessControl.defaultAdminDelayIncreaseWait();
  594. const changeDelay = newDefaultAdminDelay.lte(delay)
  595. ? delay.sub(newDefaultAdminDelay)
  596. : BN.min(newDefaultAdminDelay, cap);
  597. const timestamp = web3.utils.toBN(await time.latest());
  598. const effectSchedule = timestamp.add(changeDelay);
  599. // Assert
  600. const { newDelay, schedule } = await this.accessControl.pendingDefaultAdminDelay();
  601. expect(newDelay).to.be.bignumber.eq(newDefaultAdminDelay);
  602. expect(schedule).to.be.bignumber.eq(effectSchedule);
  603. expectEvent(receipt, 'DefaultAdminDelayChangeScheduled', {
  604. newDelay,
  605. effectSchedule,
  606. });
  607. });
  608. describe('scheduling again', function () {
  609. beforeEach('schedule once', async function () {
  610. await this.accessControl.changeDefaultAdminDelay(newDefaultAdminDelay, { from: defaultAdmin });
  611. });
  612. for (const [fromSchedule, tag] of [
  613. [-1, 'before'],
  614. [0, 'exactly when'],
  615. [1, 'after'],
  616. ]) {
  617. const passed = fromSchedule > 0;
  618. it(`succeeds ${tag} the delay schedule passes`, async function () {
  619. // Wait until schedule + fromSchedule
  620. const { schedule: firstSchedule } = await this.accessControl.pendingDefaultAdminDelay();
  621. await time.setNextBlockTimestamp(firstSchedule.toNumber() + fromSchedule);
  622. // Default admin changes its mind and begins another delay change
  623. const anotherNewDefaultAdminDelay = newDefaultAdminDelay.addn(time.duration.hours(2));
  624. const receipt = await this.accessControl.changeDefaultAdminDelay(anotherNewDefaultAdminDelay, {
  625. from: defaultAdmin,
  626. });
  627. // Calculate expected values
  628. const cap = await this.accessControl.defaultAdminDelayIncreaseWait();
  629. const timestamp = web3.utils.toBN(await time.latest());
  630. const effectSchedule = timestamp.add(BN.min(cap, anotherNewDefaultAdminDelay));
  631. // Assert
  632. const { newDelay, schedule } = await this.accessControl.pendingDefaultAdminDelay();
  633. expect(newDelay).to.be.bignumber.eq(anotherNewDefaultAdminDelay);
  634. expect(schedule).to.be.bignumber.eq(effectSchedule);
  635. expectEvent(receipt, 'DefaultAdminDelayChangeScheduled', {
  636. newDelay,
  637. effectSchedule,
  638. });
  639. });
  640. const emit = passed ? 'not emit' : 'emit';
  641. it(`should ${emit} a cancellation event ${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. const eventMatcher = passed ? expectNoEvent : expectEvent;
  651. eventMatcher(receipt, 'DefaultAdminDelayChangeCanceled');
  652. });
  653. }
  654. });
  655. });
  656. }
  657. });
  658. describe('rollbacks a delay change', function () {
  659. it('reverts if called by non default admin accounts', async function () {
  660. await expectRevert(
  661. this.accessControl.rollbackDefaultAdminDelay({ from: other }),
  662. `${errorPrefix}: account ${other.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`,
  663. );
  664. });
  665. describe('when there is a pending delay', function () {
  666. beforeEach('set pending delay', async function () {
  667. await this.accessControl.changeDefaultAdminDelay(time.duration.hours(12), { from: defaultAdmin });
  668. });
  669. for (const [fromSchedule, tag] of [
  670. [-1, 'before'],
  671. [0, 'exactly when'],
  672. [1, 'after'],
  673. ]) {
  674. const passed = fromSchedule > 0;
  675. it(`resets pending delay and schedule ${tag} delay change schedule passes`, async function () {
  676. // Wait until schedule + fromSchedule
  677. const { schedule: firstSchedule } = await this.accessControl.pendingDefaultAdminDelay();
  678. await time.setNextBlockTimestamp(firstSchedule.toNumber() + fromSchedule);
  679. await this.accessControl.rollbackDefaultAdminDelay({ from: defaultAdmin });
  680. const { newDelay, schedule } = await this.accessControl.pendingDefaultAdminDelay();
  681. expect(newDelay).to.be.bignumber.eq(ZERO);
  682. expect(schedule).to.be.bignumber.eq(ZERO);
  683. });
  684. const emit = passed ? 'not emit' : 'emit';
  685. it(`should ${emit} a cancellation event ${tag} the delay schedule passes`, async function () {
  686. // Wait until schedule + fromSchedule
  687. const { schedule: firstSchedule } = await this.accessControl.pendingDefaultAdminDelay();
  688. await time.setNextBlockTimestamp(firstSchedule.toNumber() + fromSchedule);
  689. const receipt = await this.accessControl.rollbackDefaultAdminDelay({ from: defaultAdmin });
  690. const eventMatcher = passed ? expectNoEvent : expectEvent;
  691. eventMatcher(receipt, 'DefaultAdminDelayChangeCanceled');
  692. });
  693. }
  694. });
  695. describe('when there is no pending delay', function () {
  696. it('succeeds without changes', async function () {
  697. await this.accessControl.rollbackDefaultAdminDelay({ from: defaultAdmin });
  698. const { newDelay, schedule } = await this.accessControl.pendingDefaultAdminDelay();
  699. expect(newDelay).to.be.bignumber.eq(ZERO);
  700. expect(schedule).to.be.bignumber.eq(ZERO);
  701. });
  702. });
  703. });
  704. }
  705. module.exports = {
  706. DEFAULT_ADMIN_ROLE,
  707. shouldBehaveLikeAccessControl,
  708. shouldBehaveLikeAccessControlEnumerable,
  709. shouldBehaveLikeAccessControlDefaultAdminRules,
  710. };