Skip to content

Commit cf60ba7

Browse files
authored
Solve format issue on balance pre-check (#855)
* Fix: Solve issue on format getting IPFS file sizes before downloading the entire content. * Fix: Added test cae for string dag_node format
1 parent bbacd76 commit cf60ba7

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/aleph/services/ipfs/service.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ async def get_ipfs_size(
6868
dag_node = await asyncio.wait_for(
6969
self.ipfs_client.dag.get(hash), timeout=timeout
7070
)
71+
result = 0
72+
if isinstance(dag_node, str):
73+
dag_node = json.loads(dag_node)
7174
if isinstance(dag_node, dict):
7275
if "Data" in dag_node and isinstance(dag_node["Data"], dict):
7376
# This is the common structure for UnixFS nodes after aioipfs parsing
@@ -81,7 +84,11 @@ async def get_ipfs_size(
8184
"Tsize" in dag_node
8285
): # Sometimes it might be at the top level directly
8386
result = dag_node["Tsize"]
84-
elif "Links" in dag_node and isinstance(dag_node["Links"], list):
87+
if (
88+
result == 0
89+
and "Links" in dag_node
90+
and isinstance(dag_node["Links"], list)
91+
):
8592
total_size = 0
8693
for link in dag_node["Links"]:
8794
# In case it's a link list, get the Tsize property if exists

tests/services/test_ipfs_service.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import json
23
from unittest.mock import AsyncMock, patch
34

45
import aioipfs
@@ -133,6 +134,31 @@ async def test_get_ipfs_size_non_dict():
133134
ipfs_client.block.stat.assert_called_once_with("test_hash")
134135

135136

137+
@pytest.mark.asyncio
138+
async def test_get_ipfs_size_json():
139+
"""Test when dag.get returns a non-dictionary (bytes)"""
140+
# Setup
141+
mock_dag_get_data = {
142+
"Data": {"/": {"bytes": "CAE"}},
143+
"Links": [
144+
{"Hash": {"/": "hash1"}, "Name": "test1", "Tsize": 12596071},
145+
{"Hash": {"/": "hash2"}, "Name": "test2", "Tsize": 20168090},
146+
],
147+
}
148+
ipfs_client = AsyncMock()
149+
ipfs_client.dag.get = AsyncMock(return_value=json.dumps(mock_dag_get_data))
150+
151+
service = IpfsService(ipfs_client=ipfs_client)
152+
153+
# Execute
154+
result = await service.get_ipfs_size("test_hash")
155+
156+
# Assert
157+
assert result == 32764161
158+
ipfs_client.dag.get.assert_called_once_with("test_hash")
159+
ipfs_client.block.stat.assert_not_called()
160+
161+
136162
@pytest.mark.asyncio
137163
async def test_get_ipfs_size_api_error():
138164
"""Test handling of APIError"""

0 commit comments

Comments
 (0)