Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations #31

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ enableGlobalCache: false

nodeLinker: node-modules

nmHoistingLimits: workspaces

yarnPath: .yarn/releases/yarn-4.6.0.cjs
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
"private": true,
"packageManager": "yarn@4.6.0",
"workspaces": [
"packages/*",
"packages/contracts/contracts"
"packages/*"
],
"installConfig": {
"hoistingLimits": "workspaces"
},
"devDependencies": {
"@commitlint/cli": "^19.7.1",
"@commitlint/config-conventional": "^19.7.1",
Expand Down Expand Up @@ -57,6 +59,5 @@
"remove:stable-version-field": "ts-node scripts/remove-stable-version-field.ts && yarn format:write",
"precommit": "lint-staged",
"postinstall": "husky && git config --local core.editor cat"
},
"version": ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ abstract contract AdvancedChecker is Clone, IAdvancedChecker {
/// @notice Validates a subject's evidence for a specific check phase.
/// @dev External entry point for validation checks, delegating logic to `_check`.
/// @param subject The address to validate.
/// @param evidence An array of custom validation data.
/// @param evidence Custom validation data.
/// @param checkType The phase of validation to execute (PRE, MAIN, POST).
/// @return checked Boolean indicating whether the validation passed.
function check(
address subject,
bytes[] calldata evidence,
bytes calldata evidence,
Check checkType
) external view override returns (bool checked) {
return _check(subject, evidence, checkType);
Expand All @@ -26,10 +26,10 @@ abstract contract AdvancedChecker is Clone, IAdvancedChecker {
/// @notice Core validation logic dispatcher.
/// @dev Routes validation calls to specific phase methods (_checkPre, _checkMain, _checkPost).
/// @param subject The address to validate.
/// @param evidence An array of custom validation data.
/// @param evidence Custom validation data.
/// @param checkType The phase of validation to execute.
/// @return checked Boolean indicating whether the validation passed.
function _check(address subject, bytes[] calldata evidence, Check checkType) internal view returns (bool checked) {
function _check(address subject, bytes calldata evidence, Check checkType) internal view returns (bool checked) {
if (checkType == Check.PRE) {
return _checkPre(subject, evidence);
}
Expand All @@ -44,21 +44,21 @@ abstract contract AdvancedChecker is Clone, IAdvancedChecker {
/// @notice Pre-condition validation logic.
/// @dev Derived contracts should override this to implement pre-check validation.
/// @param subject The address to validate.
/// @param evidence An array of custom validation data.
/// @param evidence Custom validation data.
/// @return checked Boolean indicating whether the validation passed.
function _checkPre(address subject, bytes[] calldata evidence) internal view virtual returns (bool checked) {}
function _checkPre(address subject, bytes calldata evidence) internal view virtual returns (bool checked) {}

/// @notice Main validation logic.
/// @dev Derived contracts should override this to implement main check validation.
/// @param subject The address to validate.
/// @param evidence An array of custom validation data.
/// @param evidence Custom validation data.
/// @return checked Boolean indicating whether the validation passed.
function _checkMain(address subject, bytes[] calldata evidence) internal view virtual returns (bool checked) {}
function _checkMain(address subject, bytes calldata evidence) internal view virtual returns (bool checked) {}

/// @notice Post-condition validation logic.
/// @dev Derived contracts should override this to implement post-check validation.
/// @param subject The address to validate.
/// @param evidence An array of custom validation data.
/// @param evidence Custom validation data.
/// @return checked Boolean indicating whether the validation passed.
function _checkPost(address subject, bytes[] calldata evidence) internal view virtual returns (bool checked) {}
function _checkPost(address subject, bytes calldata evidence) internal view virtual returns (bool checked) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ abstract contract BaseChecker is Clone, IBaseChecker {
/// @dev External view function that calls the `_check` method, allowing derived contracts
/// to implement custom validation logic.
/// @param subject The address to validate.
/// @param evidence An array of custom validation data.
/// @param evidence Custom validation data.
/// @return checked Boolean indicating whether the validation passed.
function check(address subject, bytes[] calldata evidence) external view override returns (bool checked) {
function check(address subject, bytes calldata evidence) external view override returns (bool checked) {
return _check(subject, evidence);
}

/// @notice Internal validation logic implementation.
/// @dev Must be overridden by derived contracts to define custom validation rules.
/// @param subject The address to validate.
/// @param evidence An array of custom validation data.
/// @param evidence Custom validation data.
/// @return checked Boolean indicating whether the validation passed.
function _check(address subject, bytes[] calldata evidence) internal view virtual returns (bool checked) {}
function _check(address subject, bytes calldata evidence) internal view virtual returns (bool checked) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ interface IAdvancedChecker {
/// @notice Validates a subject for a specific check phase.
/// @dev Implementations should route to appropriate phase-specific logic.
/// @param subject The address to validate.
/// @param evidence An array of custom validation data.
/// @param evidence Custom validation data.
/// @param checkType The phase of validation to execute (PRE, MAIN, POST).
/// @return checked Boolean indicating whether the validation passed.
function check(address subject, bytes[] calldata evidence, Check checkType) external view returns (bool checked);
function check(address subject, bytes calldata evidence, Check checkType) external view returns (bool checked);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ interface IAdvancedPolicy is IPolicy {
/// @notice Emitted when a subject successfully passes a validation check.
/// @param subject Address that passed the validation.
/// @param target Address of the protected contract.
/// @param evidence Data used during validation.
/// @param evidence Custom validation data.
/// @param checkType The type of check performed (PRE, MAIN, POST).
event Enforced(address indexed subject, address indexed target, bytes[] evidence, Check checkType);
event Enforced(address indexed subject, address indexed target, bytes evidence, Check checkType);

/// @notice Enforces a specific phase of the policy check on a given subject.
/// @dev Delegates validation logic to the corresponding phase's check method.
/// @param subject Address to validate.
/// @param evidence Data required for validation.
/// @param evidence Custom validation data.
/// @param checkType The type of check performed (PRE, MAIN, POST).
function enforce(address subject, bytes[] calldata evidence, Check checkType) external;
function enforce(address subject, bytes calldata evidence, Check checkType) external;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pragma solidity ^0.8.20;
interface IBaseChecker {
/// @notice Validates a subject against provided evidence.
/// @param subject The address to validate.
/// @param evidence An array of custom validation data.
/// @param evidence Custom validation data.
/// @return checked Boolean indicating whether the validation passed.
function check(address subject, bytes[] calldata evidence) external view returns (bool checked);
function check(address subject, bytes calldata evidence) external view returns (bool checked);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ interface IBasePolicy is IPolicy {
/// @notice Emitted when a subject successfully passes a policy enforcement check.
/// @param subject Address that passed the validation.
/// @param target Address of the protected contract.
/// @param evidence Data used during validation.
event Enforced(address indexed subject, address indexed target, bytes[] evidence);
/// @param evidence Custom validation data.
event Enforced(address indexed subject, address indexed target, bytes evidence);

/// @notice Enforces a validation check on a given subject.
/// @dev This method ensures that the provided subject meets the policy's criteria.
/// @param subject Address to validate.
/// @param evidence Data required for validation.
function enforce(address subject, bytes[] calldata evidence) external;
/// @param evidence Custom validation data.
function enforce(address subject, bytes calldata evidence) external;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ interface IPolicy {
/// @notice Error thrown when attempting to set the target more than once.
error TargetAlreadySet();

/// @notice Error thrown when a subject is already enforced.
error AlreadyEnforced();

/// @notice Retrieves the policy trait identifier.
/// @dev This is typically used to distinguish policy implementations (e.g., "Semaphore").
/// @return The policy trait string.
Expand Down
1 change: 0 additions & 1 deletion packages/contracts/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"publishConfig": {
"access": "public"
},
"packageManager": "yarn@4.5.0",
"dependencies": {
"@openzeppelin/contracts": "^5.2.0",
"solady": "^0.1.4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.20;

import {IAdvancedPolicy, Check} from "../interfaces/IAdvancedPolicy.sol";
import {AdvancedChecker, CheckStatus} from "../checker/AdvancedChecker.sol";
import {AdvancedChecker} from "../checker/AdvancedChecker.sol";
import {Policy} from "./Policy.sol";

/// @title AdvancedPolicy
Expand All @@ -18,60 +18,42 @@ abstract contract AdvancedPolicy is IAdvancedPolicy, Policy {
/// @notice Controls whether post-condition checks are required.
bool public SKIP_POST;

/// @notice Controls whether main check can be executed multiple times.
bool public ALLOW_MULTIPLE_MAIN;

/// @notice Tracks enforcement status for each subject for each phase.
mapping(address => CheckStatus) public enforced;

/// @notice Initializes the contract with appended bytes data for configuration.
/// @dev Decodes AdvancedChecker address and sets the owner.
function _initialize() internal virtual override {
super._initialize();

bytes memory data = _getAppendedBytes();
(address sender, address advCheckerAddr, bool skipPre, bool skipPost, bool allowMultipleMain) = abi.decode(
(address sender, address advCheckerAddr, bool skipPre, bool skipPost) = abi.decode(
data,
(address, address, bool, bool, bool)
(address, address, bool, bool)
);

_transferOwnership(sender);

ADVANCED_CHECKER = AdvancedChecker(advCheckerAddr);
SKIP_PRE = skipPre;
SKIP_POST = skipPost;
ALLOW_MULTIPLE_MAIN = allowMultipleMain;
}

/// @notice Enforces a multi-stage policy check.
/// @dev Handles pre, main, and post validation stages. Only callable by the target contract.
/// @param subject Address to enforce the policy on.
/// @param evidence Evidence required for validation.
/// @param evidence Custom validation data.
/// @param checkType The type of check performed (PRE, MAIN, POST).
function enforce(address subject, bytes[] calldata evidence, Check checkType) external override onlyTarget {
function enforce(address subject, bytes calldata evidence, Check checkType) external override onlyTarget {
_enforce(subject, evidence, checkType);
}

/// @notice Internal implementation of multi-stage enforcement logic.
/// @param subject Address to enforce the policy on.
/// @param evidence Evidence required for validation.
/// @param evidence Custom validation data.
/// @param checkType The type of check performed (PRE, MAIN, POST).
function _enforce(address subject, bytes[] calldata evidence, Check checkType) internal {
CheckStatus storage status = enforced[subject];

function _enforce(address subject, bytes calldata evidence, Check checkType) internal {
if (checkType == Check.PRE) {
if (SKIP_PRE) revert CannotPreCheckWhenSkipped();
if (status.pre) revert AlreadyEnforced();
status.pre = true;
} else if (checkType == Check.POST) {
if (SKIP_POST) revert CannotPostCheckWhenSkipped();
if (status.main == 0) revert MainCheckNotEnforced();
if (status.post) revert AlreadyEnforced();
status.post = true;
} else {
if (!SKIP_PRE && !status.pre) revert PreCheckNotEnforced();
if (!ALLOW_MULTIPLE_MAIN && status.main > 0) revert MainCheckAlreadyEnforced();
status.main += 1;
}

if (!ADVANCED_CHECKER.check(subject, evidence, checkType)) revert UnsuccessfulCheck();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ abstract contract BasePolicy is Policy, IBasePolicy {
/// @notice Reference to the BaseChecker contract used for validation.
BaseChecker public BASE_CHECKER;

/// @notice Tracks enforcement status for each subject.
mapping(address => bool) public enforced;

/// @notice Initializes the contract with appended bytes data for configuration.
/// @dev Decodes BaseChecker address and sets the owner.
function _initialize() internal virtual override {
Expand All @@ -31,20 +28,17 @@ abstract contract BasePolicy is Policy, IBasePolicy {
/// @notice Enforces a policy check for a subject.
/// @dev Uses the BaseChecker for validation logic. Only callable by the target contract.
/// @param subject Address to enforce the policy on.
/// @param evidence Evidence required for validation.
function enforce(address subject, bytes[] calldata evidence) external override onlyTarget {
/// @param evidence Custom validation data.
function enforce(address subject, bytes calldata evidence) external override onlyTarget {
_enforce(subject, evidence);
}

/// @notice Internal logic for enforcing policy checks.
/// @param subject Address to enforce the policy on.
/// @param evidence Evidence required for validation.
function _enforce(address subject, bytes[] memory evidence) internal {
if (enforced[subject]) revert AlreadyEnforced();
/// @param evidence Custom validation data.
function _enforce(address subject, bytes calldata evidence) internal {
if (!BASE_CHECKER.check(subject, evidence)) revert UnsuccessfulCheck();

enforced[subject] = true;

emit Enforced(subject, target, evidence);
}
}
Loading
Loading