Add tx count query

This commit is contained in:
nolash 2021-06-04 19:37:12 +02:00
parent 2fd1c51b5c
commit eee7bf0ee2
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 85 additions and 1 deletions

View File

@ -339,3 +339,18 @@ def get_account_tx(chain_spec, address, as_sender=True, as_recipient=True, count
SessionBase.release_session(session) SessionBase.release_session(session)
return txs return txs
def count_tx(chain_spec, address=None, status=None, status_target=None, session=None):
session = SessionBase.bind_session(session)
q = session.query(Otx.id)
q = q.join(TxCache)
if status != None:
if status_target == None:
status_target = status
q = q.filter(Otx.status.op('&')(status)==status_target)
if address != None:
q = q.filter(TxCache.sender==address)
result = q.count()
SessionBase.release_session(session)
return result

View File

@ -88,6 +88,7 @@ class TestTxBase(TestOtxBase):
def setUp(self): def setUp(self):
super(TestTxBase, self).setUp() super(TestTxBase, self).setUp()
self.bob = add_0x(os.urandom(20).hex()) 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.foo_token = add_0x(os.urandom(20).hex())
self.bar_token = add_0x(os.urandom(20).hex()) self.bar_token = add_0x(os.urandom(20).hex())
self.from_value = 42 self.from_value = 42

View File

@ -12,7 +12,14 @@ from hexathon import (
# local imports # local imports
from chainqueue.sql.query import * from chainqueue.sql.query import *
from chainqueue.sql.tx import create from chainqueue.sql.tx import create
from chainqueue.sql.state import set_waitforgas from chainqueue.sql.state import (
set_waitforgas,
set_ready,
set_reserved,
set_sent,
set_final,
)
from chainqueue.enum import StatusBits
# test imports # test imports
from tests.base import TestTxBase from tests.base import TestTxBase
@ -162,5 +169,66 @@ class TestTxQuery(TestTxBase):
self.assertEqual(len(txs.keys()), 1) 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, address=self.alice, status=StatusBits.IN_NETWORK | StatusBits.FINAL, status_target=StatusBits.IN_NETWORK), 1)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()