From be6afa87852265d523165786f9769e51edfc2706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Eckel?= Date: Thu, 26 Jan 2023 00:17:31 +0100 Subject: [PATCH] blackified the project MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jürgen Eckel --- planetmint/lib.py | 41 +++++++++++++++-------------- tests/assets/test_digital_assets.py | 8 +++--- tests/web/test_transactions.py | 32 ++++++++++++++-------- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/planetmint/lib.py b/planetmint/lib.py index 1198fc1..5b67d70 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -385,12 +385,12 @@ class Planetmint(object): return transaction def validate_transfer_inputs(self, tx, current_transactions=[]): - input_txs, input_conditions = self.get_input_txs_and_conditions(tx.inputs, current_transactions) + input_txs, input_conditions = self.get_input_txs_and_conditions(tx.inputs, current_transactions) self.validate_input_conditions(tx, input_conditions) - + self.validate_asset_id(tx, input_txs) - + self.validate_inputs_distinct(tx) input_amount = sum([input_condition.amount for input_condition in input_conditions]) @@ -404,23 +404,23 @@ class Planetmint(object): ) return True - - def validate_compose_inputs(self, tx, current_transactions=[]) -> bool: + + def validate_compose_inputs(self, tx, current_transactions=[]) -> bool: input_txs, input_conditions = self.get_input_txs_and_conditions(tx.inputs, current_transactions) - + self.validate_input_conditions(tx, input_conditions) - + self.validate_asset_id(tx, input_txs) - + self.validate_inputs_distinct(tx) - + return True - + def get_input_txs_and_conditions(self, inputs, current_transactions=[]): # store the inputs so that we can check if the asset ids match input_txs = [] input_conditions = [] - + for input_ in inputs: input_txid = input_.fulfills.txid input_tx = self.get_transaction(input_txid) @@ -449,9 +449,9 @@ class Planetmint(object): tx_dict = DbTransaction.remove_generated_fields(tx_dict) pm_transaction = Transaction.from_dict(tx_dict, False) input_txs.append(pm_transaction) - + return (input_txs, input_conditions) - + def validate_input_conditions(self, tx, input_conditions): # convert planetmint.Output objects to transactions.common.Output objects input_conditions_dict = Output.list_to_dict(input_conditions) @@ -461,19 +461,20 @@ class Planetmint(object): if not tx.inputs_valid(input_conditions_converted): raise InvalidSignature("Transaction signature is invalid.") - - def validate_asset_id(self, tx: Transaction, input_txs:list): - # validate asset + + 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")) + 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 ) + 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 c7e11fd..f91e0de 100644 --- a/tests/assets/test_digital_assets.py +++ b/tests/assets/test_digital_assets.py @@ -68,17 +68,19 @@ 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) + 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 7108c65..40c9b8d 100644 --- a/tests/web/test_transactions.py +++ b/tests/web/test_transactions.py @@ -32,6 +32,7 @@ from transactions.common.crypto import generate_key_pair TX_ENDPOINT = "/api/v1/transactions/" + @pytest.mark.abci def test_get_transaction_endpoint(client, posted_create_tx): res = client.get(TX_ENDPOINT + posted_create_tx.id) @@ -473,31 +474,40 @@ 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]) + 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] + 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]) + signed_compose_tx = compose_transaction.sign([alice.private_key]) compose_dict = signed_compose_tx.to_dict() - compose_obj = Transaction.from_dict( compose_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,b): + +@pytest.mark.abci +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]) + 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 @@ -506,9 +516,9 @@ def test_post_transaction_compose_valid(client,b): compose_asset_cid = "bafkreignwcoye67vn6edp23mj4llhpzzkgyuefu7xesjzjxcv2bz3p4nfm" inputs_ = tx_obj.to_inputs() - assets_ = [ tx["id"], compose_asset_cid] + 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]) + 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()))