WIP remove db session from filters

This commit is contained in:
lash 2022-04-23 09:58:17 +00:00
parent cdb26b558f
commit 38d5ca3826
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
17 changed files with 43 additions and 44 deletions

1
MANIFEST.in Normal file
View File

@ -0,0 +1 @@
include *requirements.txt

View File

@ -1,10 +1,12 @@
# external imports
from chainlib.eth.constant import ZERO_ADDRESS
from chainsyncer.filter import SyncFilter as BaseSyncFilter
class SyncFilter:
class SyncFilter(BaseSyncFilter):
def __init__(self, chain_spec, registry, queue, caller_address=ZERO_ADDRESS):
super(SyncFilter, self).__init__()
self.exec_count = 0
self.match_count = 0
self.queue = queue
@ -13,7 +15,7 @@ class SyncFilter:
self.caller_address = caller_address
def filter(self, conn, block, tx, db_session):
def filter(self, conn, block, tx):
self.exec_count += 1

View File

@ -80,8 +80,8 @@ class CallbackFilter(SyncFilter):
return (transfer_type, transfer_data)
def filter(self, conn, block, tx, db_session=None):
super(CallbackFilter, self).filter(conn, block, tx, db_session)
def filter(self, conn, block, tx):
super(CallbackFilter, self).filter(conn, block, tx)
transfer_data = None
transfer_type = None
try:

View File

@ -8,14 +8,15 @@ from hexathon import (
)
from chainlib.eth.tx import unpack
from chainqueue.db.enum import StatusBits
from chainqueue.db.models.tx import TxCache
from chainqueue.db.models.otx import Otx
#from chainqueue.db.models.tx import TxCache
#from chainqueue.db.models.otx import Otx
from chainlib.eth.address import to_checksum_address
# local imports
from cic_eth.db.models.base import SessionBase
#from cic_eth.db.models.base import SessionBase
from cic_eth.queue.query import get_account_tx_local
from cic_eth.eth.gas import create_check_gas_task
from cic_eth.queue.query import get_paused_tx
# local imports
from cic_eth.encode import tx_normalize
from .base import SyncFilter
@ -24,30 +25,24 @@ logg = logging.getLogger()
class GasFilter(SyncFilter):
def filter(self, conn, block, tx, db_session):
super(GasFilter, self).filter(conn, block, tx, db_session)
def filter(self, conn, block, tx):
super(GasFilter, self).filter(conn, block, tx)
if tx.value > 0 or len(tx.payload) == 0:
tx_hash_hex = add_0x(tx.hash)
sender_target = tx_normalize.wallet_address(tx.inputs[0])
session = SessionBase.bind_session(db_session)
q = session.query(TxCache.recipient)
q = q.filter(TxCache.sender==sender_target)
r = q.all()
txc = get_account_tx_local(self.chain_spec, sender_target, as_recipient=False)
logline = None
if len(r) == 0:
logline = 'unsolicited gas refill tx {}; cannot find {} among senders'.format(tx_hash_hex, tx.outputs[0])
if len(txc) == 0:
logline = 'unsolicited gas refill tx {}; cannot find {} among senders'.format(tx_hash_hex, sender_target)
logline = self.to_logline(block, tx, logline)
logg.info(logline)
SessionBase.release_session(session)
return None
self.register_match()
txs = get_paused_tx(self.chain_spec, status=StatusBits.GAS_ISSUES, sender=sender_target, session=session, decoder=unpack)
SessionBase.release_session(session)
txs = get_paused_tx(self.chain_spec, status=StatusBits.GAS_ISSUES, sender=sender_target, decoder=unpack)
t = None
address = to_checksum_address(sender_target)

View File

@ -3,7 +3,7 @@ from .base import SyncFilter
class LogFilter(SyncFilter):
def filter(self, conn, block, tx, db_session=None):
def filter(self, conn, block, tx):
logg.debug('block {} tx {}'.format(block, tx))

View File

@ -25,8 +25,8 @@ class RegistrationFilter(SyncFilter):
self.contract_address = registry.by_name('AccountRegistry', sender_address=caller_address)
def filter(self, conn, block, tx, db_session=None):
super(RegistrationFilter, self).filter(conn, block, tx, db_session)
def filter(self, conn, block, tx):
super(RegistrationFilter, self).filter(conn, block, tx)
if self.contract_address != tx.inputs[0]:
logg.debug('not an account registry tx; {} != {}'.format(self.contract_address, tx.inputs[0]))
return None

View File

@ -23,8 +23,8 @@ class StragglerFilter(SyncFilter):
gas_balance_threshold = 0
def filter(self, conn, block, tx, db_session=None):
txc = get_tx_cache(self.chain_spec, tx.hash, session=db_session)
def filter(self, conn, block, tx):
txc = get_tx_cache(self.chain_spec, tx.hash)
if txc['status_code'] & StatusBits.GAS_ISSUES > 0:
o = balance(tx.outputs[0])
r = conn.do(o)

View File

@ -24,8 +24,8 @@ logg = logging.getLogger(__name__)
class TokenFilter(SyncFilter):
def filter(self, conn, block, tx, db_session=None):
super(TokenFilter, self).filter(conn, block, tx, db_session)
def filter(self, conn, block, tx):
super(TokenFilter, self).filter(conn, block, tx)
if not tx.payload:
return None

View File

