-
Notifications
You must be signed in to change notification settings - Fork 643
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/ibc-eureka' into aditya/write-ack-api
- Loading branch information
Showing
3 changed files
with
32 additions
and
40 deletions.
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