From d18f9303cbdbc6352cc3544a4f12eb02991ea56a Mon Sep 17 00:00:00 2001 From: nolash Date: Thu, 27 May 2021 16:16:18 +0200 Subject: [PATCH] Add multisend test, hash-to-tx resolve test --- apps/cic-eth/cic_eth/eth/tx.py | 3 + apps/cic-eth/tests/task/test_task_tx_misc.py | 165 +++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 apps/cic-eth/tests/task/test_task_tx_misc.py diff --git a/apps/cic-eth/cic_eth/eth/tx.py b/apps/cic-eth/cic_eth/eth/tx.py index 4883e872..f50da0df 100644 --- a/apps/cic-eth/cic_eth/eth/tx.py +++ b/apps/cic-eth/cic_eth/eth/tx.py @@ -58,6 +58,9 @@ def hashes_to_txs(self, tx_hashes): if len(tx_hashes) == 0: raise ValueError('no transaction to send') + for i in range(len(tx_hashes)): + tx_hashes[i] = strip_0x(tx_hashes[i]) + queue = self.request.delivery_info['routing_key'] session = SessionBase.create_session() diff --git a/apps/cic-eth/tests/task/test_task_tx_misc.py b/apps/cic-eth/tests/task/test_task_tx_misc.py new file mode 100644 index 00000000..08ccd2d9 --- /dev/null +++ b/apps/cic-eth/tests/task/test_task_tx_misc.py @@ -0,0 +1,165 @@ +# standard imports +import os +import logging + +# external imports +import pytest +import celery +from chainqueue.tx import create as queue_create +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.gas import ( + OverrideGasOracle, + Gas, + ) +from chainlib.eth.tx import ( + TxFormat, + unpack, + receipt, + ) +from hexathon import ( + add_0x, + strip_0x, + ) +from chainqueue.state import ( + set_reserved, + set_ready, + ) + +logg = logging.getLogger() + + +@pytest.mark.skip() +def test_hashes_to_txs( + init_database, + default_chain_spec, + agent_roles, + eth_rpc, + eth_signer, + celery_session_worker, + ): + + 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_one, tx_signed_raw_hex_one) = 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_one, + tx_signed_raw_hex_one, + session=init_database, + ) + + #nonce_oracle = OverrideNonceOracle(agent_roles['ALICE'], 43) + c = Gas(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) + (tx_hash_hex_two, tx_signed_raw_hex_two) = c.create(agent_roles['ALICE'], agent_roles['CAROL'], 200 * (10 ** 6), tx_format=TxFormat.RLP_SIGNED) + + queue_create( + default_chain_spec, + 43, + agent_roles['ALICE'], + tx_hash_hex_two, + tx_signed_raw_hex_two, + session=init_database, + ) + + init_database.commit() + + bogus_one = add_0x(os.urandom(32).hex()) + bogus_two = add_0x(os.urandom(32).hex()) + + yarrgs = [ + bogus_one, + tx_hash_hex_two, + bogus_two, + tx_hash_hex_one, + ] + s = celery.signature( + 'cic_eth.eth.tx.hashes_to_txs', + [ + yarrgs, + ], + queue=None, + ) + t = s.apply_async() + r = t.get_leaf() + assert t.successful() + assert len(r) == 2 + + logg.debug('r {}'.format(r)) + txs = [ + tx_signed_raw_hex_two, + tx_signed_raw_hex_one, + ] + for tx in r: + txs.remove(add_0x(tx)) + assert len(txs) == 0 + + + +def test_hashes_to_txs( + init_database, + default_chain_spec, + agent_roles, + eth_rpc, + eth_signer, + celery_session_worker, + ): + + nonce_oracle = RPCNonceOracle(agent_roles['ALICE'], conn=eth_rpc) + 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_one, tx_signed_raw_hex_one) = 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_one, + tx_signed_raw_hex_one, + session=init_database, + ) + set_ready(default_chain_spec, tx_hash_hex_one, session=init_database) + set_reserved(default_chain_spec, tx_hash_hex_one, session=init_database) + + c = Gas(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) + (tx_hash_hex_two, tx_signed_raw_hex_two) = c.create(agent_roles['ALICE'], agent_roles['CAROL'], 200 * (10 ** 6), tx_format=TxFormat.RLP_SIGNED) + + queue_create( + default_chain_spec, + 43, + agent_roles['ALICE'], + tx_hash_hex_two, + tx_signed_raw_hex_two, + session=init_database, + ) + + set_ready(default_chain_spec, tx_hash_hex_two, session=init_database) + set_reserved(default_chain_spec, tx_hash_hex_two, session=init_database) + init_database.commit() + + yarrgs = [ + tx_signed_raw_hex_one, + tx_signed_raw_hex_two, + ] + s = celery.signature( + 'cic_eth.eth.tx.send', + [ + yarrgs, + default_chain_spec.asdict(), + ], + queue=None + ) + t = s.apply_async() + r = t.get_leaf() + assert t.successful() + + o = receipt(tx_hash_hex_one) + r = eth_rpc.do(o) + assert r['status'] == 1 + + o = receipt(tx_hash_hex_two) + r = eth_rpc.do(o) + assert r['status'] == 1