Skip to content

Commit 8e01322

Browse files
committed
refactor(common): improve address funding logic
- Updated `_get_funded_addresses` to handle `amount` parameter more effectively. - Refactored to re-fund addresses when balance drops below `min_amount`.
1 parent 1e86532 commit 8e01322

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

cardano_node_tests/tests/common.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -344,39 +344,44 @@ def _get_funded_addresses(
344344
caching_key: str = "",
345345
amount: int | None = None,
346346
) -> list:
347-
"""Create and fund addresses."""
348-
# Initially fund the addresses with more funds, so the funds don't need to be
349-
# added for each and every test.
350-
init_amount = 250_000_000
351-
# Refund the addresses if the amount is lower than this
347+
"""Create and fund addresses.
348+
349+
If `amount` is provided, fund once and never re-fund.
350+
If `amount` is not provided, re-fund when balance drops below `min_amount`.
351+
"""
352+
fund_amount = amount or 150_000_000
353+
# Re-fund the addresses if the amount is lower than this
352354
min_amount = 50_000_000
353355

354356
if caching_key:
355357
fixture_cache: cluster_management.FixtureCache[list | None]
356358
with cluster_manager.cache_fixture(key=caching_key) as fixture_cache:
357359
if fixture_cache.value is None:
358360
addrs = create_func()
359-
amount = amount or init_amount
360361
fixture_cache.value = addrs
361362
else:
362363
addrs = fixture_cache.value
363-
# If amount was passed, fund the addresses only initially
364+
# If amount is explicitly specified, skip re-funding
364365
if amount:
365366
return addrs
366367

367368
else:
368369
addrs = create_func()
369-
amount = amount or init_amount
370370

371371
# Fund source addresses
372-
fund_addrs = addrs if fund_idx is None else [addrs[i] for i in fund_idx]
372+
selected_addrs = addrs if fund_idx is None else [addrs[i] for i in fund_idx]
373+
# The `selected_addrs` can be both `AddressRecord`s or `PoolUser`s
374+
payment_addrs = ((sa.payment if hasattr(sa, "payment") else sa) for sa in selected_addrs)
375+
fund_addrs: list[clusterlib.AddressRecord] = [
376+
a for a in payment_addrs if cluster_obj.g_query.get_address_balance(a.address) < min_amount
377+
]
373378
if fund_addrs:
374-
amount = amount or min_amount
375379
clusterlib_utils.fund_from_faucet(
376380
*fund_addrs,
377381
cluster_obj=cluster_obj,
378382
all_faucets=cluster_manager.cache.addrs_data,
379-
amount=amount,
383+
amount=fund_amount,
384+
force=True,
380385
)
381386

382387
return addrs

0 commit comments

Comments
 (0)