Skip to content

Commit

Permalink
error: for some search terminal node forks are None instead of {}
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviksaikat committed Jun 17, 2024
1 parent c46d0d6 commit cccde68
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file.

## [0.0.1] - 2024-06-14
## [0.0.1] - 2024-06-16

### 🐛 Bug Fixes

Expand All @@ -28,5 +28,8 @@ All notable changes to this project will be documented in this file.
### Error

- Serialise method seems to be faulty
- Issue with the encrypt_decrypt function. have to fix deserialise as well
- Some reason fork is None for path=b'/'
- Still issue with the serialised object retruend. it has a extra white space character which is changing the whole hash

<!-- generated by git-cliff -->
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ ignore = [
"ISC001", # causes unexpected behaviour with formatter
]
[tool.ruff.lint.pylint]
# The MantarayNode class has 23 public methods just to ignore unnecessary warnings
max-public-methods = 23
# The MantarayNode class has 24 public methods just to ignore unnecessary warnings
max-public-methods = 24

[tool.ruff.lint.isort]
known-first-party = ["mantaray_py"]
Expand Down
60 changes: 34 additions & 26 deletions src/mantaray_py/node.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import re
from typing import Any, Optional, Union
from typing import Optional, Union

from eth_utils import keccak
from pydantic import BaseModel, ConfigDict
from rich.console import Console
from rich.traceback import install