@ -33,7 +33,8 @@ class TransferAuthFilter(SyncFilter):
self.transfer_request_contract = registry.by_name('TransferAuthorization', sender_address=call_address)
def filter(self, conn, block, tx, db_session): #rcpt, chain_str, session=None):
def filter(self, conn, block, tx):
super(TransferAuthFilter, self).filter(conn, block, tx)
if tx.payload == None:
logg.debug('no payload')

View File

@ -18,8 +18,8 @@ logg = logging.getLogger(__name__)
class TxFilter(SyncFilter):
def filter(self, conn, block, tx, db_session=None):
super(TxFilter, self).filter(conn, block, tx, db_session=db_session)
def filter(self, conn, block, tx):
super(TxFilter, self).filter(conn, block)
try:
get_tx_local(self.chain_spec, tx.hash)

View File

@ -40,7 +40,7 @@ def test_filter_bogus(
for fltr in fltrs:
r = None
try:
r = fltr.filter(eth_rpc, bogus_tx_block[0], bogus_tx_block[1], db_session=init_database)
r = fltr.filter(eth_rpc, bogus_tx_block[0], bogus_tx_block[1])
except:
pass
assert not r

View File

@ -106,7 +106,7 @@ def test_filter_gas(
for v in r:
logg.info('have row {}'.format(v))
t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
t = fltr.filter(eth_rpc, block, tx)
t.get_leaf()
assert t.successful()

View File

@ -80,7 +80,7 @@ def test_register_filter(
registry = CICRegistry(default_chain_spec, eth_rpc)
queue = None
fltr = RegistrationFilter(default_chain_spec, registry, queue, caller_address=contract_roles['CONTRACT_DEPLOYER'])
t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
t = fltr.filter(eth_rpc, block, tx)
assert t != None
t.get_leaf()
assert t.successful()
@ -139,5 +139,5 @@ def test_register_filter_nomatch(
registry = CICRegistry(default_chain_spec, eth_rpc)
queue = None
fltr = RegistrationFilter(default_chain_spec, registry, queue, caller_address=contract_roles['CONTRACT_DEPLOYER'])
t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
t = fltr.filter(eth_rpc, block, tx)
assert t == None

View File

@ -96,7 +96,7 @@ def test_straggler_tx(
tx_signed_raw_bytes = bytes.fromhex(strip_0x(tx_signed_raw_hex))
tx_src = unpack(tx_signed_raw_bytes, default_chain_spec)
tx = Tx(tx_src, block=block)
t = fltr.filter(None, block, tx, db_session=init_database)
t = fltr.filter(None, block, tx)
logg.debug('foo')
tx_hash_hex_successor = t.get_leaf()
logg.debug('bar')
@ -163,7 +163,7 @@ def test_waitforgas_tx(
tx_src = unpack(tx_signed_raw_bytes, default_chain_spec)
tx = Tx(tx_src, block=block)
t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
t = fltr.filter(eth_rpc, block, tx)
t.get_leaf()
assert t.successful()
@ -181,7 +181,7 @@ def test_waitforgas_tx(
assert r['status'] == 1
t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
t = fltr.filter(eth_rpc, block, tx)
t.get_leaf()
assert t.successful()
@ -201,7 +201,7 @@ def test_waitforgas_tx(
init_database.commit()
t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
t = fltr.filter(eth_rpc, block, tx)
t.get_leaf()
otx = get_tx_local(default_chain_spec, tx.hash, session=init_database)

View File

@ -73,7 +73,7 @@ def test_filter_gas(
tx_src = unpack(tx_signed_raw_bytes, default_chain_spec)
tx = Tx(tx_src, block=block)
tx.apply_receipt(rcpt)
t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
t = fltr.filter(eth_rpc, block, tx)
assert t.get() == None
nonce_oracle = RPCNonceOracle(contract_roles['CONTRACT_DEPLOYER'], eth_rpc)
@ -84,7 +84,7 @@ def test_filter_gas(
r = eth_rpc.do(o)
assert r['status'] == 1
t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
t = fltr.filter(eth_rpc, block, tx)
r = t.get_leaf()
assert t.successful()
@ -127,4 +127,4 @@ def test_filter_unknown_contract_error(
tx_signed_raw_bytes = bytes.fromhex(strip_0x(tx_signed_raw_hex))
tx_src = unpack(tx_signed_raw_bytes, default_chain_spec)
tx = Tx(tx_src)
t = fltr.filter(eth_rpc, None, tx, db_session=init_database)
t = fltr.filter(eth_rpc, None, tx)

View File

@ -62,7 +62,7 @@ def test_filter_transferauth(
tx = Tx(tx_src, block=block)
fltr = TransferAuthFilter(cic_registry, default_chain_spec, eth_rpc, call_address=contract_roles['CONTRACT_DEPLOYER'])
t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
t = fltr.filter(eth_rpc, block, tx)
t.get_leaf()
assert t.successful()

View File

@ -97,7 +97,7 @@ def test_filter_tx(
tx_signed_raw_bytes = bytes.fromhex(strip_0x(tx_signed_raw_hex))
tx_src = unpack(tx_signed_raw_bytes, default_chain_spec)
tx = Tx(tx_src, block=block)
t = fltr.filter(eth_rpc, block, tx, db_session=init_database)
t = fltr.filter(eth_rpc, block, tx)
t.get()
assert t.successful()