cic-internal-integration/apps/cic-eth/tests/tasks/test_states.py

134 lines
3.3 KiB
Python
Raw Normal View History

2021-02-01 18:12:51 +01:00
# standard imports
import logging
import time
# third-party imports
import celery
# local imports
from cic_eth.db.models.base import SessionBase
from cic_eth.db.models.otx import Otx
from cic_eth.db.enum import (
StatusEnum,
StatusBits,
is_error_status,
)
2021-02-01 18:12:51 +01:00
from cic_eth.eth.task import sign_and_register_tx
logg = logging.getLogger()
def test_states_initial(
init_w3,
init_database,
init_eth_account_roles,
celery_session_worker,
):
tx = {
'from': init_w3.eth.accounts[0],
'to': init_w3.eth.accounts[1],
2021-03-01 21:15:17 +01:00
'nonce': 13,
2021-02-01 18:12:51 +01:00
'gas': 21000,
'gasPrice': 1000000,
'value': 128,
2021-03-01 21:15:17 +01:00
'chainId': 42,
2021-02-01 18:12:51 +01:00
'data': '',
}
2021-03-01 21:15:17 +01:00
(tx_hash_hex, tx_raw_signed_hex) = sign_and_register_tx(tx, 'foo:bar:42', None)
2021-02-01 18:12:51 +01:00
otx = init_database.query(Otx).filter(Otx.tx_hash==tx_hash_hex).first()
assert otx.status == StatusEnum.PENDING.value
s = celery.signature(
'cic_eth.eth.tx.check_gas',
[
[tx_hash_hex],
2021-03-01 21:15:17 +01:00
'foo:bar:42',
2021-02-01 18:12:51 +01:00
[tx_raw_signed_hex],
init_w3.eth.accounts[0],
8000000,
],
queue=None,
)
t = s.apply_async()
r = t.get()
for c in t.collect():
pass
assert t.successful()
session = SessionBase.create_session()
otx = session.query(Otx).filter(Otx.tx_hash==tx_hash_hex).first()
assert otx.status == StatusEnum.READYSEND.value
otx.waitforgas(session=session)
session.commit()
s = celery.signature(
'cic_eth.eth.tx.check_gas',
[
[tx_hash_hex],
2021-03-01 21:15:17 +01:00
'foo:bar:42',
2021-02-01 18:12:51 +01:00
[tx_raw_signed_hex],
init_w3.eth.accounts[0],
8000000,
],
queue=None,
)
t = s.apply_async()
r = t.get()
for c in t.collect():
pass
assert t.successful()
session = SessionBase.create_session()
otx = session.query(Otx).filter(Otx.tx_hash==tx_hash_hex).first()
assert otx.status == StatusEnum.READYSEND.value
def test_states_failed(
init_w3,
init_database,
init_eth_account_roles,
celery_session_worker,
):
tx = {
'from': init_w3.eth.accounts[0],
'to': init_w3.eth.accounts[1],
2021-03-01 21:15:17 +01:00
'nonce': 13,
2021-02-01 18:12:51 +01:00
'gas': 21000,
'gasPrice': 1000000,
'value': 128,
2021-03-01 21:15:17 +01:00
'chainId': 42,
2021-02-01 18:12:51 +01:00
'data': '',
}
2021-03-01 21:15:17 +01:00
(tx_hash_hex, tx_raw_signed_hex) = sign_and_register_tx(tx, 'foo:bar:42', None)
2021-02-01 18:12:51 +01:00
otx = init_database.query(Otx).filter(Otx.tx_hash==tx_hash_hex).first()
otx.sendfail(session=init_database)
2021-02-01 18:12:51 +01:00
init_database.commit()
s = celery.signature(
'cic_eth.eth.tx.check_gas',
[
[tx_hash_hex],
2021-03-01 21:15:17 +01:00
'foo:bar:42',
2021-02-01 18:12:51 +01:00
[tx_raw_signed_hex],
init_w3.eth.accounts[0],
8000000,
],
queue=None,
)
t = s.apply_async()
r = t.get()
for c in t.collect():
pass
assert t.successful()
init_database.commit()
2021-02-01 18:12:51 +01:00
otx = init_database.query(Otx).filter(Otx.tx_hash==tx_hash_hex).first()
assert otx.status & StatusEnum.RETRY == StatusEnum.RETRY
#assert otx.status & StatusBits.QUEUED
assert is_error_status(otx.status)