From a293d4badafc37e8aeb63aa7c23d944e93bc32f1 Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Tue, 8 Oct 2019 22:26:24 +0200 Subject: [PATCH 1/3] Move naming to Generator (reflect reality) Closes https://github.com/datpy/merkle-tree-stream/issues/4. --- CHANGELOG.rst | 9 ++++++++ README.rst | 22 +++++++++---------- merkle_tree_stream/__init__.py | 2 +- merkle_tree_stream/tree.py | 8 +++---- test/test_tree.py | 40 +++++++++++++++++----------------- 5 files changed, 44 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cff2059..b904c2c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,12 @@ +Merkle_Tree_Stream 0.0.1a3 (2019-10-08) +======================================= + +Bug Fixes +--------- + +- MerkleTreeIterator -> MerkleTreeGenerator + + Merkle_Tree_Stream 0.0.1a2 (2019-10-06) ======================================= diff --git a/README.rst b/README.rst index becd9a9..414a684 100644 --- a/README.rst +++ b/README.rst @@ -52,22 +52,20 @@ A note on naming ================ For the purposes of uniformity and easy of discovery alongside the reference -implementation, we use the same module name as `merkle-tree-stream`_. This may -cause confusion since it is not clear what exactly is referred to when using -the term "stream" in the context of Python. To be clear, this module provides a -`Python iterator`_ which appears to match the implementation and meaning of the -reference implementation. +implementation, we use the same module name as `merkle-tree-stream`_. However, +there is currently no stream implemented, only a generator is available. This +is because the reference implementation of Hypercore 7 makes use of the +generator only. A `stream`_ implementation may follow. .. _merkle-tree-stream: https://github.com/mafintosh/merkle-tree-stream -.. _Python iterator: https://docs.python.org/3/c-api/iter.html +.. _stream: https://docs.python.org/3/library/asyncio-stream.html .. _example: .. code-block:: python from hashlib import sha256 - from merkle_tree_stream import MerkleTreeIterator - + from merkle_tree_stream import MerkleTreeGenerator def leaf(node, roots=None): return sha256(node.data).digest() @@ -78,10 +76,10 @@ reference implementation. sha256.update(second.data) return sha256.digest() - merkle_iter = MerkleTreeIterator(leaf=leaf, parent=parent) - merkle_iter.write('hello') - merkle_iter.write('hashed') - merkle_iter.write('world') + merkle = MerkleTreeGenerator(leaf=leaf, parent=parent) + merkle.write('hello') + merkle.write('hashed') + merkle.write('world') .. _documentation: diff --git a/merkle_tree_stream/__init__.py b/merkle_tree_stream/__init__.py index a23b817..9465804 100644 --- a/merkle_tree_stream/__init__.py +++ b/merkle_tree_stream/__init__.py @@ -1,7 +1,7 @@ """merkle-tree-stream module.""" from merkle_tree_stream.node import MerkleTreeNode # noqa -from merkle_tree_stream.tree import MerkleTreeIterator # noqa +from merkle_tree_stream.tree import MerkleTreeGenerator # noqa try: import pkg_resources diff --git a/merkle_tree_stream/tree.py b/merkle_tree_stream/tree.py index 930af05..fda0198 100644 --- a/merkle_tree_stream/tree.py +++ b/merkle_tree_stream/tree.py @@ -1,4 +1,4 @@ -"""A merkle tree iterator.""" +"""A merkle tree generator.""" from typing import Any, Callable, Iterator, List @@ -10,14 +10,14 @@ from merkle_tree_stream.node import MerkleTreeNode EMPTY_DATA = b'' EMPTY_HASH = None -__all__ = ['MerkleTreeIterator'] +__all__ = ['MerkleTreeGenerator'] flat_tree = FlatTreeAccessor() @attr.s(auto_attribs=True) -class MerkleTreeIterator: - """A merkle tree iterator. +class MerkleTreeGenerator: + """A merkle tree generator. :param leaf: The leaf hash generation function :param parent: The parent hash generation function diff --git a/test/test_tree.py b/test/test_tree.py index 6953296..23c0923 100644 --- a/test/test_tree.py +++ b/test/test_tree.py @@ -4,53 +4,53 @@ import hashlib import pytest -from merkle_tree_stream import MerkleTreeIterator, MerkleTreeNode +from merkle_tree_stream import MerkleTreeGenerator, MerkleTreeNode def test_hashes(leaf, parent): - merkle_iter = MerkleTreeIterator(leaf=leaf, parent=parent) + merkle = MerkleTreeGenerator(leaf=leaf, parent=parent) - merkle_iter.write(b'a') - merkle_iter.write(b'b') + merkle.write(b'a') + merkle.write(b'b') expected_count = 2 + 1 # nodes plus parent - assert len(merkle_iter) == expected_count + assert len(merkle) == expected_count - assert next(merkle_iter) == MerkleTreeNode( + assert next(merkle) == MerkleTreeNode( index=0, parent=1, hash=hashlib.sha256(b'a').digest(), size=1, data=b'a' ) - assert next(merkle_iter) == MerkleTreeNode( + assert next(merkle) == MerkleTreeNode( index=2, parent=1, hash=hashlib.sha256(b'b').digest(), size=1, data=b'b' ) hashed = hashlib.sha256(b'a') hashed.update(b'b') - assert next(merkle_iter) == MerkleTreeNode( + assert next(merkle) == MerkleTreeNode( index=1, parent=3, hash=hashed.digest(), size=2, data=b'' ) with pytest.raises(StopIteration): - next(merkle_iter) + next(merkle) def test_single_root(leaf, parent): - merkle_iter = MerkleTreeIterator(leaf=leaf, parent=parent) + merkle = MerkleTreeGenerator(leaf=leaf, parent=parent) - merkle_iter.write(b'a') - merkle_iter.write(b'b') - merkle_iter.write(b'c') - merkle_iter.write(b'd') + merkle.write(b'a') + merkle.write(b'b') + merkle.write(b'c') + merkle.write(b'd') - assert len(merkle_iter.roots) == 1 + assert len(merkle.roots) == 1 def multiple_roots(leaf, parent): - merkle_iter = MerkleTreeIterator(leaf=leaf, parent=parent) + merkle = MerkleTreeGenerator(leaf=leaf, parent=parent) - merkle_iter.write(b'a') - merkle_iter.write(b'b') - merkle_iter.write(b'c') + merkle.write(b'a') + merkle.write(b'b') + merkle.write(b'c') - assert len(merkle_iter.roots) > 1 + assert len(merkle.roots) > 1 From 9a651a9d3d33529bc5ac2e7b719967538247e8cc Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Tue, 8 Oct 2019 22:26:47 +0200 Subject: [PATCH 2/3] Clean up formatting on CoC --- CODE_OF_CONDUCT.rst | 46 ++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/CODE_OF_CONDUCT.rst b/CODE_OF_CONDUCT.rst index c211c44..f0a45cf 100644 --- a/CODE_OF_CONDUCT.rst +++ b/CODE_OF_CONDUCT.rst @@ -1,28 +1,28 @@ Introduction ------------ -The DatPy community is committed to providing an inclusive, -safe, and collaborative environment for all participants, regardless of their -gender, gender expression, race, ethnicity, religion, sexual orientation, -sexual characteristics, physical appearance, disability, or age. We encourage -every participant to be themselves, and must respect the rights of others. The -code of conduct is a set of guidelines that establishes shared values and -ensures that behaviors that may harm participants are avoided. +The DatPy community is committed to providing an inclusive, safe, and +collaborative environment for all participants, regardless of their gender, +gender expression, race, ethnicity, religion, sexual orientation, sexual +characteristics, physical appearance, disability, or age. We encourage every +participant to be themselves, and must respect the rights of others. The code +of conduct is a set of guidelines that establishes shared values and ensures +that behaviors that may harm participants are avoided. -The values of the DatPy community are focused on developing -both our individual and collective potential, supporting and empowering the -most marginalized, mutual respect, and an anti-violence approach that favors -support and collaboration among participants and the resolution of conflicts. A -code of conduct helps us co-exist in a more positive way and provides -individuals who are victims of negative behaviors with confidence that they -will be supported by the organization and the DatPy -community, who respects and stands behind the code of conduct. +The values of the DatPy community are focused on developing both our individual +and collective potential, supporting and empowering the most marginalized, +mutual respect, and an anti-violence approach that favors support and +collaboration among participants and the resolution of conflicts. A code of +conduct helps us co-exist in a more positive way and provides individuals who +are victims of negative behaviors with confidence that they will be supported +by the organization and the DatPy community, who respects and stands behind the +code of conduct. -The DatPy community works towards providing a welcoming -environment where participants are treated with dignity and respect and are -free to be themselves. We encourage all participants to approach the -Librehosters network with an open and positive attitude, engaging -constructively with others at all times. +The DatPy community works towards providing a welcoming environment where +participants are treated with dignity and respect and are free to be +themselves. We encourage all participants to approach the Librehosters network +with an open and positive attitude, engaging constructively with others at all +times. Respect for Diversity & Inclusion --------------------------------- @@ -92,9 +92,9 @@ Enforcement Overseeing the code of conduct ============================== -The DatPy community, composed of volunteers, oversees the -code of conduct, including addressing all incident reports. Breaking the code -of conduct may result in immediate expulsion from the Librehosters network. +The DatPy community, composed of volunteers, oversees the code of conduct, +including addressing all incident reports. Breaking the code of conduct may +result in immediate expulsion from the Librehosters network. How to Report an Incident ========================= From c470b707221c1a1a4a4f9ea00f4efec18f220659 Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Tue, 8 Oct 2019 22:26:55 +0200 Subject: [PATCH 3/3] Remove base python restrictions --- tox.ini | 2 -- 1 file changed, 2 deletions(-) diff --git a/tox.ini b/tox.ini index e2c5b31..ef1705a 100644 --- a/tox.ini +++ b/tox.ini @@ -34,13 +34,11 @@ commands = isort {posargs:-rc -c} -sp setup.cfg merkle_tree_stream/ test/ [testenv:format] description = format the source skipdist = True -basepython = python3.6 deps = black commands = black {posargs:--check} merkle_tree_stream/ test/ [testenv:type] description = type check the source -basepython = python3.7 skipdist = True deps = mypy commands = mypy merkle_tree_stream/ test/