From 0e79bbecca922b06d0be5dabf9287380ff26ad15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Eckel?= Date: Thu, 26 Jan 2023 00:16:07 +0100 Subject: [PATCH] * extended validate_asset_id * added asset & web test cases (abci and non-abic) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jürgen Eckel --- planetmint/lib.py | 18 ++++++++----- tests/assets/test_digital_assets.py | 11 ++++++++ tests/web/test_transactions.py | 42 ++++++++++++++++------------- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/planetmint/lib.py b/planetmint/lib.py index 424606d..1198fc1 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -462,12 +462,18 @@ class Planetmint(object): if not tx.inputs_valid(input_conditions_converted): raise InvalidSignature("Transaction signature is invalid.") - def validate_asset_id(self, tx, input_txs): - # validate asset id - asset_id = tx.get_asset_id(input_txs) - if asset_id != Transaction.read_out_asset_id(tx): - raise AssetIdMismatch(("The asset id of the input does not" " match the asset id of the" " transaction")) - + def validate_asset_id(self, tx: Transaction, input_txs:list): + # validate asset + if tx.operation != Transaction.COMPOSE: + asset_id = tx.get_asset_id(input_txs) + if asset_id != Transaction.read_out_asset_id(tx): + raise AssetIdMismatch(("The asset id of the input does not" " match the asset id of the" " transaction")) + else: + asset_ids = Transaction.get_asset_ids( input_txs ) + if Transaction.read_out_asset_id(tx) in asset_ids: + raise AssetIdMismatch(("The asset ID of the compose must be different to all of its input asset IDs")) + + def validate_inputs_distinct(self, tx): # Validate that all inputs are distinct links = [i.fulfills.to_uri() for i in tx.inputs] diff --git a/tests/assets/test_digital_assets.py b/tests/assets/test_digital_assets.py index e0e678d..c7e11fd 100644 --- a/tests/assets/test_digital_assets.py +++ b/tests/assets/test_digital_assets.py @@ -68,6 +68,17 @@ def test_asset_id_mismatch(alice, user_pk): with pytest.raises(AssetIdMismatch): Transaction.get_asset_id([tx1, tx2]) +def test_compose_valid_transactions(b, user_pk, user_sk, alice, signed_create_tx, _bdb): + from transactions.types.assets.compose import Compose + + validated = b.validate_transaction(signed_create_tx) + b.store_bulk_transactions([validated]) + + inputs = signed_create_tx.to_inputs() + assets = [signed_create_tx.id, "QmW5GVMW98D3mktSDfWHS8nX2UiCd8gP1uCiujnFX4yK8n"] + compose_transaction = Compose.generate(inputs=inputs, recipients=[([user_pk], 1)], assets=assets) + compose_transaction.sign([user_sk]) + assert b.validate_transaction( compose_transaction) def test_create_valid_divisible_asset(b, user_pk, user_sk, _bdb): tx = Create.generate([user_pk], [([user_pk], 2)]) diff --git a/tests/web/test_transactions.py b/tests/web/test_transactions.py index 83925c1..7108c65 100644 --- a/tests/web/test_transactions.py +++ b/tests/web/test_transactions.py @@ -473,37 +473,43 @@ def test_post_transaction_invalid_mode(client): assert "400 BAD REQUEST" in response.status assert 'Mode must be "async", "sync" or "commit"' == json.loads(response.data.decode("utf8"))["message"]["mode"] +def test_post_transaction_compose_valid_wo_abci(b, _bdb): + alice = generate_key_pair() + tx = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=[{"data":"QmW5GVMW98D3mktSDfWHS8nX2UiCd8gP1uCiujnFX4yK97"}]).sign([alice.private_key]) + validated = b.validate_transaction(tx) + b.store_bulk_transactions([validated]) + + tx_obj = tx + tx = tx.to_dict() + compose_asset_cid = "bafkreignwcoye67vn6edp23mj4llhpzzkgyuefu7xesjzjxcv2bz3p4nfm" + inputs_ = tx_obj.to_inputs() + assets_ = [ tx["id"], compose_asset_cid] + compose_transaction = Compose.generate(inputs=inputs_, recipients=[([alice.public_key], 1)], assets=assets_) + signed_compose_tx = compose_transaction.sign( [alice.private_key]) + compose_dict = signed_compose_tx.to_dict() + compose_obj = Transaction.from_dict( compose_dict) + validated_compose = b.validate_transaction(compose_obj) + b.store_bulk_transactions([validated_compose]) + + @pytest.mark.abci -def test_post_transaction_compose_valid( client): +def test_post_transaction_compose_valid(client,b): mode = ("?mode=commit", BROADCAST_TX_COMMIT) alice = generate_key_pair() tx = Create.generate([alice.public_key], [([alice.public_key], 1)], assets=[{"data":"QmW5GVMW98D3mktSDfWHS8nX2UiCd8gP1uCiujnFX4yK97"}]).sign([alice.private_key]) mode_endpoint = TX_ENDPOINT + mode[0] response = client.post(mode_endpoint, data=json.dumps(tx.to_dict())) assert "202 ACCEPTED" in response.status - + tx_obj = tx tx = tx.to_dict() + compose_asset_cid = "bafkreignwcoye67vn6edp23mj4llhpzzkgyuefu7xesjzjxcv2bz3p4nfm" + inputs_ = tx_obj.to_inputs() - compose_asset_cid = "QmW5GVMW98D3mktSDfWHS8nX2UiCd8gP1uCiujnFX4yK8n" - - condition_index = 0 - condition = tx["outputs"][condition_index] - inputs_ = [ - Input( - _fulfillment_from_details(condition["condition"]["details"]), - condition["public_keys"], - fulfills=TransactionLink( - txid=tx["id"], - output=condition_index, - ), - ) - ] assets_ = [ tx["id"], compose_asset_cid] compose_transaction = Compose.generate(inputs=inputs_, recipients=[([alice.public_key], 1)], assets=assets_) signed_tx = compose_transaction.sign( [alice.private_key]) + validated_compose = b.validate_transaction(signed_tx) mode_endpoint = TX_ENDPOINT + "?mode=commit" response = client.post(mode_endpoint, data=json.dumps(signed_tx.to_dict())) assert "202 ACCEPTED" in response.status - -