syncrule

package
v2.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2025 License: Apache-2.0 Imports: 11 Imported by: 3

README

基于go语法的规则引擎,支持对区块交易进行过滤和筛选,可以通过为节点指定同步规则来限制节点可同步的区块交易数据权限,以达到对数据的保护抑或对节点资源的节省。

  1. 标识符(lable)定义:

    • O: 交易发起组织

    • I: 交易发起身份

    • TT: 交易类型

    • TD: 交易ID

    • C: 合约

      注:使用标识C(合约)时,可指定{合约名}或者{合约名}.{合约方法},不可单独指定合约方法

  2. 支持的语法

    • 算术运算符: ==、!=
    • 逻辑运算符: &&、||
    • 数据类型: 数字、字符串
    • 函数: contain_of
    • 优先级符号:()

    说明:

    字符串数据类型要遵从go语法,如:O=="org1" 而不是 O==org1

    contain_of: contian_of(lable, var) 指定标识是否包含var指定的数据,如contain_of(O, "org1")表示交易发起组织的字符串中含有org1组织

  3. 示例

    • 筛选属于/不属于某个组织的交易

      //筛选属于org1的所有交易
      O == "org1" 
      //筛选不属于org1的所有交易
      O != "org1" 
      
    • 筛选某个账户的交易

      //筛选账户为admin的所有交易
      I == "admin"
      //筛选账户不为admin的所有交易
      I != "admin"
      
    • 筛选某种类型的交易

      //筛选类型为0(调用合约)所有交易, TT可取的值为 0:调用合约 
      TT == 0
      
    • 筛选某个/类交易id的交易

      //筛选交易ID为123的交易
      TD == "123"
      //筛选交易id包含huoyun的交易, 如huoyun123、huoyun_111等
      contain_of(TD, "huoyun")
      
    • 筛选某个合约的交易

      //筛选调用合约save生成的所有交易
      C == "save"
      //筛选调用合约save的Save方法生成的所有交易
      C == "save.Save"
      
    • 筛选合约名字中包含某个关键字的所有交易

      //筛选合约名字中含有save关键字的所有交易,如save1、save2、up_save、up_save_2
      contain_of(c, "save")
      
    • 筛选组织名中包含某个关键字的所有交易(某类组织)

      //筛选组织名字中含有beijing关键字的所有交易,如beijinggugong、beijing_gugong、gugong_beijing_jie
      contain_of(O, "beijing")
      
    • 筛选某个组织下的某个账户的交易

      //筛选由隶属于组织org1身份为admin用户发起的交易
      O == "org1" && I == "admin"
      
    • 筛选某个/些组织下的使用某个合约的生成的交易

      //筛选由隶属于组织org1的用户通过合约save发起的交易
      O == "org1" && C == "save"
      //筛选由隶属于组织org1或者org2的用户通过合约save发起的交易 
      (O == "org1" || O == "org2") && C = "save"
      //筛选由隶属于组织org1或者org2的用户通过合约名字包含save的合约发起的交易 
      (O == "org1" || O == "org2") && contain_of(C, "save") 
      //筛选由隶属于组织org1或者org2的用户通过合约save或合约transfer发起的交易 
      (O == "org1" || O == "org2") && (C = "save" || C == "transfer")
      
    • 其他复杂条件的交易

      // 筛选由组织org1或组织org2中的admin使用合约名包含save的合约或者使用transfer合约或者使用合约名为cross合约方法commit发起的普通交易(TT==0)且交易ID包含huoyun的所有交易
      (O == "org1" || O == "org2") && I == "admin" && TT == 0 && (contain_of(C, "save") || C == "transfer" || C == "cross.commit") && contain_of(TD, "huoyun")
      

Documentation

Overview

Package rule provides a set of rules for filtering transactions.

Index

Constants

View Source
const (
	// FuncContainsOf the token of function contain_of
	FuncContainsOf = "contain_of"
	// FuncInArray the token of function in_array
	FuncInArray = "in_array"
)
View Source
const (
	// KeyEqual macro for ==
	KeyEqual = "=="
	// KeyNotEqual macro for !=
	KeyNotEqual = "!="
	// KeyContains macro for %=
	KeyContains = "%="
	// DefaultLength default length of random string
	DefaultLength = 16
	// PartLength default length.
	PartLength = 3
)
View Source
const (
	// Letters for random string.
	Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	// ExpandFactor is the number of times to expand the rule
	ExpandFactor = 1
)

Variables

