2022-01-23 18:49:04 +01:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
import logging
|
2022-01-23 23:07:59 +01:00
|
|
|
import json
|
2022-01-23 18:49:04 +01:00
|
|
|
|
|
|
|
# external imports
|
2022-01-23 19:07:58 +01:00
|
|
|
from hexathon import strip_0x
|
2022-03-30 10:11:46 +02:00
|
|
|
from chainsyncer.filter import SyncFilter
|
2022-01-23 18:49:04 +01:00
|
|
|
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-03-30 10:11:46 +02:00
|
|
|
class RuledFilter(SyncFilter):
|
2022-01-23 18:49:04 +01:00
|
|
|
|
|
|
|
def __init__(self, rules_filter=None):
|
2022-01-23 23:44:31 +01:00
|
|
|
if self.store.chain_dir == None:
|
|
|
|
raise RuntimeError('store must be initialized. call RuledFilter.init() first')
|
2022-01-23 18:49:04 +01:00
|
|
|
self.rules_filter = rules_filter
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2022-01-23 23:44:31 +01:00
|
|
|
def init(store, include_block_data=False, include_tx_data=False):
|
|
|
|
RuledFilter.store = store
|
2022-01-23 23:07:59 +01:00
|
|
|
RuledFilter.include_block_data = include_block_data
|
|
|
|
RuledFilter.include_tx_data = include_tx_data
|
2022-01-23 18:49:04 +01:00
|
|
|
|
|
|
|
|
2022-01-23 19:07:58 +01:00
|
|
|
@classmethod
|
|
|
|
def block_callback(cls, block, extra=None):
|
2022-01-30 15:43:53 +01:00
|
|
|
logg.info('processing {}'.format(block))
|
2022-01-23 23:44:31 +01:00
|
|
|
cls.store.put_block(block, include_data=cls.include_block_data)
|
2022-01-23 19:07:58 +01:00
|
|
|
|
|
|
|
|
2022-01-23 18:49:04 +01:00
|
|
|
def filter(self, conn, block, tx, db_session=None):
|
|
|
|
if self.rules_filter != None:
|
|
|
|
if not self.rules_filter.apply_rules(tx):
|
|
|
|
logg.debug('rule match failed for tx {}'.format(tx.hash))
|
2022-03-30 10:11:46 +02:00
|
|
|
return True
|
|
|
|
return False
|