from mantaray_py.types import (
Expand All @@ -17,6 +18,7 @@
from mantaray_py.utils import IndexBytes, check_reference, common, encrypt_decrypt, equal_bytes, flatten_bytes_array

install()
console = Console()

PATH_SEPARATOR = "/"
PATH_SEPARATOR_BYTE = 47
Expand Down Expand Up @@ -79,8 +81,10 @@ def serialise(self) -> bytes:
data = bytes([node_type]) + prefix_len_bytes + prefix_bytes + entry

if self.node.is_with_metadata_type():
json_string = json.dumps(self.node.get_metadata())
metadata_bytes = json_string.encode("utf-8")
# console.log(json.dumps(self.node.get_metadata()).replace(' ', ''))
json_string = json.dumps(self.node.get_metadata()).replace(" ", "")
# * default utf-8 encoding
metadata_bytes = json_string.encode()

metadata_size_with_size = len(metadata_bytes) + node_fork_sizes.metadata
padding = self.__create_metadata_padding(metadata_size_with_size)
Expand Down Expand Up @@ -129,7 +133,8 @@ def deserialise(
node.set_entry(data[entry_start:])

node.set_type(node_type)
#* For some reason recursive save is making the last level fork nodes to None. To fix it tweaking the logic
# * For some reason recursive save is making the last level fork nodes to None. To fix it tweaking the logic
# ! FIXME: this condition should never happpen. Most likely some recursive fn. is breaking
if node.forks is None:
node.forks = {}
return cls(prefix=prefix, node=node)
Expand Down Expand Up @@ -281,18 +286,19 @@ def add_fork(self, path: bytes, entry: Reference, metadata: Optional[MetadataMap
Adds a fork to the current node based on the provided path, entry, and metadata.
Parameters:
- path (list[int]): A list representing the path in bytes. Can be empty, in which case `entry`
- path (bytes): A byte array representing the path. Can be empty, in which case `entry`
will be set as the current node's entry.
- entry (Reference): The entry to be associated with the fork.
- metadata (Dict[str, Any], optional): Additional metadata to associate with the fork.
- metadata (Optional[MetadataMapping]): Additional metadata to associate with the fork.
Defaults to an empty dictionary.
Returns:
None
"""
if metadata is None:
metadata = {}
if not path:

if len(path) == 0:
self.set_entry(entry)
if metadata:
self.set_metadata(metadata)
Expand All @@ -303,17 +309,17 @@ def add_fork(self, path: bytes, entry: Reference, metadata: Optional[MetadataMap
self.forks = {}

if self.forks is None:
msg = "Fork mapping is not defined in the manifest"
raise ValueError(msg)
raise ValueError("Fork mapping is not defined in the manifest")

fork = self.forks.get(path[0])
fork: MantarayFork = self.forks.get(path[0])

if fork is None:
if not fork:
new_node: MantarayNode = MantarayNode()
if self.__obfuscation_key:
new_node.set_obfuscation_key(self.__obfuscation_key)

node_fork_sizes: NodeForkSizes = NodeForkSizes()
node_fork_sizes: NodeHeaderSizes = NodeForkSizes()
# * check for prefix size limit
if len(path) > node_fork_sizes.prefix_max_size:
prefix = path[: node_fork_sizes.prefix_max_size]
rest = path[node_fork_sizes.prefix_max_size :]
Expand All @@ -327,6 +333,7 @@ def add_fork(self, path: bytes, entry: Reference, metadata: Optional[MetadataMap
new_node.set_entry(entry)
if metadata:
new_node.set_metadata(metadata)

new_node.__update_with_path_separator(path)
self.forks[path[0]] = MantarayFork(prefix=path, node=new_node)
self.make_dirty()
Expand All @@ -338,12 +345,14 @@ def add_fork(self, path: bytes, entry: Reference, metadata: Optional[MetadataMap
new_node = fork.node

if rest_path:
# * move current common prefix node
new_node = MantarayNode()
new_node.set_obfuscation_key(self.__obfuscation_key or bytes(32))
fork.node.__update_with_path_separator(rest_path)
new_node.forks = {rest_path[0]: MantarayFork(prefix=rest_path, node=fork.node)}
new_node.__make_edge()

# * if common path is full path new node is value type
if len(path) == len(common_path):
new_node.__make_value()

Expand All @@ -360,7 +369,7 @@ def add_fork(self, path: bytes, entry: Reference, metadata: Optional[MetadataMap
self.__make_edge()
self.make_dirty()

def get_fork_at_path(self, path: bytes) -> Optional[Union[Any, MantarayFork]]:
def get_fork_at_path(self, path: bytes) -> Optional[MantarayFork]:
"""
Retrieves a MantarayFork under the given path.
Expand All @@ -375,23 +384,23 @@ def get_fork_at_path(self, path: bytes) -> Optional[Union[Any, MantarayFork]]:
"""
if not path:
raise EmptyPathError()

if self.forks is None:
msg = "Fork mapping is not defined in the manifest"
raise ValueError(msg)

fork = self.forks.get(path[0])
fork: MantarayFork = self.forks.get(path[0])
print(f"{path=}")
if fork is None:
raise NotFoundError(path)
raise NotFoundError(path, fork.prefix)

if path.startswith(fork.prefix):
rest = path[len(fork.prefix) :]
if not rest:
return fork
return fork.node.get_fork_at_path(rest)
else:
raise NotFoundError(path)
raise NotFoundError(path, fork.prefix)

def remove_path(self, path: bytes) -> None:
"""
Expand Down Expand Up @@ -484,7 +493,7 @@ def serialise(self) -> bytes:
reference_len_bytes: bytes = serialise_reference_len(self.__entry)

# ForksIndexBytes
index = IndexBytes()
index: IndexBytes = IndexBytes()
for fork_index in self.forks.keys():
index.set_byte(int(fork_index))
index_bytes = index.get_bytes()
Expand All @@ -495,17 +504,17 @@ def serialise(self) -> bytes:
for byte in range(256):
byte = int(byte)
if index.check_byte_present(byte):
fork = self.forks.get(byte)
fork: MantarayFork = self.forks.get(byte)
if fork is None:
raise Exception(f"Fork indexing error: fork has not found under {byte!r} index")
fork_serialisations += bytearray(fork.serialise())

# print(f"{list(bytearray(self.__obfuscation_key))=}")
# print(f"{list(bytearray(version_bytes))=}")
# print(f"{list(bytearray(reference_len_bytes))=}")
# print(f"{list(bytearray(self.__entry))=}")
# print(f"{list(bytearray(index_bytes))=}")
# print(f"{list(bytearray(fork_serialisations))=}")
# console.print(f"{bytearray(self.__obfuscation_key)=}")
# console.print(f"{version_bytes=}")
# console.print(f"{reference_len_bytes=}")
# console.print(f"{self.__entry=}")
# console.print(f"{index_bytes=}")
# console.print(f"{fork_serialisations=}")

bytes_data = b"".join(
[
Expand All @@ -525,7 +534,6 @@ def serialise(self) -> bytes:
# print(f"{list(bytearray(bytes_data))=}")

return bytes_data
return remove_space_and_add_newlines(bytes_data)

def deserialise(self, data: bytes) -> None:
"""
Expand Down
8 changes: 6 additions & 2 deletions tests/integration/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ async def test_should_generate_same_content_hash_as_bee(bee_class, get_debug_pos
{"Content-Type": "image/png", "Filename": "icon.png"},
)
i_node.add_fork(b"/", bytes(32), {"website-index-document": "index.html"})

console.log(i_node.forks)

save_function = create_save_function(bee_class, get_debug_postage)
i_node_ref = i_node.save(save_function)
Expand Down Expand Up @@ -153,10 +155,11 @@ async def test_construct_manifests_of_testpage_folder(get_debug_postage, bee_cla
upload_file(image_path, get_debug_postage, bee_class),
)
text_reference = upload_data(
bytes([104, 97, 108, 105]), get_debug_postage, bee_class
bytes(bytearray([104, 97, 108, 105])), get_debug_postage, bee_class
)

i_node = MantarayNode()
console.log(i_node)
i_node.add_fork(
b"index.html",
hex_to_bytes(index_reference),
Expand Down Expand Up @@ -188,8 +191,9 @@ async def test_construct_manifests_of_testpage_folder(get_debug_postage, bee_cla
"website-index-document": "index.html",
},
)
console.log(i_node.forks)

save_function = create_save_function(bee_class, get_debug_postage)
# save_function = create_save_function(bee_class, get_debug_postage)
# i_node_ref = i_node.save(save_function)

assert list(i_node.forks.keys())[::-1] == list(node.forks.keys())
Expand Down

0 comments on commit cccde68

Please sign in to comment.