AccessManager.test.js 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690
  1. const { web3 } = require('hardhat');
  2. const { constants, expectEvent, time, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { expectRevertCustomError } = require('../../helpers/customError');
  4. const { selector } = require('../../helpers/methods');
  5. const { clockFromReceipt } = require('../../helpers/time');
  6. const {
  7. buildBaseRoles,
  8. formatAccess,
  9. EXPIRATION,
  10. MINSETBACK,
  11. EXECUTION_ID_STORAGE_SLOT,
  12. CONSUMING_SCHEDULE_STORAGE_SLOT,
  13. scheduleOperation,
  14. hashOperation,
  15. } = require('../../helpers/access-manager');
  16. const {
  17. shouldBehaveLikeDelayedAdminOperation,
  18. shouldBehaveLikeNotDelayedAdminOperation,
  19. shouldBehaveLikeRoleAdminOperation,
  20. shouldBehaveLikeAManagedRestrictedOperation,
  21. } = require('./AccessManager.behavior');
  22. const {
  23. LIKE_COMMON_SCHEDULABLE,
  24. testAsClosable,
  25. testAsDelay,
  26. testAsSchedulableOperation,
  27. testAsCanCall,
  28. testAsHasRole,
  29. testAsGetAccess,
  30. } = require('./AccessManager.predicate');
  31. const { default: Wallet } = require('ethereumjs-wallet');
  32. const {
  33. mine,
  34. time: { setNextBlockTimestamp },
  35. getStorageAt,
  36. } = require('@nomicfoundation/hardhat-network-helpers');
  37. const { MAX_UINT48 } = require('../../helpers/constants');
  38. const { impersonate } = require('../../helpers/account');
  39. const AccessManager = artifacts.require('$AccessManager');
  40. const AccessManagedTarget = artifacts.require('$AccessManagedTarget');
  41. const Ownable = artifacts.require('$Ownable');
  42. const someAddress = Wallet.generate().getChecksumAddressString();
  43. // This test suite is made using the following tools:
  44. //
  45. // * Predicates: Functions with common conditional setups without assertions.
  46. // * Behaviors: Functions with common assertions.
  47. //
  48. // The behavioral tests are built by composing predicates and are used as templates
  49. // for testing access to restricted functions.
  50. //
  51. // Similarly, unit tests in this suite will use predicates to test subsets of these
  52. // behaviors and are helped by common assertions provided for some of the predicates.
  53. //
  54. // The predicates can be identified by the `testAs*` prefix while the behaviors
  55. // are prefixed with `shouldBehave*`. The common assertions for predicates are
  56. // defined as constants.
  57. contract('AccessManager', function (accounts) {
  58. const [admin, manager, guardian, member, user, other] = accounts;
  59. beforeEach(async function () {
  60. this.roles = buildBaseRoles();
  61. // Add members
  62. this.roles.ADMIN.members = [admin];
  63. this.roles.SOME_ADMIN.members = [manager];
  64. this.roles.SOME_GUARDIAN.members = [guardian];
  65. this.roles.SOME.members = [member];
  66. this.roles.PUBLIC.members = [admin, manager, guardian, member, user, other];
  67. this.manager = await AccessManager.new(admin);
  68. this.target = await AccessManagedTarget.new(this.manager.address);
  69. for (const { id: roleId, admin, guardian, members } of Object.values(this.roles)) {
  70. if (roleId === this.roles.PUBLIC.id) continue; // Every address belong to public and is locked
  71. if (roleId === this.roles.ADMIN.id) continue; // Admin set during construction and is locked
  72. // Set admin role avoiding default
  73. if (admin.id !== this.roles.ADMIN.id) {
  74. await this.manager.$_setRoleAdmin(roleId, admin.id);
  75. }
  76. // Set guardian role avoiding default
  77. if (guardian.id !== this.roles.ADMIN.id) {
  78. await this.manager.$_setRoleGuardian(roleId, guardian.id);
  79. }
  80. // Grant role to members
  81. for (const member of members) {
  82. await this.manager.$_grantRole(roleId, member, 0, 0);
  83. }
  84. }
  85. });
  86. describe('during construction', function () {
  87. it('grants admin role to initialAdmin', async function () {
  88. const manager = await AccessManager.new(other);
  89. expect(await manager.hasRole(this.roles.ADMIN.id, other).then(formatAccess)).to.be.deep.equal([true, '0']);
  90. });
  91. it('rejects zero address for initialAdmin', async function () {
  92. await expectRevertCustomError(AccessManager.new(constants.ZERO_ADDRESS), 'AccessManagerInvalidInitialAdmin', [
  93. constants.ZERO_ADDRESS,
  94. ]);
  95. });
  96. it('initializes setup roles correctly', async function () {
  97. for (const { id: roleId, admin, guardian, members } of Object.values(this.roles)) {
  98. expect(await this.manager.getRoleAdmin(roleId)).to.be.bignumber.equal(admin.id);
  99. expect(await this.manager.getRoleGuardian(roleId)).to.be.bignumber.equal(guardian.id);
  100. for (const user of this.roles.PUBLIC.members) {
  101. expect(await this.manager.hasRole(roleId, user).then(formatAccess)).to.be.deep.equal([
  102. members.includes(user),
  103. '0',
  104. ]);
  105. }
  106. }
  107. });
  108. });
  109. describe('getters', function () {
  110. describe('#canCall', function () {
  111. beforeEach('set calldata', function () {
  112. this.calldata = '0x12345678';
  113. this.role = { id: web3.utils.toBN(379204) };
  114. });
  115. testAsCanCall({
  116. closed() {
  117. it('should return false and no delay', async function () {
  118. const { immediate, delay } = await this.manager.canCall(
  119. someAddress,
  120. this.target.address,
  121. this.calldata.substring(0, 10),
  122. );
  123. expect(immediate).to.be.equal(false);
  124. expect(delay).to.be.bignumber.equal('0');
  125. });
  126. },
  127. open: {
  128. callerIsTheManager: {
  129. executing() {
  130. it('should return true and no delay', async function () {
  131. const { immediate, delay } = await this.manager.canCall(
  132. this.caller,
  133. this.target.address,
  134. this.calldata.substring(0, 10),
  135. );
  136. expect(immediate).to.be.equal(true);
  137. expect(delay).to.be.bignumber.equal('0');
  138. });
  139. },
  140. notExecuting() {
  141. it('should return false and no delay', async function () {
  142. const { immediate, delay } = await this.manager.canCall(
  143. this.caller,
  144. this.target.address,
  145. this.calldata.substring(0, 10),
  146. );
  147. expect(immediate).to.be.equal(false);
  148. expect(delay).to.be.bignumber.equal('0');
  149. });
  150. },
  151. },
  152. callerIsNotTheManager: {
  153. publicRoleIsRequired() {
  154. it('should return true and no delay', async function () {
  155. const { immediate, delay } = await this.manager.canCall(
  156. this.caller,
  157. this.target.address,
  158. this.calldata.substring(0, 10),
  159. );
  160. expect(immediate).to.be.equal(true);
  161. expect(delay).to.be.bignumber.equal('0');
  162. });
  163. },
  164. specificRoleIsRequired: {
  165. requiredRoleIsGranted: {
  166. roleGrantingIsDelayed: {
  167. callerHasAnExecutionDelay: {
  168. beforeGrantDelay() {
  169. beforeEach('consume previously set grant delay', async function () {
  170. // Consume previously set delay
  171. await mine();
  172. });
  173. it('should return false and no execution delay', async function () {
  174. const { immediate, delay } = await this.manager.canCall(
  175. this.caller,
  176. this.target.address,
  177. this.calldata.substring(0, 10),
  178. );
  179. expect(immediate).to.be.equal(false);
  180. expect(delay).to.be.bignumber.equal('0');
  181. });
  182. },
  183. afterGrantDelay() {
  184. beforeEach('consume previously set grant delay', async function () {
  185. // Consume previously set delay
  186. await mine();
  187. this.scheduleIn = this.executionDelay; // For testAsSchedulableOperation
  188. });
  189. testAsSchedulableOperation({
  190. scheduled: {
  191. before() {
  192. beforeEach('consume previously set delay', async function () {
  193. // Consume previously set delay
  194. await mine();
  195. });
  196. it('should return false and execution delay', async function () {
  197. const { immediate, delay } = await this.manager.canCall(
  198. this.caller,
  199. this.target.address,
  200. this.calldata.substring(0, 10),
  201. );
  202. expect(immediate).to.be.equal(false);
  203. expect(delay).to.be.bignumber.equal(this.executionDelay);
  204. });
  205. },
  206. after() {
  207. beforeEach('consume previously set delay', async function () {
  208. // Consume previously set delay
  209. await mine();
  210. });
  211. it('should return false and execution delay', async function () {
  212. const { immediate, delay } = await this.manager.canCall(
  213. this.caller,
  214. this.target.address,
  215. this.calldata.substring(0, 10),
  216. );
  217. expect(immediate).to.be.equal(false);
  218. expect(delay).to.be.bignumber.equal(this.executionDelay);
  219. });
  220. },
  221. expired() {
  222. beforeEach('consume previously set delay', async function () {
  223. // Consume previously set delay
  224. await mine();
  225. });
  226. it('should return false and execution delay', async function () {
  227. const { immediate, delay } = await this.manager.canCall(
  228. this.caller,
  229. this.target.address,
  230. this.calldata.substring(0, 10),
  231. );
  232. expect(immediate).to.be.equal(false);
  233. expect(delay).to.be.bignumber.equal(this.executionDelay);
  234. });
  235. },
  236. },
  237. notScheduled() {
  238. it('should return false and execution delay', async function () {
  239. const { immediate, delay } = await this.manager.canCall(
  240. this.caller,
  241. this.target.address,
  242. this.calldata.substring(0, 10),
  243. );
  244. expect(immediate).to.be.equal(false);
  245. expect(delay).to.be.bignumber.equal(this.executionDelay);
  246. });
  247. },
  248. });
  249. },
  250. },
  251. callerHasNoExecutionDelay: {
  252. beforeGrantDelay() {
  253. beforeEach('consume previously set grant delay', async function () {
  254. // Consume previously set delay
  255. await mine();
  256. });
  257. it('should return false and no execution delay', async function () {
  258. const { immediate, delay } = await this.manager.canCall(
  259. this.caller,
  260. this.target.address,
  261. this.calldata.substring(0, 10),
  262. );
  263. expect(immediate).to.be.equal(false);
  264. expect(delay).to.be.bignumber.equal('0');
  265. });
  266. },
  267. afterGrantDelay() {
  268. beforeEach('consume previously set grant delay', async function () {
  269. // Consume previously set delay
  270. await mine();
  271. });
  272. it('should return true and no execution delay', async function () {
  273. const { immediate, delay } = await this.manager.canCall(
  274. this.caller,
  275. this.target.address,
  276. this.calldata.substring(0, 10),
  277. );
  278. expect(immediate).to.be.equal(true);
  279. expect(delay).to.be.bignumber.equal('0');
  280. });
  281. },
  282. },
  283. },
  284. roleGrantingIsNotDelayed: {
  285. callerHasAnExecutionDelay() {
  286. it('should return false and execution delay', async function () {
  287. const { immediate, delay } = await this.manager.canCall(
  288. this.caller,
  289. this.target.address,
  290. this.calldata.substring(0, 10),
  291. );
  292. expect(immediate).to.be.equal(false);
  293. expect(delay).to.be.bignumber.equal(this.executionDelay);
  294. });
  295. },
  296. callerHasNoExecutionDelay() {
  297. it('should return true and no execution delay', async function () {
  298. const { immediate, delay } = await this.manager.canCall(
  299. this.caller,
  300. this.target.address,
  301. this.calldata.substring(0, 10),
  302. );
  303. expect(immediate).to.be.equal(true);
  304. expect(delay).to.be.bignumber.equal('0');
  305. });
  306. },
  307. },
  308. },
  309. requiredRoleIsNotGranted() {
  310. it('should return false and no execution delay', async function () {
  311. const { immediate, delay } = await this.manager.canCall(
  312. this.caller,
  313. this.target.address,
  314. this.calldata.substring(0, 10),
  315. );
  316. expect(immediate).to.be.equal(false);
  317. expect(delay).to.be.bignumber.equal('0');
  318. });
  319. },
  320. },
  321. },
  322. },
  323. });
  324. });
  325. describe('#expiration', function () {
  326. it('has a 7 days default expiration', async function () {
  327. expect(await this.manager.expiration()).to.be.bignumber.equal(EXPIRATION);
  328. });
  329. });
  330. describe('#minSetback', function () {
  331. it('has a 5 days default minimum setback', async function () {
  332. expect(await this.manager.minSetback()).to.be.bignumber.equal(MINSETBACK);
  333. });
  334. });
  335. describe('#isTargetClosed', function () {
  336. testAsClosable({
  337. closed() {
  338. it('returns true', async function () {
  339. expect(await this.manager.isTargetClosed(this.target.address)).to.be.equal(true);
  340. });
  341. },
  342. open() {
  343. it('returns false', async function () {
  344. expect(await this.manager.isTargetClosed(this.target.address)).to.be.equal(false);
  345. });
  346. },
  347. });
  348. });
  349. describe('#getTargetFunctionRole', function () {
  350. const methodSelector = selector('something(address,bytes)');
  351. it('returns the target function role', async function () {
  352. const roleId = web3.utils.toBN(21498);
  353. await this.manager.$_setTargetFunctionRole(this.target.address, methodSelector, roleId);
  354. expect(await this.manager.getTargetFunctionRole(this.target.address, methodSelector)).to.be.bignumber.equal(
  355. roleId,
  356. );
  357. });
  358. it('returns the ADMIN role if not set', async function () {
  359. expect(await this.manager.getTargetFunctionRole(this.target.address, methodSelector)).to.be.bignumber.equal(
  360. this.roles.ADMIN.id,
  361. );
  362. });
  363. });
  364. describe('#getTargetAdminDelay', function () {
  365. describe('when the target admin delay is setup', function () {
  366. beforeEach('set target admin delay', async function () {
  367. this.oldDelay = await this.manager.getTargetAdminDelay(this.target.address);
  368. this.newDelay = time.duration.days(10);
  369. await this.manager.$_setTargetAdminDelay(this.target.address, this.newDelay);
  370. this.delay = MINSETBACK; // For testAsDelay
  371. });
  372. testAsDelay('effect', {
  373. before() {
  374. beforeEach('consume previously set grant delay', async function () {
  375. // Consume previously set delay
  376. await mine();
  377. });
  378. it('returns the old target admin delay', async function () {
  379. expect(await this.manager.getTargetAdminDelay(this.target.address)).to.be.bignumber.equal(this.oldDelay);
  380. });
  381. },
  382. after() {
  383. beforeEach('consume previously set grant delay', async function () {
  384. // Consume previously set delay
  385. await mine();
  386. });
  387. it('returns the new target admin delay', async function () {
  388. expect(await this.manager.getTargetAdminDelay(this.target.address)).to.be.bignumber.equal(this.newDelay);
  389. });
  390. },
  391. });
  392. });
  393. it('returns the 0 if not set', async function () {
  394. expect(await this.manager.getTargetAdminDelay(this.target.address)).to.be.bignumber.equal('0');
  395. });
  396. });
  397. describe('#getRoleAdmin', function () {
  398. const roleId = web3.utils.toBN(5234907);
  399. it('returns the role admin', async function () {
  400. const adminId = web3.utils.toBN(789433);
  401. await this.manager.$_setRoleAdmin(roleId, adminId);
  402. expect(await this.manager.getRoleAdmin(roleId)).to.be.bignumber.equal(adminId);
  403. });
  404. it('returns the ADMIN role if not set', async function () {
  405. expect(await this.manager.getRoleAdmin(roleId)).to.be.bignumber.equal(this.roles.ADMIN.id);
  406. });
  407. });
  408. describe('#getRoleGuardian', function () {
  409. const roleId = web3.utils.toBN(5234907);
  410. it('returns the role guardian', async function () {
  411. const guardianId = web3.utils.toBN(789433);
  412. await this.manager.$_setRoleGuardian(roleId, guardianId);
  413. expect(await this.manager.getRoleGuardian(roleId)).to.be.bignumber.equal(guardianId);
  414. });
  415. it('returns the ADMIN role if not set', async function () {
  416. expect(await this.manager.getRoleGuardian(roleId)).to.be.bignumber.equal(this.roles.ADMIN.id);
  417. });
  418. });
  419. describe('#getRoleGrantDelay', function () {
  420. const roleId = web3.utils.toBN(9248439);
  421. describe('when the grant admin delay is setup', function () {
  422. beforeEach('set grant admin delay', async function () {
  423. this.oldDelay = await this.manager.getRoleGrantDelay(roleId);
  424. this.newDelay = time.duration.days(11);
  425. await this.manager.$_setGrantDelay(roleId, this.newDelay);
  426. this.delay = MINSETBACK; // For testAsDelay
  427. });
  428. testAsDelay('grant', {
  429. before() {
  430. beforeEach('consume previously set grant delay', async function () {
  431. // Consume previously set delay
  432. await mine();
  433. });
  434. it('returns the old role grant delay', async function () {
  435. expect(await this.manager.getRoleGrantDelay(roleId)).to.be.bignumber.equal(this.oldDelay);
  436. });
  437. },
  438. after() {
  439. beforeEach('consume previously set grant delay', async function () {
  440. // Consume previously set delay
  441. await mine();
  442. });
  443. it('returns the new role grant delay', async function () {
  444. expect(await this.manager.getRoleGrantDelay(roleId)).to.be.bignumber.equal(this.newDelay);
  445. });
  446. },
  447. });
  448. });
  449. it('returns 0 if delay is not set', async function () {
  450. expect(await this.manager.getTargetAdminDelay(this.target.address)).to.be.bignumber.equal('0');
  451. });
  452. });
  453. describe('#getAccess', function () {
  454. beforeEach('set role', function () {
  455. this.role = { id: web3.utils.toBN(9452) };
  456. this.caller = user;
  457. });
  458. testAsGetAccess({
  459. requiredRoleIsGranted: {
  460. roleGrantingIsDelayed: {
  461. callerHasAnExecutionDelay: {
  462. beforeGrantDelay() {
  463. beforeEach('consume previously set grant delay', async function () {
  464. // Consume previously set delay
  465. await mine();
  466. });
  467. it('role is not in effect and execution delay is set', async function () {
  468. const access = await this.manager.getAccess(this.role.id, this.caller);
  469. expect(access[0]).to.be.bignumber.equal(this.delayEffect); // inEffectSince
  470. expect(access[1]).to.be.bignumber.equal(this.executionDelay); // currentDelay
  471. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  472. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  473. // Not in effect yet
  474. expect(await time.latest()).to.be.bignumber.lt(access[0]);
  475. });
  476. },
  477. afterGrantDelay() {
  478. beforeEach('consume previously set grant delay', async function () {
  479. // Consume previously set delay
  480. await mine();
  481. });
  482. it('access has role in effect and execution delay is set', async function () {
  483. const access = await this.manager.getAccess(this.role.id, this.caller);
  484. expect(access[0]).to.be.bignumber.equal(this.delayEffect); // inEffectSince
  485. expect(access[1]).to.be.bignumber.equal(this.executionDelay); // currentDelay
  486. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  487. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  488. // Already in effect
  489. expect(await time.latest()).to.be.bignumber.equal(access[0]);
  490. });
  491. },
  492. },
  493. callerHasNoExecutionDelay: {
  494. beforeGrantDelay() {
  495. beforeEach('consume previously set grant delay', async function () {
  496. // Consume previously set delay
  497. await mine();
  498. });
  499. it('access has role not in effect without execution delay', async function () {
  500. const access = await this.manager.getAccess(this.role.id, this.caller);
  501. expect(access[0]).to.be.bignumber.equal(this.delayEffect); // inEffectSince
  502. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  503. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  504. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  505. // Not in effect yet
  506. expect(await time.latest()).to.be.bignumber.lt(access[0]);
  507. });
  508. },
  509. afterGrantDelay() {
  510. beforeEach('consume previously set grant delay', async function () {
  511. // Consume previously set delay
  512. await mine();
  513. });
  514. it('role is in effect without execution delay', async function () {
  515. const access = await this.manager.getAccess(this.role.id, this.caller);
  516. expect(access[0]).to.be.bignumber.equal(this.delayEffect); // inEffectSince
  517. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  518. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  519. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  520. // Already in effect
  521. expect(await time.latest()).to.be.bignumber.equal(access[0]);
  522. });
  523. },
  524. },
  525. },
  526. roleGrantingIsNotDelayed: {
  527. callerHasAnExecutionDelay() {
  528. it('access has role in effect and execution delay is set', async function () {
  529. const access = await this.manager.getAccess(this.role.id, this.caller);
  530. expect(access[0]).to.be.bignumber.equal(await time.latest()); // inEffectSince
  531. expect(access[1]).to.be.bignumber.equal(this.executionDelay); // currentDelay
  532. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  533. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  534. // Already in effect
  535. expect(await time.latest()).to.be.bignumber.equal(access[0]);
  536. });
  537. },
  538. callerHasNoExecutionDelay() {
  539. it('access has role in effect without execution delay', async function () {
  540. const access = await this.manager.getAccess(this.role.id, this.caller);
  541. expect(access[0]).to.be.bignumber.equal(await time.latest()); // inEffectSince
  542. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  543. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  544. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  545. // Already in effect
  546. expect(await time.latest()).to.be.bignumber.equal(access[0]);
  547. });
  548. },
  549. },
  550. },
  551. requiredRoleIsNotGranted() {
  552. it('has empty access', async function () {
  553. const access = await this.manager.getAccess(this.role.id, this.caller);
  554. expect(access[0]).to.be.bignumber.equal('0'); // inEffectSince
  555. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  556. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  557. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  558. });
  559. },
  560. });
  561. });
  562. describe('#hasRole', function () {
  563. beforeEach('setup testAsHasRole', function () {
  564. this.role = { id: web3.utils.toBN(49832) };
  565. this.calldata = '0x1234';
  566. this.caller = user;
  567. });
  568. testAsHasRole({
  569. publicRoleIsRequired() {
  570. it('has PUBLIC role', async function () {
  571. const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
  572. expect(isMember).to.be.true;
  573. expect(executionDelay).to.be.bignumber.eq('0');
  574. });
  575. },
  576. specificRoleIsRequired: {
  577. requiredRoleIsGranted: {
  578. roleGrantingIsDelayed: {
  579. callerHasAnExecutionDelay: {
  580. beforeGrantDelay() {
  581. beforeEach('consume previously set grant delay', async function () {
  582. // Consume previously set delay
  583. await mine();
  584. });
  585. it('does not have role but execution delay', async function () {
  586. const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
  587. expect(isMember).to.be.false;
  588. expect(executionDelay).to.be.bignumber.eq(this.executionDelay);
  589. });
  590. },
  591. afterGrantDelay() {
  592. beforeEach('consume previously set grant delay', async function () {
  593. // Consume previously set delay
  594. await mine();
  595. });
  596. it('has role and execution delay', async function () {
  597. const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
  598. expect(isMember).to.be.true;
  599. expect(executionDelay).to.be.bignumber.eq(this.executionDelay);
  600. });
  601. },
  602. },
  603. callerHasNoExecutionDelay: {
  604. beforeGrantDelay() {
  605. beforeEach('consume previously set grant delay', async function () {
  606. // Consume previously set delay
  607. await mine();
  608. });
  609. it('does not have role nor execution delay', async function () {
  610. const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
  611. expect(isMember).to.be.false;
  612. expect(executionDelay).to.be.bignumber.eq('0');
  613. });
  614. },
  615. afterGrantDelay() {
  616. beforeEach('consume previously set grant delay', async function () {
  617. // Consume previously set delay
  618. await mine();
  619. });
  620. it('has role and no execution delay', async function () {
  621. const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
  622. expect(isMember).to.be.true;
  623. expect(executionDelay).to.be.bignumber.eq('0');
  624. });
  625. },
  626. },
  627. },
  628. roleGrantingIsNotDelayed: {
  629. callerHasAnExecutionDelay() {
  630. it('has role and execution delay', async function () {
  631. const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
  632. expect(isMember).to.be.true;
  633. expect(executionDelay).to.be.bignumber.eq(this.executionDelay);
  634. });
  635. },
  636. callerHasNoExecutionDelay() {
  637. it('has role and no execution delay', async function () {
  638. const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
  639. expect(isMember).to.be.true;
  640. expect(executionDelay).to.be.bignumber.eq('0');
  641. });
  642. },
  643. },
  644. },
  645. requiredRoleIsNotGranted() {
  646. it('has no role and no execution delay', async function () {
  647. const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
  648. expect(isMember).to.be.false;
  649. expect(executionDelay).to.be.bignumber.eq('0');
  650. });
  651. },
  652. },
  653. });
  654. });
  655. describe('#getSchedule', function () {
  656. beforeEach('set role and calldata', async function () {
  657. const method = 'fnRestricted()';
  658. this.caller = user;
  659. this.role = { id: web3.utils.toBN(493590) };
  660. await this.manager.$_setTargetFunctionRole(this.target.address, selector(method), this.role.id);
  661. await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // nonzero execution delay
  662. this.calldata = this.target.contract.methods[method]().encodeABI();
  663. this.scheduleIn = time.duration.days(10); // For testAsSchedulableOperation
  664. });
  665. testAsSchedulableOperation({
  666. scheduled: {
  667. before() {
  668. beforeEach('consume previously set grant delay', async function () {
  669. // Consume previously set delay
  670. await mine();
  671. });
  672. it('returns schedule in the future', async function () {
  673. const schedule = await this.manager.getSchedule(this.operationId);
  674. expect(schedule).to.be.bignumber.equal(this.scheduledAt.add(this.scheduleIn));
  675. expect(schedule).to.be.bignumber.gt(await time.latest());
  676. });
  677. },
  678. after() {
  679. beforeEach('consume previously set grant delay', async function () {
  680. // Consume previously set delay
  681. await mine();
  682. });
  683. it('returns schedule', async function () {
  684. const schedule = await this.manager.getSchedule(this.operationId);
  685. expect(schedule).to.be.bignumber.equal(this.scheduledAt.add(this.scheduleIn));
  686. expect(schedule).to.be.bignumber.eq(await time.latest());
  687. });
  688. },
  689. expired() {
  690. beforeEach('consume previously set grant delay', async function () {
  691. // Consume previously set delay
  692. await mine();
  693. });
  694. it('returns 0', async function () {
  695. expect(await this.manager.getSchedule(this.operationId)).to.be.bignumber.equal('0');
  696. });
  697. },
  698. },
  699. notScheduled() {
  700. it('defaults to 0', async function () {
  701. expect(await this.manager.getSchedule(this.operationId)).to.be.bignumber.equal('0');
  702. });
  703. },
  704. });
  705. });
  706. describe('#getNonce', function () {
  707. describe('when operation is scheduled', function () {
  708. beforeEach('schedule operation', async function () {
  709. const method = 'fnRestricted()';
  710. this.caller = user;
  711. this.role = { id: web3.utils.toBN(4209043) };
  712. await this.manager.$_setTargetFunctionRole(this.target.address, selector(method), this.role.id);
  713. await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // nonzero execution delay
  714. this.calldata = this.target.contract.methods[method]().encodeABI();
  715. this.delay = time.duration.days(10);
  716. const { operationId } = await scheduleOperation(this.manager, {
  717. caller: this.caller,
  718. target: this.target.address,
  719. calldata: this.calldata,
  720. delay: this.delay,
  721. });
  722. this.operationId = operationId;
  723. });
  724. it('returns nonce', async function () {
  725. expect(await this.manager.getNonce(this.operationId)).to.be.bignumber.equal('1');
  726. });
  727. });
  728. describe('when is not scheduled', function () {
  729. it('returns default 0', async function () {
  730. expect(await this.manager.getNonce(web3.utils.keccak256('operation'))).to.be.bignumber.equal('0');
  731. });
  732. });
  733. });
  734. describe('#hashOperation', function () {
  735. it('returns an operationId', async function () {
  736. const calldata = '0x123543';
  737. const address = someAddress;
  738. const args = [user, address, calldata];
  739. expect(await this.manager.hashOperation(...args)).to.be.bignumber.eq(hashOperation(...args));
  740. });
  741. });
  742. });
  743. describe('admin operations', function () {
  744. beforeEach('set required role', function () {
  745. this.role = this.roles.ADMIN;
  746. });
  747. describe('subject to a delay', function () {
  748. describe('#labelRole', function () {
  749. describe('restrictions', function () {
  750. beforeEach('set method and args', function () {
  751. const method = 'labelRole(uint64,string)';
  752. const args = [123443, 'TEST'];
  753. this.calldata = this.manager.contract.methods[method](...args).encodeABI();
  754. });
  755. shouldBehaveLikeDelayedAdminOperation();
  756. });
  757. it('emits an event with the label', async function () {
  758. expectEvent(await this.manager.labelRole(this.roles.SOME.id, 'Some label', { from: admin }), 'RoleLabel', {
  759. roleId: this.roles.SOME.id,
  760. label: 'Some label',
  761. });
  762. });
  763. it('updates label on a second call', async function () {
  764. await this.manager.labelRole(this.roles.SOME.id, 'Some label', { from: admin });
  765. expectEvent(await this.manager.labelRole(this.roles.SOME.id, 'Updated label', { from: admin }), 'RoleLabel', {
  766. roleId: this.roles.SOME.id,
  767. label: 'Updated label',
  768. });
  769. });
  770. it('reverts labeling PUBLIC_ROLE', async function () {
  771. await expectRevertCustomError(
  772. this.manager.labelRole(this.roles.PUBLIC.id, 'Some label', { from: admin }),
  773. 'AccessManagerLockedRole',
  774. [this.roles.PUBLIC.id],
  775. );
  776. });
  777. it('reverts labeling ADMIN_ROLE', async function () {
  778. await expectRevertCustomError(
  779. this.manager.labelRole(this.roles.ADMIN.id, 'Some label', { from: admin }),
  780. 'AccessManagerLockedRole',
  781. [this.roles.ADMIN.id],
  782. );
  783. });
  784. });
  785. describe('#setRoleAdmin', function () {
  786. describe('restrictions', function () {
  787. beforeEach('set method and args', function () {
  788. const method = 'setRoleAdmin(uint64,uint64)';
  789. const args = [93445, 84532];
  790. this.calldata = this.manager.contract.methods[method](...args).encodeABI();
  791. });
  792. shouldBehaveLikeDelayedAdminOperation();
  793. });
  794. it("sets any role's admin if called by an admin", async function () {
  795. expect(await this.manager.getRoleAdmin(this.roles.SOME.id)).to.be.bignumber.equal(this.roles.SOME_ADMIN.id);
  796. const { receipt } = await this.manager.setRoleAdmin(this.roles.SOME.id, this.roles.ADMIN.id, { from: admin });
  797. expectEvent(receipt, 'RoleAdminChanged', { roleId: this.roles.SOME.id, admin: this.roles.ADMIN.id });
  798. expect(await this.manager.getRoleAdmin(this.roles.SOME.id)).to.be.bignumber.equal(this.roles.ADMIN.id);
  799. });
  800. it('reverts setting PUBLIC_ROLE admin', async function () {
  801. await expectRevertCustomError(
  802. this.manager.setRoleAdmin(this.roles.PUBLIC.id, this.roles.ADMIN.id, { from: admin }),
  803. 'AccessManagerLockedRole',
  804. [this.roles.PUBLIC.id],
  805. );
  806. });
  807. it('reverts setting ADMIN_ROLE admin', async function () {
  808. await expectRevertCustomError(
  809. this.manager.setRoleAdmin(this.roles.ADMIN.id, this.roles.ADMIN.id, { from: admin }),
  810. 'AccessManagerLockedRole',
  811. [this.roles.ADMIN.id],
  812. );
  813. });
  814. });
  815. describe('#setRoleGuardian', function () {
  816. describe('restrictions', function () {
  817. beforeEach('set method and args', function () {
  818. const method = 'setRoleGuardian(uint64,uint64)';
  819. const args = [93445, 84532];
  820. this.calldata = this.manager.contract.methods[method](...args).encodeABI();
  821. });
  822. shouldBehaveLikeDelayedAdminOperation();
  823. });
  824. it("sets any role's guardian if called by an admin", async function () {
  825. expect(await this.manager.getRoleGuardian(this.roles.SOME.id)).to.be.bignumber.equal(
  826. this.roles.SOME_GUARDIAN.id,
  827. );
  828. const { receipt } = await this.manager.setRoleGuardian(this.roles.SOME.id, this.roles.ADMIN.id, {
  829. from: admin,
  830. });
  831. expectEvent(receipt, 'RoleGuardianChanged', { roleId: this.roles.SOME.id, guardian: this.roles.ADMIN.id });
  832. expect(await this.manager.getRoleGuardian(this.roles.SOME.id)).to.be.bignumber.equal(this.roles.ADMIN.id);
  833. });
  834. it('reverts setting PUBLIC_ROLE admin', async function () {
  835. await expectRevertCustomError(
  836. this.manager.setRoleGuardian(this.roles.PUBLIC.id, this.roles.ADMIN.id, { from: admin }),
  837. 'AccessManagerLockedRole',
  838. [this.roles.PUBLIC.id],
  839. );
  840. });
  841. it('reverts setting ADMIN_ROLE admin', async function () {
  842. await expectRevertCustomError(
  843. this.manager.setRoleGuardian(this.roles.ADMIN.id, this.roles.ADMIN.id, { from: admin }),
  844. 'AccessManagerLockedRole',
  845. [this.roles.ADMIN.id],
  846. );
  847. });
  848. });
  849. describe('#setGrantDelay', function () {
  850. describe('restrictions', function () {
  851. beforeEach('set method and args', function () {
  852. const method = 'setGrantDelay(uint64,uint32)';
  853. const args = [984910, time.duration.days(2)];
  854. this.calldata = this.manager.contract.methods[method](...args).encodeABI();
  855. });
  856. shouldBehaveLikeDelayedAdminOperation();
  857. });
  858. it('reverts setting grant delay for the PUBLIC_ROLE', async function () {
  859. await expectRevertCustomError(
  860. this.manager.setGrantDelay(this.roles.PUBLIC.id, web3.utils.toBN(69), { from: admin }),
  861. 'AccessManagerLockedRole',
  862. [this.roles.PUBLIC.id],
  863. );
  864. });
  865. describe('when increasing the delay', function () {
  866. const oldDelay = web3.utils.toBN(10);
  867. const newDelay = web3.utils.toBN(100);
  868. beforeEach('sets old delay', async function () {
  869. this.role = this.roles.SOME;
  870. await this.manager.$_setGrantDelay(this.role.id, oldDelay);
  871. await time.increase(MINSETBACK);
  872. expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(oldDelay);
  873. });
  874. it('increases the delay after minsetback', async function () {
  875. const { receipt } = await this.manager.setGrantDelay(this.role.id, newDelay, { from: admin });
  876. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  877. expectEvent(receipt, 'RoleGrantDelayChanged', {
  878. roleId: this.role.id,
  879. delay: newDelay,
  880. since: timestamp.add(MINSETBACK),
  881. });
  882. expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(oldDelay);
  883. await time.increase(MINSETBACK);
  884. expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(newDelay);
  885. });
  886. });
  887. describe('when reducing the delay', function () {
  888. const oldDelay = time.duration.days(10);
  889. beforeEach('sets old delay', async function () {
  890. this.role = this.roles.SOME;
  891. await this.manager.$_setGrantDelay(this.role.id, oldDelay);
  892. await time.increase(MINSETBACK);
  893. expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(oldDelay);
  894. });
  895. describe('when the delay difference is shorter than minimum setback', function () {
  896. const newDelay = oldDelay.subn(1);
  897. it('increases the delay after minsetback', async function () {
  898. const { receipt } = await this.manager.setGrantDelay(this.role.id, newDelay, { from: admin });
  899. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  900. expectEvent(receipt, 'RoleGrantDelayChanged', {
  901. roleId: this.role.id,
  902. delay: newDelay,
  903. since: timestamp.add(MINSETBACK),
  904. });
  905. expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(oldDelay);
  906. await time.increase(MINSETBACK);
  907. expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(newDelay);
  908. });
  909. });
  910. describe('when the delay difference is longer than minimum setback', function () {
  911. const newDelay = web3.utils.toBN(1);
  912. beforeEach('assert delay difference is higher than minsetback', function () {
  913. expect(oldDelay.sub(newDelay)).to.be.bignumber.gt(MINSETBACK);
  914. });
  915. it('increases the delay after delay difference', async function () {
  916. const setback = oldDelay.sub(newDelay);
  917. const { receipt } = await this.manager.setGrantDelay(this.role.id, newDelay, { from: admin });
  918. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  919. expectEvent(receipt, 'RoleGrantDelayChanged', {
  920. roleId: this.role.id,
  921. delay: newDelay,
  922. since: timestamp.add(setback),
  923. });
  924. expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(oldDelay);
  925. await time.increase(setback);
  926. expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(newDelay);
  927. });
  928. });
  929. });
  930. });
  931. describe('#setTargetAdminDelay', function () {
  932. describe('restrictions', function () {
  933. beforeEach('set method and args', function () {
  934. const method = 'setTargetAdminDelay(address,uint32)';
  935. const args = [someAddress, time.duration.days(3)];
  936. this.calldata = this.manager.contract.methods[method](...args).encodeABI();
  937. });
  938. shouldBehaveLikeDelayedAdminOperation();
  939. });
  940. describe('when increasing the delay', function () {
  941. const oldDelay = time.duration.days(10);
  942. const newDelay = time.duration.days(11);
  943. const target = someAddress;
  944. beforeEach('sets old delay', async function () {
  945. await this.manager.$_setTargetAdminDelay(target, oldDelay);
  946. await time.increase(MINSETBACK);
  947. expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(oldDelay);
  948. });
  949. it('increases the delay after minsetback', async function () {
  950. const { receipt } = await this.manager.setTargetAdminDelay(target, newDelay, { from: admin });
  951. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  952. expectEvent(receipt, 'TargetAdminDelayUpdated', {
  953. target,
  954. delay: newDelay,
  955. since: timestamp.add(MINSETBACK),
  956. });
  957. expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(oldDelay);
  958. await time.increase(MINSETBACK);
  959. expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(newDelay);
  960. });
  961. });
  962. describe('when reducing the delay', function () {
  963. const oldDelay = time.duration.days(10);
  964. const target = someAddress;
  965. beforeEach('sets old delay', async function () {
  966. await this.manager.$_setTargetAdminDelay(target, oldDelay);
  967. await time.increase(MINSETBACK);
  968. expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(oldDelay);
  969. });
  970. describe('when the delay difference is shorter than minimum setback', function () {
  971. const newDelay = oldDelay.subn(1);
  972. it('increases the delay after minsetback', async function () {
  973. const { receipt } = await this.manager.setTargetAdminDelay(target, newDelay, { from: admin });
  974. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  975. expectEvent(receipt, 'TargetAdminDelayUpdated', {
  976. target,
  977. delay: newDelay,
  978. since: timestamp.add(MINSETBACK),
  979. });
  980. expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(oldDelay);
  981. await time.increase(MINSETBACK);
  982. expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(newDelay);
  983. });
  984. });
  985. describe('when the delay difference is longer than minimum setback', function () {
  986. const newDelay = web3.utils.toBN(1);
  987. beforeEach('assert delay difference is higher than minsetback', function () {
  988. expect(oldDelay.sub(newDelay)).to.be.bignumber.gt(MINSETBACK);
  989. });
  990. it('increases the delay after delay difference', async function () {
  991. const setback = oldDelay.sub(newDelay);
  992. const { receipt } = await this.manager.setTargetAdminDelay(target, newDelay, { from: admin });
  993. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  994. expectEvent(receipt, 'TargetAdminDelayUpdated', {
  995. target,
  996. delay: newDelay,
  997. since: timestamp.add(setback),
  998. });
  999. expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(oldDelay);
  1000. await time.increase(setback);
  1001. expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(newDelay);
  1002. });
  1003. });
  1004. });
  1005. });
  1006. });
  1007. describe('not subject to a delay', function () {
  1008. describe('#updateAuthority', function () {
  1009. beforeEach('create a target and a new authority', async function () {
  1010. this.newAuthority = await AccessManager.new(admin);
  1011. this.newManagedTarget = await AccessManagedTarget.new(this.manager.address);
  1012. });
  1013. describe('restrictions', function () {
  1014. beforeEach('set method and args', function () {
  1015. const method = 'updateAuthority(address,address)';
  1016. const args = [this.newManagedTarget.address, this.newAuthority.address];
  1017. this.calldata = this.manager.contract.methods[method](...args).encodeABI();
  1018. });
  1019. shouldBehaveLikeNotDelayedAdminOperation();
  1020. });
  1021. it('changes the authority', async function () {
  1022. expect(await this.newManagedTarget.authority()).to.be.equal(this.manager.address);
  1023. const { tx } = await this.manager.updateAuthority(this.newManagedTarget.address, this.newAuthority.address, {
  1024. from: admin,
  1025. });
  1026. // Managed contract is responsible of notifying the change through an event
  1027. await expectEvent.inTransaction(tx, this.newManagedTarget, 'AuthorityUpdated', {
  1028. authority: this.newAuthority.address,
  1029. });
  1030. expect(await this.newManagedTarget.authority()).to.be.equal(this.newAuthority.address);
  1031. });
  1032. });
  1033. describe('#setTargetClosed', function () {
  1034. describe('restrictions', function () {
  1035. beforeEach('set method and args', function () {
  1036. const method = 'setTargetClosed(address,bool)';
  1037. const args = [someAddress, true];
  1038. this.calldata = this.manager.contract.methods[method](...args).encodeABI();
  1039. });
  1040. shouldBehaveLikeNotDelayedAdminOperation();
  1041. });
  1042. it('closes and opens a target', async function () {
  1043. const close = await this.manager.setTargetClosed(this.target.address, true, { from: admin });
  1044. expectEvent(close.receipt, 'TargetClosed', { target: this.target.address, closed: true });
  1045. expect(await this.manager.isTargetClosed(this.target.address)).to.be.equal(true);
  1046. const open = await this.manager.setTargetClosed(this.target.address, false, { from: admin });
  1047. expectEvent(open.receipt, 'TargetClosed', { target: this.target.address, closed: false });
  1048. expect(await this.manager.isTargetClosed(this.target.address)).to.be.equal(false);
  1049. });
  1050. it('reverts if closing the manager', async function () {
  1051. await expectRevertCustomError(
  1052. this.manager.setTargetClosed(this.manager.address, true, { from: admin }),
  1053. 'AccessManagerLockedAccount',
  1054. [this.manager.address],
  1055. );
  1056. });
  1057. });
  1058. describe('#setTargetFunctionRole', function () {
  1059. describe('restrictions', function () {
  1060. beforeEach('set method and args', function () {
  1061. const method = 'setTargetFunctionRole(address,bytes4[],uint64)';
  1062. const args = [someAddress, ['0x12345678'], 443342];
  1063. this.calldata = this.manager.contract.methods[method](...args).encodeABI();
  1064. });
  1065. shouldBehaveLikeNotDelayedAdminOperation();
  1066. });
  1067. const sigs = ['someFunction()', 'someOtherFunction(uint256)', 'oneMoreFunction(address,uint8)'].map(selector);
  1068. it('sets function roles', async function () {
  1069. for (const sig of sigs) {
  1070. expect(await this.manager.getTargetFunctionRole(this.target.address, sig)).to.be.bignumber.equal(
  1071. this.roles.ADMIN.id,
  1072. );
  1073. }
  1074. const { receipt: receipt1 } = await this.manager.setTargetFunctionRole(
  1075. this.target.address,
  1076. sigs,
  1077. this.roles.SOME.id,
  1078. {
  1079. from: admin,
  1080. },
  1081. );
  1082. for (const sig of sigs) {
  1083. expectEvent(receipt1, 'TargetFunctionRoleUpdated', {
  1084. target: this.target.address,
  1085. selector: sig,
  1086. roleId: this.roles.SOME.id,
  1087. });
  1088. expect(await this.manager.getTargetFunctionRole(this.target.address, sig)).to.be.bignumber.equal(
  1089. this.roles.SOME.id,
  1090. );
  1091. }
  1092. const { receipt: receipt2 } = await this.manager.setTargetFunctionRole(
  1093. this.target.address,
  1094. [sigs[1]],
  1095. this.roles.SOME_ADMIN.id,
  1096. {
  1097. from: admin,
  1098. },
  1099. );
  1100. expectEvent(receipt2, 'TargetFunctionRoleUpdated', {
  1101. target: this.target.address,
  1102. selector: sigs[1],
  1103. roleId: this.roles.SOME_ADMIN.id,
  1104. });
  1105. for (const sig of sigs) {
  1106. expect(await this.manager.getTargetFunctionRole(this.target.address, sig)).to.be.bignumber.equal(
  1107. sig == sigs[1] ? this.roles.SOME_ADMIN.id : this.roles.SOME.id,
  1108. );
  1109. }
  1110. });
  1111. });
  1112. describe('role admin operations', function () {
  1113. const ANOTHER_ADMIN = web3.utils.toBN(0xdeadc0de1);
  1114. const ANOTHER_ROLE = web3.utils.toBN(0xdeadc0de2);
  1115. beforeEach('set required role', async function () {
  1116. // Make admin a member of ANOTHER_ADMIN
  1117. await this.manager.$_grantRole(ANOTHER_ADMIN, admin, 0, 0);
  1118. await this.manager.$_setRoleAdmin(ANOTHER_ROLE, ANOTHER_ADMIN);
  1119. this.role = { id: ANOTHER_ADMIN };
  1120. this.user = user;
  1121. await this.manager.$_grantRole(this.role.id, this.user, 0, 0);
  1122. });
  1123. describe('#grantRole', function () {
  1124. describe('restrictions', function () {
  1125. beforeEach('set method and args', function () {
  1126. const method = 'grantRole(uint64,address,uint32)';
  1127. const args = [ANOTHER_ROLE, someAddress, 0];
  1128. this.calldata = this.manager.contract.methods[method](...args).encodeABI();
  1129. });
  1130. shouldBehaveLikeRoleAdminOperation(ANOTHER_ADMIN);
  1131. });
  1132. it('reverts when granting PUBLIC_ROLE', async function () {
  1133. await expectRevertCustomError(
  1134. this.manager.grantRole(this.roles.PUBLIC.id, user, 0, {
  1135. from: admin,
  1136. }),
  1137. 'AccessManagerLockedRole',
  1138. [this.roles.PUBLIC.id],
  1139. );
  1140. });
  1141. describe('when the user is not a role member', function () {
  1142. describe('with grant delay', function () {
  1143. beforeEach('set grant delay and grant role', async function () {
  1144. // Delay granting
  1145. this.grantDelay = time.duration.weeks(2);
  1146. await this.manager.$_setGrantDelay(ANOTHER_ROLE, this.grantDelay);
  1147. await time.increase(MINSETBACK);
  1148. // Grant role
  1149. this.executionDelay = time.duration.days(3);
  1150. expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
  1151. false,
  1152. '0',
  1153. ]);
  1154. const { receipt } = await this.manager.grantRole(ANOTHER_ROLE, this.user, this.executionDelay, {
  1155. from: admin,
  1156. });
  1157. this.receipt = receipt;
  1158. this.delay = this.grantDelay; // For testAsDelay
  1159. });
  1160. testAsDelay('grant', {
  1161. before() {
  1162. beforeEach('consume previously set grant delay', async function () {
  1163. // Consume previously set delay
  1164. await mine();
  1165. });
  1166. it('does not grant role to the user yet', async function () {
  1167. const timestamp = await clockFromReceipt.timestamp(this.receipt).then(web3.utils.toBN);
  1168. expectEvent(this.receipt, 'RoleGranted', {
  1169. roleId: ANOTHER_ROLE,
  1170. account: this.user,
  1171. since: timestamp.add(this.grantDelay),
  1172. delay: this.executionDelay,
  1173. newMember: true,
  1174. });
  1175. // Access is correctly stored
  1176. const access = await this.manager.getAccess(ANOTHER_ROLE, user);
  1177. expect(access[0]).to.be.bignumber.equal(timestamp.add(this.grantDelay)); // inEffectSince
  1178. expect(access[1]).to.be.bignumber.equal(this.executionDelay); // currentDelay
  1179. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  1180. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  1181. // Not in effect yet
  1182. const currentTimestamp = await time.latest();
  1183. expect(currentTimestamp).to.be.a.bignumber.lt(access[0]);
  1184. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1185. false,
  1186. this.executionDelay.toString(),
  1187. ]);
  1188. });
  1189. },
  1190. after() {
  1191. beforeEach('consume previously set grant delay', async function () {
  1192. // Consume previously set delay
  1193. await mine();
  1194. });
  1195. it('grants role to the user', async function () {
  1196. const timestamp = await clockFromReceipt.timestamp(this.receipt).then(web3.utils.toBN);
  1197. expectEvent(this.receipt, 'RoleGranted', {
  1198. roleId: ANOTHER_ROLE,
  1199. account: this.user,
  1200. since: timestamp.add(this.grantDelay),
  1201. delay: this.executionDelay,
  1202. newMember: true,
  1203. });
  1204. // Access is correctly stored
  1205. const access = await this.manager.getAccess(ANOTHER_ROLE, user);
  1206. expect(access[0]).to.be.bignumber.equal(timestamp.add(this.grantDelay)); // inEffectSince
  1207. expect(access[1]).to.be.bignumber.equal(this.executionDelay); // currentDelay
  1208. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  1209. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  1210. // Already in effect
  1211. const currentTimestamp = await time.latest();
  1212. expect(currentTimestamp).to.be.a.bignumber.equal(access[0]);
  1213. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1214. true,
  1215. this.executionDelay.toString(),
  1216. ]);
  1217. });
  1218. },
  1219. });
  1220. });
  1221. describe('without grant delay', function () {
  1222. beforeEach('set granting delay', async function () {
  1223. // Delay granting
  1224. this.grantDelay = 0;
  1225. await this.manager.$_setGrantDelay(ANOTHER_ROLE, this.grantDelay);
  1226. await time.increase(MINSETBACK);
  1227. });
  1228. it('immediately grants the role to the user', async function () {
  1229. this.executionDelay = time.duration.days(6);
  1230. expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
  1231. false,
  1232. '0',
  1233. ]);
  1234. const { receipt } = await this.manager.grantRole(ANOTHER_ROLE, this.user, this.executionDelay, {
  1235. from: admin,
  1236. });
  1237. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  1238. expectEvent(receipt, 'RoleGranted', {
  1239. roleId: ANOTHER_ROLE,
  1240. account: this.user,
  1241. since: timestamp,
  1242. delay: this.executionDelay,
  1243. newMember: true,
  1244. });
  1245. // Access is correctly stored
  1246. const access = await this.manager.getAccess(ANOTHER_ROLE, user);
  1247. expect(access[0]).to.be.bignumber.equal(timestamp); // inEffectSince
  1248. expect(access[1]).to.be.bignumber.equal(this.executionDelay); // currentDelay
  1249. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  1250. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  1251. // Already in effect
  1252. const currentTimestamp = await time.latest();
  1253. expect(currentTimestamp).to.be.a.bignumber.equal(access[0]);
  1254. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1255. true,
  1256. this.executionDelay.toString(),
  1257. ]);
  1258. });
  1259. });
  1260. });
  1261. describe('when the user is already a role member', function () {
  1262. beforeEach('make user role member', async function () {
  1263. this.previousExecutionDelay = time.duration.days(6);
  1264. await this.manager.$_grantRole(ANOTHER_ROLE, this.user, 0, this.previousExecutionDelay);
  1265. this.oldAccess = await this.manager.getAccess(ANOTHER_ROLE, user);
  1266. });
  1267. describe('with grant delay', function () {
  1268. beforeEach('set granting delay', async function () {
  1269. // Delay granting
  1270. const grantDelay = time.duration.weeks(2);
  1271. await this.manager.$_setGrantDelay(ANOTHER_ROLE, grantDelay);
  1272. await time.increase(MINSETBACK);
  1273. });
  1274. describe('when increasing the execution delay', function () {
  1275. beforeEach('set increased new execution delay', async function () {
  1276. expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
  1277. true,
  1278. this.previousExecutionDelay.toString(),
  1279. ]);
  1280. this.newExecutionDelay = this.previousExecutionDelay.add(time.duration.days(4));
  1281. });
  1282. it('emits event and immediately changes the execution delay', async function () {
  1283. expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
  1284. true,
  1285. this.previousExecutionDelay.toString(),
  1286. ]);
  1287. const { receipt } = await this.manager.grantRole(ANOTHER_ROLE, this.user, this.newExecutionDelay, {
  1288. from: admin,
  1289. });
  1290. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  1291. expectEvent(receipt, 'RoleGranted', {
  1292. roleId: ANOTHER_ROLE,
  1293. account: this.user,
  1294. since: timestamp,
  1295. delay: this.newExecutionDelay,
  1296. newMember: false,
  1297. });
  1298. // Access is correctly stored
  1299. const access = await this.manager.getAccess(ANOTHER_ROLE, user);
  1300. expect(access[0]).to.be.bignumber.equal(this.oldAccess[0]); // inEffectSince
  1301. expect(access[1]).to.be.bignumber.equal(this.newExecutionDelay); // currentDelay
  1302. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  1303. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  1304. // Already in effect
  1305. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1306. true,
  1307. this.newExecutionDelay.toString(),
  1308. ]);
  1309. });
  1310. });
  1311. describe('when decreasing the execution delay', function () {
  1312. beforeEach('decrease execution delay', async function () {
  1313. expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
  1314. true,
  1315. this.previousExecutionDelay.toString(),
  1316. ]);
  1317. this.newExecutionDelay = this.previousExecutionDelay.sub(time.duration.days(4));
  1318. const { receipt } = await this.manager.grantRole(ANOTHER_ROLE, this.user, this.newExecutionDelay, {
  1319. from: admin,
  1320. });
  1321. this.grantTimestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  1322. this.receipt = receipt;
  1323. this.delay = this.previousExecutionDelay.sub(this.newExecutionDelay); // For testAsDelay
  1324. });
  1325. it('emits event', function () {
  1326. expectEvent(this.receipt, 'RoleGranted', {
  1327. roleId: ANOTHER_ROLE,
  1328. account: this.user,
  1329. since: this.grantTimestamp.add(this.delay),
  1330. delay: this.newExecutionDelay,
  1331. newMember: false,
  1332. });
  1333. });
  1334. testAsDelay('execution delay effect', {
  1335. before() {
  1336. beforeEach('consume effect delay', async function () {
  1337. // Consume previously set delay
  1338. await mine();
  1339. });
  1340. it('does not change the execution delay yet', async function () {
  1341. // Access is correctly stored
  1342. const access = await this.manager.getAccess(ANOTHER_ROLE, user);
  1343. expect(access[0]).to.be.bignumber.equal(this.oldAccess[0]); // inEffectSince
  1344. expect(access[1]).to.be.bignumber.equal(this.previousExecutionDelay); // currentDelay
  1345. expect(access[2]).to.be.bignumber.equal(this.newExecutionDelay); // pendingDelay
  1346. expect(access[3]).to.be.bignumber.equal(this.grantTimestamp.add(this.delay)); // pendingDelayEffect
  1347. // Not in effect yet
  1348. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1349. true,
  1350. this.previousExecutionDelay.toString(),
  1351. ]);
  1352. });
  1353. },
  1354. after() {
  1355. beforeEach('consume effect delay', async function () {
  1356. // Consume previously set delay
  1357. await mine();
  1358. });
  1359. it('changes the execution delay', async function () {
  1360. // Access is correctly stored
  1361. const access = await this.manager.getAccess(ANOTHER_ROLE, user);
  1362. expect(access[0]).to.be.bignumber.equal(this.oldAccess[0]); // inEffectSince
  1363. expect(access[1]).to.be.bignumber.equal(this.newExecutionDelay); // currentDelay
  1364. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  1365. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  1366. // Already in effect
  1367. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1368. true,
  1369. this.newExecutionDelay.toString(),
  1370. ]);
  1371. });
  1372. },
  1373. });
  1374. });
  1375. });
  1376. describe('without grant delay', function () {
  1377. beforeEach('set granting delay', async function () {
  1378. // Delay granting
  1379. const grantDelay = 0;
  1380. await this.manager.$_setGrantDelay(ANOTHER_ROLE, grantDelay);
  1381. await time.increase(MINSETBACK);
  1382. });
  1383. describe('when increasing the execution delay', function () {
  1384. beforeEach('set increased new execution delay', async function () {
  1385. expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
  1386. true,
  1387. this.previousExecutionDelay.toString(),
  1388. ]);
  1389. this.newExecutionDelay = this.previousExecutionDelay.add(time.duration.days(4));
  1390. });
  1391. it('emits event and immediately changes the execution delay', async function () {
  1392. expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
  1393. true,
  1394. this.previousExecutionDelay.toString(),
  1395. ]);
  1396. const { receipt } = await this.manager.grantRole(ANOTHER_ROLE, this.user, this.newExecutionDelay, {
  1397. from: admin,
  1398. });
  1399. const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  1400. expectEvent(receipt, 'RoleGranted', {
  1401. roleId: ANOTHER_ROLE,
  1402. account: this.user,
  1403. since: timestamp,
  1404. delay: this.newExecutionDelay,
  1405. newMember: false,
  1406. });
  1407. // Access is correctly stored
  1408. const access = await this.manager.getAccess(ANOTHER_ROLE, user);
  1409. expect(access[0]).to.be.bignumber.equal(this.oldAccess[0]); // inEffectSince
  1410. expect(access[1]).to.be.bignumber.equal(this.newExecutionDelay); // currentDelay
  1411. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  1412. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  1413. // Already in effect
  1414. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1415. true,
  1416. this.newExecutionDelay.toString(),
  1417. ]);
  1418. });
  1419. });
  1420. describe('when decreasing the execution delay', function () {
  1421. beforeEach('decrease execution delay', async function () {
  1422. expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
  1423. true,
  1424. this.previousExecutionDelay.toString(),
  1425. ]);
  1426. this.newExecutionDelay = this.previousExecutionDelay.sub(time.duration.days(4));
  1427. const { receipt } = await this.manager.grantRole(ANOTHER_ROLE, this.user, this.newExecutionDelay, {
  1428. from: admin,
  1429. });
  1430. this.grantTimestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
  1431. this.receipt = receipt;
  1432. this.delay = this.previousExecutionDelay.sub(this.newExecutionDelay); // For testAsDelay
  1433. });
  1434. it('emits event', function () {
  1435. expectEvent(this.receipt, 'RoleGranted', {
  1436. roleId: ANOTHER_ROLE,
  1437. account: this.user,
  1438. since: this.grantTimestamp.add(this.delay),
  1439. delay: this.newExecutionDelay,
  1440. newMember: false,
  1441. });
  1442. });
  1443. testAsDelay('execution delay effect', {
  1444. before() {
  1445. beforeEach('consume effect delay', async function () {
  1446. // Consume previously set delay
  1447. await mine();
  1448. });
  1449. it('does not change the execution delay yet', async function () {
  1450. // Access is correctly stored
  1451. const access = await this.manager.getAccess(ANOTHER_ROLE, user);
  1452. expect(access[0]).to.be.bignumber.equal(this.oldAccess[0]); // inEffectSince
  1453. expect(access[1]).to.be.bignumber.equal(this.previousExecutionDelay); // currentDelay
  1454. expect(access[2]).to.be.bignumber.equal(this.newExecutionDelay); // pendingDelay
  1455. expect(access[3]).to.be.bignumber.equal(this.grantTimestamp.add(this.delay)); // pendingDelayEffect
  1456. // Not in effect yet
  1457. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1458. true,
  1459. this.previousExecutionDelay.toString(),
  1460. ]);
  1461. });
  1462. },
  1463. after() {
  1464. beforeEach('consume effect delay', async function () {
  1465. // Consume previously set delay
  1466. await mine();
  1467. });
  1468. it('changes the execution delay', async function () {
  1469. // Access is correctly stored
  1470. const access = await this.manager.getAccess(ANOTHER_ROLE, user);
  1471. expect(access[0]).to.be.bignumber.equal(this.oldAccess[0]); // inEffectSince
  1472. expect(access[1]).to.be.bignumber.equal(this.newExecutionDelay); // currentDelay
  1473. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  1474. expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
  1475. // Already in effect
  1476. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1477. true,
  1478. this.newExecutionDelay.toString(),
  1479. ]);
  1480. });
  1481. },
  1482. });
  1483. });
  1484. });
  1485. });
  1486. });
  1487. describe('#revokeRole', function () {
  1488. describe('restrictions', function () {
  1489. beforeEach('set method and args', async function () {
  1490. const method = 'revokeRole(uint64,address)';
  1491. const args = [ANOTHER_ROLE, someAddress];
  1492. this.calldata = this.manager.contract.methods[method](...args).encodeABI();
  1493. // Need to be set before revoking
  1494. await this.manager.$_grantRole(...args, 0, 0);
  1495. });
  1496. shouldBehaveLikeRoleAdminOperation(ANOTHER_ADMIN);
  1497. });
  1498. describe('when role has been granted', function () {
  1499. beforeEach('grant role with grant delay', async function () {
  1500. this.grantDelay = time.duration.weeks(1);
  1501. await this.manager.$_grantRole(ANOTHER_ROLE, user, this.grantDelay, 0);
  1502. this.delay = this.grantDelay; // For testAsDelay
  1503. });
  1504. testAsDelay('grant', {
  1505. before() {
  1506. beforeEach('consume previously set grant delay', async function () {
  1507. // Consume previously set delay
  1508. await mine();
  1509. });
  1510. it('revokes a granted role that will take effect in the future', async function () {
  1511. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1512. false,
  1513. '0',
  1514. ]);
  1515. const { receipt } = await this.manager.revokeRole(ANOTHER_ROLE, user, { from: admin });
  1516. expectEvent(receipt, 'RoleRevoked', { roleId: ANOTHER_ROLE, account: user });
  1517. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1518. false,
  1519. '0',
  1520. ]);
  1521. const access = await this.manager.getAccess(ANOTHER_ROLE, user);
  1522. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  1523. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  1524. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  1525. expect(access[3]).to.be.bignumber.equal('0'); // effect
  1526. });
  1527. },
  1528. after() {
  1529. beforeEach('consume previously set grant delay', async function () {
  1530. // Consume previously set delay
  1531. await mine();
  1532. });
  1533. it('revokes a granted role that already took effect', async function () {
  1534. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1535. true,
  1536. '0',
  1537. ]);
  1538. const { receipt } = await this.manager.revokeRole(ANOTHER_ROLE, user, { from: admin });
  1539. expectEvent(receipt, 'RoleRevoked', { roleId: ANOTHER_ROLE, account: user });
  1540. expect(await this.manager.hasRole(ANOTHER_ROLE, user).then(formatAccess)).to.be.deep.equal([
  1541. false,
  1542. '0',
  1543. ]);
  1544. const access = await this.manager.getAccess(ANOTHER_ROLE, user);
  1545. expect(access[0]).to.be.bignumber.equal('0'); // inRoleSince
  1546. expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
  1547. expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
  1548. expect(access[3]).to.be.bignumber.equal('0'); // effect
  1549. });
  1550. },
  1551. });
  1552. });
  1553. describe('when role has not been granted', function () {
  1554. it('has no effect', async function () {
  1555. expect(await this.manager.hasRole(this.roles.SOME.id, user).then(formatAccess)).to.be.deep.equal([
  1556. false,
  1557. '0',
  1558. ]);
  1559. const { receipt } = await this.manager.revokeRole(this.roles.SOME.id, user, { from: manager });
  1560. expectEvent.notEmitted(receipt, 'RoleRevoked', { roleId: ANOTHER_ROLE, account: user });
  1561. expect(await this.manager.hasRole(this.roles.SOME.id, user).then(formatAccess)).to.be.deep.equal([
  1562. false,
  1563. '0',
  1564. ]);
  1565. });
  1566. });
  1567. it('reverts revoking PUBLIC_ROLE', async function () {
  1568. await expectRevertCustomError(
  1569. this.manager.revokeRole(this.roles.PUBLIC.id, user, { from: admin }),
  1570. 'AccessManagerLockedRole',
  1571. [this.roles.PUBLIC.id],
  1572. );
  1573. });
  1574. });
  1575. });
  1576. describe('self role operations', function () {
  1577. describe('#renounceRole', function () {
  1578. beforeEach('grant role', async function () {
  1579. this.role = { id: web3.utils.toBN(783164) };
  1580. this.caller = user;
  1581. await this.manager.$_grantRole(this.role.id, this.caller, 0, 0);
  1582. });
  1583. it('renounces a role', async function () {
  1584. expect(await this.manager.hasRole(this.role.id, this.caller).then(formatAccess)).to.be.deep.equal([
  1585. true,
  1586. '0',
  1587. ]);
  1588. const { receipt } = await this.manager.renounceRole(this.role.id, this.caller, {
  1589. from: this.caller,
  1590. });
  1591. expectEvent(receipt, 'RoleRevoked', {
  1592. roleId: this.role.id,
  1593. account: this.caller,
  1594. });
  1595. expect(await this.manager.hasRole(this.role.id, this.caller).then(formatAccess)).to.be.deep.equal([
  1596. false,
  1597. '0',
  1598. ]);
  1599. });
  1600. it('reverts if renouncing the PUBLIC_ROLE', async function () {
  1601. await expectRevertCustomError(
  1602. this.manager.renounceRole(this.roles.PUBLIC.id, this.caller, {
  1603. from: this.caller,
  1604. }),
  1605. 'AccessManagerLockedRole',
  1606. [this.roles.PUBLIC.id],
  1607. );
  1608. });
  1609. it('reverts if renouncing with bad caller confirmation', async function () {
  1610. await expectRevertCustomError(
  1611. this.manager.renounceRole(this.role.id, someAddress, {
  1612. from: this.caller,
  1613. }),
  1614. 'AccessManagerBadConfirmation',
  1615. [],
  1616. );
  1617. });
  1618. });
  1619. });
  1620. });
  1621. });
  1622. describe('access managed target operations', function () {
  1623. describe('when calling a restricted target function', function () {
  1624. const method = 'fnRestricted()';
  1625. beforeEach('set required role', function () {
  1626. this.role = { id: web3.utils.toBN(3597243) };
  1627. this.manager.$_setTargetFunctionRole(this.target.address, selector(method), this.role.id);
  1628. });
  1629. describe('restrictions', function () {
  1630. beforeEach('set method and args', function () {
  1631. this.calldata = this.target.contract.methods[method]().encodeABI();
  1632. this.caller = user;
  1633. });
  1634. shouldBehaveLikeAManagedRestrictedOperation();
  1635. });
  1636. it('succeeds called by a role member', async function () {
  1637. await this.manager.$_grantRole(this.role.id, user, 0, 0);
  1638. const { receipt } = await this.target.methods[method]({
  1639. data: this.calldata,
  1640. from: user,
  1641. });
  1642. expectEvent(receipt, 'CalledRestricted', {
  1643. caller: user,
  1644. });
  1645. });
  1646. });
  1647. describe('when calling a non-restricted target function', function () {
  1648. const method = 'fnUnrestricted()';
  1649. beforeEach('set required role', async function () {
  1650. this.role = { id: web3.utils.toBN(879435) };
  1651. await this.manager.$_setTargetFunctionRole(this.target.address, selector(method), this.role.id);
  1652. });
  1653. it('succeeds called by anyone', async function () {
  1654. const { receipt } = await this.target.methods[method]({
  1655. data: this.calldata,
  1656. from: user,
  1657. });
  1658. expectEvent(receipt, 'CalledUnrestricted', {
  1659. caller: user,
  1660. });
  1661. });
  1662. });
  1663. });
  1664. describe('#schedule', function () {
  1665. const method = 'fnRestricted()';
  1666. beforeEach('set target function role', async function () {
  1667. this.role = { id: web3.utils.toBN(498305) };
  1668. this.caller = user;
  1669. await this.manager.$_setTargetFunctionRole(this.target.address, selector(method), this.role.id);
  1670. await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // nonzero execution delay
  1671. this.calldata = this.target.contract.methods[method]().encodeABI();
  1672. this.delay = time.duration.weeks(2);
  1673. });
  1674. describe('restrictions', function () {
  1675. testAsCanCall({
  1676. closed() {
  1677. it('reverts as AccessManagerUnauthorizedCall', async function () {
  1678. await expectRevertCustomError(
  1679. scheduleOperation(this.manager, {
  1680. caller: this.caller,
  1681. target: this.target.address,
  1682. calldata: this.calldata,
  1683. delay: this.delay,
  1684. }),
  1685. 'AccessManagerUnauthorizedCall',
  1686. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  1687. );
  1688. });
  1689. },
  1690. open: {
  1691. callerIsTheManager: {
  1692. executing() {
  1693. it.skip('is not reachable because schedule is not restrictable');
  1694. },
  1695. notExecuting() {
  1696. it('reverts as AccessManagerUnauthorizedCall', async function () {
  1697. await expectRevertCustomError(
  1698. scheduleOperation(this.manager, {
  1699. caller: this.caller,
  1700. target: this.target.address,
  1701. calldata: this.calldata,
  1702. delay: this.delay,
  1703. }),
  1704. 'AccessManagerUnauthorizedCall',
  1705. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  1706. );
  1707. });
  1708. },
  1709. },
  1710. callerIsNotTheManager: {
  1711. publicRoleIsRequired() {
  1712. it('reverts as AccessManagerUnauthorizedCall', async function () {
  1713. // scheduleOperation is not used here because it alters the next block timestamp
  1714. await expectRevertCustomError(
  1715. this.manager.schedule(this.target.address, this.calldata, MAX_UINT48, {
  1716. from: this.caller,
  1717. }),
  1718. 'AccessManagerUnauthorizedCall',
  1719. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  1720. );
  1721. });
  1722. },
  1723. specificRoleIsRequired: {
  1724. requiredRoleIsGranted: {
  1725. roleGrantingIsDelayed: {
  1726. callerHasAnExecutionDelay: {
  1727. beforeGrantDelay() {
  1728. it('reverts as AccessManagerUnauthorizedCall', async function () {
  1729. // scheduleOperation is not used here because it alters the next block timestamp
  1730. await expectRevertCustomError(
  1731. this.manager.schedule(this.target.address, this.calldata, MAX_UINT48, {
  1732. from: this.caller,
  1733. }),
  1734. 'AccessManagerUnauthorizedCall',
  1735. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  1736. );
  1737. });
  1738. },
  1739. afterGrantDelay() {
  1740. it('succeeds', async function () {
  1741. // scheduleOperation is not used here because it alters the next block timestamp
  1742. await this.manager.schedule(this.target.address, this.calldata, MAX_UINT48, {
  1743. from: this.caller,
  1744. });
  1745. });
  1746. },
  1747. },
  1748. callerHasNoExecutionDelay: {
  1749. beforeGrantDelay() {
  1750. it('reverts as AccessManagerUnauthorizedCall', async function () {
  1751. // scheduleOperation is not used here because it alters the next block timestamp
  1752. await expectRevertCustomError(
  1753. this.manager.schedule(this.target.address, this.calldata, MAX_UINT48, {
  1754. from: this.caller,
  1755. }),
  1756. 'AccessManagerUnauthorizedCall',
  1757. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  1758. );
  1759. });
  1760. },
  1761. afterGrantDelay() {
  1762. it('reverts as AccessManagerUnauthorizedCall', async function () {
  1763. // scheduleOperation is not used here because it alters the next block timestamp
  1764. await expectRevertCustomError(
  1765. this.manager.schedule(this.target.address, this.calldata, MAX_UINT48, {
  1766. from: this.caller,
  1767. }),
  1768. 'AccessManagerUnauthorizedCall',
  1769. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  1770. );
  1771. });
  1772. },
  1773. },
  1774. },
  1775. roleGrantingIsNotDelayed: {
  1776. callerHasAnExecutionDelay() {
  1777. it('succeeds', async function () {
  1778. await scheduleOperation(this.manager, {
  1779. caller: this.caller,
  1780. target: this.target.address,
  1781. calldata: this.calldata,
  1782. delay: this.delay,
  1783. });
  1784. });
  1785. },
  1786. callerHasNoExecutionDelay() {
  1787. it('reverts as AccessManagerUnauthorizedCall', async function () {
  1788. // scheduleOperation is not used here because it alters the next block timestamp
  1789. await expectRevertCustomError(
  1790. this.manager.schedule(this.target.address, this.calldata, MAX_UINT48, {
  1791. from: this.caller,
  1792. }),
  1793. 'AccessManagerUnauthorizedCall',
  1794. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  1795. );
  1796. });
  1797. },
  1798. },
  1799. },
  1800. requiredRoleIsNotGranted() {
  1801. it('reverts as AccessManagerUnauthorizedCall', async function () {
  1802. await expectRevertCustomError(
  1803. scheduleOperation(this.manager, {
  1804. caller: this.caller,
  1805. target: this.target.address,
  1806. calldata: this.calldata,
  1807. delay: this.delay,
  1808. }),
  1809. 'AccessManagerUnauthorizedCall',
  1810. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  1811. );
  1812. });
  1813. },
  1814. },
  1815. },
  1816. },
  1817. });
  1818. });
  1819. it('schedules an operation at the specified execution date if it is larger than caller execution delay', async function () {
  1820. const { operationId, scheduledAt, receipt } = await scheduleOperation(this.manager, {
  1821. caller: this.caller,
  1822. target: this.target.address,
  1823. calldata: this.calldata,
  1824. delay: this.delay,
  1825. });
  1826. expect(await this.manager.getSchedule(operationId)).to.be.bignumber.equal(scheduledAt.add(this.delay));
  1827. expectEvent(receipt, 'OperationScheduled', {
  1828. operationId,
  1829. nonce: '1',
  1830. schedule: scheduledAt.add(this.delay),
  1831. target: this.target.address,
  1832. data: this.calldata,
  1833. });
  1834. });
  1835. it('schedules an operation at the minimum execution date if no specified execution date (when == 0)', async function () {
  1836. const executionDelay = await time.duration.hours(72);
  1837. await this.manager.$_grantRole(this.role.id, this.caller, 0, executionDelay);
  1838. const timestamp = await time.latest();
  1839. const scheduledAt = timestamp.addn(1);
  1840. await setNextBlockTimestamp(scheduledAt);
  1841. const { receipt } = await this.manager.schedule(this.target.address, this.calldata, 0, {
  1842. from: this.caller,
  1843. });
  1844. const operationId = await this.manager.hashOperation(this.caller, this.target.address, this.calldata);
  1845. expect(await this.manager.getSchedule(operationId)).to.be.bignumber.equal(scheduledAt.add(executionDelay));
  1846. expectEvent(receipt, 'OperationScheduled', {
  1847. operationId,
  1848. nonce: '1',
  1849. schedule: scheduledAt.add(executionDelay),
  1850. target: this.target.address,
  1851. data: this.calldata,
  1852. });
  1853. });
  1854. it('increases the nonce of an operation scheduled more than once', async function () {
  1855. // Setup and check initial nonce
  1856. const expectedOperationId = hashOperation(this.caller, this.target.address, this.calldata);
  1857. expect(await this.manager.getNonce(expectedOperationId)).to.be.bignumber.eq('0');
  1858. // Schedule
  1859. const op1 = await scheduleOperation(this.manager, {
  1860. caller: this.caller,
  1861. target: this.target.address,
  1862. calldata: this.calldata,
  1863. delay: this.delay,
  1864. });
  1865. expectEvent(op1.receipt, 'OperationScheduled', {
  1866. operationId: op1.operationId,
  1867. nonce: '1',
  1868. schedule: op1.scheduledAt.add(this.delay),
  1869. target: this.target.address,
  1870. data: this.calldata,
  1871. });
  1872. expect(expectedOperationId).to.eq(op1.operationId);
  1873. // Consume
  1874. await time.increase(this.delay);
  1875. await this.manager.$_consumeScheduledOp(expectedOperationId);
  1876. // Check nonce
  1877. expect(await this.manager.getNonce(expectedOperationId)).to.be.bignumber.eq('1');
  1878. // Schedule again
  1879. const op2 = await scheduleOperation(this.manager, {
  1880. caller: this.caller,
  1881. target: this.target.address,
  1882. calldata: this.calldata,
  1883. delay: this.delay,
  1884. });
  1885. expectEvent(op2.receipt, 'OperationScheduled', {
  1886. operationId: op2.operationId,
  1887. nonce: '2',
  1888. schedule: op2.scheduledAt.add(this.delay),
  1889. target: this.target.address,
  1890. data: this.calldata,
  1891. });
  1892. expect(expectedOperationId).to.eq(op2.operationId);
  1893. // Check final nonce
  1894. expect(await this.manager.getNonce(expectedOperationId)).to.be.bignumber.eq('2');
  1895. });
  1896. it('reverts if the specified execution date is before the current timestamp + caller execution delay', async function () {
  1897. const executionDelay = time.duration.weeks(1).add(this.delay);
  1898. await this.manager.$_grantRole(this.role.id, this.caller, 0, executionDelay);
  1899. await expectRevertCustomError(
  1900. scheduleOperation(this.manager, {
  1901. caller: this.caller,
  1902. target: this.target.address,
  1903. calldata: this.calldata,
  1904. delay: this.delay,
  1905. }),
  1906. 'AccessManagerUnauthorizedCall',
  1907. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  1908. );
  1909. });
  1910. it('reverts if an operation is already schedule', async function () {
  1911. const { operationId } = await scheduleOperation(this.manager, {
  1912. caller: this.caller,
  1913. target: this.target.address,
  1914. calldata: this.calldata,
  1915. delay: this.delay,
  1916. });
  1917. await expectRevertCustomError(
  1918. scheduleOperation(this.manager, {
  1919. caller: this.caller,
  1920. target: this.target.address,
  1921. calldata: this.calldata,
  1922. delay: this.delay,
  1923. }),
  1924. 'AccessManagerAlreadyScheduled',
  1925. [operationId],
  1926. );
  1927. });
  1928. it('panics scheduling calldata with less than 4 bytes', async function () {
  1929. const calldata = '0x1234'; // 2 bytes
  1930. // Managed contract
  1931. await expectRevert.unspecified(
  1932. scheduleOperation(this.manager, {
  1933. caller: this.caller,
  1934. target: this.target.address,
  1935. calldata: calldata,
  1936. delay: this.delay,
  1937. }),
  1938. );
  1939. // Manager contract
  1940. await expectRevert.unspecified(
  1941. scheduleOperation(this.manager, {
  1942. caller: this.caller,
  1943. target: this.manager.address,
  1944. calldata: calldata,
  1945. delay: this.delay,
  1946. }),
  1947. );
  1948. });
  1949. it('reverts scheduling an unknown operation to the manager', async function () {
  1950. const calldata = '0x12345678';
  1951. await expectRevertCustomError(
  1952. scheduleOperation(this.manager, {
  1953. caller: this.caller,
  1954. target: this.manager.address,
  1955. calldata,
  1956. delay: this.delay,
  1957. }),
  1958. 'AccessManagerUnauthorizedCall',
  1959. [this.caller, this.manager.address, calldata],
  1960. );
  1961. });
  1962. });
  1963. describe('#execute', function () {
  1964. const method = 'fnRestricted()';
  1965. beforeEach('set target function role', async function () {
  1966. this.role = { id: web3.utils.toBN(9825430) };
  1967. this.caller = user;
  1968. await this.manager.$_setTargetFunctionRole(this.target.address, selector(method), this.role.id);
  1969. await this.manager.$_grantRole(this.role.id, this.caller, 0, 0);
  1970. this.calldata = this.target.contract.methods[method]().encodeABI();
  1971. });
  1972. describe('restrictions', function () {
  1973. testAsCanCall({
  1974. closed() {
  1975. it('reverts as AccessManagerUnauthorizedCall', async function () {
  1976. await expectRevertCustomError(
  1977. this.manager.execute(this.target.address, this.calldata, { from: this.caller }),
  1978. 'AccessManagerUnauthorizedCall',
  1979. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  1980. );
  1981. });
  1982. },
  1983. open: {
  1984. callerIsTheManager: {
  1985. executing() {
  1986. it('succeeds', async function () {
  1987. await this.manager.execute(this.target.address, this.calldata, { from: this.caller });
  1988. });
  1989. },
  1990. notExecuting() {
  1991. it('reverts as AccessManagerUnauthorizedCall', async function () {
  1992. await expectRevertCustomError(
  1993. this.manager.execute(this.target.address, this.calldata, { from: this.caller }),
  1994. 'AccessManagerUnauthorizedCall',
  1995. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  1996. );
  1997. });
  1998. },
  1999. },
  2000. callerIsNotTheManager: {
  2001. publicRoleIsRequired() {
  2002. it('succeeds', async function () {
  2003. await this.manager.execute(this.target.address, this.calldata, { from: this.caller });
  2004. });
  2005. },
  2006. specificRoleIsRequired: {
  2007. requiredRoleIsGranted: {
  2008. roleGrantingIsDelayed: {
  2009. callerHasAnExecutionDelay: {
  2010. beforeGrantDelay() {
  2011. it('reverts as AccessManagerUnauthorizedCall', async function () {
  2012. await expectRevertCustomError(
  2013. this.manager.execute(this.target.address, this.calldata, { from: this.caller }),
  2014. 'AccessManagerUnauthorizedCall',
  2015. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  2016. );
  2017. });
  2018. },
  2019. afterGrantDelay() {
  2020. beforeEach('define schedule delay', async function () {
  2021. // Consume previously set delay
  2022. await mine();
  2023. this.scheduleIn = time.duration.days(21);
  2024. });
  2025. testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
  2026. },
  2027. },
  2028. callerHasNoExecutionDelay: {
  2029. beforeGrantDelay() {
  2030. it('reverts as AccessManagerUnauthorizedCall', async function () {
  2031. await expectRevertCustomError(
  2032. this.manager.execute(this.target.address, this.calldata, { from: this.caller }),
  2033. 'AccessManagerUnauthorizedCall',
  2034. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  2035. );
  2036. });
  2037. },
  2038. afterGrantDelay() {
  2039. beforeEach('define schedule delay', async function () {
  2040. // Consume previously set delay
  2041. await mine();
  2042. });
  2043. it('succeeds', async function () {
  2044. await this.manager.execute(this.target.address, this.calldata, { from: this.caller });
  2045. });
  2046. },
  2047. },
  2048. },
  2049. roleGrantingIsNotDelayed: {
  2050. callerHasAnExecutionDelay() {
  2051. beforeEach('define schedule delay', async function () {
  2052. this.scheduleIn = time.duration.days(15);
  2053. });
  2054. testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
  2055. },
  2056. callerHasNoExecutionDelay() {
  2057. it('succeeds', async function () {
  2058. await this.manager.execute(this.target.address, this.calldata, { from: this.caller });
  2059. });
  2060. },
  2061. },
  2062. },
  2063. requiredRoleIsNotGranted() {
  2064. it('reverts as AccessManagerUnauthorizedCall', async function () {
  2065. await expectRevertCustomError(
  2066. this.manager.execute(this.target.address, this.calldata, { from: this.caller }),
  2067. 'AccessManagerUnauthorizedCall',
  2068. [this.caller, this.target.address, this.calldata.substring(0, 10)],
  2069. );
  2070. });
  2071. },
  2072. },
  2073. },
  2074. },
  2075. });
  2076. });
  2077. it('executes with a delay consuming the scheduled operation', async function () {
  2078. const delay = time.duration.hours(4);
  2079. await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // Execution delay is needed so the operation is consumed
  2080. const { operationId } = await scheduleOperation(this.manager, {
  2081. caller: this.caller,
  2082. target: this.target.address,
  2083. calldata: this.calldata,
  2084. delay,
  2085. });
  2086. await time.increase(delay);
  2087. const { receipt } = await this.manager.execute(this.target.address, this.calldata, { from: this.caller });
  2088. expectEvent(receipt, 'OperationExecuted', {
  2089. operationId,
  2090. nonce: '1',
  2091. });
  2092. expect(await this.manager.getSchedule(operationId)).to.be.bignumber.equal('0');
  2093. });
  2094. it('executes with no delay consuming a scheduled operation', async function () {
  2095. const delay = time.duration.hours(4);
  2096. // give caller an execution delay
  2097. await this.manager.$_grantRole(this.role.id, this.caller, 0, 1);
  2098. const { operationId } = await scheduleOperation(this.manager, {
  2099. caller: this.caller,
  2100. target: this.target.address,
  2101. calldata: this.calldata,
  2102. delay,
  2103. });
  2104. // remove the execution delay
  2105. await this.manager.$_grantRole(this.role.id, this.caller, 0, 0);
  2106. await time.increase(delay);
  2107. const { receipt } = await this.manager.execute(this.target.address, this.calldata, { from: this.caller });
  2108. expectEvent(receipt, 'OperationExecuted', {
  2109. operationId,
  2110. nonce: '1',
  2111. });
  2112. expect(await this.manager.getSchedule(operationId)).to.be.bignumber.equal('0');
  2113. });
  2114. it('keeps the original _executionId after finishing the call', async function () {
  2115. const executionIdBefore = await getStorageAt(this.manager.address, EXECUTION_ID_STORAGE_SLOT);
  2116. await this.manager.execute(this.target.address, this.calldata, { from: this.caller });
  2117. const executionIdAfter = await getStorageAt(this.manager.address, EXECUTION_ID_STORAGE_SLOT);
  2118. expect(executionIdBefore).to.be.bignumber.equal(executionIdAfter);
  2119. });
  2120. it('reverts executing twice', async function () {
  2121. const delay = time.duration.hours(2);
  2122. await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // Execution delay is needed so the operation is consumed
  2123. const { operationId } = await scheduleOperation(this.manager, {
  2124. caller: this.caller,
  2125. target: this.target.address,
  2126. calldata: this.calldata,
  2127. delay,
  2128. });
  2129. await time.increase(delay);
  2130. await this.manager.execute(this.target.address, this.calldata, { from: this.caller });
  2131. await expectRevertCustomError(
  2132. this.manager.execute(this.target.address, this.calldata, { from: this.caller }),
  2133. 'AccessManagerNotScheduled',
  2134. [operationId],
  2135. );
  2136. });
  2137. });
  2138. describe('#consumeScheduledOp', function () {
  2139. beforeEach('define scheduling parameters', async function () {
  2140. const method = 'fnRestricted()';
  2141. this.caller = this.target.address;
  2142. this.calldata = this.target.contract.methods[method]().encodeABI();
  2143. this.role = { id: web3.utils.toBN(9834983) };
  2144. await this.manager.$_setTargetFunctionRole(this.target.address, selector(method), this.role.id);
  2145. await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // nonzero execution delay
  2146. this.scheduleIn = time.duration.hours(10); // For testAsSchedulableOperation
  2147. });
  2148. describe('when caller is not consuming scheduled operation', function () {
  2149. beforeEach('set consuming false', async function () {
  2150. await this.target.setIsConsumingScheduledOp(false, `0x${CONSUMING_SCHEDULE_STORAGE_SLOT.toString(16)}`);
  2151. });
  2152. it('reverts as AccessManagerUnauthorizedConsume', async function () {
  2153. await impersonate(this.caller);
  2154. await expectRevertCustomError(
  2155. this.manager.consumeScheduledOp(this.caller, this.calldata, { from: this.caller }),
  2156. 'AccessManagerUnauthorizedConsume',
  2157. [this.caller],
  2158. );
  2159. });
  2160. });
  2161. describe('when caller is consuming scheduled operation', function () {
  2162. beforeEach('set consuming true', async function () {
  2163. await this.target.setIsConsumingScheduledOp(true, `0x${CONSUMING_SCHEDULE_STORAGE_SLOT.toString(16)}`);
  2164. });
  2165. testAsSchedulableOperation({
  2166. scheduled: {
  2167. before() {
  2168. it('reverts as AccessManagerNotReady', async function () {
  2169. await impersonate(this.caller);
  2170. await expectRevertCustomError(
  2171. this.manager.consumeScheduledOp(this.caller, this.calldata, { from: this.caller }),
  2172. 'AccessManagerNotReady',
  2173. [this.operationId],
  2174. );
  2175. });
  2176. },
  2177. after() {
  2178. it('consumes the scheduled operation and resets timepoint', async function () {
  2179. expect(await this.manager.getSchedule(this.operationId)).to.be.bignumber.equal(
  2180. this.scheduledAt.add(this.scheduleIn),
  2181. );
  2182. await impersonate(this.caller);
  2183. const { receipt } = await this.manager.consumeScheduledOp(this.caller, this.calldata, {
  2184. from: this.caller,
  2185. });
  2186. expectEvent(receipt, 'OperationExecuted', {
  2187. operationId: this.operationId,
  2188. nonce: '1',
  2189. });
  2190. expect(await this.manager.getSchedule(this.operationId)).to.be.bignumber.equal('0');
  2191. });
  2192. },
  2193. expired() {
  2194. it('reverts as AccessManagerExpired', async function () {
  2195. await impersonate(this.caller);
  2196. await expectRevertCustomError(
  2197. this.manager.consumeScheduledOp(this.caller, this.calldata, { from: this.caller }),
  2198. 'AccessManagerExpired',
  2199. [this.operationId],
  2200. );
  2201. });
  2202. },
  2203. },
  2204. notScheduled() {
  2205. it('reverts as AccessManagerNotScheduled', async function () {
  2206. await impersonate(this.caller);
  2207. await expectRevertCustomError(
  2208. this.manager.consumeScheduledOp(this.caller, this.calldata, { from: this.caller }),
  2209. 'AccessManagerNotScheduled',
  2210. [this.operationId],
  2211. );
  2212. });
  2213. },
  2214. });
  2215. });
  2216. });
  2217. describe('#cancelScheduledOp', function () {
  2218. const method = 'fnRestricted()';
  2219. beforeEach('setup scheduling', async function () {
  2220. this.caller = this.roles.SOME.members[0];
  2221. await this.manager.$_setTargetFunctionRole(this.target.address, selector(method), this.roles.SOME.id);
  2222. await this.manager.$_grantRole(this.roles.SOME.id, this.caller, 0, 1); // nonzero execution delay
  2223. this.calldata = this.target.contract.methods[method]().encodeABI();
  2224. this.scheduleIn = time.duration.days(10); // For testAsSchedulableOperation
  2225. });
  2226. testAsSchedulableOperation({
  2227. scheduled: {
  2228. before() {
  2229. describe('when caller is the scheduler', function () {
  2230. it('succeeds', async function () {
  2231. await this.manager.cancel(this.caller, this.target.address, this.calldata, { from: this.caller });
  2232. });
  2233. });
  2234. describe('when caller is an admin', function () {
  2235. it('succeeds', async function () {
  2236. await this.manager.cancel(this.caller, this.target.address, this.calldata, {
  2237. from: this.roles.ADMIN.members[0],
  2238. });
  2239. });
  2240. });
  2241. describe('when caller is the role guardian', function () {
  2242. it('succeeds', async function () {
  2243. await this.manager.cancel(this.caller, this.target.address, this.calldata, {
  2244. from: this.roles.SOME_GUARDIAN.members[0],
  2245. });
  2246. });
  2247. });
  2248. describe('when caller is any other account', function () {
  2249. it('reverts as AccessManagerUnauthorizedCancel', async function () {
  2250. await expectRevertCustomError(
  2251. this.manager.cancel(this.caller, this.target.address, this.calldata, { from: other }),
  2252. 'AccessManagerUnauthorizedCancel',
  2253. [other, this.caller, this.target.address, selector(method)],
  2254. );
  2255. });
  2256. });
  2257. },
  2258. after() {
  2259. it('succeeds', async function () {
  2260. await this.manager.cancel(this.caller, this.target.address, this.calldata, { from: this.caller });
  2261. });
  2262. },
  2263. expired() {
  2264. it('succeeds', async function () {
  2265. await this.manager.cancel(this.caller, this.target.address, this.calldata, { from: this.caller });
  2266. });
  2267. },
  2268. },
  2269. notScheduled() {
  2270. it('reverts as AccessManagerNotScheduled', async function () {
  2271. await expectRevertCustomError(
  2272. this.manager.cancel(this.caller, this.target.address, this.calldata),
  2273. 'AccessManagerNotScheduled',
  2274. [this.operationId],
  2275. );
  2276. });
  2277. },
  2278. });
  2279. it('cancels an operation and resets schedule', async function () {
  2280. const { operationId } = await scheduleOperation(this.manager, {
  2281. caller: this.caller,
  2282. target: this.target.address,
  2283. calldata: this.calldata,
  2284. delay: this.scheduleIn,
  2285. });
  2286. const { receipt } = await this.manager.cancel(this.caller, this.target.address, this.calldata, {
  2287. from: this.caller,
  2288. });
  2289. expectEvent(receipt, 'OperationCanceled', {
  2290. operationId,
  2291. nonce: '1',
  2292. });
  2293. expect(await this.manager.getSchedule(operationId)).to.be.bignumber.eq('0');
  2294. });
  2295. });
  2296. describe('with Ownable target contract', function () {
  2297. const roleId = web3.utils.toBN(1);
  2298. beforeEach(async function () {
  2299. this.ownable = await Ownable.new(this.manager.address);
  2300. // add user to role
  2301. await this.manager.$_grantRole(roleId, user, 0, 0);
  2302. });
  2303. it('initial state', async function () {
  2304. expect(await this.ownable.owner()).to.be.equal(this.manager.address);
  2305. });
  2306. describe('Contract is closed', function () {
  2307. beforeEach(async function () {
  2308. await this.manager.$_setTargetClosed(this.ownable.address, true);
  2309. });
  2310. it('directly call: reverts', async function () {
  2311. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [user]);
  2312. });
  2313. it('relayed call (with role): reverts', async function () {
  2314. await expectRevertCustomError(
  2315. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user }),
  2316. 'AccessManagerUnauthorizedCall',
  2317. [user, this.ownable.address, selector('$_checkOwner()')],
  2318. );
  2319. });
  2320. it('relayed call (without role): reverts', async function () {
  2321. await expectRevertCustomError(
  2322. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other }),
  2323. 'AccessManagerUnauthorizedCall',
  2324. [other, this.ownable.address, selector('$_checkOwner()')],
  2325. );
  2326. });
  2327. });
  2328. describe('Contract is managed', function () {
  2329. describe('function is open to specific role', function () {
  2330. beforeEach(async function () {
  2331. await this.manager.$_setTargetFunctionRole(this.ownable.address, selector('$_checkOwner()'), roleId);
  2332. });
  2333. it('directly call: reverts', async function () {
  2334. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [
  2335. user,
  2336. ]);
  2337. });
  2338. it('relayed call (with role): success', async function () {
  2339. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user });
  2340. });
  2341. it('relayed call (without role): reverts', async function () {
  2342. await expectRevertCustomError(
  2343. this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other }),
  2344. 'AccessManagerUnauthorizedCall',
  2345. [other, this.ownable.address, selector('$_checkOwner()')],
  2346. );
  2347. });
  2348. });
  2349. describe('function is open to public role', function () {
  2350. beforeEach(async function () {
  2351. await this.manager.$_setTargetFunctionRole(
  2352. this.ownable.address,
  2353. selector('$_checkOwner()'),
  2354. this.roles.PUBLIC.id,
  2355. );
  2356. });
  2357. it('directly call: reverts', async function () {
  2358. await expectRevertCustomError(this.ownable.$_checkOwner({ from: user }), 'OwnableUnauthorizedAccount', [
  2359. user,
  2360. ]);
  2361. });
  2362. it('relayed call (with role): success', async function () {
  2363. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: user });
  2364. });
  2365. it('relayed call (without role): success', async function () {
  2366. await this.manager.execute(this.ownable.address, selector('$_checkOwner()'), { from: other });
  2367. });
  2368. });
  2369. });
  2370. });
  2371. });