bigchaindb/tests/assets/test_digital_assets.py
Sylvain Bellemare 64cafc62ad Merge common lib (#739)
* Planning release

* Clean up after move

* Add exceptions.py

* Add crypto.py

* Adjust setup to package structure

* Fix tests

* Add test coverage

* Comply to flake8

* Add test coverage

* Transfer-tx fulfillments validation

* Remove condition and fulfillment ids

* Fix signing logic

Specifically for transfer-tx with multiple inputs
and outputs.

* Compliance to legacy BDB models

* Adjust fulfillment validation interface

* Add serialization validation for txids

* Use __eq__ to compare objects

* Heavy refactor to comply with current implementation

* Add Transaction.create

* Correct fulfillment validation logic

* Add Transaction.create for hashlock conditions

* Add hashlock condition serialization

* Transaction.transfer add single input and outputs

* Small adjustments to transfer-tx interface

* Create transfer-tx interface

* Increase test coverage

* Adjust fulfillment (de)serialization

* Catch CC Error for Fulfillment

* Allow custom thresholds

* PR feedback

* Fix tests

* Rename Data to Metadata

* Add Asset exceptions

* Add basic Asset model

* More renaming of payload => data

* Add Asset into work-flow-functions

* Add Asset amount to condition

* add fulfillment exception

* initial integration of asset

* Make transaction.py compy to 79 chars

* Make util.py comply to 79 chars

* Make exceptions.py comply to 80 chars

* Renaming inp to input_

* fix pep8 issues

* Correct raised error

* Remove resolved TODOs

* prevent adding None as fulfillment / condition to Transaction

* Small modifications to support new cryptoconditions

* Improve documentation (#42)

* Add doc strings for Fulfillment cls

* Add doc strings for TransactionLink cls

* Add doc strings for Condition cls

* Add doc strings for Data cls

* Add doc strings for Transaction cls

* Add doc strings for Asset cls

* Extract common implementation

* Tx model: Add test for empty inputs

* WIP: Implement sign tx

* Add tests for:
    - Conditions; and
    - Fulfillments

Mostly on the (de)serialization part.

* Finalize serialization logic for tx class

* Add Tests for tx serialization logic

* Add fulfillment validation

* Add ThresholdCondition support

* WIP transfer

* Clean up after move

* Adjust setup to package structure

* Fix tests

* Add test coverage

* Add test coverage

* Transfer-tx fulfillments validation

* Remove condition and fulfillment ids

* Fix signing logic

Specifically for transfer-tx with multiple inputs
and outputs.

* Fix test case

* Compliance to legacy BDB models

* Adjust fulfillment validation interface

* Add serialization validation for txids

* Use __eq__ to compare objects

* Heavy refactor to comply with current implementation

* Add Transaction.create

* Add validation tests

* Add Transaction.create for hashlock conditions

* Add hashlock condition serialization

* Transaction.transfer add single input and outputs

* Small adjustments to transfer-tx interface

* Create transfer-tx interface

* Increase test coverage

* Adjust fulfillment (de)serialization

* Catch CC Error for Fulfillment

* Allow custom thresholds

* Rename Data to Metadata

* Add basic Asset model

* Add Asset into work-flow-functions

* Add Asset amount to condition

* initial integration of asset

* Make tests comply to 79 chars per line

* Fixed tests

* fix pep8 issues

* Correct raised error

* Add test for asset initialization

* Remove resolved TODOs

* prevent adding None as fulfillment / condition to Transaction

* Small modifications to support new cryptoconditions

* Extract common tests

* Copy conftest from bigchaindb-common - by @timdaub

* Replace bigchaindb_common pkg by bigchaindb.common
2016-10-25 10:21:20 +02:00

164 lines
5.6 KiB
Python

import pytest
from ..db.conftest import inputs
@pytest.mark.usefixtures('inputs')
def test_asset_transfer(b, user_vk, user_sk):
from bigchaindb.models import Transaction
tx_input = b.get_owned_ids(user_vk).pop()
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])
assert tx_transfer_signed.validate(b) == tx_transfer_signed
assert tx_transfer_signed.asset.data_id == tx_create.asset.data_id
def test_validate_bad_asset_creation(b, user_vk):
from bigchaindb.models import Transaction
# `divisible` needs to be a boolean
tx = Transaction.create([b.me], [user_vk])
tx.asset.divisible = 1
tx_signed = tx.sign([b.me_private])
with pytest.raises(TypeError):
tx_signed.validate(b)
# `refillable` needs to be a boolean
tx = Transaction.create([b.me], [user_vk])
tx.asset.refillable = 1
tx_signed = tx.sign([b.me_private])
with pytest.raises(TypeError):
b.validate_transaction(tx_signed)
# `updatable` needs to be a boolean
tx = Transaction.create([b.me], [user_vk])
tx.asset.updatable = 1
tx_signed = tx.sign([b.me_private])
with pytest.raises(TypeError):
b.validate_transaction(tx_signed)
# `data` needs to be a dictionary
tx = Transaction.create([b.me], [user_vk])
tx.asset.data = 'a'
tx_signed = tx.sign([b.me_private])
with pytest.raises(TypeError):
b.validate_transaction(tx_signed)
# TODO: Check where to test for the amount
"""
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')
def test_validate_transfer_asset_id_mismatch(b, user_vk, user_sk):
from bigchaindb.common.exceptions import AssetIdMismatch
from bigchaindb.models import Transaction
tx_create = b.get_owned_ids(user_vk).pop()
tx_create = b.get_transaction(tx_create.txid)
tx_transfer = Transaction.transfer(tx_create.to_inputs(), [user_vk],
tx_create.asset)
tx_transfer.asset.data_id = 'aaa'
tx_transfer_signed = tx_transfer.sign([user_sk])
with pytest.raises(AssetIdMismatch):
tx_transfer_signed.validate(b)
def test_get_asset_id_create_transaction(b, user_vk):
from bigchaindb.models import Transaction, Asset
tx_create = Transaction.create([b.me], [user_vk])
asset_id = Asset.get_asset_id(tx_create)
assert asset_id == tx_create.asset.data_id
@pytest.mark.usefixtures('inputs')
def test_get_asset_id_transfer_transaction(b, user_vk, user_sk):
from bigchaindb.models import Transaction, Asset
tx_create = b.get_owned_ids(user_vk).pop()
tx_create = b.get_transaction(tx_create.txid)
# create a transfer transaction
tx_transfer = Transaction.transfer(tx_create.to_inputs(), [user_vk],
tx_create.asset)
tx_transfer_signed = tx_transfer.sign([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)
asset_id = Asset.get_asset_id(tx_transfer)
assert asset_id == tx_transfer.asset.data_id
def test_asset_id_mismatch(b, user_vk):
from bigchaindb.models import Transaction, Asset
from bigchaindb.common.exceptions import AssetIdMismatch
tx1 = Transaction.create([b.me], [user_vk])
tx2 = Transaction.create([b.me], [user_vk])
with pytest.raises(AssetIdMismatch):
Asset.get_asset_id([tx1, tx2])
@pytest.mark.usefixtures('inputs')
def test_get_txs_by_asset_id(b, user_vk, user_sk):
from bigchaindb.models import Transaction
tx_create = b.get_owned_ids(user_vk).pop()
tx_create = b.get_transaction(tx_create.txid)
asset_id = tx_create.asset.data_id
txs = b.get_txs_by_asset_id(asset_id)
assert len(txs) == 1
assert txs[0].id == tx_create.id
assert txs[0].asset.data_id == asset_id
# create a transfer transaction
tx_transfer = Transaction.transfer(tx_create.to_inputs(), [user_vk],
tx_create.asset)
tx_transfer_signed = tx_transfer.sign([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_create.id in [t.id for t in txs]
assert tx_transfer.id in [t.id for t in txs]
assert asset_id == txs[0].asset.data_id
assert asset_id == txs[1].asset.data_id