Skip to content

Commit

Permalink
25th Dec update. All tests passed 914 of them....Merry Xmas
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviksaikat committed Dec 25, 2023
1 parent ddef075 commit ff600c9
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/bee_py/bee.py
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ def make_feed_writer(
assert_feed_type(feed_type)

canonical_topic = make_topic(topic)
canonical_signer = self.__resolve_signer(signer).address
canonical_signer = self.__resolve_signer(signer)

return _make_feed_writer(
self.__get_request_options_for_call(options),
Expand Down
2 changes: 1 addition & 1 deletion src/bee_py/chunk/soc.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SingleOwnerChunkBase(BaseModel):
owner: The owner of the SOC.
"""

identifier: str = Field(..., description="The identifier of the SOC")
identifier: Union[str, bytes] = Field(..., description="The identifier of the SOC")
signature: bytes = Field(..., description="The signature of the SOC")
owner: str = Field(..., description="The owner of the SOC")

Expand Down
27 changes: 16 additions & 11 deletions src/bee_py/feed/feed.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import datetime
from datetime import datetime, timezone
from functools import partial
from typing import Optional, Union

import requests
from ape.managers.accounts import AccountAPI
from eth_typing import ChecksumAddress as AddressType

Expand All @@ -13,11 +14,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 Down Expand Up @@ -59,7 +60,7 @@ def find_next_index(
try:
feed_update = fetch_latest_feed_update(request_options, owner, topic, options)
return make_hex_string(feed_update.feed_index_next, FEED_INDEX_HEX_LENGTH)
except Exception as e:
except requests.HTTPError as e:
if e.response.status_code == 404: # noqa: PLR2004
return bytes_to_hex(make_bytes(8))
raise e
Expand All @@ -68,7 +69,7 @@ def find_next_index(
def update_feed(
request_options: BeeRequestOptions,
signer: AccountAPI,
topic: Topic,
topic: Union[Topic, str],
reference: Reference,
postage_batch_id: BatchId,
options: Optional[FeedUpdateOptions] = None,
Expand All @@ -94,12 +95,14 @@ def update_feed(
:return: The reference.
:rtype: Reference
"""
owner_hex = make_hex_eth_address(signer.address)
owner_hex = make_hex_eth_address(signer.address).hex()
if isinstance(topic, Topic):
topic = topic.value
next_index = index if index != "latest" else find_next_index(request_options, owner_hex, topic, options)

identifier = make_feed_identifier(topic, next_index)
at = options.at if options and options.at else datetime.now().timestamp()
timestamp = write_big_endian(at)
at = options.at if options and options.at else datetime.now(tz=timezone.utc).timestamp()
timestamp = write_big_endian(int(at))
payload_bytes = serialize_bytes(timestamp, reference)

return upload_single_owner_chunk_data(request_options, signer, postage_batch_id, identifier, payload_bytes, options)
Expand Down Expand Up @@ -179,7 +182,9 @@ def __download(
options = FeedUpdateOptions.model_validate(options)

if not options or not options.index:
options = options.model_dump()
# * if options exists but not options.index then keep other configs from options
if not options:
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)
Expand All @@ -190,7 +195,7 @@ def __download(
feed_index_next="",
)

download_partial = partial(__download, request_options)
download_partial = partial(__download)

return FeedReader(
request_options=request_options,
Expand Down Expand Up @@ -224,7 +229,7 @@ def make_feed_writer(
def __upload(
postage_batch_id: Union[BatchId, AddressType],
reference: Reference,
options: Optional[FeedUpdateOptions] = None,
options: Optional[FeedUpdateOptions] = {}, # noqa: B006
) -> Reference:
canonical_reference = make_bytes_reference(reference)
return update_feed(
Expand All @@ -238,7 +243,7 @@ def __upload(

return FeedWriter(
request_options=request_options,
type=_type,
Type=_type,
topic=topic,
signer=signer,
upload=__upload,
Expand Down
6 changes: 3 additions & 3 deletions src/bee_py/feed/identifiers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Union

from bee_py.chunk.soc import Identifier
from bee_py.types.type import FEED_INDEX_HEX_LENGTH, Index, IndexBytes, Topic
Expand All @@ -12,7 +12,7 @@ def is_epoch(epoch: Any) -> bool:


def hash_feed_identifier(topic: Topic, index: IndexBytes) -> Identifier:
keccak256_hash(hex_to_bytes(topic), index)
return keccak256_hash(hex_to_bytes(topic), index)


def make_sequential_feed_identifier(topic: Topic, index: int) -> Identifier:
Expand All @@ -35,7 +35,7 @@ def make_feed_index_bytes(s: str) -> IndexBytes:
return hex_to_bytes(hex_string)


def make_feed_identifier(topic: Topic, index: Index) -> Identifier:
def make_feed_identifier(topic: Topic, index: Index) -> Union[Identifier, bytes]:
"""
Converts a topic and an index into a feed identifier.
Expand Down
2 changes: 1 addition & 1 deletion src/bee_py/modules/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def write_big_endian(value: int, bytes_data: Optional[bytes] = None):
:return: The byte array.
:rtype: bytearray
"""
if bytes_data:
if not bytes_data:
bytes_data = make_bytes(8)
struct.pack_into(">Q", bytes_data, 0, value)
return bytes_data
Expand Down
11 changes: 8 additions & 3 deletions src/bee_py/modules/feed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Union

from eth_typing import ChecksumAddress as AddressType

Expand All @@ -21,7 +21,7 @@
def create_feed_manifest(
request_options: BeeRequestOptions,
owner: AddressType,
topic: Topic,
topic: Union[Topic, str],
postage_batch_id: str,
options: Optional[CreateFeedOptions] = None,
) -> Reference:
Expand All @@ -38,6 +38,9 @@ def create_feed_manifest(
Returns:
Reference: The reference of the created feed.
"""
if isinstance(topic, Topic):
topic = str(topic)

config = {
"method": "post",
"url": f"{FEED_ENDPOINT}/{owner}/{topic}",
Expand Down Expand Up @@ -82,7 +85,7 @@ def read_feed_update_headers(headers: dict[str, str]) -> FeedUpdateHeaders:
def fetch_latest_feed_update(
request_options: BeeRequestOptions,
owner: AddressType,
topic: Topic,
topic: Union[Topic, str],
options: Optional[FeedUpdateOptions] = None,
) -> FetchFeedUpdateResponse:
"""Finds and retrieves the latest feed update.
Expand All @@ -96,6 +99,8 @@ def fetch_latest_feed_update(
Returns:
A FetchFeedUpdateResponse object containing the feed update reference, index, and next index.
"""
if isinstance(topic, Topic):
topic = topic.value

response = http(
request_options,
Expand Down
14 changes: 7 additions & 7 deletions src/bee_py/types/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def assert_address(value: Any):
Raises:
ValueError: If the value is not an Ethereum address.
"""
if not isinstance(value, str) or len(value) != ADDRESS_HEX_LENGTH or not value.startswith("0x"):
if not isinstance(value, str) or len(value) != ADDRESS_HEX_LENGTH:
msg = "Value is not an Ethereum address!"
raise ValueError(msg)

Expand Down Expand Up @@ -798,13 +798,12 @@ class Config:


class FeedReader(BaseModel):
Type: FeedType
owner: str
topic: str
Type: Union[FeedType, str]
owner: Optional[str] = ""
topic: Union[Topic, str]
request_options: Optional[BeeRequestOptions] = None

download: Callable
# * Callable[[Union[str, BatchId], Union[bytes, Reference], Optional[FeedUploadOptions], Reference]]
download: Callable = ""
# upload: Callable


Expand All @@ -819,8 +818,9 @@ class FeedWriter(FeedReader):
upload: The upload function.
"""

# * Callable[[Union[str, BatchId], Union[bytes, Reference], Optional[FeedUploadOptions], Reference]]
upload: Callable
signer: Signer
signer: Union[Signer, AccountAPI]


class JsonFeedOptions(BaseModel):
Expand Down
9 changes: 7 additions & 2 deletions src/bee_py/utils/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
from eth_account.messages import encode_defunct
from eth_pydantic_types import HexBytes
from eth_typing import ChecksumAddress as AddressType
from eth_utils import to_checksum_address # ValidationError,
from eth_utils import is_address, is_checksum_address, keccak, to_normalized_address
from eth_utils import (
is_address,
is_checksum_address,
keccak,
to_checksum_address, # ValidationError,
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
3 changes: 2 additions & 1 deletion src/bee_py/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def sanitise_config(options: Union[BeeRequestOptions, dict]) -> Union[BeeRequest
if key and key not in bad_configs:
if key not in [True, False]:
if key == "type":
value = str(value.value) # noqa: PLW2901
if not isinstance(value, str):
value = str(value.value) # noqa: PLW2901
query_params.append(f"{key}={value}")
if key != "type":
keys_to_remove.append(key)
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/test_bee_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,26 @@ def test_if_reference_is_retrievable(bee_class, get_debug_postage):
assert (
bee_class.is_reference_retrievable("ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4332") is False
)


# @pytest.mark.timeout(ERR_TIMEOUT)
# def test_send_receive_data(bee_class, get_debug_postage):
# topic = "bee-class-topic"
# message = bytes([1, 2, 3])
# ! need bee debug


@pytest.mark.timeout(ERR_TIMEOUT)
def test_write_two_updates(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"

0 comments on commit ff600c9

Please sign in to comment.