Git Source

Inherits: 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

checkPolicies

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

Primary entry point for policy checks.

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

Parameters

NameTypeDescription
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

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 callingFunctionArgs,
    uint256 foreignCallIndex,
    bytes[] memory retVals
) public returns (ForeignCallReturnValue memory returnValue);

Parameters

NameTypeDescription
policyIduint256Id of the policy.
callingFunctionArgsbytesrepresentation of the calling function arguments
foreignCallIndexuint256Index of the foreign call.
retValsbytes[]array of return values from previous foreign calls, trackers, etc.

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, bytes[] memory retVals)
    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)
retValsbytes[]

Returns

NameTypeDescription
retValForeignCallReturnValuethe foreign calls return value

_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.

_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 _callingFunctionArgs
) 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.
_callingFunctionArgsbytesThe arguments for the calling function.

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 _callingFunctionArgs)
    internal
    returns (bool response);

Parameters

NameTypeDescription
_ruleRulethe rule structure containing the instruction set, with placeholders, to execute
_policyIduint256Policy id being evaluated.
_callingFunctionArgsbytesthe 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 _callingFunctionArgs, 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.
_callingFunctionArgsbytesThe calldata containing the arguments for the calling function.
_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.

_loadApplicableRules

Loads applicable rules for a given calling function.

function _loadApplicableRules(
    mapping(uint256 ruleId => RuleStorageSet) storage _ruleData,
    Policy storage _policy,
    bytes4 _callingFunction
) 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.
_callingFunctionbytes4The function signature to match rules against.

Returns

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

_getAbsoluteAssembly

Calculates the absolute value of a signed integer.

function _getAbsoluteAssembly(int256 _value) internal pure returns (uint256);

Parameters

NameTypeDescription
_valueint256The signed integer value to convert to its absolute value.

Returns

NameTypeDescription
<none>uint256result The absolute value of the input number as an unsigned integer.

_doEffects

Internal function to process the effects of a rule.

function _doEffects(
    Rule storage _rule,
    uint256 _policyId,
    Effect[] memory _effects,
    bytes calldata _callingFunctionArgs
) 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.
_callingFunctionArgsbytesEncoded calldata containing arguments for the calling function.

_buildEvent

Define event to be fired

function _buildEvent(
    Rule storage _rule,
    bool _isDynamicParam,
    uint256 _policyId,
    bytes32 _message,
    Effect memory _effectStruct,
    bytes calldata _callingFunctionArgs
) internal;

Parameters

NameTypeDescription
_ruleRulethe rule struct event is associated to
_isDynamicParamboolstatic or dynamic event parameters
_policyIduint256policy Id
_messagebytes32Event Message String
_effectStructEffecteffect struct
_callingFunctionArgsbytescalling function arguments

_fireDynamicEvent

Fire dynamic Event

function _fireDynamicEvent(Rule storage _rule, uint256 _policyId, bytes32 _message, bytes calldata _callingFunctionArgs)
    internal;

Parameters

NameTypeDescription
_ruleRulethe rule struct event is associated to
_policyIduint256policy Id
_messagebytes32Event Message String
_callingFunctionArgsbytescalling function 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.

_evaluateExpression

Evaluate an effect expression

function _evaluateExpression(
    Rule storage _rule,
    uint256 _policyId,
    bytes calldata _callingFunctionArgs,
    uint256[] memory _instructionSet
) internal;

Parameters

NameTypeDescription
_ruleRulethe rule structure containing the instruction set, with placeholders, to execute
_policyIduint256the policy id
_callingFunctionArgsbytesarguments of the calling function
_instructionSetuint256[]instruction set

_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.

_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)
    internal
    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.

_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
) internal 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