2016-08-22 18:00:38 +02:00
|
|
|
import pytest
|
|
|
|
from ..db.conftest import inputs
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures('inputs')
|
|
|
|
def test_asset_transfer(b, user_vk, user_sk):
|
2016-10-07 15:46:21 +02:00
|
|
|
from bigchaindb.models import Transaction
|
|
|
|
|
2016-08-22 18:00:38 +02:00
|
|
|
tx_input = b.get_owned_ids(user_vk).pop()
|
2016-10-07 15:46:21 +02:00
|
|
|
tx_create = b.get_transaction(tx_input.txid)
|
|
|
|
|
|
|
|
tx_transfer = Transaction.transfer(tx_create.to_inputs(), [user_vk], tx_create.asset)
|
|
|
|
tx_transfer_signed = tx_transfer.sign([user_sk])
|
2016-08-22 18:00:38 +02:00
|
|
|
|
2016-10-07 15:46:21 +02:00
|
|
|
assert tx_transfer_signed.validate(b) == tx_transfer_signed
|
|
|
|
assert tx_transfer_signed.asset.data_id == tx_create.asset.data_id
|
2016-08-22 18:00:38 +02:00
|
|
|
|
2016-08-23 15:53:39 +02:00
|
|
|
|
2016-08-24 15:57:24 +02:00
|
|
|
def test_validate_bad_asset_creation(b, user_vk):
|
2016-10-07 15:46:21 +02:00
|
|
|
from bigchaindb_common.exceptions import AmountError
|
|
|
|
from bigchaindb.models import Transaction
|
2016-08-24 15:57:24 +02:00
|
|
|
|
2016-10-07 15:46:21 +02:00
|
|
|
tx = Transaction.create([b.me], [user_vk])
|
|
|
|
tx.asset.divisible = 1
|
|
|
|
tx_signed = tx.sign([b.me_private])
|
2016-08-24 15:57:24 +02:00
|
|
|
with pytest.raises(TypeError):
|
2016-10-07 15:46:21 +02:00
|
|
|
tx_signed.validate(b)
|
2016-08-24 15:57:24 +02:00
|
|
|
|
|
|
|
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
|
|
|
|
tx['transaction']['asset'].update({'refillable': 1})
|
|
|
|
tx['id'] = get_hash_data(tx['transaction'])
|
|
|
|
tx_signed = b.sign_transaction(tx, b.me_private)
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
b.validate_transaction(tx_signed)
|
|
|
|
|
|
|
|
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
|
|
|
|
tx['transaction']['asset'].update({'updatable': 1})
|
|
|
|
tx['id'] = get_hash_data(tx['transaction'])
|
|
|
|
tx_signed = b.sign_transaction(tx, b.me_private)
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
b.validate_transaction(tx_signed)
|
|
|
|
|
|
|
|
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
|
|
|
|
tx['transaction']['asset'].update({'data': 'a'})
|
|
|
|
tx['id'] = get_hash_data(tx['transaction'])
|
|
|
|
tx_signed = b.sign_transaction(tx, b.me_private)
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
b.validate_transaction(tx_signed)
|
|
|
|
|
|
|
|
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
|
|
|
|
tx['transaction']['conditions'][0]['amount'] = 'a'
|
|
|
|
tx['id'] = get_hash_data(tx['transaction'])
|
|
|
|
tx_signed = b.sign_transaction(tx, b.me_private)
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
b.validate_transaction(tx_signed)
|
|
|
|
|
|
|
|
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
|
|
|
|
tx['transaction']['conditions'][0]['amount'] = 2
|
|
|
|
tx['transaction']['asset'].update({'divisible': False})
|
|
|
|
tx['id'] = get_hash_data(tx['transaction'])
|
|
|
|
tx_signed = b.sign_transaction(tx, b.me_private)
|
|
|
|
with pytest.raises(AmountError):
|
|
|
|
b.validate_transaction(tx_signed)
|
|
|
|
|
|
|
|
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
|
|
|
|
tx['transaction']['conditions'][0]['amount'] = 0
|
|
|
|
tx['id'] = get_hash_data(tx['transaction'])
|
|
|
|
tx_signed = b.sign_transaction(tx, b.me_private)
|
|
|
|
with pytest.raises(AmountError):
|
|
|
|
b.validate_transaction(tx_signed)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures('inputs')
|
2016-09-09 10:53:28 +02:00
|
|
|
def test_validate_transfer_asset_id_mismatch(b, user_vk, user_sk):
|
2016-08-24 15:57:24 +02:00
|
|
|
from bigchaindb.util import get_hash_data
|
2016-10-07 15:46:21 +02:00
|
|
|
from bigchaindb_common.exceptions import AssetIdMismatch
|
2016-08-24 15:57:24 +02:00
|
|
|
|
|
|
|
tx_input = b.get_owned_ids(user_vk).pop()
|
|
|
|
tx = b.create_transaction(user_vk, user_vk, tx_input, 'TRANFER')
|
2016-08-24 16:12:21 +02:00
|
|
|
tx['transaction']['asset']['id'] = 'aaa'
|
2016-08-24 15:57:24 +02:00
|
|
|
tx['id'] = get_hash_data(tx['transaction'])
|
|
|
|
tx_signed = b.sign_transaction(tx, user_sk)
|
|
|
|
with pytest.raises(AssetIdMismatch):
|
|
|
|
b.validate_transaction(tx_signed)
|
|
|
|
|
|
|
|
|
2016-08-23 15:53:39 +02:00
|
|
|
def test_validate_asset_arguments(b):
|
2016-10-07 15:46:21 +02:00
|
|
|
from bigchaindb_common.exceptions import AmountError
|
2016-08-23 15:53:39 +02:00
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
b.create_transaction(b.me, b.me, None, 'CREATE', divisible=1)
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
b.create_transaction(b.me, b.me, None, 'CREATE', refillable=1)
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
b.create_transaction(b.me, b.me, None, 'CREATE', updatable=1)
|
2016-08-24 15:57:24 +02:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
b.create_transaction(b.me, b.me, None, 'CREATE', data='a')
|
2016-08-23 15:53:39 +02:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
b.create_transaction(b.me, b.me, None, 'CREATE', amount='a')
|
|
|
|
with pytest.raises(AmountError):
|
|
|
|
b.create_transaction(b.me, b.me, None, 'CREATE', divisible=False, amount=2)
|
|
|
|
with pytest.raises(AmountError):
|
|
|
|
b.create_transaction(b.me, b.me, None, 'CREATE', amount=0)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures('inputs')
|
|
|
|
def test_get_asset_id_create_transaction(b, user_vk):
|
|
|
|
from bigchaindb.assets import get_asset_id
|
|
|
|
|
|
|
|
tx_input = b.get_owned_ids(user_vk).pop()
|
|
|
|
tx_create = b.get_transaction(tx_input['txid'])
|
2016-09-09 10:41:38 +02:00
|
|
|
asset_id = get_asset_id(tx_create)
|
2016-08-23 15:53:39 +02:00
|
|
|
|
|
|
|
assert asset_id == tx_create['transaction']['asset']['id']
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures('inputs')
|
|
|
|
def test_get_asset_id_transfer_transaction(b, user_vk, user_sk):
|
|
|
|
from bigchaindb.assets import get_asset_id
|
|
|
|
|
|
|
|
tx_input = b.get_owned_ids(user_vk).pop()
|
|
|
|
# create a transfer transaction
|
|
|
|
tx_transfer = b.create_transaction(user_vk, user_vk, tx_input, 'TRANSFER')
|
|
|
|
tx_transfer_signed = b.sign_transaction(tx_transfer, user_sk)
|
|
|
|
# create a block
|
|
|
|
block = b.create_block([tx_transfer_signed])
|
|
|
|
b.write_block(block, durability='hard')
|
|
|
|
# vote the block valid
|
|
|
|
vote = b.vote(block['id'], b.get_last_voted_block()['id'], True)
|
|
|
|
b.write_vote(vote)
|
2016-09-09 10:41:38 +02:00
|
|
|
asset_id = get_asset_id(tx_transfer)
|
2016-08-23 15:53:39 +02:00
|
|
|
|
2016-08-24 16:12:21 +02:00
|
|
|
assert asset_id == tx_transfer['transaction']['asset']['id']
|
2016-08-23 15:53:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures('inputs')
|
|
|
|
def test_asset_id_mismatch(b, user_vk):
|
|
|
|
from bigchaindb.assets import get_asset_id
|
2016-10-07 15:46:21 +02:00
|
|
|
from bigchaindb_common.exceptions import AssetIdMismatch
|
2016-08-23 15:53:39 +02:00
|
|
|
|
|
|
|
tx_input1, tx_input2 = b.get_owned_ids(user_vk)[:2]
|
2016-09-09 10:41:38 +02:00
|
|
|
tx1 = b.get_transaction(tx_input1['txid'])
|
|
|
|
tx2 = b.get_transaction(tx_input2['txid'])
|
2016-08-23 15:53:39 +02:00
|
|
|
|
|
|
|
with pytest.raises(AssetIdMismatch):
|
2016-09-09 10:41:38 +02:00
|
|
|
get_asset_id([tx1, tx2])
|
2016-08-24 15:57:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_asset_id_transaction_does_not_exist(b, user_vk):
|
2016-10-07 15:46:21 +02:00
|
|
|
from bigchaindb_common.exceptions import TransactionDoesNotExist
|
2016-08-24 15:57:24 +02:00
|
|
|
|
|
|
|
with pytest.raises(TransactionDoesNotExist):
|
|
|
|
b.create_transaction(user_vk, user_vk, {'txid': 'bored', 'cid': '0'}, 'TRANSFER')
|
2016-08-24 18:12:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures('inputs')
|
|
|
|
def test_get_txs_by_asset_id(b, user_vk, user_sk):
|
|
|
|
tx_input = b.get_owned_ids(user_vk).pop()
|
|
|
|
tx = b.get_transaction(tx_input['txid'])
|
|
|
|
asset_id = tx['transaction']['asset']['id']
|
|
|
|
txs = b.get_txs_by_asset_id(asset_id)
|
|
|
|
|
|
|
|
assert len(txs) == 1
|
|
|
|
assert txs[0]['id'] == tx['id']
|
|
|
|
assert txs[0]['transaction']['asset']['id'] == asset_id
|
|
|
|
|
|
|
|
# create a transfer transaction
|
|
|
|
tx_transfer = b.create_transaction(user_vk, user_vk, tx_input, 'TRANSFER')
|
|
|
|
tx_transfer_signed = b.sign_transaction(tx_transfer, user_sk)
|
|
|
|
# create the block
|
|
|
|
block = b.create_block([tx_transfer_signed])
|
|
|
|
b.write_block(block, durability='hard')
|
|
|
|
# vote the block valid
|
|
|
|
vote = b.vote(block['id'], b.get_last_voted_block()['id'], True)
|
|
|
|
b.write_vote(vote)
|
|
|
|
|
|
|
|
txs = b.get_txs_by_asset_id(asset_id)
|
|
|
|
|
|
|
|
assert len(txs) == 2
|
|
|
|
assert tx['id'] in [t['id'] for t in txs]
|
|
|
|
assert tx_transfer['id'] in [t['id'] for t in txs]
|
|
|
|
assert asset_id == txs[0]['transaction']['asset']['id']
|
|
|
|
assert asset_id == txs[1]['transaction']['asset']['id']
|