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(x/tx): use dynamicpb instead of type resolver in any marshal #16048

Merged
merged 50 commits into from
May 8, 2023

Conversation

kocubinski
Copy link
Member

@kocubinski kocubinski commented May 5, 2023

Description

Closes: #16024


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@github-actions github-actions bot added the C:Rosetta Issues and PR related to Rosetta label May 5, 2023
@@ -17,17 +20,26 @@ type MessageEncoder func(*Encoder, protoreflect.Message, io.Writer) error
// FieldEncoder is a function that can encode a protobuf protoreflect.Value to JSON.
type FieldEncoder func(*Encoder, protoreflect.Value, io.Writer) error

// EncoderOptions are options for creating a new Encoder.
type EncoderOptions struct {
FileResolver signing.ProtoFileResolver
Copy link
Member

Choose a reason for hiding this comment

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

I think there should be a TypeResolver option too

msgName)
}

valueMsg := dynamicpb.NewMessageType(desc.(protoreflect.MessageDescriptor)).New().Interface()
Copy link
Member

Choose a reason for hiding this comment

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

I think we should use the type from the type resolver if it resolves first, if not then use dynamicpb

@@ -95,11 +101,36 @@ func NewOperationMsg(msg sdk.Msg, ok bool, comment string, cdc *codec.ProtoCodec
moduleName = msgType
}

if legacyMsg, okType := msg.(legacytx.LegacyMsg); okType {
Copy link
Member Author

Choose a reason for hiding this comment

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

Another option I explored was packing sdk.Msg into an any message and then marshaling that, but if we do
the encoder will reject messages that are not tagged with an amino.name. Strictly speaking we don't need
an amino.name on messages which aren't expected to be packed into any fields except for this one case.

Comment on lines 109 to 130
resolver := gogoproto.HybridResolver
desc, err := resolver.FindDescriptorByName(protoreflect.FullName(gogoproto.MessageName(msg)))
if err != nil {
panic(fmt.Errorf("failed to find proto descriptor for %s: %w", msgType, err))
}

dynamicMsgType := dynamicpb.NewMessageType(desc.(protoreflect.MessageDescriptor))
dynamicMsg := dynamicMsgType.New().Interface()
gogoBytes, err := gogoproto.Marshal(msg)
if err != nil {
panic(fmt.Errorf("failed to marshal msg: %w", err))
}
err = proto.Unmarshal(gogoBytes, dynamicMsg)
if err != nil {
panic(fmt.Errorf("failed to unmarshal msg: %w", err))
}

encoder := aminojson.NewEncoder(aminojson.EncoderOptions{FileResolver: resolver})
jsonBytes, err := encoder.Marshal(dynamicMsg)
if err != nil {
panic(fmt.Errorf("failed to marshal msg: %w", err))
}
Copy link
Member

@aaronc aaronc May 8, 2023

Choose a reason for hiding this comment

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

Since this will be pretty common let's create some helpers for it. I would even go as far as saying that we can add a MarshalAminoJSON method to codec.Codec which does exactly this - it already has the proper file resolver from InterfaceRegistry

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you suggesting adding MarshalAminoJSON to JSONCodec?

codec/proto_codec.go Outdated Show resolved Hide resolved
return nil, err
}

dynamicMsgType := dynamicpb.NewMessageType(desc.(protoreflect.MessageDescriptor))
Copy link
Member

Choose a reason for hiding this comment

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

Should we try to find a non-dynamic version in protoregistry.GlobalTypes first? That might be slightly more performant than dynamicpb

typeResolver protoregistry.MessageTypeResolver
encoder Encoder
}

// SignModeHandlerOptions are the options for the SignModeHandler.
type SignModeHandlerOptions struct {
FileResolver protodesc.Resolver
FileResolver signing.ProtoFileResolver
Copy link
Member

Choose a reason for hiding this comment

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

Can't we just use the file resolver on the Encoder? Or is this so that Encoder can be left nil?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right this supports he case where Encoder is nil

@kocubinski kocubinski added this pull request to the merge queue May 8, 2023
Merged via the queue into main with commit 2f21cb5 May 8, 2023
@kocubinski kocubinski deleted the kocubinski/x-tx-0.7.0 branch May 8, 2023 19:22
larry0x pushed a commit to larry0x/cosmos-sdk that referenced this pull request May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:Rosetta Issues and PR related to Rosetta C:x/auth C:x/evidence C:x/tx
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove usages of GetSignBytes
3 participants