Skip to content

Commit

Permalink
Increase grace period for max cost calculations (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucekdudek authored Apr 17, 2024
1 parent 824daab commit 7d6b6d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
14 changes: 9 additions & 5 deletions golem/resources/utils/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ def validate_payment_max_cost(
inf: InfrastructureProps,
duration: timedelta,
amount: Decimal,
max_cost_grace_period: timedelta = timedelta(minutes=5),
time_since_last_debit_note: Optional[timedelta] = None,
amount_since_last_debit_note: Optional[Decimal] = None,
grace_period: timedelta = timedelta(minutes=1),
last_debit_note_grace_period: timedelta = timedelta(minutes=1),
) -> Tuple[Decimal, Optional[Decimal]]:
"""Validate payment data max cost.
Expand All @@ -128,7 +129,7 @@ def validate_payment_max_cost(
Raises: PaymentValidationException
"""
duration_grace = duration + grace_period
duration_grace = duration + max_cost_grace_period
max_cost = eth_decimal(
coeffs.price_storage_gib * Decimal(inf.storage_gib)
+ coeffs.price_mem_gib * Decimal(inf.memory_gib)
Expand All @@ -139,26 +140,29 @@ def validate_payment_max_cost(

if amount > max_cost:
raise PaymentValidationException(
"Total amount due exceeds expected max possible cost " f"{amount} > {max_cost}"
f"Total amount due exceeds expected max possible cost {amount} > {max_cost}"
f"{coeffs=} {inf=} {duration_grace=}"
)

if time_since_last_debit_note is None or amount_since_last_debit_note is None:
return max_cost, None
time_since_last_debit_note_grace = time_since_last_debit_note + last_debit_note_grace_period

max_cost_since_last_debit_note = eth_decimal(
coeffs.price_storage_gib * Decimal(inf.storage_gib)
+ coeffs.price_mem_gib * Decimal(inf.memory_gib)
+ coeffs.price_cpu_sec
* Decimal(inf.cpu_threads)
* Decimal(time_since_last_debit_note.total_seconds())
+ coeffs.price_duration_sec * Decimal(time_since_last_debit_note.total_seconds())
* Decimal(time_since_last_debit_note_grace.total_seconds())
+ coeffs.price_duration_sec * Decimal(time_since_last_debit_note_grace.total_seconds())
+ coeffs.price_initial
)

if amount_since_last_debit_note > max_cost_since_last_debit_note:
raise PaymentValidationException(
"Amount due since last debit note exceeds expected max possible cost "
f"{amount_since_last_debit_note} > {max_cost_since_last_debit_note}"
f"{coeffs} {inf=} {time_since_last_debit_note_grace=}"
)

return max_cost, max_cost_since_last_debit_note
Expand Down
12 changes: 8 additions & 4 deletions tests/unit/resources/test_utils_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def test_validate_payment_max_cost_ok():
inf=given_inf,
duration=given_duration,
amount=given_amount,
grace_period=timedelta(),
max_cost_grace_period=timedelta(),
last_debit_note_grace_period=timedelta(),
)
assert received_max_cost == Decimal(181)
assert received_max_cost_since_last_debit_note is None
Expand All @@ -49,7 +50,8 @@ def test_validate_payment_max_cost_payment_validation_exception():
inf=given_inf,
duration=given_duration,
amount=given_amount,
grace_period=timedelta(),
max_cost_grace_period=timedelta(),
last_debit_note_grace_period=timedelta(),
)


Expand All @@ -72,7 +74,8 @@ def test_validate_payment_max_cost_last_debit_note_ok():
amount=given_amount,
time_since_last_debit_note=given_time_since_last_debit_note,
amount_since_last_debit_note=given_amount_since_last_debit_note,
grace_period=timedelta(),
max_cost_grace_period=timedelta(),
last_debit_note_grace_period=timedelta(),
)
assert received_max_cost == Decimal(181)
assert received_max_cost_since_last_debit_note is not None
Expand All @@ -98,7 +101,8 @@ def test_validate_payment_max_cost_last_debit_note_payment_validation_exception(
amount=given_amount,
time_since_last_debit_note=given_time_since_last_debit_note,
amount_since_last_debit_note=given_amount_since_last_debit_note,
grace_period=timedelta(),
max_cost_grace_period=timedelta(),
last_debit_note_grace_period=timedelta(),
)


Expand Down

0 comments on commit 7d6b6d8

Please sign in to comment.