Skip to content

Commit

Permalink
13 Nov update. stamps.py, http.py & test for stamps.py added
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviksaikat committed Nov 13, 2023
1 parent 8bff442 commit a251d0b
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
83 changes: 81 additions & 2 deletions src/bee_py/types/type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
from typing import Any, Callable, Generic, TypeVar, Union
from typing import Any, Callable, Generic, Optional, TypeVar, Union

from typing_extensions import TypeAlias

from bee_py.utils.hex import bytes_to_hex

Expand All @@ -9,6 +11,36 @@
T = TypeVar("T", bound=Callable)


SPAN_SIZE = 8
SECTION_SIZE = 32
BRANCHES = 128
CHUNK_SIZE = SECTION_SIZE * BRANCHES

ADDRESS_HEX_LENGTH = 64
PSS_TARGET_HEX_LENGTH_MAX = 6
PUBKEY_HEX_LENGTH = 66
BATCH_ID_HEX_LENGTH = 64
REFERENCE_HEX_LENGTH = 64
ENCRYPTED_REFERENCE_HEX_LENGTH = 128
REFERENCE_BYTES_LENGTH = 32
ENCRYPTED_REFERENCE_BYTES_LENGTH = 64

# Minimal depth that can be used for creation of postage batch
STAMPS_DEPTH_MIN = 17

# Maximal depth that can be used for creation of postage batch
STAMPS_DEPTH_MAX = 255

TAGS_LIMIT_MIN = 1
TAGS_LIMIT_MAX = 1000
FEED_INDEX_HEX_LENGTH = 16


# Type aliases
BatchId: TypeAlias = str
AddressPrefix: TypeAlias = str


class BrandedType(Generic[Type, Name]):
"""A type that is branded with a name.
Expand Down Expand Up @@ -162,7 +194,7 @@ def text(self) -> str:

return self.data.decode("utf-8")

def hex(self) -> str:
def hex(self) -> str: # noqa: A003
"""Converts the binary data into hex-string.
Returns:
Expand Down Expand Up @@ -190,3 +222,50 @@ def json(self) -> dict[str, Any]:
json_object = json.dumps(dict_obj)

return json.loads(json_object)


def is_object(value: Any) -> bool:
"""
Checks if a value is an object.
Args:
value: The value to check.
Returns:
True if the value is an object, False otherwise.
"""
return value is not None and isinstance(value, dict)


class UploadOptions:
"""Represents the options for uploading a file to Bee."""

pin: Optional[bool]
encrypt: Optional[bool]
tag: Optional[int]
deferred: Optional[bool]

def __init__(
self,
pin: Optional[bool] = None,
encrypt: Optional[bool] = None,
tag: Optional[int] = None,
deferred: Optional[bool] = True, # noqa: FBT002
):
self.pin = pin
self.encrypt = encrypt
self.tag = tag
self.deferred = deferred


class FileHeaders:
"""Represents the headers for a file."""

name: Optional[str]
tag_uid: Optional[int]
content_type: Optional[str]

def __init__(self, name: Optional[str] = None, tag_uid: Optional[int] = None, content_type: Optional[str] = None):
self.name = name
self.tagUid = tag_uid
self.contentType = content_type
34 changes: 34 additions & 0 deletions tests/unit/utils/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import pytest

from bee_py.utils.bytes import wrap_bytes_with_helpers
from bee_py.utils.stamps import (
get_stamp_cost_in_bzz,
get_stamp_cost_in_plur,
get_stamp_maximum_capacity_bytes,
get_stamp_ttl_seconds,
get_stamp_usage,
)


@pytest.fixture
Expand All @@ -14,6 +21,7 @@ def test_data():
return bytes.fromhex("00112233445566778899aabbccddeeff"), "00112233445566778899aabbccddeeff"


# text_bytes
@pytest.fixture
def wrap_bytes_with_helpers_fixture():
data = b"hello world"
Expand All @@ -24,3 +32,29 @@ def wrap_bytes_with_helpers_fixture():
@pytest.fixture
def wrapped_bytes(wrap_bytes_with_helpers_fixture):
return wrap_bytes_with_helpers_fixture


# test_stamps
@pytest.fixture
def stamp_usage(utilization=4, depth=18, bucket_depth=16):
return get_stamp_usage(utilization, depth, bucket_depth)


@pytest.fixture
def stamp_maximum_capacity_bytes(depth=20):
return get_stamp_maximum_capacity_bytes(depth)


@pytest.fixture
def stamp_ttl_seconds(amount=20_000_000_000, price_per_block=24_000, block_time=5):
return get_stamp_ttl_seconds(amount, price_per_block, block_time)


@pytest.fixture
def stamp_cost_in_bzz(depth=20, amount=20_000_000_000):
return get_stamp_cost_in_bzz(depth, amount)


@pytest.fixture
def stamp_cost_in_plur(depth=20, amount=20_000_000_000):
return get_stamp_cost_in_plur(depth, amount)

0 comments on commit a251d0b

Please sign in to comment.