From 6bbfa945076827607b52e75375abc8b427f19861 Mon Sep 17 00:00:00 2001 From: saratomaz Date: Wed, 27 Jul 2022 13:20:54 +0100 Subject: [PATCH 1/2] test build command with automatic collateral values and default change address --- .../tests/test_plutus_v2_spend_build.py | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/cardano_node_tests/tests/test_plutus_v2_spend_build.py b/cardano_node_tests/tests/test_plutus_v2_spend_build.py index c0639e029..41c7c4cab 100644 --- a/cardano_node_tests/tests/test_plutus_v2_spend_build.py +++ b/cardano_node_tests/tests/test_plutus_v2_spend_build.py @@ -2324,8 +2324,10 @@ def test_with_total_return_collateral( assert plutus_op.datum_file assert plutus_op.redeemer_cbor_file + protocol_params = cluster.get_protocol_params() + redeem_cost = plutus_common.compute_cost( - execution_cost=plutus_op.execution_cost, protocol_params=cluster.get_protocol_params() + execution_cost=plutus_op.execution_cost, protocol_params=protocol_params ) # fund the script address and create a UTxO for collateral @@ -2344,6 +2346,7 @@ def test_with_total_return_collateral( collateral_amount=amount_for_collateral, ) + payment_init_balance = cluster.get_address_balance(payment_addr.address) dst_init_balance = cluster.get_address_balance(dst_addr.address) # spend the "locked" UTxO @@ -2370,17 +2373,30 @@ def test_with_total_return_collateral( dst_balance == dst_init_balance - redeem_cost.collateral ), f"Collateral was NOT spent from `{dst_addr.address}` correctly" - if use_return_collateral: + if use_return_collateral or not use_total_collateral: txid_redeem = cluster.get_txid(tx_body_file=tx_output_redeem.out_file) return_col_utxos = cluster.get_utxo( txin=f"{txid_redeem}#{len(tx_output_redeem.txouts) + 1}" ) assert return_col_utxos, "Return collateral UTxO was not created" - assert ( - clusterlib.calculate_utxos_balance(utxos=return_col_utxos) - == return_collateral_amount - ), f"Incorrect balance for collateral return address `{dst_addr.address}`" + change_amount = clusterlib.calculate_utxos_balance(utxos=return_col_utxos) + + if use_return_collateral: + assert ( + change_amount == return_collateral_amount + ), f"Incorrect balance for collateral return address `{dst_addr.address}`" + else: + # check that the collateral amount charged is according to 'collateralPercentage' + collateral_charged = amount_for_collateral - change_amount + assert collateral_charged == round( + tx_output_redeem.fee * protocol_params["collateralPercentage"] / 100 + ), "The collateral amount charged is not the expected" + + payment_balance = cluster.get_address_balance(payment_addr.address) + assert payment_balance == payment_init_balance + clusterlib.calculate_utxos_balance( + utxos=return_col_utxos + ), f"Incorrect balance for change address `{payment_addr.address}`" @allure.link(helpers.get_vcs_link()) def test_collateral_with_tokens( From ff2b7d7ef77dedfd66ec7c5e423c2b591cf86b62 Mon Sep 17 00:00:00 2001 From: Martin Kourim Date: Wed, 17 Aug 2022 17:17:25 +0200 Subject: [PATCH 2/2] Small improvements and checking of return collateral address --- .../tests/test_plutus_v2_spend_build.py | 69 ++++++++++--------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/cardano_node_tests/tests/test_plutus_v2_spend_build.py b/cardano_node_tests/tests/test_plutus_v2_spend_build.py index 41c7c4cab..b59d05eb0 100644 --- a/cardano_node_tests/tests/test_plutus_v2_spend_build.py +++ b/cardano_node_tests/tests/test_plutus_v2_spend_build.py @@ -2346,9 +2346,6 @@ def test_with_total_return_collateral( collateral_amount=amount_for_collateral, ) - payment_init_balance = cluster.get_address_balance(payment_addr.address) - dst_init_balance = cluster.get_address_balance(dst_addr.address) - # spend the "locked" UTxO return_collateral_txouts = [ @@ -2367,36 +2364,44 @@ def test_with_total_return_collateral( return_collateral_txouts=return_collateral_txouts if use_return_collateral else (), ) - # check that the right amount of collateral was taken - dst_balance = cluster.get_address_balance(dst_addr.address) - assert ( - dst_balance == dst_init_balance - redeem_cost.collateral - ), f"Collateral was NOT spent from `{dst_addr.address}` correctly" + # check that collateral was taken + assert not cluster.get_utxo(utxo=collateral_utxos), "Collateral was NOT spent" - if use_return_collateral or not use_total_collateral: - txid_redeem = cluster.get_txid(tx_body_file=tx_output_redeem.out_file) - return_col_utxos = cluster.get_utxo( - txin=f"{txid_redeem}#{len(tx_output_redeem.txouts) + 1}" - ) - assert return_col_utxos, "Return collateral UTxO was not created" - - change_amount = clusterlib.calculate_utxos_balance(utxos=return_col_utxos) - - if use_return_collateral: - assert ( - change_amount == return_collateral_amount - ), f"Incorrect balance for collateral return address `{dst_addr.address}`" - else: - # check that the collateral amount charged is according to 'collateralPercentage' - collateral_charged = amount_for_collateral - change_amount - assert collateral_charged == round( - tx_output_redeem.fee * protocol_params["collateralPercentage"] / 100 - ), "The collateral amount charged is not the expected" - - payment_balance = cluster.get_address_balance(payment_addr.address) - assert payment_balance == payment_init_balance + clusterlib.calculate_utxos_balance( - utxos=return_col_utxos - ), f"Incorrect balance for change address `{payment_addr.address}`" + txid_redeem = cluster.get_txid(tx_body_file=tx_output_redeem.out_file) + return_collateral_utxos = cluster.get_utxo( + txin=f"{txid_redeem}#{len(tx_output_redeem.txouts) + 1}" + ) + + # when total collateral amount is specified, it is necessary to specify also return + # collateral `TxOut` + if use_total_collateral and not use_return_collateral: + assert not return_collateral_utxos, "Return collateral UTxO was unexpectedly created" + return + + # check that correct return collateral UTxO was created + assert return_collateral_utxos, "Return collateral UTxO was NOT created" + + returned_collateral_amount = clusterlib.calculate_utxos_balance( + utxos=return_collateral_utxos + ) + + if use_return_collateral: + assert ( + returned_collateral_amount == return_collateral_amount + ), f"Incorrect balance for collateral return address `{dst_addr.address}`" + assert ( + return_collateral_txouts[0].address == return_collateral_utxos[0].address + ), "Return collateral address doesn't match the specified address" + else: + # check that the collateral amount charged corresponds to 'collateralPercentage' + collateral_charged = amount_for_collateral - returned_collateral_amount + assert collateral_charged == round( + tx_output_redeem.fee * protocol_params["collateralPercentage"] / 100 + ), "The collateral amount charged is not the expected amount" + + assert ( + payment_addr.address == return_collateral_utxos[0].address + ), "Return collateral address doesn't match change address" @allure.link(helpers.get_vcs_link()) def test_collateral_with_tokens(