Skip to content

Commit

Permalink
Merge pull request #1146 from saratomaz/test_collateral_output_build
Browse files Browse the repository at this point in the history
check collateral output feature using build
  • Loading branch information
mkoura committed Jun 29, 2022
2 parents 1fb7734 + d3c6130 commit f7f3fa1
Show file tree
Hide file tree
Showing 2 changed files with 371 additions and 2 deletions.
109 changes: 109 additions & 0 deletions cardano_node_tests/tests/test_plutus_v2_mint_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,112 @@ def test_minting_one_token(
plutus_cost=plutus_cost,
expected_cost=[expected_cost],
)


@pytest.mark.testnets
class TestNegativeCollateralOutput:
"""Tests for collateral output that are expected to fail."""

@allure.link(helpers.get_vcs_link())
@pytest.mark.parametrize(
"with_return_collateral",
(True, False),
ids=("with_return_collateral", "without_return_collateral"),
)
def test_minting_with_unbalanced_total_collateral(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: List[clusterlib.AddressRecord],
with_return_collateral: bool,
request: FixtureRequest,
):
"""Test minting a token with a Plutus script with unbalanced total collateral.
Expect failure.
"""
temp_template = f"{common.get_test_id(cluster)}_{request.node.callspec.id}"
payment_addr = payment_addrs[0]
issuer_addr = payment_addrs[1]

lovelace_amount = 2_000_000
token_amount = 5
script_fund = 200_000_000

minting_cost = plutus_common.compute_cost(
execution_cost=plutus_common.MINTING_V2_COST,
protocol_params=cluster.get_protocol_params(),
)

# Step 1: fund the token issuer

mint_utxos, *__ = _fund_issuer(
cluster_obj=cluster,
temp_template=temp_template,
payment_addr=payment_addr,
issuer_addr=issuer_addr,
minting_cost=minting_cost,
amount=script_fund,
)

collateral_utxo = clusterlib.UTXOData(
utxo_hash=mint_utxos[0].utxo_hash,
utxo_ix=2,
amount=minting_cost.collateral,
address=issuer_addr.address,
)

# Step 2: mint the "qacoin"

policyid = cluster.get_policyid(plutus_common.MINTING_PLUTUS_V2)
asset_name = f"qacoin{clusterlib.get_rand_str(4)}".encode("utf-8").hex()
token = f"{policyid}.{asset_name}"
mint_txouts = [
clusterlib.TxOut(address=issuer_addr.address, amount=token_amount, coin=token)
]

plutus_mint_data = [
clusterlib.Mint(
txouts=mint_txouts,
script_file=plutus_common.MINTING_PLUTUS_V2,
collaterals=[collateral_utxo],
execution_units=(
plutus_common.MINTING_COST.per_time,
plutus_common.MINTING_COST.per_space,
),
redeemer_cbor_file=plutus_common.REDEEMER_42_CBOR,
)
]

tx_files_step2 = clusterlib.TxFiles(
signing_key_files=[issuer_addr.skey_file],
)
txouts_step2 = [
clusterlib.TxOut(address=issuer_addr.address, amount=lovelace_amount),
*mint_txouts,
]

return_collateral_txouts = [
clusterlib.TxOut(payment_addr.address, amount=minting_cost.collateral)
]

tx_output_step2 = cluster.build_tx(
src_address=payment_addr.address,
tx_name=f"{temp_template}_step2",
tx_files=tx_files_step2,
txins=mint_utxos,
txouts=txouts_step2,
return_collateral_txouts=return_collateral_txouts if with_return_collateral else (),
total_collateral_amount=minting_cost.collateral // 2,
mint=plutus_mint_data,
)
tx_signed_step2 = cluster.sign_tx(
tx_body_file=tx_output_step2.out_file,
signing_key_files=tx_files_step2.signing_key_files,
tx_name=f"{temp_template}_step2",
)

# it should NOT be possible to mint with an unbalanced total collateral
with pytest.raises(clusterlib.CLIError) as excinfo:
cluster.submit_tx(tx_file=tx_signed_step2, txins=mint_utxos)
err_str = str(excinfo.value)
assert "IncorrectTotalCollateralField" in err_str, err_str

0 comments on commit f7f3fa1

Please sign in to comment.