123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469 |
- const { ethers } = require('hardhat');
- const { expect } = require('chai');
- const { loadFixture, getStorageAt } = require('@nomicfoundation/hardhat-network-helpers');
- const { impersonate } = require('../../helpers/account');
- const { MAX_UINT48 } = require('../../helpers/constants');
- const { bigint: time } = require('../../helpers/time');
- const { selector } = require('../../helpers/methods');
- const {
- buildBaseRoles,
- formatAccess,
- EXPIRATION,
- MINSETBACK,
- EXECUTION_ID_STORAGE_SLOT,
- CONSUMING_SCHEDULE_STORAGE_SLOT,
- prepareOperation,
- hashOperation,
- } = require('../../helpers/access-manager');
- const {
- shouldBehaveLikeDelayedAdminOperation,
- shouldBehaveLikeNotDelayedAdminOperation,
- shouldBehaveLikeRoleAdminOperation,
- shouldBehaveLikeAManagedRestrictedOperation,
- } = require('./AccessManager.behavior');
- const {
- LIKE_COMMON_SCHEDULABLE,
- testAsClosable,
- testAsDelay,
- testAsSchedulableOperation,
- testAsCanCall,
- testAsHasRole,
- testAsGetAccess,
- } = require('./AccessManager.predicate');
- const { address: someAddress } = ethers.Wallet.createRandom();
- async function fixture() {
- const [admin, roleAdmin, roleGuardian, member, user, other] = await ethers.getSigners();
- // Build roles
- const roles = buildBaseRoles();
- // Add members
- roles.ADMIN.members = [admin];
- roles.SOME_ADMIN.members = [roleAdmin];
- roles.SOME_GUARDIAN.members = [roleGuardian];
- roles.SOME.members = [member];
- roles.PUBLIC.members = [admin, roleAdmin, roleGuardian, member, user, other];
- const manager = await ethers.deployContract('$AccessManager', [admin]);
- const target = await ethers.deployContract('$AccessManagedTarget', [manager]);
- for (const { id: roleId, admin, guardian, members } of Object.values(roles)) {
- if (roleId === roles.PUBLIC.id) continue; // Every address belong to public and is locked
- if (roleId === roles.ADMIN.id) continue; // Admin set during construction and is locked
- // Set admin role avoiding default
- if (admin.id !== roles.ADMIN.id) {
- await manager.$_setRoleAdmin(roleId, admin.id);
- }
- // Set guardian role avoiding default
- if (guardian.id !== roles.ADMIN.id) {
- await manager.$_setRoleGuardian(roleId, guardian.id);
- }
- // Grant role to members
- for (const member of members) {
- await manager.$_grantRole(roleId, member, 0, 0);
- }
- }
- return {
- // TODO: Check if all signers are actually used
- admin,
- roleAdmin,
- roleGuardian,
- member,
- user,
- other,
- roles,
- manager,
- target,
- };
- }
- // This test suite is made using the following tools:
- //
- // * Predicates: Functions with common conditional setups without assertions.
- // * Behaviors: Functions with common assertions.
- //
- // The behavioral tests are built by composing predicates and are used as templates
- // for testing access to restricted functions.
- //
- // Similarly, unit tests in this suite will use predicates to test subsets of these
- // behaviors and are helped by common assertions provided for some of the predicates.
- //
- // The predicates can be identified by the `testAs*` prefix while the behaviors
- // are prefixed with `shouldBehave*`. The common assertions for predicates are
- // defined as constants.
- contract('AccessManager', function () {
- // const [admin, manager, guardian, member, user, other] = accounts;
- beforeEach(async function () {
- Object.assign(this, await loadFixture(fixture));
- });
- describe('during construction', function () {
- it('grants admin role to initialAdmin', async function () {
- const manager = await ethers.deployContract('$AccessManager', [this.other]);
- expect(await manager.hasRole(this.roles.ADMIN.id, this.other).then(formatAccess)).to.be.deep.equal([true, '0']);
- });
- it('rejects zero address for initialAdmin', async function () {
- await expect(ethers.deployContract('$AccessManager', [ethers.ZeroAddress]))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerInvalidInitialAdmin')
- .withArgs(ethers.ZeroAddress);
- });
- it('initializes setup roles correctly', async function () {
- for (const { id: roleId, admin, guardian, members } of Object.values(this.roles)) {
- expect(await this.manager.getRoleAdmin(roleId)).to.equal(admin.id);
- expect(await this.manager.getRoleGuardian(roleId)).to.equal(guardian.id);
- for (const user of this.roles.PUBLIC.members) {
- expect(await this.manager.hasRole(roleId, user).then(formatAccess)).to.be.deep.equal([
- members.includes(user),
- '0',
- ]);
- }
- }
- });
- });
- describe('getters', function () {
- describe('#canCall', function () {
- beforeEach('set calldata', function () {
- this.calldata = '0x12345678';
- this.role = { id: 379204n };
- });
- testAsCanCall({
- closed() {
- it('should return false and no delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- someAddress,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.false;
- expect(delay).to.equal(0n);
- });
- },
- open: {
- callerIsTheManager: {
- executing() {
- it('should return true and no delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.true;
- expect(delay).to.equal(0n);
- });
- },
- notExecuting() {
- it('should return false and no delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.false;
- expect(delay).to.equal(0n);
- });
- },
- },
- callerIsNotTheManager: {
- publicRoleIsRequired() {
- it('should return true and no delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.true;
- expect(delay).to.equal(0n);
- });
- },
- specificRoleIsRequired: {
- requiredRoleIsGranted: {
- roleGrantingIsDelayed: {
- callerHasAnExecutionDelay: {
- beforeGrantDelay: function self() {
- self.mineDelay = true;
- it('should return false and no execution delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.false;
- expect(delay).to.equal(0n);
- });
- },
- afterGrantDelay: function self() {
- self.mineDelay = true;
- beforeEach('sets execution delay', function () {
- this.scheduleIn = this.executionDelay; // For testAsSchedulableOperation
- });
- testAsSchedulableOperation({
- scheduled: {
- before: function self() {
- self.mineDelay = true;
- it('should return false and execution delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.false;
- expect(delay).to.equal(this.executionDelay);
- });
- },
- after: function self() {
- self.mineDelay = true;
- it('should return false and execution delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.false;
- expect(delay).to.equal(this.executionDelay);
- });
- },
- expired: function self() {
- self.mineDelay = true;
- it('should return false and execution delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.false;
- expect(delay).to.equal(this.executionDelay);
- });
- },
- },
- notScheduled() {
- it('should return false and execution delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.false;
- expect(delay).to.equal(this.executionDelay);
- });
- },
- });
- },
- },
- callerHasNoExecutionDelay: {
- beforeGrantDelay: function self() {
- self.mineDelay = true;
- it('should return false and no execution delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.false;
- expect(delay).to.equal(0n);
- });
- },
- afterGrantDelay: function self() {
- self.mineDelay = true;
- it('should return true and no execution delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.true;
- expect(delay).to.equal(0n);
- });
- },
- },
- },
- roleGrantingIsNotDelayed: {
- callerHasAnExecutionDelay() {
- it('should return false and execution delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.false;
- expect(delay).to.equal(this.executionDelay);
- });
- },
- callerHasNoExecutionDelay() {
- it('should return true and no execution delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.true;
- expect(delay).to.equal(0n);
- });
- },
- },
- },
- requiredRoleIsNotGranted() {
- it('should return false and no execution delay', async function () {
- const { immediate, delay } = await this.manager.canCall(
- this.caller,
- this.target,
- this.calldata.substring(0, 10),
- );
- expect(immediate).to.be.false;
- expect(delay).to.equal(0n);
- });
- },
- },
- },
- },
- });
- });
- describe('#expiration', function () {
- it('has a 7 days default expiration', async function () {
- expect(await this.manager.expiration()).to.equal(EXPIRATION);
- });
- });
- describe('#minSetback', function () {
- it('has a 5 days default minimum setback', async function () {
- expect(await this.manager.minSetback()).to.equal(MINSETBACK);
- });
- });
- describe('#isTargetClosed', function () {
- testAsClosable({
- closed() {
- it('returns true', async function () {
- expect(await this.manager.isTargetClosed(this.target)).to.be.true;
- });
- },
- open() {
- it('returns false', async function () {
- expect(await this.manager.isTargetClosed(this.target)).to.be.false;
- });
- },
- });
- });
- describe('#getTargetFunctionRole', function () {
- const methodSelector = selector('something(address,bytes)');
- it('returns the target function role', async function () {
- const roleId = 21498n;
- await this.manager.$_setTargetFunctionRole(this.target, methodSelector, roleId);
- expect(await this.manager.getTargetFunctionRole(this.target, methodSelector)).to.equal(roleId);
- });
- it('returns the ADMIN role if not set', async function () {
- expect(await this.manager.getTargetFunctionRole(this.target, methodSelector)).to.equal(this.roles.ADMIN.id);
- });
- });
- describe('#getTargetAdminDelay', function () {
- describe('when the target admin delay is setup', function () {
- beforeEach('set target admin delay', async function () {
- this.oldDelay = await this.manager.getTargetAdminDelay(this.target);
- this.newDelay = time.duration.days(10);
- await this.manager.$_setTargetAdminDelay(this.target, this.newDelay);
- this.delay = MINSETBACK; // For testAsDelay
- });
- testAsDelay('effect', {
- before: function self() {
- self.mineDelay = true;
- it('returns the old target admin delay', async function () {
- expect(await this.manager.getTargetAdminDelay(this.target)).to.equal(this.oldDelay);
- });
- },
- after: function self() {
- self.mineDelay = true;
- it('returns the new target admin delay', async function () {
- expect(await this.manager.getTargetAdminDelay(this.target)).to.equal(this.newDelay);
- });
- },
- });
- });
- it('returns the 0 if not set', async function () {
- expect(await this.manager.getTargetAdminDelay(this.target)).to.equal(0n);
- });
- });
- describe('#getRoleAdmin', function () {
- const roleId = 5234907n;
- it('returns the role admin', async function () {
- const adminId = 789433n;
- await this.manager.$_setRoleAdmin(roleId, adminId);
- expect(await this.manager.getRoleAdmin(roleId)).to.equal(adminId);
- });
- it('returns the ADMIN role if not set', async function () {
- expect(await this.manager.getRoleAdmin(roleId)).to.equal(this.roles.ADMIN.id);
- });
- });
- describe('#getRoleGuardian', function () {
- const roleId = 5234907n;
- it('returns the role guardian', async function () {
- const guardianId = 789433n;
- await this.manager.$_setRoleGuardian(roleId, guardianId);
- expect(await this.manager.getRoleGuardian(roleId)).to.equal(guardianId);
- });
- it('returns the ADMIN role if not set', async function () {
- expect(await this.manager.getRoleGuardian(roleId)).to.equal(this.roles.ADMIN.id);
- });
- });
- describe('#getRoleGrantDelay', function () {
- const roleId = 9248439n;
- describe('when the grant admin delay is setup', function () {
- beforeEach('set grant admin delay', async function () {
- this.oldDelay = await this.manager.getRoleGrantDelay(roleId);
- this.newDelay = time.duration.days(11);
- await this.manager.$_setGrantDelay(roleId, this.newDelay);
- this.delay = MINSETBACK; // For testAsDelay
- });
- testAsDelay('grant', {
- before: function self() {
- self.mineDelay = true;
- it('returns the old role grant delay', async function () {
- expect(await this.manager.getRoleGrantDelay(roleId)).to.equal(this.oldDelay);
- });
- },
- after: function self() {
- self.mineDelay = true;
- it('returns the new role grant delay', async function () {
- expect(await this.manager.getRoleGrantDelay(roleId)).to.equal(this.newDelay);
- });
- },
- });
- });
- it('returns 0 if delay is not set', async function () {
- expect(await this.manager.getTargetAdminDelay(this.target)).to.equal(0n);
- });
- });
- describe('#getAccess', function () {
- beforeEach('set role', function () {
- this.role = { id: 9452n };
- this.caller = this.user;
- });
- testAsGetAccess({
- requiredRoleIsGranted: {
- roleGrantingIsDelayed: {
- callerHasAnExecutionDelay: {
- beforeGrantDelay: function self() {
- self.mineDelay = true;
- it('role is not in effect and execution delay is set', async function () {
- const access = await this.manager.getAccess(this.role.id, this.caller);
- expect(access[0]).to.equal(this.delayEffect); // inEffectSince
- expect(access[1]).to.equal(this.executionDelay); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Not in effect yet
- expect(await time.clock.timestamp()).to.lt(access[0]);
- });
- },
- afterGrantDelay: function self() {
- self.mineDelay = true;
- it('access has role in effect and execution delay is set', async function () {
- const access = await this.manager.getAccess(this.role.id, this.caller);
- expect(access[0]).to.equal(this.delayEffect); // inEffectSince
- expect(access[1]).to.equal(this.executionDelay); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Already in effect
- expect(await time.clock.timestamp()).to.equal(access[0]);
- });
- },
- },
- callerHasNoExecutionDelay: {
- beforeGrantDelay: function self() {
- self.mineDelay = true;
- it('access has role not in effect without execution delay', async function () {
- const access = await this.manager.getAccess(this.role.id, this.caller);
- expect(access[0]).to.equal(this.delayEffect); // inEffectSince
- expect(access[1]).to.equal(0n); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Not in effect yet
- expect(await time.clock.timestamp()).to.lt(access[0]);
- });
- },
- afterGrantDelay: function self() {
- self.mineDelay = true;
- it('role is in effect without execution delay', async function () {
- const access = await this.manager.getAccess(this.role.id, this.caller);
- expect(access[0]).to.equal(this.delayEffect); // inEffectSince
- expect(access[1]).to.equal(0n); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Already in effect
- expect(await time.clock.timestamp()).to.equal(access[0]);
- });
- },
- },
- },
- roleGrantingIsNotDelayed: {
- callerHasAnExecutionDelay() {
- it('access has role in effect and execution delay is set', async function () {
- const access = await this.manager.getAccess(this.role.id, this.caller);
- expect(access[0]).to.equal(await time.clock.timestamp()); // inEffectSince
- expect(access[1]).to.equal(this.executionDelay); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Already in effect
- expect(await time.clock.timestamp()).to.equal(access[0]);
- });
- },
- callerHasNoExecutionDelay() {
- it('access has role in effect without execution delay', async function () {
- const access = await this.manager.getAccess(this.role.id, this.caller);
- expect(access[0]).to.equal(await time.clock.timestamp()); // inEffectSince
- expect(access[1]).to.equal(0n); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Already in effect
- expect(await time.clock.timestamp()).to.equal(access[0]);
- });
- },
- },
- },
- requiredRoleIsNotGranted() {
- it('has empty access', async function () {
- const access = await this.manager.getAccess(this.role.id, this.caller);
- expect(access[0]).to.equal(0n); // inEffectSince
- expect(access[1]).to.equal(0n); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- });
- },
- });
- });
- describe('#hasRole', function () {
- beforeEach('setup testAsHasRole', function () {
- this.role = { id: 49832n };
- this.calldata = '0x12345678';
- this.caller = this.user;
- });
- testAsHasRole({
- publicRoleIsRequired() {
- it('has PUBLIC role', async function () {
- const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
- expect(isMember).to.be.true;
- expect(executionDelay).to.equal('0');
- });
- },
- specificRoleIsRequired: {
- requiredRoleIsGranted: {
- roleGrantingIsDelayed: {
- callerHasAnExecutionDelay: {
- beforeGrantDelay: function self() {
- self.mineDelay = true;
- it('does not have role but execution delay', async function () {
- const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
- expect(isMember).to.be.false;
- expect(executionDelay).to.equal(this.executionDelay);
- });
- },
- afterGrantDelay: function self() {
- self.mineDelay = true;
- it('has role and execution delay', async function () {
- const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
- expect(isMember).to.be.true;
- expect(executionDelay).to.equal(this.executionDelay);
- });
- },
- },
- callerHasNoExecutionDelay: {
- beforeGrantDelay: function self() {
- self.mineDelay = true;
- it('does not have role nor execution delay', async function () {
- const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
- expect(isMember).to.be.false;
- expect(executionDelay).to.equal('0');
- });
- },
- afterGrantDelay: function self() {
- self.mineDelay = true;
- it('has role and no execution delay', async function () {
- const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
- expect(isMember).to.be.true;
- expect(executionDelay).to.equal('0');
- });
- },
- },
- },
- roleGrantingIsNotDelayed: {
- callerHasAnExecutionDelay() {
- it('has role and execution delay', async function () {
- const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
- expect(isMember).to.be.true;
- expect(executionDelay).to.equal(this.executionDelay);
- });
- },
- callerHasNoExecutionDelay() {
- it('has role and no execution delay', async function () {
- const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
- expect(isMember).to.be.true;
- expect(executionDelay).to.equal('0');
- });
- },
- },
- },
- requiredRoleIsNotGranted() {
- it('has no role and no execution delay', async function () {
- const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
- expect(isMember).to.be.false;
- expect(executionDelay).to.equal('0');
- });
- },
- },
- });
- });
- describe('#getSchedule', function () {
- beforeEach('set role and calldata', async function () {
- const fnRestricted = this.target.fnRestricted.getFragment().selector;
- this.caller = this.user;
- this.role = { id: 493590n };
- await this.manager.$_setTargetFunctionRole(this.target, fnRestricted, this.role.id);
- await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // nonzero execution delay
- this.calldata = this.target.interface.encodeFunctionData(fnRestricted, []);
- this.scheduleIn = time.duration.days(10); // For testAsSchedulableOperation
- });
- testAsSchedulableOperation({
- scheduled: {
- before: function self() {
- self.mineDelay = true;
- it('returns schedule in the future', async function () {
- const schedule = await this.manager.getSchedule(this.operationId);
- expect(schedule).to.equal(this.scheduledAt + this.scheduleIn);
- expect(schedule).to.gt(await time.clock.timestamp());
- });
- },
- after: function self() {
- self.mineDelay = true;
- it('returns schedule', async function () {
- const schedule = await this.manager.getSchedule(this.operationId);
- expect(schedule).to.equal(this.scheduledAt + this.scheduleIn);
- expect(schedule).to.equal(await time.clock.timestamp());
- });
- },
- expired: function self() {
- self.mineDelay = true;
- it('returns 0', async function () {
- expect(await this.manager.getSchedule(this.operationId)).to.equal(0n);
- });
- },
- },
- notScheduled() {
- it('defaults to 0', async function () {
- expect(await this.manager.getSchedule(this.operationId)).to.equal(0n);
- });
- },
- });
- });
- describe('#getNonce', function () {
- describe('when operation is scheduled', function () {
- beforeEach('schedule operation', async function () {
- const fnRestricted = this.target.fnRestricted.getFragment().selector;
- this.caller = this.user;
- this.role = { id: 4209043n };
- await this.manager.$_setTargetFunctionRole(this.target, fnRestricted, this.role.id);
- await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // nonzero execution delay
- this.calldata = this.target.interface.encodeFunctionData(fnRestricted, []);
- this.delay = time.duration.days(10);
- const { operationId, schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.delay,
- });
- await schedule();
- this.operationId = operationId;
- });
- it('returns nonce', async function () {
- expect(await this.manager.getNonce(this.operationId)).to.equal(1n);
- });
- });
- describe('when is not scheduled', function () {
- it('returns default 0', async function () {
- expect(await this.manager.getNonce(ethers.id('operation'))).to.equal(0n);
- });
- });
- });
- describe('#hashOperation', function () {
- it('returns an operationId', async function () {
- const calldata = '0x123543';
- const address = someAddress;
- const args = [this.user.address, address, calldata];
- expect(await this.manager.hashOperation(...args)).to.equal(hashOperation(...args));
- });
- });
- });
- describe('admin operations', function () {
- beforeEach('set required role', function () {
- this.role = this.roles.ADMIN;
- });
- describe('subject to a delay', function () {
- describe('#labelRole', function () {
- describe('restrictions', function () {
- beforeEach('set method and args', function () {
- const args = [123443, 'TEST'];
- const method = this.manager.interface.getFunction('labelRole(uint64,string)');
- this.calldata = this.manager.interface.encodeFunctionData(method, args);
- });
- shouldBehaveLikeDelayedAdminOperation();
- });
- it('emits an event with the label', async function () {
- await expect(this.manager.connect(this.admin).labelRole(this.roles.SOME.id, 'Some label'))
- .to.emit(this.manager, 'RoleLabel')
- .withArgs(this.roles.SOME.id, 'Some label');
- });
- it('updates label on a second call', async function () {
- await this.manager.connect(this.admin).labelRole(this.roles.SOME.id, 'Some label');
- await expect(this.manager.connect(this.admin).labelRole(this.roles.SOME.id, 'Updated label'))
- .to.emit(this.manager, 'RoleLabel')
- .withArgs(this.roles.SOME.id, 'Updated label');
- });
- it('reverts labeling PUBLIC_ROLE', async function () {
- await expect(this.manager.connect(this.admin).labelRole(this.roles.PUBLIC.id, 'Some label'))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerLockedRole')
- .withArgs(this.roles.PUBLIC.id);
- });
- it('reverts labeling ADMIN_ROLE', async function () {
- await expect(this.manager.connect(this.admin).labelRole(this.roles.ADMIN.id, 'Some label'))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerLockedRole')
- .withArgs(this.roles.ADMIN.id);
- });
- });
- describe('#setRoleAdmin', function () {
- describe('restrictions', function () {
- beforeEach('set method and args', function () {
- const args = [93445, 84532];
- const method = this.manager.interface.getFunction('setRoleAdmin(uint64,uint64)');
- this.calldata = this.manager.interface.encodeFunctionData(method, args);
- });
- shouldBehaveLikeDelayedAdminOperation();
- });
- it("sets any role's admin if called by an admin", async function () {
- expect(await this.manager.getRoleAdmin(this.roles.SOME.id)).to.equal(this.roles.SOME_ADMIN.id);
- await expect(this.manager.connect(this.admin).setRoleAdmin(this.roles.SOME.id, this.roles.ADMIN.id))
- .to.emit(this.manager, 'RoleAdminChanged')
- .withArgs(this.roles.SOME.id, this.roles.ADMIN.id);
- expect(await this.manager.getRoleAdmin(this.roles.SOME.id)).to.equal(this.roles.ADMIN.id);
- });
- it('reverts setting PUBLIC_ROLE admin', async function () {
- await expect(this.manager.connect(this.admin).setRoleAdmin(this.roles.PUBLIC.id, this.roles.ADMIN.id))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerLockedRole')
- .withArgs(this.roles.PUBLIC.id);
- });
- it('reverts setting ADMIN_ROLE admin', async function () {
- await expect(this.manager.connect(this.admin).setRoleAdmin(this.roles.ADMIN.id, this.roles.ADMIN.id))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerLockedRole')
- .withArgs(this.roles.ADMIN.id);
- });
- });
- describe('#setRoleGuardian', function () {
- describe('restrictions', function () {
- beforeEach('set method and args', function () {
- const args = [93445, 84532];
- const method = this.manager.interface.getFunction('setRoleGuardian(uint64,uint64)');
- this.calldata = this.manager.interface.encodeFunctionData(method, args);
- });
- shouldBehaveLikeDelayedAdminOperation();
- });
- it("sets any role's guardian if called by an admin", async function () {
- expect(await this.manager.getRoleGuardian(this.roles.SOME.id)).to.equal(this.roles.SOME_GUARDIAN.id);
- await expect(this.manager.connect(this.admin).setRoleGuardian(this.roles.SOME.id, this.roles.ADMIN.id))
- .to.emit(this.manager, 'RoleGuardianChanged')
- .withArgs(this.roles.SOME.id, this.roles.ADMIN.id);
- expect(await this.manager.getRoleGuardian(this.roles.SOME.id)).to.equal(this.roles.ADMIN.id);
- });
- it('reverts setting PUBLIC_ROLE admin', async function () {
- await expect(this.manager.connect(this.admin).setRoleGuardian(this.roles.PUBLIC.id, this.roles.ADMIN.id))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerLockedRole')
- .withArgs(this.roles.PUBLIC.id);
- });
- it('reverts setting ADMIN_ROLE admin', async function () {
- await expect(this.manager.connect(this.admin).setRoleGuardian(this.roles.ADMIN.id, this.roles.ADMIN.id))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerLockedRole')
- .withArgs(this.roles.ADMIN.id);
- });
- });
- describe('#setGrantDelay', function () {
- describe('restrictions', function () {
- beforeEach('set method and args', function () {
- const args = [984910, time.duration.days(2)];
- const method = this.manager.interface.getFunction('setGrantDelay(uint64,uint32)');
- this.calldata = this.manager.interface.encodeFunctionData(method, args);
- });
- shouldBehaveLikeDelayedAdminOperation();
- });
- it('reverts setting grant delay for the PUBLIC_ROLE', function () {
- expect(this.manager.connect(this.admin).setGrantDelay(this.roles.PUBLIC.id, 69n))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerLockedRole')
- .withArgs(this.roles.PUBLIC.id);
- });
- describe('when increasing the delay', function () {
- const oldDelay = 10n;
- const newDelay = 100n;
- beforeEach('sets old delay', async function () {
- this.role = this.roles.SOME;
- await this.manager.$_setGrantDelay(this.role.id, oldDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- expect(await this.manager.getRoleGrantDelay(this.role.id)).to.equal(oldDelay);
- });
- it('increases the delay after minsetback', async function () {
- const txResponse = await this.manager.connect(this.admin).setGrantDelay(this.role.id, newDelay);
- const setGrantDelayAt = await time.clockFromReceipt.timestamp(txResponse);
- expect(txResponse)
- .to.emit(this.manager, 'RoleGrantDelayChanged')
- .withArgs(this.role.id, newDelay, setGrantDelayAt + MINSETBACK);
- expect(await this.manager.getRoleGrantDelay(this.role.id)).to.equal(oldDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- expect(await this.manager.getRoleGrantDelay(this.role.id)).to.equal(newDelay);
- });
- });
- describe('when reducing the delay', function () {
- const oldDelay = time.duration.days(10);
- beforeEach('sets old delay', async function () {
- this.role = this.roles.SOME;
- await this.manager.$_setGrantDelay(this.role.id, oldDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- expect(await this.manager.getRoleGrantDelay(this.role.id)).to.equal(oldDelay);
- });
- describe('when the delay difference is shorter than minimum setback', function () {
- const newDelay = oldDelay - 1n;
- it('increases the delay after minsetback', async function () {
- const txResponse = await this.manager.connect(this.admin).setGrantDelay(this.role.id, newDelay);
- const setGrantDelayAt = await time.clockFromReceipt.timestamp(txResponse);
- expect(txResponse)
- .to.emit(this.manager, 'RoleGrantDelayChanged')
- .withArgs(this.role.id, newDelay, setGrantDelayAt + MINSETBACK);
- expect(await this.manager.getRoleGrantDelay(this.role.id)).to.equal(oldDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- expect(await this.manager.getRoleGrantDelay(this.role.id)).to.equal(newDelay);
- });
- });
- describe('when the delay difference is longer than minimum setback', function () {
- const newDelay = 1n;
- beforeEach('assert delay difference is higher than minsetback', function () {
- expect(oldDelay - newDelay).to.gt(MINSETBACK);
- });
- it('increases the delay after delay difference', async function () {
- const setback = oldDelay - newDelay;
- const txResponse = await this.manager.connect(this.admin).setGrantDelay(this.role.id, newDelay);
- const setGrantDelayAt = await time.clockFromReceipt.timestamp(txResponse);
- expect(txResponse)
- .to.emit(this.manager, 'RoleGrantDelayChanged')
- .withArgs(this.role.id, newDelay, setGrantDelayAt + setback);
- expect(await this.manager.getRoleGrantDelay(this.role.id)).to.equal(oldDelay);
- await time.increaseBy.timestamp(setback);
- expect(await this.manager.getRoleGrantDelay(this.role.id)).to.equal(newDelay);
- });
- });
- });
- });
- describe('#setTargetAdminDelay', function () {
- describe('restrictions', function () {
- beforeEach('set method and args', function () {
- const args = [someAddress, time.duration.days(3)];
- const method = this.manager.interface.getFunction('setTargetAdminDelay(address,uint32)');
- this.calldata = this.manager.interface.encodeFunctionData(method, args);
- });
- shouldBehaveLikeDelayedAdminOperation();
- });
- describe('when increasing the delay', function () {
- const oldDelay = time.duration.days(10);
- const newDelay = time.duration.days(11);
- const target = someAddress;
- beforeEach('sets old delay', async function () {
- await this.manager.$_setTargetAdminDelay(target, oldDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- expect(await this.manager.getTargetAdminDelay(target)).to.equal(oldDelay);
- });
- it('increases the delay after minsetback', async function () {
- const txResponse = await this.manager.connect(this.admin).setTargetAdminDelay(target, newDelay);
- const setTargetAdminDelayAt = await time.clockFromReceipt.timestamp(txResponse);
- expect(txResponse)
- .to.emit(this.manager, 'TargetAdminDelayUpdated')
- .withArgs(target, newDelay, setTargetAdminDelayAt + MINSETBACK);
- expect(await this.manager.getTargetAdminDelay(target)).to.equal(oldDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- expect(await this.manager.getTargetAdminDelay(target)).to.equal(newDelay);
- });
- });
- describe('when reducing the delay', function () {
- const oldDelay = time.duration.days(10);
- const target = someAddress;
- beforeEach('sets old delay', async function () {
- await this.manager.$_setTargetAdminDelay(target, oldDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- expect(await this.manager.getTargetAdminDelay(target)).to.equal(oldDelay);
- });
- describe('when the delay difference is shorter than minimum setback', function () {
- const newDelay = oldDelay - 1n;
- it('increases the delay after minsetback', async function () {
- const txResponse = await this.manager.connect(this.admin).setTargetAdminDelay(target, newDelay);
- const setTargetAdminDelayAt = await time.clockFromReceipt.timestamp(txResponse);
- expect(txResponse)
- .to.emit(this.manager, 'TargetAdminDelayUpdated')
- .withArgs(target, newDelay, setTargetAdminDelayAt + MINSETBACK);
- expect(await this.manager.getTargetAdminDelay(target)).to.equal(oldDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- expect(await this.manager.getTargetAdminDelay(target)).to.equal(newDelay);
- });
- });
- describe('when the delay difference is longer than minimum setback', function () {
- const newDelay = 1n;
- beforeEach('assert delay difference is higher than minsetback', function () {
- expect(oldDelay - newDelay).to.gt(MINSETBACK);
- });
- it('increases the delay after delay difference', async function () {
- const setback = oldDelay - newDelay;
- const txResponse = await this.manager.connect(this.admin).setTargetAdminDelay(target, newDelay);
- const setTargetAdminDelayAt = await time.clockFromReceipt.timestamp(txResponse);
- expect(txResponse)
- .to.emit(this.manager, 'TargetAdminDelayUpdated')
- .withArgs(target, newDelay, setTargetAdminDelayAt + setback);
- expect(await this.manager.getTargetAdminDelay(target)).to.equal(oldDelay);
- await time.increaseBy.timestamp(setback);
- expect(await this.manager.getTargetAdminDelay(target)).to.equal(newDelay);
- });
- });
- });
- });
- });
- describe('not subject to a delay', function () {
- describe('#updateAuthority', function () {
- beforeEach('create a target and a new authority', async function () {
- this.newAuthority = await ethers.deployContract('$AccessManager', [this.admin]);
- this.newManagedTarget = await ethers.deployContract('$AccessManagedTarget', [this.manager]);
- });
- describe('restrictions', function () {
- beforeEach('set method and args', function () {
- this.calldata = this.manager.interface.encodeFunctionData('updateAuthority(address,address)', [
- this.newManagedTarget.target,
- this.newAuthority.target,
- ]);
- });
- shouldBehaveLikeNotDelayedAdminOperation();
- });
- it('changes the authority', async function () {
- expect(await this.newManagedTarget.authority()).to.be.equal(this.manager.target);
- await expect(this.manager.connect(this.admin).updateAuthority(this.newManagedTarget, this.newAuthority))
- .to.emit(this.newManagedTarget, 'AuthorityUpdated') // Managed contract is responsible of notifying the change through an event
- .withArgs(this.newAuthority.target);
- expect(await this.newManagedTarget.authority()).to.be.equal(this.newAuthority.target);
- });
- });
- describe('#setTargetClosed', function () {
- describe('restrictions', function () {
- beforeEach('set method and args', function () {
- const args = [someAddress, true];
- const method = this.manager.interface.getFunction('setTargetClosed(address,bool)');
- this.calldata = this.manager.interface.encodeFunctionData(method, args);
- });
- shouldBehaveLikeNotDelayedAdminOperation();
- });
- it('closes and opens a target', async function () {
- await expect(this.manager.connect(this.admin).setTargetClosed(this.target, true))
- .to.emit(this.manager, 'TargetClosed')
- .withArgs(this.target.target, true);
- expect(await this.manager.isTargetClosed(this.target)).to.be.true;
- await expect(this.manager.connect(this.admin).setTargetClosed(this.target, false))
- .to.emit(this.manager, 'TargetClosed')
- .withArgs(this.target.target, false);
- expect(await this.manager.isTargetClosed(this.target)).to.be.false;
- });
- it('reverts if closing the manager', async function () {
- await expect(this.manager.connect(this.admin).setTargetClosed(this.manager, true))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerLockedAccount')
- .withArgs(this.manager.target);
- });
- });
- describe('#setTargetFunctionRole', function () {
- describe('restrictions', function () {
- beforeEach('set method and args', function () {
- const args = [someAddress, ['0x12345678'], 443342];
- const method = this.manager.interface.getFunction('setTargetFunctionRole(address,bytes4[],uint64)');
- this.calldata = this.manager.interface.encodeFunctionData(method, args);
- });
- shouldBehaveLikeNotDelayedAdminOperation();
- });
- const sigs = ['someFunction()', 'someOtherFunction(uint256)', 'oneMoreFunction(address,uint8)'].map(selector);
- it('sets function roles', async function () {
- for (const sig of sigs) {
- expect(await this.manager.getTargetFunctionRole(this.target, sig)).to.equal(this.roles.ADMIN.id);
- }
- const allowRole = await this.manager
- .connect(this.admin)
- .setTargetFunctionRole(this.target, sigs, this.roles.SOME.id);
- for (const sig of sigs) {
- expect(allowRole)
- .to.emit(this.manager, 'TargetFunctionRoleUpdated')
- .withArgs(this.target.target, sig, this.roles.SOME.id);
- expect(await this.manager.getTargetFunctionRole(this.target, sig)).to.equal(this.roles.SOME.id);
- }
- await expect(
- this.manager.connect(this.admin).setTargetFunctionRole(this.target, [sigs[1]], this.roles.SOME_ADMIN.id),
- )
- .to.emit(this.manager, 'TargetFunctionRoleUpdated')
- .withArgs(this.target.target, sigs[1], this.roles.SOME_ADMIN.id);
- for (const sig of sigs) {
- expect(await this.manager.getTargetFunctionRole(this.target, sig)).to.equal(
- sig == sigs[1] ? this.roles.SOME_ADMIN.id : this.roles.SOME.id,
- );
- }
- });
- });
- describe('role admin operations', function () {
- const ANOTHER_ADMIN = 0xdeadc0de1n;
- const ANOTHER_ROLE = 0xdeadc0de2n;
- beforeEach('set required role', async function () {
- // Make admin a member of ANOTHER_ADMIN
- await this.manager.$_grantRole(ANOTHER_ADMIN, this.admin, 0, 0);
- await this.manager.$_setRoleAdmin(ANOTHER_ROLE, ANOTHER_ADMIN);
- this.role = { id: ANOTHER_ADMIN };
- await this.manager.$_grantRole(this.role.id, this.user, 0, 0);
- });
- describe('#grantRole', function () {
- describe('restrictions', function () {
- beforeEach('set method and args', function () {
- const args = [ANOTHER_ROLE, someAddress, 0];
- const method = this.manager.interface.getFunction('grantRole(uint64,address,uint32)');
- this.calldata = this.manager.interface.encodeFunctionData(method, args);
- });
- shouldBehaveLikeRoleAdminOperation(ANOTHER_ADMIN);
- });
- it('reverts when granting PUBLIC_ROLE', async function () {
- await expect(this.manager.connect(this.admin).grantRole(this.roles.PUBLIC.id, this.user, 0))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerLockedRole')
- .withArgs(this.roles.PUBLIC.id);
- });
- describe('when the user is not a role member', function () {
- describe('with grant delay', function () {
- beforeEach('set grant delay and grant role', async function () {
- // Delay granting
- this.grantDelay = time.duration.weeks(2);
- await this.manager.$_setGrantDelay(ANOTHER_ROLE, this.grantDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- // Grant role
- this.executionDelay = time.duration.days(3);
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- false,
- '0',
- ]);
- this.txResponse = await this.manager
- .connect(this.admin)
- .grantRole(ANOTHER_ROLE, this.user, this.executionDelay);
- this.delay = this.grantDelay; // For testAsDelay
- });
- testAsDelay('grant', {
- before: function self() {
- self.mineDelay = true;
- it('does not grant role to the user yet', async function () {
- const timestamp = await time.clockFromReceipt.timestamp(this.txResponse);
- expect(this.txResponse)
- .to.emit(this.manager, 'RoleGranted')
- .withArgs(ANOTHER_ROLE, this.user, timestamp + this.grantDelay, this.executionDelay, true);
- // Access is correctly stored
- const access = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- expect(access[0]).to.equal(timestamp + this.grantDelay); // inEffectSince
- expect(access[1]).to.equal(this.executionDelay); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Not in effect yet
- const currentTimestamp = await time.clock.timestamp();
- expect(currentTimestamp).to.be.lt(access[0]);
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- false,
- this.executionDelay.toString(),
- ]);
- });
- },
- after: function self() {
- self.mineDelay = true;
- it('grants role to the user', async function () {
- const timestamp = await time.clockFromReceipt.timestamp(this.txResponse);
- expect(this.txResponse)
- .to.emit(this.manager, 'RoleAccessRequested')
- .withArgs(ANOTHER_ROLE, this.user, timestamp + this.grantDelay, this.executionDelay, true);
- // Access is correctly stored
- const access = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- expect(access[0]).to.equal(timestamp + this.grantDelay); // inEffectSince
- expect(access[1]).to.equal(this.executionDelay); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Already in effect
- const currentTimestamp = await time.clock.timestamp();
- expect(currentTimestamp).to.be.equal(access[0]);
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.executionDelay.toString(),
- ]);
- });
- },
- });
- });
- describe('without grant delay', function () {
- beforeEach('set granting delay', async function () {
- // Delay granting
- this.grantDelay = 0;
- await this.manager.$_setGrantDelay(ANOTHER_ROLE, this.grantDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- });
- it('immediately grants the role to the user', async function () {
- const executionDelay = time.duration.days(6);
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- false,
- '0',
- ]);
- const txResponse = await this.manager
- .connect(this.admin)
- .grantRole(ANOTHER_ROLE, this.user, executionDelay);
- const grantedAt = await time.clockFromReceipt.timestamp(txResponse);
- expect(txResponse)
- .to.emit(this.manager, 'RoleGranted')
- .withArgs(ANOTHER_ROLE, this.user.address, executionDelay, grantedAt, true);
- // Access is correctly stored
- const access = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- expect(access[0]).to.equal(grantedAt); // inEffectSince
- expect(access[1]).to.equal(executionDelay); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Already in effect
- const currentTimestamp = await time.clock.timestamp();
- expect(currentTimestamp).to.be.equal(access[0]);
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- executionDelay.toString(),
- ]);
- });
- });
- });
- describe('when the user is already a role member', function () {
- beforeEach('make user role member', async function () {
- this.previousExecutionDelay = time.duration.days(6);
- await this.manager.$_grantRole(ANOTHER_ROLE, this.user, 0, this.previousExecutionDelay);
- this.oldAccess = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- });
- describe('with grant delay', function () {
- beforeEach('set granting delay', async function () {
- // Delay granting
- const grantDelay = time.duration.weeks(2);
- await this.manager.$_setGrantDelay(ANOTHER_ROLE, grantDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- });
- describe('when increasing the execution delay', function () {
- beforeEach('set increased new execution delay', async function () {
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.previousExecutionDelay.toString(),
- ]);
- this.newExecutionDelay = this.previousExecutionDelay + time.duration.days(4);
- });
- it('emits event and immediately changes the execution delay', async function () {
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.previousExecutionDelay.toString(),
- ]);
- const txResponse = await this.manager
- .connect(this.admin)
- .grantRole(ANOTHER_ROLE, this.user, this.newExecutionDelay);
- const timestamp = await time.clockFromReceipt.timestamp(txResponse);
- expect(txResponse)
- .to.emit(this.manager, 'RoleGranted')
- .withArgs(ANOTHER_ROLE, this.user.address, timestamp, this.newExecutionDelay, false);
- // Access is correctly stored
- const access = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- expect(access[0]).to.equal(this.oldAccess[0]); // inEffectSince
- expect(access[1]).to.equal(this.newExecutionDelay); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Already in effect
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.newExecutionDelay.toString(),
- ]);
- });
- });
- describe('when decreasing the execution delay', function () {
- beforeEach('decrease execution delay', async function () {
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.previousExecutionDelay.toString(),
- ]);
- this.newExecutionDelay = this.previousExecutionDelay - time.duration.days(4);
- this.txResponse = await this.manager
- .connect(this.admin)
- .grantRole(ANOTHER_ROLE, this.user, this.newExecutionDelay);
- this.grantTimestamp = await time.clockFromReceipt.timestamp(this.txResponse);
- this.delay = this.previousExecutionDelay - this.newExecutionDelay; // For testAsDelay
- });
- it('emits event', function () {
- expect(this.txResponse)
- .to.emit(this.manager, 'RoleGranted')
- .withArgs(
- ANOTHER_ROLE,
- this.user.address,
- this.grantTimestamp + this.delay,
- this.newExecutionDelay,
- false,
- );
- });
- testAsDelay('execution delay effect', {
- before: function self() {
- self.mineDelay = true;
- it('does not change the execution delay yet', async function () {
- // Access is correctly stored
- const access = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- expect(access[0]).to.equal(this.oldAccess[0]); // inEffectSince
- expect(access[1]).to.equal(this.previousExecutionDelay); // currentDelay
- expect(access[2]).to.equal(this.newExecutionDelay); // pendingDelay
- expect(access[3]).to.equal(this.grantTimestamp + this.delay); // pendingDelayEffect
- // Not in effect yet
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.previousExecutionDelay.toString(),
- ]);
- });
- },
- after: function self() {
- self.mineDelay = true;
- it('changes the execution delay', async function () {
- // Access is correctly stored
- const access = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- expect(access[0]).to.equal(this.oldAccess[0]); // inEffectSince
- expect(access[1]).to.equal(this.newExecutionDelay); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Already in effect
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.newExecutionDelay.toString(),
- ]);
- });
- },
- });
- });
- });
- describe('without grant delay', function () {
- beforeEach('set granting delay', async function () {
- // Delay granting
- const grantDelay = 0;
- await this.manager.$_setGrantDelay(ANOTHER_ROLE, grantDelay);
- await time.increaseBy.timestamp(MINSETBACK);
- });
- describe('when increasing the execution delay', function () {
- beforeEach('set increased new execution delay', async function () {
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.previousExecutionDelay.toString(),
- ]);
- this.newExecutionDelay = this.previousExecutionDelay + time.duration.days(4);
- });
- it('emits event and immediately changes the execution delay', async function () {
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.previousExecutionDelay.toString(),
- ]);
- const txResponse = await this.manager
- .connect(this.admin)
- .grantRole(ANOTHER_ROLE, this.user, this.newExecutionDelay);
- const timestamp = await time.clockFromReceipt.timestamp(txResponse);
- expect(txResponse)
- .to.emit(this.manager, 'RoleGranted')
- .withArgs(ANOTHER_ROLE, this.user.address, timestamp, this.newExecutionDelay, false);
- // Access is correctly stored
- const access = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- expect(access[0]).to.equal(this.oldAccess[0]); // inEffectSince
- expect(access[1]).to.equal(this.newExecutionDelay); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Already in effect
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.newExecutionDelay.toString(),
- ]);
- });
- });
- describe('when decreasing the execution delay', function () {
- beforeEach('decrease execution delay', async function () {
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.previousExecutionDelay.toString(),
- ]);
- this.newExecutionDelay = this.previousExecutionDelay - time.duration.days(4);
- this.txResponse = await this.manager
- .connect(this.admin)
- .grantRole(ANOTHER_ROLE, this.user, this.newExecutionDelay);
- this.grantTimestamp = await time.clockFromReceipt.timestamp(this.txResponse);
- this.delay = this.previousExecutionDelay - this.newExecutionDelay; // For testAsDelay
- });
- it('emits event', function () {
- expect(this.txResponse)
- .to.emit(this.manager, 'RoleGranted')
- .withArgs(
- ANOTHER_ROLE,
- this.user.address,
- this.grantTimestamp + this.delay,
- this.newExecutionDelay,
- false,
- );
- });
- testAsDelay('execution delay effect', {
- before: function self() {
- self.mineDelay = true;
- it('does not change the execution delay yet', async function () {
- // Access is correctly stored
- const access = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- expect(access[0]).to.equal(this.oldAccess[0]); // inEffectSince
- expect(access[1]).to.equal(this.previousExecutionDelay); // currentDelay
- expect(access[2]).to.equal(this.newExecutionDelay); // pendingDelay
- expect(access[3]).to.equal(this.grantTimestamp + this.delay); // pendingDelayEffect
- // Not in effect yet
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.previousExecutionDelay.toString(),
- ]);
- });
- },
- after: function self() {
- self.mineDelay = true;
- it('changes the execution delay', async function () {
- // Access is correctly stored
- const access = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- expect(access[0]).to.equal(this.oldAccess[0]); // inEffectSince
- expect(access[1]).to.equal(this.newExecutionDelay); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // pendingDelayEffect
- // Already in effect
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- this.newExecutionDelay.toString(),
- ]);
- });
- },
- });
- });
- });
- });
- });
- describe('#revokeRole', function () {
- describe('restrictions', function () {
- beforeEach('set method and args', async function () {
- const args = [ANOTHER_ROLE, someAddress];
- const method = this.manager.interface.getFunction('revokeRole(uint64,address)');
- this.calldata = this.manager.interface.encodeFunctionData(method, args);
- // Need to be set before revoking
- await this.manager.$_grantRole(...args, 0, 0);
- });
- shouldBehaveLikeRoleAdminOperation(ANOTHER_ADMIN);
- });
- describe('when role has been granted', function () {
- beforeEach('grant role with grant delay', async function () {
- this.grantDelay = time.duration.weeks(1);
- await this.manager.$_grantRole(ANOTHER_ROLE, this.user, this.grantDelay, 0);
- this.delay = this.grantDelay; // For testAsDelay
- });
- testAsDelay('grant', {
- before: function self() {
- self.mineDelay = true;
- it('revokes a granted role that will take effect in the future', async function () {
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- false,
- '0',
- ]);
- await expect(this.manager.connect(this.admin).revokeRole(ANOTHER_ROLE, this.user))
- .to.emit(this.manager, 'RoleRevoked')
- .withArgs(ANOTHER_ROLE, this.user.address);
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- false,
- '0',
- ]);
- const access = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- expect(access[0]).to.equal(0n); // inRoleSince
- expect(access[1]).to.equal(0n); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // effect
- });
- },
- after: function self() {
- self.mineDelay = true;
- it('revokes a granted role that already took effect', async function () {
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- true,
- '0',
- ]);
- await expect(this.manager.connect(this.admin).revokeRole(ANOTHER_ROLE, this.user))
- .to.emit(this.manager, 'RoleRevoked')
- .withArgs(ANOTHER_ROLE, this.user.address);
- expect(await this.manager.hasRole(ANOTHER_ROLE, this.user).then(formatAccess)).to.be.deep.equal([
- false,
- '0',
- ]);
- const access = await this.manager.getAccess(ANOTHER_ROLE, this.user);
- expect(access[0]).to.equal(0n); // inRoleSince
- expect(access[1]).to.equal(0n); // currentDelay
- expect(access[2]).to.equal(0n); // pendingDelay
- expect(access[3]).to.equal(0n); // effect
- });
- },
- });
- });
- describe('when role has not been granted', function () {
- it('has no effect', async function () {
- expect(await this.manager.hasRole(this.roles.SOME.id, this.user).then(formatAccess)).to.be.deep.equal([
- false,
- '0',
- ]);
- await expect(this.manager.connect(this.roleAdmin).revokeRole(this.roles.SOME.id, this.user)).to.not.emit(
- this.manager,
- 'RoleRevoked',
- );
- expect(await this.manager.hasRole(this.roles.SOME.id, this.user).then(formatAccess)).to.be.deep.equal([
- false,
- '0',
- ]);
- });
- });
- it('reverts revoking PUBLIC_ROLE', async function () {
- await expect(this.manager.connect(this.admin).revokeRole(this.roles.PUBLIC.id, this.user))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerLockedRole')
- .withArgs(this.roles.PUBLIC.id);
- });
- });
- });
- describe('self role operations', function () {
- describe('#renounceRole', function () {
- beforeEach('grant role', async function () {
- this.role = { id: 783164n };
- this.caller = this.user;
- await this.manager.$_grantRole(this.role.id, this.caller, 0, 0);
- });
- it('renounces a role', async function () {
- expect(await this.manager.hasRole(this.role.id, this.caller).then(formatAccess)).to.be.deep.equal([
- true,
- '0',
- ]);
- await expect(this.manager.connect(this.caller).renounceRole(this.role.id, this.caller))
- .to.emit(this.manager, 'RoleRevoked')
- .withArgs(this.role.id, this.caller.address);
- expect(await this.manager.hasRole(this.role.id, this.caller).then(formatAccess)).to.be.deep.equal([
- false,
- '0',
- ]);
- });
- it('reverts if renouncing the PUBLIC_ROLE', async function () {
- await expect(this.manager.connect(this.caller).renounceRole(this.roles.PUBLIC.id, this.caller))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerLockedRole')
- .withArgs(this.roles.PUBLIC.id);
- });
- it('reverts if renouncing with bad caller confirmation', async function () {
- await expect(
- this.manager.connect(this.caller).renounceRole(this.role.id, someAddress),
- ).to.be.revertedWithCustomError(this.manager, 'AccessManagerBadConfirmation');
- });
- });
- });
- });
- });
- describe('access managed target operations', function () {
- describe('when calling a restricted target function', function () {
- beforeEach('set required role', function () {
- this.method = this.target.fnRestricted.getFragment();
- this.role = { id: 3597243n };
- this.manager.$_setTargetFunctionRole(this.target, this.method.selector, this.role.id);
- });
- describe('restrictions', function () {
- beforeEach('set method and args', function () {
- this.calldata = this.target.interface.encodeFunctionData(this.method, []);
- this.caller = this.user;
- });
- shouldBehaveLikeAManagedRestrictedOperation();
- });
- it('succeeds called by a role member', async function () {
- await this.manager.$_grantRole(this.role.id, this.user, 0, 0);
- await expect(
- this.target.connect(this.user)[this.method.selector]({
- data: this.calldata,
- }),
- )
- .to.emit(this.target, 'CalledRestricted')
- .withArgs(this.user.address);
- });
- });
- describe('when calling a non-restricted target function', function () {
- const method = 'fnUnrestricted()';
- beforeEach('set required role', async function () {
- this.role = { id: 879435n };
- await this.manager.$_setTargetFunctionRole(
- this.target,
- this.target[method].getFragment().selector,
- this.role.id,
- );
- });
- it('succeeds called by anyone', async function () {
- await expect(
- this.target.connect(this.user)[method]({
- data: this.calldata,
- }),
- )
- .to.emit(this.target, 'CalledUnrestricted')
- .withArgs(this.user.address);
- });
- });
- });
- describe('#schedule', function () {
- beforeEach('set target function role', async function () {
- this.method = this.target.fnRestricted.getFragment();
- this.role = { id: 498305n };
- this.caller = this.user;
- await this.manager.$_setTargetFunctionRole(this.target, this.method.selector, this.role.id);
- await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // nonzero execution delay
- this.calldata = this.target.interface.encodeFunctionData(this.method, []);
- this.delay = time.duration.weeks(2);
- });
- describe('restrictions', function () {
- testAsCanCall({
- closed() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- const { schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.delay,
- });
- await expect(schedule())
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- open: {
- callerIsTheManager: {
- executing() {
- it.skip('is not reachable because schedule is not restrictable');
- },
- notExecuting() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- const { schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.delay,
- });
- await expect(schedule())
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- },
- callerIsNotTheManager: {
- publicRoleIsRequired() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- // prepareOperation is not used here because it alters the next block timestamp
- await expect(this.manager.connect(this.caller).schedule(this.target, this.calldata, MAX_UINT48))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- specificRoleIsRequired: {
- requiredRoleIsGranted: {
- roleGrantingIsDelayed: {
- callerHasAnExecutionDelay: {
- beforeGrantDelay() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- // prepareOperation is not used here because it alters the next block timestamp
- await expect(this.manager.connect(this.caller).schedule(this.target, this.calldata, MAX_UINT48))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- afterGrantDelay() {
- it('succeeds', async function () {
- // prepareOperation is not used here because it alters the next block timestamp
- await this.manager.connect(this.caller).schedule(this.target, this.calldata, MAX_UINT48);
- });
- },
- },
- callerHasNoExecutionDelay: {
- beforeGrantDelay() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- // prepareOperation is not used here because it alters the next block timestamp
- await expect(this.manager.connect(this.caller).schedule(this.target, this.calldata, MAX_UINT48))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- afterGrantDelay() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- // prepareOperation is not used here because it alters the next block timestamp
- await expect(this.manager.connect(this.caller).schedule(this.target, this.calldata, MAX_UINT48))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- },
- },
- roleGrantingIsNotDelayed: {
- callerHasAnExecutionDelay() {
- it('succeeds', async function () {
- const { schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.delay,
- });
- await schedule();
- });
- },
- callerHasNoExecutionDelay() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- // prepareOperation is not used here because it alters the next block timestamp
- await expect(this.manager.connect(this.caller).schedule(this.target, this.calldata, MAX_UINT48))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- },
- },
- requiredRoleIsNotGranted() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- const { schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.delay,
- });
- await expect(schedule())
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- },
- },
- },
- });
- });
- it('schedules an operation at the specified execution date if it is larger than caller execution delay', async function () {
- const { operationId, scheduledAt, schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.delay,
- });
- const txResponse = await schedule();
- expect(await this.manager.getSchedule(operationId)).to.equal(scheduledAt + this.delay);
- expect(txResponse)
- .to.emit(this.manager, 'OperationScheduled')
- .withArgs(operationId, '1', scheduledAt + this.delay, this.target.target, this.calldata);
- });
- it('schedules an operation at the minimum execution date if no specified execution date (when == 0)', async function () {
- const executionDelay = await time.duration.hours(72);
- await this.manager.$_grantRole(this.role.id, this.caller, 0, executionDelay);
- const txResponse = await this.manager.connect(this.caller).schedule(this.target, this.calldata, 0);
- const scheduledAt = await time.clockFromReceipt.timestamp(txResponse);
- const operationId = await this.manager.hashOperation(this.caller, this.target, this.calldata);
- expect(await this.manager.getSchedule(operationId)).to.equal(scheduledAt + executionDelay);
- expect(txResponse)
- .to.emit(this.manager, 'OperationScheduled')
- .withArgs(operationId, '1', scheduledAt + executionDelay, this.target.target, this.calldata);
- });
- it('increases the nonce of an operation scheduled more than once', async function () {
- // Setup and check initial nonce
- const expectedOperationId = hashOperation(this.caller, this.target, this.calldata);
- expect(await this.manager.getNonce(expectedOperationId)).to.equal('0');
- // Schedule
- const op1 = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.delay,
- });
- await expect(op1.schedule())
- .to.emit(this.manager, 'OperationScheduled')
- .withArgs(
- op1.operationId,
- 1n,
- op1.scheduledAt + this.delay,
- this.caller.address,
- this.target.target,
- this.calldata,
- );
- expect(expectedOperationId).to.equal(op1.operationId);
- // Consume
- await time.increaseBy.timestamp(this.delay);
- await this.manager.$_consumeScheduledOp(expectedOperationId);
- // Check nonce
- expect(await this.manager.getNonce(expectedOperationId)).to.equal('1');
- // Schedule again
- const op2 = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.delay,
- });
- await expect(op2.schedule())
- .to.emit(this.manager, 'OperationScheduled')
- .withArgs(
- op2.operationId,
- 2n,
- op2.scheduledAt + this.delay,
- this.caller.address,
- this.target.target,
- this.calldata,
- );
- expect(expectedOperationId).to.equal(op2.operationId);
- // Check final nonce
- expect(await this.manager.getNonce(expectedOperationId)).to.equal('2');
- });
- it('reverts if the specified execution date is before the current timestamp + caller execution delay', async function () {
- const executionDelay = time.duration.weeks(1) + this.delay;
- await this.manager.$_grantRole(this.role.id, this.caller, 0, executionDelay);
- const { schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.delay,
- });
- await expect(schedule())
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- it('reverts if an operation is already schedule', async function () {
- const op1 = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.delay,
- });
- await op1.schedule();
- const op2 = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.delay,
- });
- await expect(op2.schedule())
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerAlreadyScheduled')
- .withArgs(op1.operationId);
- });
- it('panics scheduling calldata with less than 4 bytes', async function () {
- const calldata = '0x1234'; // 2 bytes
- // Managed contract
- const op1 = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: calldata,
- delay: this.delay,
- });
- await expect(op1.schedule()).to.be.revertedWithoutReason();
- // Manager contract
- const op2 = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.manager,
- calldata: calldata,
- delay: this.delay,
- });
- await expect(op2.schedule()).to.be.revertedWithoutReason();
- });
- it('reverts scheduling an unknown operation to the manager', async function () {
- const calldata = '0x12345678';
- const { schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.manager,
- calldata,
- delay: this.delay,
- });
- await expect(schedule())
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.manager.target, calldata);
- });
- });
- describe('#execute', function () {
- beforeEach('set target function role', async function () {
- this.method = this.target.fnRestricted.getFragment();
- this.role = { id: 9825430n };
- this.caller = this.user;
- await this.manager.$_setTargetFunctionRole(this.target, this.method.selector, this.role.id);
- await this.manager.$_grantRole(this.role.id, this.caller, 0, 0);
- this.calldata = this.target.interface.encodeFunctionData(this.method, []);
- });
- describe('restrictions', function () {
- testAsCanCall({
- closed() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- await expect(this.manager.connect(this.caller).execute(this.target, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- open: {
- callerIsTheManager: {
- executing() {
- it('succeeds', async function () {
- await this.manager.connect(this.caller).execute(this.target, this.calldata);
- });
- },
- notExecuting() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- await expect(this.manager.connect(this.caller).execute(this.target, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- },
- callerIsNotTheManager: {
- publicRoleIsRequired() {
- it('succeeds', async function () {
- await this.manager.connect(this.caller).execute(this.target, this.calldata);
- });
- },
- specificRoleIsRequired: {
- requiredRoleIsGranted: {
- roleGrantingIsDelayed: {
- callerHasAnExecutionDelay: {
- beforeGrantDelay() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- await expect(this.manager.connect(this.caller).execute(this.target, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- afterGrantDelay: function self() {
- self.mineDelay = true;
- beforeEach('define schedule delay', function () {
- this.scheduleIn = time.duration.days(21); // For testAsSchedulableOperation
- });
- testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
- },
- },
- callerHasNoExecutionDelay: {
- beforeGrantDelay() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- await expect(this.manager.connect(this.caller).execute(this.target, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- afterGrantDelay: function self() {
- self.mineDelay = true;
- it('succeeds', async function () {
- await this.manager.connect(this.caller).execute(this.target, this.calldata);
- });
- },
- },
- },
- roleGrantingIsNotDelayed: {
- callerHasAnExecutionDelay() {
- beforeEach('define schedule delay', function () {
- this.scheduleIn = time.duration.days(15); // For testAsSchedulableOperation
- });
- testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
- },
- callerHasNoExecutionDelay() {
- it('succeeds', async function () {
- await this.manager.connect(this.caller).execute(this.target, this.calldata);
- });
- },
- },
- },
- requiredRoleIsNotGranted() {
- it('reverts as AccessManagerUnauthorizedCall', async function () {
- await expect(this.manager.connect(this.caller).execute(this.target, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.caller.address, this.target.target, this.calldata.substring(0, 10));
- });
- },
- },
- },
- },
- });
- });
- it('executes with a delay consuming the scheduled operation', async function () {
- const delay = time.duration.hours(4);
- await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // Execution delay is needed so the operation is consumed
- const { operationId, schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay,
- });
- await schedule();
- await time.increaseBy.timestamp(delay);
- await expect(this.manager.connect(this.caller).execute(this.target, this.calldata))
- .to.emit(this.manager, 'OperationExecuted')
- .withArgs(operationId, 1n);
- expect(await this.manager.getSchedule(operationId)).to.equal(0n);
- });
- it('executes with no delay consuming a scheduled operation', async function () {
- const delay = time.duration.hours(4);
- // give caller an execution delay
- await this.manager.$_grantRole(this.role.id, this.caller, 0, 1);
- const { operationId, schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay,
- });
- await schedule();
- // remove the execution delay
- await this.manager.$_grantRole(this.role.id, this.caller, 0, 0);
- await time.increaseBy.timestamp(delay);
- await expect(this.manager.connect(this.caller).execute(this.target, this.calldata))
- .to.emit(this.manager, 'OperationExecuted')
- .withArgs(operationId, 1n);
- expect(await this.manager.getSchedule(operationId)).to.equal(0n);
- });
- it('keeps the original _executionId after finishing the call', async function () {
- const executionIdBefore = await getStorageAt(this.manager.target, EXECUTION_ID_STORAGE_SLOT);
- await this.manager.connect(this.caller).execute(this.target, this.calldata);
- const executionIdAfter = await getStorageAt(this.manager.target, EXECUTION_ID_STORAGE_SLOT);
- expect(executionIdBefore).to.equal(executionIdAfter);
- });
- it('reverts executing twice', async function () {
- const delay = time.duration.hours(2);
- await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // Execution delay is needed so the operation is consumed
- const { operationId, schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay,
- });
- await schedule();
- await time.increaseBy.timestamp(delay);
- await this.manager.connect(this.caller).execute(this.target, this.calldata);
- await expect(this.manager.connect(this.caller).execute(this.target, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerNotScheduled')
- .withArgs(operationId);
- });
- });
- describe('#consumeScheduledOp', function () {
- beforeEach('define scheduling parameters', async function () {
- const method = this.target.fnRestricted.getFragment();
- this.caller = await ethers.getSigner(this.target.target);
- await impersonate(this.caller.address);
- this.calldata = this.target.interface.encodeFunctionData(method, []);
- this.role = { id: 9834983n };
- await this.manager.$_setTargetFunctionRole(this.target, method.selector, this.role.id);
- await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // nonzero execution delay
- this.scheduleIn = time.duration.hours(10); // For testAsSchedulableOperation
- });
- describe('when caller is not consuming scheduled operation', function () {
- beforeEach('set consuming false', async function () {
- await this.target.setIsConsumingScheduledOp(false, ethers.toBeHex(CONSUMING_SCHEDULE_STORAGE_SLOT, 32));
- });
- it('reverts as AccessManagerUnauthorizedConsume', async function () {
- await expect(this.manager.connect(this.caller).consumeScheduledOp(this.caller, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedConsume')
- .withArgs(this.caller.address);
- });
- });
- describe('when caller is consuming scheduled operation', function () {
- beforeEach('set consuming true', async function () {
- await this.target.setIsConsumingScheduledOp(true, ethers.toBeHex(CONSUMING_SCHEDULE_STORAGE_SLOT, 32));
- });
- testAsSchedulableOperation({
- scheduled: {
- before() {
- it('reverts as AccessManagerNotReady', async function () {
- await expect(this.manager.connect(this.caller).consumeScheduledOp(this.caller, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerNotReady')
- .withArgs(this.operationId);
- });
- },
- after() {
- it('consumes the scheduled operation and resets timepoint', async function () {
- expect(await this.manager.getSchedule(this.operationId)).to.equal(this.scheduledAt + this.scheduleIn);
- await expect(this.manager.connect(this.caller).consumeScheduledOp(this.caller, this.calldata))
- .to.emit(this.manager, 'OperationExecuted')
- .withArgs(this.operationId, 1n);
- expect(await this.manager.getSchedule(this.operationId)).to.equal(0n);
- });
- },
- expired() {
- it('reverts as AccessManagerExpired', async function () {
- await expect(this.manager.connect(this.caller).consumeScheduledOp(this.caller, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerExpired')
- .withArgs(this.operationId);
- });
- },
- },
- notScheduled() {
- it('reverts as AccessManagerNotScheduled', async function () {
- await expect(this.manager.connect(this.caller).consumeScheduledOp(this.caller, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerNotScheduled')
- .withArgs(this.operationId);
- });
- },
- });
- });
- });
- describe('#cancelScheduledOp', function () {
- beforeEach('setup scheduling', async function () {
- this.method = this.target.fnRestricted.getFragment();
- this.caller = this.roles.SOME.members[0];
- await this.manager.$_setTargetFunctionRole(this.target, this.method.selector, this.roles.SOME.id);
- await this.manager.$_grantRole(this.roles.SOME.id, this.caller, 0, 1); // nonzero execution delay
- this.calldata = this.target.interface.encodeFunctionData(this.method, []);
- this.scheduleIn = time.duration.days(10); // For testAsSchedulableOperation
- });
- testAsSchedulableOperation({
- scheduled: {
- before() {
- describe('when caller is the scheduler', function () {
- it('succeeds', async function () {
- await this.manager.connect(this.caller).cancel(this.caller, this.target, this.calldata);
- });
- });
- describe('when caller is an admin', function () {
- it('succeeds', async function () {
- await this.manager.connect(this.roles.ADMIN.members[0]).cancel(this.caller, this.target, this.calldata);
- });
- });
- describe('when caller is the role guardian', function () {
- it('succeeds', async function () {
- await this.manager
- .connect(this.roles.SOME_GUARDIAN.members[0])
- .cancel(this.caller, this.target, this.calldata);
- });
- });
- describe('when caller is any other account', function () {
- it('reverts as AccessManagerUnauthorizedCancel', async function () {
- await expect(this.manager.connect(this.other).cancel(this.caller, this.target, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCancel')
- .withArgs(this.other.address, this.caller.address, this.target.target, this.method.selector);
- });
- });
- },
- after() {
- it('succeeds', async function () {
- await this.manager.connect(this.caller).cancel(this.caller, this.target, this.calldata);
- });
- },
- expired() {
- it('succeeds', async function () {
- await this.manager.connect(this.caller).cancel(this.caller, this.target, this.calldata);
- });
- },
- },
- notScheduled() {
- it('reverts as AccessManagerNotScheduled', async function () {
- await expect(this.manager.cancel(this.caller, this.target, this.calldata))
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerNotScheduled')
- .withArgs(this.operationId);
- });
- },
- });
- it('cancels an operation and resets schedule', async function () {
- const { operationId, schedule } = await prepareOperation(this.manager, {
- caller: this.caller,
- target: this.target,
- calldata: this.calldata,
- delay: this.scheduleIn,
- });
- await schedule();
- await expect(this.manager.connect(this.caller).cancel(this.caller, this.target, this.calldata))
- .to.emit(this.manager, 'OperationCanceled')
- .withArgs(operationId, 1n);
- expect(await this.manager.getSchedule(operationId)).to.equal('0');
- });
- });
- describe('with Ownable target contract', function () {
- const roleId = 1n;
- beforeEach(async function () {
- this.ownable = await ethers.deployContract('$Ownable', [this.manager]);
- // add user to role
- await this.manager.$_grantRole(roleId, this.user, 0, 0);
- });
- it('initial state', async function () {
- expect(await this.ownable.owner()).to.be.equal(this.manager.target);
- });
- describe('Contract is closed', function () {
- beforeEach(async function () {
- await this.manager.$_setTargetClosed(this.ownable, true);
- });
- it('directly call: reverts', async function () {
- await expect(this.ownable.connect(this.user).$_checkOwner())
- .to.be.revertedWithCustomError(this.ownable, 'OwnableUnauthorizedAccount')
- .withArgs(this.user.address);
- });
- it('relayed call (with role): reverts', async function () {
- await expect(
- this.manager.connect(this.user).execute(this.ownable, this.ownable.$_checkOwner.getFragment().selector),
- )
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.user.address, this.ownable.target, this.ownable.$_checkOwner.getFragment().selector);
- });
- it('relayed call (without role): reverts', async function () {
- await expect(
- this.manager.connect(this.other).execute(this.ownable, this.ownable.$_checkOwner.getFragment().selector),
- )
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.other.address, this.ownable.target, this.ownable.$_checkOwner.getFragment().selector);
- });
- });
- describe('Contract is managed', function () {
- describe('function is open to specific role', function () {
- beforeEach(async function () {
- await this.manager.$_setTargetFunctionRole(
- this.ownable,
- this.ownable.$_checkOwner.getFragment().selector,
- roleId,
- );
- });
- it('directly call: reverts', async function () {
- await expect(this.ownable.connect(this.user).$_checkOwner())
- .to.be.revertedWithCustomError(this.ownable, 'OwnableUnauthorizedAccount')
- .withArgs(this.user.address);
- });
- it('relayed call (with role): success', async function () {
- await this.manager.connect(this.user).execute(this.ownable, this.ownable.$_checkOwner.getFragment().selector);
- });
- it('relayed call (without role): reverts', async function () {
- await expect(
- this.manager.connect(this.other).execute(this.ownable, this.ownable.$_checkOwner.getFragment().selector),
- )
- .to.be.revertedWithCustomError(this.manager, 'AccessManagerUnauthorizedCall')
- .withArgs(this.other.address, this.ownable.target, this.ownable.$_checkOwner.getFragment().selector);
- });
- });
- describe('function is open to public role', function () {
- beforeEach(async function () {
- await this.manager.$_setTargetFunctionRole(
- this.ownable,
- this.ownable.$_checkOwner.getFragment().selector,
- this.roles.PUBLIC.id,
- );
- });
- it('directly call: reverts', async function () {
- await expect(this.ownable.connect(this.user).$_checkOwner())
- .to.be.revertedWithCustomError(this.ownable, 'OwnableUnauthorizedAccount')
- .withArgs(this.user.address);
- });
- it('relayed call (with role): success', async function () {
- await this.manager.connect(this.user).execute(this.ownable, this.ownable.$_checkOwner.getFragment().selector);
- });
- it('relayed call (without role): success', async function () {
- await this.manager
- .connect(this.other)
- .execute(this.ownable, this.ownable.$_checkOwner.getFragment().selector);
- });
- });
- });
- });
- });
|