From 7178373038bb09eb658e20268eed07f72b8f84db Mon Sep 17 00:00:00 2001 From: nolash Date: Thu, 27 May 2021 09:54:21 +0200 Subject: [PATCH] Add gas filter test --- apps/cic-eth/cic_eth/eth/gas.py | 1 - .../cic_eth/runnable/daemons/filters/gas.py | 9 +- apps/cic-eth/tests/filters/test_gas_filter.py | 101 ++++++++++++++++++ apps/cic-eth/tests/filters/test_tx_filter.py | 3 +- 4 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 apps/cic-eth/tests/filters/test_gas_filter.py diff --git a/apps/cic-eth/cic_eth/eth/gas.py b/apps/cic-eth/cic_eth/eth/gas.py index 258054c0..1494d308 100644 --- a/apps/cic-eth/cic_eth/eth/gas.py +++ b/apps/cic-eth/cic_eth/eth/gas.py @@ -177,7 +177,6 @@ def check_gas(self, tx_hashes, chain_spec_dict, txs=[], address=None, gas_requir if address == None: address = o['address'] - #if not web3.Web3.isChecksumAddress(address): if not is_checksum_address(address): raise ValueError('invalid address {}'.format(address)) diff --git a/apps/cic-eth/cic_eth/runnable/daemons/filters/gas.py b/apps/cic-eth/cic_eth/runnable/daemons/filters/gas.py index b0219093..4b17f86c 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/filters/gas.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/filters/gas.py @@ -17,7 +17,8 @@ from cic_eth.db.models.base import SessionBase from cic_eth.eth.gas import create_check_gas_task from .base import SyncFilter -logg = logging.getLogger().getChild(__name__) +#logg = logging.getLogger().getChild(__name__) +logg = logging.getLogger() class GasFilter(SyncFilter): @@ -27,11 +28,11 @@ class GasFilter(SyncFilter): self.chain_spec = chain_spec - def filter(self, conn, block, tx, session): + def filter(self, conn, block, tx, db_session): if tx.value > 0: tx_hash_hex = add_0x(tx.hash) 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 = q.join(Otx) q = q.filter(Otx.tx_hash==strip_0x(tx_hash_hex)) @@ -56,7 +57,7 @@ class GasFilter(SyncFilter): tx_hashes_hex=list(txs.keys()), queue=self.queue, ) - s.apply_async() + return s.apply_async() def __str__(self): diff --git a/apps/cic-eth/tests/filters/test_gas_filter.py b/apps/cic-eth/tests/filters/test_gas_filter.py new file mode 100644 index 00000000..8f91d2fd --- /dev/null +++ b/apps/cic-eth/tests/filters/test_gas_filter.py @@ -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 diff --git a/apps/cic-eth/tests/filters/test_tx_filter.py b/apps/cic-eth/tests/filters/test_tx_filter.py index bd706e5d..06d2c94e 100644 --- a/apps/cic-eth/tests/filters/test_tx_filter.py +++ b/apps/cic-eth/tests/filters/test_tx_filter.py @@ -23,7 +23,6 @@ from chainqueue.state import ( set_ready, set_sent, ) - from hexathon import strip_0x # local imports @@ -31,7 +30,7 @@ from cic_eth.runnable.daemons.filters.tx import TxFilter from cic_eth.eth.gas import cache_gas_data -def test_tx( +def test_filter_tx( default_chain_spec, init_database, eth_rpc,