Skip to content

Commit

Permalink
Add helper functions to IncrementalMerkleTree.
Browse files Browse the repository at this point in the history
  • Loading branch information
ebfull committed Dec 31, 2015
1 parent f756c36 commit 0c062e7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion libzerocash/IncrementalMerkleTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace libzerocash {
return serialized;
}

IncrementalMerkleTreeCompact IncrementalMerkleTreeCompact::Deserialize(std::vector<unsigned char> serialized) {
IncrementalMerkleTreeCompact IncrementalMerkleTreeCompact::deserialize(const std::vector<unsigned char>& serialized) {
IncrementalMerkleTreeCompact deserialized;

size_t currentPos = 0;
Expand Down
11 changes: 10 additions & 1 deletion libzerocash/IncrementalMerkleTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class IncrementalMerkleTreeCompact {
std::vector< bool > const& getHashList() { return hashList; }

std::vector<unsigned char> serialize();
static IncrementalMerkleTreeCompact Deserialize(std::vector<unsigned char> serialized);
static IncrementalMerkleTreeCompact deserialize(const std::vector<unsigned char>& serialized);

private:
IncrementalMerkleTreeCompact() : treeHeight(0) {}
Expand Down Expand Up @@ -115,6 +115,15 @@ class IncrementalMerkleTree {
std::vector<unsigned char>getRoot();
bool prune();
IncrementalMerkleTreeCompact getCompactRepresentation();
std::vector<unsigned char> serialize() {
auto compact = getCompactRepresentation();
return compact.serialize();
}

static IncrementalMerkleTree deserialize(const std::vector<unsigned char>& serialized) {
auto deserialized = IncrementalMerkleTreeCompact::deserialize(serialized);
return IncrementalMerkleTree(deserialized);

This comment has been minimized.

Copy link
@nathan-at-least

nathan-at-least Jan 14, 2016

I just remembered we need explicit on all ctor's which may be called with exactly one argument. This is one example. See https://github.com/Electric-Coin-Company/zcash/issues/100

}

bool fromCompactRepresentation(IncrementalMerkleTreeCompact &rep);

Expand Down
6 changes: 3 additions & 3 deletions tests/merkleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ BOOST_AUTO_TEST_CASE( testCompactRepresentation ) {

/* Test serializing and deserializing. */
std::vector<unsigned char> serializedCompact = compact.serialize();
IncrementalMerkleTreeCompact deserializedCompact = IncrementalMerkleTreeCompact::Deserialize(serializedCompact);
IncrementalMerkleTreeCompact deserializedCompact = IncrementalMerkleTreeCompact::deserialize(serializedCompact);
BOOST_REQUIRE(compact.getTreeHeight() == deserializedCompact.getTreeHeight());
BOOST_REQUIRE(compact.getHashList() == deserializedCompact.getHashList());
BOOST_REQUIRE(compact.getHashVec() == deserializedCompact.getHashVec());
Expand Down Expand Up @@ -138,7 +138,7 @@ BOOST_AUTO_TEST_CASE( testCompactDeserializationFailures ) {
for (size_t trunc_len = 0; trunc_len < serialized.size(); trunc_len++) {
std::vector<unsigned char> truncated(serialized.begin(), serialized.begin() + trunc_len);
BOOST_CHECK_THROW(
IncrementalMerkleTreeCompact::Deserialize(truncated),
IncrementalMerkleTreeCompact::deserialize(truncated),
std::out_of_range
);
}
Expand All @@ -147,7 +147,7 @@ BOOST_AUTO_TEST_CASE( testCompactDeserializationFailures ) {
std::vector<unsigned char> extra_byte = serialized;
extra_byte.push_back(0x00);
BOOST_CHECK_THROW(
IncrementalMerkleTreeCompact::Deserialize(extra_byte),
IncrementalMerkleTreeCompact::deserialize(extra_byte),
std::runtime_error
);
}

2 comments on commit 0c062e7

@ebfull
Copy link
Author

@ebfull ebfull commented on 0c062e7 Jan 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I ended up using these helpers anyway...

@nathan-at-least
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, unless they aren't used elsewhere in which case dead code should be removed. We generally have a serious need for coverage analysis. Added https://github.com/Electric-Coin-Company/zcash/issues/607 as future task.

Please sign in to comment.