Skip to content

Commit

Permalink
fix(cosmosclient): use protobuf Any for TxMsgData (#2714)
Browse files Browse the repository at this point in the history
* fix comment

* use proto Any

* Update ignite/pkg/cosmosclient/cosmosclient.go

Co-authored-by: Lucas Btd <[email protected]>

Co-authored-by: Lucas Btd <[email protected]>
  • Loading branch information
Alex Johnson and lumtis authored Aug 9, 2022
1 parent 5e40317 commit c64fe0d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
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

0 comments on commit c64fe0d

Please sign in to comment.