Add state log, tx cache test, add final setter on tx cache

This commit is contained in:
nolash
2021-04-02 14:02:22 +02:00
parent a219f3272e
commit 5ea1b15c53
11 changed files with 226 additions and 43 deletions

View File

@@ -6,13 +6,17 @@ 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
import alembic
import alembic.config
from hexathon import add_0x
# local imports
from chainqueue.db import dsn_from_config
from chainqueue.db.models.base import SessionBase
from chainqueue.tx import create
script_dir = os.path.realpath(os.path.dirname(__file__))
@@ -57,3 +61,44 @@ class TestBase(unittest.TestCase):
def tearDown(self):
self.session.commit()
self.session.close()
class TestOtxBase(TestBase):
def setUp(self):
super(TestOtxBase, self).setUp()
self.tx_hash = add_0x(os.urandom(32).hex())
self.tx = add_0x(os.urandom(128).hex())
self.nonce = 42
self.alice = add_0x(os.urandom(20).hex())
tx_hash = create(self.nonce, self.alice, self.tx_hash, self.tx, self.chain_spec, session=self.session)
self.assertEqual(tx_hash, self.tx_hash)
class TestTxBase(TestOtxBase):
def setUp(self):
super(TestTxBase, self).setUp()
self.bob = 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
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)
self.assertEqual(txc.otx_id, otx.id)

View File

@@ -4,16 +4,10 @@ import logging
import unittest
# external imports
from hexathon import (
strip_0x,
add_0x,
)
from chainlib.chain import ChainSpec
# local imports
from chainqueue.db.models.otx import Otx
from chainqueue.db.models.tx import TxCache
from chainqueue.tx import create
from chainqueue.state import *
from chainqueue.db.enum import (
is_alive,
@@ -21,24 +15,13 @@ from chainqueue.db.enum import (
)
# test imports
from tests.base import TestBase
from tests.base import TestOtxBase
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class TestOtx(TestBase):
def setUp(self):
super(TestOtx, self).setUp()
self.tx_hash = add_0x(os.urandom(32).hex())
self.tx = add_0x(os.urandom(128).hex())
self.nonce = 42
self.alice = add_0x(os.urandom(20).hex())
tx_hash = create(self.nonce, self.alice, self.tx_hash, self.tx, self.chain_spec, session=self.session)
self.assertEqual(tx_hash, self.tx_hash)
class TestOtx(TestOtxBase):
def test_ideal_state_sequence(self):
set_ready(self.tx_hash)
@@ -131,6 +114,7 @@ class TestOtx(TestBase):
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):
@@ -154,10 +138,31 @@ class TestOtx(TestBase):
set_cancel(self.tx_hash)
self.session.refresh(otx)
self.assertEqual(otx.status & StatusBits.OBSOLETE, 0)
set_cancel(self.tx_hash, manual=True)
self.session.refresh(otx)
self.assertEqual(otx.status & StatusBits.OBSOLETE, 0)
with self.assertRaises(TxStateChangeError):
set_reserved(self.tx_hash)
with self.assertRaises(TxStateChangeError):
set_waitforgas(self.tx_hash)
with self.assertRaises(TxStateChangeError):
set_manual(self.tx_hash)
def test_manual_persist(self):
set_manual(self.tx_hash)
set_ready(self.tx_hash)
set_reserved(self.tx_hash)
set_sent(self.tx_hash)
set_final(self.tx_hash, block=1042)
otx = Otx.load(self.tx_hash, session=self.session)
self.assertEqual(otx.status & StatusBits.MANUAL, StatusBits.MANUAL)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,35 @@
# standard imports
import unittest
# local imports
from chainqueue.db.models.otx import Otx
from chainqueue.state import *
# test imports
from tests.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.tx_hash)
set_reserved(self.tx_hash)
set_sent(self.tx_hash)
set_final(self.tx_hash, block=1042)
state_log = get_state_log(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()

35
tests/test_tx_cache.py Normal file
View File

@@ -0,0 +1,35 @@
# standard imports
import unittest
# local imports
from chainqueue.db.models.tx import TxCache
from chainqueue.error import NotLocalTxError
from chainqueue.state import *
# test imports
from tests.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.tx_hash)
set_reserved(self.tx_hash)
set_sent(self.tx_hash)
set_final(self.tx_hash, block=1024)
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)
if __name__ == '__main__':
unittest.main()