merkle-tree-stream/merkle_tree_stream/tree.py

110 lines
2.9 KiB
Python
Raw Normal View History

2019-08-07 05:58:26 +00:00
"""A merkle tree iterator."""
2019-08-07 05:58:26 +00:00
from typing import Any, Callable, Iterator, List
import attr
from flat_tree import FlatTreeAccessor
2019-08-07 05:58:26 +00:00
from merkle_tree_stream.node import MerkleTreeNode
2019-08-07 05:58:26 +00:00
EMPTY_DATA = b''
EMPTY_HASH = None
2019-08-07 05:58:26 +00:00
__all__ = ['MerkleTreeIterator']
2019-08-07 05:58:26 +00:00
flat_tree = FlatTreeAccessor()
@attr.s(auto_attribs=True)
class MerkleTreeIterator:
2019-08-07 05:58:26 +00:00
"""A merkle tree iterator.
:param leaf: The leaf hash generation function
:param parent: The parent hash generation function
:param roots: The tree roots
"""
leaf: Callable[[MerkleTreeNode], bytes]
parent: Callable[[MerkleTreeNode, MerkleTreeNode], bytes]
roots: List[MerkleTreeNode] = attr.Factory(list)
_position: int = 0
_nodes: List[MerkleTreeNode] = attr.Factory(list)
def __attrs_post_init__(self) -> Any:
"""Initialise parent and block defaults."""
try:
index = self.roots[len(self.roots) - 1].index
except IndexError:
index = 0
right_span = flat_tree.right_span(index)
self.blocks = (1 + (right_span / 2)) if self.roots else 0
for root in self.roots:
if not root.parent:
root.parent = flat_tree.parent(root.index)
def __iter__(self) -> Iterator:
"""The iterator initialisation."""
return self
def __next__(self) -> MerkleTreeNode:
"""The following node."""
try:
node = self._nodes[self._position]
except IndexError:
raise StopIteration
self._position += 1
return node
def __len__(self) -> int:
"""The number of nodes stored in the tree."""
return len(self._nodes)
2019-08-07 05:58:26 +00:00
# TODO(decentral1se): we need to take pass on async capability. Please see
# https://datprotocol.github.io/book/ch02-02-merkle-tree-stream.html#async
def write(self, data: bytes):
2019-08-07 05:58:26 +00:00
"""Write a new node to the tree and compute the new hashes.
:param data: The new tree data
"""
index = 2 * self.blocks
self.blocks += 1
leaf_node = MerkleTreeNode(
index=index,
parent=flat_tree.parent(index),
2019-08-07 05:58:26 +00:00
hash=EMPTY_HASH,
data=data,
size=len(data),
)
leaf_node.hash = self.leaf(leaf_node)
self.roots.append(leaf_node)
self._nodes.append(leaf_node)
while len(self.roots) > 1:
left = self.roots[len(self.roots) - 2]
right = self.roots[len(self.roots) - 1]
if left.parent != right.parent:
break
self.roots.pop()
new_node = MerkleTreeNode(
index=left.parent,
parent=flat_tree.parent(left.parent),
hash=self.parent(left, right),
size=left.size + right.size,
2019-08-07 05:58:26 +00:00
data=EMPTY_DATA,
)
self.roots[len(self.roots) - 1] = new_node
self._nodes.append(new_node)