ERC-2746 Rules Engine standard

Stagnant · Other · created 2020-06-20

An interface that registers a data region (Attribute) and a hierarchy of rules and rule sets (RuleTree) within a single deployed contract, letting rules be composed and executed in a single transaction without writing new code.

Why it is needed

Without this standard, any attempt to add new functionality on chain must go through writing and deploying a new smart contract. Even an extremely simple operation like checking or changing a value requires reviewing gas cost and security every time. This is a major barrier for non-technical companies or users unfamiliar with languages like Solidity and the deployment process, making it hard for them to build even simple logic on chain themselves. As a result, even a small feature such as a minor setting or a repetitive condition check ends up depending each time on a professional developer's coding and deployment work.

What to implement

In depth

Data model: Attribute, Rule, RuleSet, RuleTree

The original text defines four terms. An Attribute is a single data point registered within the data region, whose value may reside inside the rule engine contract or elsewhere. A Rule is a predetermined action performed on a single Attribute, for example checking whether the value of the Attribute 'TokenAmt' is less than the right-hand value (RHV) of 10. A RuleSet is a collection of multiple Rules whose collective execution result becomes a single boolean value that determines the execution flow between RuleSets. A RuleTree is a structure in which RuleSets are organized hierarchically, where a RuleSet can contain other RuleSets as children.

Defining the data region with addAttribute

addAttribute takes _attrName (name/ID), _maxLen (max length if a string), _maxNumVal (max numeric value if numeric), _defaultVal (the default value to use when a value cannot be found at the source), _isString, and _isNumeric. The caller must be the deployer/owner of the rule engine contract. An Attribute registered this way becomes the target that subsequent Rules check or manipulate.

Organizing RuleTree, RuleSet, and Rule into a hierarchy

addRuleTree creates a new RuleTree with _owner, _ruleTreeName, and _desc. addRuleSet builds the hierarchy by specifying a parent RuleSet via _parentRSName in addition to _owner, _ruleSetName, and _desc; a child RuleSet is only invoked when the parent RuleSet's Rules execute and the result is true. _severalFailFlag determines whether, when that RuleSet is a leaf, a failure propagates as a failure of the entire RuleTree; _useAndOp determines whether the Rules within the RuleSet are combined with AND (or otherwise OR); and _failQuickFlag determines whether RuleTree execution stops immediately on failure. addRule adds an individual Rule to a RuleSet, taking _owner, _ruleSetName, _ruleName, along with the target _attrName, _ruleType, the _rightHandValue used as the comparison basis, and whether the NOT operation applies (_notFlag).

executeRuleTree execution and event tracking

executeRuleTree(_owner) evaluates the RuleTree of the specified owner and returns a boolean result. During execution, the events CallRuleTree (when the RuleTree is invoked), CallRuleSet (when an individual RuleSet is invoked, including ruler and tmpRuleSetId), and CallRule (when an individual Rule is invoked, including ruler, ruleSetId, ruleId, and ruleType) must each be emitted. If a RuleSet fails, RuleSetError(ruler, ruleSetId, severeFailure) is emitted, where severeFailure indicates whether that RuleSet is a leaf with the 'severe' error flag set. These events let the caller determine, after execution, how the execution flow proceeded within the RuleTree and which RuleSet failed.

Getter functions

getRuleProps(_owner, _ruleSetName, _ruleIdx) returns the name, type, target Attribute, value, and NOT flag of the Rule at a specific index within a RuleSet, along with the list of values (bytes32[]) used for delegatecall if it is a custom operation. getRuleSetProps(_owner, _ruleSetName) returns the RuleSet's description, the flag for whether a leaf failure propagates as a full failure, whether the AND operation is used, the number of Rules, and the list of child RuleSets. getRuleTreeProps(_owner) returns the RuleTree's name, description, and the ID of the RuleSet acting as its root. removeRuleTree(_owner) deletes the corresponding RuleTree.

Why the Right-Hand Value is a string

The reason the right-hand value's type in addRule() is string is that a Rule's behavior depends on its Rule type (ruleType), so the value must be provided in a generic form. For a Rule that performs a numeric operation, the explanation states that the supplied string value can be converted to a number when it is stored in the Rule.

Unspecified extension possibilities (Considerations)

The original text leaves several items out of the specification, mentioning them only as discussion points. Examples include a feature that would let users other than the RuleTree owner also be registered as executors, a feature that would let an administrator configure whether an Attribute's value comes from an internal data structure or from a method call on another contract (such as a Diamond Standard implementation), a feature that would let methods on other contracts be added as Rule operations to expand the catalog of functionality, and a feature that would calculate and report the minimum and maximum gas cost range for invoking a RuleTree. The text states that one implementation, called Wonka, has implemented all of these additional considerations.

Current status: Stagnant

This proposal is in Stagnant status, meaning it has stalled without active discussion or progress. Implementations called Wonka and the Wonka Rules Editor are mentioned as existing, but the standard itself has not been finalized.

Things to watch out for

Original ERC · ← ERCs