From 678cd6328ba9d562bf63b8b6e60e19e4072ef47e Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Thu, 12 May 2022 15:06:14 +0200 Subject: [PATCH] fixed integration tests, removed unused code and adjusted some docstrings Signed-off-by: Lorenz Herzberger --- acceptance/python/src/test_zenroom.py | 4 +--- integration/python/Dockerfile | 4 +--- integration/python/src/conftest.py | 4 ++-- integration/python/src/test_basic.py | 12 ++++++------ integration/python/src/test_divisible_asset.py | 14 +++++++------- integration/python/src/test_double_spend.py | 2 +- integration/python/src/test_multiple_owners.py | 10 +++++----- integration/python/src/test_naughty_strings.py | 12 ++++++------ integration/python/src/test_stream.py | 2 +- integration/python/src/test_threshold.py | 14 +++++++------- integration/python/src/test_zenroom.py | 6 ++---- planetmint/transactions/common/transaction.py | 17 ----------------- .../transactions/types/assets/transfer.py | 2 +- 13 files changed, 40 insertions(+), 63 deletions(-) diff --git a/acceptance/python/src/test_zenroom.py b/acceptance/python/src/test_zenroom.py index 97cd440..3520036 100644 --- a/acceptance/python/src/test_zenroom.py +++ b/acceptance/python/src/test_zenroom.py @@ -76,9 +76,7 @@ condition_script_zencode, zenroom_data, zenroom_house_assets): try: assert(not zenSha.validate(message=message)) - except JSONDecodeError: - pass - except ValueError: + except: # noqa pass message = zenSha.sign(message, condition_script_zencode, alice) diff --git a/integration/python/Dockerfile b/integration/python/Dockerfile index 4ebd115..036d92d 100644 --- a/integration/python/Dockerfile +++ b/integration/python/Dockerfile @@ -13,11 +13,9 @@ RUN mkdir -p /src RUN pip install --upgrade meson ninja RUN pip install --upgrade \ pytest~=6.2.5 \ - # planetmint-driver~=0.9.0 \ + git+https://github.com/planetmint/cryptoconditions.git@asset-migration \ git+https://github.com/planetmint/planetmint-driver-python.git@asset-migration \ pycco \ websocket-client~=0.47.0 \ - #git+https://github.com/planetmint/cryptoconditions.git@gitzenroom \ - #git+https://github.com/planetmint/planetmint-driver.git@gitzenroom \ blns diff --git a/integration/python/src/conftest.py b/integration/python/src/conftest.py index 808914b..3afba13 100644 --- a/integration/python/src/conftest.py +++ b/integration/python/src/conftest.py @@ -36,7 +36,7 @@ FULFILL_SCRIPT = \ When I verify the 'houses' has a signature in 'data.signature' by 'Alice' Then print the string 'ok'""" -HOUSE_ASSETS = { +HOUSE_ASSETS = [{ "data": { "houses": [ { @@ -49,7 +49,7 @@ HOUSE_ASSETS = { } ], } -} +}] ZENROOM_DATA = { 'also': 'more data' diff --git a/integration/python/src/test_basic.py b/integration/python/src/test_basic.py index 691dbc3..49ee745 100644 --- a/integration/python/src/test_basic.py +++ b/integration/python/src/test_basic.py @@ -21,12 +21,12 @@ def test_basic(): alice = generate_keypair() # create a digital asset for Alice - game_boy_token = { + game_boy_token = [{ 'data': { 'hash': '0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 'storageID': '0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', }, - } + }] # prepare the transaction with the digital asset and issue 10 tokens to bob prepared_creation_tx = pm_alpha.transactions.prepare( @@ -36,7 +36,7 @@ def test_basic(): 'storageID': '0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', }, signers=alice.public_key, recipients=[([alice.public_key], 10)], - asset=game_boy_token) + assets=game_boy_token) # fulfill and send the transaction fulfilled_creation_tx = pm_alpha.transactions.fulfill( @@ -52,18 +52,18 @@ def test_basic(): # Transfer # create the output and inout for the transaction - transfer_asset = {'id': creation_tx_id} + transfer_assets = [{'id': creation_tx_id}] output_index = 0 output = fulfilled_creation_tx['outputs'][output_index] transfer_input = {'fulfillment': output['condition']['details'], 'fulfills': {'output_index': output_index, - 'transaction_id': transfer_asset['id']}, + 'transaction_id': transfer_assets[0]['id']}, 'owners_before': output['public_keys']} # prepare the transaction and use 3 tokens prepared_transfer_tx = pm_alpha.transactions.prepare( operation='TRANSFER', - asset=transfer_asset, + assets=transfer_assets, inputs=transfer_input, metadata={'hash': '0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 'storageID': '0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', }, diff --git a/integration/python/src/test_divisible_asset.py b/integration/python/src/test_divisible_asset.py index c324f61..0fa4cc1 100644 --- a/integration/python/src/test_divisible_asset.py +++ b/integration/python/src/test_divisible_asset.py @@ -50,7 +50,7 @@ def test_divisible_assets(): # the description, Bob and Alice agree that each token can be used to ride # the bike for one hour. - bike_token = { + bike_token = [{ 'data': { 'token_for': { 'bike': { @@ -59,7 +59,7 @@ def test_divisible_assets(): }, 'description': 'Time share token. Each token equals one hour of riding.', }, - } + }] # She prepares a `CREATE` transaction and issues 10 tokens. # Here, Alice defines in a tuple that she wants to assign @@ -68,7 +68,7 @@ def test_divisible_assets(): operation='CREATE', signers=alice.public_key, recipients=[([bob.public_key], 10)], - asset=bike_token) + assets=bike_token) # She fulfills and sends the transaction. fulfilled_token_tx = pm.transactions.fulfill( @@ -94,7 +94,7 @@ def test_divisible_assets(): # To use the bike he has to send the tokens back to Alice. # To learn about the details of transferring a transaction check out # [test_basic.py](./test_basic.html) - transfer_asset = {'id': bike_token_id} + transfer_assets = [{'id': bike_token_id}] output_index = 0 output = fulfilled_token_tx['outputs'][output_index] @@ -108,7 +108,7 @@ def test_divisible_assets(): # amount he wants to use to Alice. prepared_transfer_tx = pm.transactions.prepare( operation='TRANSFER', - asset=transfer_asset, + assets=transfer_assets, inputs=transfer_input, recipients=[([alice.public_key], 3), ([bob.public_key], 7)]) @@ -138,7 +138,7 @@ def test_divisible_assets(): # Now he wants to ride for 8 hours, that's a lot Bob! # He prepares the transaction again. - transfer_asset = {'id': bike_token_id} + transfer_assets = [{'id': bike_token_id}] # This time we need an `output_index` of 1, since we have two outputs # in the `fulfilled_transfer_tx` we created before. The first output with # index 0 is for Alice and the second output is for Bob. @@ -157,7 +157,7 @@ def test_divisible_assets(): # to spend all his tokens prepared_transfer_tx = pm.transactions.prepare( operation='TRANSFER', - asset=transfer_asset, + assets=transfer_assets, inputs=transfer_input, recipients=[([alice.public_key], 8)]) diff --git a/integration/python/src/test_double_spend.py b/integration/python/src/test_double_spend.py index 1a17738..0d1e988 100644 --- a/integration/python/src/test_double_spend.py +++ b/integration/python/src/test_double_spend.py @@ -26,7 +26,7 @@ def test_double_create(): pm.transactions.prepare( operation='CREATE', signers=alice.public_key, - asset={'data': {'uuid': str(uuid4())}}), + assets=[{'data': {'uuid': str(uuid4())}}]), private_keys=alice.private_key) def send_and_queue(tx): diff --git a/integration/python/src/test_multiple_owners.py b/integration/python/src/test_multiple_owners.py index 98f3ea6..c5e1f9f 100644 --- a/integration/python/src/test_multiple_owners.py +++ b/integration/python/src/test_multiple_owners.py @@ -41,13 +41,13 @@ def test_multiple_owners(): # high rents anymore. Bob suggests to get a dish washer for the # kitchen. Alice agrees and here they go, creating the asset for their # dish washer. - dw_asset = { + dw_asset = [{ 'data': { 'dish washer': { 'serial_number': 1337 } } - } + }] # They prepare a `CREATE` transaction. To have multiple owners, both # Bob and Alice need to be the recipients. @@ -55,7 +55,7 @@ def test_multiple_owners(): operation='CREATE', signers=alice.public_key, recipients=(alice.public_key, bob.public_key), - asset=dw_asset) + assets=dw_asset) # Now they both sign the transaction by providing their private keys. # And send it afterwards. @@ -91,7 +91,7 @@ def test_multiple_owners(): # Alice and Bob prepare the transaction to transfer the dish washer to # Carol. - transfer_asset = {'id': dw_id} + transfer_assets = [{'id': dw_id}] output_index = 0 output = fulfilled_dw_tx['outputs'][output_index] @@ -104,7 +104,7 @@ def test_multiple_owners(): # Now they create the transaction... prepared_transfer_tx = pm_alpha.transactions.prepare( operation='TRANSFER', - asset=transfer_asset, + assets=transfer_assets, inputs=transfer_input, recipients=carol.public_key) diff --git a/integration/python/src/test_naughty_strings.py b/integration/python/src/test_naughty_strings.py index 4a090c0..700d1d0 100644 --- a/integration/python/src/test_naughty_strings.py +++ b/integration/python/src/test_naughty_strings.py @@ -30,7 +30,7 @@ naughty_strings = blns.all() # This is our base test case, but we'll reuse it to send naughty strings as both keys and values. -def send_naughty_tx(asset, metadata): +def send_naughty_tx(assets, metadata): # ## Set up a connection to Planetmint # Check [test_basic.py](./test_basic.html) to get some more details # about the endpoint. @@ -44,7 +44,7 @@ def send_naughty_tx(asset, metadata): prepared_transaction = pm.transactions.prepare( operation='CREATE', signers=alice.public_key, - asset=asset, + assets=assets, metadata=metadata) # She fulfills the transaction @@ -85,16 +85,16 @@ def send_naughty_tx(asset, metadata): @pytest.mark.parametrize("naughty_string", naughty_strings, ids=naughty_strings) def test_naughty_keys(naughty_string): - asset = {'data': {naughty_string: 'nice_value'}} + assets = [{'data': {naughty_string: 'nice_value'}}] metadata = {naughty_string: 'nice_value'} - send_naughty_tx(asset, metadata) + send_naughty_tx(assets, metadata) @pytest.mark.parametrize("naughty_string", naughty_strings, ids=naughty_strings) def test_naughty_values(naughty_string): - asset = {'data': {'nice_key': naughty_string}} + assets = [{'data': {'nice_key': naughty_string}}] metadata = {'nice_key': naughty_string} - send_naughty_tx(asset, metadata) + send_naughty_tx(assets, metadata) diff --git a/integration/python/src/test_stream.py b/integration/python/src/test_stream.py index c93d5c6..3be9ec3 100644 --- a/integration/python/src/test_stream.py +++ b/integration/python/src/test_stream.py @@ -92,7 +92,7 @@ def test_stream(): pm.transactions.prepare( operation='CREATE', signers=alice.public_key, - asset={'data': {'uuid': str(uuid4())}}), + assets=[{'data': {'uuid': str(uuid4())}}]), private_keys=alice.private_key) # We don't want to wait for each transaction to be in a block. By using # `async` mode, we make sure that the driver returns as soon as the diff --git a/integration/python/src/test_threshold.py b/integration/python/src/test_threshold.py index f118651..8b6db4a 100644 --- a/integration/python/src/test_threshold.py +++ b/integration/python/src/test_threshold.py @@ -49,13 +49,13 @@ def test_threshold(): # high rents anymore. Bob suggests to get a dish washer for the # kitchen. Alice agrees and here they go, creating the asset for their # dish washer. - dw_asset = { + dw_asset = [{ 'data': { 'dish washer': { 'serial_number': 1337 } } - } + }] # Create subfulfillments alice_ed25519 = Ed25519Sha256(public_key=base58.b58decode(alice.public_key)) @@ -92,7 +92,7 @@ def test_threshold(): # Assemble the handcrafted transaction handcrafted_dw_tx = { 'operation': 'CREATE', - 'asset': dw_asset, + 'assets': dw_asset, 'metadata': None, 'outputs': (output,), 'inputs': (input_,), @@ -149,13 +149,13 @@ def test_weighted_threshold(): alice, bob, carol = generate_keypair(), generate_keypair(), generate_keypair() - asset = { + assets = [{ 'data': { 'trashcan': { 'animals': ['racoon_1', 'racoon_2'] } } - } + }] alice_ed25519 = Ed25519Sha256(public_key=base58.b58decode(alice.public_key)) bob_ed25519 = Ed25519Sha256(public_key=base58.b58decode(bob.public_key)) @@ -193,7 +193,7 @@ def test_weighted_threshold(): # Assemble the handcrafted transaction handcrafted_tx = { 'operation': 'CREATE', - 'asset': asset, + 'assets': assets, 'metadata': None, 'outputs': (output,), 'inputs': (input_,), @@ -278,7 +278,7 @@ def test_weighted_threshold(): # Assemble the handcrafted transaction handcrafted_transfer_tx = { 'operation': 'TRANSFER', - 'asset': {'id': creation_tx_id}, + 'assets': [{'id': creation_tx_id}], 'metadata': None, 'outputs': (transfer_output,), 'inputs': (transfer_input_,), diff --git a/integration/python/src/test_zenroom.py b/integration/python/src/test_zenroom.py index 8f749a6..5fdbcea 100644 --- a/integration/python/src/test_zenroom.py +++ b/integration/python/src/test_zenroom.py @@ -57,7 +57,7 @@ def test_zenroom(gen_key_zencode, secret_key_to_private_key_zencode, fulfill_scr token_creation_tx = { 'operation': 'CREATE', - 'asset': zenroom_house_assets, + 'assets': zenroom_house_assets, 'metadata': None, 'outputs': (output,), 'inputs': (input_,), @@ -75,9 +75,7 @@ def test_zenroom(gen_key_zencode, secret_key_to_private_key_zencode, fulfill_scr try: assert(not zenSha.validate(message=message)) - except JSONDecodeError: - pass - except ValueError: + except: # noqa pass message = zenSha.sign(message, condition_script_zencode, alice) diff --git a/planetmint/transactions/common/transaction.py b/planetmint/transactions/common/transaction.py index 6cdfa89..45d1051 100644 --- a/planetmint/transactions/common/transaction.py +++ b/planetmint/transactions/common/transaction.py @@ -105,23 +105,13 @@ class Transaction(object): # dicts holding a `data` property. Asset payloads for 'TRANSFER' # operations must be dicts holding an `id` property. - # NOTE: Changes: CREATE needs list of 1 asset that holds data in asset or None if (operation == self.CREATE and 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` ' " property instance for '{}' Transactions".format(operation))) - # if (operation == self.CREATE and - # 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` ' - # " 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 assets is not None and not (isinstance(assets, list) and all('id' in asset for asset in assets))): raise TypeError(('`asset` must be a list containing dicts holding an `id` property')) - # 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): raise TypeError('`outputs` must be a list instance or None') @@ -756,13 +746,6 @@ class Transaction(object): if len(links) != len(set(links)): raise DoubleSpend('tx "{}" spends inputs twice'.format(self.id)) - # validate asset id - # asset_id = self.get_asset_id(input_txs) - # if asset_id != self.asset['id']: - # raise AssetIdMismatch(('The asset id of the input does not' - # ' match the asset id of the' - # ' transaction')) - input_amount = sum([input_condition.amount for input_condition in input_conditions]) output_amount = sum([output_condition.amount for output_condition in self.outputs]) diff --git a/planetmint/transactions/types/assets/transfer.py b/planetmint/transactions/types/assets/transfer.py index 92ea37e..63de8a7 100644 --- a/planetmint/transactions/types/assets/transfer.py +++ b/planetmint/transactions/types/assets/transfer.py @@ -69,7 +69,7 @@ class Transfer(Transaction): recipients (:obj:`list` of :obj:`tuple`): A list of ([keys],amount) that represent the recipients of this Transaction. - asset_id (str): The asset ID of the asset to be transferred in + asset_ids (:obj:`list` of :obj:`str`): The asset IDs of the assets to be transferred in this Transaction. metadata (dict): Python dictionary to be stored along with the Transaction.