Skip to content

Commit 10df993

Browse files
authored
Merge pull request #284 from input-output-hk/pass_src_addr_utxos
feat: avoid redundant UTxO queries
2 parents 6bf95b9 + 15318a9 commit 10df993

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

cardano_clusterlib/transaction_group.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ def build_raw_tx(
426426
treasury_donation: int | None = None,
427427
invalid_hereafter: int | None = None,
428428
invalid_before: int | None = None,
429+
src_addr_utxos: list[structs.UTXOData] | None = None,
429430
join_txouts: bool = True,
430431
destination_dir: itp.FileType = ".",
431432
) -> structs.TxRawOutput:
@@ -466,6 +467,7 @@ def build_raw_tx(
466467
treasury_donation: A donation to the treasury to perform (optional).
467468
invalid_hereafter: A last block when the transaction is still valid (optional).
468469
invalid_before: A first block when the transaction is valid (optional).
470+
src_addr_utxos: A list of UTxOs for the source address (optional).
469471
join_txouts: A bool indicating whether to aggregate transaction outputs
470472
by payment address (True by default).
471473
destination_dir: A path to directory for storing artifacts (optional).
@@ -495,6 +497,7 @@ def build_raw_tx(
495497
script_withdrawals=script_withdrawals,
496498
deposit=deposit,
497499
treasury_donation=treasury_donation,
500+
src_addr_utxos=src_addr_utxos,
498501
skip_asset_balancing=False,
499502
)
500503

@@ -609,6 +612,7 @@ def calculate_tx_fee(
609612
treasury_donation: int | None = None,
610613
invalid_hereafter: int | None = None,
611614
invalid_before: int | None = None,
615+
src_addr_utxos: list[structs.UTXOData] | None = None,
612616
witness_count_add: int = 0,
613617
join_txouts: bool = True,
614618
destination_dir: itp.FileType = ".",
@@ -650,6 +654,7 @@ def calculate_tx_fee(
650654
treasury_donation: A donation to the treasury to perform (optional).
651655
invalid_hereafter: A last block when the transaction is still valid (optional).
652656
invalid_before: A first block when the transaction is valid (optional).
657+
src_addr_utxos: A list of UTxOs for the source address (optional).
653658
witness_count_add: A number of witnesses to add - workaround to make the fee
654659
calculation more precise.
655660
join_txouts: A bool indicating whether to aggregate transaction outputs
@@ -693,6 +698,7 @@ def calculate_tx_fee(
693698
script_votes=script_votes,
694699
invalid_hereafter=invalid_hereafter or ttl,
695700
invalid_before=invalid_before,
701+
src_addr_utxos=src_addr_utxos,
696702
deposit=deposit,
697703
current_treasury_value=current_treasury_value,
698704
treasury_donation=treasury_donation,
@@ -1309,6 +1315,15 @@ def send_tx(
13091315
script_withdrawals=script_withdrawals,
13101316
)
13111317

1318+
# Get UTxOs for src address here so the records can be passed around, and it is
1319+
# not necessary to get them once for fee calculation and again for the final transaction
1320+
# building.
1321+
src_addr_utxos = (
1322+
self._clusterlib_obj.g_query.get_utxo(address=src_address)
1323+
if fee is None and not txins
1324+
else None
1325+
)
1326+
13121327
if fee is None:
13131328
fee = self.calculate_tx_fee(
13141329
src_address=src_address,
@@ -1331,6 +1346,7 @@ def send_tx(
13311346
current_treasury_value=current_treasury_value,
13321347
treasury_donation=treasury_donation,
13331348
invalid_hereafter=invalid_hereafter or ttl,
1349+
src_addr_utxos=src_addr_utxos,
13341350
witness_count_add=witness_count_add,
13351351
join_txouts=join_txouts,
13361352
destination_dir=destination_dir,
@@ -1364,6 +1380,7 @@ def send_tx(
13641380
treasury_donation=treasury_donation,
13651381
invalid_hereafter=invalid_hereafter or ttl,
13661382
invalid_before=invalid_before,
1383+
src_addr_utxos=src_addr_utxos,
13671384
join_txouts=join_txouts,
13681385
destination_dir=destination_dir,
13691386
)

cardano_clusterlib/txtools.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ def _get_tx_ins_outs(
638638
treasury_donation: int | None = None,
639639
withdrawals: structs.OptionalTxOuts = (),
640640
mint_txouts: structs.OptionalTxOuts = (),
641+
src_addr_utxos: list[structs.UTXOData] | None = None,
641642
skip_asset_balancing: bool = False,
642643
) -> tuple[list[structs.UTXOData], list[structs.TxOut]]:
643644
"""Return list of transaction's inputs and outputs.
@@ -653,6 +654,7 @@ def _get_tx_ins_outs(
653654
treasury_donation: A donation to the treasury to perform (optional).
654655
withdrawals: A list (iterable) of `TxOuts`, specifying reward withdrawals (optional).
655656
mint_txouts: A list (iterable) of `TxOuts`, specifying minted tokens (optional).
657+
src_addr_utxos: A list of UTxOs for the source address (optional).
656658
skip_asset_balancing: A bool indicating if assets balancing should be skipped
657659
(`build` command balance the assets automatically in newer versions).
658660
@@ -670,7 +672,7 @@ def _get_tx_ins_outs(
670672
txins_all = list(txins)
671673
if not txins_all:
672674
# No txins were provided, so we'll select them from the source address
673-
address_utxos = clusterlib_obj.g_query.get_utxo(address=src_address)
675+
address_utxos = src_addr_utxos or clusterlib_obj.g_query.get_utxo(address=src_address)
674676
if not address_utxos:
675677
msg = f"No UTxO returned for '{src_address}'."
676678
raise exceptions.CLIError(msg)
@@ -760,6 +762,7 @@ def collect_data_for_build(
760762
script_withdrawals: structs.OptionalScriptWithdrawals = (),
761763
deposit: int | None = None,
762764
treasury_donation: int | None = None,
765+
src_addr_utxos: list[structs.UTXOData] | None = None,
763766
skip_asset_balancing: bool = False,
764767
) -> structs.DataForBuild:
765768
"""Collect data (txins, txouts, withdrawals) needed for building a transaction.
@@ -783,6 +786,7 @@ def collect_data_for_build(
783786
data (optional).
784787
deposit: A deposit amount needed by the transaction (optional).
785788
treasury_donation: A donation to the treasury to perform (optional).
789+
src_addr_utxos: A list of UTxOs for the source address (optional).
786790
skip_asset_balancing: A bool indicating if assets balancing should be skipped
787791
(`build` command balance the assets automatically in newer versions).
788792
@@ -833,6 +837,7 @@ def collect_data_for_build(
833837
treasury_donation=treasury_donation,
834838
withdrawals=withdrawals_txouts,
835839
mint_txouts=mint_txouts,
840+
src_addr_utxos=src_addr_utxos,
836841
skip_asset_balancing=skip_asset_balancing,
837842
)
838843

0 commit comments

Comments
 (0)