-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add importer and exporter (#2)
Co-authored-by: mokrzesa <[email protected]> Co-authored-by: Sergii Denysiuk <[email protected]>
- Loading branch information
1 parent
f7c1cb3
commit 973ac60
Showing
13 changed files
with
247 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,18 @@ | ||
from .data_io.base_exporter import MerkleTreeExporter | ||
from .data_io.base_importer import MerkleTreeImporter | ||
from .data_io.dto import LeafValueDTO, MerkleTreeDTO | ||
from .data_io.json_exporter import MerkleTreeJSONExporter | ||
from .data_io.json_importer import MerkleTreeJSONImporter | ||
from .trees.binary import BinaryTree | ||
from .trees.merkle import MerkleTree | ||
|
||
__all__ = ["BinaryTree", "MerkleTree"] | ||
__all__ = [ | ||
"BinaryTree", | ||
"MerkleTree", | ||
"LeafValueDTO", | ||
"MerkleTreeDTO", | ||
"MerkleTreeImporter", | ||
"MerkleTreeExporter", | ||
"MerkleTreeJSONImporter", | ||
"MerkleTreeJSONExporter", | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Any | ||
|
||
from .dto import MerkleTreeDTO | ||
|
||
|
||
class MerkleTreeExporter(ABC): | ||
@staticmethod | ||
@abstractmethod | ||
def export_tree(data: MerkleTreeDTO) -> Any: | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Any | ||
|
||
from .dto import MerkleTreeDTO | ||
|
||
|
||
class MerkleTreeImporter(ABC): | ||
@staticmethod | ||
@abstractmethod | ||
def import_tree(data: Any) -> MerkleTreeDTO: | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import Any | ||
|
||
from pydantic import BaseModel, ConfigDict, Field | ||
from pydantic.alias_generators import to_camel | ||
|
||
Leaf = tuple[Any, ...] | ||
|
||
|
||
class LeafValueDTO(BaseModel): | ||
model_config = ConfigDict(alias_generator=to_camel) | ||
|
||
value: Leaf | ||
tree_index: int | ||
|
||
|
||
class MerkleTreeDTO(BaseModel): | ||
model_config = ConfigDict(alias_generator=to_camel) | ||
|
||
format: str = Field("standard-v1", frozen=True) | ||
tree: list[str] | ||
values: list[LeafValueDTO] | ||
leaf_encoding: list[str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from .base_exporter import MerkleTreeExporter | ||
from .dto import MerkleTreeDTO | ||
|
||
|
||
class MerkleTreeJSONExporter(MerkleTreeExporter): | ||
@staticmethod | ||
def export_tree(data: MerkleTreeDTO) -> str: | ||
return data.model_dump_json(by_alias=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from .base_importer import MerkleTreeImporter | ||
from .dto import MerkleTreeDTO | ||
|
||
|
||
class MerkleTreeJSONImporter(MerkleTreeImporter): | ||
@staticmethod | ||
def import_tree(data: str) -> MerkleTreeDTO: | ||
return MerkleTreeDTO.model_validate_json(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
leafs = [ | ||
(123, True), | ||
(71254, False), | ||
(42386, True), | ||
] | ||
|
||
json_dump = """ | ||
{ | ||
"format": "standard-v1", | ||
"tree": [ | ||
"0x045578d5e654c3f10fecce520b17f5b8073630bf780db850721b1a8a5df3b839", | ||
"0x9385965ba65029c2cbdb3782f35b755a76788ef236602b98118ef535cca36e5c", | ||
"0xee7349a2ed7003d5da5bcfdc43253f4a7b757d72ab681f3178469b73087ac3e7", | ||
"0x8b1d412fe317e16a1c98414d61c95cd2e44ba6b16907e90c2b0035cf6261d01d", | ||
"0x39ee1707f21ec11bac8c0d42538c09f71c0fe3ceb0a2f6be012c2769a714af3d" | ||
], | ||
"values": [ | ||
{"value": [123, true], "treeIndex": 2}, | ||
{"value": [71254, false], "treeIndex": 4}, | ||
{"value": [42386, true], "treeIndex": 3} | ||
], | ||
"leafEncoding": ["int256", "bool"] | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from merkle_zeppelin import MerkleTree, MerkleTreeJSONExporter | ||
|
||
from ..utils import remove_whitespaces | ||
from .example import json_dump, leafs | ||
|
||
|
||
def test_json_export() -> None: | ||
# when | ||
tree = MerkleTree(leafs, ["int256", "bool"]) | ||
exported_tree = tree.export_tree(MerkleTreeJSONExporter) | ||
|
||
# then | ||
assert remove_whitespaces(exported_tree) == remove_whitespaces(json_dump) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import json | ||
|
||
from merkle_zeppelin import MerkleTree, MerkleTreeJSONImporter | ||
|
||
from ..utils import remove_0x | ||
from .example import json_dump, leafs | ||
|
||
|
||
def test_json_import() -> None: | ||
# given | ||
tree_input = json.loads(json_dump) | ||
|
||
# when | ||
tree = MerkleTree.import_tree(json_dump, MerkleTreeJSONImporter, validate=True) | ||
|
||
# then | ||
assert tree.root.hex() == remove_0x(tree_input["tree"][0]) | ||
assert len(tree._nodes) == (len(leafs) * 2) - 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import string | ||
|
||
|
||
def remove_0x(data: str) -> str: | ||
return data[2:] if data.startswith("0x") else data | ||
|
||
|
||
def remove_whitespaces(data: str) -> str: | ||
return data.translate(str.maketrans("", "", string.whitespace)) |