View Source
var (
	// ErrFuncArgType the error of func argument
	ErrFuncArgType = fmt.Errorf("func argument wrong")
)

Functions

func CheckRule

func CheckRule(raw string) error

CheckRule validates a given rule string and returns an error if it is invalid.

func CompareRules

func CompareRules(mr string, sr string) bool

CompareRules Compare two rules mr: rule corresponding to local node sr: rule corresponding to remote node return: true if mr contains sr

func CopyTx

func CopyTx(src *common.Transaction) *common.Transaction

CopyTx Duplicate a transaction, copying only the fields of interest.

func LoadNotEquals

func LoadNotEquals(cs []*Converge) [][]string

LoadNotEquals load not equals conditions

func NewRandomString

func NewRandomString(length int) string

NewRandomString Create a random string of specified length

func NewTx

func NewTx() *common.Transaction

NewTx Create a transaction, mainly to initialize some fields in the transaction.

func SetContracts

func SetContracts(r *Converge, baseDatas []*common.Transaction, nes []string) []*common.Transaction

SetContracts Generate a transaction set based on the input 'baseDatas' and the 'contracts' rules. The transaction set includes all parts corresponding to the CONTRACT properties.

func SetIds

func SetIds(r *Converge, baseDatas []*common.Transaction, nes []string) []*common.Transaction

SetIds Generate a transaction set based on the input 'baseDatas' and the 'ids' rules. The transaction set includes all parts corresponding to the ID properties.

func SetOrgs

func SetOrgs(r *Converge, baseDatas []*common.Transaction, nes []string) []*common.Transaction

SetOrgs Generate a transaction set based on the input 'baseDatas' and the 'orgs' rules. The transaction set includes all parts corresponding to the ORG properties.

func SetTxIds

func SetTxIds(r *Converge, baseDatas []*common.Transaction, nes []string) []*common.Transaction

SetTxIds Generate a transaction set based on the input 'baseDatas' and the 'txIds' rules. The transaction set includes all parts corresponding to the TXID properties.

func SetTxTypes

func SetTxTypes(r *Converge, baseDatas []*common.Transaction, nes []string) []*common.Transaction

SetTxTypes Generate a transaction set based on the input 'baseDatas' and the 'txTypes' rules. The transaction set includes all parts corresponding to the TXTYPE properties.

Types

type ArrayIdent

type ArrayIdent struct {
	// contains filtered or unexported fields
}

ArrayIdent is a array expression.

func (*ArrayIdent) Check

func (i *ArrayIdent) Check() error

Check checks if the array expression is valid.

type ContainOfFunc

type ContainOfFunc struct {
	// contains filtered or unexported fields
}

ContainOfFunc the function of contain_of.

func (*ContainOfFunc) Check

func (l *ContainOfFunc) Check() error

Check check for grammatical errors

func (*ContainOfFunc) Parse

func (l *ContainOfFunc) Parse() (TxSpecification, error)

Parse the ContainOfFunc to TxSpecification

type ContractMatcher

type ContractMatcher struct {
	// contains filtered or unexported fields
}

ContractMatcher the matcher used to match the contract.

func (*ContractMatcher) MatchTx

func (m *ContractMatcher) MatchTx(tx *common.Transaction) bool

MatchTx determines if a given transaction matches the contract name or contract name and method as specified by the ContractMatcher's matcher.

tx: A pointer to a common.Transaction object representing a transaction. bool: A boolean value indicating if the transaction matches the contract name or contract name and method.

type Converge

type Converge struct {
	Orgs      map[string][]string
	Ids       map[string][]string
	TxTypes   map[string][]string
	TxIds     map[string][]string
	Contracts map[string][]string
}

Converge This is a rule definition parsed from ast.Expr. There is an 'OR' relationship between different Converges, while there is an 'AND' relationship between fields within the same Converge.

func NewConverge

func NewConverge() *Converge

NewConverge Create a Converge.

func Parse2Converges

func Parse2Converges(expr ast.Expr) []*Converge

Parse2Converges transforms expr into a collection of Converge

func (*Converge) AppendContract

func (r *Converge) AppendContract(op, contract string)

AppendContract append contract name data

func (*Converge) AppendId

func (r *Converge) AppendId(op, id string)

AppendId append identity data

func (*Converge) AppendOrg

func (r *Converge) AppendOrg(op, org string)

AppendOrg append org data

func (*Converge) AppendTxId

func (r *Converge) AppendTxId(op, txId string)

AppendTxId append transaction id data

func (*Converge) AppendTxType

func (r *Converge) AppendTxType(op, txType string)

