/
mint_raw.py
2037 lines (1735 loc) · 76.4 KB
/
mint_raw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for minting with Plutus using `transaction build-raw`."""
import datetime
import logging
import shutil
from pathlib import Path
from typing import List
from typing import Tuple
import allure
import hypothesis
import hypothesis.strategies as st
import pytest
from _pytest.fixtures import FixtureRequest
from _pytest.fixtures import SubRequest
from cardano_clusterlib import clusterlib
from cardano_clusterlib import clusterlib_helpers
from cardano_node_tests.cluster_management import cluster_management
from cardano_node_tests.tests import common
from cardano_node_tests.tests import plutus_common
from cardano_node_tests.utils import clusterlib_utils
from cardano_node_tests.utils import configuration
from cardano_node_tests.utils import dbsync_utils
from cardano_node_tests.utils import helpers
from cardano_node_tests.utils import tx_view
from cardano_node_tests.utils.versions import VERSIONS
LOGGER = logging.getLogger(__name__)
# skip all tests if Tx era < alonzo
pytestmark = [
common.SKIPIF_PLUTUS_UNUSABLE,
pytest.mark.smoke,
]
# approx. fee for Tx size
FEE_MINT_TXSIZE = 400_000
@pytest.fixture
def payment_addrs(
cluster_manager: cluster_management.ClusterManager,
cluster: clusterlib.ClusterLib,
) -> List[clusterlib.AddressRecord]:
"""Create new payment address."""
test_id = common.get_test_id(cluster)
addrs = clusterlib_utils.create_payment_addr_records(
*[f"{test_id}_payment_addr_{i}" for i in range(2)],
cluster_obj=cluster,
)
# fund source address
clusterlib_utils.fund_from_faucet(
addrs[0],
cluster_obj=cluster,
faucet_data=cluster_manager.cache.addrs_data["user1"],
amount=3_000_000_000,
)
return addrs
def _check_pretty_utxo(
cluster_obj: clusterlib.ClusterLib, tx_raw_output: clusterlib.TxRawOutput
) -> str:
"""Check that pretty printed `query utxo` output looks as expected."""
err = ""
txid = cluster_obj.g_transaction.get_txid(tx_body_file=tx_raw_output.out_file)
utxo_out = (
cluster_obj.cli(
[
"query",
"utxo",
"--tx-in",
f"{txid}#0",
*cluster_obj.magic_args,
]
)
.stdout.decode("utf-8")
.split()
)
expected_out = [
"TxHash",
"TxIx",
"Amount",
"--------------------------------------------------------------------------------------",
txid,
"0",
str(tx_raw_output.txouts[0].amount),
tx_raw_output.txouts[0].coin,
"+",
str(tx_raw_output.txouts[1].amount),
tx_raw_output.txouts[1].coin,
"+",
str(tx_raw_output.txouts[2].amount),
tx_raw_output.txouts[2].coin,
"+",
"TxOutDatumNone",
]
if utxo_out != expected_out:
err = f"Pretty UTxO output doesn't match expected output:\n{utxo_out}\nvs\n{expected_out}"
return err
def _fund_issuer(
cluster_obj: clusterlib.ClusterLib,
temp_template: str,
payment_addr: clusterlib.AddressRecord,
issuer_addr: clusterlib.AddressRecord,
minting_cost: plutus_common.ScriptCost,
amount: int,
fee_txsize: int = FEE_MINT_TXSIZE,
collateral_utxo_num: int = 1,
) -> Tuple[List[clusterlib.UTXOData], List[clusterlib.UTXOData], clusterlib.TxRawOutput]:
"""Fund the token issuer."""
single_collateral_amount = minting_cost.collateral // collateral_utxo_num
collateral_amounts = [single_collateral_amount for __ in range(collateral_utxo_num - 1)]
collateral_subtotal = sum(collateral_amounts)
collateral_amounts.append(minting_cost.collateral - collateral_subtotal)
issuer_init_balance = cluster_obj.g_query.get_address_balance(issuer_addr.address)
tx_files = clusterlib.TxFiles(
signing_key_files=[payment_addr.skey_file],
)
txouts = [
clusterlib.TxOut(
address=issuer_addr.address,
amount=amount + minting_cost.fee + fee_txsize,
),
*[clusterlib.TxOut(address=issuer_addr.address, amount=a) for a in collateral_amounts],
]
tx_raw_output = cluster_obj.g_transaction.send_tx(
src_address=payment_addr.address,
tx_name=f"{temp_template}_step1",
txouts=txouts,
tx_files=tx_files,
# TODO: workaround for https://github.com/input-output-hk/cardano-node/issues/1892
witness_count_add=2,
# don't join 'change' and 'collateral' txouts, we need separate UTxOs
join_txouts=False,
)
issuer_balance = cluster_obj.g_query.get_address_balance(issuer_addr.address)
assert (
issuer_balance
== issuer_init_balance + amount + minting_cost.fee + fee_txsize + minting_cost.collateral
), f"Incorrect balance for token issuer address `{issuer_addr.address}`"
txid = cluster_obj.g_transaction.get_txid(tx_body_file=tx_raw_output.out_file)
mint_utxos = cluster_obj.g_query.get_utxo(txin=f"{txid}#0")
collateral_utxos = [
clusterlib.UTXOData(utxo_hash=txid, utxo_ix=idx, amount=a, address=issuer_addr.address)
for idx, a in enumerate(collateral_amounts, start=1)
]
return mint_utxos, collateral_utxos, tx_raw_output
class TestMinting:
"""Tests for minting using Plutus smart contracts."""
@allure.link(helpers.get_vcs_link())
@pytest.mark.dbsync
@pytest.mark.testnets
@common.PARAM_PLUTUS_VERSION
def test_minting_two_tokens(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: List[clusterlib.AddressRecord],
plutus_version: str,
):
"""Test minting two tokens with a single Plutus script.
* fund the token issuer and create a UTxO for collateral
* check that the expected amount was transferred to token issuer's address
* mint the tokens using a Plutus script
* check that the tokens were minted and collateral UTxO was not spent
* (optional) check transactions in db-sync
"""
# pylint: disable=too-many-locals
temp_template = f"{common.get_test_id(cluster)}_{plutus_version}"
payment_addr = payment_addrs[0]
issuer_addr = payment_addrs[1]
lovelace_amount = 2_000_000
token_amount = 5
fee_txsize = 600_000
plutus_v_record = plutus_common.MINTING_PLUTUS[plutus_version]
minting_cost = plutus_common.compute_cost(
execution_cost=plutus_v_record.execution_cost,
protocol_params=cluster.g_query.get_protocol_params(),
)
# Step 1: fund the token issuer
mint_utxos, collateral_utxos, tx_raw_output_step1 = _fund_issuer(
cluster_obj=cluster,
temp_template=temp_template,
payment_addr=payment_addr,
issuer_addr=issuer_addr,
minting_cost=minting_cost,
amount=lovelace_amount,
fee_txsize=fee_txsize,
collateral_utxo_num=2,
)
issuer_fund_balance = cluster.g_query.get_address_balance(issuer_addr.address)
# Step 2: mint the "qacoin"
policyid = cluster.g_transaction.get_policyid(plutus_v_record.script_file)
asset_name_a = f"qacoina{clusterlib.get_rand_str(4)}".encode().hex()
token_a = f"{policyid}.{asset_name_a}"
asset_name_b = f"qacoinb{clusterlib.get_rand_str(4)}".encode().hex()
token_b = f"{policyid}.{asset_name_b}"
mint_txouts = [
clusterlib.TxOut(address=issuer_addr.address, amount=token_amount, coin=token_a),
clusterlib.TxOut(address=issuer_addr.address, amount=token_amount, coin=token_b),
]
plutus_mint_data = [
clusterlib.Mint(
txouts=mint_txouts,
script_file=plutus_v_record.script_file,
collaterals=collateral_utxos,
execution_units=(
plutus_v_record.execution_cost.per_time,
plutus_v_record.execution_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,
]
tx_raw_output_step2 = cluster.g_transaction.build_raw_tx_bare(
out_file=f"{temp_template}_step2_tx.body",
txins=mint_utxos,
txouts=txouts_step2,
mint=plutus_mint_data,
tx_files=tx_files_step2,
fee=minting_cost.fee + fee_txsize,
# ttl is optional in this test
invalid_hereafter=cluster.g_query.get_slot_no() + 200,
)
tx_signed_step2 = cluster.g_transaction.sign_tx(
tx_body_file=tx_raw_output_step2.out_file,
signing_key_files=tx_files_step2.signing_key_files,
tx_name=f"{temp_template}_step2",
)
cluster.g_transaction.submit_tx(tx_file=tx_signed_step2, txins=mint_utxos)
assert (
cluster.g_query.get_address_balance(issuer_addr.address)
== issuer_fund_balance - tx_raw_output_step2.fee
), f"Incorrect balance for token issuer address `{issuer_addr.address}`"
out_utxos = cluster.g_query.get_utxo(tx_raw_output=tx_raw_output_step2)
token_utxo_a = clusterlib.filter_utxos(
utxos=out_utxos, address=issuer_addr.address, coin=token_a
)
assert (
token_utxo_a and token_utxo_a[0].amount == token_amount
), "The 'token a' was not minted"
token_utxo_b = clusterlib.filter_utxos(
utxos=out_utxos, address=issuer_addr.address, coin=token_b
)
assert (
token_utxo_b and token_utxo_b[0].amount == token_amount
), "The 'token b' was not minted"
# check tx view
tx_view.check_tx_view(cluster_obj=cluster, tx_raw_output=tx_raw_output_step2)
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_raw_output_step1)
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_raw_output_step2)
utxo_err = _check_pretty_utxo(cluster_obj=cluster, tx_raw_output=tx_raw_output_step2)
if utxo_err:
pytest.fail(utxo_err)
@allure.link(helpers.get_vcs_link())
@pytest.mark.dbsync
@pytest.mark.testnets
@pytest.mark.parametrize(
"key",
(
"normal",
"extended",
),
)
def test_witness_redeemer(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: List[clusterlib.AddressRecord],
key: str,
):
"""Test minting a token with a Plutus script.
* fund the token issuer and create a UTxO for collateral
* check that the expected amount was transferred to token issuer's address
* mint the token using a Plutus script with required signer
* check that the token was minted and collateral UTxO was not spent
* (optional) check transactions in db-sync
"""
# pylint: disable=too-many-locals
temp_template = f"{common.get_test_id(cluster)}_{key}"
payment_addr = payment_addrs[0]
issuer_addr = payment_addrs[1]
lovelace_amount = 2_000_000
token_amount = 5
minting_cost = plutus_common.compute_cost(
execution_cost=plutus_common.MINTING_WITNESS_REDEEMER_COST,
protocol_params=cluster.g_query.get_protocol_params(),
)
if key == "normal":
redeemer_file = plutus_common.DATUM_WITNESS_GOLDEN_NORMAL
signing_key_golden = plutus_common.SIGNING_KEY_GOLDEN
else:
redeemer_file = plutus_common.DATUM_WITNESS_GOLDEN_EXTENDED
signing_key_golden = plutus_common.SIGNING_KEY_GOLDEN_EXTENDED
# Step 1: fund the token issuer
mint_utxos, collateral_utxos, tx_raw_output_step1 = _fund_issuer(
cluster_obj=cluster,
temp_template=temp_template,
payment_addr=payment_addr,
issuer_addr=issuer_addr,
minting_cost=minting_cost,
amount=lovelace_amount,
)
issuer_fund_balance = cluster.g_query.get_address_balance(issuer_addr.address)
# Step 2: mint the "qacoin"
policyid = cluster.g_transaction.get_policyid(
plutus_common.MINTING_WITNESS_REDEEMER_PLUTUS_V1
)
asset_name = f"qacoin{clusterlib.get_rand_str(4)}".encode().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_WITNESS_REDEEMER_PLUTUS_V1,
collaterals=collateral_utxos,
execution_units=(
plutus_common.MINTING_WITNESS_REDEEMER_COST.per_time,
plutus_common.MINTING_WITNESS_REDEEMER_COST.per_space,
),
redeemer_file=redeemer_file,
)
]
tx_files_step2 = clusterlib.TxFiles(
signing_key_files=[issuer_addr.skey_file, signing_key_golden],
)
txouts_step2 = [
clusterlib.TxOut(address=issuer_addr.address, amount=lovelace_amount),
*mint_txouts,
]
tx_raw_output_step2 = cluster.g_transaction.build_raw_tx_bare(
out_file=f"{temp_template}_step2_tx.body",
txins=mint_utxos,
txouts=txouts_step2,
mint=plutus_mint_data,
tx_files=tx_files_step2,
fee=minting_cost.fee + FEE_MINT_TXSIZE,
required_signers=[signing_key_golden],
)
# sign incrementally (just to check that it works)
tx_signed_step2 = cluster.g_transaction.sign_tx(
tx_body_file=tx_raw_output_step2.out_file,
signing_key_files=[issuer_addr.skey_file],
tx_name=f"{temp_template}_step2_sign0",
)
tx_signed_step2_inc = cluster.g_transaction.sign_tx(
tx_file=tx_signed_step2,
signing_key_files=[signing_key_golden],
tx_name=f"{temp_template}_step2_sign1",
)
cluster.g_transaction.submit_tx(tx_file=tx_signed_step2_inc, txins=mint_utxos)
assert (
cluster.g_query.get_address_balance(issuer_addr.address)
== issuer_fund_balance - tx_raw_output_step2.fee
), f"Incorrect balance for token issuer address `{issuer_addr.address}`"
out_utxos = cluster.g_query.get_utxo(tx_raw_output=tx_raw_output_step2)
token_utxo = clusterlib.filter_utxos(
utxos=out_utxos, address=issuer_addr.address, coin=token
)
assert token_utxo and token_utxo[0].amount == token_amount, "The token was not minted"
# check tx_view
tx_view.check_tx_view(cluster_obj=cluster, tx_raw_output=tx_raw_output_step2)
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_raw_output_step1)
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_raw_output_step2)
@allure.link(helpers.get_vcs_link())
@pytest.mark.dbsync
@pytest.mark.testnets
def test_time_range_minting(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: List[clusterlib.AddressRecord],
):
"""Test minting a token with a time constraints Plutus script.
* fund the token issuer and create a UTxO for collateral
* check that the expected amount was transferred to token issuer's address
* mint the token using a Plutus script
* check that the token was minted and collateral UTxO was not spent
* (optional) check transactions in db-sync
"""
# pylint: disable=too-many-locals
temp_template = common.get_test_id(cluster)
payment_addr = payment_addrs[0]
issuer_addr = payment_addrs[1]
lovelace_amount = 2_000_000
token_amount = 5
minting_cost = plutus_common.compute_cost(
execution_cost=plutus_common.MINTING_TIME_RANGE_COST,
protocol_params=cluster.g_query.get_protocol_params(),
)
# Step 1: fund the token issuer
mint_utxos, collateral_utxos, tx_raw_output_step1 = _fund_issuer(
cluster_obj=cluster,
temp_template=temp_template,
payment_addr=payment_addr,
issuer_addr=issuer_addr,
minting_cost=minting_cost,
amount=lovelace_amount,
)
issuer_fund_balance = cluster.g_query.get_address_balance(issuer_addr.address)
# Step 2: mint the "qacoin"
slot_step2 = cluster.g_query.get_slot_no()
slots_offset = 200
timestamp_offset_ms = int(slots_offset * cluster.slot_length + 5) * 1_000
protocol_version = cluster.g_query.get_protocol_params()["protocolVersion"]["major"]
if protocol_version > 5:
# POSIX timestamp + offset
redeemer_value = int(datetime.datetime.now().timestamp() * 1_000) + timestamp_offset_ms
else:
# BUG: https://github.com/input-output-hk/cardano-node/issues/3090
redeemer_value = 1_000_000_000_000
policyid = cluster.g_transaction.get_policyid(plutus_common.MINTING_TIME_RANGE_PLUTUS_V1)
asset_name = f"qacoin{clusterlib.get_rand_str(4)}".encode().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_TIME_RANGE_PLUTUS_V1,
collaterals=collateral_utxos,
execution_units=(
plutus_common.MINTING_TIME_RANGE_COST.per_time,
plutus_common.MINTING_TIME_RANGE_COST.per_space,
),
redeemer_value=str(redeemer_value),
)
]
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,
]
tx_raw_output_step2 = cluster.g_transaction.build_raw_tx_bare(
out_file=f"{temp_template}_step2_tx.body",
txins=mint_utxos,
txouts=txouts_step2,
mint=plutus_mint_data,
tx_files=tx_files_step2,
fee=minting_cost.fee + FEE_MINT_TXSIZE,
invalid_before=slot_step2 - slots_offset,
invalid_hereafter=slot_step2 + slots_offset,
)
tx_signed_step2 = cluster.g_transaction.sign_tx(
tx_body_file=tx_raw_output_step2.out_file,
signing_key_files=tx_files_step2.signing_key_files,
tx_name=f"{temp_template}_step2",
)
cluster.g_transaction.submit_tx(tx_file=tx_signed_step2, txins=mint_utxos)
assert (
cluster.g_query.get_address_balance(issuer_addr.address)
== issuer_fund_balance - tx_raw_output_step2.fee
), f"Incorrect balance for token issuer address `{issuer_addr.address}`"
out_utxos = cluster.g_query.get_utxo(tx_raw_output=tx_raw_output_step2)
token_utxo = clusterlib.filter_utxos(
utxos=out_utxos, address=issuer_addr.address, coin=token
)
assert token_utxo and token_utxo[0].amount == token_amount, "The token was not minted"
# check tx_view
tx_view.check_tx_view(cluster_obj=cluster, tx_raw_output=tx_raw_output_step2)
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_raw_output_step1)
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_raw_output_step2)
@allure.link(helpers.get_vcs_link())
@pytest.mark.dbsync
@pytest.mark.testnets
@pytest.mark.parametrize(
"plutus_version",
(
"plutus_v1",
pytest.param("mix_v2_v1", marks=common.SKIPIF_PLUTUSV2_UNUSABLE),
),
)
def test_two_scripts_minting(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: List[clusterlib.AddressRecord],
plutus_version: str,
):
"""Test minting two tokens with two different Plutus scripts.
* fund the token issuer and create a UTxO for collaterals
* check that the expected amount was transferred to token issuer's address
* mint the tokens using two different Plutus scripts
* check that the tokens were minted and collateral UTxOs were not spent
* check transaction view output
* (optional) check transactions in db-sync
"""
# pylint: disable=too-many-locals,too-many-statements
temp_template = f"{common.get_test_id(cluster)}_{plutus_version}"
payment_addr = payment_addrs[0]
issuer_addr = payment_addrs[1]
lovelace_amount = 2_000_000
token_amount = 5
script_file1_v1 = plutus_common.MINTING_PLUTUS_V1
script_file1_v2 = plutus_common.MINTING_PLUTUS_V2
# this is higher than `plutus_common.MINTING*_COST`, because the script context has changed
# to include more stuff
if configuration.ALONZO_COST_MODEL or VERSIONS.cluster_era == VERSIONS.ALONZO:
minting_cost1_v1 = plutus_common.ExecutionCost(
per_time=408_545_501, per_space=1_126_016, fixed_cost=94_428
)
minting_cost2_v1 = plutus_common.ExecutionCost(
per_time=427_707_230, per_space=1_188_952, fixed_cost=99_441
)
else:
minting_cost1_v1 = plutus_common.ExecutionCost(
per_time=297_744_405, per_space=1_126_016, fixed_cost=86_439
)
minting_cost2_v1 = plutus_common.ExecutionCost(
per_time=312_830_204, per_space=1_188_952, fixed_cost=91_158
)
minting_cost1_v2 = plutus_common.ExecutionCost(
per_time=185_595_199, per_space=595_446, fixed_cost=47_739
)
if plutus_version == "plutus_v1":
script_file1 = script_file1_v1
execution_cost1 = minting_cost1_v1
elif plutus_version == "mix_v2_v1":
script_file1 = script_file1_v2
execution_cost1 = minting_cost1_v2
else:
raise AssertionError("Unknown test variant.")
script_file2 = plutus_common.MINTING_TIME_RANGE_PLUTUS_V1
protocol_params = cluster.g_query.get_protocol_params()
minting_cost1 = plutus_common.compute_cost(
execution_cost=execution_cost1, protocol_params=protocol_params
)
minting_cost2 = plutus_common.compute_cost(
execution_cost=minting_cost2_v1, protocol_params=protocol_params
)
fee_step2_total = minting_cost1.fee + minting_cost2.fee + FEE_MINT_TXSIZE
issuer_init_balance = cluster.g_query.get_address_balance(issuer_addr.address)
# Step 1: fund the token issuer
tx_files_step1 = clusterlib.TxFiles(
signing_key_files=[payment_addr.skey_file],
)
txouts_step1 = [
clusterlib.TxOut(address=issuer_addr.address, amount=lovelace_amount + fee_step2_total),
# for collaterals
clusterlib.TxOut(address=issuer_addr.address, amount=minting_cost1.collateral),
clusterlib.TxOut(address=issuer_addr.address, amount=minting_cost2.collateral),
]
tx_raw_output_step1 = cluster.g_transaction.send_tx(
src_address=payment_addr.address,
tx_name=f"{temp_template}_step1",
txouts=txouts_step1,
tx_files=tx_files_step1,
# TODO: workaround for https://github.com/input-output-hk/cardano-node/issues/1892
witness_count_add=2,
# don't join 'change' and 'collateral' txouts, we need separate UTxOs
join_txouts=False,
)
issuer_step1_balance = cluster.g_query.get_address_balance(issuer_addr.address)
assert (
issuer_step1_balance
== issuer_init_balance
+ lovelace_amount
+ fee_step2_total
+ minting_cost1.collateral
+ minting_cost2.collateral
), f"Incorrect balance for token issuer address `{issuer_addr.address}`"
# Step 2: mint the "qacoins"
txid_step1 = cluster.g_transaction.get_txid(tx_body_file=tx_raw_output_step1.out_file)
mint_utxos = cluster.g_query.get_utxo(txin=f"{txid_step1}#0")
collateral_utxo1 = cluster.g_query.get_utxo(txin=f"{txid_step1}#1")
collateral_utxo2 = cluster.g_query.get_utxo(txin=f"{txid_step1}#2")
slot_step2 = cluster.g_query.get_slot_no()
# "anyone can mint" qacoin
policyid1 = cluster.g_transaction.get_policyid(script_file1)
asset_name1 = f"qacoina{clusterlib.get_rand_str(4)}".encode().hex()
token1 = f"{policyid1}.{asset_name1}"
mint_txouts1 = [
clusterlib.TxOut(address=issuer_addr.address, amount=token_amount, coin=token1)
]
# "timerange" qacoin
slots_offset = 200
timestamp_offset_ms = int(slots_offset * cluster.slot_length + 5) * 1_000
protocol_version = cluster.g_query.get_protocol_params()["protocolVersion"]["major"]
if protocol_version > 5:
# POSIX timestamp + offset
redeemer_value_timerange = (
int(datetime.datetime.now().timestamp() * 1_000) + timestamp_offset_ms
)
else:
# BUG: https://github.com/input-output-hk/cardano-node/issues/3090
redeemer_value_timerange = 1_000_000_000_000
policyid2 = cluster.g_transaction.get_policyid(script_file2)
asset_name2 = f"qacoint{clusterlib.get_rand_str(4)}".encode().hex()
token2 = f"{policyid2}.{asset_name2}"
mint_txouts2 = [
clusterlib.TxOut(address=issuer_addr.address, amount=token_amount, coin=token2)
]
# mint the tokens
plutus_mint_data = [
clusterlib.Mint(
txouts=mint_txouts1,
script_file=script_file1,
collaterals=collateral_utxo1,
execution_units=(
execution_cost1.per_time,
execution_cost1.per_space,
),
redeemer_cbor_file=plutus_common.REDEEMER_42_CBOR,
),
clusterlib.Mint(
txouts=mint_txouts2,
script_file=script_file2,
collaterals=collateral_utxo2,
execution_units=(
minting_cost2_v1.per_time,
minting_cost2_v1.per_space,
),
redeemer_value=str(redeemer_value_timerange),
),
]
tx_files_step2 = clusterlib.TxFiles(
signing_key_files=[issuer_addr.skey_file],
)
txouts_step2 = [
clusterlib.TxOut(address=issuer_addr.address, amount=lovelace_amount),
*mint_txouts1,
*mint_txouts2,
]
tx_raw_output_step2 = cluster.g_transaction.build_raw_tx_bare(
out_file=f"{temp_template}_step2_tx.body",
txins=mint_utxos,
txouts=txouts_step2,
mint=plutus_mint_data,
tx_files=tx_files_step2,
fee=fee_step2_total,
invalid_before=slot_step2 - slots_offset,
invalid_hereafter=slot_step2 + slots_offset,
)
tx_signed_step2 = cluster.g_transaction.sign_tx(
tx_body_file=tx_raw_output_step2.out_file,
signing_key_files=tx_files_step2.signing_key_files,
tx_name=f"{temp_template}_step2",
)
cluster.g_transaction.submit_tx(tx_file=tx_signed_step2, txins=mint_utxos)
assert (
cluster.g_query.get_address_balance(issuer_addr.address)
== issuer_init_balance
+ minting_cost1.collateral
+ minting_cost2.collateral
+ lovelace_amount
), f"Incorrect balance for token issuer address `{issuer_addr.address}`"
out_utxos = cluster.g_query.get_utxo(tx_raw_output=tx_raw_output_step2)
token_utxo1 = clusterlib.filter_utxos(
utxos=out_utxos, address=issuer_addr.address, coin=token1
)
assert (
token_utxo1 and token_utxo1[0].amount == token_amount
), "The 'anyone' token was not minted"
token_utxo2 = clusterlib.filter_utxos(
utxos=out_utxos, address=issuer_addr.address, coin=token2
)
assert (
token_utxo2 and token_utxo2[0].amount == token_amount
), "The 'timerange' token was not minted"
# check tx_view
tx_view.check_tx_view(cluster_obj=cluster, tx_raw_output=tx_raw_output_step2)
# check transactions in db-sync
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_raw_output_step1)
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_raw_output_step2)
@allure.link(helpers.get_vcs_link())
@pytest.mark.dbsync
@pytest.mark.testnets
def test_minting_policy_executed_once1(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: List[clusterlib.AddressRecord],
):
"""Test that minting policy is executed only once even when the same policy is used twice.
Test by minting two tokens while using the same Plutus script twice
with two different redeemers.
The Plutus script used in this test takes the expected token name as
redeemer. Even though the redeemer used for minting the first token
doesn't match the token name, the token gets minted anyway. That's
because only the last redeemer is used and all the other scripts with
identical minting policy (and corresponding redeemers) are ignored. So
it only matters that the last redeemer matches the last token name.
* fund the token issuer and create a UTxO for collateral - funds for fees and collateral
are sufficient for just single minting script
* check that the expected amount was transferred to token issuer's address
* mint the tokens using two identical Plutus scripts and two redeemers, where the first
redeemer value is invalid
* check that the tokens were minted and collateral UTxOs were not spent, i.e. the first
script and its redeemer were ignored
* check transaction view output
* (optional) check transactions in db-sync
"""
# pylint: disable=too-many-locals,too-many-statements
temp_template = common.get_test_id(cluster)
payment_addr = payment_addrs[0]
issuer_addr = payment_addrs[1]
lovelace_amount = 2_000_000
token_amount = 5
minting_cost = plutus_common.compute_cost(
execution_cost=plutus_common.MINTING_TOKENNAME_COST,
protocol_params=cluster.g_query.get_protocol_params(),
)
# Step 1: fund the token issuer
mint_utxos, collateral_utxos, tx_raw_output_step1 = _fund_issuer(
cluster_obj=cluster,
temp_template=temp_template,
payment_addr=payment_addr,
issuer_addr=issuer_addr,
minting_cost=minting_cost,
amount=lovelace_amount,
)
issuer_init_balance = cluster.g_query.get_address_balance(issuer_addr.address)
# Step 2: mint the "qacoins"
policyid_tokenname = cluster.g_transaction.get_policyid(
plutus_common.MINTING_TOKENNAME_PLUTUS_V1
)
# qacoinA
asset_name_a_dec = f"qacoinA{clusterlib.get_rand_str(4)}"
asset_name_a = asset_name_a_dec.encode("utf-8").hex()
token_a = f"{policyid_tokenname}.{asset_name_a}"
mint_txouts_a = [
clusterlib.TxOut(address=issuer_addr.address, amount=token_amount, coin=token_a)
]
# qacoinB
asset_name_b_dec = f"qacoinB{clusterlib.get_rand_str(4)}"
asset_name_b = asset_name_b_dec.encode("utf-8").hex()
token_b = f"{policyid_tokenname}.{asset_name_b}"
mint_txouts_b = [
clusterlib.TxOut(address=issuer_addr.address, amount=token_amount, coin=token_b)
]
# mint the tokens
plutus_mint_data = [
# First redeemer and first script are ignored when there are
# multiple scripts for the same minting policy. Even though we
# specified execution units for the script, these will not be used.
# That's why we were able to use the costs for just single script,
# even when we passed it twice.
clusterlib.Mint(
txouts=mint_txouts_a,
script_file=plutus_common.MINTING_TOKENNAME_PLUTUS_V1,
# execution units are too low, but it doesn't matter as they get ignored anyway
execution_units=(1, 1),
redeemer_value='"ignored_value"',
),
clusterlib.Mint(
txouts=mint_txouts_b,
script_file=plutus_common.MINTING_TOKENNAME_PLUTUS_V1,
collaterals=collateral_utxos,
execution_units=(
plutus_common.MINTING_TOKENNAME_COST.per_time,
plutus_common.MINTING_TOKENNAME_COST.per_space,
),
redeemer_value=f'"{asset_name_b_dec}"',
),
]
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_a,
*mint_txouts_b,
]
tx_raw_output_step2 = cluster.g_transaction.build_raw_tx_bare(
out_file=f"{temp_template}_step2_tx.body",
txins=mint_utxos,
txouts=txouts_step2,
mint=plutus_mint_data,
tx_files=tx_files_step2,
fee=minting_cost.fee + FEE_MINT_TXSIZE,
)
tx_signed_step2 = cluster.g_transaction.sign_tx(
tx_body_file=tx_raw_output_step2.out_file,
signing_key_files=tx_files_step2.signing_key_files,
tx_name=f"{temp_template}_step2",
)
cluster.g_transaction.submit_tx(tx_file=tx_signed_step2, txins=mint_utxos)
assert (
cluster.g_query.get_address_balance(issuer_addr.address)
== issuer_init_balance - tx_raw_output_step2.fee
), f"Incorrect balance for token issuer address `{issuer_addr.address}`"
out_utxos = cluster.g_query.get_utxo(tx_raw_output=tx_raw_output_step2)
token_utxo_a = clusterlib.filter_utxos(
utxos=out_utxos, address=issuer_addr.address, coin=token_a
)
assert (
token_utxo_a and token_utxo_a[0].amount == token_amount
), f"The '{asset_name_a_dec}' token was not minted"
token_utxo_b = clusterlib.filter_utxos(
utxos=out_utxos, address=issuer_addr.address, coin=token_b
)
assert (
token_utxo_b and token_utxo_b[0].amount == token_amount
), f"The '{asset_name_b_dec}' token was not minted"
# check tx_view
tx_view.check_tx_view(cluster_obj=cluster, tx_raw_output=tx_raw_output_step2)
# check transactions in db-sync
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_raw_output_step1)
dbsync_utils.check_tx(cluster_obj=cluster, tx_raw_output=tx_raw_output_step2)
@allure.link(helpers.get_vcs_link())
@pytest.mark.dbsync
@pytest.mark.testnets
def test_minting_policy_executed_once2(
self,
cluster: clusterlib.ClusterLib,
payment_addrs: List[clusterlib.AddressRecord],
):
"""Test that minting policy is executed only once even when the same policy is used twice.
Test minting two tokens while using one Plutus script and one redeemer.
The Plutus script used in this test takes the expected token name as
redeemer. Even though the redeemer doesn't match name of the first
token, the token get's minted anyway. That's because it is only checked
that the last token name matches the redeemer, and redeemer for the
first token is not needed.
* fund the token issuer and create a UTxO for collateral
* check that the expected amount was transferred to token issuer's address
* mint the tokens using a redeemer value that doesn't match the name of the first token
* check that the tokens were minted and collateral UTxOs were not spent, i.e. redeemer for
the first token was not needed
* check transaction view output
* (optional) check transactions in db-sync
"""
# pylint: disable=too-many-locals
temp_template = common.get_test_id(cluster)
payment_addr = payment_addrs[0]
issuer_addr = payment_addrs[1]
lovelace_amount = 2_000_000
token_amount = 5
minting_cost = plutus_common.compute_cost(
execution_cost=plutus_common.MINTING_TOKENNAME_COST,
protocol_params=cluster.g_query.get_protocol_params(),
)
# Step 1: fund the token issuer
mint_utxos, collateral_utxos, tx_raw_output_step1 = _fund_issuer(
cluster_obj=cluster,
temp_template=temp_template,
payment_addr=payment_addr,
issuer_addr=issuer_addr,
minting_cost=minting_cost,
amount=lovelace_amount,
collateral_utxo_num=2,
)
issuer_fund_balance = cluster.g_query.get_address_balance(issuer_addr.address)
# Step 2: mint the "qacoin"
policyid = cluster.g_transaction.get_policyid(plutus_common.MINTING_TOKENNAME_PLUTUS_V1)
# qacoinA
asset_name_a_dec = f"qacoinA{clusterlib.get_rand_str(4)}"
asset_name_a = asset_name_a_dec.encode("utf-8").hex()
token_a = f"{policyid}.{asset_name_a}"
# qacoinB
asset_name_b_dec = f"qacoinB{clusterlib.get_rand_str(4)}"
asset_name_b = asset_name_b_dec.encode("utf-8").hex()
token_b = f"{policyid}.{asset_name_b}"
mint_txouts = [
clusterlib.TxOut(address=issuer_addr.address, amount=token_amount, coin=token_a),
clusterlib.TxOut(address=issuer_addr.address, amount=token_amount, coin=token_b),