diff --git a/cardano_node_tests/tests/test_plutus_spend_build.py b/cardano_node_tests/tests/test_plutus_spend_build.py index 62b9d2bbc..8734d241c 100644 --- a/cardano_node_tests/tests/test_plutus_spend_build.py +++ b/cardano_node_tests/tests/test_plutus_spend_build.py @@ -5,6 +5,7 @@ import shutil from pathlib import Path from typing import Any +from typing import Generator from typing import List from typing import Optional from typing import Tuple @@ -13,6 +14,7 @@ import hypothesis import hypothesis.strategies as st import pytest +from _pytest.fixtures import FixtureRequest from cardano_clusterlib import clusterlib from cardano_node_tests.tests import common @@ -461,9 +463,7 @@ def test_txout_locking( @pytest.mark.dbsync @pytest.mark.testnets def test_context_equivalance( - self, - cluster: clusterlib.ClusterLib, - pool_users: List[clusterlib.PoolUser], + self, cluster: clusterlib.ClusterLib, pool_users: List[clusterlib.PoolUser] ): """Test context equivalence while spending a locked UTxO. @@ -1077,6 +1077,7 @@ def test_partial_spending( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, ): """Test spending part of funds (Lovelace and native tokens) on a locked UTxO. @@ -1091,6 +1092,7 @@ def test_partial_spending( * check expected Plutus cost * (optional) check transactions in db-sync """ + # pylint: disable=too-many-locals temp_template = common.get_test_id(cluster) token_rand = clusterlib.get_rand_str(5) @@ -1157,6 +1159,7 @@ def test_partial_spending( script_lovelace_balance = functools.reduce( lambda x, y: x + y.amount, script_lovelace_utxos, 0 ) + # Lovelace balance on change UTxOs change_lovelace_utxos = [ u for u in [*change_utxos, *build_change_utxos] if u.coin == clusterlib.DEFAULT_COIN @@ -1165,6 +1168,24 @@ def test_partial_spending( lambda x, y: x + y.amount, change_lovelace_utxos, 0 ) + # withdrawal script address + def _withdrawal_script_address(): + _build_spend_locked_txin( + temp_template=temp_template, + cluster_obj=cluster, + payment_addr=payment_addrs[0], + dst_addr=payment_addrs[1], + script_utxos=change_utxos, + collateral_utxos=collateral_utxos, + plutus_op=plutus_op, + amount=2_000_000, + tokens=[plutus_common.Token(coin=t.token, amount=80) for t in tokens], + ) + + request.addfinalizer(_withdrawal_script_address) + + cluster.get_utxo(script_utxos[0].address) + assert ( change_lovelace_balance == script_lovelace_balance - tx_output_spend.fee - amount_spend ) @@ -1312,6 +1333,7 @@ def test_collateral_w_tokens( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, ): """Test spending the locked UTxO while collateral contains native tokens. @@ -1354,6 +1376,41 @@ def test_collateral_w_tokens( tokens_collateral=tokens_rec, ) + # withdrawal script address + def _withdrawal_script_address(): + redeem_cost = plutus_common.compute_cost( + execution_cost=plutus_op.execution_cost, + protocol_params=cluster.get_protocol_params(), + ) + + destinations = [ + clusterlib.TxOut(address=payment_addrs[1].address, amount=redeem_cost.collateral) + ] + tx_files = clusterlib.TxFiles(signing_key_files=[payment_addrs[0].skey_file]) + + tx_raw_output = cluster.send_funds( + src_address=payment_addrs[0].address, + destinations=destinations, + tx_name=temp_template, + tx_files=tx_files, + ) + + txid_collateral = cluster.get_txid(tx_body_file=tx_raw_output.out_file) + collateral_utxos_without_tokens = cluster.get_utxo(txin=f"{txid_collateral}#0") + + _build_spend_locked_txin( + temp_template=temp_template, + cluster_obj=cluster, + payment_addr=payment_addrs[0], + dst_addr=payment_addrs[1], + script_utxos=script_utxos, + collateral_utxos=collateral_utxos_without_tokens, + plutus_op=plutus_op, + amount=2_000_000, + ) + + request.addfinalizer(_withdrawal_script_address) + with pytest.raises(clusterlib.CLIError) as excinfo: _build_spend_locked_txin( temp_template=temp_template, @@ -1379,6 +1436,7 @@ def test_same_collateral_txin( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, ): """Test spending the locked UTxO while using the same UTxO as collateral. @@ -1401,7 +1459,7 @@ def test_same_collateral_txin( execution_cost=plutus_common.ALWAYS_SUCCEEDS_COST, ) - script_utxos, __, tx_output_fund = _build_fund_script( + script_utxos, collateral_utxos, tx_output_fund = _build_fund_script( temp_template=temp_template, cluster_obj=cluster, payment_addr=payment_addrs[0], @@ -1409,6 +1467,21 @@ def test_same_collateral_txin( plutus_op=plutus_op, ) + # withdrawal script address + def _withdrawal_script_address(): + _build_spend_locked_txin( + temp_template=temp_template, + cluster_obj=cluster, + payment_addr=payment_addrs[0], + dst_addr=payment_addrs[1], + script_utxos=script_utxos, + collateral_utxos=collateral_utxos, + plutus_op=plutus_op, + amount=2_000_000, + ) + + request.addfinalizer(_withdrawal_script_address) + with pytest.raises(clusterlib.CLIError) as excinfo: _build_spend_locked_txin( temp_template=temp_template, @@ -1420,6 +1493,7 @@ def test_same_collateral_txin( plutus_op=plutus_op, amount=2_000_000, ) + assert "Expected key witnessed collateral" in str(excinfo.value) # check expected fees @@ -1441,6 +1515,7 @@ def test_guessing_game( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, variant: str, ): """Test locking a Tx output with a Plutus script and spending the locked UTxO. @@ -1458,6 +1533,37 @@ def test_guessing_game( """ temp_template = f"{common.get_test_id(cluster)}_{variant}" + plutus_op = plutus_common.PlutusOp( + script_file=plutus_common.GUESSING_GAME_PLUTUS, + datum_file=plutus_common.DATUM_42_TYPED, + redeemer_file=plutus_common.REDEEMER_42_TYPED, + execution_cost=plutus_common.GUESSING_GAME_COST, + ) + + script_utxos, collateral_utxos, __ = _build_fund_script( + temp_template=temp_template, + cluster_obj=cluster, + payment_addr=payment_addrs[0], + dst_addr=payment_addrs[1], + plutus_op=plutus_op, + ) + + # withdrawal script address + def _withdrawal_script_address(): + _build_spend_locked_txin( + temp_template=temp_template, + cluster_obj=cluster, + payment_addr=payment_addrs[0], + dst_addr=payment_addrs[1], + script_utxos=script_utxos, + collateral_utxos=collateral_utxos, + plutus_op=plutus_op, + amount=2_000_000, + ) + + # add the finalizer just one time + request.addfinalizer(_withdrawal_script_address) + if variant == "42_43": datum_file = plutus_common.DATUM_42_TYPED redeemer_file = plutus_common.REDEEMER_43_TYPED @@ -1470,21 +1576,13 @@ def test_guessing_game( else: raise AssertionError("Unknown test variant.") - plutus_op = plutus_common.PlutusOp( + plutus_op_variant = plutus_common.PlutusOp( script_file=plutus_common.GUESSING_GAME_PLUTUS, datum_file=datum_file, redeemer_file=redeemer_file, execution_cost=plutus_common.GUESSING_GAME_COST, ) - script_utxos, collateral_utxos, __ = _build_fund_script( - temp_template=temp_template, - cluster_obj=cluster, - payment_addr=payment_addrs[0], - dst_addr=payment_addrs[1], - plutus_op=plutus_op, - ) - with pytest.raises(clusterlib.CLIError) as excinfo: _build_spend_locked_txin( temp_template=temp_template, @@ -1493,11 +1591,13 @@ def test_guessing_game( dst_addr=payment_addrs[1], script_utxos=script_utxos, collateral_utxos=collateral_utxos, - plutus_op=plutus_op, + plutus_op=plutus_op_variant, amount=2_000_000, ) - assert "The Plutus script evaluation failed" in str(excinfo.value) + assert "The Plutus script evaluation failed" in str( + excinfo.value + ) or "The Plutus script witness has the wrong datum" in str(excinfo.value) @allure.link(helpers.get_vcs_link()) @pytest.mark.testnets @@ -1505,6 +1605,7 @@ def test_two_scripts_spending_one_fail( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, ): """Test locking two Tx outputs with two different Plutus scripts in single Tx, one fails. @@ -1596,6 +1697,22 @@ def test_two_scripts_spending_one_fail( collateral_utxos1 = cluster.get_utxo(txin=f"{txid_fund}#3") collateral_utxos2 = cluster.get_utxo(txin=f"{txid_fund}#4") + # withdrawal script address + def _withdrawal_script_address(): + _build_spend_locked_txin( + temp_template=temp_template, + cluster_obj=cluster, + payment_addr=payment_addrs[0], + dst_addr=payment_addrs[1], + script_utxos=script_utxos1, + collateral_utxos=collateral_utxos1, + plutus_op=plutus_op1, + amount=2_000_000, + ) + + # add the finalizer + request.addfinalizer(_withdrawal_script_address) + # Step 2: spend the "locked" UTxOs assert plutus_op1.datum_file and plutus_op2.datum_file @@ -1650,7 +1767,7 @@ def fund_script_guessing_game( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], - ) -> Tuple[List[clusterlib.UTXOData], List[clusterlib.UTXOData]]: + ) -> Generator[Tuple[List[clusterlib.UTXOData], List[clusterlib.UTXOData]], None, None]: """Fund a Plutus script and create the locked UTxO and collateral UTxO. Uses `cardano-cli transaction build` command for building the transactions. @@ -1671,7 +1788,26 @@ def fund_script_guessing_game( plutus_op=plutus_op, ) - return script_utxos, collateral_utxos + yield script_utxos, collateral_utxos + + # withdrawal script address + plutus_op = plutus_common.PlutusOp( + script_file=plutus_common.GUESSING_GAME_UNTYPED_PLUTUS, + datum_file=plutus_common.DATUM_42, + redeemer_value="42", + execution_cost=plutus_common.GUESSING_GAME_UNTYPED_COST, + ) + + _build_spend_locked_txin( + temp_template=temp_template, + cluster_obj=cluster, + payment_addr=payment_addrs[0], + dst_addr=payment_addrs[1], + script_utxos=script_utxos, + collateral_utxos=collateral_utxos, + plutus_op=plutus_op, + amount=self.AMOUNT, + ) def _int_out_of_range( self, @@ -2305,6 +2441,7 @@ def test_unlock_tx_wrong_datum( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, ): """Test locking a Tx output and try to spend it with a wrong datum. @@ -2327,6 +2464,22 @@ def test_unlock_tx_wrong_datum( plutus_op=plutus_op_1, ) + # withdrawal script address + def _withdrawal_script_address(): + _build_spend_locked_txin( + temp_template=temp_template, + cluster_obj=cluster, + payment_addr=payment_addrs[0], + dst_addr=payment_addrs[1], + script_utxos=script_utxos, + collateral_utxos=collateral_utxos, + plutus_op=plutus_op_1, + amount=2_000_000, + ) + + # add the finalizer + request.addfinalizer(_withdrawal_script_address) + # use a wrong datum to try to unlock the funds plutus_op_2 = plutus_common.PlutusOp( script_file=plutus_common.ALWAYS_SUCCEEDS_PLUTUS, diff --git a/cardano_node_tests/tests/test_plutus_spend_raw.py b/cardano_node_tests/tests/test_plutus_spend_raw.py index e05632967..a593f3745 100644 --- a/cardano_node_tests/tests/test_plutus_spend_raw.py +++ b/cardano_node_tests/tests/test_plutus_spend_raw.py @@ -7,6 +7,7 @@ import time from pathlib import Path from typing import Any +from typing import Generator from typing import List from typing import Optional from typing import Tuple @@ -15,6 +16,7 @@ import hypothesis import hypothesis.strategies as st import pytest +from _pytest.fixtures import FixtureRequest from cardano_clusterlib import clusterlib from cardano_node_tests.tests import common @@ -436,6 +438,49 @@ def _check_pretty_utxo( return err +def _withdrawal_script_address( + cluster: clusterlib.ClusterLib, + collateral_utxos: List[clusterlib.UTXOData], + dst_addr: clusterlib.AddressRecord, + plutus_op: plutus_common.PlutusOp, + script_utxos: List[clusterlib.UTXOData], + tokens: Optional[List[plutus_common.Token]] = None, +): + # for mypy + assert plutus_op.execution_cost and plutus_op.execution_cost.per_space + + plutus_txins = [ + clusterlib.ScriptTxIn( + txins=script_utxos, + script_file=plutus_op.script_file, + collaterals=collateral_utxos, + execution_units=(plutus_op.execution_cost.per_time, plutus_op.execution_cost.per_space), + datum_file=plutus_op.datum_file if plutus_op.datum_file else "", + datum_cbor_file=plutus_op.datum_cbor_file if plutus_op.datum_cbor_file else "", + datum_value=plutus_op.datum_value if plutus_op.datum_value else "", + redeemer_file=plutus_op.redeemer_file if plutus_op.redeemer_file else "", + redeemer_cbor_file=plutus_op.redeemer_cbor_file if plutus_op.redeemer_cbor_file else "", + redeemer_value=plutus_op.redeemer_value if plutus_op.redeemer_value else "", + ) + ] + + txouts = [clusterlib.TxOut(address=dst_addr.address, amount=-1)] + + for token in tokens or []: + txouts.append( + clusterlib.TxOut(address=dst_addr.address, amount=token.amount, coin=token.coin) + ) + + cluster.send_tx( + src_address=script_utxos[0].address, + tx_name="test", + script_txins=plutus_txins, + txouts=txouts, + tx_files=clusterlib.TxFiles(signing_key_files=[dst_addr.skey_file]), + verify_tx=False, + ) + + class TestLocking: """Tests for Tx output locking using Plutus smart contracts.""" @@ -1063,6 +1108,7 @@ def test_partial_spending( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, ): """Test spending part of funds (Lovelace and native tokens) on a locked UTxO. @@ -1133,6 +1179,25 @@ def test_partial_spending( txid_spend = cluster.get_txid(tx_body_file=tx_output_spend.out_file) change_utxos = cluster.get_utxo(txin=f"{txid_spend}#1") + # withdrawal script address + def _withdrawal_finalizer(): + unspent_tokens = [ + plutus_common.Token(coin=t.token, amount=token_amount_fund - token_amount_spend) + for t in tokens + ] + + _withdrawal_script_address( + cluster=cluster, + dst_addr=payment_addrs[1], + plutus_op=plutus_op, + collateral_utxos=collateral_utxos, + script_utxos=change_utxos, + tokens=unspent_tokens, + ) + + # add the finalizer + request.addfinalizer(_withdrawal_finalizer) + # check that the expected amounts of Lovelace and native tokens were spent and change UTxOs # with appropriate datum hash were created token_amount_exp = token_amount_fund - token_amount_spend @@ -1273,6 +1338,7 @@ def test_invalid_guessing_game( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, variant: str, ): """Test locking a Tx output with a Plutus script and spending the locked UTxO. @@ -1290,6 +1356,35 @@ def test_invalid_guessing_game( temp_template = f"{common.get_test_id(cluster)}_{variant}" amount = 2_000_000 + plutus_op = plutus_common.PlutusOp( + script_file=plutus_common.GUESSING_GAME_PLUTUS, + datum_file=plutus_common.DATUM_42_TYPED, + redeemer_file=plutus_common.REDEEMER_42_TYPED, + execution_cost=plutus_common.GUESSING_GAME_COST, + ) + + script_utxos, collateral_utxos, __ = _fund_script( + temp_template=temp_template, + cluster_obj=cluster, + payment_addr=payment_addrs[0], + dst_addr=payment_addrs[1], + plutus_op=plutus_op, + amount=amount, + ) + + # withdrawal script address + def _withdrawal_finalizer(): + _withdrawal_script_address( + cluster=cluster, + dst_addr=payment_addrs[1], + plutus_op=plutus_op, + collateral_utxos=collateral_utxos, + script_utxos=script_utxos, + ) + + # add the finalizer + request.addfinalizer(_withdrawal_finalizer) + if variant == "42_43": datum_file = plutus_common.DATUM_42_TYPED redeemer_file = plutus_common.REDEEMER_43_TYPED @@ -1302,33 +1397,25 @@ def test_invalid_guessing_game( else: raise AssertionError("Unknown test variant.") - plutus_op = plutus_common.PlutusOp( + plutus_op_variant = plutus_common.PlutusOp( script_file=plutus_common.GUESSING_GAME_PLUTUS, datum_file=datum_file, redeemer_file=redeemer_file, execution_cost=plutus_common.GUESSING_GAME_COST, ) - script_utxos, collateral_utxos, __ = _fund_script( - temp_template=temp_template, - cluster_obj=cluster, - payment_addr=payment_addrs[0], - dst_addr=payment_addrs[1], - plutus_op=plutus_op, - amount=amount, - ) err, __ = _spend_locked_txin( temp_template=temp_template, cluster_obj=cluster, dst_addr=payment_addrs[1], script_utxos=script_utxos, collateral_utxos=collateral_utxos, - plutus_op=plutus_op, + plutus_op=plutus_op_variant, amount=amount, expect_failure=True, ) - assert "ValidationTagMismatch (IsValid True)" in err + assert "ValidationTagMismatch (IsValid True)" in err or "MissingRequiredDatums" in err @allure.link(helpers.get_vcs_link()) @pytest.mark.dbsync @@ -1337,6 +1424,7 @@ def test_collateral_w_tokens( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, ): """Test spending the locked UTxO while collateral contains native tokens. @@ -1380,6 +1468,39 @@ def test_collateral_w_tokens( tokens_collateral=tokens_rec, ) + # withdrawal script address + def _withdrawal_finalizer(): + redeem_cost = plutus_common.compute_cost( + execution_cost=plutus_op.execution_cost, + protocol_params=cluster.get_protocol_params(), + ) + + destinations = [ + clusterlib.TxOut(address=payment_addrs[1].address, amount=redeem_cost.collateral) + ] + tx_files = clusterlib.TxFiles(signing_key_files=[payment_addrs[0].skey_file]) + + tx_raw_output = cluster.send_funds( + src_address=payment_addrs[0].address, + destinations=destinations, + tx_name=temp_template, + tx_files=tx_files, + ) + + txid_collateral = cluster.get_txid(tx_body_file=tx_raw_output.out_file) + collateral_utxos_without_tokens = cluster.get_utxo(txin=f"{txid_collateral}#0") + + _withdrawal_script_address( + cluster=cluster, + dst_addr=payment_addrs[1], + plutus_op=plutus_op, + collateral_utxos=collateral_utxos_without_tokens, + script_utxos=script_utxos, + ) + + # add the finalizer + request.addfinalizer(_withdrawal_finalizer) + with pytest.raises(clusterlib.CLIError) as excinfo: _spend_locked_txin( temp_template=temp_template, @@ -1399,6 +1520,7 @@ def test_same_collateral_txin( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, ): """Test spending the locked UTxO while using the same UTxO as collateral. @@ -1420,7 +1542,7 @@ def test_same_collateral_txin( execution_cost=plutus_common.ALWAYS_SUCCEEDS_COST, ) - script_utxos, *__ = _fund_script( + script_utxos, collateral_utxos, __ = _fund_script( temp_template=temp_template, cluster_obj=cluster, payment_addr=payment_addrs[0], @@ -1429,6 +1551,19 @@ def test_same_collateral_txin( amount=amount, ) + # withdrawal script address + def _withdrawal_finalizer(): + _withdrawal_script_address( + cluster=cluster, + dst_addr=payment_addrs[1], + plutus_op=plutus_op, + collateral_utxos=collateral_utxos, + script_utxos=script_utxos, + ) + + # add the finalizer + request.addfinalizer(_withdrawal_finalizer) + with pytest.raises(clusterlib.CLIError) as excinfo: _spend_locked_txin( temp_template=temp_template, @@ -1450,6 +1585,7 @@ def test_collateral_percent( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, ): """Try to spend locked UTxO while collateral is less than required by `collateralPercentage`. @@ -1484,6 +1620,41 @@ def test_collateral_percent( collateral_fraction_offset=0.9, ) + # withdrawal script address + def _withdrawal_finalizer(): + redeem_cost = plutus_common.compute_cost( + execution_cost=plutus_op.execution_cost, + protocol_params=cluster.get_protocol_params(), + ) + + destinations = [ + clusterlib.TxOut( + address=payment_addrs[1].address, amount=redeem_cost.collateral * 2 + ) + ] + tx_files = clusterlib.TxFiles(signing_key_files=[payment_addrs[0].skey_file]) + + tx_raw_output = cluster.send_funds( + src_address=payment_addrs[0].address, + destinations=destinations, + tx_name=temp_template, + tx_files=tx_files, + ) + + txid_collateral = cluster.get_txid(tx_body_file=tx_raw_output.out_file) + collateral_utxos_right_value = cluster.get_utxo(txin=f"{txid_collateral}#0") + + _withdrawal_script_address( + cluster=cluster, + dst_addr=payment_addrs[1], + plutus_op=plutus_op, + collateral_utxos=collateral_utxos_right_value, + script_utxos=script_utxos, + ) + + # add the finalizer + request.addfinalizer(_withdrawal_finalizer) + with pytest.raises(clusterlib.CLIError) as excinfo: _spend_locked_txin( temp_template=temp_template, @@ -1502,6 +1673,7 @@ def test_two_scripts_spending_one_fail( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, ): """Test locking two Tx outputs with two different Plutus scripts in single Tx, one fails. @@ -1601,6 +1773,19 @@ def test_two_scripts_spending_one_fail( collateral_utxos1 = cluster.get_utxo(txin=f"{txid_fund}#2") collateral_utxos2 = cluster.get_utxo(txin=f"{txid_fund}#3") + # withdrawal script address + def _withdrawal_finalizer(): + _withdrawal_script_address( + cluster=cluster, + dst_addr=payment_addrs[1], + plutus_op=plutus_op1, + collateral_utxos=collateral_utxos1, + script_utxos=script_utxos1, + ) + + # add the finalizer + request.addfinalizer(_withdrawal_finalizer) + # Step 2: spend the "locked" UTxOs assert plutus_op1.datum_file and plutus_op2.datum_file @@ -1668,45 +1853,56 @@ def fund_script_guessing_game( self, cluster_manager: cluster_management.ClusterManager, cluster: clusterlib.ClusterLib, - ) -> FundTupleT: + ) -> Generator[FundTupleT, None, None]: """Fund a Plutus script and create the locked UTxO and collateral UTxO.""" - with cluster_manager.cache_fixture() as fixture_cache: - if fixture_cache.value: - return fixture_cache.value # type: ignore + temp_template = common.get_test_id(cluster) - temp_template = common.get_test_id(cluster) + payment_addrs = clusterlib_utils.create_payment_addr_records( + *[f"{temp_template}_payment_addr_{i}" for i in range(2)], + cluster_obj=cluster, + ) - payment_addrs = clusterlib_utils.create_payment_addr_records( - *[f"{temp_template}_payment_addr_{i}" for i in range(2)], - cluster_obj=cluster, - ) + # fund source address + clusterlib_utils.fund_from_faucet( + payment_addrs[0], + cluster_obj=cluster, + faucet_data=cluster_manager.cache.addrs_data["user1"], + amount=3_000_000_000, + ) - # fund source address - clusterlib_utils.fund_from_faucet( - payment_addrs[0], - cluster_obj=cluster, - faucet_data=cluster_manager.cache.addrs_data["user1"], - amount=3_000_000_000, - ) + plutus_op = plutus_common.PlutusOp( + script_file=plutus_common.GUESSING_GAME_UNTYPED_PLUTUS, + datum_file=plutus_common.DATUM_42, + execution_cost=plutus_common.GUESSING_GAME_UNTYPED_COST, + ) - plutus_op = plutus_common.PlutusOp( - script_file=plutus_common.GUESSING_GAME_UNTYPED_PLUTUS, - datum_file=plutus_common.DATUM_42, - execution_cost=plutus_common.GUESSING_GAME_UNTYPED_COST, - ) + script_utxos, collateral_utxos, __ = _fund_script( + temp_template=temp_template, + cluster_obj=cluster, + payment_addr=payment_addrs[0], + dst_addr=payment_addrs[1], + plutus_op=plutus_op, + amount=self.AMOUNT, + ) - script_utxos, collateral_utxos, __ = _fund_script( - temp_template=temp_template, - cluster_obj=cluster, - payment_addr=payment_addrs[0], - dst_addr=payment_addrs[1], - plutus_op=plutus_op, - amount=self.AMOUNT, - ) + yield script_utxos, collateral_utxos, payment_addrs - fixture_cache.value = script_utxos, collateral_utxos, payment_addrs + # withdrawal script address + plutus_op = plutus_common.PlutusOp( + script_file=plutus_common.GUESSING_GAME_UNTYPED_PLUTUS, + datum_file=plutus_common.DATUM_42, + redeemer_value="42", + execution_cost=plutus_common.GUESSING_GAME_UNTYPED_COST, + ) - return script_utxos, collateral_utxos, payment_addrs + # withdrawal script address + _withdrawal_script_address( + cluster=cluster, + dst_addr=payment_addrs[1], + plutus_op=plutus_op, + collateral_utxos=collateral_utxos, + script_utxos=script_utxos, + ) @pytest.fixture def cost_per_unit( @@ -2370,6 +2566,7 @@ def test_unlock_tx_wrong_datum( self, cluster: clusterlib.ClusterLib, payment_addrs: List[clusterlib.AddressRecord], + request: FixtureRequest, ): """Test locking a Tx output and try to spend it with a wrong datum. @@ -2398,6 +2595,21 @@ def test_unlock_tx_wrong_datum( amount=amount, ) + # withdrawal script address + def _withdrawal_script_address(): + _spend_locked_txin( + temp_template=temp_template, + cluster_obj=cluster, + dst_addr=dst_addr, + script_utxos=script_utxos, + collateral_utxos=collateral_utxos, + plutus_op=plutus_op_1, + amount=amount, + ) + + # add the finalizer + request.addfinalizer(_withdrawal_script_address) + # use a wrong datum to try to unlock the funds plutus_op_2 = plutus_common.PlutusOp( script_file=plutus_common.ALWAYS_SUCCEEDS_PLUTUS,