AppendTxType append transaction type data

func (*Converge) Copy

func (r *Converge) Copy() *Converge

Copy copy a new Converge, it is important to note that all fields in the new Converge are deep copied.

func (*Converge) Merge

func (r *Converge) Merge(rd *Converge) *Converge

Merge Merge two Converge together.

func (*Converge) ToDatas

func (r *Converge) ToDatas() []*common.Transaction

ToDatas Convert Converge to a transaction set. For each Converge, it may generate multiple transaction records depending on its rule attribute settings. In general, the inclusion property will affect the generation of multiple records, while the equal and not equal properties will not.

func (*Converge) ToDatasWithNotEqual

func (r *Converge) ToDatasWithNotEqual(neps [][]string) []*common.Transaction

ToDatasWithNotEqual Convert Converge to a transaction set. This method is different from ToDatas. It has an input called 'neps', which suggests the equal value when generating transaction data, mainly used for comparison. However, it should be noted that compared to 'neps', one's own not equal permission is higher.

func (*Converge) ToString

func (r *Converge) ToString() string

ToString Convert Converge to a string that can be visually inspected.

type ErrIdent

type ErrIdent struct {
	// contains filtered or unexported fields
}

ErrIdent represents an error caused by an invalid label.

func (ErrIdent) Error

func (r ErrIdent) Error() string

Error returns a string representation of the error.

type ErrKind

type ErrKind struct {
	// contains filtered or unexported fields
}

ErrKind represents an error caused by an invalid token.

func (ErrKind) Error

func (r ErrKind) Error() string

Error returns a string representation of ErrKind.

type ErrStatement

type ErrStatement struct {
	// contains filtered or unexported fields
}

ErrStatement is a custom error type that represents a problem with a statement.

func (ErrStatement) Error

func (r ErrStatement) Error() string

Error returns the error message for ErrStatement.

type ErrToken

type ErrToken struct {
	// contains filtered or unexported fields
}

ErrToken represents an error type for invalid tokens in a lexer.

func (ErrToken) Error

func (r ErrToken) Error() string

Error returns a string representation of the ErrToken error.

type FunNameIdent

type FunNameIdent ast.Ident

FunNameIdent the ident of function name.

func (*FunNameIdent) Check

func (l *FunNameIdent) Check() error

Check check for grammatical errors.

type FuncArgIdent

type FuncArgIdent struct {
	// contains filtered or unexported fields
}

FuncArgIdent the ident of function argument.

type FuncIdent

type FuncIdent ast.CallExpr

FuncIdent function ident.

func (FuncIdent) Check

func (l FuncIdent) Check() error

Check check for grammatical errors

func (FuncIdent) Parse

func (l FuncIdent) Parse() (TxSpecification, error)

Parse the FuncIdent to TxSpecification.

type IdentityMatcher

type IdentityMatcher struct {
	// contains filtered or unexported fields
}

IdentityMatcher the matcher used to match the identity.

func (*IdentityMatcher) MatchTx

func (m *IdentityMatcher) MatchTx(tx *common.Transaction) bool

MatchTx checks if the given transaction matches the identity matcher.

tx: pointer to common.Transaction that needs to be matched returns a boolean indicating if the transaction matches or not

type InArrayFunc

type InArrayFunc struct {
	// contains filtered or unexported fields
}

InArrayFunc the function of in_array

func (*InArrayFunc) Check

func (l *InArrayFunc) Check() error

Check check for grammatical errors

func (*InArrayFunc) Parse

func (l *InArrayFunc) Parse() (TxSpecification, error)

Parse the InArrayFunc to TxSpecification

type LabelIdent

type LabelIdent ast.Ident

LabelIdent is the rule lable.

func (*LabelIdent) Check

func (l *LabelIdent) Check() error

Check checks if the rule lable is a legal lable(such as O, I, C, TT, TD)

type LeafIdent

type LeafIdent ast.BinaryExpr

LeafIdent is a leaf expression, such as O=="ORG1", 'O' is the rule lable, '==' is the operator, 'ORG1' is the value.

func (*LeafIdent) Check

func (l *LeafIdent) Check() error

Check checks if the leaf expression is valid. Such as O=="ORG1", 'O' is the rule lable, '==' is the operator, 'ORG1' is the value.

func (*LeafIdent) Parse

func (l *LeafIdent) Parse() (TxSpecification, error)

Parse parses the leaf expression to TxSpecification.

type LeafOperator

type LeafOperator struct {
	// contains filtered or unexported fields
}

