chainqueue

Blockchain transaction queue control
Log | Files | Refs | LICENSE

commit b50145082713e6af6da8c09d855c2ca5aa5495f1
parent a5078a7e37deed45d7fa279c95590ee916db7c45
Author: nolash <dev@holbrook.no>
Date:   Fri,  2 Apr 2021 10:48:54 +0200

Add tx cache test

Diffstat:
Mchainqueue/db/migrations/default/versions/2215c497248b_transaction_cache.py | 1+
Mchainqueue/db/models/otx.py | 7-------
Mchainqueue/db/models/tx.py | 14+++++++-------
Achainqueue/error.py | 4++++
Mtests/test_basic.py | 32++++++++++++++++++++++++++++++++
5 files changed, 44 insertions(+), 14 deletions(-)

diff --git a/chainqueue/db/migrations/default/versions/2215c497248b_transaction_cache.py b/chainqueue/db/migrations/default/versions/2215c497248b_transaction_cache.py @@ -23,6 +23,7 @@ def upgrade(): sa.Column('otx_id', sa.Integer, sa.ForeignKey('otx.id'), nullable=True), sa.Column('date_created', sa.DateTime, nullable=False), sa.Column('date_updated', sa.DateTime, nullable=False), + sa.Column('date_checked', sa.DateTime, nullable=False), sa.Column('source_token_address', sa.String(42), nullable=False), sa.Column('destination_token_address', sa.String(42), nullable=False), sa.Column('sender', sa.String(42), nullable=False), diff --git a/chainqueue/db/models/otx.py b/chainqueue/db/models/otx.py @@ -562,10 +562,3 @@ class Otx(SessionBase): self.tx_hash = strip_0x(tx_hash) self.signed_tx = strip_0x(signed_tx) self.status = StatusEnum.PENDING - #signed_tx_bytes = bytes.fromhex(strip_0x(signed_tx)) - #signed_tx_bytes = bytes.fromhex(strip_0x(tx_hash)) - - # sender_address = address_hex_from_signed_tx(signed_tx_bytes) - # logg.debug('decoded tx {}'.format(sender_address)) - - diff --git a/chainqueue/db/models/tx.py b/chainqueue/db/models/tx.py @@ -1,18 +1,18 @@ # standard imports import datetime -# third-party imports +# external imports from sqlalchemy import Column, String, Integer, DateTime, Enum, ForeignKey, Boolean, NUMERIC from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property -#from sqlalchemy.orm import relationship, backref -#from sqlalchemy.ext.declarative import declarative_base +from hexathon import ( + strip_0x, + ) # local imports from .base import SessionBase from .otx import Otx -from cic_eth.db.util import num_serialize -from cic_eth.error import NotLocalTxError -from cic_eth.db.error import TxStateChangeError +from chainqueue.error import NotLocalTxError +from chainqueue.db.error import TxStateChangeError class TxCache(SessionBase): @@ -127,7 +127,7 @@ class TxCache(SessionBase): def __init__(self, tx_hash, sender, recipient, source_token_address, destination_token_address, from_value, to_value, block_number=None, tx_index=None, session=None): session = SessionBase.bind_session(session) q = session.query(Otx) - q = q.filter(Otx.tx_hash==tx_hash) + q = q.filter(Otx.tx_hash==strip_0x(tx_hash)) tx = q.first() if tx == None: SessionBase.release_session(session) diff --git a/chainqueue/error.py b/chainqueue/error.py @@ -0,0 +1,4 @@ +class NotLocalTxError(Exception): + """Exception raised when trying to access a tx not originated from a local task + """ + pass diff --git a/tests/test_basic.py b/tests/test_basic.py @@ -11,6 +11,7 @@ from hexathon import ( # local imports from chainqueue.db.models.otx import Otx +from chainqueue.db.models.tx import TxCache # test imports from tests.base import TestBase @@ -33,5 +34,36 @@ class TestBasic(TestBase): 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) + 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()