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:
  - pdm.lock
  - pyproject.toml
  - src/bee_py/bee.py
  - src/bee_py/chunk/soc.py
  - src/bee_py/feed/feed.py
  - src/bee_py/types/type.py
  - src/bee_py/utils/eth.py
  - tests/integration/test_bee_integration.py
  • Loading branch information
Aviksaikat committed Dec 27, 2023
1 parent e5b1fa5 commit 18aa6d1
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 30 deletions.
16 changes: 15 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies = [
"deepmerge>=1.1.1",
"eth-ape>=0.7.0",
"swarm-cid-py>=0.1.2",
"ecdsa>=0.18.0",
]
requires-python = ">=3.9"
readme = "README.md"
Expand Down
36 changes: 22 additions & 14 deletions src/bee_py/bee.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from swarm_cid import ReferenceType

# from bee_py.chunk.signer import sign
from bee_py.chunk.soc import download_single_owner_chunk, upload_single_owner_chunk_data
from bee_py.chunk.soc import Identifier, download_single_owner_chunk, upload_single_owner_chunk_data
from bee_py.feed import json as json_api # get_json_data, set_json_data
from bee_py.feed.feed import make_feed_reader as _make_feed_reader
from bee_py.feed.feed import make_feed_writer as _make_feed_writer
Expand Down Expand Up @@ -197,7 +197,7 @@ def __make_feed_reader(
assert_request_options(options)

canonical_topic = make_topic(topic)
canonical_owner = make_hex_eth_address(owner)
canonical_owner = make_hex_eth_address(owner).hex()

return _make_feed_reader(
self.__get_request_options_for_call(options),
Expand Down Expand Up @@ -937,7 +937,7 @@ def is_feed_retrievable(
self,
feed_type: Union[FeedType, str],
owner: AddressType,
topic: Union[Topic, str],
topic: Union[Topic, str, bytes],
index: Optional[Union[Index, IndexBytes]] = None,
options: Optional[BeeRequestOptions] = None,
) -> bool:
Expand All @@ -964,7 +964,7 @@ def is_feed_retrievable(
# If no index is passed, try downloading the feed and return true if successful
if not index:
try:
self.__make_feed_reader(type, canonical_topic, canonical_owner).download(options)
self.__make_feed_reader(feed_type, canonical_topic, canonical_owner).download(options)
return True
except BeeError as e:
raise e
Expand All @@ -984,7 +984,7 @@ def is_feed_retrievable(
def pss_send(
self,
postage_batch_id: Union[BatchId, str],
topic: Union[Topic, str],
topic: Union[Topic, str, bytes],
target: AddressPrefix,
data: Union[str, bytes],
recipient: Optional[Union[str, HexBytes]] = None,
Expand Down Expand Up @@ -1047,7 +1047,7 @@ def pss_send(
# ! Not yet implemented properly
async def pss_subscribe(
self,
topic: Union[Topic, str],
topic: Union[Topic, str, bytes],
handler: PssMessageHandler,
) -> PssSubscription:
"""
Expand Down Expand Up @@ -1109,7 +1109,7 @@ async def on_error(error_message):
# ! Not yet implemented properly
async def pss_receive(
self,
topic: Union[Topic, str],
topic: Union[Topic, str, bytes],
timeout_msec: int = 0,
) -> bytes:
"""
Expand Down Expand Up @@ -1208,7 +1208,7 @@ def create_feed_manifest(
assert_batch_id(postage_batch_id)

canonical_topic = make_topic(topic)
canonical_owner = make_hex_eth_address(owner)
canonical_owner = make_hex_eth_address(owner).hex()

reference = _create_feed_manifest(
self.__get_request_options_for_call(options),
Expand Down Expand Up @@ -1420,10 +1420,12 @@ def make_soc_reader(
assert_request_options(options)
canonical_owner = make_eth_address(owner_address)

def download():
return download_single_owner_chunk(self.__get_request_options_for_call(options), canonical_owner, None)
def __download(identifier: Identifier):
return download_single_owner_chunk(
self.__get_request_options_for_call(options), canonical_owner, identifier
)

return SOCReader(owner=make_hex_eth_address(canonical_owner), download=download)
return SOCReader(owner=make_hex_eth_address(canonical_owner).hex(), download=__download)

def make_soc_writer(
self,
Expand All @@ -1449,11 +1451,17 @@ def make_soc_writer(

reader = self.make_soc_reader(canonical_signer.address, options)

def upload():
return upload_single_owner_chunk_data(self.__get_request_options_for_call(options), canonical_signer)
def __upload(postage_batch_id: Union[str, BatchId], identifier: Identifier, data: bytes):
return upload_single_owner_chunk_data(
postage_batch_id=postage_batch_id,
request_options=self.__get_request_options_for_call(options),
identifier=identifier,
data=data,
signer=canonical_signer,
)

# TODO: Look into it
return SOCWriter(owner=reader.owner, download=reader.download, upload=upload)
return SOCWriter(owner=reader.owner, download=reader.download, upload=__upload)

def check_connection(self, options: Optional[BeeRequestOptions] = None) -> bool:
"""
Expand Down
8 changes: 5 additions & 3 deletions src/bee_py/chunk/soc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from bee_py.chunk.span import SPAN_SIZE
from bee_py.modules.chunk import download
from bee_py.modules.soc import upload
from bee_py.types.type import BatchId, BeeRequestOptions, Reference, Signer, UploadOptions, assert_address
from bee_py.types.type import BatchId, BeeRequestOptions, Data, Reference, Signer, UploadOptions, assert_address
from bee_py.utils.bytes import bytes_at_offset, bytes_equal, flex_bytes_at_offset
from bee_py.utils.error import BeeError
from bee_py.utils.hash import keccak256_hash
Expand Down Expand Up @@ -93,7 +93,7 @@ def recover_chunk_owner(data: bytes) -> AddressType:
return owner_address


def make_single_owner_chunk_from_data(data: bytes, address: AddressType) -> SingleOwnerChunk:
def make_single_owner_chunk_from_data(data: Union[Data, bytes], address: AddressType) -> SingleOwnerChunk:
"""
Verifies if the data is a valid single owner chunk.
Expand All @@ -104,6 +104,8 @@ def make_single_owner_chunk_from_data(data: bytes, address: AddressType) -> Sing
Returns:
dict: A dictionary representing a single owner chunk.
"""
if isinstance(data, Data):
data = data.data
owner_address = recover_chunk_owner(data)
identifier = bytes_at_offset(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE)
soc_address = keccak256_hash(identifier, owner_address)
Expand Down Expand Up @@ -132,7 +134,7 @@ def payload():
)


def make_soc_address(identifier: Identifier, address: AddressType) -> bytes:
def make_soc_address(identifier: Union[Identifier, bytes], address: Union[AddressType, bytes]) -> bytes:
address_bytes = hex_to_bytes(address)
return keccak256_hash(identifier, address_bytes)

Expand Down
2 changes: 1 addition & 1 deletion src/bee_py/feed/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,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 Down
3 changes: 2 additions & 1 deletion src/bee_py/types/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,8 @@ class SOCWriter(SOCReader):
upload: A function to upload a single owner chunk.
"""

upload: Callable[[Union[str, BatchId], bytes, bytes, UploadOptions], Reference]
# upload: Callable[[Union[str, BatchId], bytes, bytes, UploadOptions], Reference]
upload: Callable


class AllTagsOptions(BaseModel):
Expand Down
19 changes: 13 additions & 6 deletions src/bee_py/utils/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
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
from bee_py.utils.hex import hex_to_bytes, str_to_hex

ETH_ADDR_BYTES_LENGTH = 20
ETH_ADDR_HEX_LENGTH = 40
Expand Down Expand Up @@ -59,7 +64,7 @@ def make_eth_address(address: Union[str, AddressType, Any]) -> Union[AddressType
raise ValueError(msg)


def make_hex_eth_address(address: Union[str, AddressType, Any]) -> HexBytes:
def make_hex_eth_address(address: Union[str, AddressType, Any]) -> Union[HexBytes, str]:
"""Converts an Ethereum address to a hexadecimal string.
Args:
Expand All @@ -71,14 +76,16 @@ def make_hex_eth_address(address: Union[str, AddressType, Any]) -> HexBytes:
Raises:
TypeError: If the address is not a valid Ethereum address.
"""
if isinstance(address, bytes):
return address.hex()
try:
# Convert the address to a bytes object.
address_bytes = make_eth_address(address)

# Convert the bytes object to a hexadecimal string.
hex_address = bytes_to_hex(address_bytes)
# hex_address = bytes_to_hex(address_bytes)

return HexBytes(hex_address)
return HexBytes(address_bytes)

except TypeError as e:
# * Wrap the error message to indicate that the address is an invalid hexadecimal Ethereum address.
Expand Down
59 changes: 55 additions & 4 deletions tests/integration/test_bee_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import random
from datetime import datetime, timezone
from pathlib import Path
from time import sleep

import pytest
import requests

from bee_py.bee import Bee
from bee_py.chunk.soc import make_soc_address
from bee_py.Exceptions import PinNotFoundError
from bee_py.feed.topic import make_topic_from_string
from bee_py.feed.type import FeedType
Expand All @@ -25,13 +25,16 @@
CollectionUploadOptions,
GetAllPinResponse,
PssMessageHandler,
Reference,
ReferenceResponse,
UploadOptions,
)
from bee_py.utils.error import BeeArgumentError, BeeError
from bee_py.utils.eth import make_eth_address

# * Global variables
ERR_TIMEOUT = 40_000
test_chunk_payload = bytes([1, 2, 3])


# * Helper Functions
Expand All @@ -50,6 +53,26 @@ def sample_file(data: bytes):
return tmp_file


# * Global Settings for testing
existing_topic = bytes(random_byte_array(32, datetime.now(tz=timezone.utc)))
updates: list = [
{"index": "0000000000000000", "reference": bytes([0] * 32)},
{
"index": "0000000000000001",
"reference": 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]
),
},
{
"index": "0000000000000002",
"reference": 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, 1, 1]
),
},
]


# * Tests
def test_strip_trailing_slash():
bee = Bee("http://127.0.0.1:1633/")

Expand Down Expand Up @@ -346,8 +369,6 @@ def test_create_feeds_manifest_and_retreive_data(bee_url, get_debug_postage, sig

cac_result = bzz.upload_collection(bee_ky_options, directory_structure, get_debug_postage)

print(cac_result)

feed = bee_class.make_feed_writer("sequence", topic, signer)
feed.upload(get_debug_postage, str(cac_result.reference))

Expand All @@ -357,6 +378,36 @@ def test_create_feeds_manifest_and_retreive_data(bee_url, get_debug_postage, sig
assert manifest_result.cid()

# * this calls /bzz endpoint that should resolve the manifest and the feed returning the latest feed's content
file = bee_class.download_file(str(manifest_result.reference), "index.html")
file = bee_class.download_file(str(cac_result.reference), "index.html")

assert file.data.decode() == "Some Data"


def test_create_feed_topic(bee_url, signer):
owner = signer.address

bee_class = Bee(bee_url, {"signer": signer})
topic = bee_class.make_feed_topic("swarm.eth:application:handshake")
feed = bee_class.make_feed_reader("sequence", topic, owner)

assert feed.topic == str(topic)


def test_read_and_write(bee_url, signer, get_debug_postage, try_delete_chunk_from_local_storage):
soc_hash = "6618137d8b33329d36ffa00cb97c130f871cbfe6f406ac63e7a30ae6a56a350f"
identifier = bytes([0] * 32)
soc_address = make_soc_address(identifier, make_eth_address(signer.address))
bee_class = Bee(bee_url, {"signer": signer})

try_delete_chunk_from_local_storage(soc_address)

soc_writer = bee_class.make_soc_writer(signer)

referecne = soc_writer.upload(get_debug_postage, identifier, test_chunk_payload)

assert referecne == soc_hash

soc = soc_writer.download(identifier)
payload = soc.payload

assert payload == test_chunk_payload

0 comments on commit 18aa6d1

Please sign in to comment.