diff --git a/bigchaindb/backend/localmongodb/query.py b/bigchaindb/backend/localmongodb/query.py index 2cf68813..c6d97263 100644 --- a/bigchaindb/backend/localmongodb/query.py +++ b/bigchaindb/backend/localmongodb/query.py @@ -120,29 +120,19 @@ def store_block(conn, block): @register_query(LocalMongoDBConnection) -def get_txids_filtered(conn, asset_id, operation=None): - match_create = { - 'operation': 'CREATE', - 'id': asset_id - } - match_transfer = { - 'operation': 'TRANSFER', - 'asset.id': asset_id - } +def get_txids_filtered(conn, asset_id, operation=None, last_tx=None): - if operation == Transaction.CREATE: - match = match_create - elif operation == Transaction.TRANSFER: - match = match_transfer - else: - match = {'$or': [match_create, match_transfer]} + match = { + Transaction.CREATE: {'operation': 'CREATE', 'id': asset_id}, + Transaction.TRANSFER: {'operation': 'TRANSFER', 'asset.id': asset_id}, + None: {'$or': [{'asset.id': asset_id}, {'id': asset_id}]}, + }[operation] + + cursor = conn.run(conn.collection('transactions').find(match)) + + if last_tx: + cursor = cursor.sort({'$natural': DESCENDING}).limit(1) - pipeline = [ - {'$match': match} - ] - cursor = conn.run( - conn.collection('transactions') - .aggregate(pipeline)) return (elem['id'] for elem in cursor) diff --git a/bigchaindb/lib.py b/bigchaindb/lib.py index 938466e3..a379c8db 100644 --- a/bigchaindb/lib.py +++ b/bigchaindb/lib.py @@ -265,7 +265,7 @@ class BigchainDB(object): """Get a list of transactions filtered on some criteria """ txids = backend.query.get_txids_filtered(self.connection, asset_id, - operation) + operation, last_tx) for txid in txids: yield self.get_transaction(txid) diff --git a/bigchaindb/web/views/transactions.py b/bigchaindb/web/views/transactions.py index e3baff27..c6b0e350 100644 --- a/bigchaindb/web/views/transactions.py +++ b/bigchaindb/web/views/transactions.py @@ -53,17 +53,8 @@ class TransactionListApi(Resource): args = parser.parse_args() with current_app.config['bigchain_pool']() as bigchain: txs = bigchain.get_transactions_filtered(**args) - if args['last_tx'] and args['last_tx'] is True: - lastTX = None - for x in txs: - lastTX = x - if lastTX: - return [lastTX.to_dict()] - else: - return [] - else: - return [tx.to_dict() for tx in txs] + return [tx.to_dict() for tx in txs] def post(self): """API endpoint to push transactions to the Federation. diff --git a/tests/web/test_transactions.py b/tests/web/test_transactions.py index ef3653b9..5ef019a4 100644 --- a/tests/web/test_transactions.py +++ b/tests/web/test_transactions.py @@ -391,6 +391,12 @@ def test_transactions_get_list_good(client): ['last_tx', None], ['operation', 'CREATE'] ] + url = TX_ENDPOINT + '?asset_id=' + asset_id + '&last_tx=true' + assert client.get(url).json == [ + ['asset_id', asset_id], + ['last_tx', True], + ['operation', None] + ] def test_transactions_get_list_bad(client):