diff --git a/bigchaindb/assets.py b/bigchaindb/assets.py index f80ab031..68a667e9 100644 --- a/bigchaindb/assets.py +++ b/bigchaindb/assets.py @@ -1,35 +1,31 @@ import rethinkdb as r -from bigchaindb.exceptions import AssetIdMismatch, TransactionDoesNotExist, AmountError +from bigchaindb.exceptions import AssetIdMismatch, AmountError -def get_asset_id(txids, bigchain): +def get_asset_id(transactions): """Get the asset id from a list of transaction ids. This is useful when we want to check if the multiple inputs of a transaction are related to the same asset id. Args: - txids (list): list of transaction ids. + transactions (list): list of transaction usually inputs that should have a matching asset_id Returns: str: uuid of the asset. Raises: AssetIdMismatch: If the inputs are related to different assets. - TransactionDoesNotExist: If one of the txids does not exist on the bigchain. """ - if not isinstance(txids, list): - txids = [txids] + if not isinstance(transactions, list): + transactions = [transactions] - asset_ids = set() - for txid in set(txids): - tx = bigchain.get_transaction(txid) - if tx is None: - raise TransactionDoesNotExist('Transaction with txid `{}` does not exist in the bigchain'.format(txid)) - asset_ids.add(tx['transaction']['asset']['id']) + # create a set of asset_ids + asset_ids = {tx['transaction']['asset']['id'] for tx in transactions} + # check that all the transasctions have the same asset_id if len(asset_ids) > 1: raise AssetIdMismatch("All inputs of a transaction need to have the same asset id.") return asset_ids.pop() diff --git a/bigchaindb/util.py b/bigchaindb/util.py index 007d8f7e..e853e314 100644 --- a/bigchaindb/util.py +++ b/bigchaindb/util.py @@ -152,8 +152,12 @@ def is_genesis_block(block): # we cannot have empty blocks, there will always be at least one # element in the list so we can safely refer to it +<<<<<<< e00a34e943b596c2302d3911929c052767008611 # TODO: Remove this try-except and only handle `Block` as input try: return block.transactions[0].operation == 'GENESIS' except AttributeError: return block['block']['transactions'][0]['transaction']['operation'] == 'GENESIS' +======= + return block['block']['transactions'][0]['transaction']['operation'] == 'GENESIS' +>>>>>>> Remove unnecessart database queries in get_asset_id diff --git a/tests/assets/test_digital_assets.py b/tests/assets/test_digital_assets.py index efe00ec8..2f48f9a3 100644 --- a/tests/assets/test_digital_assets.py +++ b/tests/assets/test_digital_assets.py @@ -120,7 +120,7 @@ def test_get_asset_id_create_transaction(b, user_vk): tx_input = b.get_owned_ids(user_vk).pop() tx_create = b.get_transaction(tx_input['txid']) - asset_id = get_asset_id(tx_input['txid'], bigchain=b) + asset_id = get_asset_id(tx_create) assert asset_id == tx_create['transaction']['asset']['id'] @@ -139,7 +139,7 @@ def test_get_asset_id_transfer_transaction(b, user_vk, user_sk): # vote the block valid vote = b.vote(block['id'], b.get_last_voted_block()['id'], True) b.write_vote(vote) - asset_id = get_asset_id(tx_transfer['id'], bigchain=b) + asset_id = get_asset_id(tx_transfer) assert asset_id == tx_transfer['transaction']['asset']['id'] @@ -150,9 +150,11 @@ def test_asset_id_mismatch(b, user_vk): from bigchaindb.exceptions import AssetIdMismatch tx_input1, tx_input2 = b.get_owned_ids(user_vk)[:2] + tx1 = b.get_transaction(tx_input1['txid']) + tx2 = b.get_transaction(tx_input2['txid']) with pytest.raises(AssetIdMismatch): - get_asset_id([tx_input1['txid'], tx_input2['txid']], bigchain=b) + get_asset_id([tx1, tx2]) def test_get_asset_id_transaction_does_not_exist(b, user_vk): diff --git a/tests/db/test_bigchain_api.py b/tests/db/test_bigchain_api.py index 6874f3db..b35d5c48 100644 --- a/tests/db/test_bigchain_api.py +++ b/tests/db/test_bigchain_api.py @@ -527,7 +527,7 @@ class TestBigchainApi(object): with pytest.raises(exceptions.TransactionDoesNotExist) as excinfo: b.create_transaction(user_vk, user_vk, {'txid': 'c', 'cid': 0}, 'TRANSFER') - assert excinfo.value.args[0] == 'Transaction with txid `c` does not exist in the bigchain' + assert excinfo.value.args[0] == 'input with txid `c` does not exist in the bigchain' # Create transaction does not let you create a malformed transaction. # Create a custom malformed transaction and check if validate catches the error