cic-internal-integration/apps/cic-eth/cic_eth/runnable/daemons/filters/gas.py

64 lines
1.9 KiB
Python
Raw Normal View History

2021-02-17 09:19:42 +01:00
# standard imports
import logging
# external imports
2021-04-04 14:40:59 +02:00
from hexathon import (
add_0x,
strip_0x,
)
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.query import get_paused_tx_cache as get_paused_tx
2021-02-17 09:19:42 +01:00
# local imports
from cic_eth.db.models.base import SessionBase
from cic_eth.eth.gas import create_check_gas_task
2021-02-17 09:19:42 +01:00
from .base import SyncFilter
logg = logging.getLogger().getChild(__name__)
2021-02-17 09:19:42 +01:00
class GasFilter(SyncFilter):
2021-03-01 21:15:17 +01:00
def __init__(self, chain_spec, queue=None):
self.queue = queue
2021-03-01 21:15:17 +01:00
self.chain_spec = chain_spec
2021-02-17 09:19:42 +01:00
def filter(self, conn, block, tx, session):
2021-03-01 21:15:17 +01:00
if tx.value > 0:
2021-04-04 14:40:59 +02:00
tx_hash_hex = add_0x(tx.hash)
2021-02-17 09:19:42 +01:00
logg.debug('gas refill tx {}'.format(tx_hash_hex))
session = SessionBase.bind_session(session)
2021-02-17 09:19:42 +01:00
q = session.query(TxCache.recipient)
q = q.join(Otx)
2021-04-04 14:40:59 +02:00
q = q.filter(Otx.tx_hash==strip_0x(tx_hash_hex))
2021-02-17 09:19:42 +01:00
r = q.first()
if r == None:
2021-03-01 21:15:17 +01:00
logg.debug('unsolicited gas refill tx {}'.format(tx_hash_hex))
SessionBase.release_session(session)
2021-02-17 09:19:42 +01:00
return
2021-04-04 14:40:59 +02:00
txs = get_paused_tx(self.chain_spec, status=StatusBits.GAS_ISSUES, sender=r[0], session=session, decoder=unpack)
SessionBase.release_session(session)
2021-02-17 09:19:42 +01:00
2021-03-01 21:15:17 +01:00
logg.info('resuming gas-in-waiting txs for {}'.format(r[0]))
2021-02-17 09:19:42 +01:00
if len(txs) > 0:
s = create_check_gas_task(
2021-02-17 09:19:42 +01:00
list(txs.values()),
self.chain_spec,
2021-02-17 09:19:42 +01:00
r[0],
0,
tx_hashes_hex=list(txs.keys()),
queue=self.queue,
2021-02-17 09:19:42 +01:00
)
s.apply_async()
2021-03-01 21:15:17 +01:00
def __str__(self):
return 'eic-eth gasfilter'