Add cache interface methods, move old tests
This commit is contained in:
86
tests/old/chainqueue_base.py
Normal file
86
tests/old/chainqueue_base.py
Normal file
@@ -0,0 +1,86 @@
|
||||
# standard imports
|
||||
import logging
|
||||
import unittest
|
||||
import tempfile
|
||||
import os
|
||||
#import pysqlite
|
||||
|
||||
# external imports
|
||||
from chainqueue.db.models.otx import Otx
|
||||
from chainqueue.db.models.tx import TxCache
|
||||
from chainlib.chain import ChainSpec
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
)
|
||||
|
||||
# local imports
|
||||
from chainqueue.sql.tx import create
|
||||
from chainqueue.unittest.db import ChainQueueDb
|
||||
from chainqueue.sql.backend import SQLBackend
|
||||
|
||||
script_dir = os.path.realpath(os.path.dirname(__file__))
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestBase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
debug = bool(os.environ.get('DATABASE_DEBUG', False))
|
||||
self.db = ChainQueueDb(debug=debug)
|
||||
self.session = self.db.bind_session()
|
||||
self.chain_spec = ChainSpec('evm', 'foo', 42, 'bar')
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
self.session.commit()
|
||||
self.db.release_session(self.session)
|
||||
|
||||
|
||||
class TestOtxBase(TestBase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestOtxBase, self).setUp()
|
||||
self.tx_hash = os.urandom(32).hex()
|
||||
self.tx = os.urandom(128).hex()
|
||||
self.nonce = 42
|
||||
self.alice = add_0x(os.urandom(20).hex())
|
||||
|
||||
tx_hash = create(self.chain_spec, self.nonce, self.alice, self.tx_hash, self.tx, session=self.session)
|
||||
self.assertEqual(tx_hash, self.tx_hash)
|
||||
self.session.commit()
|
||||
|
||||
logg.info('using tx hash {}'.format(self.tx_hash))
|
||||
|
||||
|
||||
class TestTxBase(TestOtxBase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestTxBase, self).setUp()
|
||||
self.bob = add_0x(os.urandom(20).hex())
|
||||
self.carol = add_0x(os.urandom(20).hex())
|
||||
self.foo_token = add_0x(os.urandom(20).hex())
|
||||
self.bar_token = add_0x(os.urandom(20).hex())
|
||||
self.from_value = 42
|
||||
self.to_value = 13
|
||||
|
||||
backend = SQLBackend(self.db.dsn)
|
||||
txc = TxCache(
|
||||
self.tx_hash,
|
||||
self.alice,
|
||||
self.bob,
|
||||
self.foo_token,
|
||||
self.bar_token,
|
||||
self.from_value,
|
||||
self.to_value,
|
||||
session=self.session,
|
||||
)
|
||||
self.session.add(txc)
|
||||
self.session.commit()
|
||||
|
||||
otx = Otx.load(self.tx_hash)
|
||||
txc = TxCache.load(self.tx_hash)
|
||||
|
||||
self.assertEqual(txc.otx_id, otx.id)
|
||||
|
||||
70
tests/old/test_basic.py
Normal file
70
tests/old/test_basic.py
Normal file
@@ -0,0 +1,70 @@
|
||||
# standard imports
|
||||
import os
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
# external imports
|
||||
from hexathon import (
|
||||
strip_0x,
|
||||
add_0x,
|
||||
)
|
||||
|
||||
# local imports
|
||||
from chainqueue.db.models.otx import Otx
|
||||
from chainqueue.db.models.tx import TxCache
|
||||
|
||||
# test imports
|
||||
from tests.chainqueue_base import TestBase
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
class TestBasic(TestBase):
|
||||
|
||||
def test_hello(self):
|
||||
pass
|
||||
|
||||
|
||||
def test_otx(self):
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
address = add_0x(os.urandom(20).hex())
|
||||
tx = add_0x(os.urandom(128).hex())
|
||||
nonce = 42
|
||||
otx = Otx(nonce, tx_hash, tx)
|
||||
self.session.add(otx)
|
||||
|
||||
|
||||
def test_tx(self):
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
tx = add_0x(os.urandom(128).hex())
|
||||
nonce = 42
|
||||
otx = Otx(nonce, tx_hash, tx)
|
||||
otx.block = 1024
|
||||
self.session.add(otx)
|
||||
|
||||
alice = add_0x(os.urandom(20).hex())
|
||||
bob = add_0x(os.urandom(20).hex())
|
||||
foo_token = add_0x(os.urandom(20).hex())
|
||||
bar_token = add_0x(os.urandom(20).hex())
|
||||
from_value = 13
|
||||
to_value = 666
|
||||
|
||||
block_number = 1024
|
||||
tx_index = 1337
|
||||
|
||||
txc = TxCache(
|
||||
tx_hash,
|
||||
alice,
|
||||
bob,
|
||||
foo_token,
|
||||
bar_token,
|
||||
from_value,
|
||||
to_value,
|
||||
block_number=block_number,
|
||||
tx_index=tx_index,
|
||||
session=self.session,
|
||||
)
|
||||
self.session.add(txc)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
84
tests/old/test_fs.py
Normal file
84
tests/old/test_fs.py
Normal file
@@ -0,0 +1,84 @@
|
||||
# standard imports
|
||||
import unittest
|
||||
import tempfile
|
||||
import shutil
|
||||
import logging
|
||||
import os
|
||||
|
||||
# external imports
|
||||
from leveldir.hex import HexDir
|
||||
|
||||
# local imports
|
||||
from chainqueue.fs.queue import FsQueue
|
||||
from chainqueue.enum import StatusBits
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
class FsQueueTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.dir = tempfile.mkdtemp()
|
||||
self.hexdir = HexDir(os.path.join(self.dir, 'q'), 32, 2, 8)
|
||||
self.q = FsQueue(os.path.join(self.dir, 'spool'), backend=self.hexdir)
|
||||
logg.debug('setup fsqueue root {}'.format(self.dir))
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.dir)
|
||||
logg.debug('cleaned fsqueue root {}'.format(self.dir))
|
||||
|
||||
|
||||
def test_new(self):
|
||||
tx_hash = os.urandom(32)
|
||||
tx_content = os.urandom(128)
|
||||
self.q.add(tx_hash, tx_content)
|
||||
|
||||
f = open(os.path.join(self.q.path_state['new'], tx_hash.hex()), 'rb')
|
||||
r = f.read()
|
||||
f.close()
|
||||
self.assertEqual(r, b'\x00' * 8)
|
||||
|
||||
|
||||
def test_change(self):
|
||||
tx_hash = os.urandom(32)
|
||||
tx_content = os.urandom(128)
|
||||
self.q.add(tx_hash, tx_content)
|
||||
self.q.set(tx_hash, StatusBits.QUEUED)
|
||||
|
||||
(tx_status, tx_content_retrieved) = self.q.get(tx_hash)
|
||||
status = int.from_bytes(tx_status, byteorder='big')
|
||||
self.assertEqual(status & StatusBits.QUEUED, StatusBits.QUEUED)
|
||||
|
||||
|
||||
def test_move(self):
|
||||
tx_hash = os.urandom(32)
|
||||
tx_content = os.urandom(128)
|
||||
self.q.add(tx_hash, tx_content)
|
||||
self.q.move(tx_hash, 'ready', from_state='new')
|
||||
|
||||
f = open(os.path.join(self.q.path_state['ready'], tx_hash.hex()), 'rb')
|
||||
r = f.read()
|
||||
f.close()
|
||||
self.assertEqual(r, b'\x00' * 8)
|
||||
|
||||
|
||||
def test_purge(self):
|
||||
tx_hash = os.urandom(32)
|
||||
tx_content = os.urandom(128)
|
||||
self.q.add(tx_hash, tx_content)
|
||||
self.q.move(tx_hash, 'ready', from_state='new')
|
||||
self.q.purge(tx_hash, 'ready')
|
||||
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
entry_path = os.path.join(self.q.path_state['ready'], tx_hash.hex())
|
||||
os.stat(entry_path)
|
||||
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
entry_path = os.path.join(self.q.index_path, tx_hash.hex())
|
||||
os.stat(entry_path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
41
tests/old/test_fs_entry.py
Normal file
41
tests/old/test_fs_entry.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# standard imports
|
||||
import unittest
|
||||
import tempfile
|
||||
import shutil
|
||||
import logging
|
||||
import os
|
||||
|
||||
# external imports
|
||||
from leveldir.hex import HexDir
|
||||
|
||||
# local imports
|
||||
from chainqueue.fs.queue import FsQueue
|
||||
from chainqueue.fs.entry import Entry
|
||||
from chainqueue.enum import StatusBits
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
class FsQueueEntryTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.dir = tempfile.mkdtemp()
|
||||
self.hexdir = HexDir(os.path.join(self.dir, 'q'), 32, 2, 8)
|
||||
self.q = FsQueue(os.path.join(self.dir, 'spool'), backend=self.hexdir)
|
||||
logg.debug('setup fsqueue root {}'.format(self.dir))
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.dir)
|
||||
logg.debug('cleaned fsqueue root {}'.format(self.dir))
|
||||
|
||||
|
||||
def test_entry(self):
|
||||
tx_hash = os.urandom(32).hex()
|
||||
tx_content = os.urandom(128).hex()
|
||||
Entry(0, tx_hash, tx_content)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
15
tests/old/test_helo.py
Normal file
15
tests/old/test_helo.py
Normal file
@@ -0,0 +1,15 @@
|
||||
# standard imports
|
||||
import unittest
|
||||
|
||||
# local imports
|
||||
from tests.chainqueue_base import TestBase
|
||||
|
||||
|
||||
class TestHelo(TestBase):
|
||||
|
||||
def test_helo(self):
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
168
tests/old/test_otx.py
Normal file
168
tests/old/test_otx.py
Normal file
@@ -0,0 +1,168 @@
|
||||
# standard imports
|
||||
import os
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
# external imports
|
||||
from chainlib.chain import ChainSpec
|
||||
|
||||
# local imports
|
||||
from chainqueue.db.models.otx import Otx
|
||||
from chainqueue.db.enum import (
|
||||
is_alive,
|
||||
is_error_status,
|
||||
)
|
||||
from chainqueue.sql.state import *
|
||||
|
||||
# test imports
|
||||
from tests.chainqueue_base import TestOtxBase
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
class TestOtx(TestOtxBase):
|
||||
|
||||
def test_ideal_state_sequence(self):
|
||||
set_ready(self.chain_spec, self.tx_hash, session=self.session)
|
||||
otx = Otx.load(self.tx_hash, session=self.session)
|
||||
self.assertEqual(otx.status, StatusBits.QUEUED)
|
||||
|
||||
set_reserved(self.chain_spec, self.tx_hash)
|
||||
self.session.refresh(otx)
|
||||
self.assertEqual(otx.status, StatusBits.RESERVED)
|
||||
|
||||
set_sent(self.chain_spec, self.tx_hash, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertEqual(otx.status, StatusBits.IN_NETWORK)
|
||||
|
||||
set_final(self.chain_spec, self.tx_hash, block=1024, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertFalse(is_alive(otx.status))
|
||||
self.assertFalse(is_error_status(otx.status))
|
||||
|
||||
|
||||
def test_send_fail_and_retry(self):
|
||||
set_ready(self.chain_spec, self.tx_hash, session=self.session)
|
||||
otx = Otx.load(self.tx_hash, session=self.session)
|
||||
self.assertEqual(otx.status, StatusBits.QUEUED)
|
||||
|
||||
set_reserved(self.chain_spec, self.tx_hash, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertEqual(otx.status, StatusBits.RESERVED)
|
||||
|
||||
set_sent(self.chain_spec, self.tx_hash, fail=True, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertTrue(is_error_status(otx.status))
|
||||
|
||||
set_ready(self.chain_spec, self.tx_hash, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertEqual(otx.status & StatusBits.QUEUED, StatusBits.QUEUED)
|
||||
self.assertTrue(is_error_status(otx.status))
|
||||
|
||||
set_reserved(self.chain_spec, self.tx_hash, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertEqual(otx.status & StatusBits.RESERVED, StatusBits.RESERVED)
|
||||
self.assertTrue(is_error_status(otx.status))
|
||||
|
||||
set_sent(self.chain_spec, self.tx_hash, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertEqual(otx.status, StatusBits.IN_NETWORK)
|
||||
self.assertFalse(is_error_status(otx.status))
|
||||
|
||||
set_final(self.chain_spec, self.tx_hash, block=1024, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertFalse(is_alive(otx.status))
|
||||
self.assertFalse(is_error_status(otx.status))
|
||||
|
||||
|
||||
def test_fubar(self):
|
||||
set_ready(self.chain_spec, self.tx_hash, session=self.session)
|
||||
otx = Otx.load(self.tx_hash, session=self.session)
|
||||
self.assertEqual(otx.status, StatusBits.QUEUED)
|
||||
|
||||
set_reserved(self.chain_spec, self.tx_hash, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertEqual(otx.status & StatusBits.RESERVED, StatusBits.RESERVED)
|
||||
|
||||
set_fubar(self.chain_spec, self.tx_hash, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertTrue(is_error_status(otx.status))
|
||||
self.assertEqual(otx.status & StatusBits.UNKNOWN_ERROR, StatusBits.UNKNOWN_ERROR)
|
||||
|
||||
|
||||
def test_reject(self):
|
||||
set_ready(self.chain_spec, self.tx_hash, session=self.session)
|
||||
otx = Otx.load(self.tx_hash, session=self.session)
|
||||
self.assertEqual(otx.status, StatusBits.QUEUED)
|
||||
|
||||
set_reserved(self.chain_spec, self.tx_hash, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertEqual(otx.status & StatusBits.RESERVED, StatusBits.RESERVED)
|
||||
|
||||
set_rejected(self.chain_spec, self.tx_hash, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertTrue(is_error_status(otx.status))
|
||||
self.assertEqual(otx.status & StatusBits.NODE_ERROR, StatusBits.NODE_ERROR)
|
||||
|
||||
|
||||
def test_final_fail(self):
|
||||
set_ready(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_reserved(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_sent(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_final(self.chain_spec, self.tx_hash, block=1042, fail=True, session=self.session)
|
||||
otx = Otx.load(self.tx_hash, session=self.session)
|
||||
self.assertFalse(is_alive(otx.status))
|
||||
self.assertTrue(is_error_status(otx.status))
|
||||
self.assertEqual(otx.status & StatusBits.NETWORK_ERROR, StatusBits.NETWORK_ERROR)
|
||||
|
||||
|
||||
def test_final_protected(self):
|
||||
set_ready(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_reserved(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_sent(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_final(self.chain_spec, self.tx_hash, block=1042, session=self.session)
|
||||
|
||||
otx = Otx.load(self.tx_hash, session=self.session)
|
||||
self.assertEqual(otx.status & StatusBits.FINAL, StatusBits.FINAL)
|
||||
|
||||
with self.assertRaises(TxStateChangeError):
|
||||
set_ready(self.chain_spec, self.tx_hash, session=self.session)
|
||||
|
||||
with self.assertRaises(TxStateChangeError):
|
||||
set_fubar(self.chain_spec, self.tx_hash, session=self.session)
|
||||
|
||||
with self.assertRaises(TxStateChangeError):
|
||||
set_rejected(self.chain_spec, self.tx_hash, session=self.session)
|
||||
|
||||
set_cancel(self.chain_spec, self.tx_hash, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertEqual(otx.status & StatusBits.OBSOLETE, 0)
|
||||
|
||||
set_cancel(self.chain_spec, self.tx_hash, manual=True, session=self.session)
|
||||
self.session.refresh(otx)
|
||||
self.assertEqual(otx.status & StatusBits.OBSOLETE, 0)
|
||||
|
||||
with self.assertRaises(TxStateChangeError):
|
||||
set_reserved(self.chain_spec, self.tx_hash, session=self.session)
|
||||
|
||||
with self.assertRaises(TxStateChangeError):
|
||||
set_waitforgas(self.chain_spec, self.tx_hash, session=self.session)
|
||||
|
||||
with self.assertRaises(TxStateChangeError):
|
||||
set_manual(self.chain_spec, self.tx_hash, session=self.session)
|
||||
|
||||
|
||||
def test_manual_persist(self):
|
||||
set_manual(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_ready(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_reserved(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_sent(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_final(self.chain_spec, self.tx_hash, block=1042, session=self.session)
|
||||
|
||||
otx = Otx.load(self.tx_hash, session=self.session)
|
||||
self.assertEqual(otx.status & StatusBits.MANUAL, StatusBits.MANUAL)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
35
tests/old/test_otx_status_log.py
Normal file
35
tests/old/test_otx_status_log.py
Normal file
@@ -0,0 +1,35 @@
|
||||
# standard imports
|
||||
import unittest
|
||||
|
||||
# local imports
|
||||
from chainqueue.db.models.otx import Otx
|
||||
from chainqueue.sql.state import *
|
||||
|
||||
# test imports
|
||||
from tests.chainqueue_base import TestOtxBase
|
||||
|
||||
|
||||
class TestOtxState(TestOtxBase):
|
||||
|
||||
|
||||
def setUp(self):
|
||||
super(TestOtxState, self).setUp()
|
||||
Otx.tracing = True
|
||||
logg.debug('state trace')
|
||||
|
||||
|
||||
def test_state_log(self):
|
||||
set_ready(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_reserved(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_sent(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_final(self.chain_spec, self.tx_hash, block=1042, session=self.session)
|
||||
|
||||
state_log = get_state_log(self.chain_spec, self.tx_hash)
|
||||
self.assertEqual(state_log[0][1], StatusEnum.READYSEND)
|
||||
self.assertEqual(state_log[1][1], StatusEnum.RESERVED)
|
||||
self.assertEqual(state_log[2][1], StatusEnum.SENT)
|
||||
self.assertEqual(state_log[3][1], StatusEnum.SUCCESS)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
408
tests/old/test_query.py
Normal file
408
tests/old/test_query.py
Normal file
@@ -0,0 +1,408 @@
|
||||
# standard imports
|
||||
import os
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
# external imports
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
)
|
||||
|
||||
# local imports
|
||||
from chainqueue.sql.query import *
|
||||
from chainqueue.sql.tx import create
|
||||
from chainqueue.sql.state import (
|
||||
set_waitforgas,
|
||||
set_ready,
|
||||
set_reserved,
|
||||
set_sent,
|
||||
set_final,
|
||||
)
|
||||
from chainqueue.enum import StatusBits
|
||||
|
||||
# test imports
|
||||
from tests.chainqueue_base import TestTxBase
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
class TestTxQuery(TestTxBase):
|
||||
|
||||
def test_get_tx(self):
|
||||
tx = get_tx(self.chain_spec, self.tx_hash)
|
||||
expected_keys = [
|
||||
'otx_id',
|
||||
'status',
|
||||
'signed_tx',
|
||||
'nonce',
|
||||
]
|
||||
for k in tx.keys():
|
||||
expected_keys.remove(k)
|
||||
|
||||
self.assertEqual(len(expected_keys), 0)
|
||||
|
||||
|
||||
def test_nonce_tx(self):
|
||||
|
||||
nonce_hashes = [self.tx_hash]
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
create(
|
||||
self.chain_spec,
|
||||
42,
|
||||
self.alice,
|
||||
tx_hash,
|
||||
signed_tx,
|
||||
session=self.session,
|
||||
)
|
||||
txc = TxCache(
|
||||
tx_hash,
|
||||
self.alice,
|
||||
self.bob,
|
||||
self.foo_token,
|
||||
self.bar_token,
|
||||
self.from_value,
|
||||
self.to_value,
|
||||
session=self.session,
|
||||
)
|
||||
self.session.add(txc)
|
||||
self.session.commit()
|
||||
|
||||
nonce_hashes.append(tx_hash)
|
||||
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
create(
|
||||
self.chain_spec,
|
||||
41,
|
||||
self.alice,
|
||||
tx_hash,
|
||||
signed_tx,
|
||||
session=self.session,
|
||||
)
|
||||
txc = TxCache(
|
||||
tx_hash,
|
||||
self.alice,
|
||||
self.bob,
|
||||
self.foo_token,
|
||||
self.bar_token,
|
||||
self.from_value,
|
||||
self.to_value,
|
||||
session=self.session,
|
||||
)
|
||||
self.session.add(txc)
|
||||
|
||||
txs = get_nonce_tx_cache(self.chain_spec, 42, self.alice)
|
||||
self.assertEqual(len(txs.keys()), 2)
|
||||
|
||||
for h in nonce_hashes:
|
||||
self.assertTrue(strip_0x(h) in txs)
|
||||
|
||||
|
||||
def test_paused_tx_cache(self):
|
||||
set_waitforgas(self.chain_spec, self.tx_hash)
|
||||
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
create(
|
||||
self.chain_spec,
|
||||
43,
|
||||
self.alice,
|
||||
tx_hash,
|
||||
signed_tx,
|
||||
session=self.session,
|
||||
)
|
||||
txc = TxCache(
|
||||
tx_hash,
|
||||
self.alice,
|
||||
self.bob,
|
||||
self.foo_token,
|
||||
self.bar_token,
|
||||
self.from_value,
|
||||
self.to_value,
|
||||
session=self.session,
|
||||
)
|
||||
self.session.add(txc)
|
||||
self.session.commit()
|
||||
|
||||
txs = get_paused_tx_cache(self.chain_spec, status=StatusBits.GAS_ISSUES, sender=self.alice, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 1)
|
||||
|
||||
txs = get_paused_tx_cache(self.chain_spec, status=StatusBits.GAS_ISSUES, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 1)
|
||||
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
create(
|
||||
self.chain_spec,
|
||||
42,
|
||||
self.bob,
|
||||
tx_hash,
|
||||
signed_tx,
|
||||
session=self.session,
|
||||
)
|
||||
txc = TxCache(
|
||||
tx_hash,
|
||||
self.bob,
|
||||
self.alice,
|
||||
self.bar_token,
|
||||
self.foo_token,
|
||||
self.to_value,
|
||||
self.from_value,
|
||||
session=self.session,
|
||||
)
|
||||
self.session.add(txc)
|
||||
self.session.commit()
|
||||
|
||||
txs = get_paused_tx_cache(self.chain_spec, status=StatusBits.GAS_ISSUES, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 1)
|
||||
|
||||
set_waitforgas(self.chain_spec, tx_hash)
|
||||
self.session.commit()
|
||||
|
||||
txs = get_paused_tx_cache(self.chain_spec, status=StatusBits.GAS_ISSUES, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 2)
|
||||
|
||||
txs = get_paused_tx_cache(self.chain_spec, status=StatusBits.GAS_ISSUES, sender=self.bob, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 1)
|
||||
|
||||
|
||||
def test_count(self):
|
||||
for i in range(3):
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
create(
|
||||
self.chain_spec,
|
||||
i,
|
||||
self.alice,
|
||||
tx_hash,
|
||||
signed_tx,
|
||||
session=self.session,
|
||||
)
|
||||
txc = TxCache(
|
||||
tx_hash,
|
||||
self.alice,
|
||||
self.bob,
|
||||
self.foo_token,
|
||||
self.bar_token,
|
||||
self.from_value,
|
||||
self.to_value,
|
||||
session=self.session,
|
||||
)
|
||||
self.session.add(txc)
|
||||
set_ready(self.chain_spec, tx_hash, session=self.session)
|
||||
set_reserved(self.chain_spec, tx_hash, session=self.session)
|
||||
if i > 0:
|
||||
set_sent(self.chain_spec, tx_hash, session=self.session)
|
||||
if i == 2:
|
||||
set_final(self.chain_spec, tx_hash, session=self.session)
|
||||
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
create(
|
||||
self.chain_spec,
|
||||
i,
|
||||
self.bob,
|
||||
tx_hash,
|
||||
signed_tx,
|
||||
session=self.session,
|
||||
)
|
||||
txc = TxCache(
|
||||
tx_hash,
|
||||
self.bob,
|
||||
self.carol,
|
||||
self.foo_token,
|
||||
self.bar_token,
|
||||
self.from_value,
|
||||
self.to_value,
|
||||
session=self.session,
|
||||
)
|
||||
|
||||
self.session.add(txc)
|
||||
set_ready(self.chain_spec, tx_hash, session=self.session)
|
||||
set_reserved(self.chain_spec, tx_hash, session=self.session)
|
||||
set_sent(self.chain_spec, tx_hash, session=self.session)
|
||||
self.session.commit()
|
||||
|
||||
self.assertEqual(count_tx(self.chain_spec, status=StatusBits.IN_NETWORK | StatusBits.FINAL, status_target=StatusBits.IN_NETWORK), 2)
|
||||
self.assertEqual(count_tx(self.chain_spec, sender=self.alice, status=StatusBits.IN_NETWORK | StatusBits.FINAL, status_target=StatusBits.IN_NETWORK), 1)
|
||||
|
||||
|
||||
def test_account_tx(self):
|
||||
|
||||
nonce_hashes = [self.tx_hash]
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
create(
|
||||
self.chain_spec,
|
||||
42,
|
||||
self.alice,
|
||||
tx_hash,
|
||||
signed_tx,
|
||||
session=self.session,
|
||||
)
|
||||
txc = TxCache(
|
||||
tx_hash,
|
||||
self.alice,
|
||||
self.bob,
|
||||
self.foo_token,
|
||||
self.bar_token,
|
||||
self.from_value,
|
||||
self.to_value,
|
||||
session=self.session,
|
||||
)
|
||||
self.session.add(txc)
|
||||
self.session.commit()
|
||||
|
||||
nonce_hashes.append(tx_hash)
|
||||
|
||||
time_between = datetime.datetime.utcnow()
|
||||
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
create(
|
||||
self.chain_spec,
|
||||
41,
|
||||
self.alice,
|
||||
tx_hash,
|
||||
signed_tx,
|
||||
session=self.session,
|
||||
)
|
||||
txc = TxCache(
|
||||
tx_hash,
|
||||
self.alice,
|
||||
self.bob,
|
||||
self.foo_token,
|
||||
self.bar_token,
|
||||
self.from_value,
|
||||
self.to_value,
|
||||
session=self.session,
|
||||
)
|
||||
self.session.add(txc)
|
||||
|
||||
nonce_hashes.append(tx_hash)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 3)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=tx_hash)
|
||||
self.assertEqual(len(txs.keys()), 1)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=nonce_hashes[0])
|
||||
self.assertEqual(len(txs.keys()), 3)
|
||||
|
||||
bogus_hash = add_0x(os.urandom(32).hex())
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=bogus_hash)
|
||||
self.assertEqual(len(txs.keys()), 0)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=time_between)
|
||||
self.assertEqual(len(txs.keys()), 1)
|
||||
|
||||
time_before = time_between - datetime.timedelta(hours=1)
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=time_before)
|
||||
self.assertEqual(len(txs.keys()), 3)
|
||||
|
||||
time_after = datetime.datetime.utcnow()
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=time_after)
|
||||
self.assertEqual(len(txs.keys()), 0)
|
||||
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=2)
|
||||
self.assertEqual(len(txs.keys()), 2)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=1)
|
||||
self.assertEqual(len(txs.keys()), 3)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=4)
|
||||
self.assertEqual(len(txs.keys()), 0)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=1, until=2)
|
||||
self.assertEqual(len(txs.keys()), 2)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=time_before, until=time_between)
|
||||
self.assertEqual(len(txs.keys()), 2)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, session=self.session, since=nonce_hashes[0], until=nonce_hashes[1])
|
||||
self.assertEqual(len(txs.keys()), 2)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, status=StatusBits.QUEUED, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 0)
|
||||
|
||||
set_ready(self.chain_spec, nonce_hashes[1], session=self.session)
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, status=StatusBits.QUEUED, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 1)
|
||||
|
||||
set_reserved(self.chain_spec, nonce_hashes[1], session=self.session)
|
||||
set_sent(self.chain_spec, nonce_hashes[1], session=self.session)
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, status=StatusBits.QUEUED, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 0)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, not_status=StatusBits.QUEUED, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 3)
|
||||
|
||||
txs = get_account_tx(self.chain_spec, self.alice, as_sender=True, as_recipient=False, not_status=StatusBits.QUEUED, status=StatusBits.IN_NETWORK, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 1)
|
||||
|
||||
def test_latest_txs(self):
|
||||
|
||||
nonce_hashes = [self.tx_hash]
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
create(
|
||||
self.chain_spec,
|
||||
42,
|
||||
self.alice,
|
||||
tx_hash,
|
||||
signed_tx,
|
||||
session=self.session,
|
||||
)
|
||||
txc = TxCache(
|
||||
tx_hash,
|
||||
self.alice,
|
||||
self.bob,
|
||||
self.foo_token,
|
||||
self.bar_token,
|
||||
self.from_value,
|
||||
self.to_value,
|
||||
session=self.session,
|
||||
)
|
||||
self.session.add(txc)
|
||||
self.session.commit()
|
||||
|
||||
nonce_hashes.append(tx_hash)
|
||||
|
||||
|
||||
tx_hash = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
create(
|
||||
self.chain_spec,
|
||||
41,
|
||||
self.alice,
|
||||
tx_hash,
|
||||
signed_tx,
|
||||
session=self.session,
|
||||
)
|
||||
txc = TxCache(
|
||||
tx_hash,
|
||||
self.alice,
|
||||
self.bob,
|
||||
self.foo_token,
|
||||
self.bar_token,
|
||||
self.from_value,
|
||||
self.to_value,
|
||||
session=self.session,
|
||||
)
|
||||
self.session.add(txc)
|
||||
|
||||
nonce_hashes.append(tx_hash)
|
||||
|
||||
txs = get_latest_txs(self.chain_spec, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 3)
|
||||
|
||||
txs = get_latest_txs(self.chain_spec, count=1, session=self.session)
|
||||
self.assertEqual(len(txs.keys()), 1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
44
tests/old/test_tx_cache.py
Normal file
44
tests/old/test_tx_cache.py
Normal file
@@ -0,0 +1,44 @@
|
||||
# standard imports
|
||||
import unittest
|
||||
|
||||
# external imports
|
||||
from hexathon import add_0x
|
||||
|
||||
# local imports
|
||||
from chainqueue.db.models.tx import TxCache
|
||||
from chainqueue.error import NotLocalTxError
|
||||
from chainqueue.sql.state import *
|
||||
from chainqueue.sql.query import get_tx_cache
|
||||
|
||||
# test imports
|
||||
from tests.chainqueue_base import TestTxBase
|
||||
|
||||
class TestTxCache(TestTxBase):
|
||||
|
||||
def test_mine(self):
|
||||
with self.assertRaises(NotLocalTxError):
|
||||
TxCache.set_final(self.tx_hash, 1024, 13, session=self.session)
|
||||
|
||||
set_ready(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_reserved(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_sent(self.chain_spec, self.tx_hash, session=self.session)
|
||||
set_final(self.chain_spec, self.tx_hash, block=1024, session=self.session)
|
||||
|
||||
with self.assertRaises(NotLocalTxError):
|
||||
TxCache.set_final(self.tx_hash, 1023, 13, session=self.session)
|
||||
|
||||
TxCache.set_final(self.tx_hash, 1024, 13, session=self.session)
|
||||
|
||||
self.session.commit()
|
||||
|
||||
txc = TxCache.load(self.tx_hash)
|
||||
self.assertEqual(txc.tx_index, 13)
|
||||
|
||||
|
||||
def test_get(self):
|
||||
tx_extended_dict = get_tx_cache(self.chain_spec, self.tx_hash)
|
||||
self.assertEqual(tx_extended_dict['tx_hash'], add_0x(self.tx_hash))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user