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:
  - src/mantaray_py/__init__.py
  - src/mantaray_py/node.py
  - tests/conftest.py
  - tests/integration/test_int.py
  • Loading branch information
Aviksaikat committed Jun 15, 2024
1 parent 7bb4ecc commit d1ba094
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/mantaray_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"flatten_bytes_array",
"gen_32_bytes",
"keccak256_hash",
"load_all_nodes",
"marshal_version_values",
"load_all_nodes"
]


Expand Down
53 changes: 36 additions & 17 deletions src/mantaray_py/node.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from typing import Any, Optional, Union
import re
from typing import Any, Optional, Union

from eth_utils import keccak
from pydantic import BaseModel, ConfigDict
from rich.traceback import install
Expand Down Expand Up @@ -295,7 +296,7 @@ def add_fork(self, path: bytes, entry: Reference, metadata: Optional[MetadataMap
self.make_dirty()
return

if self.is_dirty() and not self.forks:
if self.is_dirty() and self.forks is None:
self.forks = {}

if self.forks is None:
Expand Down Expand Up @@ -371,7 +372,8 @@ def get_fork_at_path(self, path: bytes) -> Optional[Union[Any, MantarayFork]]:
"""
if not path:
raise EmptyPathError()
if not self.forks:
print(f"{self.forks=}")
if self.forks is None:
msg = "Fork mapping is not defined in the manifest"
raise ValueError(msg)

Expand All @@ -397,7 +399,7 @@ def remove_path(self, path: bytes) -> None:
if not path:
msg = "Path is empty"
raise ValueError(msg)
if not self.forks:
if self.forks is None:
msg = "Fork mapping is not defined in the manifest"
raise ValueError(msg)

Expand Down Expand Up @@ -461,7 +463,7 @@ def serialise(self) -> bytes:
if not self.__obfuscation_key:
self.set_obfuscation_key(bytes(32))
# self.set_obfuscation_key(
if not self.forks:
if self.forks is None:
if not self.__entry:
msg = "Entry"
raise UndefinedFieldError(msg)
Expand Down Expand Up @@ -533,22 +535,24 @@ def deserialise(self, data: bytes) -> None:
if len(data) < node_header_size:
raise ValueError("The serialised input is too short")

self.__obfuscation_key = data[:node_header_sizes.obfuscation_key]
self.__obfuscation_key = data[: node_header_sizes.obfuscation_key]
data = encrypt_decrypt(self.__obfuscation_key, data, len(self.__obfuscation_key))

version_hash = data[node_header_sizes.obfuscation_key:node_header_sizes.obfuscation_key + node_header_sizes.version_hash]
version_hash = data[
node_header_sizes.obfuscation_key : node_header_sizes.obfuscation_key + node_header_sizes.version_hash
]

if equal_bytes(version_hash, serialise_version("0.1")):
raise NotImplementedError()
elif equal_bytes(version_hash, serialise_version("0.2")):
ref_bytes_size = data[node_header_size - 1]
entry = data[node_header_size:node_header_size + ref_bytes_size]
entry = data[node_header_size : node_header_size + ref_bytes_size]

if ref_bytes_size == 0:
entry = bytes(32)
self.__entry = entry
offset = node_header_size + ref_bytes_size
index_bytes = data[offset:offset + 32]
index_bytes = data[offset : offset + 32]

if not equal_bytes(index_bytes, bytes(32)):
self.__make_edge()
Expand All @@ -563,22 +567,37 @@ def deserialise(self, data: bytes) -> None:
if len(data) < offset + node_fork_sizes.node_type:
raise ValueError(f"There is not enough size to read nodeType of fork at offset {offset}")

node_type = data[offset:offset + node_fork_sizes.node_type]
node_type = data[offset : offset + node_fork_sizes.node_type]
node_fork_size = node_fork_sizes.pre_reference + ref_bytes_size

if node_type_is_with_metadata_type(node_type[0]):
if len(data) < offset + node_fork_sizes.pre_reference + ref_bytes_size + node_fork_sizes.metadata:
if (
len(data)
< offset + node_fork_sizes.pre_reference + ref_bytes_size + node_fork_sizes.metadata
):
raise ValueError(f"Not enough bytes for metadata node fork at byte {byte}")

metadata_byte_size = int.from_bytes(data[offset + node_fork_size:offset + node_fork_size + node_fork_sizes.metadata], byteorder="big")
metadata_byte_size = int.from_bytes(
data[offset + node_fork_size : offset + node_fork_size + node_fork_sizes.metadata],
byteorder="big",
)
node_fork_size += node_fork_sizes.metadata + metadata_byte_size

fork = MantarayFork.deserialise(data[offset:offset + node_fork_size], self.__obfuscation_key, {"with_metadata": {"ref_bytes_size": ref_bytes_size, "metadata_byte_size": metadata_byte_size}})
fork = MantarayFork.deserialise(
data[offset : offset + node_fork_size],
self.__obfuscation_key,
{
"with_metadata": {
"ref_bytes_size": ref_bytes_size,
"metadata_byte_size": metadata_byte_size,
}
},
)
else:
if len(data) < offset + node_fork_sizes.pre_reference + ref_bytes_size:
raise ValueError(f"There is not enough size to read fork at offset {offset}")

fork = MantarayFork.deserialise(data[offset:offset + node_fork_size], self.__obfuscation_key)
fork = MantarayFork.deserialise(data[offset : offset + node_fork_size], self.__obfuscation_key)

self.forks[byte] = fork
offset += node_fork_size
Expand Down Expand Up @@ -834,15 +853,15 @@ def remove_space_and_add_newlines(byte_data: bytes) -> bytes:
- Processed byte data.
"""
# Pattern to identify JSON-like strings
json_pattern = re.compile(rb'\{[^\{\}]*\}')
json_pattern = re.compile(rb"\{[^\{\}]*\}")

# Function to process each match
def process_match(match):
json_bytes = match.group()
# Remove spaces after colons
fixed_json_bytes = re.sub(rb':\s+', b':', json_bytes)
fixed_json_bytes = re.sub(rb":\s+", b":", json_bytes)
# Add newlines after each closing brace `}` that follows a quote `"`
fixed_json_bytes_with_newlines = re.sub(rb'("\})', rb'\1\n', fixed_json_bytes)
fixed_json_bytes_with_newlines = re.sub(rb'("\})', rb"\1\n", fixed_json_bytes)
return fixed_json_bytes_with_newlines

# Replace each JSON-like match in the byte_data
Expand Down
25 changes: 13 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ def get_sample_mantaray_node() -> dict[MantarayNode, bytes]:
random_address = gen_32_bytes()
node.set_entry(random_address)

paths = [
"path1/valami/elso",
"path1/valami/masodik",
"path1/valami/masodik.ext",
"path1/valami",
"path2",
]

for path in paths:
encoded_path = path.encode()
node.add_fork(encoded_path, random_address)
path1 = 'path1/valami/elso'.encode()
path2 = 'path1/valami/masodik'.encode()
path3 = 'path1/valami/masodik.ext'.encode()
path4 = 'path1/valami'.encode()
path5 = 'path2'.encode()

# Add forks to the node
node.add_fork(path1, random_address, {'vmi': 'elso'})
node.add_fork(path2, random_address)
node.add_fork(path3, random_address)
node.add_fork(path4, random_address, {'vmi': 'negy'})
node.add_fork(path5, random_address)

return {
"node": node,
"paths": [path.encode() for path in paths],
"paths": [path1, path2, path3, path4, path5],
}


Expand Down
5 changes: 1 addition & 4 deletions tests/integration/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def create_save_function(
bee_class:Bee, get_debug_postage:str
) -> Callable[[bytes], bytes]:
def save_function(data:bytes) -> bytes:
console.print(f"{bytes_to_hex(data)=}")
# console.print(f"{bytes_to_hex(data)=}")
hex_reference = bee_class.upload_data(get_debug_postage, data)
# console.print(f"{str(hex_reference.reference)=}")
return hex_to_bytes(str(hex_reference.reference))
Expand Down Expand Up @@ -186,8 +186,6 @@ async def test_construct_manifests_of_testpage_folder(get_debug_postage, bee_cla

# Check Bee manifest is equal to the constructed one
assert i_node == node

# print('Constructed root manifest hash', i_node_ref.hex())


def test_remove_fork_then_upload(get_sample_mantaray_node, get_debug_postage, bee_class):
Expand All @@ -200,7 +198,6 @@ def test_remove_fork_then_upload(get_sample_mantaray_node, get_debug_postage, be
# Save the sample node
ref_original = node.save(save_function)


check_node1 = node.get_fork_at_path(b'path1/valami/').node
ref_check_node1 = check_node1.get_content_address()

Expand Down

0 comments on commit d1ba094

Please sign in to comment.