Skip to content

Commit 51195a1

Browse files
committed
feat(common): add min_amount parameter for funding logic
Introduce a `min_amount` parameter to enhance funding logic in utility functions. This allows more flexibility in determining when to re-fund addresses or pool users based on balance thresholds. - Updated `_get_funded_addresses` to handle `min_amount` logic. - Modified related functions to accept and pass `min_amount`. - Adjusted default behavior for funding when `amount` is not provided. This change improves the usability and configurability of funding operations.
1 parent 1a73013 commit 51195a1

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

cardano_node_tests/tests/common.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,20 +345,30 @@ def _get_funded_addresses(
345345
fund_idx: list[int] | None = None,
346346
caching_key: str = "",
347347
amount: int | None = None,
348+
min_amount: int | None = None,
348349
) -> list:
349350
"""Create and fund addresses.
350351
351-
If `amount` is provided, fund once and never re-fund.
352-
If `amount` is not provided, re-fund when balance drops below `min_amount`.
352+
If `amount` and no `min_amount` is provided, fund once and never re-fund.
353+
If `amount` is not provided, re-fund 3 * `min_amount` when balance drops below `min_amount`.
354+
If both `amount` and `min_amount` are provided, re-fund `amount` when balance
355+
drops below `min_amount`.
353356
"""
354-
if amount is None:
355-
fund_amount = 150_000_000
356-
# Re-fund the addresses if the amount is lower than this
357-
min_amount = 50_000_000
358-
else:
357+
no_refund = amount is not None and min_amount is None
358+
# Set a default minimum amount if none is provided
359+
drop_amount = min_amount or 50_000_000
360+
361+
if no_refund:
362+
assert amount # For mypy
359363
# Use the exact specified amount
360364
fund_amount = amount
361-
min_amount = amount
365+
drop_amount = amount
366+
elif amount is not None:
367+
# Amount given: use it
368+
fund_amount = amount
369+
else:
370+
# No amount given: fund triple the minimum
371+
fund_amount = drop_amount * 3
362372

363373
if caching_key:
364374
fixture_cache: cluster_management.FixtureCache[list | None]
@@ -369,7 +379,7 @@ def _get_funded_addresses(
369379
else:
370380
addrs = fixture_cache.value
371381
# If amount is explicitly specified, skip re-funding
372-
if amount:
382+
if no_refund:
373383
return addrs
374384

375385
else:
@@ -380,7 +390,7 @@ def _get_funded_addresses(
380390
# The `selected_addrs` can be both `AddressRecord`s or `PoolUser`s
381391
payment_addrs = ((sa.payment if hasattr(sa, "payment") else sa) for sa in selected_addrs)
382392
fund_addrs: list[clusterlib.AddressRecord] = [
383-
a for a in payment_addrs if cluster_obj.g_query.get_address_balance(a.address) < min_amount
393+
a for a in payment_addrs if cluster_obj.g_query.get_address_balance(a.address) < drop_amount
384394
]
385395
if fund_addrs:
386396
clusterlib_utils.fund_from_faucet(
@@ -403,6 +413,7 @@ def get_payment_addrs(
403413
fund_idx: list[int] | None = None,
404414
caching_key: str = "",
405415
amount: int | None = None,
416+
min_amount: int | None = None,
406417
) -> list[clusterlib.AddressRecord]:
407418
"""Create new payment addresses."""
408419
if num < 1:
@@ -424,6 +435,7 @@ def _create_addrs() -> list[clusterlib.AddressRecord]:
424435
fund_idx=fund_idx,
425436
caching_key=caching_key,
426437
amount=amount,
438+
min_amount=min_amount,
427439
)
428440

429441

@@ -433,6 +445,7 @@ def get_payment_addr(
433445
cluster_obj: clusterlib.ClusterLib,
434446
caching_key: str = "",
435447
amount: int | None = None,
448+
min_amount: int | None = None,
436449
) -> clusterlib.AddressRecord:
437450
"""Create a single new payment address."""
438451
return get_payment_addrs(
@@ -442,6 +455,7 @@ def get_payment_addr(
442455
num=1,
443456
caching_key=caching_key,
444457
amount=amount,
458+
min_amount=min_amount,
445459
)[0]
446460

447461

@@ -453,6 +467,7 @@ def get_pool_users(
453467
fund_idx: list[int] | None = None,
454468
caching_key: str = "",
455469
amount: int | None = None,
470+
min_amount: int | None = None,
456471
) -> list[clusterlib.PoolUser]:
457472
"""Create new pool users."""
458473
if num < 1:
@@ -475,6 +490,7 @@ def _create_pool_users() -> list[clusterlib.PoolUser]:
475490
fund_idx=fund_idx,
476491
caching_key=caching_key,
477492
amount=amount,
493+
min_amount=min_amount,
478494
)
479495

480496

@@ -484,6 +500,7 @@ def get_pool_user(
484500
cluster_obj: clusterlib.ClusterLib,
485501
caching_key: str = "",
486502
amount: int | None = None,
503+
min_amount: int | None = None,
487504
) -> clusterlib.PoolUser:
488505
"""Create a single new pool user."""
489506
return get_pool_users(
@@ -493,6 +510,7 @@ def get_pool_user(
493510
num=1,
494511
caching_key=caching_key,
495512
amount=amount,
513+
min_amount=min_amount,
496514
)[0]
497515

498516

@@ -502,6 +520,7 @@ def get_registered_pool_user(
502520
cluster_obj: clusterlib.ClusterLib,
503521
caching_key: str = "",
504522
amount: int | None = None,
523+
min_amount: int | None = None,
505524
) -> clusterlib.PoolUser:
506525
"""Create new registered pool users."""
507526
pool_user = get_pool_user(
@@ -510,6 +529,7 @@ def get_registered_pool_user(
510529
cluster_obj=cluster_obj,
511530
caching_key=caching_key,
512531
amount=amount,
532+
min_amount=min_amount,
513533
)
514534

515535
if not cluster_obj.g_query.get_stake_addr_info(pool_user.stake.address):

0 commit comments

Comments
 (0)