From 30997f4227170b64be4b7b2181214cb12fb4e71b Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Fri, 18 Aug 2023 21:36:14 +0800 Subject: [PATCH] refactor(baseapp,types)!: msg responses (#17348) Co-authored-by: Julien Robert Co-authored-by: Aleksandr Bezobchuk --- CHANGELOG.md | 2 ++ baseapp/baseapp.go | 8 ++--- baseapp/msg_service_router.go | 16 ++++++++- docs/docs/building-modules/03-msg-services.md | 2 +- types/result.go | 35 ------------------- types/result_test.go | 34 ------------------ 6 files changed, 21 insertions(+), 76 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52fc4329f1d4..e903b21ab327 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -126,6 +126,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/genutil) [#17098](https://github.com/cosmos/cosmos-sdk/pull/17098) `GenAppStateFromConfig`, AddGenesisAccountCmd and `GenTxCmd` takes an addresscodec to decode addresses * (x/distribution) [#17098](https://github.com/cosmos/cosmos-sdk/pull/17098) `NewMsgDepositValidatorRewardsPool`, `NewMsgFundCommunityPool`, `NewMsgWithdrawValidatorCommission` and `NewMsgWithdrawDelegatorReward` takes a string instead of `sdk.ValAddress` or `sdk.AccAddress` * (client) [#17215](https://github.com/cosmos/cosmos-sdk/pull/17215) `server.StartCmd`,`server.ExportCmd`,`server.NewRollbackCmd`,`pruning.Cmd`,`genutilcli.InitCmd`,`genutilcli.GenTxCmd`,`genutilcli.CollectGenTxsCmd`,`genutilcli.AddGenesisAccountCmd`, do not take a home directory anymore. It is inferred from the root command. +* (types) [#17348](https://github.com/cosmos/cosmos-sdk/pull/17348) Remove the `WrapServiceResult` function. + * The `*sdk.Result` returned by the msg server router will not contain the `.Data` field. * (x/staking) [#17157](https://github.com/cosmos/cosmos-sdk/pull/17157) `GetValidatorsByPowerIndexKey` and `ValidateBasic` for historical info takes a validator address codec in order to be able to decode/encode addresses. * `GetOperator()` now returns the address as it is represented in state, by default this is an encoded address * `GetConsAddr() ([]byte, error)` returns `[]byte` instead of sdk.ConsAddres. diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 84fb00a066f7..34b9548685c8 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -959,7 +959,7 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte) (gInfo sdk.GasInfo, res // Result is returned. The caller must not commit state if an error is returned. func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, msgsV2 []protov2.Message, mode execMode) (*sdk.Result, error) { events := sdk.EmptyEvents() - var msgResponses []*codectypes.Any + msgResponses := make([]*codectypes.Any, 0, len(msgs)) // NOTE: GasWanted is determined by the AnteHandler and GasUsed by the GasMeter. for i, msg := range msgs { @@ -997,9 +997,8 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, msgsV2 []protov2.Me // Each individual sdk.Result that went through the MsgServiceRouter // (which should represent 99% of the Msgs now, since everyone should - // be using protobuf Msgs) has exactly one Msg response, set inside - // `WrapServiceResult`. We take that Msg response, and aggregate it - // into an array. + // be using protobuf Msgs) has exactly one Msg response. + // We take that Msg response, and aggregate it into an array. if len(msgResult.MsgResponses) > 0 { msgResponse := msgResult.MsgResponses[0] if msgResponse == nil { @@ -1007,7 +1006,6 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, msgsV2 []protov2.Me } msgResponses = append(msgResponses, msgResponse) } - } data, err := makeABCIData(msgResponses) diff --git a/baseapp/msg_service_router.go b/baseapp/msg_service_router.go index b3876075af98..b31a8dc5a139 100644 --- a/baseapp/msg_service_router.go +++ b/baseapp/msg_service_router.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + abci "github.com/cometbft/cometbft/abci/types" gogogrpc "github.com/cosmos/gogoproto/grpc" "github.com/cosmos/gogoproto/proto" "google.golang.org/grpc" @@ -157,7 +158,20 @@ func (msr *MsgServiceRouter) RegisterService(sd *grpc.ServiceDesc, handler inter return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting proto.Message, got %T", resMsg) } - return sdk.WrapServiceResult(ctx, resMsg, err) + anyResp, err := codectypes.NewAnyWithValue(resMsg) + if err != nil { + return nil, err + } + + var events []abci.Event + if evtMgr := ctx.EventManager(); evtMgr != nil { + events = evtMgr.ABCIEvents() + } + + return &sdk.Result{ + Events: events, + MsgResponses: []*codectypes.Any{anyResp}, + }, nil } } } diff --git a/docs/docs/building-modules/03-msg-services.md b/docs/docs/building-modules/03-msg-services.md index 918a5d45a4a0..ac275a7deadc 100644 --- a/docs/docs/building-modules/03-msg-services.md +++ b/docs/docs/building-modules/03-msg-services.md @@ -92,7 +92,7 @@ ctx.EventManager().EmitEvent( These events are relayed back to the underlying consensus engine and can be used by service providers to implement services around the application. Click [here](../core/08-events.md) to learn more about events. -The invoked `msgServer` method returns a `proto.Message` response and an `error`. These return values are then wrapped into an `*sdk.Result` or an `error` using `sdk.WrapServiceResult(ctx context.Context, res proto.Message, err error)`: +The invoked `msgServer` method returns a `proto.Message` response and an `error`. These return values are then wrapped into an `*sdk.Result` or an `error`: ```go reference https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-alpha.0/baseapp/msg_service_router.go#L160 diff --git a/types/result.go b/types/result.go index ea3d491025fa..4463bf8da058 100644 --- a/types/result.go +++ b/types/result.go @@ -5,10 +5,8 @@ import ( "encoding/json" "strings" - abci "github.com/cometbft/cometbft/abci/types" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" coretypes "github.com/cometbft/cometbft/rpc/core/types" - "github.com/cosmos/gogoproto/proto" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -195,39 +193,6 @@ func (r TxResponse) GetTx() HasMsgs { return nil } -// WrapServiceResult wraps a result from a protobuf RPC service method call (res proto.Message, err error) -// in a Result object or error. This method takes care of marshaling the res param to -// protobuf and attaching any events on the ctx.EventManager() to the Result. -func WrapServiceResult(ctx Context, res proto.Message, err error) (*Result, error) { - if err != nil { - return nil, err - } - - any, err := codectypes.NewAnyWithValue(res) - if err != nil { - return nil, err - } - - var data []byte - if res != nil { - data, err = proto.Marshal(res) - if err != nil { - return nil, err - } - } - - var events []abci.Event - if evtMgr := ctx.EventManager(); evtMgr != nil { - events = evtMgr.ABCIEvents() - } - - return &Result{ - Data: data, - Events: events, - MsgResponses: []*codectypes.Any{any}, - }, nil -} - // calculate total pages in an overflow safe manner func calcTotalPages(totalCount, limit int64) int64 { totalPages := int64(0) diff --git a/types/result_test.go b/types/result_test.go index 5911bda5b972..19e43cd5e298 100644 --- a/types/result_test.go +++ b/types/result_test.go @@ -2,7 +2,6 @@ package types_test import ( "encoding/hex" - "fmt" "strings" "testing" "time" @@ -11,12 +10,9 @@ import ( cmtt "github.com/cometbft/cometbft/proto/tendermint/types" coretypes "github.com/cometbft/cometbft/rpc/core/types" cmt "github.com/cometbft/cometbft/types" - "github.com/golang/protobuf/proto" //nolint:staticcheck // grpc-gateway uses deprecated golang/protobuf - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -178,33 +174,3 @@ func (s *resultTestSuite) TestResponseResultBlock() { s.Require().Equal(want, sdk.NewResponseResultBlock(resultBlock, timestampStr)) } - -func TestWrapServiceResult(t *testing.T) { - ctx := sdk.Context{} - - res, err := sdk.WrapServiceResult(ctx, nil, fmt.Errorf("test")) - require.Nil(t, res) - require.NotNil(t, err) - - res, err = sdk.WrapServiceResult(ctx, &testdata.Dog{}, nil) - require.NotNil(t, res) - require.Nil(t, err) - require.Empty(t, res.Events) - - ctx = ctx.WithEventManager(sdk.NewEventManager()) - ctx.EventManager().EmitEvent(sdk.NewEvent("test")) - res, err = sdk.WrapServiceResult(ctx, &testdata.Dog{}, nil) - require.NotNil(t, res) - require.Nil(t, err) - require.Len(t, res.Events, 1) - - spot := testdata.Dog{Name: "spot"} - res, err = sdk.WrapServiceResult(ctx, &spot, nil) - require.NotNil(t, res) - require.Nil(t, err) - require.Len(t, res.Events, 1) - var spot2 testdata.Dog - err = proto.Unmarshal(res.Data, &spot2) - require.NoError(t, err) - require.Equal(t, spot, spot2) -}