-
Notifications
You must be signed in to change notification settings - Fork 21
Add direct coverage for serialization helpers and tree boundaries #32
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
Merged
+368
−9
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
60f1513
Add coverage tests for serialization and tree semantics
Copilot e36939b
Fix coverage test build issues
Copilot 97c78f0
Format coverage test
Copilot 842776c
Deduplicate partial range validation
Copilot 7744b93
Clarify partial range validation
Copilot df1e73a
Document self-assignment coverage
Copilot 05797c0
Fix CI compiler warnings and TreeT assignment safety
Copilot 3f1edbb
Add safe TreeT move assignment and finalize coverage fixes
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -497,7 +497,10 @@ namespace merkle | |
| /// @brief Equality operator for paths | ||
| bool operator==(const PathT<HASH_SIZE, HASH_FUNCTION>& other) const | ||
| { | ||
| if (_leaf != other._leaf || elements.size() != other.elements.size()) | ||
| if ( | ||
| _leaf != other._leaf || _leaf_index != other._leaf_index || | ||
| _max_index != other._max_index || | ||
| elements.size() != other.elements.size()) | ||
| { | ||
| return false; | ||
| } | ||
|
|
@@ -516,7 +519,7 @@ namespace merkle | |
| } | ||
|
|
||
| /// @brief Inequality operator for paths | ||
| bool operator!=(const PathT<HASH_SIZE, HASH_FUNCTION>& other) | ||
| bool operator!=(const PathT<HASH_SIZE, HASH_FUNCTION>& other) const | ||
| { | ||
| return !this->operator==(other); | ||
| } | ||
|
|
@@ -727,7 +730,15 @@ namespace merkle | |
| insertion_stack(std::move(other.insertion_stack)), | ||
| hashing_stack(std::move(other.hashing_stack)), | ||
| walk_stack(std::move(other.walk_stack)) | ||
| {} | ||
| { | ||
| other.leaf_nodes.clear(); | ||
| other.uninserted_leaf_nodes.clear(); | ||
| other._root = nullptr; | ||
| other.num_flushed = 0; | ||
| other.insertion_stack.clear(); | ||
| other.hashing_stack.clear(); | ||
| other.walk_stack.clear(); | ||
| } | ||
|
|
||
| /// @brief Deserialises a tree | ||
| /// @param bytes Byte buffer containing a serialised tree | ||
|
|
@@ -941,6 +952,10 @@ namespace merkle | |
| /// @return The tree | ||
| Tree& operator=(const Tree& other) | ||
| { | ||
| if (this == &other) | ||
| { | ||
| return *this; | ||
| } | ||
| leaf_nodes.clear(); | ||
| for (auto n : uninserted_leaf_nodes) | ||
| { | ||
|
|
@@ -950,6 +965,8 @@ namespace merkle | |
| insertion_stack.clear(); | ||
| hashing_stack.clear(); | ||
| walk_stack.clear(); | ||
| delete (_root); | ||
| _root = nullptr; | ||
|
|
||
| size_t to_skip = (other.num_flushed % 2 == 0) ? 0 : 1; | ||
| _root = Node::copy_node( | ||
|
|
@@ -968,6 +985,46 @@ namespace merkle | |
| return *this; | ||
| } | ||
|
|
||
| /// @brief Assigns a tree by move | ||
| /// @param other The tree to assign | ||
| /// @return The tree | ||
| Tree& operator=(Tree&& other) noexcept | ||
| { | ||
| if (this == &other) | ||
| { | ||
| return *this; | ||
| } | ||
|
|
||
| leaf_nodes.clear(); | ||
| for (auto n : uninserted_leaf_nodes) | ||
| { | ||
| delete (n); | ||
| } | ||
| uninserted_leaf_nodes.clear(); | ||
| insertion_stack.clear(); | ||
| hashing_stack.clear(); | ||
| walk_stack.clear(); | ||
| delete (_root); | ||
| _root = nullptr; | ||
|
|
||
| leaf_nodes = std::move(other.leaf_nodes); | ||
| uninserted_leaf_nodes = std::move(other.uninserted_leaf_nodes); | ||
| _root = other._root; | ||
| num_flushed = other.num_flushed; | ||
| insertion_stack = std::move(other.insertion_stack); | ||
| hashing_stack = std::move(other.hashing_stack); | ||
| walk_stack = std::move(other.walk_stack); | ||
|
|
||
| other.leaf_nodes.clear(); | ||
| other.uninserted_leaf_nodes.clear(); | ||
| other._root = nullptr; | ||
| other.num_flushed = 0; | ||
| other.insertion_stack.clear(); | ||
| other.hashing_stack.clear(); | ||
| other.walk_stack.clear(); | ||
| return *this; | ||
| } | ||
|
|
||
| /// @brief Extracts the root hash of the tree | ||
| /// @return The root hash | ||
| const Hash& root() | ||
|
|
@@ -1337,12 +1394,7 @@ namespace merkle | |
| MERKLECPP_TRACE(MERKLECPP_TOUT << "> serialise from " << from << " to " | ||
| << to << std::endl;); | ||
|
|
||
| if ( | ||
| (from < min_index() || max_index() < from) || | ||
| (to < min_index() || max_index() < to) || from > to) | ||
| { | ||
| throw std::runtime_error("invalid leaf indices"); | ||
| } | ||
| validate_partial_range(from, to); | ||
|
|
||
| serialise_uint64_t(to - from + 1, bytes); | ||
| serialise_uint64_t(from, bytes); | ||
|
|
@@ -1577,6 +1629,8 @@ namespace merkle | |
| /// @return The number of bytes required to serialise the tree segment | ||
| size_t serialised_size(size_t from, size_t to) | ||
| { | ||
| validate_partial_range(from, to); | ||
|
|
||
| size_t num_extras = 0; | ||
| walk_to(from, false, [&num_extras](Node*&, bool go_right) { | ||
| if (go_right) | ||
|
|
@@ -1689,6 +1743,17 @@ namespace merkle | |
| } | ||
|
|
||
| protected: | ||
| void validate_partial_range(size_t from, size_t to) const | ||
| { | ||
| const bool from_out_of_range = from < min_index() || max_index() < from; | ||
| const bool to_out_of_range = to < min_index() || max_index() < to; | ||
| const bool reversed_range = from > to; | ||
| if (empty() || from_out_of_range || to_out_of_range || reversed_range) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit. 3 comparisons are enough |
||
| { | ||
| throw std::runtime_error("invalid leaf indices"); | ||
| } | ||
| } | ||
|
|
||
| /// @brief Vector of leaf nodes current in the tree | ||
| std::vector<Node*> leaf_nodes; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious why .clear() and only then = move()?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also this's a dup of what we do in move ctor, so could've been made a single function, as in "clear()"