Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INDY-1137] Making proof possible for given root hash #575

Merged
merged 3 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions plenum/server/domain_req_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def getNymDetails(state, nym, isCommitted: bool=True):
def nym_to_state_key(nym: str) -> bytes:
return sha256(nym.encode()).digest()

def make_proof(self, path):
def make_proof(self, path, head_hash=None):
'''
Creates a state proof for the given path in state trie.
Returns None if there is no BLS multi-signature for the given state (it can
Expand All @@ -171,10 +171,10 @@ def make_proof(self, path):
:param path: the path generate a state proof for
:return: a state proof or None
'''
root_hash = head_hash if head_hash else self.state.committedHeadHash
proof = self.state.generate_state_proof(key=path,
root=self.state.committedHead,
root=self.state.get_head_by_hash(root_hash),
serialize=True)
root_hash = self.state.committedHeadHash
encoded_proof = proof_nodes_serializer.serialize(proof)
encoded_root_hash = state_roots_serializer.serialize(bytes(root_hash))

Expand Down
22 changes: 22 additions & 0 deletions plenum/test/bls/test_make_proof.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from base58 import b58encode
from plenum.test.pool_transactions.conftest import looper
from plenum.test.helper import sdk_send_random_and_check
from plenum.common.types import f
from plenum.common.constants import ROOT_HASH


def test_make_proof(looper, sdk_wallet_steward, sdk_pool_handle, txnPoolNodeSet):
node = txnPoolNodeSet[0]
req_handler = node.getDomainReqHandler()
req1, _ = sdk_send_random_and_check(looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_steward, 1)[0]
# Save headHash after first request
head1 = req_handler.state.headHash
sdk_send_random_and_check(looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_steward, 1)
# Save headHash after second request
head2 = req_handler.state.headHash
# Build path to first request
path1 = req_handler.prepare_buy_key(req1[f.IDENTIFIER.nm], req1[f.REQ_ID.nm])
# Check that if parameter "head_hash" is None, then we make proof for commitedHeadHash (by default)
assert b58encode(head2) == req_handler.make_proof(path1)[ROOT_HASH]
# Check that if parameter "head_hash" is not None, then we make proof for given headHash
assert b58encode(head1) == req_handler.make_proof(path1, head_hash=head1)[ROOT_HASH]
4 changes: 4 additions & 0 deletions state/pruning_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def committedHead(self):
# head is the root
return self._hash_to_node(self.committedHeadHash)

def get_head_by_hash(self, root_hash):
# return node of a merkle tree by given hash
return self._hash_to_node(root_hash)

def _hash_to_node(self, node_hash):
if node_hash == BLANK_ROOT:
return BLANK_NODE
Expand Down