diff --git a/bigchaindb/core.py b/bigchaindb/core.py index a520bba4..e18ec46d 100644 --- a/bigchaindb/core.py +++ b/bigchaindb/core.py @@ -417,6 +417,19 @@ class Bigchain(object): return owned + def get_transactions_filtered(self, asset_id=None, operation=None): + """ + Get a list of transactions filtered on some criteria + """ + if not asset_id: + raise ValueError("Need asset_id") + txids = backend.query.get_txids_filtered(self.connection, asset_id, + operation) + for txid in txids: + tx, status = self.get_transaction(txid, True) + if status == self.TX_VALID: + yield tx + def create_block(self, validated_transactions): """Creates a block given a list of `validated_transactions`. diff --git a/tests/test_txlist.py b/tests/test_txlist.py new file mode 100644 index 00000000..6f9f203a --- /dev/null +++ b/tests/test_txlist.py @@ -0,0 +1,66 @@ +""" +Test getting a list of transactions from the backend. + +This test module defines it's own fixture which is used by all the tests. +""" +import pytest + + +@pytest.fixture +def txlist(b, user_pk, user2_pk, user_sk, user2_sk, genesis_block): + from bigchaindb.models import Transaction + prev_block_id = genesis_block.id + + # Create first block with CREATE transactions + create1 = Transaction.create([user_pk], [([user2_pk], 6)]) \ + .sign([user_sk]) + create2 = Transaction.create([user2_pk], + [([user2_pk], 5), ([user_pk], 5)]) \ + .sign([user2_sk]) + block1 = b.create_block([create1, create2]) + b.write_block(block1) + + # Create second block with TRANSFER transactions + transfer1 = Transaction.transfer(create1.to_inputs(), + [([user_pk], 8)], + create1.id).sign([user2_sk]) + block2 = b.create_block([transfer1]) + b.write_block(block2) + + # Create block with double spend + tx_doublespend = Transaction.transfer(create1.to_inputs(), + [([user_pk], 9)], + create1.id).sign([user2_sk]) + block_doublespend = b.create_block([tx_doublespend]) + b.write_block(block_doublespend) + + # Vote on all the blocks + prev_block_id = genesis_block.id + for bid in [block1.id, block2.id]: + vote = b.vote(bid, prev_block_id, True) + prev_block_id = bid + b.write_vote(vote) + + # Create undecided block + untx = Transaction.create([user_pk], [([user2_pk], 7)]) \ + .sign([user_sk]) + block_undecided = b.create_block([untx]) + b.write_block(block_undecided) + + return type('', (), { + 'create1': create1, + 'transfer1': transfer1, + }) + + +@pytest.mark.bdb +def test_get_txlist_by_asset(b, txlist): + res = b.get_transactions_filtered(txlist.create1.id) + assert set(tx.id for tx in res) == set([txlist.transfer1.id, + txlist.create1.id]) + + +@pytest.mark.bdb +def test_get_txlist_by_operation(b, txlist): + res = b.get_transactions_filtered(txlist.create1.id, operation='CREATE') + assert set(tx.id for tx in res) == {txlist.create1.id}