diff --git a/torba/tests/client_tests/unit/test_database.py b/torba/tests/client_tests/unit/test_database.py index b82d0d487..240933cc0 100644 --- a/torba/tests/client_tests/unit/test_database.py +++ b/torba/tests/client_tests/unit/test_database.py @@ -235,10 +235,20 @@ class TestQueries(AsyncioTestCase): self.ledger.db.db.execute_fetchall = check_parameters_length account = await self.create_account() - tx = await self.create_tx_from_nothing(account, 1) - for _ in range(1200): - tx = await self.create_tx_from_txo(tx.outputs[0], account, 1) - await self.ledger.db.get_transactions() + tx = await self.create_tx_from_nothing(account, 0) + for height in range(1200): + tx = await self.create_tx_from_txo(tx.outputs[0], account, height=height) + variable_limit = self.ledger.db.MAX_QUERY_VARIABLES + for limit in range(variable_limit-2, variable_limit+2): + txs = await self.ledger.db.get_transactions(limit=limit, order_by='height asc') + self.assertEqual(len(txs), limit) + inputs, outputs, last_tx = set(), set(), txs[0] + for tx in txs[1:]: + self.assertEqual(len(tx.inputs), 1) + self.assertEqual(tx.inputs[0].txo_ref.tx_ref.id, last_tx.id) + self.assertEqual(len(tx.outputs), 1) + last_tx = tx + async def test_queries(self): self.assertEqual(0, await self.ledger.db.get_address_count()) diff --git a/torba/torba/client/basedatabase.py b/torba/torba/client/basedatabase.py index 24b71a616..b050f5d6e 100644 --- a/torba/torba/client/basedatabase.py +++ b/torba/torba/client/basedatabase.py @@ -198,6 +198,7 @@ def rows_to_dict(rows, fields): class SQLiteMixin: CREATE_TABLES_QUERY: str + MAX_QUERY_VARIABLES = 900 def __init__(self, path): self._db_path = path @@ -396,7 +397,7 @@ class BaseDatabase(SQLiteMixin): tx_rows = await self.select_transactions( 'txid, raw, height, position, is_verified', - order_by=["height=0 DESC", "height DESC", "position DESC"], + order_by=constraints.pop('order_by', ["height=0 DESC", "height DESC", "position DESC"]), **constraints ) @@ -412,23 +413,24 @@ class BaseDatabase(SQLiteMixin): for txi in txs[-1].inputs: txi_txoids.append(txi.txo_ref.id) + step = self.MAX_QUERY_VARIABLES annotated_txos = {} - for offset in range(0, len(txids), 900): + for offset in range(0, len(txids), step): annotated_txos.update({ txo.id: txo for txo in (await self.get_txos( my_account=my_account, - txid__in=txids[offset:offset+900], + txid__in=txids[offset:offset+step], )) }) referenced_txos = {} - for offset in range(0, len(txi_txoids), 900): + for offset in range(0, len(txi_txoids), step): referenced_txos.update({ txo.id: txo for txo in (await self.get_txos( my_account=my_account, - txoid__in=txi_txoids[offset:offset+900], + txoid__in=txi_txoids[offset:offset+step], )) })