Skip to content

Commit 3cf1938

Browse files
authored
Solve file content not found (#812)
* Fix: Solve issue with not rejecting messages that it's content doesn't exist on IPFS. * Fix: Solve failing test.
1 parent 440c96a commit 3cf1938

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/aleph/services/ipfs/service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from aleph.services.ipfs.common import make_ipfs_client
1212
from aleph.services.utils import get_IP
13-
from aleph.types.message_status import FileUnavailable
13+
from aleph.types.message_status import FileContentUnavailable, FileUnavailable
1414
from aleph.utils import run_in_executor
1515

1616
LOGGER = logging.getLogger(__name__)
@@ -113,8 +113,9 @@ async def get_ipfs_size(
113113
await asyncio.sleep(0.5)
114114
continue
115115
except asyncio.TimeoutError:
116-
result = None
117-
await asyncio.sleep(0.5)
116+
raise FileContentUnavailable(
117+
"Could not retrieve IPFS content at this time"
118+
)
118119
except (
119120
concurrent.futures.CancelledError,
120121
aiohttp.client_exceptions.ClientConnectorError,

src/aleph/types/message_status.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ def __init__(self, file_hash: str):
169169
super().__init__(f"File not found: {file_hash}")
170170

171171

172+
class FileContentNotFoundException(RetryMessageException):
173+
"""
174+
A file required to process the message could not be found, locally and/or
175+
on the network.
176+
"""
177+
178+
def __init__(self, file_hash: str):
179+
super().__init__(f"File content not found: {file_hash}")
180+
181+
172182
class MessageContentUnavailable(FileNotFoundException):
173183
"""
174184
The message content is not available at the moment (storage/IPFS item types).
@@ -185,6 +195,14 @@ class FileUnavailable(FileNotFoundException):
185195
error_code = ErrorCode.FILE_UNAVAILABLE
186196

187197

198+
class FileContentUnavailable(FileContentNotFoundException):
199+
"""
200+
A file pointed to by the message is not available at the moment.
201+
"""
202+
203+
error_code = ErrorCode.FILE_UNAVAILABLE
204+
205+
188206
class NoAmendTarget(InvalidMessageException):
189207
"""
190208
A POST with type = amend does not specify a value in the ref field.

tests/services/test_ipfs_service.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from aleph.services.ipfs.service import IpfsService
8+
from aleph.types.message_status import FileContentUnavailable
89

910

1011
@pytest.mark.asyncio
@@ -178,14 +179,15 @@ async def test_get_ipfs_size_timeout_error():
178179

179180
service = IpfsService(ipfs_client=ipfs_client)
180181

181-
# Mock asyncio.sleep to not actually sleep during test
182-
with patch("asyncio.sleep", new_callable=AsyncMock):
183-
# Execute
184-
result = await service.get_ipfs_size("test_hash")
182+
with pytest.raises(FileContentUnavailable):
183+
# Mock asyncio.sleep to not actually sleep during test
184+
with patch("asyncio.sleep", new_callable=AsyncMock):
185+
# Execute
186+
result = await service.get_ipfs_size("test_hash")
185187

186-
# Assert
187-
assert result is None
188-
ipfs_client.dag.get.assert_called_once_with("test_hash")
188+
# Assert
189+
assert result is None
190+
ipfs_client.dag.get.assert_called_once_with("test_hash")
189191

190192

191193
@pytest.mark.asyncio

0 commit comments

Comments
 (0)