Skip to content

Commit 1834f4c

Browse files
mergify[bot]julienrbrtaljo242aaronc
authored
fix(client/v2): improve message parsing by replacing proto.Merge with cloneMessage (backport cosmos#24722) (cosmos#24818)
Co-authored-by: julienrbrt <[email protected]> Co-authored-by: Alex | Interchain Labs <[email protected]> Co-authored-by: Aaron <[email protected]>
1 parent c92ff3a commit 1834f4c

24 files changed

+2441
-1564
lines changed

client/v2/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ Ref: https://keepachangelog.com/en/1.0.0/
3434

3535
# Changelog
3636

37+
## [v2.0.0-beta.11](https://github.com/cosmos/cosmos-sdk/tree/client/v2.0.0-beta.11) - 2025-05-30
38+
39+
### Bug Fixes
40+
41+
* [#24722](https://github.com/cosmos/cosmos-sdk/pull/24722) Fix msg parsing in when no pulsar file is present.
42+
3743
## [v2.0.0-beta.9](https://github.com/cosmos/cosmos-sdk/tree/client/v2.0.0-beta.9) - 2025-04-24
3844

3945
### Features
@@ -42,7 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4248

4349
### Improvements
4450

45-
* [#22890](https://github.com/cosmos/cosmos-sdk/pull/22890) Added support for flattening inner message fields in autocli as positional arguments.
51+
* [#22890](https://github.com/cosmos/cosmos-sdk/pull/22890) Added support for flattening inner message fields in autocli as positional arguments.
4652

4753
### Bug Fixes
4854

client/v2/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
codegen:
2-
@(cd internal; buf generate)
2+
@(cd internal; buf generate --template testpbpulsar/buf.gen.yaml)
3+
@(cd internal; buf generate --template testpbgogo/buf.gen.yaml)
4+
@(cd internal/testpbgogo; rm *_grpc.pb.go; rm *.pulsar.go)
5+
@(cd internal; mv github.com/cosmos/cosmos-sdk/client/v2/internal/testpbgogo/* testpbgogo)
6+
@(cd internal; rm -r github.com)

client/v2/autocli/common_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net"
77
"testing"
88

9+
"github.com/cosmos/gogoproto/proto"
910
"github.com/spf13/cobra"
1011
"google.golang.org/grpc"
1112
"google.golang.org/grpc/credentials/insecure"
@@ -15,14 +16,16 @@ import (
1516
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
1617
reflectionv2alpha1 "cosmossdk.io/api/cosmos/base/reflection/v2alpha1"
1718
"cosmossdk.io/client/v2/autocli/flag"
18-
"cosmossdk.io/client/v2/internal/testpb"
19+
"cosmossdk.io/client/v2/internal/testpbgogo"
20+
testpb "cosmossdk.io/client/v2/internal/testpbpulsar"
1921

2022
"github.com/cosmos/cosmos-sdk/client"
2123
"github.com/cosmos/cosmos-sdk/client/flags"
2224
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
2325
sdkkeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
2426
sdk "github.com/cosmos/cosmos-sdk/types"
2527
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
28+
"github.com/cosmos/cosmos-sdk/types/msgservice"
2629
"github.com/cosmos/cosmos-sdk/x/bank"
2730
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
2831
)
@@ -57,6 +60,7 @@ func initFixture(t *testing.T) *fixture {
5760

5861
interfaceRegistry := encodingConfig.Codec.InterfaceRegistry()
5962
banktypes.RegisterInterfaces(interfaceRegistry)
63+
msgservice.RegisterMsgServiceDesc(interfaceRegistry, &testpbgogo.MsgGogoOnly_serviceDesc)
6064

6165
clientCtx := client.Context{}.
6266
WithKeyring(kr).
@@ -69,10 +73,15 @@ func initFixture(t *testing.T) *fixture {
6973
WithChainID("autocli-test")
7074

7175
conn := &testClientConn{ClientConn: clientConn}
76+
77+
// using merged registry to get pulsar + gogo files
78+
mergedFiles, err := proto.MergedRegistry()
79+
assert.NilError(t, err)
80+
7281
b := &Builder{
7382
Builder: flag.Builder{
7483
TypeResolver: protoregistry.GlobalTypes,
75-
FileResolver: protoregistry.GlobalFiles,
84+
FileResolver: mergedFiles,
7685
AddressCodec: addresscodec.NewBech32Codec("cosmos"),
7786
ValidatorAddressCodec: addresscodec.NewBech32Codec("cosmosvaloper"),
7887
ConsensusAddressCodec: addresscodec.NewBech32Codec("cosmosvalcons"),

client/v2/autocli/msg.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ func (b *Builder) BuildMsgMethodCommand(descriptor protoreflect.MethodDescriptor
161161
// Here we use dynamicpb, to create a proto v1 compatible message.
162162
// The SDK codec will handle protov2 -> protov1 (marshal)
163163
msg := dynamicpb.NewMessage(input.Descriptor())
164-
proto.Merge(msg, input.Interface())
164+
if err := cloneMessage(msg, input); err != nil {
165+
return fmt.Errorf("failed to clone message: %w", err)
166+
}
165167

166168
return clienttx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
167169
}
@@ -217,11 +219,23 @@ func (b *Builder) handleGovProposal(
217219
// Here we use dynamicpb, to create a proto v1 compatible message.
218220
// The SDK codec will handle protov2 -> protov1 (marshal)
219221
msg := dynamicpb.NewMessage(input.Descriptor())
220-
proto.Merge(msg, input.Interface())
222+
if err := cloneMessage(msg, input); err != nil {
223+
return fmt.Errorf("failed to clone message: %w", err)
224+
}
221225

222226
if err := proposal.SetMsgs([]gogoproto.Message{msg}); err != nil {
223227
return fmt.Errorf("failed to set msg in proposal %w", err)
224228
}
225229

226230
return clienttx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), proposal)
227231
}
232+
233+
// cloneMessage safely copies fields from src message to dst message.
234+
// this avoids the proto.Merge issue with field descriptors from different repositories.
235+
func cloneMessage(dst, src protoreflect.Message) error {
236+
bz, err := proto.Marshal(src.Interface())
237+
if err != nil {
238+
return err
239+
}
240+
return proto.Unmarshal(bz, dst.Interface())
241+
}

client/v2/autocli/msg_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import (
1515

1616
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
1717
bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
18-
"cosmossdk.io/client/v2/internal/testpb"
18+
"cosmossdk.io/client/v2/internal/testpbgogo"
19+
testpb "cosmossdk.io/client/v2/internal/testpbpulsar"
1920

2021
"github.com/cosmos/cosmos-sdk/client"
2122
)
@@ -124,6 +125,31 @@ func TestMsg(t *testing.T) {
124125
assertNormalizedJSONEqual(t, out.Bytes(), goldenLoad(t, "msg-output.golden"))
125126
}
126127

128+
// TestMsgGogo set same as previous, but the message are only gogoproto generated.
129+
// There are no protov2 files registered in the global registry for those types.
130+
func TestMsgGogo(t *testing.T) {
131+
fixture := initFixture(t)
132+
out, err := runCmd(fixture, buildCustomModuleMsgCommand(&autocliv1.ServiceCommandDescriptor{
133+
Service: testpbgogo.MsgGogoOnly_serviceDesc.ServiceName, // using gogoproto only test msg
134+
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
135+
{
136+
RpcMethod: "Send",
137+
Use: "send [a_coin] [str] [flags]",
138+
Short: "Send coins from one account to another",
139+
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "a_coin"}, {ProtoField: "str"}},
140+
// an_address should be automatically added
141+
},
142+
},
143+
}), "send",
144+
"1foo", "henlo",
145+
"--from", "cosmos1y74p8wyy4enfhfn342njve6cjmj5c8dtl6emdk",
146+
"--generate-only",
147+
"--output", "json",
148+
)
149+
assert.NilError(t, err)
150+
golden.Assert(t, out.String(), "msg-gogo-output.golden")
151+
}
152+
127153
func TestMsgWithFlattenFields(t *testing.T) {
128154
fixture := initFixture(t)
129155

client/v2/autocli/query_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
1919
queryv1beta1 "cosmossdk.io/api/cosmos/base/query/v1beta1"
2020
basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
21-
"cosmossdk.io/client/v2/internal/testpb"
21+
testpb "cosmossdk.io/client/v2/internal/testpbpulsar"
2222

2323
"github.com/cosmos/cosmos-sdk/client"
2424
)

client/v2/autocli/testdata/help-deprecated.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Flags:
88
--a-bool
99
--a-coin cosmos.base.v1beta1.Coin
1010
--a-consensus-address account address or key name
11-
--a-message testpb.AMessage (json)
11+
--a-message testpbpulsar.AMessage (json)
1212
--a-validator-address account address or key name
1313
--an-address account address or key name
1414
--an-enum Enum (unspecified | one | two | five | neg-three) (default unspecified)
@@ -41,7 +41,7 @@ Flags:
4141
--positional2 string
4242
--positional3-varargs cosmos.base.v1beta1.Coin (repeated)
4343
--shorthand-deprecated-field string
44-
--some-messages testpb.AMessage (json) (repeated)
44+
--some-messages testpbpulsar.AMessage (json) (repeated)
4545
--str string
4646
--strings strings
4747
--timestamp timestamp (RFC 3339)

client/v2/autocli/testdata/help-echo.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Flags:
1313
--a-bool
1414
--a-coin cosmos.base.v1beta1.Coin some random coin
1515
--a-consensus-address account address or key name
16-
--a-message testpb.AMessage (json)
16+
--a-message testpbpulsar.AMessage (json)
1717
--a-validator-address account address or key name
1818
--an-address account address or key name
1919
--an-enum Enum (unspecified | one | two | five | neg-three) (default unspecified)
@@ -42,7 +42,7 @@ Flags:
4242
--page-offset uint
4343
--page-reverse
4444
-s, --shorthand-deprecated-field string (DEPRECATED: bad idea)
45-
--some-messages testpb.AMessage (json) (repeated)
45+
--some-messages testpbpulsar.AMessage (json) (repeated)
4646
--str string
4747
--strings strings
4848
--timestamp timestamp (RFC 3339)

client/v2/autocli/testdata/help-skip.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Querying commands for the testpb.Query service
1+
Querying commands for the testpbpulsar.Query service
22

33
Usage:
44
test skipecho [flags]

client/v2/autocli/testdata/help-toplevel.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ Usage:
66

77
Available Commands:
88
completion Generate the autocompletion script for the specified shell
9-
deprecatedecho Querying commands for the testpb.Query service
9+
deprecatedecho Querying commands for the testpbpulsar.Query service
1010
echo echo echos the value provided by the user
1111
help Help about any command
12-
skipecho Querying commands for the testpb.Query service
12+
skipecho Querying commands for the testpbpulsar.Query service
1313

1414
Flags:
1515
-h, --help help for test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"body":{"messages":[{"@type":"/testpbgogo.MsgRequestGogoOnly","str":"henlo","a_coin":{"denom":"foo","amount":"1"},"an_address":"cosmos1y74p8wyy4enfhfn342njve6cjmj5c8dtl6emdk","a_validator_address":""}],"memo":"","timeout_height":"0","unordered":false,"timeout_timestamp":null,"extension_options":[],"non_critical_extension_options":[]},"auth_info":{"signer_infos":[],"fee":{"amount":[],"gas_limit":"200000","payer":"","granter":""},"tip":null},"signatures":[]}

client/v2/internal/buf.lock

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ deps:
99
- remote: buf.build
1010
owner: cosmos
1111
repository: cosmos-sdk
12-
commit: 05419252bcc241ea8023acf1ed4cadc5
13-
digest: shake256:1e54a48c19a8b59d35e0a7efa76402939f515f2d8005df099856f24c37c20a52800308f025abb8cffcd014d437b49707388aaca4865d9d063d8f25d5d4eb77d5
12+
commit: 34ac2e8322d44db08830e553ad21b93c
13+
digest: shake256:b0dd0de5ef770147002e290a9e9f8de8cc8727cbf932628892fbb9027d5ebec5c2a63c0e340e4ef7af72369ecdf51221dcc385ce2137afaa72532228f6999d74
1414
- remote: buf.build
1515
owner: cosmos
1616
repository: gogo-proto
@@ -19,15 +19,10 @@ deps:
1919
- remote: buf.build
2020
owner: googleapis
2121
repository: googleapis
22-
commit: 7e6f6e774e29406da95bd61cdcdbc8bc
23-
digest: shake256:fe43dd2265ea0c07d76bd925eeba612667cf4c948d2ce53d6e367e1b4b3cb5fa69a51e6acb1a6a50d32f894f054a35e6c0406f6808a483f2752e10c866ffbf73
22+
commit: 751cbe31638d43a9bfb6162cd2352e67
23+
digest: shake256:87f55470d9d124e2d1dedfe0231221f4ed7efbc55bc5268917c678e2d9b9c41573a7f9a557f6d8539044524d9fc5ca8fbb7db05eb81379d168285d76b57eb8a4
2424
- remote: buf.build
2525
owner: protocolbuffers
2626
repository: wellknowntypes
27-
commit: 657250e6a39648cbb169d079a60bd9ba
28-
digest: shake256:00de25001b8dd2e29d85fc4bcc3ede7aed886d76d67f5e0f7a9b320b90f871d3eb73507d50818d823a0512f3f8db77a11c043685528403e31ff3fef18323a9fb
29-
- remote: buf.build
30-
owner: tendermint
31-
repository: tendermint
32-
commit: 33ed361a90514289beabf3189e1d7665
33-
digest: shake256:038267e06294714fd883610626554b04a127b576b4e253befb4206cb72d5d3c1eeccacd4b9ec8e3fb891f7c14e1cb0f770c077d2989638995b0a61c85afedb1d
27+
commit: 7727a3b7399540398e5736b9916f0faa
28+
digest: shake256:74c046f009811965d227acd11bb2c6259bf387759d4230b95405e402a2ed4212d6f4babab690407d07bc81095c3917c4ae9144195ddfb081525c9aec58e51173

client/v2/internal/buf.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
version: v1
22
deps:
3+
- buf.build/cosmos/gogo-proto
34
- buf.build/cosmos/cosmos-sdk
45
- buf.build/cosmos/cosmos-proto
6+
- buf.build/protocolbuffers/wellknowntypes
57
lint:
68
use:
79
- STANDARD
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: v1
2+
managed:
3+
enabled: true
4+
go_package_prefix:
5+
default: github.com/cosmos/cosmos-sdk/client/v2/internal
6+
except:
7+
- buf.build/cosmos/cosmos-proto
8+
- buf.build/cosmos/cosmos-sdk
9+
override:
10+
buf.build/cosmos/gogo-proto: github.com/cosmos/gogoproto
11+
plugins:
12+
- name: gocosmos
13+
out: .
14+
opt: plugins=grpc,Mgoogle/protobuf/any.proto=github.com/cosmos/gogoproto/types/any

0 commit comments

Comments
 (0)