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(cosmosclient): use protobuf Any for TxMsgData #2714

Merged
merged 3 commits into from
Aug 9, 2022
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
1 change: 1 addition & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package docs
import "embed"

// Docs are Ignite CLI docs.
//
//go:embed *.md */*.md
var Docs embed.FS
30 changes: 21 additions & 9 deletions ignite/pkg/cosmosclient/cosmosclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,16 @@ type Response struct {
}

// Decode decodes the proto func response defined in your Msg service into your message type.
// message needs be a pointer. and you need to provide the correct proto message(struct) type to the Decode func.
// message needs to be a pointer. and you need to provide the correct proto message(struct) type to the Decode func.
//
// e.g., for the following CreateChain func the type would be: `types.MsgCreateChainResponse`.
//
// ```proto
// service Msg {
// rpc CreateChain(MsgCreateChain) returns (MsgCreateChainResponse);
// }
//
// service Msg {
// rpc CreateChain(MsgCreateChain) returns (MsgCreateChainResponse);
// }
//
// ```
func (r Response) Decode(message proto.Message) error {
data, err := hex.DecodeString(r.Data)
Expand All @@ -241,14 +243,24 @@ func (r Response) Decode(message proto.Message) error {
return err
}

resData := txMsgData.Data[0]
// check deprecated Data
if len(txMsgData.Data) != 0 {
resData := txMsgData.Data[0]
return prototypes.UnmarshalAny(&prototypes.Any{
// TODO get type url dynamically(basically remove `+ "Response"`) after the following issue has solved.
// https://github.com/ignite/cli/issues/2098
// https://github.com/cosmos/cosmos-sdk/issues/10496
TypeUrl: resData.MsgType + "Response",
Value: resData.Data,
}, message)
}

resData := txMsgData.MsgResponses[0]
return prototypes.UnmarshalAny(&prototypes.Any{
// TODO get type url dynamically(basically remove `+ "Response"`) after the following issue has solved.
// https://github.com/cosmos/cosmos-sdk/issues/10496
TypeUrl: resData.MsgType + "Response",
Value: resData.Data,
TypeUrl: resData.TypeUrl,
Value: resData.Value,
}, message)

}

// ConsensusInfo is the validator consensus info
Expand Down
14 changes: 4 additions & 10 deletions ignite/services/network/testutil/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package testutil

import (
"encoding/hex"
"strings"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
prototypes "github.com/gogo/protobuf/types"
"google.golang.org/protobuf/runtime/protoiface"

"github.com/ignite/cli/ignite/pkg/cosmosclient"
Expand All @@ -17,14 +15,10 @@ import (
// for using as a return result for a cosmosclient mock
func NewResponse(data protoiface.MessageV1) cosmosclient.Response {
marshaler := codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
anyEncoded, _ := prototypes.MarshalAny(data)
txData := &sdk.TxMsgData{Data: []*sdk.MsgData{
{
Data: anyEncoded.Value,
// TODO: Find a better way
MsgType: strings.TrimSuffix(anyEncoded.TypeUrl, "Response"),
},
}}
anyEncoded, _ := codectypes.NewAnyWithValue(data)

txData := &sdk.TxMsgData{MsgResponses: []*codectypes.Any{anyEncoded}}

encodedTxData, _ := marshaler.Marshal(txData)
resp := cosmosclient.Response{
Codec: marshaler,
Expand Down