Skip to content

feat: avoid redundant UTxO queries #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cardano_clusterlib/transaction_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def build_raw_tx(
treasury_donation: int | None = None,
invalid_hereafter: int | None = None,
invalid_before: int | None = None,
src_addr_utxos: list[structs.UTXOData] | None = None,
join_txouts: bool = True,
destination_dir: itp.FileType = ".",
) -> structs.TxRawOutput:
Expand Down Expand Up @@ -466,6 +467,7 @@ def build_raw_tx(
treasury_donation: A donation to the treasury to perform (optional).
invalid_hereafter: A last block when the transaction is still valid (optional).
invalid_before: A first block when the transaction is valid (optional).
src_addr_utxos: A list of UTxOs for the source address (optional).
join_txouts: A bool indicating whether to aggregate transaction outputs
by payment address (True by default).
destination_dir: A path to directory for storing artifacts (optional).
Expand Down Expand Up @@ -495,6 +497,7 @@ def build_raw_tx(
script_withdrawals=script_withdrawals,
deposit=deposit,
treasury_donation=treasury_donation,
src_addr_utxos=src_addr_utxos,
skip_asset_balancing=False,
)

Expand Down Expand Up @@ -609,6 +612,7 @@ def calculate_tx_fee(
treasury_donation: int | None = None,
invalid_hereafter: int | None = None,
invalid_before: int | None = None,
src_addr_utxos: list[structs.UTXOData] | None = None,
witness_count_add: int = 0,
join_txouts: bool = True,
destination_dir: itp.FileType = ".",
Expand Down Expand Up @@ -650,6 +654,7 @@ def calculate_tx_fee(
treasury_donation: A donation to the treasury to perform (optional).
invalid_hereafter: A last block when the transaction is still valid (optional).
invalid_before: A first block when the transaction is valid (optional).
src_addr_utxos: A list of UTxOs for the source address (optional).
witness_count_add: A number of witnesses to add - workaround to make the fee
calculation more precise.
join_txouts: A bool indicating whether to aggregate transaction outputs
Expand Down Expand Up @@ -693,6 +698,7 @@ def calculate_tx_fee(
script_votes=script_votes,
invalid_hereafter=invalid_hereafter or ttl,
invalid_before=invalid_before,
src_addr_utxos=src_addr_utxos,
deposit=deposit,
current_treasury_value=current_treasury_value,
treasury_donation=treasury_donation,
Expand Down Expand Up @@ -1309,6 +1315,15 @@ def send_tx(
script_withdrawals=script_withdrawals,
)

# Get UTxOs for src address here so the records can be passed around, and it is
# not necessary to get them once for fee calculation and again for the final transaction
# building.
src_addr_utxos = (
self._clusterlib_obj.g_query.get_utxo(address=src_address)
if fee is None and not txins
else None
)

if fee is None:
fee = self.calculate_tx_fee(
src_address=src_address,
Expand All @@ -1331,6 +1346,7 @@ def send_tx(
current_treasury_value=current_treasury_value,
treasury_donation=treasury_donation,
invalid_hereafter=invalid_hereafter or ttl,
src_addr_utxos=src_addr_utxos,
witness_count_add=witness_count_add,
join_txouts=join_txouts,
destination_dir=destination_dir,
Expand Down Expand Up @@ -1364,6 +1380,7 @@ def send_tx(
treasury_donation=treasury_donation,
invalid_hereafter=invalid_hereafter or ttl,
invalid_before=invalid_before,
src_addr_utxos=src_addr_utxos,
join_txouts=join_txouts,
destination_dir=destination_dir,
)
Expand Down
7 changes: 6 additions & 1 deletion cardano_clusterlib/txtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ def _get_tx_ins_outs(
treasury_donation: int | None = None,
withdrawals: structs.OptionalTxOuts = (),
mint_txouts: structs.OptionalTxOuts = (),
src_addr_utxos: list[structs.UTXOData] | None = None,
skip_asset_balancing: bool = False,
) -> tuple[list[structs.UTXOData], list[structs.TxOut]]:
"""Return list of transaction's inputs and outputs.
Expand All @@ -653,6 +654,7 @@ def _get_tx_ins_outs(
treasury_donation: A donation to the treasury to perform (optional).
withdrawals: A list (iterable) of `TxOuts`, specifying reward withdrawals (optional).
mint_txouts: A list (iterable) of `TxOuts`, specifying minted tokens (optional).
src_addr_utxos: A list of UTxOs for the source address (optional).
skip_asset_balancing: A bool indicating if assets balancing should be skipped
(`build` command balance the assets automatically in newer versions).

Expand All @@ -670,7 +672,7 @@ def _get_tx_ins_outs(
txins_all = list(txins)
if not txins_all:
# No txins were provided, so we'll select them from the source address
address_utxos = clusterlib_obj.g_query.get_utxo(address=src_address)
address_utxos = src_addr_utxos or clusterlib_obj.g_query.get_utxo(address=src_address)
if not address_utxos:
msg = f"No UTxO returned for '{src_address}'."
raise exceptions.CLIError(msg)
Expand Down Expand Up @@ -760,6 +762,7 @@ def collect_data_for_build(
script_withdrawals: structs.OptionalScriptWithdrawals = (),
deposit: int | None = None,
treasury_donation: int | None = None,
src_addr_utxos: list[structs.UTXOData] | None = None,
skip_asset_balancing: bool = False,
) -> structs.DataForBuild:
"""Collect data (txins, txouts, withdrawals) needed for building a transaction.
Expand All @@ -783,6 +786,7 @@ def collect_data_for_build(
data (optional).
deposit: A deposit amount needed by the transaction (optional).
treasury_donation: A donation to the treasury to perform (optional).
src_addr_utxos: A list of UTxOs for the source address (optional).
skip_asset_balancing: A bool indicating if assets balancing should be skipped
(`build` command balance the assets automatically in newer versions).

Expand Down Expand Up @@ -833,6 +837,7 @@ def collect_data_for_build(
treasury_donation=treasury_donation,
withdrawals=withdrawals_txouts,
mint_txouts=mint_txouts,
src_addr_utxos=src_addr_utxos,
skip_asset_balancing=skip_asset_balancing,
)

Expand Down
Loading