123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- const { mine } = require('@nomicfoundation/hardhat-network-helpers');
- const { expectRevertCustomError } = require('../../helpers/customError');
- const {
- LIKE_COMMON_IS_EXECUTING,
- LIKE_COMMON_GET_ACCESS,
- LIKE_COMMON_SCHEDULABLE,
- testAsSchedulableOperation,
- testAsRestrictedOperation,
- testAsDelayedOperation,
- testAsCanCall,
- testAsHasRole,
- } = require('./AccessManager.predicate');
- // ============ ADMIN OPERATION ============
- /**
- * @requires this.{manager,roles,calldata,role}
- */
- function shouldBehaveLikeDelayedAdminOperation() {
- const getAccessPath = LIKE_COMMON_GET_ACCESS;
- getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasAnExecutionDelay.afterGrantDelay = function () {
- beforeEach('consume previously set grant delay', async function () {
- // Consume previously set delay
- await mine();
- });
- testAsDelayedOperation();
- };
- getAccessPath.requiredRoleIsGranted.roleGrantingIsNotDelayed.callerHasAnExecutionDelay = function () {
- beforeEach('set execution delay', async function () {
- this.scheduleIn = this.executionDelay; // For testAsDelayedOperation
- });
- testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
- };
- beforeEach('set target as manager', function () {
- this.target = this.manager;
- });
- testAsRestrictedOperation({
- callerIsTheManager: LIKE_COMMON_IS_EXECUTING,
- callerIsNotTheManager() {
- testAsHasRole({
- publicRoleIsRequired() {
- it('reverts as AccessManagerUnauthorizedAccount', async function () {
- await expectRevertCustomError(
- web3.eth.sendTransaction({ to: this.target.address, data: this.calldata, from: this.caller }),
- 'AccessManagerUnauthorizedAccount',
- [
- this.caller,
- this.roles.ADMIN.id, // Although PUBLIC is required, target function role doesn't apply to admin ops
- ],
- );
- });
- },
- specificRoleIsRequired: getAccessPath,
- });
- },
- });
- }
- /**
- * @requires this.{manager,roles,calldata,role}
- */
- function shouldBehaveLikeNotDelayedAdminOperation() {
- const getAccessPath = LIKE_COMMON_GET_ACCESS;
- getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasAnExecutionDelay.afterGrantDelay = function () {
- beforeEach('set execution delay', async function () {
- await mine();
- this.scheduleIn = this.executionDelay; // For testAsSchedulableOperation
- });
- testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
- };
- getAccessPath.requiredRoleIsGranted.roleGrantingIsNotDelayed.callerHasAnExecutionDelay = function () {
- beforeEach('set execution delay', async function () {
- this.scheduleIn = this.executionDelay; // For testAsSchedulableOperation
- });
- testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
- };
- beforeEach('set target as manager', function () {
- this.target = this.manager;
- });
- testAsRestrictedOperation({
- callerIsTheManager: LIKE_COMMON_IS_EXECUTING,
- callerIsNotTheManager() {
- testAsHasRole({
- publicRoleIsRequired() {
- it('reverts as AccessManagerUnauthorizedAccount', async function () {
- await expectRevertCustomError(
- web3.eth.sendTransaction({ to: this.target.address, data: this.calldata, from: this.caller }),
- 'AccessManagerUnauthorizedAccount',
- [this.caller, this.roles.ADMIN.id], // Although PUBLIC_ROLE is required, admin ops are not subject to target function roles
- );
- });
- },
- specificRoleIsRequired: getAccessPath,
- });
- },
- });
- }
- /**
- * @requires this.{manager,roles,calldata,role}
- */
- function shouldBehaveLikeRoleAdminOperation(roleAdmin) {
- const getAccessPath = LIKE_COMMON_GET_ACCESS;
- getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasAnExecutionDelay.afterGrantDelay = function () {
- beforeEach('set operation delay', async function () {
- await mine();
- this.scheduleIn = this.executionDelay; // For testAsSchedulableOperation
- });
- testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
- };
- getAccessPath.requiredRoleIsGranted.roleGrantingIsNotDelayed.callerHasAnExecutionDelay = function () {
- beforeEach('set execution delay', async function () {
- this.scheduleIn = this.executionDelay; // For testAsSchedulableOperation
- });
- testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
- };
- beforeEach('set target as manager', function () {
- this.target = this.manager;
- });
- testAsRestrictedOperation({
- callerIsTheManager: LIKE_COMMON_IS_EXECUTING,
- callerIsNotTheManager() {
- testAsHasRole({
- publicRoleIsRequired() {
- it('reverts as AccessManagerUnauthorizedAccount', async function () {
- await expectRevertCustomError(
- web3.eth.sendTransaction({ to: this.target.address, data: this.calldata, from: this.caller }),
- 'AccessManagerUnauthorizedAccount',
- [this.caller, roleAdmin], // Role admin ops require the role's admin
- );
- });
- },
- specificRoleIsRequired: getAccessPath,
- });
- },
- });
- }
- // ============ RESTRICTED OPERATION ============
- /**
- * @requires this.{manager,roles,calldata,role}
- */
- function shouldBehaveLikeAManagedRestrictedOperation() {
- function revertUnauthorized() {
- it('reverts as AccessManagedUnauthorized', async function () {
- await expectRevertCustomError(
- web3.eth.sendTransaction({ to: this.target.address, data: this.calldata, from: this.caller }),
- 'AccessManagedUnauthorized',
- [this.caller],
- );
- });
- }
- const getAccessPath = LIKE_COMMON_GET_ACCESS;
- getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasAnExecutionDelay.beforeGrantDelay =
- revertUnauthorized;
- getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasNoExecutionDelay.beforeGrantDelay =
- revertUnauthorized;
- getAccessPath.requiredRoleIsNotGranted = revertUnauthorized;
- getAccessPath.requiredRoleIsGranted.roleGrantingIsDelayed.callerHasAnExecutionDelay.afterGrantDelay = function () {
- beforeEach('consume previously set grant delay', async function () {
- // Consume previously set delay
- await mine();
- this.scheduleIn = this.executionDelay; // For testAsSchedulableOperation
- });
- testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
- };
- getAccessPath.requiredRoleIsGranted.roleGrantingIsNotDelayed.callerHasAnExecutionDelay = function () {
- beforeEach('consume previously set grant delay', async function () {
- this.scheduleIn = this.executionDelay; // For testAsSchedulableOperation
- });
- testAsSchedulableOperation(LIKE_COMMON_SCHEDULABLE);
- };
- const isExecutingPath = LIKE_COMMON_IS_EXECUTING;
- isExecutingPath.notExecuting = revertUnauthorized;
- testAsCanCall({
- closed: revertUnauthorized,
- open: {
- callerIsTheManager: isExecutingPath,
- callerIsNotTheManager: {
- publicRoleIsRequired() {
- it('succeeds called directly', async function () {
- await web3.eth.sendTransaction({ to: this.target.address, data: this.calldata, from: this.caller });
- });
- it('succeeds via execute', async function () {
- await this.manager.execute(this.target.address, this.calldata, { from: this.caller });
- });
- },
- specificRoleIsRequired: getAccessPath,
- },
- },
- });
- }
- module.exports = {
- shouldBehaveLikeDelayedAdminOperation,
- shouldBehaveLikeNotDelayedAdminOperation,
- shouldBehaveLikeRoleAdminOperation,
- shouldBehaveLikeAManagedRestrictedOperation,
- };
|