Skip to content
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

refactor!: adapt merkle path to use repeated bytes in favour of strings #6633

Merged
merged 4 commits into from
Jun 19, 2024

Conversation

damiannolan
Copy link
Member

Description

Initial PR. Will break the API of NewMerklePath in a following PR.

  • Change MerklePath to use repeated bytes in favour of repeated string.

ref: #6496


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against the correct branch (see CONTRIBUTING.md).
  • Linked to GitHub issue with discussion and accepted design, OR link to spec that describes this work.
  • Code follows the module structure standards and Go style guide.
  • Wrote unit and integration tests.
  • Updated relevant documentation (docs/).
  • Added relevant godoc comments.
  • Provide a conventional commit message to follow the repository standards.
  • Include a descriptive changelog entry when appropriate. This may be left to the discretion of the PR reviewers. (e.g. chores should be omitted from changelog)
  • Re-reviewed Files changed in the GitHub PR explorer.
  • Review SonarCloud Report in the comment section below once CI passes.

@damiannolan damiannolan marked this pull request as ready for review June 18, 2024 14:30
Copy link
Contributor

@colin-axner colin-axner left a comment

Choose a reason for hiding this comment

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

Excellent! ❤️

@colin-axner colin-axner added the priority PRs that need prompt reviews label Jun 18, 2024
Copy link
Contributor

@crodriguezvega crodriguezvega left a comment

Choose a reason for hiding this comment

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

Nice this refactor is started, @damiannolan!

I am wondering if it would help readability to have a type alias for [][]byte?

Should we also mention in migration docs that KeyPath type is changed from string to [][]byte?

@damiannolan
Copy link
Member Author

I am wondering if it would help readability to have a type alias for [][]byte?

I mentioned something similar to @colin-axner yesterday in dms. I can explore that when the bulk of the code is updated.

Should we also mention in migration docs that KeyPath type is changed from string to [][]byte?

Yes! I'll add it to the issue 👍🏻

@damiannolan damiannolan added this pull request to the merge queue Jun 19, 2024
@damiannolan damiannolan removed this pull request from the merge queue due to a manual request Jun 19, 2024
@damiannolan
Copy link
Member Author

I'll add a changelog to this before merging

@damiannolan damiannolan enabled auto-merge June 19, 2024 10:11
@damiannolan damiannolan added this pull request to the merge queue Jun 19, 2024
Copy link

Merged via the queue into main with commit 97b248d Jun 19, 2024
82 of 84 checks passed
@damiannolan damiannolan deleted the damian/6496-merklepath-bz branch June 19, 2024 10:41
@damiannolan damiannolan mentioned this pull request Jun 20, 2024
10 tasks
@@ -26,7 +26,7 @@ message MerklePrefix {
// arbitrary structured object (defined by a commitment type).
// MerklePath is represented from root-to-leaf
message MerklePath {
repeated string key_path = 1;
repeated bytes key_path = 1;
Copy link

Choose a reason for hiding this comment

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

This is a proto breaking change, what's the tradeoff here vs. field deprecation?

Copy link
Member Author

@damiannolan damiannolan Jun 24, 2024

Choose a reason for hiding this comment

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

Hey @erwanor! Does this raise any concern in particular for you and do you depend on the message type?

This is a bit of a protobuf grey area. Myself and @colin-axner looked into this quite a bit to ensure it was okay to make this change in order to support proving kvs stored under non utf8 encoded keys (as requested by union).

Thus far we've considered this Go api breaking but not actually proto encoding breaking. Backwards compatibility should be maintained with this change. I wrote a quick test to validate this, introducing a temp LegacyMerklePath type with repeated string (old diff). I also tested for equality of bytes when both types are marshalled using proto codec.

func (suite *MerkleTestSuite) TestMerklePathEncoding() {
	encodingCfg := moduletestutil.MakeTestEncodingConfig()

	keyPath := []string{"ibc", "commitments/ports/transfer/channels/channel-0/sequences/1"}
	merklePath := types.NewMerklePath(keyPath...)

	bz, err := encodingCfg.Codec.Marshal(&merklePath)
	suite.Require().NoError(err)

	var legacy types.LegacyMerklePath
	err = encodingCfg.Codec.Unmarshal(bz, &legacy)
	suite.Require().NoError(err)

	suite.Require().Equal(keyPath, legacy.KeyPath)
}

Some of the resources which helped us come to this conclusion:

https://protobuf.dev/programming-guides/encoding/#structure

The binary version of a message just uses the field’s number as the key – the name and declared type for each field can only be determined on the decoding end by referencing the message type’s definition (i.e. the .proto file).

https://protobuf.dev/programming-guides/proto2/#updating

string and bytes are compatible as long as the bytes are valid UTF-8.

It's possible that we may have overlooked something wrt to rust users/tooling, please let us know if you have any concerns!

Copy link
Member Author

Choose a reason for hiding this comment

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

The biggest concern for me here was that when encoding to JSON you now end up with a key_path field which is a base64 encoded byte string.

Copy link
Member Author

@damiannolan damiannolan Jun 24, 2024

Choose a reason for hiding this comment

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

Also happy to introduce a v2 proto pkg and adjust ibc-go code as necessary. If it makes things easier to keep the old type around! Let me know what you think!

bznein pushed a commit that referenced this pull request Jun 26, 2024
…gs (#6633)

* refactor: adapt merkle path to use repeated bytes in favour of strings

* chore: add changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority PRs that need prompt reviews
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants