Skip to content

Commit dc8a47e

Browse files
1yamhoh
authored andcommitted
Fix: storage price calculations on small file
1 parent db8a007 commit dc8a47e

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/aleph/services/cost.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ def _get_volumes_costs(
247247
for volume in volumes:
248248
if isinstance(volume, SizedVolume):
249249
storage_mib = Decimal(volume.size_mib)
250-
251250
elif isinstance(volume, RefVolume):
252251
file = _get_file_from_ref(
253252
session=session, ref=volume.ref, use_latest=volume.use_latest
@@ -294,7 +293,7 @@ def _get_execution_volumes_costs(
294293
volumes.append(
295294
SizedVolume(
296295
CostType.EXECUTION_INSTANCE_VOLUME_ROOTFS,
297-
content.rootfs.size_mib,
296+
Decimal(content.rootfs.size_mib),
298297
content.rootfs.parent.ref,
299298
)
300299
)
@@ -339,7 +338,7 @@ def _get_execution_volumes_costs(
339338
volumes.append(
340339
SizedVolume(
341340
CostType.EXECUTION_VOLUME_INMUTABLE,
342-
volume.estimated_size_mib,
341+
Decimal(volume.estimated_size_mib),
343342
volume.ref,
344343
name,
345344
),
@@ -361,7 +360,7 @@ def _get_execution_volumes_costs(
361360
volumes.append(
362361
SizedVolume(
363362
CostType.EXECUTION_VOLUME_PERSISTENT,
364-
volume.size_mib,
363+
Decimal(volume.size_mib),
365364
None,
366365
name,
367366
),
@@ -498,12 +497,12 @@ def _calculate_storage_costs(
498497
payment_type = get_payment_type(content)
499498

500499
if isinstance(content, CostEstimationStoreContent) and content.estimated_size_mib:
501-
storage_mib = content.estimated_size_mib
500+
storage_mib = Decimal(content.estimated_size_mib)
502501
else:
503502
file = get_file(session, content.item_hash)
504503
if not file:
505504
return []
506-
storage_mib = int(file.size / MiB)
505+
storage_mib = Decimal(file.size / MiB)
507506

508507
volume = SizedVolume(CostType.STORAGE, storage_mib, item_hash)
509508

src/aleph/types/cost.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class SizedVolume(VolumeCost):
169169
def __init__(
170170
self,
171171
cost_type: CostType,
172-
size_mib: int,
172+
size_mib: Decimal,
173173
ref: Optional[str] = None,
174174
*args,
175175
):

tests/message_processing/test_process_stores.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime as dt
22
import json
3+
from decimal import Decimal
34
from typing import Mapping, Optional
45

56
import pytest
@@ -12,6 +13,7 @@
1213
from aleph.handlers.content.store import StoreMessageHandler
1314
from aleph.handlers.message_handler import MessageHandler
1415
from aleph.jobs.process_pending_messages import PendingMessageProcessor
16+
from aleph.services.cost import get_total_and_detailed_costs_from_db
1517
from aleph.services.storage.engine import StorageEngine
1618
from aleph.storage import StorageService
1719
from aleph.toolkit.timestamp import timestamp_to_datetime
@@ -90,6 +92,14 @@ async def test_process_store(
9092
)
9193
session.commit()
9294

95+
cost, _ = get_total_and_detailed_costs_from_db(
96+
session=session,
97+
content=fixture_store_message.content,
98+
item_hash=fixture_store_message.item_hash,
99+
)
100+
101+
assert cost == Decimal("0.000004450480138778")
102+
93103

94104
@pytest.mark.asyncio
95105
async def test_process_store_no_signature(

0 commit comments

Comments
 (0)