cic-eth: Make nonce separate task
This commit is contained in:
@@ -1,8 +1,29 @@
|
||||
# third-party imports
|
||||
import pytest
|
||||
import uuid
|
||||
|
||||
# local imports
|
||||
from cic_eth.db.models.nonce import Nonce
|
||||
from cic_eth.db.models.nonce import (
|
||||
Nonce,
|
||||
NonceReservation,
|
||||
)
|
||||
from cic_eth.error import (
|
||||
InitializationError,
|
||||
IntegrityError,
|
||||
)
|
||||
|
||||
|
||||
def test_nonce_init(
|
||||
init_database,
|
||||
eth_empty_accounts,
|
||||
):
|
||||
|
||||
nonce = Nonce.init(eth_empty_accounts[0], 42, session=init_database)
|
||||
init_database.commit()
|
||||
|
||||
with pytest.raises(InitializationError):
|
||||
nonce = Nonce.init(eth_empty_accounts[0], 42, session=init_database)
|
||||
|
||||
|
||||
def test_nonce_increment(
|
||||
init_database,
|
||||
@@ -10,11 +31,46 @@ def test_nonce_increment(
|
||||
database_engine,
|
||||
):
|
||||
|
||||
# if database_engine[:6] == 'sqlite':
|
||||
# pytest.skip('sqlite cannot lock tables which is required for this test, skipping')
|
||||
|
||||
nonce = Nonce.next(eth_empty_accounts[0], 3)
|
||||
assert nonce == 3
|
||||
|
||||
nonce = Nonce.next(eth_empty_accounts[0], 3)
|
||||
assert nonce == 4
|
||||
|
||||
|
||||
def test_nonce_reserve(
|
||||
init_database,
|
||||
eth_empty_accounts,
|
||||
):
|
||||
|
||||
nonce = Nonce.init(eth_empty_accounts[0], 42, session=init_database)
|
||||
init_database.commit()
|
||||
uu = uuid.uuid4()
|
||||
nonce = NonceReservation.next(eth_empty_accounts[0], str(uu), session=init_database)
|
||||
init_database.commit()
|
||||
assert nonce == 42
|
||||
|
||||
q = init_database.query(Nonce)
|
||||
q = q.filter(Nonce.address_hex==eth_empty_accounts[0])
|
||||
o = q.first()
|
||||
assert o.nonce == 43
|
||||
|
||||
nonce = NonceReservation.release(str(uu))
|
||||
init_database.commit()
|
||||
assert nonce == 42
|
||||
|
||||
q = init_database.query(NonceReservation)
|
||||
q = q.filter(NonceReservation.key==str(uu))
|
||||
o = q.first()
|
||||
assert o == None
|
||||
|
||||
|
||||
def test_nonce_reserve_integrity(
|
||||
init_database,
|
||||
eth_empty_accounts,
|
||||
):
|
||||
|
||||
uu = uuid.uuid4()
|
||||
nonce = Nonce.init(eth_empty_accounts[0], 42, session=init_database)
|
||||
with pytest.raises(IntegrityError):
|
||||
NonceReservation.release(str(uu))
|
||||
|
||||
@@ -17,6 +17,7 @@ from cic_eth.db.models.tx import TxCache
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
@pytest.mark.skip()
|
||||
def test_resolve_converters_by_tokens(
|
||||
cic_registry,
|
||||
init_w3,
|
||||
@@ -43,6 +44,7 @@ def test_resolve_converters_by_tokens(
|
||||
assert len(t['converters']) == 1
|
||||
|
||||
|
||||
@pytest.mark.skip()
|
||||
def test_unpack_convert(
|
||||
default_chain_spec,
|
||||
cic_registry,
|
||||
@@ -84,6 +86,7 @@ def test_unpack_convert(
|
||||
assert convert_data['fee'] == 0
|
||||
|
||||
|
||||
@pytest.mark.skip()
|
||||
def test_queue_cache_convert(
|
||||
default_chain_spec,
|
||||
init_w3,
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
# standard imports
|
||||
import logging
|
||||
|
||||
# local imports
|
||||
from cic_eth.eth.nonce import NonceOracle
|
||||
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
def test_nonce_sequence(
|
||||
eth_empty_accounts,
|
||||
init_database,
|
||||
init_rpc,
|
||||
):
|
||||
|
||||
account= init_rpc.w3.eth.personal.new_account('')
|
||||
no = NonceOracle(account, 0)
|
||||
n = no.next()
|
||||
assert n == 0
|
||||
|
||||
n = no.next()
|
||||
assert n == 1
|
||||
|
||||
init_rpc.w3.eth.sendTransaction({
|
||||
'from': init_rpc.w3.eth.accounts[0],
|
||||
'to': account,
|
||||
'value': 200000000,
|
||||
})
|
||||
init_rpc.w3.eth.sendTransaction({
|
||||
'from': account,
|
||||
'to': eth_empty_accounts[0],
|
||||
'value': 100,
|
||||
})
|
||||
|
||||
c = init_rpc.w3.eth.getTransactionCount(account, 'pending')
|
||||
logg.debug('nonce {}'.format(c))
|
||||
|
||||
account= init_rpc.w3.eth.personal.new_account('')
|
||||
no = NonceOracle(account, c)
|
||||
|
||||
n = no.next()
|
||||
assert n == 1
|
||||
|
||||
n = no.next()
|
||||
assert n == 2
|
||||
|
||||
# try with bogus value
|
||||
no = NonceOracle(account, 4)
|
||||
n = no.next()
|
||||
assert n == 3
|
||||
|
||||
@@ -11,12 +11,14 @@ from cic_eth.eth.util import unpack_signed_raw_tx
|
||||
from cic_eth.queue.tx import create as queue_create
|
||||
from cic_eth.db.models.otx import Otx
|
||||
from cic_eth.db.models.tx import TxCache
|
||||
from cic_eth.db.models.nonce import NonceReservation
|
||||
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
def test_unpack_transfer(
|
||||
default_chain_spec,
|
||||
init_database,
|
||||
init_w3,
|
||||
init_rpc,
|
||||
cic_registry,
|
||||
@@ -24,6 +26,9 @@ def test_unpack_transfer(
|
||||
bancor_registry,
|
||||
):
|
||||
|
||||
NonceReservation.next(init_w3.eth.accounts[0], 'foo', init_database)
|
||||
init_database.commit()
|
||||
|
||||
source_token = CICRegistry.get_address(default_chain_spec, bancor_tokens[0])
|
||||
logg.debug('bancor tokens {} {}'.format(bancor_tokens, source_token))
|
||||
txf = TokenTxFactory(init_w3.eth.accounts[0], init_rpc)
|
||||
@@ -32,6 +37,7 @@ def test_unpack_transfer(
|
||||
init_w3.eth.accounts[1],
|
||||
42,
|
||||
default_chain_spec,
|
||||
'foo',
|
||||
)
|
||||
s = init_w3.eth.sign_transaction(transfer_tx)
|
||||
s_bytes = bytes.fromhex(s['raw'][2:])
|
||||
@@ -56,6 +62,9 @@ def test_queue_cache_transfer(
|
||||
bancor_registry,
|
||||
):
|
||||
|
||||
NonceReservation.next(init_w3.eth.accounts[0], 'foo', init_database)
|
||||
init_database.commit()
|
||||
|
||||
source_token = CICRegistry.get_address(default_chain_spec, bancor_tokens[0])
|
||||
txf = TokenTxFactory(init_w3.eth.accounts[0], init_rpc)
|
||||
value = 42
|
||||
@@ -64,6 +73,7 @@ def test_queue_cache_transfer(
|
||||
init_w3.eth.accounts[1],
|
||||
value,
|
||||
default_chain_spec,
|
||||
'foo',
|
||||
)
|
||||
tx_signed = init_w3.eth.sign_transaction(transfer_tx)
|
||||
tx_hash = init_w3.eth.sendRawTransaction(tx_signed['raw'])
|
||||
|
||||
@@ -8,12 +8,17 @@ import moolb
|
||||
# local imports
|
||||
from cic_eth.eth.token import TokenTxFactory
|
||||
from cic_eth.eth.task import sign_tx
|
||||
from cic_eth.db.models.nonce import (
|
||||
NonceReservation,
|
||||
Nonce,
|
||||
)
|
||||
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
# TODO: This test fails when not run alone. Identify which fixture leaves a dirty state
|
||||
def test_filter_process(
|
||||
init_database,
|
||||
init_rpc,
|
||||
default_chain_spec,
|
||||
default_chain_registry,
|
||||
@@ -29,9 +34,22 @@ def test_filter_process(
|
||||
|
||||
tx_hashes = []
|
||||
# external tx
|
||||
|
||||
# TODO: it does not make sense to use the db setup for nonce here, but we need it as long as we are using the factory to assemble to tx
|
||||
nonce = init_w3.eth.getTransactionCount(init_w3.eth.accounts[0])
|
||||
q = init_database.query(Nonce)
|
||||
q = q.filter(Nonce.address_hex==init_w3.eth.accounts[0])
|
||||
o = q.first()
|
||||
o.nonce = nonce
|
||||
init_database.add(o)
|
||||
init_database.commit()
|
||||
|
||||
NonceReservation.next(init_w3.eth.accounts[0], 'foo', init_database)
|
||||
init_database.commit()
|
||||
|
||||
init_eth_tester.mine_blocks(13)
|
||||
txf = TokenTxFactory(init_w3.eth.accounts[0], init_rpc)
|
||||
tx = txf.transfer(dummy_token_gifted, init_w3.eth.accounts[1], 3000, default_chain_spec)
|
||||
tx = txf.transfer(dummy_token_gifted, init_w3.eth.accounts[1], 3000, default_chain_spec, 'foo')
|
||||
(tx_hash_hex, tx_signed_raw_hex) = sign_tx(tx, str(default_chain_spec))
|
||||
tx_hashes.append(tx_hash_hex)
|
||||
init_w3.eth.sendRawTransaction(tx_signed_raw_hex)
|
||||
@@ -43,9 +61,12 @@ def test_filter_process(
|
||||
t.add(a.to_bytes(4, 'big'))
|
||||
|
||||
# external tx
|
||||
NonceReservation.next(init_w3.eth.accounts[0], 'bar', init_database)
|
||||
init_database.commit()
|
||||
|
||||
init_eth_tester.mine_blocks(28)
|
||||
txf = TokenTxFactory(init_w3.eth.accounts[0], init_rpc)
|
||||
tx = txf.transfer(dummy_token_gifted, init_w3.eth.accounts[1], 4000, default_chain_spec)
|
||||
tx = txf.transfer(dummy_token_gifted, init_w3.eth.accounts[1], 4000, default_chain_spec, 'bar')
|
||||
(tx_hash_hex, tx_signed_raw_hex) = sign_tx(tx, str(default_chain_spec))
|
||||
tx_hashes.append(tx_hash_hex)
|
||||
init_w3.eth.sendRawTransaction(tx_signed_raw_hex)
|
||||
|
||||
71
apps/cic-eth/tests/unit/queue/test_list_tx.py
Normal file
71
apps/cic-eth/tests/unit/queue/test_list_tx.py
Normal file
@@ -0,0 +1,71 @@
|
||||
# standard imports
|
||||
import logging
|
||||
|
||||
# local imports
|
||||
from cic_eth.queue.tx import get_status_tx
|
||||
from cic_eth.db.enum import (
|
||||
StatusEnum,
|
||||
StatusBits,
|
||||
)
|
||||
from cic_eth.queue.tx import create as queue_create
|
||||
from cic_eth.eth.tx import cache_gas_refill_data
|
||||
from cic_eth.db.models.otx import Otx
|
||||
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
def test_status_tx_list(
|
||||
default_chain_spec,
|
||||
init_database,
|
||||
init_w3,
|
||||
):
|
||||
|
||||
tx = {
|
||||
'from': init_w3.eth.accounts[0],
|
||||
'to': init_w3.eth.accounts[1],
|
||||
'nonce': 42,
|
||||
'gas': 21000,
|
||||
'gasPrice': 1000000,
|
||||
'value': 128,
|
||||
'chainId': 666,
|
||||
'data': '',
|
||||
}
|
||||
logg.debug('nonce {}'.format(tx['nonce']))
|
||||
tx_signed = init_w3.eth.sign_transaction(tx)
|
||||
#tx_hash = RpcClient.w3.keccak(hexstr=tx_signed['raw'])
|
||||
tx_hash = init_w3.keccak(hexstr=tx_signed['raw'])
|
||||
queue_create(tx['nonce'], tx['from'], tx_hash.hex(), tx_signed['raw'], str(default_chain_spec))
|
||||
cache_gas_refill_data(tx_hash.hex(), tx)
|
||||
tx_hash_hex = tx_hash.hex()
|
||||
|
||||
q = init_database.query(Otx)
|
||||
otx = q.get(1)
|
||||
otx.sendfail(session=init_database)
|
||||
init_database.add(otx)
|
||||
init_database.commit()
|
||||
init_database.refresh(otx)
|
||||
|
||||
txs = get_status_tx(StatusBits.LOCAL_ERROR, session=init_database)
|
||||
assert len(txs) == 1
|
||||
|
||||
otx.sendfail(session=init_database)
|
||||
otx.retry(session=init_database)
|
||||
init_database.add(otx)
|
||||
init_database.commit()
|
||||
init_database.refresh(otx)
|
||||
|
||||
txs = get_status_tx(StatusBits.LOCAL_ERROR, session=init_database)
|
||||
assert len(txs) == 1
|
||||
|
||||
txs = get_status_tx(StatusBits.QUEUED, session=init_database)
|
||||
assert len(txs) == 1
|
||||
|
||||
txs = get_status_tx(StatusBits.QUEUED, not_status=StatusBits.LOCAL_ERROR, session=init_database)
|
||||
assert len(txs) == 0
|
||||
|
||||
txs = get_status_tx(StatusBits.QUEUED, not_status=StatusBits.IN_NETWORK, session=init_database)
|
||||
assert len(txs) == 1
|
||||
|
||||
txs = get_status_tx(StatusBits.IN_NETWORK, session=init_database)
|
||||
assert len(txs) == 0
|
||||
|
||||
Reference in New Issue
Block a user