Git Source

Inherits: [FacetCommonImports](/v2(/v2/reference/engine/facets/FacetCommonImports.sol/abstract.FacetCommonImports)

Author: @mpetersoCode55, @ShaneDuncan602, @TJ-Everett, @VoR0220

This contract is a critical component of the Rules Engine, enabling secure and flexible policy management.

This contract serves as the primary data facet for the Rules Engine. It is responsible for creating, updating, retrieving, and managing policies and rules. It enforces role-based access control and ensures that only authorized users can modify or retrieve data. The contract also supports policy cementing to prevent further modifications.

Functions

createRule

Creates a rule in storage.

Adds a new rule to the specified policy. Only accessible by policy admins.

function createRule(uint256 _policyId, Rule calldata _rule)
    public
    policyAdminOnly(_policyId, msg.sender)
    notCemented(_policyId)
    returns (uint256);

Parameters

NameTypeDescription
_policyIduint256ID of the policy the rule will be added to.
_ruleRuleThe rule to create.

Returns

NameTypeDescription
<none>uint256ruleId The generated rule ID.

_storeRule

Stores a rule in storage.

Validates the policy existence before storing the rule.

function _storeRule(RuleS storage _data, uint256 _policyId, uint256 _ruleId, Rule calldata _rule)
    internal
    returns (uint256);

Parameters

NameTypeDescription
_dataRuleSThe rule storage structure.
_policyIduint256The ID of the policy the rule belongs to.
_ruleIduint256The ID of the rule to store.
_ruleRuleThe rule to store.

Returns

NameTypeDescription
<none>uint256ruleId The stored rule ID.

updateRule

Updates a rule in storage.

Modifies an existing rule in the specified policy. Only accessible by policy admins.

function updateRule(uint256 _policyId, uint256 _ruleId, Rule calldata _rule)
    external
    policyAdminOnly(_policyId, msg.sender)
    notCemented(_policyId)
    returns (uint256);

Parameters

NameTypeDescription
_policyIduint256ID of the policy the rule belongs to.
_ruleIduint256The ID of the rule to update.
_ruleRuleThe updated rule data.

Returns

NameTypeDescription
<none>uint256ruleId The updated rule ID.

getRule

Retrieves a rule from storage.

function getRule(uint256 _policyId, uint256 _ruleId) public view returns (RuleStorageSet memory);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy the rule belongs to.
_ruleIduint256The ID of the rule to retrieve.

Returns

NameTypeDescription
<none>RuleStorageSetruleStorageSets The rule data.

deleteRule

Deletes a rule from storage.

function deleteRule(uint256 _policyId, uint256 _ruleId)
    public
    policyAdminOnly(_policyId, msg.sender)
    notCemented(_policyId);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy the rule belongs to.
_ruleIduint256The ID of the rule to delete.

getAllRules

Retrieves all rules associated with a specific policy.

function getAllRules(uint256 _policyId) external view returns (Rule[][] memory);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy.

Returns

NameTypeDescription
<none>Rule[][]rules A two-dimensional array of rules grouped by function signatures.

updatePolicy

Updates a policy in storage.

Updates the policy type, function signatures, and associated rules.

function updatePolicy(
    uint256 _policyId,
    bytes4[] calldata _signatures,
    uint256[] calldata _functionSignatureIds,
    uint256[][] calldata _ruleIds,
    PolicyType _policyType
) external policyAdminOnly(_policyId, msg.sender) notCemented(_policyId) returns (uint256);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy to update.
_signaturesbytes4[]The function signatures in the policy.
_functionSignatureIdsuint256[]The IDs of the function signatures.
_ruleIdsuint256[][]A two-dimensional array of rule IDs associated with the policy.
_policyTypePolicyTypeThe type of the policy (CLOSED_POLICY or OPEN_POLICY).

Returns

NameTypeDescription
<none>uint256policyId The updated policy ID.

closePolicy

Closes a policy by changing its type to CLOSED_POLICY.

This function ensures that only policy admins can close a policy and that the policy is not cemented.

function closePolicy(uint256 _policyId) external policyAdminOnly(_policyId, msg.sender) notCemented(_policyId);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy to close.

openPolicy

Opens a policy by changing its type to OPEN_POLICY.

This function ensures that only policy admins can open a policy and that the policy is not cemented.

function openPolicy(uint256 _policyId) external policyAdminOnly(_policyId, msg.sender) notCemented(_policyId);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy to open.

isClosedPolicy

Checks if a policy is closed.

function isClosedPolicy(uint256 _policyId) external view returns (bool);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy to check.

Returns

NameTypeDescription
<none>boolbool True if the policy is closed, false otherwise.

deletePolicy

Deletes a policy from storage.

Removes the policy and all associated rules, trackers, and foreign calls. Ensures the policy is not cemented.

function deletePolicy(uint256 _policyId) public policyAdminOnly(_policyId, msg.sender) notCemented(_policyId);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy to delete.

applyPolicy

Applies policies to a specified contract.

Ensures that only calling contract admins can apply policies and validates policy types.

function applyPolicy(address _contractAddress, uint256[] calldata _policyIds)
    external
    callingContractAdminOnly(_contractAddress, msg.sender);

Parameters

NameTypeDescription
_contractAddressaddressThe address of the contract to apply policies to.
_policyIdsuint256[]The IDs of the policies to apply.

getAppliedPolicyIds

Retrieves the IDs of policies applied to a specific contract.

function getAppliedPolicyIds(address _contractAddress) external view returns (uint256[] memory);

Parameters

NameTypeDescription
_contractAddressaddressThe address of the contract.

Returns

NameTypeDescription
<none>uint256[]uint256[] An array of applied policy IDs.

createPolicy

Creates a new policy and assigns a policy admin.

Generates a policy ID and initializes the policy with the specified type.

function createPolicy(
    FunctionSignatureStorageSet[] calldata _functionSignatures,
    Rule[] calldata _rules,
    PolicyType _policyType
) external returns (uint256);

Parameters

NameTypeDescription
_functionSignaturesFunctionSignatureStorageSet[]The function signatures associated with the policy.
_rulesRule[]The rules associated with the policy.
_policyTypePolicyTypeThe type of the policy (CLOSED_POLICY or OPEN_POLICY).

Returns

NameTypeDescription
<none>uint256uint256 The generated policy ID.

_storePolicyData

Internal helper function for creating and updating policy data.

This function processes and stores policy data, including function signatures, rules, and policy type.

The parameters are complex because nested structs are not allowed for externally facing functions.

function _storePolicyData(
    uint256 _policyId,
    bytes4[] calldata _signatures,
    uint256[] calldata _functionSignatureIds,
    uint256[][] calldata _ruleIds,
    PolicyType _policyType
) internal returns (uint256);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy.
_signaturesbytes4[]All function signatures in the policy.
_functionSignatureIdsuint256[]Corresponding signature IDs in the policy. Each element matches one-to-one with the elements in _signatures.
_ruleIdsuint256[][]A two-dimensional array of rule IDs. The first level represents function signatures, and the second level contains rule IDs for each signature.
_policyTypePolicyTypeThe type of the policy (OPEN or CLOSED).

Returns

NameTypeDescription
<none>uint256policyId The updated policy ID.

_processPolicyTypeChange

Internal helper function to handle data cleanup during policy type changes.

This function removes applied contract associations when transitioning to a CLOSED policy.

function _processPolicyTypeChange(uint256 _policyId, PolicyType _policyType) internal;

Parameters

NameTypeDescription
_policyIduint256The ID of the policy.
_policyTypePolicyTypeThe new type of the policy (OPEN or CLOSED).

getPolicy

Retrieves a policy from storage.

Since Policy contains nested mappings, the data is broken into arrays for external return.

function getPolicy(uint256 _policyId)
    external
    view
    returns (bytes4[] memory functionSigs, uint256[] memory functionSigIds, uint256[][] memory ruleIds);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy.

Returns

NameTypeDescription
functionSigsbytes4[]The function signatures within the policy.
functionSigIdsuint256[]The function signature IDs corresponding to each function signature.
ruleIdsuint256[][]The rule IDs corresponding to each function signature.

_isCemented

Checks if a policy is cemented.

function _isCemented(uint256 _policyId) internal view returns (bool _cemented);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy.

Returns

NameTypeDescription
_cementedboolTrue if the policy is cemented, false otherwise.

notCemented

Modifier to ensure that cemented policies cannot be modified.

modifier notCemented(uint256 _policyId);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy.

cementPolicy

Marks a policy as cemented, preventing further modifications.

function cementPolicy(uint256 _policyId) external policyAdminOnly(_policyId, msg.sender);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy to cement.

_isFunctionSignatureSet

This section is for internal functions used for validation of components. They are here to optimize gas consumption.

Checks if a function signature is set for the specified policy.

Validates whether the function signature exists in the policy’s storage.

function _isFunctionSignatureSet(uint256 _policyId, uint256 _functionSignatureId) internal view returns (bool);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy the function signature is associated with.
_functionSignatureIduint256The ID of the function signature to check.

Returns

NameTypeDescription
<none>boolset True if the function signature is set, false otherwise.

_isForeignCallSet

Checks if a foreign call is set for the specified policy.

Validates whether the foreign call exists in the policy’s storage.

function _isForeignCallSet(uint256 _policyId, uint256 _foreignCallId) internal view returns (bool);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy the foreign call is associated with.
_foreignCallIduint256The ID of the foreign call to check.

Returns

NameTypeDescription
<none>boolset True if the foreign call is set, false otherwise.

_isTrackerSet

Checks if a tracker is set for the specified policy.

Validates whether the tracker exists in the policy’s storage.

function _isTrackerSet(uint256 _policyId, uint256 _index) public view returns (bool);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy the tracker is associated with.
_indexuint256The index of the tracker to check.

Returns

NameTypeDescription
<none>boolset True if the tracker is set, false otherwise.