2019-07-14 11:14:02 +00:00
|
|
|
"""The merkle tree stream generator."""
|
|
|
|
|
|
|
|
from typing import Any, Callable, List, Optional
|
|
|
|
|
|
|
|
import attr
|
|
|
|
from flat_tree import FlatTreeAccessor
|
|
|
|
|
|
|
|
Hash = str
|
|
|
|
|
2019-08-01 11:06:32 +00:00
|
|
|
__all__ = ['MerkleTreeGenerator', 'MerkleTreeNode']
|
|
|
|
|
2019-07-14 11:14:02 +00:00
|
|
|
flat_tree = FlatTreeAccessor()
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s(auto_attribs=True)
|
|
|
|
class MerkleTreeNode:
|
|
|
|
"""A node in a merkle tree.
|
|
|
|
|
2019-08-01 11:06:32 +00:00
|
|
|
:param index: The index of node
|
|
|
|
:param parent: The parent of the node
|
|
|
|
:param size: The size of the data
|
|
|
|
:param data: The data of the node
|
|
|
|
:param hash: The hash of the data
|
2019-07-14 11:14:02 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
index: int
|
2019-08-01 11:06:32 +00:00
|
|
|
parent: int
|
2019-07-14 11:14:02 +00:00
|
|
|
size: int
|
2019-08-01 11:06:32 +00:00
|
|
|
data: Optional[bytes]
|
2019-07-14 11:14:02 +00:00
|
|
|
hash: Optional[str] = None
|
|
|
|
|
|
|
|
def __attrs_post_init__(self) -> Any:
|
|
|
|
"""Initialise the parent index."""
|
|
|
|
self.parent = flat_tree.parent(self.index)
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s(auto_attribs=True)
|
|
|
|
class MerkleTreeGenerator:
|
|
|
|
"""A stream that generates a merkle tree based on the incoming data.
|
|
|
|
|
2019-08-01 11:06:32 +00:00
|
|
|
:param leaf: The leaf hash generation function
|
|
|
|
:param parent: The parent hash generation function
|
|
|
|
:param roots: The tree roots
|
2019-07-14 11:14:02 +00:00
|
|
|
"""
|
|
|
|
|
2019-08-01 11:06:32 +00:00
|
|
|
leaf: Callable[[MerkleTreeNode, List[MerkleTreeNode]], Hash]
|
|
|
|
parent: Callable[[MerkleTreeNode, List[MerkleTreeNode]], Hash]
|
|
|
|
roots: List[MerkleTreeNode] = attr.Factory(list)
|
2019-07-14 11:14:02 +00:00
|
|
|
|
2019-08-01 11:06:32 +00:00
|
|
|
def next(
|
|
|
|
self, data: bytes, nodes: Optional[List[MerkleTreeNode]] = None
|
|
|
|
) -> List[MerkleTreeNode]:
|
|
|
|
"""Further generate the tree based on the incoming data.
|
2019-07-14 11:14:02 +00:00
|
|
|
|
|
|
|
:param data: Incoming data
|
2019-08-01 11:06:32 +00:00
|
|
|
:param nodes: Pre-existing nodes
|
2019-07-14 11:14:02 +00:00
|
|
|
"""
|
2019-08-01 11:06:32 +00:00
|
|
|
nodes = nodes or []
|
|
|
|
|
|
|
|
index = 2 * (self.blocks + 1)
|
|
|
|
|
|
|
|
leaf_node = MerkleTreeNode(
|
|
|
|
index=index,
|
|
|
|
parent=flat_tree.parent(index),
|
|
|
|
hash=None,
|
|
|
|
data=data,
|
|
|
|
size=len(data),
|
|
|
|
)
|
|
|
|
|
|
|
|
leaf_node.hash = self.leaf(leaf_node, self.roots)
|
|
|
|
|
|
|
|
self.roots.append(leaf_node)
|
|
|
|
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,
|
|
|
|
data=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.roots[len(self.roots) - 1] = new_node
|
|
|
|
|
|
|
|
nodes.append(new_node)
|
|
|
|
|
|
|
|
return nodes
|
2019-07-14 11:14:02 +00:00
|
|
|
|
|
|
|
def __attrs_post_init__(self) -> Any:
|
|
|
|
"""Initialise parent and block defaults."""
|
2019-08-01 11:06:32 +00:00
|
|
|
index = self.roots[len(self.roots) - 1].index
|
|
|
|
right_span = flat_tree.right_span(index)
|
|
|
|
self.blocks = (1 + (right_span / 2)) if self.roots else 0
|
|
|
|
|
2019-07-14 11:14:02 +00:00
|
|
|
for root in self.roots:
|
|
|
|
if not root.parent:
|
|
|
|
root.parent = flat_tree.parent(root.index)
|