-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathclusterlib_utils.py
1210 lines (1019 loc) · 40.4 KB
/
clusterlib_utils.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
"""Utilities that extends the functionality of `cardano-clusterlib`."""
# pylint: disable=abstract-class-instantiated
import contextlib
import itertools
import json
import logging
import math
import time
from pathlib import Path
from typing import Any
from typing import Dict
from typing import List
from typing import NamedTuple
from typing import Optional
from typing import Tuple
from typing import Union
import cbor2
from cardano_clusterlib import clusterlib
from cardano_node_tests.utils import helpers
from cardano_node_tests.utils import locking
from cardano_node_tests.utils import temptools
from cardano_node_tests.utils.types import FileType
LOGGER = logging.getLogger(__name__)
class UpdateProposal(NamedTuple):
arg: str
value: Any
name: str = ""
class TokenRecord(NamedTuple):
token: str
amount: int
issuers_addrs: List[clusterlib.AddressRecord]
token_mint_addr: clusterlib.AddressRecord
script: Path
class TxMetadata(NamedTuple):
metadata: dict
aux_data: list
@helpers.callonce
def _cli_has_query_pool_state() -> bool:
"""Return if `query pool-state` is available."""
return cli_has("query pool-state")
def get_pool_state(
cluster_obj: clusterlib.ClusterLib,
pool_id: str,
) -> clusterlib.PoolParamsTop:
"""Get pool state using the available command."""
return (
cluster_obj.g_query.get_pool_state(pool_id)
if _cli_has_query_pool_state()
else cluster_obj.g_query.get_pool_params(pool_id)
)
def register_stake_address(
cluster_obj: clusterlib.ClusterLib, pool_user: clusterlib.PoolUser, name_template: str
) -> clusterlib.TxRawOutput:
"""Register stake address."""
# files for registering stake address
addr_reg_cert = cluster_obj.g_stake_address.gen_stake_addr_registration_cert(
addr_name=name_template,
stake_vkey_file=pool_user.stake.vkey_file,
)
tx_files = clusterlib.TxFiles(
certificate_files=[addr_reg_cert],
signing_key_files=[pool_user.payment.skey_file, pool_user.stake.skey_file],
)
tx_raw_output = cluster_obj.g_transaction.send_tx(
src_address=pool_user.payment.address,
tx_name=f"{name_template}_reg_stake_addr",
tx_files=tx_files,
)
if not cluster_obj.g_query.get_stake_addr_info(pool_user.stake.address):
raise AssertionError(f"The address {pool_user.stake.address} was not registered.")
return tx_raw_output
def deregister_stake_address(
cluster_obj: clusterlib.ClusterLib, pool_user: clusterlib.PoolUser, name_template: str
) -> Tuple[clusterlib.TxRawOutput, clusterlib.TxRawOutput]:
"""Deregister stake address."""
# files for deregistering stake address
stake_addr_dereg_cert = cluster_obj.g_stake_address.gen_stake_addr_deregistration_cert(
addr_name=f"{name_template}_addr0_dereg", stake_vkey_file=pool_user.stake.vkey_file
)
tx_files_deregister = clusterlib.TxFiles(
certificate_files=[stake_addr_dereg_cert],
signing_key_files=[pool_user.payment.skey_file, pool_user.stake.skey_file],
)
# withdraw rewards to payment address
tx_raw_output_withdrawal = cluster_obj.g_stake_address.withdraw_reward(
stake_addr_record=pool_user.stake,
dst_addr_record=pool_user.payment,
tx_name=name_template,
)
# deregister the stake address
tx_raw_output_dereg = cluster_obj.g_transaction.send_tx(
src_address=pool_user.payment.address,
tx_name=f"{name_template}_dereg_stake_addr",
tx_files=tx_files_deregister,
)
return tx_raw_output_withdrawal, tx_raw_output_dereg
def fund_from_genesis(
*dst_addrs: str,
cluster_obj: clusterlib.ClusterLib,
amount: int = 2_000_000,
tx_name: Optional[str] = None,
destination_dir: FileType = ".",
) -> None:
"""Send `amount` from genesis addr to all `dst_addrs`."""
fund_dst = [
clusterlib.TxOut(address=d, amount=amount)
for d in dst_addrs
if cluster_obj.g_query.get_address_balance(d) < amount
]
if not fund_dst:
return
with locking.FileLockIfXdist(
f"{temptools.get_basetemp()}/{cluster_obj.g_genesis.genesis_utxo_addr}.lock"
):
tx_name = tx_name or helpers.get_timestamped_rand_str()
tx_name = f"{tx_name}_genesis_funding"
fund_tx_files = clusterlib.TxFiles(
signing_key_files=[
*cluster_obj.g_genesis.genesis_keys.delegate_skeys,
cluster_obj.g_genesis.genesis_keys.genesis_utxo_skey,
]
)
cluster_obj.g_transaction.send_funds(
src_address=cluster_obj.g_genesis.genesis_utxo_addr,
destinations=fund_dst,
tx_name=tx_name,
tx_files=fund_tx_files,
destination_dir=destination_dir,
)
def return_funds_to_faucet(
*src_addrs: clusterlib.AddressRecord,
cluster_obj: clusterlib.ClusterLib,
faucet_addr: str,
amount: Union[int, List[int]] = -1,
tx_name: Optional[str] = None,
destination_dir: FileType = ".",
) -> None:
"""Send `amount` from all `src_addrs` to `faucet_addr`.
The amount of "-1" means all available funds.
"""
tx_name = tx_name or helpers.get_timestamped_rand_str()
tx_name = f"{tx_name}_return_funds"
if isinstance(amount, int):
amount = [amount] * len(src_addrs)
with locking.FileLockIfXdist(f"{temptools.get_basetemp()}/{faucet_addr}.lock"):
try:
logging.disable(logging.ERROR)
for addr, amount_rec in zip(src_addrs, amount):
fund_dst = [clusterlib.TxOut(address=faucet_addr, amount=amount_rec)]
fund_tx_files = clusterlib.TxFiles(signing_key_files=[addr.skey_file])
# try to return funds; don't mind if there's not enough funds for fees etc.
with contextlib.suppress(Exception):
cluster_obj.g_transaction.send_funds(
src_address=addr.address,
destinations=fund_dst,
tx_name=tx_name,
tx_files=fund_tx_files,
destination_dir=destination_dir,
)
finally:
logging.disable(logging.NOTSET)
def fund_from_faucet(
*dst_addrs: Union[clusterlib.AddressRecord, clusterlib.PoolUser],
cluster_obj: clusterlib.ClusterLib,
faucet_data: dict,
amount: Union[int, List[int]] = 1000_000_000,
tx_name: Optional[str] = None,
destination_dir: FileType = ".",
force: bool = False,
) -> Optional[clusterlib.TxRawOutput]:
"""Send `amount` from faucet addr to all `dst_addrs`."""
# get payment AddressRecord out of PoolUser
dst_addr_records: List[clusterlib.AddressRecord] = [
(r.payment if hasattr(r, "payment") else r) for r in dst_addrs # type: ignore
]
if isinstance(amount, int):
amount = [amount] * len(dst_addr_records)
fund_dst = [
clusterlib.TxOut(address=d.address, amount=a)
for d, a in zip(dst_addr_records, amount)
if force or cluster_obj.g_query.get_address_balance(d.address) < a
]
if not fund_dst:
return None
src_address = faucet_data["payment"].address
with locking.FileLockIfXdist(f"{temptools.get_basetemp()}/{src_address}.lock"):
tx_name = tx_name or helpers.get_timestamped_rand_str()
tx_name = f"{tx_name}_funding"
fund_tx_files = clusterlib.TxFiles(signing_key_files=[faucet_data["payment"].skey_file])
tx_raw_output = cluster_obj.g_transaction.send_funds(
src_address=src_address,
destinations=fund_dst,
tx_name=tx_name,
tx_files=fund_tx_files,
destination_dir=destination_dir,
)
return tx_raw_output
def create_payment_addr_records(
*names: str,
cluster_obj: clusterlib.ClusterLib,
stake_vkey_file: Optional[FileType] = None,
destination_dir: FileType = ".",
) -> List[clusterlib.AddressRecord]:
"""Create new payment address(es)."""
addrs = [
cluster_obj.g_address.gen_payment_addr_and_keys(
name=name,
stake_vkey_file=stake_vkey_file,
destination_dir=destination_dir,
)
for name in names
]
LOGGER.debug(f"Created {len(addrs)} payment address(es)")
return addrs
def create_stake_addr_records(
*names: str,
cluster_obj: clusterlib.ClusterLib,
destination_dir: FileType = ".",
) -> List[clusterlib.AddressRecord]:
"""Create new stake address(es)."""
addrs = [
cluster_obj.g_stake_address.gen_stake_addr_and_keys(
name=name, destination_dir=destination_dir
)
for name in names
]
LOGGER.debug(f"Created {len(addrs)} stake address(es)")
return addrs
def create_pool_users(
cluster_obj: clusterlib.ClusterLib,
name_template: str,
no_of_addr: int = 1,
) -> List[clusterlib.PoolUser]:
"""Create PoolUsers."""
pool_users = []
for i in range(no_of_addr):
# create key pairs and addresses
stake_addr_rec = create_stake_addr_records(
f"{name_template}_addr{i}", cluster_obj=cluster_obj
)[0]
payment_addr_rec = create_payment_addr_records(
f"{name_template}_addr{i}",
cluster_obj=cluster_obj,
stake_vkey_file=stake_addr_rec.vkey_file,
)[0]
# create pool user struct
pool_user = clusterlib.PoolUser(payment=payment_addr_rec, stake=stake_addr_rec)
pool_users.append(pool_user)
return pool_users
def wait_for_rewards(cluster_obj: clusterlib.ClusterLib) -> None:
"""Wait until 4th epoch, if necessary, for first reward distribution."""
epoch = cluster_obj.g_query.get_epoch()
if epoch >= 4:
return
new_epochs = 4 - epoch
LOGGER.info(f"Waiting {new_epochs} epoch(s) to get first rewards.")
cluster_obj.wait_for_new_epoch(new_epochs, padding_seconds=10)
def load_registered_pool_data(
cluster_obj: clusterlib.ClusterLib, pool_name: str, pool_id: str
) -> clusterlib.PoolData:
"""Load data of existing registered pool."""
if pool_id.startswith("pool"):
pool_id = helpers.decode_bech32(pool_id)
pool_state: dict = get_pool_state(cluster_obj=cluster_obj, pool_id=pool_id).pool_params
metadata = pool_state.get("metadata") or {}
# TODO: extend to handle more relays records
relays_list = pool_state.get("relays") or []
relay = relays_list[0] if relays_list else {}
relay = relay.get("single host address") or {}
pool_data = clusterlib.PoolData(
pool_name=pool_name,
pool_pledge=pool_state["pledge"],
pool_cost=pool_state["cost"],
pool_margin=pool_state["margin"],
pool_metadata_url=metadata.get("url") or "",
pool_metadata_hash=metadata.get("hash") or "",
pool_relay_ipv4=relay.get("IPv4") or "",
pool_relay_port=relay.get("port") or 0,
)
return pool_data
def check_pool_data( # noqa: C901
pool_params: dict, pool_creation_data: clusterlib.PoolData
) -> str:
"""Check that actual pool state corresponds with pool creation data."""
errors_list = []
if pool_params["cost"] != pool_creation_data.pool_cost:
errors_list.append(
"'cost' value is different than expected; "
f"Expected: {pool_creation_data.pool_cost} vs Returned: {pool_params['cost']}"
)
if pool_params["margin"] != pool_creation_data.pool_margin:
errors_list.append(
"'margin' value is different than expected; "
f"Expected: {pool_creation_data.pool_margin} vs Returned: {pool_params['margin']}"
)
if pool_params["pledge"] != pool_creation_data.pool_pledge:
errors_list.append(
"'pledge' value is different than expected; "
f"Expected: {pool_creation_data.pool_pledge} vs Returned: {pool_params['pledge']}"
)
if pool_params["relays"] != (pool_creation_data.pool_relay_dns or []):
errors_list.append(
"'relays' value is different than expected; "
f"Expected: {pool_creation_data.pool_relay_dns} vs "
f"Returned: {pool_params['relays']}"
)
if pool_creation_data.pool_metadata_url and pool_creation_data.pool_metadata_hash:
metadata = pool_params.get("metadata") or {}
metadata_hash = metadata.get("hash")
if metadata_hash != pool_creation_data.pool_metadata_hash:
errors_list.append(
"'metadata hash' value is different than expected; "
f"Expected: {pool_creation_data.pool_metadata_hash} vs "
f"Returned: {metadata_hash}"
)
metadata_url = metadata.get("url")
if metadata_url != pool_creation_data.pool_metadata_url:
errors_list.append(
"'metadata url' value is different than expected; "
f"Expected: {pool_creation_data.pool_metadata_url} vs "
f"Returned: {metadata_url}"
)
elif pool_params["metadata"] is not None:
errors_list.append(
"'metadata' value is different than expected; "
f"Expected: None vs Returned: {pool_params['metadata']}"
)
if errors_list:
for err in errors_list:
LOGGER.error(err)
LOGGER.error(f"Stake Pool Details: \n{pool_params}")
return "\n\n".join(errors_list)
def check_updated_params(update_proposals: List[UpdateProposal], protocol_params: dict) -> None:
"""Compare update proposals with actual protocol parameters."""
failures = []
for u in update_proposals:
if not u.name:
continue
# nested dictionaries - keys are separated with comma (,)
names = u.name.split(",")
nested = protocol_params
for n in names:
nested = nested[n.strip()]
updated_value = nested
if str(updated_value) != str(u.value):
failures.append(f"Param value for {u.name}: {updated_value}.\nExpected: {u.value}")
if failures:
failures_str = "\n".join(failures)
raise AssertionError(f"Cluster update proposal failed!\n{failures_str}")
def update_params(
cluster_obj: clusterlib.ClusterLib,
src_addr_record: clusterlib.AddressRecord,
update_proposals: List[UpdateProposal],
) -> None:
"""Update params using update proposal."""
if not update_proposals:
return
_cli_args = [(u.arg, str(u.value)) for u in update_proposals]
cli_args = list(itertools.chain.from_iterable(_cli_args))
cluster_obj.g_governance.submit_update_proposal(
cli_args=cli_args,
src_address=src_addr_record.address,
src_skey_file=src_addr_record.skey_file,
tx_name=helpers.get_timestamped_rand_str(),
)
LOGGER.info(f"Update Proposal submitted ({cli_args})")
def update_params_build(
cluster_obj: clusterlib.ClusterLib,
src_addr_record: clusterlib.AddressRecord,
update_proposals: List[UpdateProposal],
) -> None:
"""Update params using update proposal.
Uses `cardano-cli transaction build` command for building the transactions.
"""
if not update_proposals:
return
_cli_args = [(u.arg, str(u.value)) for u in update_proposals]
cli_args = list(itertools.chain.from_iterable(_cli_args))
temp_template = helpers.get_timestamped_rand_str()
# assumption is update proposals are submitted near beginning of epoch
epoch = cluster_obj.g_query.get_epoch()
out_file = cluster_obj.g_governance.gen_update_proposal(
cli_args=cli_args,
epoch=epoch,
tx_name=temp_template,
)
tx_files = clusterlib.TxFiles(
proposal_files=[out_file],
signing_key_files=[
*cluster_obj.g_genesis.genesis_keys.delegate_skeys,
Path(src_addr_record.skey_file),
],
)
tx_output = cluster_obj.g_transaction.build_tx(
src_address=src_addr_record.address,
tx_name=f"{temp_template}_submit_proposal",
tx_files=tx_files,
fee_buffer=2000_000,
)
tx_signed = cluster_obj.g_transaction.sign_tx(
tx_body_file=tx_output.out_file,
signing_key_files=tx_files.signing_key_files,
tx_name=f"{temp_template}_submit_proposal",
)
cluster_obj.g_transaction.submit_tx(tx_file=tx_signed, txins=tx_output.txins)
LOGGER.info(f"Update Proposal submitted ({cli_args})")
def mint_or_burn_witness(
cluster_obj: clusterlib.ClusterLib,
new_tokens: List[TokenRecord],
temp_template: str,
invalid_hereafter: Optional[int] = None,
invalid_before: Optional[int] = None,
use_build_cmd: bool = False,
sign_incrementally: bool = False,
) -> clusterlib.TxRawOutput:
"""Mint or burn tokens, depending on the `amount` value. Sign using witnesses.
Positive `amount` value means minting, negative means burning.
"""
_issuers_addrs = [t.issuers_addrs for t in new_tokens]
issuers_addrs = set(itertools.chain.from_iterable(_issuers_addrs))
issuers_skey_files = {p.skey_file for p in issuers_addrs}
token_mint_addr = new_tokens[0].token_mint_addr
signing_key_files = list({*issuers_skey_files, token_mint_addr.skey_file})
# create TX body
mint = [
clusterlib.Mint(
txouts=[
clusterlib.TxOut(address=t.token_mint_addr.address, amount=t.amount, coin=t.token)
],
script_file=t.script,
)
for t in new_tokens
]
mint_txouts = [
clusterlib.TxOut(address=t.token_mint_addr.address, amount=t.amount, coin=t.token)
for t in new_tokens
if t.amount >= 0
]
txouts = []
if mint_txouts:
# meet the minimum required UTxO value
lovelace_amount = 2_000_000 + math.ceil(len(mint_txouts) / 8) * 1_000_000
txouts = [
clusterlib.TxOut(address=new_tokens[0].token_mint_addr.address, amount=lovelace_amount),
*mint_txouts,
]
if use_build_cmd:
tx_raw_output = cluster_obj.g_transaction.build_tx(
src_address=token_mint_addr.address,
tx_name=temp_template,
txouts=txouts,
fee_buffer=2000_000,
mint=mint,
invalid_hereafter=invalid_hereafter,
invalid_before=invalid_before,
witness_override=len(signing_key_files),
)
else:
fee = cluster_obj.g_transaction.calculate_tx_fee(
src_address=token_mint_addr.address,
tx_name=temp_template,
txouts=txouts,
mint=mint,
# TODO: workaround for https://github.com/input-output-hk/cardano-node/issues/1892
witness_count_add=len(signing_key_files),
)
tx_raw_output = cluster_obj.g_transaction.build_raw_tx(
src_address=token_mint_addr.address,
tx_name=temp_template,
txouts=txouts,
mint=mint,
fee=fee,
invalid_hereafter=invalid_hereafter,
invalid_before=invalid_before,
)
# sign incrementally (just to check that it works)
if sign_incrementally and len(signing_key_files) >= 1:
# create witness file for first required key
witness_file = cluster_obj.g_transaction.witness_tx(
tx_body_file=tx_raw_output.out_file,
witness_name=f"{temp_template}_skey0",
signing_key_files=signing_key_files[:1],
)
# sign Tx using witness file
tx_witnessed_file = cluster_obj.g_transaction.assemble_tx(
tx_body_file=tx_raw_output.out_file,
witness_files=[witness_file],
tx_name=f"{temp_template}_sign0",
)
# incrementally sign the already signed Tx with rest of required skeys
for idx, skey in enumerate(signing_key_files[1:], start=1):
tx_witnessed_file = cluster_obj.g_transaction.sign_tx(
tx_file=tx_witnessed_file,
signing_key_files=[skey],
tx_name=f"{temp_template}_sign{idx}",
)
else:
# create witness file for each required key
witness_files = [
cluster_obj.g_transaction.witness_tx(
tx_body_file=tx_raw_output.out_file,
witness_name=f"{temp_template}_skey{idx}",
signing_key_files=[skey],
)
for idx, skey in enumerate(signing_key_files)
]
# sign Tx using witness files
tx_witnessed_file = cluster_obj.g_transaction.assemble_tx(
tx_body_file=tx_raw_output.out_file,
witness_files=witness_files,
tx_name=temp_template,
)
# submit signed TX
cluster_obj.g_transaction.submit_tx(tx_file=tx_witnessed_file, txins=tx_raw_output.txins)
return tx_raw_output
def mint_or_burn_sign(
cluster_obj: clusterlib.ClusterLib,
new_tokens: List[TokenRecord],
temp_template: str,
sign_incrementally: bool = False,
) -> clusterlib.TxRawOutput:
"""Mint or burn tokens, depending on the `amount` value. Sign using skeys.
Positive `amount` value means minting, negative means burning.
"""
_issuers_addrs = [t.issuers_addrs for t in new_tokens]
issuers_addrs = set(itertools.chain.from_iterable(_issuers_addrs))
issuers_skey_files = {p.skey_file for p in issuers_addrs}
token_mint_addr = new_tokens[0].token_mint_addr
signing_key_files = list({*issuers_skey_files, token_mint_addr.skey_file})
# build and sign a transaction
tx_files = clusterlib.TxFiles(signing_key_files=signing_key_files)
mint = [
clusterlib.Mint(
txouts=[
clusterlib.TxOut(address=t.token_mint_addr.address, amount=t.amount, coin=t.token)
],
script_file=t.script,
)
for t in new_tokens
]
mint_txouts = [
clusterlib.TxOut(address=t.token_mint_addr.address, amount=t.amount, coin=t.token)
for t in new_tokens
if t.amount >= 0
]
txouts = []
if mint_txouts:
# meet the minimum required UTxO value
lovelace_amount = 2_000_000 + math.ceil(len(mint_txouts) / 8) * 1_000_000
txouts = [
clusterlib.TxOut(address=new_tokens[0].token_mint_addr.address, amount=lovelace_amount),
*mint_txouts,
]
fee = cluster_obj.g_transaction.calculate_tx_fee(
src_address=token_mint_addr.address,
tx_name=temp_template,
txouts=txouts,
mint=mint,
tx_files=tx_files,
# TODO: workaround for https://github.com/input-output-hk/cardano-node/issues/1892
witness_count_add=len(issuers_skey_files),
)
tx_raw_output = cluster_obj.g_transaction.build_raw_tx(
src_address=token_mint_addr.address,
tx_name=temp_template,
txouts=txouts,
mint=mint,
tx_files=tx_files,
fee=fee,
)
# sign incrementally (just to check that it works)
if sign_incrementally and len(signing_key_files) >= 1:
out_file_signed = cluster_obj.g_transaction.sign_tx(
tx_body_file=tx_raw_output.out_file,
signing_key_files=signing_key_files[:1],
tx_name=f"{temp_template}_sign0",
)
# incrementally sign the already signed Tx with rest of required skeys
for idx, skey in enumerate(signing_key_files[1:], start=1):
out_file_signed = cluster_obj.g_transaction.sign_tx(
tx_file=out_file_signed,
signing_key_files=[skey],
tx_name=f"{temp_template}_sign{idx}",
)
else:
out_file_signed = cluster_obj.g_transaction.sign_tx(
tx_body_file=tx_raw_output.out_file,
signing_key_files=tx_files.signing_key_files,
tx_name=temp_template,
)
# submit signed transaction
cluster_obj.g_transaction.submit_tx(tx_file=out_file_signed, txins=tx_raw_output.txins)
return tx_raw_output
def withdraw_reward_w_build(
cluster_obj: clusterlib.ClusterLib,
stake_addr_record: clusterlib.AddressRecord,
dst_addr_record: clusterlib.AddressRecord,
tx_name: str,
verify: bool = True,
destination_dir: clusterlib.FileType = ".",
) -> clusterlib.TxRawOutput:
"""Withdraw reward to payment address.
Args:
cluster_obj: An instance of `clusterlib.ClusterLib`.
stake_addr_record: An `AddressRecord` tuple for the stake address with reward.
dst_addr_record: An `AddressRecord` tuple for the destination payment address.
tx_name: A name of the transaction.
verify: A bool indicating whether to verify that the reward was transferred correctly.
destination_dir: A path to directory for storing artifacts (optional).
"""
dst_address = dst_addr_record.address
src_init_balance = cluster_obj.g_query.get_address_balance(dst_address)
tx_files_withdrawal = clusterlib.TxFiles(
signing_key_files=[dst_addr_record.skey_file, stake_addr_record.skey_file],
)
tx_raw_withdrawal_output = cluster_obj.g_transaction.build_tx(
src_address=dst_address,
tx_name=f"{tx_name}_reward_withdrawal",
tx_files=tx_files_withdrawal,
withdrawals=[clusterlib.TxOut(address=stake_addr_record.address, amount=-1)],
fee_buffer=2000_000,
witness_override=len(tx_files_withdrawal.signing_key_files),
destination_dir=destination_dir,
)
# sign incrementally (just to check that it works)
tx_signed = cluster_obj.g_transaction.sign_tx(
tx_body_file=tx_raw_withdrawal_output.out_file,
signing_key_files=[dst_addr_record.skey_file],
tx_name=f"{tx_name}_reward_withdrawal_sign0",
)
tx_signed_inc = cluster_obj.g_transaction.sign_tx(
tx_file=tx_signed,
signing_key_files=[stake_addr_record.skey_file],
tx_name=f"{tx_name}_reward_withdrawal_sign1",
)
cluster_obj.g_transaction.submit_tx(tx_file=tx_signed_inc, txins=tx_raw_withdrawal_output.txins)
if not verify:
return tx_raw_withdrawal_output
# check that reward is 0
if (
cluster_obj.g_query.get_stake_addr_info(stake_addr_record.address).reward_account_balance
!= 0
):
raise AssertionError("Not all rewards were transferred.")
# check that rewards were transferred
src_reward_balance = cluster_obj.g_query.get_address_balance(dst_address)
if (
src_reward_balance
!= src_init_balance
- tx_raw_withdrawal_output.fee
+ tx_raw_withdrawal_output.withdrawals[0].amount # type: ignore
):
raise AssertionError(f"Incorrect balance for destination address `{dst_address}`.")
return tx_raw_withdrawal_output
def new_tokens(
*asset_names: str,
cluster_obj: clusterlib.ClusterLib,
temp_template: str,
token_mint_addr: clusterlib.AddressRecord,
issuer_addr: clusterlib.AddressRecord,
amount: int,
) -> List[TokenRecord]:
"""Mint new token, sign using skeys."""
# create simple script
keyhash = cluster_obj.g_address.get_payment_vkey_hash(issuer_addr.vkey_file)
script_content = {"keyHash": keyhash, "type": "sig"}
script = Path(f"{temp_template}.script")
with open(f"{temp_template}.script", "w", encoding="utf-8") as out_json:
json.dump(script_content, out_json)
policyid = cluster_obj.g_transaction.get_policyid(script)
tokens_to_mint = []
for asset_name in asset_names:
token = f"{policyid}.{asset_name}"
if cluster_obj.g_query.get_utxo(address=token_mint_addr.address, coins=[token]):
raise AssertionError("The token already exists.")
tokens_to_mint.append(
TokenRecord(
token=token,
amount=amount,
issuers_addrs=[issuer_addr],
token_mint_addr=token_mint_addr,
script=script,
)
)
# token minting
mint_or_burn_sign(
cluster_obj=cluster_obj,
new_tokens=tokens_to_mint,
temp_template=f"{temp_template}_mint",
)
for token_rec in tokens_to_mint:
token_utxo = cluster_obj.g_query.get_utxo(
address=token_mint_addr.address, coins=[token_rec.token]
)
if not (token_utxo and token_utxo[0].amount == amount):
raise AssertionError("The token was not minted.")
return tokens_to_mint
def filtered_ledger_state(
cluster_obj: clusterlib.ClusterLib,
) -> str:
"""Get filtered output of `query ledger-state`."""
cardano_cli_args = [
"cardano-cli",
"query",
"ledger-state",
*cluster_obj.magic_args,
f"--{cluster_obj.protocol}-mode",
]
cardano_cmd = " ".join(cardano_cli_args)
# record cli coverage
clusterlib.record_cli_coverage(
cli_args=cardano_cli_args, coverage_dict=cluster_obj.cli_coverage
)
# get rid of a huge amount of data we don't have any use for
cmd = (
f"{cardano_cmd} | jq -n --stream -c "
"'fromstream(inputs|select((length == 2 and .[0][1] == \"esLState\")|not))'"
)
return helpers.run_in_bash(cmd).decode("utf-8").strip()
def get_blocks_before(
cluster_obj: clusterlib.ClusterLib,
) -> Dict[str, int]:
"""Get `blocksBefore` section of ledger state with bech32 encoded pool ids."""
cardano_cli_args = [
"cardano-cli",
"query",
"ledger-state",
*cluster_obj.magic_args,
f"--{cluster_obj.protocol}-mode",
]
cardano_cmd = " ".join(cardano_cli_args)
# record cli coverage
clusterlib.record_cli_coverage(
cli_args=cardano_cli_args, coverage_dict=cluster_obj.cli_coverage
)
# get rid of a huge amount of data we don't have any use for
cmd = (
f"{cardano_cmd} | jq -n --stream -c "
"'fromstream(1|truncate_stream(inputs|select(.[0][0] == \"blocksBefore\")))'"
)
out_str = helpers.run_in_bash(cmd).decode("utf-8").strip()
out_json: dict = json.loads(out_str)
return {helpers.encode_bech32(prefix="pool", data=key): val for key, val in out_json.items()}
def get_ledger_state(
cluster_obj: clusterlib.ClusterLib,
) -> dict:
"""Return the current ledger state info."""
f_ledger_state = filtered_ledger_state(cluster_obj)
if not f_ledger_state:
return {}
ledger_state: dict = json.loads(f_ledger_state)
return ledger_state
def save_ledger_state(
cluster_obj: clusterlib.ClusterLib,
state_name: str,
ledger_state: Optional[dict] = None,
destination_dir: FileType = ".",
) -> Path:
"""Save ledger state to file.
Args:
cluster_obj: An instance of `clusterlib.ClusterLib`.
state_name: A name of the ledger state (can be epoch number, etc.).
ledger_state: A dict with ledger state to save (optional).
destination_dir: A path to directory for storing the state JSON file (optional).
Returns:
Path: A path to the generated state JSON file.
"""
json_file = Path(destination_dir) / f"{state_name}_ledger_state.json"
ledger_state = ledger_state or get_ledger_state(cluster_obj)
with open(json_file, "w", encoding="utf-8") as fp_out:
json.dump(ledger_state, fp_out, indent=4)
return json_file
def wait_for_epoch_interval(
cluster_obj: clusterlib.ClusterLib,
start: int,
stop: int,
force_epoch: bool = False,
check_slot: bool = False,
) -> None:
"""Wait for time interval within an epoch.
Args:
cluster_obj: An instance of `clusterlib.ClusterLib`.
start: A start of the interval, in seconds. Negative number for counting from the
end of an epoch.
stop: An end of the interval, in seconds. Negative number for counting from the
end of an epoch.
force_epoch: A bool indicating whether the interval must be in current epoch
(False by default).
check_slot: A bool indicating whether to check if slot number matches time interval
after waiting (False by default).
"""
start_abs = start if start >= 0 else cluster_obj.epoch_length_sec + start
stop_abs = stop if stop >= 0 else cluster_obj.epoch_length_sec + stop
if start_abs > stop_abs:
raise AssertionError(f"The 'start' ({start_abs}) needs to be <= 'stop' ({stop_abs}).")
start_epoch = cluster_obj.g_query.get_epoch()
# wait for new block so we start counting with an up-to-date slot number
cluster_obj.wait_for_new_block()
for __ in range(40):
s_from_epoch_start = cluster_obj.time_from_epoch_start()
# return if we are in the required interval
if start_abs <= s_from_epoch_start <= stop_abs:
break
# if we are already after the required interval, wait for next epoch
if stop_abs < s_from_epoch_start:
if force_epoch:
raise AssertionError(
f"Cannot reach the given interval ({start_abs}s to {stop_abs}s) in this epoch."
)
if cluster_obj.g_query.get_epoch() >= start_epoch + 2:
raise AssertionError(
f"Was unable to reach the given interval ({start_abs}s to {stop_abs}s) "
"in past 3 epochs."
)
cluster_obj.wait_for_new_epoch()
continue
# sleep until `start_abs`
to_sleep = start_abs - s_from_epoch_start
if to_sleep > 0:
# `to_sleep` is float, wait for at least 1 second
time.sleep(to_sleep if to_sleep > 1 else 1)
# we can finish if slot number of last minted block doesn't need
# to match the time interval
if not check_slot:
break
else:
raise AssertionError(f"Failed to wait for given interval from {start_abs}s to {stop_abs}s.")
def load_body_metadata(tx_body_file: Path) -> Any:
"""Load metadata from file containing transaction body."""
with open(tx_body_file, encoding="utf-8") as body_fp:
tx_body_json = json.load(body_fp)
cbor_body = bytes.fromhex(tx_body_json["cborHex"])
loaded_body = cbor2.loads(cbor_body)
metadata = loaded_body[-1]
if not metadata:
return []
return metadata
def load_tx_metadata(tx_body_file: Path) -> TxMetadata:
"""Load transaction metadata from file containing transaction body."""
metadata_section = load_body_metadata(tx_body_file=tx_body_file)
if not metadata_section:
return TxMetadata(metadata={}, aux_data=[])
# the `metadata_section` can be either list or `CBORTag`- check if it is `CBORTag`
try:
metadata_value = metadata_section.value
except AttributeError:
pass