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 rule evaluation and effect execution.

This contract serves as the core processor for evaluating rules and executing effects in the Rules Engine. It provides functionality for evaluating policies, rules, and conditions, as well as executing effects based on rule outcomes. The contract also supports foreign calls, dynamic argument handling, and tracker updates.

Functions

initialize

Initializes the Rules Engine with the specified owner.

This function sets the initial owner of the diamond and ensures the contract is only initialized once.

function initialize(address _owner) external;

Parameters

NameTypeDescription
_owneraddressThe initial owner of the diamond.

checkPolicies

Evaluates the conditions associated with all applicable rules and returns the result.

Primary entry point for policy checks.

function checkPolicies(address contractAddress, bytes calldata arguments) public returns (uint256 retVal);

Parameters

NameTypeDescription
contractAddressaddressThe address of the rules-enabled contract, used to pull the applicable rules.
argumentsbytesFunction arguments, including the function signature and the arguments to be passed to the function.

Returns

NameTypeDescription
retValuint2561 if all rules pass, 0 if any rule fails. TODO: refine the parameters to this function. contractAddress is not necessary as it’s the message caller

_checkPolicy

Checks a specific policy for compliance.

function _checkPolicy(uint256 _policyId, address _contractAddress, bytes calldata arguments)
    internal
    returns (bool retVal);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy to check.
_contractAddressaddressThe address of the contract being evaluated.
argumentsbytesFunction arguments for the policy evaluation.

Returns

NameTypeDescription
retValboolTrue if the policy passes, false otherwise.

_loadApplicableRules

Loads applicable rules for a given function signature.

function _loadApplicableRules(
    mapping(uint256 ruleId => RuleStorageSet) storage ruleData,
    Policy storage policy,
    bytes4 functionSignature
) internal view returns (uint256[] memory);

Parameters

NameTypeDescription
ruleDatamapping(uint256 ruleId => RuleStorageSet)The mapping of rule IDs to rule storage sets.
policyPolicyThe policy structure containing the rules.
functionSignaturebytes4The function signature to match rules against.

Returns

NameTypeDescription
<none>uint256[]An array of applicable rule IDs.

evaluateRulesAndExecuteEffects

Evaluates rules and executes their effects based on the evaluation results.

function evaluateRulesAndExecuteEffects(
    mapping(uint256 ruleId => RuleStorageSet) storage ruleData,
    uint256 _policyId,
    uint256[] memory applicableRules,
    bytes calldata functionSignatureArgs
) internal returns (bool retVal);

Parameters

NameTypeDescription
ruleDatamapping(uint256 ruleId => RuleStorageSet)The mapping of rule IDs to rule storage sets.
_policyIduint256The ID of the policy being evaluated.
applicableRulesuint256[]An array of applicable rule IDs.
functionSignatureArgsbytesThe arguments for the function signature.

Returns

NameTypeDescription
retValboolTrue if all rules pass, false otherwise.

_evaluateIndividualRule

evaluates an individual rules condition(s)

function _evaluateIndividualRule(Rule storage rule, uint256 _policyId, bytes calldata functionSignatureArgs)
    internal
    returns (bool response);

Parameters

NameTypeDescription
ruleRulethe rule structure containing the instruction set, with placeholders, to execute
_policyIduint256Policy id being evaluated.
functionSignatureArgsbytesthe values to replace the placeholders in the instruction set with.

Returns

NameTypeDescription
responseboolthe result of the rule condition evaluation

_buildArguments

Constructs the arguments required for building the rule’s place holders.

function _buildArguments(Rule storage rule, uint256 _policyId, bytes calldata functionSignatureArgs, bool effect)
    internal
    returns (bytes[] memory, Placeholder[] memory);

Parameters

NameTypeDescription
ruleRuleThe storage reference to the Rule struct containing the rule’s details.
_policyIduint256The unique identifier of the policy associated with the rule.
functionSignatureArgsbytesThe calldata containing the arguments for the function signature.
effectboolA boolean indicating whether the rule has an effect or not.

Returns

NameTypeDescription
<none>bytes[]A tuple containing: - An array of bytes representing the constructed arguments. - An array of Placeholder structs used for argument substitution.
<none>Placeholder[]

_run

Internal function to decode the arguments and do the comparisons.

function _run(uint256[] memory prog, Placeholder[] memory placeHolders, uint256 _policyId, bytes[] memory arguments)
    internal
    returns (bool ans);

Parameters

