Remove unnecessart database queries in get_asset_id

This commit is contained in:
Rodolphe Marques 2016-09-09 10:41:38 +02:00 committed by tim
parent e00a34e943
commit 148e30a84b
4 changed files with 18 additions and 16 deletions

View File

@ -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()

View File

@ -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

View File

@ -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):

View File

@ -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