__init__ transfer now uses multiple assets

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2022-04-06 16:26:51 +02:00
parent e32047d999
commit ebc1a4d85e
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
3 changed files with 16 additions and 12 deletions

View File

@ -105,7 +105,7 @@ class Transaction(object):
# dicts holding a `data` property. Asset payloads for 'TRANSFER' # dicts holding a `data` property. Asset payloads for 'TRANSFER'
# operations must be dicts holding an `id` property. # operations must be dicts holding an `id` property.
# Changes: CREATE needs list of 1 asset that holds data in asset or None # NOTE: Changes: CREATE needs list of 1 asset that holds data in asset or None
if (operation == self.CREATE and if (operation == self.CREATE and
assets is not None and not (isinstance(assets, list) and 'data' in assets[0])): assets is not None and not (isinstance(assets, list) and 'data' in assets[0])):
raise TypeError(('`asset` must be None or a list of length 1 with a dict holding a `data` ' raise TypeError(('`asset` must be None or a list of length 1 with a dict holding a `data` '
@ -114,10 +114,14 @@ class Transaction(object):
# asset is not None and not (isinstance(asset, dict) and 'data' in asset)): # asset is not None and not (isinstance(asset, dict) and 'data' in asset)):
# raise TypeError(('`asset` must be None or a dict holding a `data` ' # raise TypeError(('`asset` must be None or a dict holding a `data` '
# " property instance for '{}' Transactions".format(operation))) # " property instance for '{}' Transactions".format(operation)))
# NOTE: Changes: TRANSFER needs a list of at least on asset that holds id in each asset
elif (operation == self.TRANSFER and elif (operation == self.TRANSFER and
not (isinstance(assets, dict) and 'id' in assets)): assets is not None and not (isinstance(assets, list) and all('id' in asset for asset in assets))):
raise TypeError(('`asset` must be a dict holding an `id` property ' raise TypeError(('`asset` must be a list containing dicts holding an `id` property'))
'for \'TRANSFER\' Transactions')) # elif (operation == self.TRANSFER and
# not (isinstance(assets, dict) and 'id' in assets)):
# raise TypeError(('`asset` must be a dict holding an `id` property '
# 'for \'TRANSFER\' Transactions'))
if outputs and not isinstance(outputs, list): if outputs and not isinstance(outputs, list):
raise TypeError('`outputs` must be a list instance or None') raise TypeError('`outputs` must be a list instance or None')
@ -567,7 +571,7 @@ class Transaction(object):
return Transaction._to_str(tx) return Transaction._to_str(tx)
@classmethod @classmethod
def get_asset_id(cls, transactions): def get_asset_ids(cls, transactions):
"""Get the asset id from a list of :class:`~.Transactions`. """Get the asset id from a list of :class:`~.Transactions`.
This is useful when we want to check if the multiple inputs of a This is useful when we want to check if the multiple inputs of a
@ -607,7 +611,7 @@ class Transaction(object):
else: else:
asset_ids.extend([asset['id'] for asset in tx.assets]) asset_ids.extend([asset['id'] for asset in tx.assets])
return asset_ids.pop() return asset_ids
@staticmethod @staticmethod
def validate_id(tx_body): def validate_id(tx_body):

View File

@ -23,7 +23,7 @@ def test_validate_transfer_asset_id_mismatch(b, signed_create_tx, user_pk, user_
from planetmint.transactions.common.exceptions import AssetIdMismatch from planetmint.transactions.common.exceptions import AssetIdMismatch
tx_transfer = Transfer.generate(signed_create_tx.to_inputs(), [([user_pk], 1)], tx_transfer = Transfer.generate(signed_create_tx.to_inputs(), [([user_pk], 1)],
signed_create_tx.id) [signed_create_tx.id])
tx_transfer.asset['id'] = 'a' * 64 tx_transfer.asset['id'] = 'a' * 64
tx_transfer_signed = tx_transfer.sign([user_sk]) tx_transfer_signed = tx_transfer.sign([user_sk])
@ -36,15 +36,15 @@ def test_validate_transfer_asset_id_mismatch(b, signed_create_tx, user_pk, user_
def test_get_asset_id_create_transaction(alice, user_pk): def test_get_asset_id_create_transaction(alice, user_pk):
from planetmint.models import Transaction from planetmint.models import Transaction
tx_create = Create.generate([alice.public_key], [([user_pk], 1)]) tx_create = Create.generate([alice.public_key], [([user_pk], 1)])
assert Transaction.get_asset_id(tx_create) == tx_create.id assert Transaction.get_asset_ids(tx_create) == tx_create.id
def test_get_asset_id_transfer_transaction(b, signed_create_tx, user_pk): def test_get_asset_id_transfer_transaction(b, signed_create_tx, user_pk):
from planetmint.models import Transaction from planetmint.models import Transaction
tx_transfer = Transfer.generate(signed_create_tx.to_inputs(), [([user_pk], 1)], tx_transfer = Transfer.generate(signed_create_tx.to_inputs(), [([user_pk], 1)],
signed_create_tx.id) [signed_create_tx.id])
asset_id = Transaction.get_asset_id(tx_transfer) asset_id = Transaction.get_asset_ids(tx_transfer)
assert asset_id == tx_transfer.asset['id'] assert asset_id == tx_transfer.asset['id']
# This test is not relevant anymore # This test is not relevant anymore
@ -60,7 +60,7 @@ def test_asset_id_mismatch(alice, user_pk):
tx2.sign([alice.private_key]) tx2.sign([alice.private_key])
with pytest.raises(AssetIdMismatch): with pytest.raises(AssetIdMismatch):
Transaction.get_asset_id([tx1, tx2]) Transaction.get_asset_ids([tx1, tx2])
def test_create_valid_divisible_asset(b, user_pk, user_sk): def test_create_valid_divisible_asset(b, user_pk, user_sk):

View File

@ -26,7 +26,7 @@ def test_get_txids_filtered(signed_create_tx, signed_transfer_tx):
conn.db.transactions.insert_one(signed_create_tx.to_dict()) conn.db.transactions.insert_one(signed_create_tx.to_dict())
conn.db.transactions.insert_one(signed_transfer_tx.to_dict()) conn.db.transactions.insert_one(signed_transfer_tx.to_dict())
asset_id = Transaction.get_asset_id([signed_create_tx, signed_transfer_tx]) asset_id = Transaction.get_asset_ids([signed_create_tx, signed_transfer_tx])
# Test get by just asset id # Test get by just asset id
txids = set(query.get_txids_filtered(conn, asset_id)) txids = set(query.get_txids_filtered(conn, asset_id))