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
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
09d9e6b
refactor(sims): rm usage of GetSignBytes from NewOperationMsg
kocubinski May 4, 2023
82ca71f
replace usage of GetSignBytes with AminoJSON encoder
kocubinski May 4, 2023
3091b43
add comment
kocubinski May 4, 2023
e7bf7bb
fix comment
kocubinski May 5, 2023
4a367d4
mid work
kocubinski May 5, 2023
5d45211
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/s…
kocubinski May 5, 2023
f28eda3
try allow unnamed any types in sims
kocubinski May 5, 2023
53b1522
push the bad up to sims package
kocubinski May 5, 2023
38c95b8
revert proto gen (out of scope)
kocubinski May 5, 2023
bbf9a4e
revert go mod change
kocubinski May 5, 2023
01e92b9
improve comment of impots
kocubinski May 5, 2023
2c120b5
spelling
kocubinski May 5, 2023
b7e7c22
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/s…
kocubinski May 5, 2023
1370739
use dynamicpb for marshal target
kocubinski May 5, 2023
a9e188d
rm generated imports
kocubinski May 5, 2023
a2127f8
chore(x/tx): Update changelog for x/tx v0.6.3.
kocubinski May 5, 2023
3986662
update to x/tx/v0.6.3
kocubinski May 5, 2023
0e4559d
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/s…
kocubinski May 5, 2023
5a92828
update constructor call
kocubinski May 5, 2023
ec384c5
aminojson.Encoder now takes EncoderOptions as an arugment.
kocubinski May 5, 2023
3b11f31
update changelog
kocubinski May 5, 2023
b1f4fde
Merge branch 'kocubinski/x-tx-0.6.3' into kocubinski/x-tx-0.7.0
kocubinski May 5, 2023
890a927
using dynamicpb instead of type resolver
kocubinski May 5, 2023
5815c54
go mod tidy
kocubinski May 5, 2023
abf3c56
go mod tidy
kocubinski May 5, 2023
1ba32d7
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/x…
kocubinski May 6, 2023
e95633b
refactoring
kocubinski May 6, 2023
184baab
simplify NewOperationMsg with aminojson changes
kocubinski May 6, 2023
20517ce
Revert "simplify NewOperationMsg with aminojson changes"
kocubinski May 6, 2023
8d6dc66
add comments
kocubinski May 7, 2023
7209dec
lint fix
kocubinski May 7, 2023
0dbf476
use signing.ProtoFileResolver throughout
kocubinski May 7, 2023
e18817b
fix spelling
kocubinski May 7, 2023
58c019f
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/x…
kocubinski May 7, 2023
4636726
go mod tidy
kocubinski May 7, 2023
90527b4
rm extra whitespace
kocubinski May 7, 2023
87cfdda
clean up comment
kocubinski May 7, 2023
0a76cb9
Merge branch 'main' into kocubinski/x-tx-0.7.0
kocubinski May 8, 2023
70a6870
Create MarshalAminoJSON on ProtoCodec
kocubinski May 8, 2023
f16c9e7
clean up comment
kocubinski May 8, 2023
f63f47e
use struct interfaceRegistry as resolver
kocubinski May 8, 2023
0687069
Merge branch 'main' into kocubinski/x-tx-0.7.0
kocubinski May 8, 2023
51dca43
include tip in signDoc
kocubinski May 8, 2023
ad360d4
Merge branch 'kocubinski/x-tx-0.7.0' of github.com:cosmos/cosmos-sdk …
kocubinski May 8, 2023
ba4a5e7
Update codec/proto_codec.go
kocubinski May 8, 2023
693c267
add test coverage for both set and unset tip
kocubinski May 8, 2023
73cac56
Merge branch 'kocubinski/x-tx-0.7.0' of github.com:cosmos/cosmos-sdk …
kocubinski May 8, 2023
e9c8c53
only fallback to dynamicpb in ProtoCodec.MarshalAminoJSON when type n…
kocubinski May 8, 2023
93d1362
fixing some whitespace
kocubinski May 8, 2023
09e7597
add nil guard
kocubinski May 8, 2023
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
cosmossdk.io/log v1.1.0
cosmossdk.io/math v1.0.0
cosmossdk.io/store v0.1.0-alpha.1.0.20230328185921-37ba88872dbc
cosmossdk.io/x/tx v0.6.1
cosmossdk.io/x/tx v0.6.3
github.com/99designs/keyring v1.2.1
github.com/armon/go-metrics v0.4.1
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816
Expand Down Expand Up @@ -164,7 +164,7 @@ require (
replace (
cosmossdk.io/core => ./core
cosmossdk.io/store => ./store
// TODO: remove after release 0.6.2
// TODO: remove after 0.7.0 release
cosmossdk.io/x/tx => ./x/tx
// use cosmos fork of keyring
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
Expand Down
6 changes: 3 additions & 3 deletions simapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
cloud.google.com/go/storage v1.30.0 // indirect
cosmossdk.io/collections v0.1.0 // indirect
cosmossdk.io/errors v1.0.0-beta.7.0.20230429155654-3ee8242364e4 // indirect
cosmossdk.io/x/tx v0.6.1 // indirect
cosmossdk.io/x/tx v0.6.3 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down Expand Up @@ -204,14 +204,14 @@ replace (
cosmossdk.io/x/evidence => ../x/evidence
cosmossdk.io/x/feegrant => ../x/feegrant
cosmossdk.io/x/nft => ../x/nft
// TODO: remove after release 0.6.2
cosmossdk.io/x/tx => ../x/tx
cosmossdk.io/x/upgrade => ../x/upgrade
)

// Below are the long-lived replace of the SimApp
replace (
cosmossdk.io/core => ../core
// TODO: remove after 0.7.0 release
cosmossdk.io/x/tx => ../x/tx
// use cosmos fork of keyring
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
// Simapp always use the latest version of the cosmos-sdk
Expand Down
7 changes: 3 additions & 4 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
cosmossdk.io/x/evidence v0.1.0
cosmossdk.io/x/feegrant v0.0.0-20230117113717-50e7c4a4ceff
cosmossdk.io/x/nft v0.0.0-20230113085233-fae3332d62fc
cosmossdk.io/x/tx v0.6.1
cosmossdk.io/x/tx v0.6.3
cosmossdk.io/x/upgrade v0.0.0-20230127052425-54c8e1568335
github.com/cometbft/cometbft v0.37.1
github.com/cosmos/cosmos-db v1.0.0-rc.1
Expand Down Expand Up @@ -200,14 +200,13 @@ replace (
cosmossdk.io/x/upgrade => ../x/upgrade
)

// temporary replace
replace cosmossdk.io/x/tx => ../x/tx

// Below are the long-lived replace for tests.
replace (
cosmossdk.io/core => ../core
// We always want to test against the latest version of the simapp.
cosmossdk.io/simapp => ../simapp
// TODO: remove after 0.7.0 release
cosmossdk.io/x/tx => ../x/tx
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
// We always want to test against the latest version of the SDK.
github.com/cosmos/cosmos-sdk => ../.
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/aminojson/aminojson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestAminoJSON_Equivalence(t *testing.T) {
distribution.AppModuleBasic{}, evidence.AppModuleBasic{}, feegrantmodule.AppModuleBasic{},
gov.AppModuleBasic{}, groupmodule.AppModuleBasic{}, mint.AppModuleBasic{}, params.AppModuleBasic{},
slashing.AppModuleBasic{}, staking.AppModuleBasic{}, upgrade.AppModuleBasic{}, vesting.AppModuleBasic{})
aj := aminojson.NewEncoder()
aj := aminojson.NewEncoder(aminojson.EncoderOptions{})

for _, tt := range rapidgen.DefaultGeneratedTypes {
name := string(tt.Pulsar.ProtoReflect().Descriptor().FullName())
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestAminoJSON_LegacyParity(t *testing.T) {
bank.AppModuleBasic{}, distribution.AppModuleBasic{}, slashing.AppModuleBasic{}, staking.AppModuleBasic{},
vesting.AppModuleBasic{})

aj := aminojson.NewEncoder()
aj := aminojson.NewEncoder(aminojson.EncoderOptions{})
addr1 := types.AccAddress("addr1")
now := time.Now()

Expand Down Expand Up @@ -480,7 +480,7 @@ func TestSendAuthorization(t *testing.T) {
encCfg := testutil.MakeTestEncodingConfig(auth.AppModuleBasic{}, authzmodule.AppModuleBasic{},
distribution.AppModuleBasic{}, bank.AppModuleBasic{})

aj := aminojson.NewEncoder()
aj := aminojson.NewEncoder(aminojson.EncoderOptions{})

// beware, Coins has as custom MarshalJSON method which changes how nil is handled
// nil -> [] (empty list)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/aminojson/repeated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func TestRepeatedFields(t *testing.T) {
cdc := codec.NewLegacyAmino()
aj := aminojson.NewEncoder()
aj := aminojson.NewEncoder(aminojson.EncoderOptions{})

cases := map[string]struct {
gogo gogoproto.Message
Expand Down
2 changes: 1 addition & 1 deletion tools/rosetta/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
cosmossdk.io/depinject v1.0.0-alpha.3 // indirect
cosmossdk.io/errors v1.0.0-beta.7.0.20230429155654-3ee8242364e4 // indirect
cosmossdk.io/store v0.1.0-alpha.1.0.20230328185921-37ba88872dbc // indirect
cosmossdk.io/x/tx v0.6.1 // indirect
cosmossdk.io/x/tx v0.6.3 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down
33 changes: 29 additions & 4 deletions types/simulation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package simulation

import (
"encoding/json"
"fmt"
"math/rand"
"time"

gogoproto "github.com/cosmos/gogoproto/proto"
"google.golang.org/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/dynamicpb"

"cosmossdk.io/x/tx/signing/aminojson"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)

// Deprecated: Use WeightedProposalMsg instead.
Expand Down Expand Up @@ -95,11 +101,30 @@ 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.

return NewOperationMsgBasic(moduleName, msgType, comment, ok, legacyMsg.GetSignBytes())
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?


return NewOperationMsgBasic(moduleName, msgType, comment, ok, cdc.MustMarshalJSON(msg))
return NewOperationMsgBasic(moduleName, msgType, comment, ok, jsonBytes)
}

// NoOpMsg - create a no-operation message
Expand Down
2 changes: 1 addition & 1 deletion x/auth/tx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func NewTxConfigWithOptions(protoCodec codec.ProtoCodecMarshaler, configOptions
panic(err)
}
case signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON:
aminoJSONEncoder := aminojson.NewEncoder()
aminoJSONEncoder := aminojson.NewEncoder(aminojson.EncoderOptions{})
handlers[i] = aminojson.NewSignModeHandler(aminojson.SignModeHandlerOptions{
FileResolver: signingOpts.FileResolver,
TypeResolver: signingOpts.TypeResolver,
Expand Down
7 changes: 7 additions & 0 deletions x/tx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Unreleased

## v0.7.0

### API Breaking

* [#16044](https://github.com/cosmos/cosmos-sdk/pull/16044): rename aminojson.NewAminoJSON -> aminojson.NewEncoder
* [#16047](https://github.com/cosmos/cosmos-sdk/pull/16047): aminojson.NewEncoder now takes EncoderOptions as an argument.

## v0.6.2

### Improvements
Expand Down
2 changes: 1 addition & 1 deletion x/tx/signing/aminojson/aminojson.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewSignModeHandler(options SignModeHandlerOptions) *SignModeHandler {
h.typeResolver = options.TypeResolver
}
if options.Encoder == nil {
h.encoder = NewEncoder()
h.encoder = NewEncoder(EncoderOptions{})
} else {
h.encoder = *options.Encoder
}
Expand Down
2 changes: 1 addition & 1 deletion x/tx/signing/aminojson/aminojson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestAminoJsonSignMode(t *testing.T) {
func TestNewSignModeHandler(t *testing.T) {
handler := aminojson.NewSignModeHandler(aminojson.SignModeHandlerOptions{})
require.NotNil(t, handler)
aj := aminojson.NewEncoder()
aj := aminojson.NewEncoder(aminojson.EncoderOptions{})
handler = aminojson.NewSignModeHandler(aminojson.SignModeHandlerOptions{
FileResolver: protoregistry.GlobalFiles,
TypeResolver: protoregistry.GlobalTypes,
Expand Down
50 changes: 41 additions & 9 deletions x/tx/signing/aminojson/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,66 @@ import (
"fmt"
"io"

"google.golang.org/protobuf/types/dynamicpb"
"google.golang.org/protobuf/types/known/anypb"

"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)

func (enc Encoder) marshalAny(message protoreflect.Message, writer io.Writer) error {
anyMsg := message.Interface().(*anypb.Any)
resolver := protoregistry.GlobalTypes
_, ok := message.Interface().(*dynamicpb.Message)
if ok {
return enc.marshalDynamic(message, writer)
}

anyMsg, ok := message.Interface().(*anypb.Any)
if !ok {
return fmt.Errorf("expected *anypb.Any, got %T", message.Interface())
}

typ, err := resolver.FindMessageByURL(anyMsg.TypeUrl)
desc, err := enc.fileResolver.FindDescriptorByName(protoreflect.FullName(anyMsg.TypeUrl[1:]))
if err != nil {
return errors.Wrapf(err, "can't resolve type URL %s", anyMsg.TypeUrl)
}

valueMsg := typ.New()
err = proto.Unmarshal(anyMsg.Value, valueMsg.Interface())
_, named := getMessageAminoName(desc.Options())
if !named {
return fmt.Errorf("message %s is packed into an any field, so requires an amino.name annotation",
anyMsg.TypeUrl)
}

valueMsg := dynamicpb.NewMessageType(desc.(protoreflect.MessageDescriptor)).New().Interface()
err = proto.Unmarshal(anyMsg.Value, valueMsg)
if err != nil {
return err
}
protoMessage := valueMsg.ProtoReflect()

return enc.beginMarshal(protoMessage, writer)
}

_, named := getMessageAminoName(valueMsg)
func (enc Encoder) marshalDynamic(message protoreflect.Message, writer io.Writer) error {
msgName := message.Get(message.Descriptor().Fields().ByName("type_url")).String()[1:]
msgBytes := message.Get(message.Descriptor().Fields().ByName("value")).Bytes()

desc, err := enc.fileResolver.FindDescriptorByName(protoreflect.FullName(msgName))
if err != nil {
return errors.Wrapf(err, "can't resolve type URL %s", msgName)
}

_, named := getMessageAminoName(desc.Options())
if !named {
return fmt.Errorf("message %s is packed into an any field, so requires an amino.name annotation",
anyMsg.TypeUrl)
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

err = proto.Unmarshal(msgBytes, valueMsg)
if err != nil {
return err
}

return enc.beginMarshal(valueMsg, writer)
return enc.beginMarshal(valueMsg.ProtoReflect(), writer)
}
17 changes: 15 additions & 2 deletions x/tx/signing/aminojson/json_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"

"cosmossdk.io/x/tx/signing"
)

// MessageEncoder is a function that can encode a protobuf protoreflect.Message to JSON.
Expand All @@ -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

}

// Encoder is a JSON encoder that uses the Amino JSON encoding rules for protobuf messages.
type Encoder struct {
// maps cosmos_proto.scalar -> field encoder
scalarEncoders map[string]FieldEncoder
messageEncoders map[string]MessageEncoder
fieldEncoders map[string]FieldEncoder
fileResolver signing.ProtoFileResolver
}

// NewEncoder returns a new Encoder capable of serializing protobuf messages to JSON using the Amino JSON encoding
// rules.
func NewEncoder() Encoder {
func NewEncoder(options EncoderOptions) Encoder {
Copy link
Member

@julienrbrt julienrbrt May 6, 2023

Choose a reason for hiding this comment

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

If the options are facultative and do not need to be used all at the same time, why not having them variadic?
Now you needed to add an empty struct everywhere.

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems like a misuse of variadic to me since we never want more than one of them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we could have two constructors:
NewEncoder
NewEncoderWithOptions

Personally it seems OK to me supply empty options as a default with only one constructor. Any thoughts @aaronc ?

Copy link
Member

Choose a reason for hiding this comment

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

Empty options would almost always be incorrect

Copy link
Member Author

Choose a reason for hiding this comment

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

They're correct in tests. What are you proposing instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

I propose one NewEncoder which takes options. Empty options are probably only correct in tests with a narrow scope. The AminoJSON encoder needs the HybridResolver from cosmos/gogoproto#62 in production or any and dynamic type marshaling will fail. The only reason I didn't make HybridResolver the default for EncoderOptions.FileResolver is that this would form a dependency on gogoproto, and there currently isn't one.

Copy link
Member

Choose a reason for hiding this comment

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

That sounds right

if options.FileResolver == nil {
options.FileResolver = protoregistry.GlobalFiles
}
enc := Encoder{
scalarEncoders: map[string]FieldEncoder{
"cosmos.Dec": cosmosDecEncoder,
Expand All @@ -42,6 +54,7 @@ func NewEncoder() Encoder {
"legacy_coins": nullSliceAsEmptyEncoder,
"cosmos_dec_bytes": cosmosDecEncoder,
},
fileResolver: options.FileResolver,
}
return enc
}
Expand Down Expand Up @@ -92,7 +105,7 @@ func (enc Encoder) Marshal(message proto.Message) ([]byte, error) {
}

func (enc Encoder) beginMarshal(msg protoreflect.Message, writer io.Writer) error {
name, named := getMessageAminoName(msg)
name, named := getMessageAminoName(msg.Descriptor().Options())
if named {
_, err := writer.Write([]byte(fmt.Sprintf(`{"type":"%s","value":`, name)))
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions x/tx/signing/aminojson/json_marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAminoJSON_EdgeCases(t *testing.T) {
cdc := amino.NewCodec()
cdc.RegisterConcrete(&testpb.ABitOfEverything{}, "ABitOfEverything", nil)
cdc.RegisterConcrete(&testpb.NestedMessage{}, "NestedMessage", nil)
aj := aminojson.NewEncoder()
aj := aminojson.NewEncoder(aminojson.EncoderOptions{})

cases := map[string]struct {
msg proto.Message
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestAminoJSON(t *testing.T) {
Si64: -59268425823934,
Sf64: -659101379604211154,
}
bz, err := aminojson.NewEncoder().Marshal(msg)
bz, err := aminojson.NewEncoder(aminojson.EncoderOptions{}).Marshal(msg)
assert.NilError(t, err)
legacyBz, err := cdc.MarshalJSON(msg)
assert.NilError(t, err)
Expand All @@ -102,7 +102,7 @@ func TestRapid(t *testing.T) {
gen := rapidproto.MessageGenerator(&testpb.ABitOfEverything{}, rapidproto.GeneratorOptions{})
rapid.Check(t, func(t *rapid.T) {
msg := gen.Draw(t, "msg")
bz, err := aminojson.NewEncoder().Marshal(msg)
bz, err := aminojson.NewEncoder(aminojson.EncoderOptions{}).Marshal(msg)
assert.NilError(t, err)
checkInvariants(t, msg, bz)
})
Expand Down
7 changes: 3 additions & 4 deletions x/tx/signing/aminojson/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (

// getMessageAminoName returns the amino name of a message if it has been set by the `amino.name` option.
// If the message does not have an amino name, then the function returns false.
func getMessageAminoName(msg protoreflect.Message) (string, bool) {
opts := msg.Descriptor().Options()
if proto.HasExtension(opts, amino.E_Name) {
name := proto.GetExtension(opts, amino.E_Name)
func getMessageAminoName(messageOptions proto.Message) (string, bool) {
if proto.HasExtension(messageOptions, amino.E_Name) {
name := proto.GetExtension(messageOptions, amino.E_Name)
return name.(string), true
}
return "", false
Expand Down