2022-01-30 15:43:53 +01:00
|
|
|
|
# standard imports
|
|
|
|
|
import sys
|
2022-01-30 20:44:03 +01:00
|
|
|
|
import logging
|
2022-04-05 13:44:15 +02:00
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
|
from hexathon import (
|
|
|
|
|
strip_0x,
|
|
|
|
|
)
|
2022-01-30 15:43:53 +01:00
|
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
|
from .base import RuledFilter
|
|
|
|
|
|
2022-01-30 20:44:03 +01:00
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Interface defining the signature for renderer in OutFilter
|
|
|
|
|
# return string after local transformation
|
2022-11-10 09:07:08 +01:00
|
|
|
|
def apply_interface(c, s, chain_str, conn, block, tx, **kwargs):
|
2022-01-30 20:44:03 +01:00
|
|
|
|
pass
|
|
|
|
|
|
2022-01-30 15:43:53 +01:00
|
|
|
|
|
2022-02-27 14:52:05 +01:00
|
|
|
|
class OutResult:
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.content = ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set(self, v):
|
|
|
|
|
self.content = v
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
|
return self.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.content
|
|
|
|
|
|
|
|
|
|
|
2022-01-30 15:43:53 +01:00
|
|
|
|
class OutFilter(RuledFilter):
|
|
|
|
|
|
2022-01-30 20:44:03 +01:00
|
|
|
|
def __init__(self, chain_spec, writer=sys.stdout, renderers=[], rules_filter=None):
|
2022-01-30 15:43:53 +01:00
|
|
|
|
super(OutFilter, self).__init__(rules_filter=rules_filter)
|
|
|
|
|
self.w = writer
|
|
|
|
|
self.renderers = renderers
|
|
|
|
|
self.c = 0
|
2022-01-30 20:44:03 +01:00
|
|
|
|
self.chain_spec = chain_spec
|
2022-02-27 14:52:05 +01:00
|
|
|
|
self.result = OutResult()
|
2022-01-30 20:44:03 +01:00
|
|
|
|
|
|
|
|
|
|
2022-11-10 09:07:08 +01:00
|
|
|
|
def filter(self, conn, block, tx, **kwargs):
|
|
|
|
|
r = super(OutFilter, self).filter(conn, block, tx, **kwargs)
|
2022-03-30 10:11:46 +02:00
|
|
|
|
if r == True:
|
2022-02-27 12:48:15 +01:00
|
|
|
|
return True
|
|
|
|
|
|
2022-01-30 20:44:03 +01:00
|
|
|
|
for renderer in self.renderers:
|
2022-02-27 14:52:05 +01:00
|
|
|
|
r = renderer.apply(self.c, self.result, self.chain_spec, conn, block, tx)
|
|
|
|
|
if not r:
|
2022-01-30 20:44:03 +01:00
|
|
|
|
break
|
2022-01-30 15:43:53 +01:00
|
|
|
|
|
2022-02-27 14:52:05 +01:00
|
|
|
|
s = str(self.result)
|
|
|
|
|
|
|
|
|
|
if s == '':
|
2022-01-30 20:44:03 +01:00
|
|
|
|
data = tx.payload
|
|
|
|
|
if len(data) > 8:
|
|
|
|
|
data = data[:8] + '...'
|
|
|
|
|
if len(data) > 0:
|
|
|
|
|
data = 'data {}'.format(data)
|
2022-04-05 13:44:15 +02:00
|
|
|
|
#s = '{} {} {} {}'.format(self.c, block, tx, data)
|
|
|
|
|
tx_count = len(block.txs)
|
2022-04-05 16:02:18 +02:00
|
|
|
|
s = '{} {} block {} {} tx {}/{} {} {} {}'.format(
|
2022-04-05 13:44:15 +02:00
|
|
|
|
self.c,
|
|
|
|
|
datetime.datetime.fromtimestamp(block.timestamp),
|
|
|
|
|
block.number,
|
|
|
|
|
strip_0x(block.hash),
|
|
|
|
|
tx.index,
|
|
|
|
|
tx_count,
|
|
|
|
|
strip_0x(tx.hash),
|
2022-04-09 21:17:16 +02:00
|
|
|
|
tx.status.name,
|
2022-04-05 13:44:15 +02:00
|
|
|
|
data,
|
|
|
|
|
)
|
2022-01-30 15:43:53 +01:00
|
|
|
|
|
2022-01-30 20:44:03 +01:00
|
|
|
|
self.w.write(s + '\n')
|
2022-01-30 15:43:53 +01:00
|
|
|
|
self.c += 1
|
2022-03-30 10:11:46 +02:00
|
|
|
|
return False
|