-
Notifications
You must be signed in to change notification settings - Fork 643
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
imp: simplified BuildMerklePath
#7863
Merged
+32
−40
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,20 @@ | ||
package types | ||
|
||
import ( | ||
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types" | ||
commitmenttypesv2 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2" | ||
) | ||
|
||
// BuildMerklePath takes the merkle path prefix and an ICS24 path | ||
// and builds a new path by appending the ICS24 path to the last element of the merkle path prefix. | ||
func BuildMerklePath(prefix commitmenttypesv2.MerklePath, path []byte) commitmenttypesv2.MerklePath { | ||
if prefix.Empty() { | ||
return commitmenttypes.NewMerklePath(path) | ||
func BuildMerklePath(prefix [][]byte, path []byte) commitmenttypesv2.MerklePath { | ||
prefixLength := len(prefix) | ||
if prefixLength == 0 { | ||
panic("cannot build merkle path with empty prefix") | ||
} | ||
|
||
// avoid mutating the provided prefix | ||
prefixKeys := make([][]byte, len(prefix.KeyPath)) | ||
copy(prefixKeys, prefix.KeyPath) | ||
|
||
lastElement := prefixKeys[len(prefixKeys)-1] | ||
// copy prefix to avoid modifying the original slice | ||
fullPath := append([][]byte(nil), prefix...) | ||
// append path to last element | ||
newLastElement := cloneAppend(lastElement, path) | ||
prefixKeys[len(prefixKeys)-1] = newLastElement | ||
return commitmenttypes.NewMerklePath(prefixKeys...) | ||
} | ||
|
||
func cloneAppend(bz []byte, tail []byte) []byte { | ||
res := make([]byte, len(bz)+len(tail)) | ||
copy(res, bz) | ||
copy(res[len(bz):], tail) | ||
return res | ||
fullPath[prefixLength-1] = append(fullPath[prefixLength-1], path...) | ||
return commitmenttypesv2.NewMerklePath(fullPath...) | ||
} |
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
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.
Don't think this should necessarily error. If there's no prefix, the path should just be returned as it was originally. See removed diff
This would be equivalent to a chain storing the ICS24 paths directly under root. Which is potentially possible for a solomachine chain or something equivalent
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.
What is the reasoning for changing the behavior here?
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.
Ahh i see it was decided in the original issue! Ok, i mean i suppose we could require all implementations to store under a prefix
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 don't remember the entire context for why we said this, but it was something we mentioned during the alpha audit itself, I believe.
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 could take this back. I don't feel strongly about it. What should we do?
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.
@AdityaSripal can have the final say on it :)
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 its more likely that someone accidentally leaves prefix empty than having a counterparty that is genuinely storing everything under root.
So lets have the error for now, and if we ever have a user run into this issue and report it we can change the behavior of this function quite easily