Skip to content

Commit 2f68e6f

Browse files
committed
fix: allow smaller store files in store handler
1 parent ba8b6ab commit 2f68e6f

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

src/aleph/handlers/content/store.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
from aleph.db.models.account_costs import AccountCostsDb
3232
from aleph.exceptions import AlephStorageException, UnknownHashError
3333
from aleph.handlers.content.content_handler import ContentHandler
34-
from aleph.services.cost import get_total_and_detailed_costs
34+
from aleph.services.cost import calculate_storage_size, get_total_and_detailed_costs
3535
from aleph.storage import StorageService
36+
from aleph.toolkit.constants import MAX_UNAUTHENTICATED_UPLOAD_FILE_SIZE
3637
from aleph.toolkit.costs import are_store_and_program_free
3738
from aleph.toolkit.timestamp import timestamp_to_datetime, utc_now
3839
from aleph.types.db_session import DbSession
@@ -217,6 +218,11 @@ async def check_balance(
217218
if are_store_and_program_free(message):
218219
return costs
219220

221+
storage_size = calculate_storage_size(session, content)
222+
223+
if storage_size and storage_size <= MAX_UNAUTHENTICATED_UPLOAD_FILE_SIZE:
224+
return costs
225+
220226
current_balance = get_total_balance(address=content.address, session=session)
221227
current_cost = get_total_cost_for_address(
222228
session=session, address=content.address

src/aleph/services/cost.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,10 @@ def _calculate_storage_costs(
535535
) -> List[AccountCostsDb]:
536536
payment_type = get_payment_type(content)
537537

538-
if isinstance(content, CostEstimationStoreContent) and content.estimated_size_mib:
539-
storage_mib = Decimal(content.estimated_size_mib)
540-
else:
541-
file = get_file(session, content.item_hash)
542-
if not file:
543-
return []
544-
storage_mib = Decimal(file.size / MiB)
538+
storage_mib = calculate_storage_size(session, content)
539+
540+
if not storage_mib:
541+
return []
545542

546543
volume = SizedVolume(CostType.STORAGE, storage_mib, item_hash)
547544

@@ -559,6 +556,22 @@ def _calculate_storage_costs(
559556
)
560557

561558

559+
def calculate_storage_size(
560+
session: DbSession,
561+
content: CostEstimationStoreContent | StoreContent,
562+
) -> Optional[Decimal]:
563+
564+
if isinstance(content, CostEstimationStoreContent) and content.estimated_size_mib:
565+
storage_mib = Decimal(content.estimated_size_mib)
566+
else:
567+
file = get_file(session, content.item_hash)
568+
if not file:
569+
return None
570+
storage_mib = Decimal(file.size / MiB)
571+
572+
return storage_mib
573+
574+
562575
def get_detailed_costs(
563576
session: DbSession,
564577
content: CostComputableContent,

src/aleph/toolkit/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,7 @@
276276

277277
STORE_AND_PROGRAM_COST_DEADLINE_HEIGHT = 22066823
278278
STORE_AND_PROGRAM_COST_DEADLINE_TIMESTAMP = 1742173859
279+
280+
MAX_FILE_SIZE = 100 * MiB
281+
MAX_UNAUTHENTICATED_UPLOAD_FILE_SIZE = 25 * MiB
282+
# MAX_UPLOAD_FILE_SIZE = 1000 * MiB (not used?)

src/aleph/web/controllers/storage.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from aiohttp import BodyPartReader, web
1212
from aiohttp.web_request import FileField
1313
from aleph_message.models import ItemType
14-
from mypy.dmypy_server import MiB
1514
from pydantic import ValidationError
1615

1716
from aleph.chains.signature_verifier import SignatureVerifier
@@ -27,6 +26,7 @@
2726
)
2827
from aleph.services.cost import get_total_and_detailed_costs
2928
from aleph.storage import StorageService
29+
from aleph.toolkit.constants import MAX_FILE_SIZE, MAX_UNAUTHENTICATED_UPLOAD_FILE_SIZE
3030
from aleph.types.db_session import DbSession
3131
from aleph.types.message_status import InvalidSignature
3232
from aleph.utils import item_type_from_hash, run_in_executor
@@ -46,10 +46,6 @@
4646

4747
logger = logging.getLogger(__name__)
4848

49-
MAX_FILE_SIZE = 100 * MiB
50-
MAX_UNAUTHENTICATED_UPLOAD_FILE_SIZE = 25 * MiB
51-
MAX_UPLOAD_FILE_SIZE = 1000 * MiB
52-
5349

5450
async def add_ipfs_json_controller(request: web.Request):
5551
"""Forward the json content to IPFS server and return an hash"""
@@ -109,7 +105,10 @@ async def _verify_message_signature(
109105
async def _verify_user_balance(
110106
session: DbSession, content: CostEstimationStoreContent
111107
) -> None:
112-
if content.estimated_size_mib and content.estimated_size_mib > 25 * MiB:
108+
if (
109+
content.estimated_size_mib
110+
and content.estimated_size_mib > MAX_UNAUTHENTICATED_UPLOAD_FILE_SIZE
111+
):
113112
current_balance = get_total_balance(session=session, address=content.address)
114113
current_cost = get_total_cost_for_address(
115114
session=session, address=content.address

0 commit comments

Comments
 (0)