AccessManager.test.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. const { web3 } = require('hardhat');
  2. const { constants, expectEvent, time } = require('@openzeppelin/test-helpers');
  3. const { expectRevertCustomError } = require('../../helpers/customError');
  4. const { selector } = require('../../helpers/methods');
  5. const { clockFromReceipt } = require('../../helpers/time');
  6. const { product } = require('../../helpers/iterate');
  7. const helpers = require('@nomicfoundation/hardhat-network-helpers');
  8. const AccessManager = artifacts.require('$AccessManager');
  9. const AccessManagedTarget = artifacts.require('$AccessManagedTarget');
  10. const Ownable = artifacts.require('$Ownable');
  11. const MAX_UINT64 = web3.utils.toBN((2n ** 64n - 1n).toString());
  12. const ROLES = {
  13. ADMIN: web3.utils.toBN(0),
  14. SOME_ADMIN: web3.utils.toBN(17),
  15. SOME: web3.utils.toBN(42),
  16. PUBLIC: MAX_UINT64,
  17. };
  18. Object.assign(ROLES, Object.fromEntries(Object.entries(ROLES).map(([key, value]) => [value, key])));
  19. const executeDelay = web3.utils.toBN(10);
  20. const grantDelay = web3.utils.toBN(10);
  21. const MINSETBACK = time.duration.days(5);
  22. const formatAccess = access => [access[0], access[1].toString()];
  23. contract('AccessManager', function (accounts) {
  24. const [admin, manager, member, user, other] = accounts;
  25. beforeEach(async function () {
  26. this.manager = await AccessManager.new(admin);
  27. // add member to role
  28. await this.manager.$_setRoleAdmin(ROLES.SOME, ROLES.SOME_ADMIN);
  29. await this.manager.$_setRoleGuardian(ROLES.SOME, ROLES.SOME_ADMIN);
  30. await this.manager.$_grantRole(ROLES.SOME_ADMIN, manager, 0, 0);
  31. await this.manager.$_grantRole(ROLES.SOME, member, 0, 0);
  32. });
  33. it('rejects zero address for initialAdmin', async function () {
  34. await expectRevertCustomError(AccessManager.new(constants.ZERO_ADDRESS), 'AccessManagerInvalidInitialAdmin', [
  35. constants.ZERO_ADDRESS,
  36. ]);
  37. });
  38. it('default minsetback is 1 day', async function () {
  39. expect(await this.manager.minSetback()).to.be.bignumber.equal(MINSETBACK);
  40. });
  41. it('roles are correctly initialized', async function () {
  42. // role admin
  43. expect(await this.manager.getRoleAdmin(ROLES.ADMIN)).to.be.bignumber.equal(ROLES.ADMIN);
  44. expect(await this.manager.getRoleAdmin(ROLES.SOME_ADMIN)).to.be.bignumber.equal(ROLES.ADMIN);
  45. expect(await this.manager.getRoleAdmin(ROLES.SOME)).to.be.bignumber.equal(ROLES.SOME_ADMIN);
  46. expect(await this.manager.getRoleAdmin(ROLES.PUBLIC)).to.be.bignumber.equal(ROLES.ADMIN);
  47. // role guardian
  48. expect(await this.manager.getRoleGuardian(ROLES.ADMIN)).to.be.bignumber.equal(ROLES.ADMIN);
  49. expect(await this.manager.getRoleGuardian(ROLES.SOME_ADMIN)).to.be.bignumber.equal(ROLES.ADMIN);
  50. expect(await this.manager.getRoleGuardian(ROLES.SOME)).to.be.bignumber.equal(ROLES.SOME_ADMIN);
  51. expect(await this.manager.getRoleGuardian(ROLES.PUBLIC)).to.be.bignumber.equal(ROLES.ADMIN);
  52. // role members
  53. expect(await this.manager.hasRole(ROLES.ADMIN, admin).then(formatAccess)).to.be.deep.equal([true, '0']);
  54. expect(await this.manager.hasRole(ROLES.ADMIN, manager).then(formatAccess)).to.be.deep.equal([false, '0']);
  55. expect(await this.manager.hasRole(ROLES.ADMIN, member).then(formatAccess)).to.be.deep.equal([false, '0']);
  56. expect(await this.manager.hasRole(ROLES.ADMIN, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  57. expect(await this.manager.hasRole(ROLES.SOME_ADMIN, admin).then(formatAccess)).to.be.deep.equal([false, '0']);
  58. expect(await this.manager.hasRole(ROLES.SOME_ADMIN, manager).then(formatAccess)).to.be.deep.equal([true, '0']);
  59. expect(await this.manager.hasRole(ROLES.SOME_ADMIN, member).then(formatAccess)).to.be.deep.equal([false, '0']);
  60. expect(await this.manager.hasRole(ROLES.SOME_ADMIN, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  61. expect(await this.manager.hasRole(ROLES.SOME, admin).then(formatAccess)).to.be.deep.equal([false, '0']);
  62. expect(await this.manager.hasRole(ROLES.SOME, manager).then(formatAccess)).to.be.deep.equal([false, '0']);
  63. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  64. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  65. expect(await this.manager.hasRole(ROLES.PUBLIC, admin).then(formatAccess)).to.be.deep.equal([true, '0']);
  66. expect(await this.manager.hasRole(ROLES.PUBLIC, manager).then(formatAccess)).to.be.deep.equal([true, '0']);
  67. expect(await this.manager.hasRole(ROLES.PUBLIC, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  68. expect(await this.manager.hasRole(ROLES.PUBLIC, user).then(formatAccess)).to.be.deep.equal([true, '0']);
  69. });
  70. describe('Roles management', function () {
  71. describe('label role', function () {
  72. it('admin can emit a label event', async function () {
  73. expectEvent(await this.manager.labelRole(ROLES.SOME, 'Some label', { from: admin }), 'RoleLabel', {
  74. roleId: ROLES.SOME,
  75. label: 'Some label',
  76. });
  77. });
  78. it('admin can re-emit a label event', async function () {
  79. await this.manager.labelRole(ROLES.SOME, 'Some label', { from: admin });
  80. expectEvent(await this.manager.labelRole(ROLES.SOME, 'Updated label', { from: admin }), 'RoleLabel', {
  81. roleId: ROLES.SOME,
  82. label: 'Updated label',
  83. });
  84. });
  85. it('emitting a label is restricted', async function () {
  86. await expectRevertCustomError(
  87. this.manager.labelRole(ROLES.SOME, 'Invalid label', { from: other }),
  88. 'AccessManagerUnauthorizedAccount',
  89. [other, ROLES.ADMIN],
  90. );
  91. });
  92. });
  93. describe('grant role', function () {
  94. describe('without a grant delay', function () {
  95. it('without an execute delay', async function () {
  96. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  97. const { receipt } = await this.manager.grantRole(ROLES.SOME, user, 0, { from: manager });
  98. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  99. expectEvent(receipt, 'RoleGranted', {
  100. roleId: ROLES.SOME,
  101. account: user,
  102. since: timestamp,
  103. delay: '0',
  104. newMember: true,
  105. });
  106. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([true, '0']);
  107. const access = await this.manager.getAccess(ROLES.SOME, user);
  108. expect(access[0]).to.be.bignumber.equal(timestamp); // inRoleSince
  109. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  110. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  111. expect(access[3]).to.be.bignumber.equal('0'); // effect
  112. });
  113. it('with an execute delay', async function () {
  114. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  115. const { receipt } = await this.manager.grantRole(ROLES.SOME, user, executeDelay, { from: manager });
  116. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  117. expectEvent(receipt, 'RoleGranted', {
  118. roleId: ROLES.SOME,
  119. account: user,
  120. since: timestamp,
  121. delay: executeDelay,
  122. newMember: true,
  123. });
  124. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([
  125. true,
  126. executeDelay.toString(),
  127. ]);
  128. const access = await this.manager.getAccess(ROLES.SOME, user);
  129. expect(access[0]).to.be.bignumber.equal(timestamp); // inRoleSince
  130. expect(access[1]).to.be.bignumber.equal(executeDelay); // currentDelay
  131. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  132. expect(access[3]).to.be.bignumber.equal('0'); // effect
  133. });
  134. it('to a user that is already in the role', async function () {
  135. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  136. await this.manager.grantRole(ROLES.SOME, member, 0, { from: manager });
  137. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  138. });
  139. it('to a user that is scheduled for joining the role', async function () {
  140. await this.manager.$_grantRole(ROLES.SOME, user, 10, 0); // grant delay 10
  141. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  142. await this.manager.grantRole(ROLES.SOME, user, 0, { from: manager });
  143. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  144. });
  145. it('grant role is restricted', async function () {
  146. await expectRevertCustomError(
  147. this.manager.grantRole(ROLES.SOME, user, 0, { from: other }),
  148. 'AccessManagerUnauthorizedAccount',
  149. [other, ROLES.SOME_ADMIN],
  150. );
  151. });
  152. });
  153. describe('with a grant delay', function () {
  154. beforeEach(async function () {
  155. await this.manager.$_setGrantDelay(ROLES.SOME, grantDelay);
  156. await time.increase(MINSETBACK);
  157. });
  158. it('granted role is not active immediately', async function () {
  159. const { receipt } = await this.manager.grantRole(ROLES.SOME, user, 0, { from: manager });
  160. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  161. expectEvent(receipt, 'RoleGranted', {
  162. roleId: ROLES.SOME,
  163. account: user,
  164. since: timestamp.add(grantDelay),
  165. delay: '0',
  166. newMember: true,
  167. });
  168. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  169. const access = await this.manager.getAccess(ROLES.SOME, user);
  170. expect(access[0]).to.be.bignumber.equal(timestamp.add(grantDelay)); // inRoleSince
  171. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  172. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  173. expect(access[3]).to.be.bignumber.equal('0'); // effect
  174. });
  175. it('granted role is active after the delay', async function () {
  176. const { receipt } = await this.manager.grantRole(ROLES.SOME, user, 0, { from: manager });
  177. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  178. expectEvent(receipt, 'RoleGranted', {
  179. roleId: ROLES.SOME,
  180. account: user,
  181. since: timestamp.add(grantDelay),
  182. delay: '0',
  183. newMember: true,
  184. });
  185. await time.increase(grantDelay);
  186. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([true, '0']);
  187. const access = await this.manager.getAccess(ROLES.SOME, user);
  188. expect(access[0]).to.be.bignumber.equal(timestamp.add(grantDelay)); // inRoleSince
  189. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  190. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  191. expect(access[3]).to.be.bignumber.equal('0'); // effect
  192. });
  193. });
  194. it('cannot grant public role', async function () {
  195. await expectRevertCustomError(
  196. this.manager.$_grantRole(ROLES.PUBLIC, other, 0, executeDelay, { from: manager }),
  197. 'AccessManagerLockedRole',
  198. [ROLES.PUBLIC],
  199. );
  200. });
  201. });
  202. describe('revoke role', function () {
  203. it('from a user that is already in the role', async function () {
  204. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  205. const { receipt } = await this.manager.revokeRole(ROLES.SOME, member, { from: manager });
  206. expectEvent(receipt, 'RoleRevoked', { roleId: ROLES.SOME, account: member });
  207. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([false, '0']);
  208. const access = await this.manager.getAccess(ROLES.SOME, user);
  209. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  210. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  211. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  212. expect(access[3]).to.be.bignumber.equal('0'); // effect
  213. });
  214. it('from a user that is scheduled for joining the role', async function () {
  215. await this.manager.$_grantRole(ROLES.SOME, user, 10, 0); // grant delay 10
  216. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  217. const { receipt } = await this.manager.revokeRole(ROLES.SOME, user, { from: manager });
  218. expectEvent(receipt, 'RoleRevoked', { roleId: ROLES.SOME, account: user });
  219. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  220. const access = await this.manager.getAccess(ROLES.SOME, user);
  221. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  222. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  223. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  224. expect(access[3]).to.be.bignumber.equal('0'); // effect
  225. });
  226. it('from a user that is not in the role', async function () {
  227. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  228. await this.manager.revokeRole(ROLES.SOME, user, { from: manager });
  229. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  230. });
  231. it('revoke role is restricted', async function () {
  232. await expectRevertCustomError(
  233. this.manager.revokeRole(ROLES.SOME, member, { from: other }),
  234. 'AccessManagerUnauthorizedAccount',
  235. [other, ROLES.SOME_ADMIN],
  236. );
  237. });
  238. });
  239. describe('renounce role', function () {
  240. it('for a user that is already in the role', async function () {
  241. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([true, '0']);
  242. const { receipt } = await this.manager.renounceRole(ROLES.SOME, member, { from: member });
  243. expectEvent(receipt, 'RoleRevoked', { roleId: ROLES.SOME, account: member });
  244. expect(await this.manager.hasRole(ROLES.SOME, member).then(formatAccess)).to.be.deep.equal([false, '0']);
  245. const access = await this.manager.getAccess(ROLES.SOME, member);
  246. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  247. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  248. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  249. expect(access[3]).to.be.bignumber.equal('0'); // effect
  250. });
  251. it('for a user that is schedule for joining the role', async function () {
  252. await this.manager.$_grantRole(ROLES.SOME, user, 10, 0); // grant delay 10
  253. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  254. const { receipt } = await this.manager.renounceRole(ROLES.SOME, user, { from: user });
  255. expectEvent(receipt, 'RoleRevoked', { roleId: ROLES.SOME, account: user });
  256. expect(await this.manager.hasRole(ROLES.SOME, user).then(formatAccess)).to.be.deep.equal([false, '0']);
  257. const access = await this.manager.getAccess(ROLES.SOME, user);
  258. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  259. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  260. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  261. expect(access[3]).to.be.bignumber.equal('0'); // effect
  262. });
  263. it('for a user that is not in the role', async function () {
  264. await this.manager.renounceRole(ROLES.SOME, user, { from: user });
  265. });
  266. it('bad user confirmation', async function () {
  267. await expectRevertCustomError(
  268. this.manager.renounceRole(ROLES.SOME, member, { from: user }),
  269. 'AccessManagerBadConfirmation',
  270. [],
  271. );
  272. });
  273. });
  274. describe('change role admin', function () {
  275. it("admin can set any role's admin", async function () {
  276. expect(await this.manager.getRoleAdmin(ROLES.SOME)).to.be.bignumber.equal(ROLES.SOME_ADMIN);
  277. const { receipt } = await this.manager.setRoleAdmin(ROLES.SOME, ROLES.ADMIN, { from: admin });
  278. expectEvent(receipt, 'RoleAdminChanged', { roleId: ROLES.SOME, admin: ROLES.ADMIN });
  279. expect(await this.manager.getRoleAdmin(ROLES.SOME)).to.be.bignumber.equal(ROLES.ADMIN);
  280. });
  281. it("setting a role's admin is restricted", async function () {
  282. await expectRevertCustomError(
  283. this.manager.setRoleAdmin(ROLES.SOME, ROLES.SOME, { from: manager }),
  284. 'AccessManagerUnauthorizedAccount',
  285. [manager, ROLES.ADMIN],
  286. );
  287. });
  288. });
  289. describe('change role guardian', function () {
  290. it("admin can set any role's admin", async function () {
  291. expect(await this.manager.getRoleGuardian(ROLES.SOME)).to.be.bignumber.equal(ROLES.SOME_ADMIN);
  292. const { receipt } = await this.manager.setRoleGuardian(ROLES.SOME, ROLES.ADMIN, { from: admin });
  293. expectEvent(receipt, 'RoleGuardianChanged', { roleId: ROLES.SOME, guardian: ROLES.ADMIN });
  294. expect(await this.manager.getRoleGuardian(ROLES.SOME)).to.be.bignumber.equal(ROLES.ADMIN);
  295. });
  296. it("setting a role's admin is restricted", async function () {
  297. await expectRevertCustomError(
  298. this.manager.setRoleGuardian(ROLES.SOME, ROLES.SOME, { from: other }),
  299. 'AccessManagerUnauthorizedAccount',
  300. [other, ROLES.ADMIN],
  301. );
  302. });
  303. });
  304. describe('change execution delay', function () {
  305. it('increasing the delay has immediate effect', async function () {
  306. const oldDelay = web3.utils.toBN(10);
  307. const newDelay = web3.utils.toBN(100);
  308. // role is already granted (with no delay) in the initial setup. this update takes time.
  309. await this.manager.$_grantRole(ROLES.SOME, member, 0, oldDelay);
  310. const accessBefore = await this.manager.getAccess(ROLES.SOME, member);
  311. expect(accessBefore[1]).to.be.bignumber.equal(oldDelay); // currentDelay
  312. expect(accessBefore[2]).to.be.bignumber.equal('0'); // pendingDelay
  313. expect(accessBefore[3]).to.be.bignumber.equal('0'); // effect
  314. const { receipt } = await this.manager.grantRole(ROLES.SOME, member, newDelay, {
  315. from: manager,
  316. });
  317. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  318. expectEvent(receipt, 'RoleGranted', {
  319. roleId: ROLES.SOME,
  320. account: member,
  321. since: timestamp,
  322. delay: newDelay,
  323. newMember: false,
  324. });
  325. // immediate effect
  326. const accessAfter = await this.manager.getAccess(ROLES.SOME, member);
  327. expect(accessAfter[1]).to.be.bignumber.equal(newDelay); // currentDelay
  328. expect(accessAfter[2]).to.be.bignumber.equal('0'); // pendingDelay
  329. expect(accessAfter[3]).to.be.bignumber.equal('0'); // effect
  330. });
  331. it('decreasing the delay takes time', async function () {
  332. const oldDelay = web3.utils.toBN(100);
  333. const newDelay = web3.utils.toBN(10);
  334. // role is already granted (with no delay) in the initial setup. this update takes time.
  335. await this.manager.$_grantRole(ROLES.SOME, member, 0, oldDelay);
  336. const accessBefore = await this.manager.getAccess(ROLES.SOME, member);
  337. expect(accessBefore[1]).to.be.bignumber.equal(oldDelay); // currentDelay
  338. expect(accessBefore[2]).to.be.bignumber.equal('0'); // pendingDelay
  339. expect(accessBefore[3]).to.be.bignumber.equal('0'); // effect
  340. const { receipt } = await this.manager.grantRole(ROLES.SOME, member, newDelay, {
  341. from: manager,
  342. });
  343. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  344. const setback = oldDelay.sub(newDelay);
  345. expectEvent(receipt, 'RoleGranted', {
  346. roleId: ROLES.SOME,
  347. account: member,
  348. since: timestamp.add(setback),
  349. delay: newDelay,
  350. newMember: false,
  351. });
  352. // no immediate effect
  353. const accessAfter = await this.manager.getAccess(ROLES.SOME, member);
  354. expect(accessAfter[1]).to.be.bignumber.equal(oldDelay); // currentDelay
  355. expect(accessAfter[2]).to.be.bignumber.equal(newDelay); // pendingDelay
  356. expect(accessAfter[3]).to.be.bignumber.equal(timestamp.add(setback)); // effect
  357. // delayed effect
  358. await time.increase(setback);
  359. const accessAfterSetback = await this.manager.getAccess(ROLES.SOME, member);
  360. expect(accessAfterSetback[1]).to.be.bignumber.equal(newDelay); // currentDelay
  361. expect(accessAfterSetback[2]).to.be.bignumber.equal('0'); // pendingDelay
  362. expect(accessAfterSetback[3]).to.be.bignumber.equal('0'); // effect
  363. });
  364. it('can set a user execution delay during the grant delay', async function () {
  365. await this.manager.$_grantRole(ROLES.SOME, other, 10, 0);
  366. // here: "other" is pending to get the role, but doesn't yet have it.
  367. const { receipt } = await this.manager.grantRole(ROLES.SOME, other, executeDelay, { from: manager });
  368. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  369. // increasing the execution delay from 0 to executeDelay is immediate
  370. expectEvent(receipt, 'RoleGranted', {
  371. roleId: ROLES.SOME,
  372. account: other,
  373. since: timestamp,
  374. delay: executeDelay,
  375. newMember: false,
  376. });
  377. });
  378. });
  379. describe('change grant delay', function () {
  380. it('increasing the delay has immediate effect', async function () {
  381. const oldDelay = web3.utils.toBN(10);
  382. const newDelay = web3.utils.toBN(100);
  383. await this.manager.$_setGrantDelay(ROLES.SOME, oldDelay);
  384. await time.increase(MINSETBACK);
  385. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  386. const { receipt } = await this.manager.setGrantDelay(ROLES.SOME, newDelay, { from: admin });
  387. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  388. const setback = web3.utils.BN.max(MINSETBACK, oldDelay.sub(newDelay));
  389. expect(setback).to.be.bignumber.equal(MINSETBACK);
  390. expectEvent(receipt, 'RoleGrantDelayChanged', {
  391. roleId: ROLES.SOME,
  392. delay: newDelay,
  393. since: timestamp.add(setback),
  394. });
  395. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  396. await time.increase(setback);
  397. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(newDelay);
  398. });
  399. it('increasing the delay has delay effect #1', async function () {
  400. const oldDelay = web3.utils.toBN(100);
  401. const newDelay = web3.utils.toBN(10);
  402. await this.manager.$_setGrantDelay(ROLES.SOME, oldDelay);
  403. await time.increase(MINSETBACK);
  404. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  405. const { receipt } = await this.manager.setGrantDelay(ROLES.SOME, newDelay, { from: admin });
  406. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  407. const setback = web3.utils.BN.max(MINSETBACK, oldDelay.sub(newDelay));
  408. expect(setback).to.be.bignumber.equal(MINSETBACK);
  409. expectEvent(receipt, 'RoleGrantDelayChanged', {
  410. roleId: ROLES.SOME,
  411. delay: newDelay,
  412. since: timestamp.add(setback),
  413. });
  414. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  415. await time.increase(setback);
  416. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(newDelay);
  417. });
  418. it('increasing the delay has delay effect #2', async function () {
  419. const oldDelay = time.duration.days(30); // more than the minsetback
  420. const newDelay = web3.utils.toBN(10);
  421. await this.manager.$_setGrantDelay(ROLES.SOME, oldDelay);
  422. await time.increase(MINSETBACK);
  423. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  424. const { receipt } = await this.manager.setGrantDelay(ROLES.SOME, newDelay, { from: admin });
  425. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  426. const setback = web3.utils.BN.max(MINSETBACK, oldDelay.sub(newDelay));
  427. expect(setback).to.be.bignumber.gt(MINSETBACK);
  428. expectEvent(receipt, 'RoleGrantDelayChanged', {
  429. roleId: ROLES.SOME,
  430. delay: newDelay,
  431. since: timestamp.add(setback),
  432. });
  433. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(oldDelay);
  434. await time.increase(setback);
  435. expect(await this.manager.getRoleGrantDelay(ROLES.SOME)).to.be.bignumber.equal(newDelay);
  436. });
  437. it('changing the grant delay is restricted', async function () {
  438. await expectRevertCustomError(
  439. this.manager.setGrantDelay(ROLES.SOME, grantDelay, { from: other }),
  440. 'AccessManagerUnauthorizedAccount',
  441. [ROLES.ADMIN, other],
  442. );
  443. });
  444. });
  445. });
  446. describe('with AccessManaged target contract', function () {
  447. beforeEach('deploy target contract', async function () {
  448. this.target = await AccessManagedTarget.new(this.manager.address);
  449. // helpers for indirect calls
  450. this.callData = selector('fnRestricted()');
  451. this.call = [this.target.address, this.callData];
  452. this.opId = web3.utils.keccak256(
  453. web3.eth.abi.encodeParameters(['address', 'address', 'bytes'], [user, ...this.call]),
  454. );
  455. this.direct = (opts = {}) => this.target.fnRestricted({ from: user, ...opts });
  456. this.schedule = (opts = {}) => this.manager.schedule(...this.call, 0, { from: user, ...opts });
  457. this.execute = (opts = {}) => this.manager.execute(...this.call, { from: user, ...opts });
  458. this.cancel = (opts = {}) => this.manager.cancel(user, ...this.call, { from: user, ...opts });
  459. });
  460. describe('Change function permissions', function () {
  461. const sigs = ['someFunction()', 'someOtherFunction(uint256)', 'oneMoreFunction(address,uint8)'].map(selector);
  462. it('admin can set function role', async function () {
  463. for (const sig of sigs) {
  464. expect(await this.manager.getTargetFunctionRole(this.target.address, sig)).to.be.bignumber.equal(ROLES.ADMIN);
  465. }
  466. const { receipt: receipt1 } = await this.manager.setTargetFunctionRole(this.target.address, sigs, ROLES.SOME, {
  467. from: admin,
  468. });
  469. for (const sig of sigs) {
  470. expectEvent(receipt1, 'TargetFunctionRoleUpdated', {
  471. target: this.target.address,
  472. selector: sig,
  473. roleId: ROLES.SOME,
  474. });
  475. expect(await this.manager.getTargetFunctionRole(this.target.address, sig)).to.be.bignumber.equal(ROLES.SOME);
  476. }
  477. const { receipt: receipt2 } = await this.manager.setTargetFunctionRole(
  478. this.target.address,
  479. [sigs[1]],
  480. ROLES.SOME_ADMIN,
  481. {
  482. from: admin,
  483. },
  484. );
  485. expectEvent(receipt2, 'TargetFunctionRoleUpdated', {
  486. target: this.target.address,
  487. selector: sigs[1],
  488. roleId: ROLES.SOME_ADMIN,
  489. });
  490. for (const sig of sigs) {
  491. expect(await this.manager.getTargetFunctionRole(this.target.address, sig)).to.be.bignumber.equal(
  492. sig == sigs[1] ? ROLES.SOME_ADMIN : ROLES.SOME,
  493. );
  494. }
  495. });
  496. it('non-admin cannot set function role', async function () {
  497. await expectRevertCustomError(
  498. this.manager.setTargetFunctionRole(this.target.address, sigs, ROLES.SOME, { from: other }),
  499. 'AccessManagerUnauthorizedAccount',
  500. [other, ROLES.ADMIN],
  501. );
  502. });
  503. });
  504. // WIP
  505. describe('Calling restricted & unrestricted functions', function () {
  506. for (const [callerRoles, fnRole, closed, delay] of product(
  507. [[], [ROLES.SOME]],
  508. [undefined, ROLES.ADMIN, ROLES.SOME, ROLES.PUBLIC],
  509. [false, true],
  510. [null, executeDelay],
  511. )) {
  512. // can we call with a delay ?
  513. const indirectSuccess = (fnRole == ROLES.PUBLIC || callerRoles.includes(fnRole)) && !closed;
  514. // can we call without a delay ?
  515. const directSuccess = (fnRole == ROLES.PUBLIC || (callerRoles.includes(fnRole) && !delay)) && !closed;
  516. const description = [
  517. 'Caller in roles',
  518. '[' + (callerRoles ?? []).map(roleId => ROLES[roleId]).join(', ') + ']',
  519. delay ? 'with a delay' : 'without a delay',
  520. '+',
  521. 'functions open to roles',
  522. '[' + (ROLES[fnRole] ?? '') + ']',
  523. closed ? `(closed)` : '',
  524. ].join(' ');
  525. describe(description, function () {
  526. beforeEach(async function () {
  527. // setup
  528. await Promise.all([
  529. this.manager.$_setTargetClosed(this.target.address, closed),
  530. fnRole && this.manager.$_setTargetFunctionRole(this.target.address, selector('fnRestricted()'), fnRole),
  531. fnRole && this.manager.$_setTargetFunctionRole(this.target.address, selector('fnUnrestricted()'), fnRole),
  532. ...callerRoles
  533. .filter(roleId => roleId != ROLES.PUBLIC)
  534. .map(roleId => this.manager.$_grantRole(roleId, user, 0, delay ?? 0)),
  535. ]);
  536. // post setup checks
  537. expect(await this.manager.isTargetClosed(this.target.address)).to.be.equal(closed);
  538. if (fnRole) {
  539. expect(
  540. await this.manager.getTargetFunctionRole(this.target.address, selector('fnRestricted()')),
  541. ).to.be.bignumber.equal(fnRole);
  542. expect(
  543. await this.manager.getTargetFunctionRole(this.target.address, selector('fnUnrestricted()')),
  544. ).to.be.bignumber.equal(fnRole);
  545. }
  546. for (const roleId of callerRoles) {
  547. const access = await this.manager.getAccess(roleId, user);
  548. if (roleId == ROLES.PUBLIC) {
  549. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  550. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  551. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  552. expect(access[3]).to.be.bignumber.equal('0'); // effect
  553. } else {
  554. expect(access[0]).to.be.bignumber.gt('0'); // inRoleSince
  555. expect(access[1]).to.be.bignumber.eq(String(delay ?? 0)); // currentDelay
  556. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  557. expect(access[3]).to.be.bignumber.equal('0'); // effect
  558. }
  559. }
  560. });
  561. it('canCall', async function () {
  562. const result = await this.manager.canCall(user, this.target.address, selector('fnRestricted()'));
  563. expect(result[0]).to.be.equal(directSuccess);
  564. expect(result[1]).to.be.bignumber.equal(!directSuccess && indirectSuccess ? delay ?? '0' : '0');
  565. });
  566. it('Calling a non restricted function never revert', async function () {
  567. expectEvent(await this.target.fnUnrestricted({ from: user }), 'CalledUnrestricted', {
  568. caller: user,
  569. });
  570. });
  571. it(`Calling a restricted function directly should ${
  572. directSuccess ? 'succeed' : 'revert'
  573. }`, async function () {
  574. const promise = this.direct();
  575. if (directSuccess) {
  576. expectEvent(await promise, 'CalledRestricted', { caller: user });
  577. } else if (indirectSuccess) {
  578. await expectRevertCustomError(promise, 'AccessManagerNotScheduled', [this.opId]);
  579. } else {
  580. await expectRevertCustomError(promise, 'AccessManagedUnauthorized', [user]);
  581. }
  582. });
  583. it('Calling indirectly: only execute', async function () {
  584. // execute without schedule
  585. if (directSuccess) {
  586. const nonceBefore = await this.manager.getNonce(this.opId);
  587. const { receipt, tx } = await this.execute();
  588. expectEvent.notEmitted(receipt, 'OperationExecuted', { operationId: this.opId });
  589. await expectEvent.inTransaction(tx, this.target, 'CalledRestricted', { caller: this.manager.address });
  590. // nonce is not modified
  591. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore);
  592. } else if (indirectSuccess) {
  593. await expectRevertCustomError(this.execute(), 'AccessManagerNotScheduled', [this.opId]);
  594. } else {
  595. await expectRevertCustomError(this.execute(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  596. }
  597. });
  598. it('Calling indirectly: schedule and execute', async function () {
  599. if (directSuccess || indirectSuccess) {
  600. const nonceBefore = await this.manager.getNonce(this.opId);
  601. const { receipt } = await this.schedule();
  602. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  603. expectEvent(receipt, 'OperationScheduled', {
  604. operationId: this.opId,
  605. caller: user,
  606. target: this.call[0],
  607. data: this.call[1],
  608. });
  609. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  610. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(
  611. timestamp.add(directSuccess ? web3.utils.toBN(0) : delay),
  612. );
  613. // nonce is incremented
  614. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  615. // execute without wait
  616. if (directSuccess) {
  617. const { receipt, tx } = await this.execute();
  618. await expectEvent.inTransaction(tx, this.target, 'CalledRestricted', { caller: this.manager.address });
  619. if (delay && fnRole !== ROLES.PUBLIC) {
  620. expectEvent(receipt, 'OperationExecuted', { operationId: this.opId });
  621. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  622. }
  623. // nonce is not modified by execute
  624. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  625. } else if (indirectSuccess) {
  626. await expectRevertCustomError(this.execute(), 'AccessManagerNotReady', [this.opId]);
  627. } else {
  628. await expectRevertCustomError(this.execute(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  629. }
  630. } else {
  631. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  632. }
  633. });
  634. it('Calling indirectly: schedule wait and execute', async function () {
  635. if (directSuccess || indirectSuccess) {
  636. const nonceBefore = await this.manager.getNonce(this.opId);
  637. const { receipt } = await this.schedule();
  638. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  639. expectEvent(receipt, 'OperationScheduled', {
  640. operationId: this.opId,
  641. caller: user,
  642. target: this.call[0],
  643. data: this.call[1],
  644. });
  645. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  646. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(
  647. timestamp.add(directSuccess ? web3.utils.toBN(0) : delay),
  648. );
  649. // nonce is incremented
  650. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  651. // wait
  652. await time.increase(delay ?? 0);
  653. // execute without wait
  654. if (directSuccess || indirectSuccess) {
  655. const { receipt, tx } = await this.execute();
  656. await expectEvent.inTransaction(tx, this.target, 'CalledRestricted', { caller: this.manager.address });
  657. if (delay && fnRole !== ROLES.PUBLIC) {
  658. expectEvent(receipt, 'OperationExecuted', { operationId: this.opId });
  659. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  660. }
  661. // nonce is not modified by execute
  662. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  663. } else {
  664. await expectRevertCustomError(this.execute(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  665. }
  666. } else {
  667. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  668. }
  669. });
  670. it('Calling directly: schedule and call', async function () {
  671. if (directSuccess || indirectSuccess) {
  672. const nonceBefore = await this.manager.getNonce(this.opId);
  673. const { receipt } = await this.schedule();
  674. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  675. expectEvent(receipt, 'OperationScheduled', {
  676. operationId: this.opId,
  677. caller: user,
  678. target: this.call[0],
  679. data: this.call[1],
  680. });
  681. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  682. const schedule = timestamp.add(directSuccess ? web3.utils.toBN(0) : delay);
  683. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  684. // nonce is incremented
  685. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  686. // execute without wait
  687. const promise = this.direct();
  688. if (directSuccess) {
  689. expectEvent(await promise, 'CalledRestricted', { caller: user });
  690. // schedule is not reset
  691. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  692. // nonce is not modified by execute
  693. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  694. } else if (indirectSuccess) {
  695. await expectRevertCustomError(promise, 'AccessManagerNotReady', [this.opId]);
  696. } else {
  697. await expectRevertCustomError(promise, 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  698. }
  699. } else {
  700. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  701. }
  702. });
  703. it('Calling directly: schedule wait and call', async function () {
  704. if (directSuccess || indirectSuccess) {
  705. const nonceBefore = await this.manager.getNonce(this.opId);
  706. const { receipt } = await this.schedule();
  707. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  708. expectEvent(receipt, 'OperationScheduled', {
  709. operationId: this.opId,
  710. caller: user,
  711. target: this.call[0],
  712. data: this.call[1],
  713. });
  714. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  715. const schedule = timestamp.add(directSuccess ? web3.utils.toBN(0) : delay);
  716. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  717. // nonce is incremented
  718. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  719. // wait
  720. await time.increase(delay ?? 0);
  721. // execute without wait
  722. const promise = await this.direct();
  723. if (directSuccess) {
  724. expectEvent(await promise, 'CalledRestricted', { caller: user });
  725. // schedule is not reset
  726. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  727. // nonce is not modified by execute
  728. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  729. } else if (indirectSuccess) {
  730. const receipt = await promise;
  731. expectEvent(receipt, 'CalledRestricted', { caller: user });
  732. await expectEvent.inTransaction(receipt.tx, this.manager, 'OperationExecuted', {
  733. operationId: this.opId,
  734. });
  735. // schedule is reset
  736. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  737. // nonce is not modified by execute
  738. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  739. } else {
  740. await expectRevertCustomError(this.direct(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  741. }
  742. } else {
  743. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  744. }
  745. });
  746. it('Scheduling for later than needed'); // TODO
  747. });
  748. }
  749. });
  750. describe('Indirect execution corner-cases', async function () {
  751. beforeEach(async function () {
  752. await this.manager.$_setTargetFunctionRole(this.target.address, this.callData, ROLES.SOME);
  753. await this.manager.$_grantRole(ROLES.SOME, user, 0, executeDelay);
  754. });
  755. it('Checking canCall when caller is the manager depend on the _executionId', async function () {
  756. const result = await this.manager.canCall(this.manager.address, this.target.address, '0x00000000');
  757. expect(result[0]).to.be.false;
  758. expect(result[1]).to.be.bignumber.equal('0');
  759. });
  760. it('Cannot execute earlier', async function () {
  761. const { receipt } = await this.schedule();
  762. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  763. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(timestamp.add(executeDelay));
  764. // too early
  765. await helpers.time.setNextBlockTimestamp(timestamp.add(executeDelay).subn(1));
  766. await expectRevertCustomError(this.execute(), 'AccessManagerNotReady', [this.opId]);
  767. // the revert happened one second before the execution delay expired
  768. expect(await time.latest()).to.be.bignumber.equal(timestamp.add(executeDelay).subn(1));
  769. // ok
  770. await helpers.time.setNextBlockTimestamp(timestamp.add(executeDelay));
  771. await this.execute();
  772. // the success happened when the delay was reached (earliest possible)
  773. expect(await time.latest()).to.be.bignumber.equal(timestamp.add(executeDelay));
  774. });
  775. it('Cannot schedule an already scheduled operation', async function () {
  776. const { receipt } = await this.schedule();
  777. expectEvent(receipt, 'OperationScheduled', {
  778. operationId: this.opId,
  779. caller: user,
  780. target: this.call[0],
  781. data: this.call[1],
  782. });
  783. await expectRevertCustomError(this.schedule(), 'AccessManagerAlreadyScheduled', [this.opId]);
  784. });
  785. it('Cannot cancel an operation that is not scheduled', async function () {
  786. await expectRevertCustomError(this.cancel(), 'AccessManagerNotScheduled', [this.opId]);
  787. });
  788. it('Cannot cancel an operation that is already executed', async function () {
  789. await this.schedule();
  790. await time.increase(executeDelay);
  791. await this.execute();
  792. await expectRevertCustomError(this.cancel(), 'AccessManagerNotScheduled', [this.opId]);
  793. });
  794. it('Scheduler can cancel', async function () {
  795. await this.schedule();
  796. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  797. expectEvent(await this.cancel({ from: manager }), 'OperationCanceled', { operationId: this.opId });
  798. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  799. });
  800. it('Guardian can cancel', async function () {
  801. await this.schedule();
  802. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  803. expectEvent(await this.cancel({ from: manager }), 'OperationCanceled', { operationId: this.opId });
  804. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  805. });
  806. it('Cancel is restricted', async function () {
  807. await this.schedule();
  808. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  809. await expectRevertCustomError(this.cancel({ from: other }), 'AccessManagerUnauthorizedCancel', [
  810. other,
  811. user,
  812. ...this.call,
  813. ]);
  814. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  815. });
  816. it('Can re-schedule after execution', async function () {
  817. await this.schedule();
  818. await time.increase(executeDelay);
  819. await this.execute();
  820. // reschedule
  821. const { receipt } = await this.schedule();
  822. expectEvent(receipt, 'OperationScheduled', {
  823. operationId: this.opId,
  824. caller: user,
  825. target: this.call[0],
  826. data: this.call[1],
  827. });
  828. });
  829. it('Can re-schedule after cancel', async function () {
  830. await this.schedule();
  831. await this.cancel();
  832. // reschedule
  833. const { receipt } = await this.schedule();
  834. expectEvent(receipt, 'OperationScheduled', {
  835. operationId: this.opId,
  836. caller: user,
  837. target: this.call[0],
  838. data: this.call[1],
  839. });
  840. });
  841. });
  842. });
  843. describe('with Ownable target contract', function () {
  844. const roleId = web3.utils.toBN(1);
  845. beforeEach(async function () {
  846. this.ownable = await Ownable.new(this.manager.address);
  847. // add user to role
  848. await this.manager.$_grantRole(roleId, user, 0, 0);
  849. });
  850. it('initial state', async function () {
  851. expect(await this.ownable.owner()).to.be.equal(this.manager.address);
  852. });
  853. describe('Contract is closed', function () {
  854. beforeEach(async function () {
  855. await this.manager.$_setTargetClosed(this.ownable.address, true);
  856. });
  857. it('directly call: reverts', async function () {
  858. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [user]);
  859. });
  860. it('relayed call (with role): reverts', async function () {
  861. await expectRevertCustomError(
  862. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user }),
  863. 'AccessManagerUnauthorizedCall',
  864. [user, this.ownable.address, selector('$_checkOwner()')],
  865. );
  866. });
  867. it('relayed call (without role): reverts', async function () {
  868. await expectRevertCustomError(
  869. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other }),
  870. 'AccessManagerUnauthorizedCall',
  871. [other, this.ownable.address, selector('$_checkOwner()')],
  872. );
  873. });
  874. });
  875. describe('Contract is managed', function () {
  876. describe('function is open to specific role', function () {
  877. beforeEach(async function () {
  878. await this.manager.$_setTargetFunctionRole(this.ownable.address, selector('$_checkOwner()'), roleId);
  879. });
  880. it('directly call: reverts', async function () {
  881. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [
  882. user,
  883. ]);
  884. });
  885. it('relayed call (with role): success', async function () {
  886. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user });
  887. });
  888. it('relayed call (without role): reverts', async function () {
  889. await expectRevertCustomError(
  890. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other }),
  891. 'AccessManagerUnauthorizedCall',
  892. [other, this.ownable.address, selector('$_checkOwner()')],
  893. );
  894. });
  895. });
  896. describe('function is open to public role', function () {
  897. beforeEach(async function () {
  898. await this.manager.$_setTargetFunctionRole(this.ownable.address, selector('$_checkOwner()'), ROLES.PUBLIC);
  899. });
  900. it('directly call: reverts', async function () {
  901. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [
  902. user,
  903. ]);
  904. });
  905. it('relayed call (with role): success', async function () {
  906. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user });
  907. });
  908. it('relayed call (without role): success', async function () {
  909. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other });
  910. });
  911. });
  912. });
  913. });
  914. describe('authority update', function () {
  915. beforeEach(async function () {
  916. this.newManager = await AccessManager.new(admin);
  917. this.target = await AccessManagedTarget.new(this.manager.address);
  918. });
  919. it('admin can change authority', async function () {
  920. expect(await this.target.authority()).to.be.equal(this.manager.address);
  921. const { tx } = await this.manager.updateAuthority(this.target.address, this.newManager.address, { from: admin });
  922. await expectEvent.inTransaction(tx, this.target, 'AuthorityUpdated', { authority: this.newManager.address });
  923. expect(await this.target.authority()).to.be.equal(this.newManager.address);
  924. });
  925. it('cannot set an address without code as the authority', async function () {
  926. await expectRevertCustomError(
  927. this.manager.updateAuthority(this.target.address, user, { from: admin }),
  928. 'AccessManagedInvalidAuthority',
  929. [user],
  930. );
  931. });
  932. it('updateAuthority is restricted on manager', async function () {
  933. await expectRevertCustomError(
  934. this.manager.updateAuthority(this.target.address, this.newManager.address, { from: other }),
  935. 'AccessManagerUnauthorizedAccount',
  936. [other, ROLES.ADMIN],
  937. );
  938. });
  939. it('setAuthority is restricted on AccessManaged', async function () {
  940. await expectRevertCustomError(
  941. this.target.setAuthority(this.newManager.address, { from: admin }),
  942. 'AccessManagedUnauthorized',
  943. [admin],
  944. );
  945. });
  946. });
  947. // TODO:
  948. // - check opening/closing a contract
  949. // - check updating the contract delay
  950. // - check the delay applies to admin function
  951. describe.skip('contract modes', function () {});
  952. });