Add gas filter test
This commit is contained in:
parent
13561378ab
commit
7178373038
@ -177,7 +177,6 @@ def check_gas(self, tx_hashes, chain_spec_dict, txs=[], address=None, gas_requir
|
|||||||
if address == None:
|
if address == None:
|
||||||
address = o['address']
|
address = o['address']
|
||||||
|
|
||||||
#if not web3.Web3.isChecksumAddress(address):
|
|
||||||
if not is_checksum_address(address):
|
if not is_checksum_address(address):
|
||||||
raise ValueError('invalid address {}'.format(address))
|
raise ValueError('invalid address {}'.format(address))
|
||||||
|
|
||||||
|
@ -17,7 +17,8 @@ from cic_eth.db.models.base import SessionBase
|
|||||||
from cic_eth.eth.gas import create_check_gas_task
|
from cic_eth.eth.gas import create_check_gas_task
|
||||||
from .base import SyncFilter
|
from .base import SyncFilter
|
||||||
|
|
||||||
logg = logging.getLogger().getChild(__name__)
|
#logg = logging.getLogger().getChild(__name__)
|
||||||
|
logg = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
class GasFilter(SyncFilter):
|
class GasFilter(SyncFilter):
|
||||||
@ -27,11 +28,11 @@ class GasFilter(SyncFilter):
|
|||||||
self.chain_spec = chain_spec
|
self.chain_spec = chain_spec
|
||||||
|
|
||||||
|
|
||||||
def filter(self, conn, block, tx, session):
|
def filter(self, conn, block, tx, db_session):
|
||||||
if tx.value > 0:
|
if tx.value > 0:
|
||||||
tx_hash_hex = add_0x(tx.hash)
|
tx_hash_hex = add_0x(tx.hash)
|
||||||
logg.debug('gas refill tx {}'.format(tx_hash_hex))
|
logg.debug('gas refill tx {}'.format(tx_hash_hex))
|
||||||
session = SessionBase.bind_session(session)
|
session = SessionBase.bind_session(db_session)
|
||||||
q = session.query(TxCache.recipient)
|
q = session.query(TxCache.recipient)
|
||||||
q = q.join(Otx)
|
q = q.join(Otx)
|
||||||
q = q.filter(Otx.tx_hash==strip_0x(tx_hash_hex))
|
q = q.filter(Otx.tx_hash==strip_0x(tx_hash_hex))
|
||||||
@ -56,7 +57,7 @@ class GasFilter(SyncFilter):
|
|||||||
tx_hashes_hex=list(txs.keys()),
|
tx_hashes_hex=list(txs.keys()),
|
||||||
queue=self.queue,
|
queue=self.queue,
|
||||||
)
|
)
|
||||||
s.apply_async()
|
return s.apply_async()
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
101
apps/cic-eth/tests/filters/test_gas_filter.py
Normal file
101
apps/cic-eth/tests/filters/test_gas_filter.py
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
# external imports
|
||||||
|
from chainlib.connection import RPCConnection
|
||||||
|
from chainlib.eth.nonce import OverrideNonceOracle
|
||||||
|
from chainqueue.tx import create as queue_create
|
||||||
|
from chainlib.eth.tx import (
|
||||||
|
TxFormat,
|
||||||
|
unpack,
|
||||||
|
Tx,
|
||||||
|
)
|
||||||
|
from chainlib.eth.gas import (
|
||||||
|
Gas,
|
||||||
|
OverrideGasOracle,
|
||||||
|
)
|
||||||
|
from chainlib.eth.block import (
|
||||||
|
block_latest,
|
||||||
|
block_by_number,
|
||||||
|
Block,
|
||||||
|
)
|
||||||
|
from chainqueue.state import (
|
||||||
|
set_waitforgas,
|
||||||
|
)
|
||||||
|
from hexathon import strip_0x
|
||||||
|
from chainqueue.db.models.otx import Otx
|
||||||
|
from chainqueue.db.enum import StatusBits
|
||||||
|
|
||||||
|
# local imports
|
||||||
|
from cic_eth.runnable.daemons.filters.gas import GasFilter
|
||||||
|
from cic_eth.eth.gas import cache_gas_data
|
||||||
|
|
||||||
|
|
||||||
|
def test_filter_gas(
|
||||||
|
default_chain_spec,
|
||||||
|
init_database,
|
||||||
|
eth_rpc,
|
||||||
|
eth_signer,
|
||||||
|
agent_roles,
|
||||||
|
celery_session_worker,
|
||||||
|
):
|
||||||
|
|
||||||
|
rpc = RPCConnection.connect(default_chain_spec, 'default')
|
||||||
|
nonce_oracle = OverrideNonceOracle(agent_roles['ALICE'], 42)
|
||||||
|
gas_oracle = OverrideGasOracle(price=1000000000, limit=21000)
|
||||||
|
c = Gas(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
|
||||||
|
(tx_hash_hex, tx_signed_raw_hex) = c.create(agent_roles['ALICE'], agent_roles['BOB'], 100 * (10 ** 6), tx_format=TxFormat.RLP_SIGNED)
|
||||||
|
|
||||||
|
queue_create(
|
||||||
|
default_chain_spec,
|
||||||
|
42,
|
||||||
|
agent_roles['ALICE'],
|
||||||
|
tx_hash_hex,
|
||||||
|
tx_signed_raw_hex,
|
||||||
|
session=init_database,
|
||||||
|
)
|
||||||
|
cache_gas_data(
|
||||||
|
tx_hash_hex,
|
||||||
|
tx_signed_raw_hex,
|
||||||
|
default_chain_spec.asdict(),
|
||||||
|
)
|
||||||
|
set_waitforgas(default_chain_spec, tx_hash_hex, session=init_database)
|
||||||
|
init_database.commit()
|
||||||
|
tx_hash_hex_wait = tx_hash_hex
|
||||||
|
otx = Otx.load(tx_hash_hex_wait, session=init_database)
|
||||||
|
assert otx.status & StatusBits.GAS_ISSUES == StatusBits.GAS_ISSUES
|
||||||
|
|
||||||
|
c = Gas(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
|
||||||
|
(tx_hash_hex, tx_signed_raw_hex) = c.create(agent_roles['BOB'], agent_roles['ALICE'], 100 * (10 ** 6), tx_format=TxFormat.RLP_SIGNED)
|
||||||
|
|
||||||
|
queue_create(
|
||||||
|
default_chain_spec,
|
||||||
|
43,
|
||||||
|
agent_roles['BOB'],
|
||||||
|
tx_hash_hex,
|
||||||
|
tx_signed_raw_hex,
|
||||||
|
session=init_database,
|
||||||
|
)
|
||||||
|
cache_gas_data(
|
||||||
|
tx_hash_hex,
|
||||||
|
tx_signed_raw_hex,
|
||||||
|
default_chain_spec.asdict(),
|
||||||
|
)
|
||||||
|
|
||||||
|
fltr = GasFilter(default_chain_spec, queue=None)
|
||||||
|
|
||||||
|
o = block_latest()
|
||||||
|
r = eth_rpc.do(o)
|
||||||
|
o = block_by_number(r, include_tx=False)
|
||||||
|
r = eth_rpc.do(o)
|
||||||
|
block = Block(r)
|
||||||
|
block.txs = [tx_hash_hex]
|
||||||
|
|
||||||
|
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.get_leaf()
|
||||||
|
assert t.successful()
|
||||||
|
init_database.commit()
|
||||||
|
|
||||||
|
otx = Otx.load(tx_hash_hex_wait, session=init_database)
|
||||||
|
assert otx.status & StatusBits.QUEUED == StatusBits.QUEUED
|
@ -23,7 +23,6 @@ from chainqueue.state import (
|
|||||||
set_ready,
|
set_ready,
|
||||||
set_sent,
|
set_sent,
|
||||||
)
|
)
|
||||||
|
|
||||||
from hexathon import strip_0x
|
from hexathon import strip_0x
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
@ -31,7 +30,7 @@ from cic_eth.runnable.daemons.filters.tx import TxFilter
|
|||||||
from cic_eth.eth.gas import cache_gas_data
|
from cic_eth.eth.gas import cache_gas_data
|
||||||
|
|
||||||
|
|
||||||
def test_tx(
|
def test_filter_tx(
|
||||||
default_chain_spec,
|
default_chain_spec,
|
||||||
init_database,
|
init_database,
|
||||||
eth_rpc,
|
eth_rpc,
|
||||||
|
Loading…
Reference in New Issue
Block a user