Skip to content

Commit

Permalink
fix: deserialised method fixed.fixed all mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviksaikat committed Jun 12, 2024
1 parent f358388 commit 7088456
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 70 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@
## Version 0.0.1 (development)

- First pre-alpha
- ...

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

## [unreleased]

### 🐛 Bug Fixes

- Serialization method fixed
- Trying to fix deserialization

3 changes: 2 additions & 1 deletion src/mantaray_py/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Mantaray data structure in Python"""

from importlib.metadata import PackageNotFoundError, version
from typing import Optional

from rich.traceback import install

Expand Down Expand Up @@ -48,7 +49,7 @@
install()


def init_manifest_node(options: dict = None) -> MantarayNode:
def init_manifest_node(options: Optional[dict] = None) -> MantarayNode:
"""
Initializes a MantarayNode with an optional obfuscation key.
Expand Down
78 changes: 25 additions & 53 deletions src/mantaray_py/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class MantarayNode(BaseModel):
__entry: Optional[Reference] = None
__metadata: Optional[MetadataMapping] = None
# * Forks of the manifest. Has to be initialized with `{}` on load even if there were no forks
forks: Optional[ForkMapping] = {}
forks: Optional[ForkMapping] = None

model_config = ConfigDict(arbitrary_types_allowed=True)

Expand Down Expand Up @@ -432,42 +432,6 @@ def serialise(self) -> bytes:
Returns:
- bytes: serialised byte array representation of the node.
"""
self.__obfuscation_key = bytes(
[
156,
128,
64,
188,
244,
62,
116,
105,
22,
191,
202,
221,
147,
40,
177,
0,
240,
247,
143,
145,
55,
237,
74,
82,
201,
163,
82,
32,
113,
207,
196,
83,
]
)
if not self.__obfuscation_key:
self.set_obfuscation_key(bytes(32))
# self.set_obfuscation_key(
Expand All @@ -491,18 +455,21 @@ def serialise(self) -> bytes:
for fork_index in self.forks.keys():
index.set_byte(int(fork_index))
index_bytes = index.get_bytes()

# Forks
fork_serializations: bytearray = []
fork_serializations: bytearray = bytearray([])

# for byte in index_bytes:
# byte_index = int(byte)
# fork = self.forks.get(byte_index)
def process_byte(byte: int) -> None:
byte_index = int(byte)
fork = self.forks.get(byte_index) # type: ignore

# if fork is None:
# msg = f"Fork indexing error: fork has not found under {byte} index"
# # raise ValueError(msg)
# else:
# fork_serializations.append(fork.serialise())
if fork is None:
msg = f"Fork indexing error: fork has not found under {byte!r} index"
raise ValueError(msg)
else:
fork_serializations.append(fork.serialise())

index.for_each(process_byte)

# print(f"{list(bytearray(self.__obfuscation_key))=}")
# print(f"{list(bytearray(version_bytes))=}")
Expand All @@ -524,7 +491,7 @@ def serialise(self) -> bytes:

# Encryption
# perform XOR encryption on bytes after obfuscation key
encrypt_decrypt(self.__obfuscation_key, bytes_data, len(self.__obfuscation_key)) # type: ignore
encrypt_decrypt(self.__obfuscation_key, bytes_data, len(self.__obfuscation_key))

return bytes_data

Expand Down Expand Up @@ -576,10 +543,13 @@ def deserialise(self, data: bytes) -> None:
offset += 32
node_fork_sizes: NodeForkSizes = NodeForkSizes()

print(index_forks)
def process_byte(byte: int) -> None:
# Use nonlocal to modify the offset in the outer scope
nonlocal offset
# print(f"Processing byte: {byte}")

for byte in index_forks:
if len(data) < offset + node_fork_sizes.node_type:
# print(f"Data Length: {len(data)}, Offset: {offset}, Node Fork Size: {node_fork_sizes.node_type}")
msg = f"There is not enough size to read nodeType of fork at offset {offset}"
raise ValueError(msg)

Expand All @@ -599,7 +569,7 @@ def deserialise(self, data: bytes) -> None:

fork = MantarayFork.deserialise(
data[offset : offset + node_fork_size],
self.__obfuscation_key,
self.__obfuscation_key, # type: ignore
{
"with_metadata": {
"ref_bytes_size": ref_bytes_size,
Expand All @@ -612,11 +582,13 @@ def deserialise(self, data: bytes) -> None:
msg = f"There is not enough size to read fork at offset {offset}"
raise ValueError(msg)

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

self.forks[byte] = fork
self.forks[byte] = fork # type: ignore

offset += node_fork_size

index_forks.for_each(process_byte)
else:
msg = "Wrong mantaray version"
raise ValueError(msg)
Expand Down Expand Up @@ -768,7 +740,7 @@ def serialise_reference_len(entry: Reference) -> bytes:
raise ValueError(msg)

# serialise the reference length into a single byte
byte_array = (reference_len).to_bytes(reference_len, byteorder="big", signed=False) # type: ignore
byte_array = (reference_len).to_bytes(reference_len, byteorder="big", signed=False)
# Remove leading and trailing zeros
return bytearray(byte_array.strip(b"\x00"))

Expand Down
8 changes: 4 additions & 4 deletions src/mantaray_py/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Callable, Optional, Union

from eth_utils import keccak # type: ignore
from eth_utils import keccak
from pydantic import BaseModel, ConfigDict, Field

from mantaray_py.types import Reference, get_random_values
Expand Down Expand Up @@ -30,7 +30,7 @@ def check_byte_present(self, byte: int) -> bool:
"""Check if a byte is present."""
return (self.bytes_data[byte // 8] >> byte % 8) & 1 > 0

def foreach(self, hook: Callable[[int], None]) -> None:
def for_each(self, hook: Callable[[int], None]) -> None:
"""Iterate through the indexed byte values."""
for i in range(256):
if self.check_byte_present(i):
Expand Down Expand Up @@ -181,7 +181,7 @@ def encrypt_decrypt(
encryption_chunk = data[i:encryption_chunk_end_index]
for j in range(len(encryption_chunk)):
encryption_chunk[j] ^= key[j % len(key)]
data[i:encryption_chunk_end_index] = encryption_chunk # type: ignore
data[i:encryption_chunk_end_index] = encryption_chunk
return data


Expand All @@ -205,7 +205,7 @@ def keccak256_hash(*messages: Union[str, bytes, bytearray]) -> bytes:
raise TypeError(msg)
combined += message

return keccak(combined) # type: ignore
return keccak(combined)


def gen_32_bytes() -> bytes:
Expand Down
13 changes: 2 additions & 11 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@

def test_single_manaray_node_with_a_random_address():
node = init_manifest_node()
# random_address = gen_32_bytes()
random_address = bytes([
229, 5, 247, 157, 211, 167, 196, 164,
82, 13, 129, 139, 75, 95, 58, 43,
188, 41, 52, 27, 52, 221, 242, 140,
150, 81, 189, 90, 184, 120, 19, 31
])
# console.print(random_address)

random_address = gen_32_bytes()
node.set_entry(random_address)

serialised = node.serialise()
# print(list(serialised))

new_node = MantarayNode()
new_node.deserialise(serialised)

Expand Down

0 comments on commit 7088456

Please sign in to comment.