AccessManager.test.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. if (!delay || fnRole === ROLES.PUBLIC) this.skip(); // TODO: Fixed in #4613
  528. // setup
  529. await Promise.all([
  530. this.manager.$_setTargetClosed(this.target.address, closed),
  531. fnRole && this.manager.$_setTargetFunctionRole(this.target.address, selector('fnRestricted()'), fnRole),
  532. fnRole && this.manager.$_setTargetFunctionRole(this.target.address, selector('fnUnrestricted()'), fnRole),
  533. ...callerRoles
  534. .filter(roleId => roleId != ROLES.PUBLIC)
  535. .map(roleId => this.manager.$_grantRole(roleId, user, 0, delay ?? 0)),
  536. ]);
  537. // post setup checks
  538. expect(await this.manager.isTargetClosed(this.target.address)).to.be.equal(closed);
  539. if (fnRole) {
  540. expect(
  541. await this.manager.getTargetFunctionRole(this.target.address, selector('fnRestricted()')),
  542. ).to.be.bignumber.equal(fnRole);
  543. expect(
  544. await this.manager.getTargetFunctionRole(this.target.address, selector('fnUnrestricted()')),
  545. ).to.be.bignumber.equal(fnRole);
  546. }
  547. for (const roleId of callerRoles) {
  548. const access = await this.manager.getAccess(roleId, user);
  549. if (roleId == ROLES.PUBLIC) {
  550. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  551. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  552. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  553. expect(access[3]).to.be.bignumber.equal('0'); // effect
  554. } else {
  555. expect(access[0]).to.be.bignumber.gt('0'); // inRoleSince
  556. expect(access[1]).to.be.bignumber.eq(String(delay ?? 0)); // currentDelay
  557. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  558. expect(access[3]).to.be.bignumber.equal('0'); // effect
  559. }
  560. }
  561. });
  562. it('canCall', async function () {
  563. const result = await this.manager.canCall(user, this.target.address, selector('fnRestricted()'));
  564. expect(result[0]).to.be.equal(directSuccess);
  565. expect(result[1]).to.be.bignumber.equal(!directSuccess && indirectSuccess ? delay ?? '0' : '0');
  566. });
  567. it('Calling a non restricted function never revert', async function () {
  568. expectEvent(await this.target.fnUnrestricted({ from: user }), 'CalledUnrestricted', {
  569. caller: user,
  570. });
  571. });
  572. it(`Calling a restricted function directly should ${
  573. directSuccess ? 'succeed' : 'revert'
  574. }`, async function () {
  575. const promise = this.direct();
  576. if (directSuccess) {
  577. expectEvent(await promise, 'CalledRestricted', { caller: user });
  578. } else if (indirectSuccess) {
  579. await expectRevertCustomError(promise, 'AccessManagerNotScheduled', [this.opId]);
  580. } else {
  581. await expectRevertCustomError(promise, 'AccessManagedUnauthorized', [user]);
  582. }
  583. });
  584. it('Calling indirectly: only execute', async function () {
  585. // execute without schedule
  586. if (directSuccess) {
  587. const nonceBefore = await this.manager.getNonce(this.opId);
  588. const { receipt, tx } = await this.execute();
  589. expectEvent.notEmitted(receipt, 'OperationExecuted', { operationId: this.opId });
  590. await expectEvent.inTransaction(tx, this.target, 'CalledRestricted', { caller: this.manager.address });
  591. // nonce is not modified
  592. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore);
  593. } else if (indirectSuccess) {
  594. await expectRevertCustomError(this.execute(), 'AccessManagerNotScheduled', [this.opId]);
  595. } else {
  596. await expectRevertCustomError(this.execute(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  597. }
  598. });
  599. it('Calling indirectly: schedule and execute', async function () {
  600. if (directSuccess || indirectSuccess) {
  601. const nonceBefore = await this.manager.getNonce(this.opId);
  602. const { receipt } = await this.schedule();
  603. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  604. expectEvent(receipt, 'OperationScheduled', {
  605. operationId: this.opId,
  606. caller: user,
  607. target: this.call[0],
  608. data: this.call[1],
  609. });
  610. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  611. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(
  612. timestamp.add(directSuccess ? web3.utils.toBN(0) : delay),
  613. );
  614. // nonce is incremented
  615. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  616. // execute without wait
  617. if (directSuccess) {
  618. const { receipt, tx } = await this.execute();
  619. await expectEvent.inTransaction(tx, this.target, 'CalledRestricted', { caller: this.manager.address });
  620. if (delay && fnRole !== ROLES.PUBLIC) {
  621. expectEvent(receipt, 'OperationExecuted', { operationId: this.opId });
  622. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  623. }
  624. // nonce is not modified by execute
  625. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  626. } else if (indirectSuccess) {
  627. await expectRevertCustomError(this.execute(), 'AccessManagerNotReady', [this.opId]);
  628. } else {
  629. await expectRevertCustomError(this.execute(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  630. }
  631. } else {
  632. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  633. }
  634. });
  635. it('Calling indirectly: schedule wait and execute', async function () {
  636. if (directSuccess || indirectSuccess) {
  637. const nonceBefore = await this.manager.getNonce(this.opId);
  638. const { receipt } = await this.schedule();
  639. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  640. expectEvent(receipt, 'OperationScheduled', {
  641. operationId: this.opId,
  642. caller: user,
  643. target: this.call[0],
  644. data: this.call[1],
  645. });
  646. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  647. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(
  648. timestamp.add(directSuccess ? web3.utils.toBN(0) : delay),
  649. );
  650. // nonce is incremented
  651. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  652. // wait
  653. await time.increase(delay ?? 0);
  654. // execute without wait
  655. if (directSuccess || indirectSuccess) {
  656. const { receipt, tx } = await this.execute();
  657. await expectEvent.inTransaction(tx, this.target, 'CalledRestricted', { caller: this.manager.address });
  658. if (delay && fnRole !== ROLES.PUBLIC) {
  659. expectEvent(receipt, 'OperationExecuted', { operationId: this.opId });
  660. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  661. }
  662. // nonce is not modified by execute
  663. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  664. } else {
  665. await expectRevertCustomError(this.execute(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  666. }
  667. } else {
  668. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  669. }
  670. });
  671. it('Calling directly: schedule and call', async function () {
  672. if (directSuccess || indirectSuccess) {
  673. const nonceBefore = await this.manager.getNonce(this.opId);
  674. const { receipt } = await this.schedule();
  675. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  676. expectEvent(receipt, 'OperationScheduled', {
  677. operationId: this.opId,
  678. caller: user,
  679. target: this.call[0],
  680. data: this.call[1],
  681. });
  682. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  683. const schedule = timestamp.add(directSuccess ? web3.utils.toBN(0) : delay);
  684. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  685. // nonce is incremented
  686. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  687. // execute without wait
  688. const promise = this.direct();
  689. if (directSuccess) {
  690. expectEvent(await promise, 'CalledRestricted', { caller: user });
  691. // schedule is not reset
  692. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  693. // nonce is not modified by execute
  694. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  695. } else if (indirectSuccess) {
  696. await expectRevertCustomError(promise, 'AccessManagerNotReady', [this.opId]);
  697. } else {
  698. await expectRevertCustomError(promise, 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  699. }
  700. } else {
  701. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  702. }
  703. });
  704. it('Calling directly: schedule wait and call', async function () {
  705. if (directSuccess || indirectSuccess) {
  706. const nonceBefore = await this.manager.getNonce(this.opId);
  707. const { receipt } = await this.schedule();
  708. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  709. expectEvent(receipt, 'OperationScheduled', {
  710. operationId: this.opId,
  711. caller: user,
  712. target: this.call[0],
  713. data: this.call[1],
  714. });
  715. // if can call directly, delay should be 0. Otherwise, the delay should be applied
  716. const schedule = timestamp.add(directSuccess ? web3.utils.toBN(0) : delay);
  717. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  718. // nonce is incremented
  719. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  720. // wait
  721. await time.increase(delay ?? 0);
  722. // execute without wait
  723. const promise = await this.direct();
  724. if (directSuccess) {
  725. expectEvent(await promise, 'CalledRestricted', { caller: user });
  726. // schedule is not reset
  727. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(schedule);
  728. // nonce is not modified by execute
  729. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  730. } else if (indirectSuccess) {
  731. const receipt = await promise;
  732. expectEvent(receipt, 'CalledRestricted', { caller: user });
  733. await expectEvent.inTransaction(receipt.tx, this.manager, 'OperationExecuted', {
  734. operationId: this.opId,
  735. });
  736. // schedule is reset
  737. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  738. // nonce is not modified by execute
  739. expect(await this.manager.getNonce(this.opId)).to.be.bignumber.equal(nonceBefore.addn(1));
  740. } else {
  741. await expectRevertCustomError(this.direct(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  742. }
  743. } else {
  744. await expectRevertCustomError(this.schedule(), 'AccessManagerUnauthorizedCall', [user, ...this.call]);
  745. }
  746. });
  747. it('Scheduling for later than needed'); // TODO
  748. });
  749. }
  750. });
  751. describe('Indirect execution corner-cases', async function () {
  752. beforeEach(async function () {
  753. await this.manager.$_setTargetFunctionRole(this.target.address, this.callData, ROLES.SOME);
  754. await this.manager.$_grantRole(ROLES.SOME, user, 0, executeDelay);
  755. });
  756. it('Checking canCall when caller is the manager depend on the _executionId', async function () {
  757. const result = await this.manager.canCall(this.manager.address, this.target.address, '0x00000000');
  758. expect(result[0]).to.be.false;
  759. expect(result[1]).to.be.bignumber.equal('0');
  760. });
  761. it('Cannot execute earlier', async function () {
  762. const { receipt } = await this.schedule();
  763. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  764. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal(timestamp.add(executeDelay));
  765. // too early
  766. await helpers.time.setNextBlockTimestamp(timestamp.add(executeDelay).subn(1));
  767. await expectRevertCustomError(this.execute(), 'AccessManagerNotReady', [this.opId]);
  768. // the revert happened one second before the execution delay expired
  769. expect(await time.latest()).to.be.bignumber.equal(timestamp.add(executeDelay).subn(1));
  770. // ok
  771. await helpers.time.setNextBlockTimestamp(timestamp.add(executeDelay));
  772. await this.execute();
  773. // the success happened when the delay was reached (earliest possible)
  774. expect(await time.latest()).to.be.bignumber.equal(timestamp.add(executeDelay));
  775. });
  776. it('Cannot schedule an already scheduled operation', async function () {
  777. const { receipt } = await this.schedule();
  778. expectEvent(receipt, 'OperationScheduled', {
  779. operationId: this.opId,
  780. caller: user,
  781. target: this.call[0],
  782. data: this.call[1],
  783. });
  784. await expectRevertCustomError(this.schedule(), 'AccessManagerAlreadyScheduled', [this.opId]);
  785. });
  786. it('Cannot cancel an operation that is not scheduled', async function () {
  787. await expectRevertCustomError(this.cancel(), 'AccessManagerNotScheduled', [this.opId]);
  788. });
  789. it('Cannot cancel an operation that is already executed', async function () {
  790. await this.schedule();
  791. await time.increase(executeDelay);
  792. await this.execute();
  793. await expectRevertCustomError(this.cancel(), 'AccessManagerNotScheduled', [this.opId]);
  794. });
  795. it('Scheduler can cancel', async function () {
  796. await this.schedule();
  797. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  798. expectEvent(await this.cancel({ from: manager }), 'OperationCanceled', { operationId: this.opId });
  799. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  800. });
  801. it('Guardian can cancel', async function () {
  802. await this.schedule();
  803. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  804. expectEvent(await this.cancel({ from: manager }), 'OperationCanceled', { operationId: this.opId });
  805. expect(await this.manager.getSchedule(this.opId)).to.be.bignumber.equal('0');
  806. });
  807. it('Cancel is restricted', async function () {
  808. await this.schedule();
  809. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  810. await expectRevertCustomError(this.cancel({ from: other }), 'AccessManagerUnauthorizedCancel', [
  811. other,
  812. user,
  813. ...this.call,
  814. ]);
  815. expect(await this.manager.getSchedule(this.opId)).to.not.be.bignumber.equal('0');
  816. });
  817. it('Can re-schedule after execution', async function () {
  818. await this.schedule();
  819. await time.increase(executeDelay);
  820. await this.execute();
  821. // reschedule
  822. const { receipt } = await this.schedule();
  823. expectEvent(receipt, 'OperationScheduled', {
  824. operationId: this.opId,
  825. caller: user,
  826. target: this.call[0],
  827. data: this.call[1],
  828. });
  829. });
  830. it('Can re-schedule after cancel', async function () {
  831. await this.schedule();
  832. await this.cancel();
  833. // reschedule
  834. const { receipt } = await this.schedule();
  835. expectEvent(receipt, 'OperationScheduled', {
  836. operationId: this.opId,
  837. caller: user,
  838. target: this.call[0],
  839. data: this.call[1],
  840. });
  841. });
  842. });
  843. });
  844. describe('with Ownable target contract', function () {
  845. const roleId = web3.utils.toBN(1);
  846. beforeEach(async function () {
  847. this.ownable = await Ownable.new(this.manager.address);
  848. // add user to role
  849. await this.manager.$_grantRole(roleId, user, 0, 0);
  850. });
  851. it('initial state', async function () {
  852. expect(await this.ownable.owner()).to.be.equal(this.manager.address);
  853. });
  854. describe('Contract is closed', function () {
  855. beforeEach(async function () {
  856. await this.manager.$_setTargetClosed(this.ownable.address, true);
  857. });
  858. it('directly call: reverts', async function () {
  859. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [user]);
  860. });
  861. it('relayed call (with role): reverts', async function () {
  862. await expectRevertCustomError(
  863. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user }),
  864. 'AccessManagerUnauthorizedCall',
  865. [user, this.ownable.address, selector('$_checkOwner()')],
  866. );
  867. });
  868. it('relayed call (without role): reverts', async function () {
  869. await expectRevertCustomError(
  870. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other }),
  871. 'AccessManagerUnauthorizedCall',
  872. [other, this.ownable.address, selector('$_checkOwner()')],
  873. );
  874. });
  875. });
  876. describe('Contract is managed', function () {
  877. describe('function is open to specific role', function () {
  878. beforeEach(async function () {
  879. await this.manager.$_setTargetFunctionRole(this.ownable.address, selector('$_checkOwner()'), roleId);
  880. });
  881. it('directly call: reverts', async function () {
  882. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [
  883. user,
  884. ]);
  885. });
  886. it('relayed call (with role): success', async function () {
  887. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user });
  888. });
  889. it('relayed call (without role): reverts', async function () {
  890. await expectRevertCustomError(
  891. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other }),
  892. 'AccessManagerUnauthorizedCall',
  893. [other, this.ownable.address, selector('$_checkOwner()')],
  894. );
  895. });
  896. });
  897. describe('function is open to public role', function () {
  898. beforeEach(async function () {
  899. await this.manager.$_setTargetFunctionRole(this.ownable.address, selector('$_checkOwner()'), ROLES.PUBLIC);
  900. });
  901. it('directly call: reverts', async function () {
  902. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [
  903. user,
  904. ]);
  905. });
  906. it('relayed call (with role): success', async function () {
  907. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user });
  908. });
  909. it('relayed call (without role): success', async function () {
  910. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other });
  911. });
  912. });
  913. });
  914. });
  915. describe('authority update', function () {
  916. beforeEach(async function () {
  917. this.newManager = await AccessManager.new(admin);
  918. this.target = await AccessManagedTarget.new(this.manager.address);
  919. });
  920. it('admin can change authority', async function () {
  921. expect(await this.target.authority()).to.be.equal(this.manager.address);
  922. const { tx } = await this.manager.updateAuthority(this.target.address, this.newManager.address, { from: admin });
  923. await expectEvent.inTransaction(tx, this.target, 'AuthorityUpdated', { authority: this.newManager.address });
  924. expect(await this.target.authority()).to.be.equal(this.newManager.address);
  925. });
  926. it('cannot set an address without code as the authority', async function () {
  927. await expectRevertCustomError(
  928. this.manager.updateAuthority(this.target.address, user, { from: admin }),
  929. 'AccessManagedInvalidAuthority',
  930. [user],
  931. );
  932. });
  933. it('updateAuthority is restricted on manager', async function () {
  934. await expectRevertCustomError(
  935. this.manager.updateAuthority(this.target.address, this.newManager.address, { from: other }),
  936. 'AccessManagerUnauthorizedAccount',
  937. [other, ROLES.ADMIN],
  938. );
  939. });
  940. it('setAuthority is restricted on AccessManaged', async function () {
  941. await expectRevertCustomError(
  942. this.target.setAuthority(this.newManager.address, { from: admin }),
  943. 'AccessManagedUnauthorized',
  944. [admin],
  945. );
  946. });
  947. });
  948. // TODO:
  949. // - check opening/closing a contract
  950. // - check updating the contract delay
  951. // - check the delay applies to admin function
  952. describe.skip('contract modes', function () {});
  953. });