LeafOperator is a leaf operator, such as O=="ORG1", '==' is the operator

func (LeafOperator) Check

func (l LeafOperator) Check() error

Check checks if the leaf operator is valid.

type NormalizedLabel

type NormalizedLabel string

NormalizedLabel is a normalized label.

const (
	ORG_LABEL      NormalizedLabel = "O"
	ID_LABEL       NormalizedLabel = "I"
	TX_TYPE_LABEL  NormalizedLabel = "TT"
	TX_ID_LABEL    NormalizedLabel = "TD"
	CONTRACT_LABEL NormalizedLabel = "C"
)

组织:O,身份:I,交易类型:TT,交易ID:TD, 合约:C

func (NormalizedLabel) String

func (l NormalizedLabel) String() string

String returns the string representation of the NormalizedLabel.

type OrgMatcher

type OrgMatcher struct {
	// contains filtered or unexported fields
}

OrgMatcher the matcher used to match the order label.

func (*OrgMatcher) MatchTx

func (m *OrgMatcher) MatchTx(tx *common.Transaction) bool

MatchTx checks if the organization ID of the transaction sender matches the pattern specified in the OrgMatcher.

tx: A pointer to a common.Transaction object. Returns a boolean value representing whether the organization ID matches or not.

type RegMatcher

type RegMatcher struct {
	// contains filtered or unexported fields
}

RegMatcher is a struct used to match regular expressions.

func (*RegMatcher) MatchV

func (r *RegMatcher) MatchV(v interface{}) bool

MatchV checks if the given interface is a string and matches the regular expression defined in the RegMatcher struct.

v: The interface to be checked. bool: Returns true if the interface is a string and matches the regular expression.

type Specification

type Specification struct {
	// contains filtered or unexported fields
}

Specification is a TxSpecification interface implementation. Specification only implement logic operations.

func (*Specification) IsSpecifiedTx

func (s *Specification) IsSpecifiedTx(tx *common.Transaction) bool

IsSpecifiedTx checks if the provided transaction matches the specification. tx: *common.Transaction - the transaction to check. bool - returns true if the transaction matches the specification,

type TxIDMatcher

type TxIDMatcher struct {
	// contains filtered or unexported fields
}

TxIDMatcher the matcher used to match the transaction ID.

func (*TxIDMatcher) MatchTx

func (m *TxIDMatcher) MatchTx(tx *common.Transaction) bool

MatchTx is a function that takes a pointer to a TxIDMatcher struct and a pointer to a common Transaction struct as its parameters. It returns a bool value. The function matches the transaction ID of the specified transaction using the matcher.MatchV method.

type TxMatcher

type TxMatcher interface {
	MatchTx(tx *common.Transaction) bool
}

TxMatcher is the interface used to match transaction.

type TxSpecification

type TxSpecification interface {
	// IsSpecifiedTx checks if a given transaction meets the specification
	IsSpecifiedTx(*common.Transaction) bool
	// contains filtered or unexported methods
}

TxSpecification is an interface for transaction specifications

func ParseRule

func ParseRule(raw string) (TxSpecification, error)

ParseRule parses a string and returns a TxSpecification or an error.

type TxSpecificationFunc

type TxSpecificationFunc func(*common.Transaction) bool

TxSpecificationFunc is a function type that implements TxSpecification interface.

func (TxSpecificationFunc) IsSpecifiedTx

func (f TxSpecificationFunc) IsSpecifiedTx(tx *common.Transaction) bool

IsSpecifiedTx wrapper function call implements IsSpecifiedTx interface function.

type TxTypeMatcher

type TxTypeMatcher struct {
	// contains filtered or unexported fields
}

TxTypeMatcher the matcher used to match the transaction type.

func (*TxTypeMatcher) MatchTx

func (m *TxTypeMatcher) MatchTx(tx *common.Transaction) bool

MatchTx returns true if the payload of the given transaction matches the TxTypeMatcher.

tx: A pointer to a common.Transaction struct. bool: A boolean indicating whether the transaction's payload matches the TxTypeMatcher.

type ValueIdent

type ValueIdent ast.BasicLit

ValueIdent is the value of the leaf expression

func (*ValueIdent) Check

func (l *ValueIdent) Check() error

Check checks if the type of the value is supported.

func (*ValueIdent) Parse

func (l *ValueIdent) Parse() (interface{}, error)

Parse the value according to the type.

Source Files

  • error.go
  • function.go
  • parser.go
  • rule.go
  • rule_data.go
  • rule_func.go
  • rule_util.go
  • types.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL