2021-04-02 11:51:00 +02:00
|
|
|
# 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.state import *
|
|
|
|
from chainqueue.db.enum import (
|
|
|
|
is_alive,
|
|
|
|
is_error_status,
|
|
|
|
)
|
|
|
|
|
|
|
|
# test imports
|
2021-04-02 14:02:22 +02:00
|
|
|
from tests.base import TestOtxBase
|
2021-04-02 11:51:00 +02:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
2021-04-02 14:02:22 +02:00
|
|
|
class TestOtx(TestOtxBase):
|
2021-04-02 11:51:00 +02:00
|
|
|
|
|
|
|
def test_ideal_state_sequence(self):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_ready(self.chain_spec, self.tx_hash)
|
2021-04-02 11:51:00 +02:00
|
|
|
otx = Otx.load(self.tx_hash, session=self.session)
|
|
|
|
self.assertEqual(otx.status, StatusBits.QUEUED)
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_reserved(self.chain_spec, self.tx_hash)
|
2021-04-02 11:51:00 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertEqual(otx.status, StatusBits.RESERVED)
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_sent(self.chain_spec, self.tx_hash)
|
2021-04-02 11:51:00 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertEqual(otx.status, StatusBits.IN_NETWORK)
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_final(self.chain_spec, self.tx_hash, block=1024)
|
2021-04-02 11:51:00 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertFalse(is_alive(otx.status))
|
|
|
|
self.assertFalse(is_error_status(otx.status))
|
|
|
|
|
|
|
|
|
2021-04-02 13:04:31 +02:00
|
|
|
def test_send_fail_and_retry(self):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_ready(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
otx = Otx.load(self.tx_hash, session=self.session)
|
|
|
|
self.assertEqual(otx.status, StatusBits.QUEUED)
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_reserved(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertEqual(otx.status, StatusBits.RESERVED)
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_sent(self.chain_spec, self.tx_hash, fail=True)
|
2021-04-02 13:04:31 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertTrue(is_error_status(otx.status))
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_ready(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertEqual(otx.status & StatusBits.QUEUED, StatusBits.QUEUED)
|
|
|
|
self.assertTrue(is_error_status(otx.status))
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_reserved(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertEqual(otx.status & StatusBits.RESERVED, StatusBits.RESERVED)
|
|
|
|
self.assertTrue(is_error_status(otx.status))
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_sent(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertEqual(otx.status, StatusBits.IN_NETWORK)
|
|
|
|
self.assertFalse(is_error_status(otx.status))
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_final(self.chain_spec, self.tx_hash, block=1024)
|
2021-04-02 13:04:31 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertFalse(is_alive(otx.status))
|
|
|
|
self.assertFalse(is_error_status(otx.status))
|
|
|
|
|
|
|
|
|
|
|
|
def test_fubar(self):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_ready(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
otx = Otx.load(self.tx_hash, session=self.session)
|
|
|
|
self.assertEqual(otx.status, StatusBits.QUEUED)
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_reserved(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertEqual(otx.status & StatusBits.RESERVED, StatusBits.RESERVED)
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_fubar(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
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):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_ready(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
otx = Otx.load(self.tx_hash, session=self.session)
|
|
|
|
self.assertEqual(otx.status, StatusBits.QUEUED)
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_reserved(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertEqual(otx.status & StatusBits.RESERVED, StatusBits.RESERVED)
|
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_rejected(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
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):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_ready(self.chain_spec, self.tx_hash)
|
|
|
|
set_reserved(self.chain_spec, self.tx_hash)
|
|
|
|
set_sent(self.chain_spec, self.tx_hash)
|
|
|
|
set_final(self.chain_spec, self.tx_hash, block=1042, fail=True)
|
2021-04-02 13:04:31 +02:00
|
|
|
otx = Otx.load(self.tx_hash, session=self.session)
|
|
|
|
self.assertFalse(is_alive(otx.status))
|
|
|
|
self.assertTrue(is_error_status(otx.status))
|
2021-04-02 14:02:22 +02:00
|
|
|
self.assertEqual(otx.status & StatusBits.NETWORK_ERROR, StatusBits.NETWORK_ERROR)
|
2021-04-02 13:04:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_final_protected(self):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_ready(self.chain_spec, self.tx_hash)
|
|
|
|
set_reserved(self.chain_spec, self.tx_hash)
|
|
|
|
set_sent(self.chain_spec, self.tx_hash)
|
|
|
|
set_final(self.chain_spec, self.tx_hash, block=1042)
|
2021-04-02 13:04:31 +02:00
|
|
|
|
|
|
|
otx = Otx.load(self.tx_hash, session=self.session)
|
|
|
|
self.assertEqual(otx.status & StatusBits.FINAL, StatusBits.FINAL)
|
|
|
|
|
|
|
|
with self.assertRaises(TxStateChangeError):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_ready(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
|
|
|
|
with self.assertRaises(TxStateChangeError):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_fubar(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
|
|
|
|
with self.assertRaises(TxStateChangeError):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_rejected(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_cancel(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertEqual(otx.status & StatusBits.OBSOLETE, 0)
|
2021-04-02 14:02:22 +02:00
|
|
|
|
2021-04-06 10:33:23 +02:00
|
|
|
set_cancel(self.chain_spec, self.tx_hash, manual=True)
|
2021-04-02 14:02:22 +02:00
|
|
|
self.session.refresh(otx)
|
|
|
|
self.assertEqual(otx.status & StatusBits.OBSOLETE, 0)
|
2021-04-02 13:04:31 +02:00
|
|
|
|
|
|
|
with self.assertRaises(TxStateChangeError):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_reserved(self.chain_spec, self.tx_hash)
|
2021-04-02 13:04:31 +02:00
|
|
|
|
2021-04-02 14:02:22 +02:00
|
|
|
with self.assertRaises(TxStateChangeError):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_waitforgas(self.chain_spec, self.tx_hash)
|
2021-04-02 14:02:22 +02:00
|
|
|
|
|
|
|
with self.assertRaises(TxStateChangeError):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_manual(self.chain_spec, self.tx_hash)
|
2021-04-02 14:02:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_manual_persist(self):
|
2021-04-06 10:33:23 +02:00
|
|
|
set_manual(self.chain_spec, self.tx_hash)
|
|
|
|
set_ready(self.chain_spec, self.tx_hash)
|
|
|
|
set_reserved(self.chain_spec, self.tx_hash)
|
|
|
|
set_sent(self.chain_spec, self.tx_hash)
|
|
|
|
set_final(self.chain_spec, self.tx_hash, block=1042)
|
2021-04-02 14:02:22 +02:00
|
|
|
|
|
|
|
otx = Otx.load(self.tx_hash, session=self.session)
|
|
|
|
self.assertEqual(otx.status & StatusBits.MANUAL, StatusBits.MANUAL)
|
|
|
|
|
2021-04-02 13:04:31 +02:00
|
|
|
|
2021-04-02 11:51:00 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|