Skip to content

Commit

Permalink
Update and modify files for the project
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Aviksaikat committed Dec 26, 2023
1 parent ff600c9 commit da5eaca
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/bee_py/bee.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1257,7 +1257,6 @@ def make_feed_reader(
feed_type,
canonical_topic,
canonical_signer,
canonical_signer,
)

def make_feed_writer(
Expand Down
15 changes: 9 additions & 6 deletions src/bee_py/feed/feed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime, timezone
from functools import partial

# from functools import partial
from typing import Optional, Union

import requests
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -187,23 +190,23 @@ 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),
feed_index=options.index,
feed_index_next="",
)

download_partial = partial(__download)
# download_partial = partial(__download)

return FeedReader(
request_options=request_options,
Type=_type,
owner=owner,
topic=topic,
options=options,
download=download_partial,
download=__download,
)


Expand Down
1 change: 1 addition & 0 deletions src/bee_py/modules/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/bee_py/modules/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 2 additions & 7 deletions src/bee_py/utils/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/bee_py/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/bee_py/utils/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
62 changes: 61 additions & 1 deletion tests/integration/test_bee_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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})

Expand All @@ -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

0 comments on commit da5eaca

Please sign in to comment.