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

fix(x/tx): fallback to injected resolver for placeholder descriptors #22852

Merged
merged 6 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions x/tx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ Since v0.13.0, x/tx follows Cosmos SDK semver: https://github.com/cosmos/cosmos-

## [Unreleased]

## [v1.0.0-alpha.3](https://github.com/cosmos/cosmos-sdk/releases/tag/x/tx/v1.0.0-alpha.3) - 2024-12-12

### Bug Fixes

* [#22852](https://github.com/cosmos/cosmos-sdk/pull/22852) Fallback to injected resolver for placeholder descriptors.

## [v1.0.0-alpha.2](https://github.com/cosmos/cosmos-sdk/releases/tag/x/tx/v1.0.0-alpha.2) - 2024-11-01

* [#22311](https://github.com/cosmos/cosmos-sdk/pull/22311) Fix add feePayer as signer.
Expand Down
11 changes: 11 additions & 0 deletions x/tx/decode/unknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ func RejectUnknownFields(bz []byte, desc protoreflect.MessageDescriptor, allowUn
if fieldMessage == nil {
continue
}
// if a message descriptor is a placeholder resolve it using the injected resolver.
// this can happen when a descriptor has been registered in the
// "google.golang.org/protobuf" resgistry but not in "github.com/cosmos/gogoproto".
// fixes: https://github.com/cosmos/cosmos-sdk/issues/22574
if fieldMessage.IsPlaceholder() {
gogoDesc, err := resolver.FindDescriptorByName(fieldMessage.FullName())
if err != nil {
return hasUnknownNonCriticals, fmt.Errorf("could not resolve placeholder descriptor: %v: %w", fieldMessage, err)
}
fieldMessage = gogoDesc.(protoreflect.MessageDescriptor)
}
Comment on lines +85 to +95
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add type assertion safety check

The implementation looks good overall, but the type assertion on line 94 could panic if the resolver returns an unexpected type. Consider adding a type safety check.

Apply this diff to add type safety:

 			if err != nil {
 				return hasUnknownNonCriticals, fmt.Errorf("could not resolve placeholder descriptor: %v: %w", fieldMessage, err)
 			}
-			fieldMessage = gogoDesc.(protoreflect.MessageDescriptor)
+			msgDesc, ok := gogoDesc.(protoreflect.MessageDescriptor)
+			if !ok {
+				return hasUnknownNonCriticals, fmt.Errorf("resolved descriptor is not a message descriptor: got %T", gogoDesc)
+			}
+			fieldMessage = msgDesc
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// if a message descriptor is a placeholder resolve it using the injected resolver.
// this can happen when a descriptor has been registered in the
// "google.golang.org/protobuf" resgistry but not in "github.com/cosmos/gogoproto".
// fixes: https://github.com/cosmos/cosmos-sdk/issues/22574
if fieldMessage.IsPlaceholder() {
gogoDesc, err := resolver.FindDescriptorByName(fieldMessage.FullName())
if err != nil {
return hasUnknownNonCriticals, fmt.Errorf("could not resolve placeholder descriptor: %v: %w", fieldMessage, err)
}
fieldMessage = gogoDesc.(protoreflect.MessageDescriptor)
}
// if a message descriptor is a placeholder resolve it using the injected resolver.
// this can happen when a descriptor has been registered in the
// "google.golang.org/protobuf" resgistry but not in "github.com/cosmos/gogoproto".
// fixes: https://github.com/cosmos/cosmos-sdk/issues/22574
if fieldMessage.IsPlaceholder() {
gogoDesc, err := resolver.FindDescriptorByName(fieldMessage.FullName())
if err != nil {
return hasUnknownNonCriticals, fmt.Errorf("could not resolve placeholder descriptor: %v: %w", fieldMessage, err)
}
msgDesc, ok := gogoDesc.(protoreflect.MessageDescriptor)
if !ok {
return hasUnknownNonCriticals, fmt.Errorf("resolved descriptor is not a message descriptor: got %T", gogoDesc)
}
fieldMessage = msgDesc
}


// consume length prefix of nested message
_, o := protowire.ConsumeVarint(fieldBytes)
Expand Down
Loading