Skip to content

Commit

Permalink
Updated and modified files for the project
Browse files Browse the repository at this point in the history
- Updated files:
  - pyproject.toml
  - src/mantaray_py/__init__.py
  - src/mantaray_py/node.py
  - src/mantaray_py/utils.py
  - tests/conftest.py
  - tests/test_unit.py
  • Loading branch information
Aviksaikat committed Jun 6, 2024
1 parent e00819d commit 3deb61d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ classifiers = [
]
requires-python = ">=3.9"
dependencies = [
"pydantic",
"eth-utils",
"eth-hash[pycryptodome]"
]

[project.urls]
Expand Down Expand Up @@ -229,8 +232,8 @@ dependencies = [
"eth-utils",
"pytest",
"pytest-cov",
"rich"
]
installer = "uv"

[tool.hatch.envs.test.scripts]
test = "pytest"
Expand Down
2 changes: 1 addition & 1 deletion src/mantaray_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def init_manifest_node(options: dict = None) -> MantarayNode:
if 'obfuscationKey' in options:
manifest_node.set_obfuscation_key(options['obfuscationKey'])
else:
manifest_node.set_obfuscation_key(None)
manifest_node.set_obfuscation_key(gen_32_bytes())

return manifest_node

Expand Down
13 changes: 7 additions & 6 deletions src/mantaray_py/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
StorageLoader,
StorageSaver,
)
from mantaray_py.utils import IndexBytes, check_reference, common, encrypt_decrypt, equal_bytes, keccak256_hash
from mantaray_py.utils import IndexBytes, check_reference, common, encrypt_decrypt, equal_bytes
from eth_utils import keccak

PATH_SEPARATOR = "/"
PATH_SEPARATOR_BYTE = 47
Expand Down Expand Up @@ -450,11 +451,10 @@ def serialize(self) -> bytes:
for fork_index in self.forks.keys():
index.set_byte(int(fork_index))
index_bytes = index.get_bytes()

# Forks
fork_serializations: list[bytes] = []
fork_serializations: bytearray = []

for byte in index:
for byte in index.get_bytes():
fork = self.forks.get(byte)
if fork is None:
msg = f"Fork indexing error: fork has not found under {byte} index"
Expand Down Expand Up @@ -693,7 +693,7 @@ def serialize_version(version: Union[MarshalVersion, str]) -> bytes:
"""
version_name = "mantaray"
version_separator = ":"
hash_bytes = keccak256_hash(version_name + version_separator + version)
hash_bytes = keccak(text=(version_name + version_separator + version))

return hash_bytes[:31]

Expand All @@ -713,8 +713,9 @@ def serialize_reference_len(entry: Reference) -> bytes:
msg = f"Wrong referenceLength. It can be only 32 or 64. Got: {reference_len}"
raise ValueError(msg)

print(reference_len)
# Serialize the reference length into a single byte
return int.to_bytes(len(entry), byteorder="big", signed=False) # type: ignore
return (reference_len).to_bytes(reference_len, byteorder="big", signed=False) # type: ignore


def load_all_nodes(storage_loader: StorageLoader, node: MantarayNode) -> None:
Expand Down
7 changes: 1 addition & 6 deletions src/mantaray_py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@


class IndexBytes(BaseModel):
bytes_data: bytearray = Field(..., min_length=32, max_length=32)
bytes_data: bytearray = Field(bytearray(32), min_length=32, max_length=32)
model_config = ConfigDict(arbitrary_types_allowed=True)

def __init__(self, **data: dict) -> None:
super().__init__(**data)
# Initialize bytes_data with zeros
self.bytes_data = bytearray(32)

def set_byte(self, byte: int) -> None:
"""Set a byte value."""
if byte > 255: # noqa: PLR2004
Expand Down
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from mantaray_py import MantarayNode, equal_nodes, gen_32_bytes
import pytest
from pytest import fixture

@pytest.fixture
@fixture
def get_sample_mantaray_node() -> dict[MantarayNode, bytes]:

node = MantarayNode()
random_address = gen_32_bytes()
node.set_entry(random_address)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_unit.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from mantaray_py import MantarayNode, equal_nodes, gen_32_bytes, check_for_separator, init_manifest_node
import pytest
from rich.console import Console

console = Console()

def test_single_manaray_node_with_a_random_address(get_sample_mantaray_node):
def test_single_manaray_node_with_a_random_address():
node = init_manifest_node()
random_address = gen_32_bytes()
console.print(random_address)

node.set_entry(random_address)

serialized = node.serialize()
Expand Down

0 comments on commit 3deb61d

Please sign in to comment.