Skip to content

Commit c74c32d

Browse files
committed
refactor(contracts): improve contracts and test documentation; small code optimization and refactor
1 parent cad1311 commit c74c32d

25 files changed

+666
-593
lines changed

packages/contracts/contracts/src/AdvancedChecker.sol

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,45 @@ pragma solidity 0.8.27;
33

44
import {IAdvancedChecker, Check} from "./interfaces/IAdvancedChecker.sol";
55

6+
/// @notice Tracks validation status for pre, main, and post checks.
7+
/// @dev Used to maintain check state in AdvancedPolicy.
68
struct CheckStatus {
9+
/// @dev Pre-check completion status.
710
bool pre;
11+
/// @dev Number of completed main checks.
812
uint8 main;
13+
/// @dev Post-check completion status.
914
bool post;
1015
}
1116

1217
/// @title AdvancedChecker.
13-
/// @notice Abstract base contract which can be extended to implement a specific `AdvancedChecker`.
14-
/// @dev The `AdvancedChecker` contract builds upon the `BaseChecker` by introducing additional validation phases.
15-
/// It allows for pre-condition (`PRE`), main (`MAIN`), and post-condition (`POST`) checks, with the option to skip
16-
/// pre and post checks based on constructor parameters. The `_check` method orchestrates the validation process
17-
/// based on the specified check type.
18+
/// @notice Multi-phase validation checker with pre, main, and post checks.
19+
/// @dev Base contract for implementing complex validation logic with configurable check phases.
1820
abstract contract AdvancedChecker is IAdvancedChecker {
19-
/// @notice Flag to determine if pre-condition checks should be skipped.
21+
/// @notice Controls whether pre-condition checks are required.
2022
bool public immutable SKIP_PRE;
2123

22-
/// @notice Flag to determine if post-condition checks should be skipped.
24+
/// @notice Controls whether post-condition checks are required.
2325
bool public immutable SKIP_POST;
2426

25-
/// @notice Flag to determine if main checks can be executed multiple times.
27+
/// @notice Controls whether main check can be executed multiple times.
2628
bool public immutable ALLOW_MULTIPLE_MAIN;
2729

28-
/// @param _skipPre Indicates whether to skip pre-condition checks.
29-
/// @param _skipPost Indicates whether to skip post-condition checks.
30-
/// @param _allowMultipleMain Indicates whether the main check can be executed multiple times.
30+
/// @notice Sets up checker configuration.
31+
/// @param _skipPre Skip pre-condition validation.
32+
/// @param _skipPost Skip post-condition validation.
33+
/// @param _allowMultipleMain Allow multiple main validations.
3134
constructor(bool _skipPre, bool _skipPost, bool _allowMultipleMain) {
3235
SKIP_PRE = _skipPre;
3336
SKIP_POST = _skipPost;
3437
ALLOW_MULTIPLE_MAIN = _allowMultipleMain;
3538
}
3639

37-
/// @notice Public method to check the validity of the provided evidence for a given address and check type.
38-
/// @param subject The address to be checked.
39-
/// @param evidence The evidence associated with the check.
40-
/// @param checkType The type of check to perform (PRE, MAIN, POST).
40+
/// @notice Entry point for validation checks.
41+
/// @param subject Address to validate.
42+
/// @param evidence Validation data.
43+
/// @param checkType Type of check (PRE, MAIN, POST).
44+
/// @return checked Validation result.
4145
function check(
4246
address subject,
4347
bytes memory evidence,
@@ -46,37 +50,46 @@ abstract contract AdvancedChecker is IAdvancedChecker {
4650
return _check(subject, evidence, checkType);
4751
}
4852

49-
/// @notice Internal method to orchestrate the validation process based on the specified check type.
50-
/// @param subject The address to be checked.
51-
/// @param evidence The evidence associated with the check.
52-
/// @param checkType The type of check to perform (PRE, MAIN, POST).
53+
/// @notice Core validation logic router.
54+
/// @dev Directs to appropriate check based on type and configuration.
55+
/// @param subject Address to validate.
56+
/// @param evidence Validation data.
57+
/// @param checkType Check type to perform.
58+
/// @return checked Validation result.
59+
/// @custom:throws PreCheckSkipped If PRE check attempted when skipped.
60+
/// @custom:throws PostCheckSkipped If POST check attempted when skipped.
5361
function _check(address subject, bytes memory evidence, Check checkType) internal view returns (bool checked) {
54-
if (SKIP_PRE && checkType == Check.PRE) revert PreCheckSkipped();
55-
if (SKIP_POST && checkType == Check.POST) revert PostCheckSkipped();
62+
// Validate skip conditions first.
63+
if (checkType == Check.PRE && SKIP_PRE) revert PreCheckSkipped();
64+
if (checkType == Check.POST && SKIP_POST) revert PostCheckSkipped();
5665

57-
if (!SKIP_PRE && checkType == Check.PRE) {
58-
return _checkPre(subject, evidence);
59-
}
60-
61-
if (!SKIP_POST && checkType == Check.POST) {
62-
return _checkPost(subject, evidence);
63-
}
64-
65-
return _checkMain(subject, evidence);
66+
// Route to appropriate check.
67+
return
68+
checkType == Check.PRE
69+
? _checkPre(subject, evidence)
70+
: checkType == Check.POST
71+
? _checkPost(subject, evidence)
72+
: _checkMain(subject, evidence);
6673
}
6774

68-
/// @notice Internal method for performing pre-condition checks.
69-
/// @param subject The address to be checked.
70-
/// @param evidence The evidence associated with the check.
75+
/// @notice Pre-condition validation implementation.
76+
/// @dev Override to implement pre-check logic.
77+
/// @param subject Address to validate.
78+
/// @param evidence Validation data.
79+
/// @return checked Validation result.
7180
function _checkPre(address subject, bytes memory evidence) internal view virtual returns (bool checked) {}
7281

73-
/// @notice Internal method for performing main checks.
74-
/// @param subject The address to be checked.
75-
/// @param evidence The evidence associated with the check.
82+
/// @notice Main validation implementation.
83+
/// @dev Override to implement main check logic.
84+
/// @param subject Address to validate.
85+
/// @param evidence Validation data.
86+
/// @return checked Validation result.
7687
function _checkMain(address subject, bytes memory evidence) internal view virtual returns (bool checked) {}
7788

78-
/// @notice Internal method for performing post-condition checks.
79-
/// @param subject The address to be checked.
80-
/// @param evidence The evidence associated with the check.
89+
/// @notice Post-condition validation implementation.
90+
/// @dev Override to implement post-check logic.
91+
/// @param subject Address to validate.
92+
/// @param evidence Validation data.
93+
/// @return checked Validation result.
8194
function _checkPost(address subject, bytes memory evidence) internal view virtual returns (bool checked) {}
8295
}

packages/contracts/contracts/src/AdvancedPolicy.sol

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,80 +5,84 @@ import {Policy} from "./Policy.sol";
55
import {IAdvancedPolicy, Check} from "./interfaces/IAdvancedPolicy.sol";
66
import {AdvancedChecker, CheckStatus} from "./AdvancedChecker.sol";
77

8-
/// @title AdvancedPolicy
9-
/// @notice Abstract base contract which can be extended to implement a specific `AdvancedPolicy`.
8+
/// @title AdvancedPolicy.
9+
/// @notice Implements advanced policy checks with pre, main, and post validation stages.
10+
/// @dev Extends Policy contract with multi-stage validation capabilities.
1011
abstract contract AdvancedPolicy is IAdvancedPolicy, Policy {
11-
/// @dev Reference to the AdvancedChecker contract for validation.
12+
/// @notice Reference to the validation checker contract.
13+
/// @dev Immutable to ensure checker cannot be changed after deployment.
1214
AdvancedChecker public immutable ADVANCED_CHECKER;
1315

14-
/// @dev Tracks the check status of each address.
16+
/// @notice Tracks validation status for each subject per target.
17+
/// @dev Maps target => subject => CheckStatus.
1518
mapping(address => mapping(address => CheckStatus)) public enforced;
1619

17-
/// @notice Constructor to initialize the AdvancedChecker contract.
18-
/// @param _advancedChecker The address of the AdvancedChecker contract.
20+
/// @notice Initializes contract with an AdvancedChecker instance.
21+
/// @param _advancedChecker Address of the AdvancedChecker contract.
1922
constructor(AdvancedChecker _advancedChecker) {
2023
ADVANCED_CHECKER = _advancedChecker;
2124
}
2225

23-
/// @notice Enforces the custom target logic.
24-
/// @dev Calls the internal `_enforce` function to enforce the target logic.
25-
/// @dev Must call the `check` to handle the logic of checking subject for specific target.
26-
/// @param subject The address of those who have successfully enforced the check.
27-
/// @param evidence Additional data required for the check (e.g., encoded token identifier).
28-
/// @param checkType The type of the check to be enforced for the subject with the given data.
26+
/// @notice Enforces policy check for a subject.
27+
/// @dev Only callable by target contract.
28+
/// @param subject Address to validate.
29+
/// @param evidence Validation data.
30+
/// @param checkType Type of check (PRE, MAIN, POST).
2931
function enforce(address subject, bytes calldata evidence, Check checkType) external override onlyTarget {
3032
_enforce(subject, evidence, checkType);
3133
}
3234

33-
/// @notice Internal function to enforce the target logic.
34-
/// @param subject The address of those who have successfully enforced the check.
35-
/// @param evidence Additional data required for the check (e.g., encoded token identifier).
36-
/// @param checkType The type of the check to be enforced for the subject with the given data.
35+
/// @notice Internal check enforcement logic.
36+
/// @dev Handles different check types and their dependencies.
37+
/// @param subject Address to validate.
38+
/// @param evidence Validation data.
39+
/// @param checkType Type of check to perform.
40+
/// @custom:throws UnsuccessfulCheck If validation fails.
41+
/// @custom:throws AlreadyEnforced If check was already completed.
42+
/// @custom:throws PreCheckNotEnforced If PRE check is required but not done.
43+
/// @custom:throws MainCheckNotEnforced If MAIN check is required but not done.
44+
/// @custom:throws MainCheckAlreadyEnforced If multiple MAIN checks not allowed.
3745
function _enforce(address subject, bytes calldata evidence, Check checkType) internal {
38-
bool checked = ADVANCED_CHECKER.check(subject, evidence, checkType);
39-
40-
if (!checked) {
46+
if (!ADVANCED_CHECKER.check(subject, evidence, checkType)) {
4147
revert UnsuccessfulCheck();
4248
}
4349

50+
CheckStatus storage status = enforced[msg.sender][subject];
51+
52+
// Handle PRE check.
4453
if (checkType == Check.PRE) {
45-
if (!ADVANCED_CHECKER.SKIP_POST() && enforced[msg.sender][subject].pre) {
54+
if (!ADVANCED_CHECKER.SKIP_POST() && status.pre) {
4655
revert AlreadyEnforced();
47-
} else {
48-
enforced[msg.sender][subject].pre = true;
4956
}
50-
} else {
51-
if (checkType == Check.POST) {
52-
if (enforced[msg.sender][subject].post) {
53-
revert AlreadyEnforced();
54-
} else {
55-
if (!ADVANCED_CHECKER.SKIP_PRE() && !enforced[msg.sender][subject].pre) {
56-
revert PreCheckNotEnforced();
57-
} else {
58-
if (enforced[msg.sender][subject].main == 0) {
59-
revert MainCheckNotEnforced();
60-
} else {
61-
enforced[msg.sender][subject].post = true;
62-
}
63-
}
64-
}
65-
} else {
66-
if (
67-
checkType == Check.MAIN &&
68-
!ADVANCED_CHECKER.ALLOW_MULTIPLE_MAIN() &&
69-
enforced[msg.sender][subject].main > 0
70-
) {
71-
revert MainCheckAlreadyEnforced();
72-
} else {
73-
if (checkType == Check.MAIN && !ADVANCED_CHECKER.SKIP_PRE() && !enforced[msg.sender][subject].pre) {
74-
revert PreCheckNotEnforced();
75-
} else {
76-
enforced[msg.sender][subject].main += 1;
77-
}
78-
}
57+
status.pre = true;
58+
emit Enforced(subject, target, evidence, checkType);
59+
return;
60+
}
61+
62+
// Handle POST check.
63+
if (checkType == Check.POST) {
64+
if (status.post) {
65+
revert AlreadyEnforced();
7966
}
67+
if (!ADVANCED_CHECKER.SKIP_PRE() && !status.pre) {
68+
revert PreCheckNotEnforced();
69+
}
70+
if (status.main == 0) {
71+
revert MainCheckNotEnforced();
72+
}
73+
status.post = true;
74+
emit Enforced(subject, target, evidence, checkType);
75+
return;
8076
}
8177

78+
// Handle MAIN check.
79+
if (!ADVANCED_CHECKER.ALLOW_MULTIPLE_MAIN() && status.main > 0) {
80+
revert MainCheckAlreadyEnforced();
81+
}
82+
if (!ADVANCED_CHECKER.SKIP_PRE() && !status.pre) {
83+
revert PreCheckNotEnforced();
84+
}
85+
status.main += 1;
8286
emit Enforced(subject, target, evidence, checkType);
8387
}
8488
}

packages/contracts/contracts/src/BaseChecker.sol

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@ pragma solidity 0.8.27;
33

44
import {IBaseChecker} from "./interfaces/IBaseChecker.sol";
55

6-
/// @title BaseChecker.
7-
/// @notice Abstract base contract which can be extended to implement a specific `BaseChecker`.
8-
/// @dev The `BaseChecker` contract provides a foundational structure for implementing specific checker logic.
9-
/// It defines a method `check` that invokes a protected `_check` method, which must be implemented by derived
10-
/// contracts.
6+
/// @title BaseChecker
7+
/// @notice Abstract base contract for implementing validation checks.
8+
/// @dev Provides a standardized interface for implementing custom validation logic
9+
/// through the internal _check method.
1110
abstract contract BaseChecker is IBaseChecker {
12-
/// @notice Checks the validity of the provided evidence for a given address.
13-
/// @param subject The address to be checked.
14-
/// @param evidence The evidence associated with the check.
11+
/// @notice Validates evidence for a given subject address.
12+
/// @dev External view function that delegates to internal _check implementation.
13+
/// @param subject Address to validate.
14+
/// @param evidence Custom validation data.
15+
/// @return checked Boolean indicating if the check passed.
1516
function check(address subject, bytes memory evidence) external view override returns (bool checked) {
1617
return _check(subject, evidence);
1718
}
1819

19-
/// @notice Internal method to perform the actual check logic.
20-
/// @param subject The address to be checked.
21-
/// @param evidence The evidence associated with the check.
20+
/// @notice Internal validation logic implementation.
21+
/// @dev Must be implemented by derived contracts.
22+
/// @param subject Address to validate.
23+
/// @param evidence Custom validation data.
24+
/// @return checked Boolean indicating if the check passed.
2225
function _check(address subject, bytes memory evidence) internal view virtual returns (bool checked) {}
2326
}

packages/contracts/contracts/src/BasePolicy.sol

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,45 @@ import {Policy} from "./Policy.sol";
66
import {BaseChecker} from "./BaseChecker.sol";
77

88
/// @title BasePolicy
9-
/// @notice Abstract base contract which can be extended to implement a specific `BasePolicy`.
9+
/// @notice Abstract base contract for implementing specific policy checks.
10+
/// @dev Inherits from Policy and implements IBasePolicy interface.
11+
///
12+
/// Provides core functionality for enforcing policy checks through a BaseChecker
13+
/// contract. Each specific policy implementation should extend this contract
14+
/// and implement its custom checking logic.
1015
abstract contract BasePolicy is Policy, IBasePolicy {
11-
/// @dev Reference to the BaseChecker contract for validation.
16+
/// @notice Reference to the BaseChecker contract used for validation.
17+
/// @dev Immutable to ensure checker cannot be changed after deployment.
1218
BaseChecker public immutable BASE_CHECKER;
1319

14-
/// @dev Tracks whether the check has been enforced for a subject.
20+
/// @notice Tracks enforcement status for each subject per target.
21+
/// @dev Maps target => subject => enforcement status.
1522
mapping(address => mapping(address => bool)) public enforced;
1623

17-
/// @notice Constructor to initialize the BaseChecker contract.
18-
/// @param _baseChecker The address of the BaseChecker contract.
24+
/// @notice Initializes the contract with a BaseChecker instance.
25+
/// @param _baseChecker Address of the BaseChecker contract.
26+
/// @dev The BaseChecker address cannot be changed after deployment.
1927
constructor(BaseChecker _baseChecker) {
2028
BASE_CHECKER = _baseChecker;
2129
}
2230

23-
/// @notice Enforces the custom target enforcing logic.
24-
/// @dev Must call the `check` to handle the logic of checking subject for specific target.
25-
/// @param subject The address of those who have successfully enforced the check.
26-
/// @param evidence Additional data required for the check (e.g., encoded token identifier).
31+
/// @notice External function to enforce policy checks.
32+
/// @dev Only callable by the target contract.
33+
/// @param subject Address to enforce the check on.
34+
/// @param evidence Additional data required for verification.
35+
/// @custom:throws AlreadyEnforced if check was previously enforced.
36+
/// @custom:throws UnsuccessfulCheck if the check fails.
37+
/// @custom:emits Enforced when check succeeds.
2738
function enforce(address subject, bytes calldata evidence) external override onlyTarget {
2839
_enforce(subject, evidence);
2940
}
3041

31-
/// @notice Enforces the custom target enforcing logic.
32-
/// @param subject The address of those who have successfully enforced the check.
33-
/// @param evidence Additional data required for the check (e.g., encoded token identifier).
42+
/// @notice Internal implementation of enforcement logic.
43+
/// @dev Performs the actual check using BASE_CHECKER.
44+
/// @param subject Address to enforce the check on.
45+
/// @param evidence Additional data required for verification.
46+
/// @custom:throws AlreadyEnforced if already enforced for this subject.
47+
/// @custom:throws UnsuccessfulCheck if BASE_CHECKER.check returns false.
3448
function _enforce(address subject, bytes calldata evidence) internal {
3549
bool checked = BASE_CHECKER.check(subject, evidence);
3650

0 commit comments

Comments
 (0)