Skip to content

Commit

Permalink
Merge pull request #674 from valory-xyz/fix/incosisten-hashing
Browse files Browse the repository at this point in the history
Fix incosistent hashing
  • Loading branch information
angrybayblade authored Oct 4, 2023
2 parents eea26fd + d66d8d1 commit 82b1aca
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 47 deletions.
34 changes: 5 additions & 29 deletions aea/helpers/ipfs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
import hashlib
import io
import os
import re
from pathlib import Path
from typing import Any, Dict, Generator, Optional, Sized, Tuple, cast

import base58

from aea.helpers.cid import to_v1
from aea.helpers.io import open_file
from aea.helpers.ipfs.utils import _protobuf_python_implementation


Expand All @@ -43,35 +41,13 @@
from aea.helpers.ipfs.pb.merkledag_pb2 import PBNode # type: ignore


def _dos2unix(file_content: bytes) -> bytes:
"""
Replace occurrences of Windows line terminator CR/LF with only LF.
:param file_content: the content of the file.
:return: the same content but with the line terminator
"""
return re.sub(b"\r\n", b"\n", file_content, flags=re.M)


def _is_text(file_path: str) -> bool:
"""Check if a file can be read as text or not."""
try:
with open_file(file_path, "r") as f:
f.read()
return True
except UnicodeDecodeError:
return False


def _read(file_path: str) -> bytes:
"""Read a file, replacing Windows line endings if it is a text file."""
is_text = _is_text(file_path)
"""Read and verify the file is not empty."""
with open(file_path, "rb") as file:
file_b = file.read()
if is_text:
file_b = _dos2unix(file_b)

return file_b
data = file.read()
if len(data) == 0:
raise ValueError(f"File cannot be empty: {file_path}")
return data


def chunks(data: Sized, size: int) -> Generator:
Expand Down
15 changes: 2 additions & 13 deletions tests/test_helpers/test_ipfs/test_base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2022 Valory AG
# Copyright 2022-2023 Valory AG
# Copyright 2018-2021 Fetch.AI Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,23 +22,12 @@
import tempfile
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch

import pytest
from aea_cli_ipfs.ipfs_utils import IPFSTool # type: ignore

from aea.helpers.cid import to_v1
from aea.helpers.ipfs.base import IPFSHashOnly, _is_text


def test_is_text_negative():
"""Test the helper method 'is_text' negative case."""
# https://gehrcke.de/2015/12/how-to-raise-unicodedecodeerror-in-python-3/
with patch(
"aea.helpers.ipfs.base.open_file",
side_effect=UnicodeDecodeError("foo", b"bytes", 1, 2, "Fake reason"),
):
assert not _is_text("path")
from aea.helpers.ipfs.base import IPFSHashOnly


def test_hash_for_big_file():
Expand Down
4 changes: 2 additions & 2 deletions tests/test_package_manager/test_v0.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_update_fingerprints(self, caplog) -> None:
path=packages_dir, packages=OrderedDict({package_id: package_hash})
)

(temp_package / "__init__.py").write_text("")
(temp_package / "__init__.py").write_text("dummy")

with caplog.at_level(logging.ERROR):
assert pm.verify() == 1
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_verify_method(self, caplog) -> None:
pm.package_path_from_package_id(package_id=EXAMPLE_PACKAGE_ID)
/ INIT_FILE_NAME
)
init_file.write_text("")
init_file.write_text("dummy")

with caplog.at_level(logging.ERROR), mock.patch(
"aea.package_manager.v0.check_fingerprint",
Expand Down
5 changes: 2 additions & 3 deletions tests/test_package_manager/test_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,7 @@ def test_update_fingerprints(self, caplog) -> None:
dev_packages=OrderedDict({package_id: package_hash}),
)

(temp_package / "__init__.py").write_text("")

(temp_package / "__init__.py").write_text("hello")
with caplog.at_level(logging.ERROR):
assert pm.verify() == 1
assert (
Expand Down Expand Up @@ -563,7 +562,7 @@ def test_verify_method(self, caplog) -> None:
pm.package_path_from_package_id(package_id=EXAMPLE_PACKAGE_ID)
/ INIT_FILE_NAME
)
init_file.write_text("")
init_file.write_text("dummy")

with caplog.at_level(logging.ERROR), mock.patch(
"aea.package_manager.v1.check_fingerprint",
Expand Down

0 comments on commit 82b1aca

Please sign in to comment.