80%! add synctx test

This commit is contained in:
nolash 2021-05-27 17:16:48 +02:00
parent d18f9303cb
commit 3434248912
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 73 additions and 4 deletions

View File

@ -21,6 +21,7 @@ from chainqueue.db.models.tx import Otx
from chainqueue.db.models.tx import TxCache from chainqueue.db.models.tx import TxCache
from chainqueue.db.enum import StatusBits from chainqueue.db.enum import StatusBits
from chainqueue.error import NotLocalTxError from chainqueue.error import NotLocalTxError
from potaahto.symbols import snake_and_camel
# local imports # local imports
from cic_eth.db import SessionBase from cic_eth.db import SessionBase
@ -151,7 +152,7 @@ def send(self, txs, chain_spec_dict):
@celery_app.task(bind=True, throws=(NotFoundEthException,), base=CriticalWeb3Task) @celery_app.task(bind=True, throws=(NotFoundEthException,), base=CriticalWeb3Task)
def sync_tx(self, tx_hash_hex, chain_spec_dict): def sync_tx(self, tx_hash_hex, chain_spec_dict):
"""Force update of network status of a simgle transaction """Force update of network status of a single transaction
:param tx_hash_hex: Transaction hash :param tx_hash_hex: Transaction hash
:type tx_hash_hex: str, 0x-hex :type tx_hash_hex: str, 0x-hex
@ -176,12 +177,14 @@ def sync_tx(self, tx_hash_hex, chain_spec_dict):
# TODO: apply receipt in tx object to validate and normalize input # TODO: apply receipt in tx object to validate and normalize input
if rcpt != None: if rcpt != None:
rcpt = snake_and_camel(rcpt)
success = rcpt['status'] == 1 success = rcpt['status'] == 1
logg.debug('sync tx {} mined block {} success {}'.format(tx_hash_hex, rcpt['blockNumber'], success)) logg.debug('sync tx {} mined block {} tx index {} success {}'.format(tx_hash_hex, rcpt['blockNumber'], rcpt['transactionIndex'], success))
s = celery.signature( s = celery.signature(
'cic_eth.queue.state.set_final', 'cic_eth.queue.state.set_final',
[ [
chain_spec_dict,
tx_hash_hex, tx_hash_hex,
rcpt['blockNumber'], rcpt['blockNumber'],
rcpt['transactionIndex'], rcpt['transactionIndex'],
@ -189,12 +192,14 @@ def sync_tx(self, tx_hash_hex, chain_spec_dict):
], ],
queue=queue, queue=queue,
) )
# TODO: it's not entirely clear how we can reliable determine that its in mempool without explicitly checking
else: else:
logg.debug('sync tx {} mempool'.format(tx_hash_hex)) logg.debug('sync tx {} mempool'.format(tx_hash_hex))
s = celery.signature( s = celery.signature(
'cic_eth.queue.state.set_sent', 'cic_eth.queue.state.set_sent',
[ [
chain_spec_dict,
tx_hash_hex, tx_hash_hex,
], ],
queue=queue, queue=queue,

View File

@ -4,16 +4,27 @@ import logging
# external imports # external imports
import pytest import pytest
import celery import celery
from chainlib.eth.gas import Gas from chainlib.eth.gas import (
OverrideGasOracle,
Gas,
)
from chainlib.eth.nonce import RPCNonceOracle from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.tx import ( from chainlib.eth.tx import (
TxFormat, TxFormat,
unpack, unpack,
transaction, transaction,
receipt, receipt,
raw,
) )
from hexathon import strip_0x from hexathon import strip_0x
from chainqueue.db.models.otx import Otx from chainqueue.db.models.otx import Otx
from chainqueue.tx import create as queue_create
from chainqueue.state import (
set_reserved,
set_ready,
set_sent,
)
from chainqueue.db.enum import StatusBits
# local imports # local imports
from cic_eth.queue.tx import register_tx from cic_eth.queue.tx import register_tx
@ -22,6 +33,7 @@ from cic_eth.eth.gas import cache_gas_data
logg = logging.getLogger() logg = logging.getLogger()
@pytest.mark.skip()
def test_tx_send( def test_tx_send(
init_database, init_database,
default_chain_spec, default_chain_spec,
@ -60,14 +72,62 @@ def test_tx_send(
def test_sync_tx( def test_sync_tx(
init_database,
default_chain_spec, default_chain_spec,
eth_rpc, eth_rpc,
eth_signer, eth_signer,
agent_roles,
celery_session_worker, celery_session_worker,
): ):
pass
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, 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_ready(default_chain_spec, tx_hash_hex, session=init_database)
set_reserved(default_chain_spec, tx_hash_hex, session=init_database)
set_sent(default_chain_spec, tx_hash_hex, session=init_database)
o = raw(tx_signed_raw_hex)
r = eth_rpc.do(o)
o = receipt(tx_hash_hex)
r = eth_rpc.do(o)
assert r['status'] == 1
s = celery.signature(
'cic_eth.eth.tx.sync_tx',
[
tx_hash_hex,
default_chain_spec.asdict(),
],
queue=None
)
t = s.apply_async()
r = t.get_leaf()
assert t.successful()
init_database.commit()
o = Otx.load(tx_hash_hex, session=init_database)
assert o.status & StatusBits.FINAL == StatusBits.FINAL
@pytest.mark.skip()
def test_resend_with_higher_gas( def test_resend_with_higher_gas(
init_database, init_database,
default_chain_spec, default_chain_spec,

View File

@ -163,3 +163,6 @@ def test_hashes_to_txs(
o = receipt(tx_hash_hex_two) o = receipt(tx_hash_hex_two)
r = eth_rpc.do(o) r = eth_rpc.do(o)
assert r['status'] == 1 assert r['status'] == 1

View File

@ -7,6 +7,7 @@ def test_registry_connect(
address_declarator, address_declarator,
token_registry, token_registry,
contract_roles, contract_roles,
purge_lookups,
registry, registry,
agent_roles, agent_roles,
): ):