refactor(baseapp,types)!: msg responses (#17348)
Co-authored-by: Julien Robert <julien@rbrt.fr> Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
parent
0dc32a0762
commit
30997f4227
@ -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.
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user