NameTypeDescription
proguint256[]An array of uint256 representing the program to be executed.
placeHoldersPlaceholder[]An array of Placeholder structs used within the program.
_policyIduint256The ID of the policy associated with the program execution.
argumentsbytes[]An array of bytes containing additional arguments for the program.

Returns

NameTypeDescription
ansboolA boolean indicating the result of the program execution.

_updateTrackerValue

This function updates the tracker value with the information provided

function _updateTrackerValue(uint256 _policyId, uint256 _trackerId, uint256 _trackerValue) internal;

Parameters

NameTypeDescription
_policyIduint256Policy id being evaluated.
_trackerIduint256ID of the tracker to update.
_trackerValueuint256Value to update within the tracker

_updateTrackerValue

Internal function to update the value of a tracker associated with a specific policy.

function _updateTrackerValue(uint256 _policyId, uint256 _trackerId, bytes memory _trackerValue) internal;

Parameters

NameTypeDescription
_policyIduint256The ID of the policy to which the tracker belongs.
_trackerIduint256The ID of the tracker whose value is being updated.
_trackerValuebytesThe new value to be assigned to the tracker, encoded as bytes.

evaluateForeignCalls

Evaluates foreign calls within the rules engine processor.

This function processes and evaluates calls to external contracts or systems as part of the rules engine’s logic. Ensure that the necessary validations and security checks are in place when interacting with foreign calls.

function evaluateForeignCalls(uint256 _policyId, bytes calldata functionSignatureArgs, uint256 foreignCallIndex)
    public
    returns (ForeignCallReturnValue memory returnValue);

Parameters

NameTypeDescription
_policyIduint256Id of the policy.
functionSignatureArgsbytesrepresentation of the function signature arguments
foreignCallIndexuint256Index of the foreign call.

Returns

NameTypeDescription
returnValueForeignCallReturnValueThe output of the foreign call.

evaluateForeignCallForRule

encodes the arguments and places a foreign call, returning the calls return value as long as it is successful

function evaluateForeignCallForRule(ForeignCall memory fc, bytes calldata functionArguments)
    public
    returns (ForeignCallReturnValue memory retVal);

Parameters

NameTypeDescription
fcForeignCallthe Foreign Call structure
functionArgumentsbytesthe arguments of the rules calling function (to be passed to the foreign call as needed)

Returns

NameTypeDescription
retValForeignCallReturnValuethe foreign calls return value

_doEffects

Internal function to process the effects of a rule.

function _doEffects(
    Rule storage rule,
    uint256 _policyId,
    Effect[] memory _effects,
    bytes calldata functionSignatureArgs
) internal;

Parameters

NameTypeDescription
ruleRuleThe rule being processed, stored in the contract’s storage.
_policyIduint256The ID of the policy associated with the rule.
_effectsEffect[]An array of effects to be applied as part of the rule execution.
functionSignatureArgsbytesEncoded calldata containing arguments for the function signature.

_buildEvent

Define event to be fired

function _buildEvent(
    Rule storage rule,
    bool isDynamicParam,
    uint256 _policyId,
    bytes32 _message,
    Effect memory effectStruct,
    bytes calldata functionSignatureArgs
) internal;

Parameters

NameTypeDescription
ruleRulethe rule struct event is associated to
isDynamicParamboolstatic or dynamic event parameters
_policyIduint256policy Id
_messagebytes32Event Message String
effectStructEffecteffect struct
functionSignatureArgsbytescalling function signature arguments

_fireDynamicEvent

Fire dynamic Event

function _fireDynamicEvent(Rule storage rule, uint256 _policyId, bytes32 _message, bytes calldata functionSignatureArgs)
    internal;

Parameters

NameTypeDescription
ruleRulethe rule struct event is associated to
_policyIduint256policy Id
_messagebytes32Event Message String
functionSignatureArgsbytescalling function signature arguments

_fireEvent

Internal function to trigger an event based on the provided policy ID, message, and effect structure.

function _fireEvent(uint256 _policyId, bytes32 _message, Effect memory effectStruct) internal;

Parameters

NameTypeDescription
_policyIduint256The unique identifier of the policy associated with the event.
_messagebytes32A bytes32 message that provides context or details about the event.
effectStructEffectA struct containing the effect data to be processed during the event.

_doRevert

Internal pure function to revert the transaction with a custom error message.

function _doRevert(string memory _message) internal pure;

Parameters

NameTypeDescription
_messagestringThe custom error message to include in the revert.

_evaluateExpression

Evaluate an effect expression

