-
Notifications
You must be signed in to change notification settings - Fork 219
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
fix: addresses mmr find_peaks
bug
#5182
Merged
stringhandler
merged 10 commits into
tari-project:development
from
jorgeantonio21:ja-mmr
Feb 21, 2023
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
996a7d5
first commit
jorgeantonio21 dfecc93
add tests
jorgeantonio21 4c03b89
Merge branch 'development' into ja-mmr
jorgeantonio21 15176a9
update comments
jorgeantonio21 17bd112
address PR comments
jorgeantonio21 65226a4
update comment
jorgeantonio21 c84648d
Merge branch 'development' into ja-mmr
jorgeantonio21 884cf49
Merge branch 'development' into ja-mmr
jorgeantonio21 8a232c5
Merge branch 'development' into ja-mmr
jorgeantonio21 a74895e
Merge branch 'development' into ja-mmr
jorgeantonio21 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 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
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.
Ah! So the reason for the vec![] return is to say that the mmr size cannot exist.
Say we have an MMR of size 5 (nodes not leaf nodes), which would look like this
However that isnt a valid MMR size because it should look like this (7 nodes)
As expected,
find_peaks(7) == [6]
So perhaps we should return an Option here instead of an empty vec to force the caller to handle the case of an invalid size. And add a comment explaining what
None
means.Then in
MerkleProof::generate_proof
andMerkleProof::verify
, there was a missing check, you would do something likeThere 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.
I think it's more intuitive (and useful) to always return the peak that would be created.
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.
I think the first example could make sense and might well represent a Merkle Mountain Range of size 5, and for that reason, we should be able to generate proofs for it. The implementations I have seen so far deal with any size MMR. But happy to make the
find_peaks
return an option in case, the size doesn't correspond to a fully generated MMR.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.
A merkle mountain range cannot be size 5 because then it is malformed. In the above example, nodes 3 and 4 must be hashed to 5 and 2 and 5 must be hashed to 6 or the MMR is not a valid MMR.
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.
The fact that it might break on
find_peaks
on invalid numbers, might point out that we might have an issue with the mmr,I agree here with @sdbondi an MMR cannot be for example size 5,
And MMR containing 5 elements would be of size 8.
It might be that our MMR implementation does not cap the peaks correctly
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.
I agree with the above comments. Let me refactor the code to output a
None
in case the MMR is not fully generated/complete/valid and check its usage later on. @SWvheerden in the example above, the size of the MMR is 8, spanning a 5 element set, I think.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.
Yes, this.
find_peaks
should really be private and documented such that it should not be called before "bagging" the MMR. Returning an Option is ok too. I'll bet this wasn't done on day 1 on performance grounds and the assumption that it gets called after bagging (but should have been made private to make that assumption a contract). But @stringhandler and I discussed this and found thatOption
overheads aren't that high.