2022-01-24 01:17:27 +01:00
|
|
|
# standard imports
|
|
|
|
import logging
|
2022-02-27 12:48:15 +01:00
|
|
|
import uuid
|
2022-01-24 01:17:27 +01:00
|
|
|
|
|
|
|
# external imports
|
|
|
|
from chainlib.eth.address import is_same_address
|
|
|
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
2022-04-05 13:44:15 +02:00
|
|
|
class RuleMethod:
|
|
|
|
|
|
|
|
def __init__(self, methods, description=None):
|
|
|
|
self.methods = methods
|
|
|
|
self.description = description
|
|
|
|
if self.description == None:
|
|
|
|
self.description = str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
|
|
def check(self, sender, recipient, data, tx_hash):
|
|
|
|
if len(self.methods) == 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
for method in self.methods:
|
|
|
|
l = len(method)
|
|
|
|
if len(method) > len(data):
|
|
|
|
continue
|
|
|
|
if data[:l] == method:
|
|
|
|
logg.debug('tx {} rule {} match in DATA {}'.format(tx_hash, self.description, method))
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'Method ' + self.description + ' {}'.format(
|
|
|
|
self.methods,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-02-27 12:48:15 +01:00
|
|
|
class RuleSimple:
|
|
|
|
|
|
|
|
def __init__(self, outputs, inputs, executables, description=None):
|
|
|
|
self.description = description
|
|
|
|
if self.description == None:
|
|
|
|
self.description = str(uuid.uuid4())
|
|
|
|
self.outputs = outputs
|
|
|
|
self.inputs = inputs
|
|
|
|
self.executables = executables
|
|
|
|
|
|
|
|
|
2022-04-05 13:44:15 +02:00
|
|
|
def check(self, sender, recipient, data, tx_hash):
|
2022-02-27 12:48:15 +01:00
|
|
|
for rule in self.outputs:
|
|
|
|
if rule != None and is_same_address(sender, rule):
|
2022-04-05 13:44:15 +02:00
|
|
|
logg.debug('tx {} rule {} match in SENDER {}'.format(tx_hash, self.description, sender))
|
2022-02-27 12:48:15 +01:00
|
|
|
return True
|
|
|
|
for rule in self.inputs:
|
|
|
|
if rule != None and is_same_address(recipient, rule):
|
2022-04-05 13:44:15 +02:00
|
|
|
logg.debug('tx {} rule {} match in RECIPIENT {}'.format(tx_hash, self.description, recipient))
|
2022-02-27 12:48:15 +01:00
|
|
|
return True
|
|
|
|
for rule in self.executables:
|
|
|
|
if rule != None and is_same_address(recipient, rule):
|
2022-04-05 13:44:15 +02:00
|
|
|
logg.debug('tx {} rule {} match in EXECUTABLE {}'.format(tx_hash, self.description, recipient))
|
2022-02-27 12:48:15 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'Simple ' + self.description + ' outputs {} inputs {} execs {}'.format(
|
|
|
|
self.outputs,
|
|
|
|
self.inputs,
|
|
|
|
self.executables,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-23 18:49:04 +01:00
|
|
|
class AddressRules:
|
|
|
|
|
|
|
|
def __init__(self, include_by_default=False):
|
|
|
|
self.excludes = []
|
|
|
|
self.includes = []
|
|
|
|
self.include_by_default = include_by_default
|
|
|
|
|
|
|
|
|
2022-02-27 12:48:15 +01:00
|
|
|
def exclude(self, rule):
|
|
|
|
self.excludes.append(rule)
|
|
|
|
logg.info('cache filter added EXCLUDE rule {}'.format(rule))
|
2022-01-23 18:49:04 +01:00
|
|
|
|
2022-02-27 12:48:15 +01:00
|
|
|
|
|
|
|
def include(self, rule):
|
|
|
|
self.includes.append(rule)
|
2022-04-05 13:44:15 +02:00
|
|
|
logg.info('cache filter added INCLUDE rule {}'.format(rule))
|
2022-01-23 18:49:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
def apply_rules(self, tx):
|
2022-04-05 13:44:15 +02:00
|
|
|
return self.apply_rules_addresses(tx.outputs[0], tx.inputs[0], tx.payload, tx.hash)
|
2022-01-24 01:17:27 +01:00
|
|
|
|
|
|
|
|
2022-04-05 13:44:15 +02:00
|
|
|
# TODO: rename
|
|
|
|
def apply_rules_addresses(self, sender, recipient, data, tx_hash):
|
2022-01-24 01:17:27 +01:00
|
|
|
v = self.include_by_default
|
2022-01-23 18:49:04 +01:00
|
|
|
|
|
|
|
for rule in self.includes:
|
2022-04-05 13:44:15 +02:00
|
|
|
if rule.check(sender, recipient, data, tx_hash):
|
2022-01-23 18:49:04 +01:00
|
|
|
v = True
|
2022-02-27 12:48:15 +01:00
|
|
|
logg.info('match in includes rule: {}'.format(rule))
|
|
|
|
break
|
|
|
|
|
2022-01-23 18:49:04 +01:00
|
|
|
for rule in self.excludes:
|
2022-04-05 13:44:15 +02:00
|
|
|
if rule.check(sender, recipient, data, tx_hash):
|
2022-01-23 18:49:04 +01:00
|
|
|
v = False
|
2022-02-27 12:48:15 +01:00
|
|
|
logg.info('match in excludes rule: {}'.format(rule))
|
|
|
|
break
|
|
|
|
|
2022-01-23 18:49:04 +01:00
|
|
|
return v
|