function _evaluateExpression(
    Rule storage rule,
    uint256 _policyId,
    bytes calldata functionSignatureArgs,
    uint256[] memory instructionSet
) internal;

Parameters

NameTypeDescription
ruleRulethe rule structure containing the instruction set, with placeholders, to execute
_policyIduint256the policy id
functionSignatureArgsbytesarguments of the function signature
instructionSetuint256[]instruction set

ui2bool

converts a uint256 to a bool

function ui2bool(uint256 x) public pure returns (bool ans);

Parameters

NameTypeDescription
xuint256the uint256 to convert

bool2ui

converts a bool to an uint256

function bool2ui(bool x) public pure returns (uint256 ans);

Parameters

NameTypeDescription
xboolthe bool to convert

ui2addr

converts a uint256 to an address

function ui2addr(uint256 x) public pure returns (address ans);

Parameters

NameTypeDescription
xuint256the uint256 to convert

ui2bytes

converts a uint256 to a bytes

function ui2bytes(uint256 x) public pure returns (bytes memory ans);

Parameters

NameTypeDescription
xuint256the uint256 to convert

getDynamicVariableFromCalldata

Extracts a dynamic variable from the provided calldata at the specified index.

This function is a pure function and does not modify state.

function getDynamicVariableFromCalldata(bytes calldata data, uint256 index) public pure returns (bytes memory retVal);

Parameters

NameTypeDescription
databytesThe calldata containing the dynamic variables.
indexuint256The index of the dynamic variable to extract.

Returns

NameTypeDescription
retValbytesThe extracted dynamic variable as a bytes array.

retrieveRawStringFromInstructionSet

Retrieves the raw string associated with a specific instruction set within a rule and policy.

This function is used to fetch a StringVerificationStruct containing the raw string data.

function retrieveRawStringFromInstructionSet(uint256 _policyId, uint256 _ruleId, uint256 instructionSetId)
    public
    view
    returns (StringVerificationStruct memory retVal);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy containing the rule.
_ruleIduint256The ID of the rule containing the instruction set.
instructionSetIduint256The ID of the instruction set to retrieve the raw string from.

Returns

NameTypeDescription
retValStringVerificationStructA StringVerificationStruct containing the raw string data for the specified instruction set.

retrieveRawAddressFromInstructionSet

Retrieves the raw address from a specific instruction set.

This function is used to fetch the raw address associated with a given policy ID, rule ID, and instruction set ID.

function retrieveRawAddressFromInstructionSet(uint256 _policyId, uint256 _ruleId, uint256 instructionSetId)
    public
    view
    returns (AddressVerificationStruct memory retVal);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy to which the rule belongs.
_ruleIduint256The ID of the rule within the policy.
instructionSetIduint256The ID of the instruction set to retrieve the address verification structure from.

Returns

NameTypeDescription
retValAddressVerificationStructThe AddressVerificationStruct containing the raw address data.

_retreiveRawEncodedFromInstructionSet

Retrieves the raw encoded data and instruction set value from a given instruction set ID.

function _retreiveRawEncodedFromInstructionSet(uint256 _policyId, uint256 _ruleId, uint256 instructionSetId, PT pType)
    internal
    view
    returns (uint256 instructionSetValue, bytes memory encoded);

Parameters

NameTypeDescription
_policyIduint256The ID of the policy associated with the instruction set.
_ruleIduint256The ID of the rule associated with the instruction set.
instructionSetIduint256The ID of the instruction set to retrieve data from.
pTypePTThe parameter type (PT) associated with the instruction set.

Returns

NameTypeDescription
instructionSetValueuint256The value of the instruction set.
encodedbytesThe raw encoded data associated with the instruction set.

getDynamicValueArrayData

Retrieves a portion of dynamic value array data from the provided inputs.

This function extracts a segment of dynamic data based on the specified length and offset.

function getDynamicValueArrayData(
    bytes calldata data,
    bytes memory dynamicData,
    uint256 length,
    uint256 lengthToAppend,
    uint256 offset
) public pure returns (bytes memory, uint256);

Parameters

NameTypeDescription
databytesThe calldata input containing the original data.
dynamicDatabytesThe memory input containing the dynamic data to process.
lengthuint256The total length of the dynamic data array.
lengthToAppenduint256The length of data to append to the result.
offsetuint256The starting position in the dynamic data array to begin extraction.

Returns

NameTypeDescription
<none>bytesA tuple containing: - A bytes memory object representing the extracted data. - A uint256 value indicating the updated offset after extraction.
<none>uint256