From da5eacad6ccd793d4a743dbc6118417ea89e51c3 Mon Sep 17 00:00:00 2001 From: SAIKAT KARMAKAR Date: Tue, 26 Dec 2023 01:10:36 +0000 Subject: [PATCH] Update and modify files for the project - Updated files: - src/bee_py/bee.py - src/bee_py/feed/feed.py - src/bee_py/modules/chunk.py - src/bee_py/modules/feed.py - src/bee_py/utils/eth.py - src/bee_py/utils/http.py - src/bee_py/utils/reference.py - tests/integration/test_bee_integration.py --- src/bee_py/bee.py | 3 +- src/bee_py/feed/feed.py | 15 +++--- src/bee_py/modules/chunk.py | 1 + src/bee_py/modules/feed.py | 2 +- src/bee_py/utils/eth.py | 9 +--- src/bee_py/utils/http.py | 1 - src/bee_py/utils/reference.py | 4 +- tests/integration/test_bee_integration.py | 62 ++++++++++++++++++++++- 8 files changed, 77 insertions(+), 20 deletions(-) diff --git a/src/bee_py/bee.py b/src/bee_py/bee.py index de12616..5ae45b9 100644 --- a/src/bee_py/bee.py +++ b/src/bee_py/bee.py @@ -1218,7 +1218,7 @@ def create_feed_manifest( {"type": feed_type}, # TODO: see this value by printing debug information on bee-js ) - return add_cid_conversion_function(UploadResult(reference=reference), ReferenceType.Feed) + return add_cid_conversion_function(UploadResult(reference=reference), ReferenceType.FEED) def make_feed_reader( self, @@ -1257,7 +1257,6 @@ def make_feed_reader( feed_type, canonical_topic, canonical_signer, - canonical_signer, ) def make_feed_writer( diff --git a/src/bee_py/feed/feed.py b/src/bee_py/feed/feed.py index a50a782..7396bce 100644 --- a/src/bee_py/feed/feed.py +++ b/src/bee_py/feed/feed.py @@ -1,5 +1,6 @@ from datetime import datetime, timezone -from functools import partial + +# from functools import partial from typing import Optional, Union import requests @@ -14,11 +15,11 @@ from bee_py.modules.bytes import read_big_endian, write_big_endian from bee_py.modules.chunk import download from bee_py.modules.feed import fetch_latest_feed_update +from bee_py.types.type import FeedReader # Reference,; FeedType, from bee_py.types.type import ( FEED_INDEX_HEX_LENGTH, BatchId, BeeRequestOptions, - FeedReader, # Reference,; FeedType, FeedUpdate, FeedUpdateOptions, FeedWriter, @@ -30,7 +31,7 @@ from bee_py.utils.bytes import bytes_at_offset, make_bytes from bee_py.utils.eth import make_hex_eth_address from bee_py.utils.hash import keccak256_hash -from bee_py.utils.hex import bytes_to_hex, make_hex_string +from bee_py.utils.hex import bytes_to_hex, hex_to_bytes, make_hex_string from bee_py.utils.reference import make_bytes_reference TIMESTAMP_PAYLOAD_OFFSET = 0 @@ -122,6 +123,8 @@ def get_feed_update_chunk_reference(owner: AddressType, topic: Topic, index: Ind :rtype: PlainBytesReference """ identifier = make_feed_identifier(topic, index) + if not isinstance(owner, bytes): + owner = hex_to_bytes(owner) return keccak256_hash(identifier, owner) @@ -187,7 +190,7 @@ def __download( options = {} return fetch_latest_feed_update(request_options, owner, topic, {**options, "type": _type}) - update = download_feed_update(request_options, bytes.fromhex(owner[2:]), topic, options.index) + update = download_feed_update(request_options, owner, topic, options.index) return FetchFeedUpdateResponse( reference=bytes_to_hex(update.reference), @@ -195,7 +198,7 @@ def __download( feed_index_next="", ) - download_partial = partial(__download) + # download_partial = partial(__download) return FeedReader( request_options=request_options, @@ -203,7 +206,7 @@ def __download( owner=owner, topic=topic, options=options, - download=download_partial, + download=__download, ) diff --git a/src/bee_py/modules/chunk.py b/src/bee_py/modules/chunk.py index 31ca29e..e2f38de 100644 --- a/src/bee_py/modules/chunk.py +++ b/src/bee_py/modules/chunk.py @@ -48,6 +48,7 @@ def download(request_options: BeeRequestOptions, _hash: ReferenceOrENS) -> Data: "url": f"/{ENDPOINT}/{_hash}", "method": "GET", } + response = http(request_options, config, False) if response.status_code != 200: # noqa: PLR2004 diff --git a/src/bee_py/modules/feed.py b/src/bee_py/modules/feed.py index 555b1e2..b256a67 100644 --- a/src/bee_py/modules/feed.py +++ b/src/bee_py/modules/feed.py @@ -52,7 +52,7 @@ def create_feed_manifest( # * Raises a HTTPError if the response status is 4xx, 5xx response.raise_for_status() - return response.json()["reference"] + return Reference(value=response.json()["reference"]) def read_feed_update_headers(headers: dict[str, str]) -> FeedUpdateHeaders: diff --git a/src/bee_py/utils/eth.py b/src/bee_py/utils/eth.py index b162563..2c29ef5 100644 --- a/src/bee_py/utils/eth.py +++ b/src/bee_py/utils/eth.py @@ -7,13 +7,8 @@ from eth_account.messages import encode_defunct from eth_pydantic_types import HexBytes from eth_typing import ChecksumAddress as AddressType -from eth_utils import ( - is_address, - is_checksum_address, - keccak, - to_checksum_address, # ValidationError, - to_normalized_address, -) +from eth_utils import to_checksum_address # ValidationError, +from eth_utils import is_address, is_checksum_address, keccak, to_normalized_address from bee_py.Exceptions import AccountNotFoundError from bee_py.utils.hex import bytes_to_hex, hex_to_bytes, str_to_hex diff --git a/src/bee_py/utils/http.py b/src/bee_py/utils/http.py index eb86e42..118cd2d 100644 --- a/src/bee_py/utils/http.py +++ b/src/bee_py/utils/http.py @@ -80,7 +80,6 @@ def http( if "http" not in request_config["url"]: msg = f"Invalid URL: {request_config['url']}" raise TypeError(msg) - response = requests.request(**request_config) return response except Exception as e: diff --git a/src/bee_py/utils/reference.py b/src/bee_py/utils/reference.py index 0ad2355..472284a 100644 --- a/src/bee_py/utils/reference.py +++ b/src/bee_py/utils/reference.py @@ -31,11 +31,11 @@ def make_bytes_reference(reference: Union[Reference, bytes, str], offset: int = try: # Non-encrypted chunk hex string reference hex_reference = make_hex_string(reference, REFERENCE_HEX_LENGTH) - return hex_to_bytes(hex_reference, REFERENCE_BYTES_LENGTH) + return hex_to_bytes(hex_reference) except TypeError: # Encrypted chunk hex string reference hex_reference = make_hex_string(reference, ENCRYPTED_REFERENCE_HEX_LENGTH) - return hex_to_bytes(hex_reference, ENCRYPTED_REFERENCE_BYTES_LENGTH) + return hex_to_bytes(hex_reference) elif isinstance(reference, bytes): if has_bytes_at_offset(reference, offset, ENCRYPTED_REFERENCE_BYTES_LENGTH): diff --git a/tests/integration/test_bee_integration.py b/tests/integration/test_bee_integration.py index 529afaa..2b9bd7e 100644 --- a/tests/integration/test_bee_integration.py +++ b/tests/integration/test_bee_integration.py @@ -13,6 +13,7 @@ from bee_py.Exceptions import PinNotFoundError from bee_py.feed.topic import make_topic_from_string from bee_py.feed.type import FeedType +from bee_py.modules import bzz from bee_py.types.type import ( CHUNK_SIZE, REFERENCE_HEX_LENGTH, @@ -281,7 +282,7 @@ def test_if_reference_is_retrievable(bee_class, get_debug_postage): @pytest.mark.timeout(ERR_TIMEOUT) -def test_write_two_updates(bee_url, get_debug_postage, signer): +def test_write_updates_reference_zero(bee_url, get_debug_postage, signer): topic = bytes(random_byte_array(32, datetime.now(tz=timezone.utc))) bee_class = Bee(bee_url, {"signer": signer}) @@ -294,3 +295,62 @@ def test_write_two_updates(bee_url, get_debug_postage, signer): assert str(first_update_reference_response) == reference_zero.hex() assert first_update_reference_response.feed_index == "0000000000000000" + + +@pytest.mark.timeout(ERR_TIMEOUT) +def test_write_updates_reference_non_zero(bee_url, get_debug_postage, signer): + topic = bytes(random_byte_array(32, datetime.now(tz=timezone.utc))) + bee_class = Bee(bee_url, {"signer": signer}) + + feed = bee_class.make_feed_writer("sequence", topic, signer) + # * with referenceOne + reference_one = bytes( + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] + ) + feed.upload(get_debug_postage, reference_one) + feed_reader = bee_class.make_feed_reader("sequence", topic, signer) + first_update_reference_response = feed_reader.download() + + assert str(first_update_reference_response) == reference_one.hex() + assert first_update_reference_response.feed_index_next == "0000000000000001" + + +@pytest.mark.timeout(ERR_TIMEOUT) +def test_fail_fetching_non_existing_index(bee_url, get_debug_postage, signer): + topic = bytes(random_byte_array(32, datetime.now(tz=timezone.utc))) + bee_class = Bee(bee_url, {"signer": signer}) + + feed = bee_class.make_feed_writer("sequence", topic, signer) + reference_zero = bytes([0] * 32) + + feed.upload(get_debug_postage, reference_zero) + feed_reader = bee_class.make_feed_reader("sequence", topic, signer) + first_update_reference_response = feed_reader.download() + + assert str(first_update_reference_response) == reference_zero.hex() + assert first_update_reference_response.feed_index == "0000000000000000" + + with pytest.raises(requests.exceptions.HTTPError): + feed_reader.download({"index": "0000000000000001"}) + + +@pytest.mark.timeout(ERR_TIMEOUT) +def test_create_feeds_manifest_and_retreive_data(bee_url, get_debug_postage, signer, bee_ky_options): + topic = bytes(random_byte_array(32, datetime.now(tz=timezone.utc))) + bee_class = Bee(bee_url, {"signer": signer}) + owner = signer.address + + directory_structure = Collection( + entries=[CollectionEntry.model_validate({"path": "index.html", "data": bytearray(b"hello-world")})] + ) + + cac_result = bzz.upload_collection(bee_ky_options, directory_structure, get_debug_postage) + + feed = bee_class.make_feed_writer("sequence", topic, signer) + feed.upload(get_debug_postage, str(cac_result.reference)) + + manifest_result = bee_class.create_feed_manifest(get_debug_postage, "sequence", topic, owner) + + print(manifest_result) + + assert manifest_result