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

feat(baseapp): Remove hybrid handlers #20782

Closed
wants to merge 18 commits into from
Closed
Prev Previous commit
Next Next commit
checkpoint
  • Loading branch information
testinginprod committed Jun 27, 2024
commit b620b307b3a4d79ff8854c0af807660e3370f237
6 changes: 5 additions & 1 deletion api/cosmos/staking/v1beta1/query.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 16 additions & 25 deletions x/accounts/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package accounts

import (
"context"
"errors"
"strconv"

"cosmossdk.io/x/accounts/testing/mockmodule"
"github.com/cosmos/gogoproto/types"

bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
"cosmossdk.io/collections"
"cosmossdk.io/x/accounts/accountstd"
"cosmossdk.io/x/accounts/internal/implementation"
Expand All @@ -28,10 +28,7 @@ type TestAccount struct {
func (t TestAccount) RegisterInitHandler(builder *implementation.InitBuilder) {
implementation.RegisterInitHandler(builder, func(ctx context.Context, _ *types.Empty) (*types.Empty, error) {
// we also force a module call here to test things work as expected.
_, err := implementation.QueryModule[bankv1beta1.QueryBalanceResponse](ctx, &bankv1beta1.QueryBalanceRequest{
Address: string(implementation.Whoami(ctx)),
Denom: "atom",
})
_, err := implementation.QueryModule[mockmodule.QueryEchoResponse](ctx, &mockmodule.QueryEchoRequest{Msg: "echo"})
return &types.Empty{}, err
})
}
Expand All @@ -52,22 +49,18 @@ func (t TestAccount) RegisterExecuteHandlers(builder *implementation.ExecuteBuil

// this is for intermodule comms testing, we simulate a bank send
implementation.RegisterExecuteHandler(builder, func(ctx context.Context, req *types.Int64Value) (*types.Empty, error) {
resp, err := implementation.ExecModule[bankv1beta1.MsgSendResponse](ctx, &bankv1beta1.MsgSend{
FromAddress: string(implementation.Whoami(ctx)),
ToAddress: "recipient",
Amount: []*basev1beta1.Coin{
{
Denom: "test",
Amount: strconv.FormatInt(req.Value, 10),
},
},
})
resp, err := implementation.ExecModule[mockmodule.MsgEchoResponse](
ctx,
&mockmodule.MsgEcho{Msg: "echo", Sender: string(implementation.Whoami(ctx))})
if err != nil {
return nil, err
}
if resp == nil {
panic("nil response") // should never happen
}
if resp.MsgEcho != "echo" {
return nil, errors.New("bad echo")
}

return &types.Empty{}, nil
})
Expand All @@ -90,19 +83,17 @@ func (t TestAccount) RegisterQueryHandlers(builder *implementation.QueryBuilder)
// test intermodule comms, we simulate someone is sending the account a request for the accounts balance
// of a given denom.
implementation.RegisterQueryHandler(builder, func(ctx context.Context, req *types.StringValue) (*types.Int64Value, error) {
resp, err := implementation.QueryModule[bankv1beta1.QueryBalanceResponse](ctx, &bankv1beta1.QueryBalanceRequest{
Address: string(implementation.Whoami(ctx)),
Denom: req.Value,
})
resp, err := implementation.QueryModule[mockmodule.QueryEchoResponse](ctx, &mockmodule.QueryEchoRequest{Msg: "echo"})
if err != nil {
return nil, err
}

amt, err := strconv.ParseInt(resp.Balance.Amount, 10, 64)
if err != nil {
return nil, err
if resp == nil {
return nil, errors.New("nil response")
}
if resp.MsgEcho != "echo" {
return nil, errors.New("invalid echo response")
}
return &types.Int64Value{Value: amt}, nil
return &types.Int64Value{}, nil
})

// genesis testing; DoubleValue does not make sense as a request type for this query, but empty is already taken
Expand Down
13 changes: 8 additions & 5 deletions x/accounts/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"errors"
"fmt"

"github.com/cosmos/gogoproto/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/runtime/protoiface"

"cosmossdk.io/collections"
Expand Down Expand Up @@ -64,7 +66,7 @@ func NewKeeper(
sb := collections.NewSchemaBuilder(env.KVStoreService)
keeper := Keeper{
Environment: env,
codec: cdc,
getSenders: cdc.GetMsgSigners,
addressCodec: addressCodec,
makeSendCoinsMsg: coinfTransferer.MakeTransferCoinsMessage,
Schema: collections.Schema{},
Expand All @@ -90,8 +92,9 @@ func NewKeeper(
type Keeper struct {
appmodule.Environment

addressCodec address.Codec
codec codec.Codec
addressCodec address.Codec
getSenders func(msg proto.Message) ([][]byte, protoreflect.Message, error)

makeSendCoinsMsg func(ctx context.Context, from []byte, to []byte, amount sdk.Coins) (implementation.ProtoMsg, implementation.ProtoMsg, error)

accounts map[string]implementation.Implementation
Expand Down Expand Up @@ -380,7 +383,7 @@ func (k Keeper) sendAnyMessages(ctx context.Context, sender []byte, anyMessages
// It should be used when the response type is not known by the caller.
func (k Keeper) SendModuleMessageUntyped(ctx context.Context, sender []byte, msg implementation.ProtoMsg) (implementation.ProtoMsg, error) {
// do sender assertions.
wantSenders, _, err := k.codec.GetMsgSigners(msg)
wantSenders, _, err := k.getSenders(msg)
if err != nil {
return nil, fmt.Errorf("cannot get signers: %w", err)
}
Expand All @@ -403,7 +406,7 @@ func (k Keeper) SendModuleMessageUntyped(ctx context.Context, sender []byte, msg
// is not trying to impersonate another account.
func (k Keeper) sendModuleMessage(ctx context.Context, sender []byte, msg, msgResp implementation.ProtoMsg) error {
// do sender assertions.
wantSenders, _, err := k.codec.GetMsgSigners(msg)
wantSenders, _, err := k.getSenders(msg)
if err != nil {
return fmt.Errorf("cannot get signers: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions x/accounts/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ func TestKeeper_Query(t *testing.T) {
})

t.Run("query module", func(t *testing.T) {
resp, err := m.Query(ctx, accAddr, &types.StringValue{Value: "atom"})
_, err := m.Query(ctx, accAddr, &types.StringValue{Value: "atom"})
require.NoError(t, err)
require.True(t, implementation.Equal(&types.Int64Value{Value: 1000}, resp))
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";

package cosmos.accounts.testing.mockmodule;

option go_package = "cosmossdk.io/x/accounts/testing/mockmodule";

import "cosmos/msg/v1/msg.proto";

message MsgEcho {
option (cosmos.msg.v1.signer) = "sender";
string sender = 1;
string msg = 2;
}

message MsgEchoResponse {
string msg_echo = 1;
}

message QueryEchoRequest {
string msg = 1;
}

message QueryEchoResponse {
string msg_echo = 1;
}
71 changes: 71 additions & 0 deletions x/accounts/testing/mockmodule/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package mockmodule

import (
"context"
"errors"
"fmt"

"cosmossdk.io/core/router"
"google.golang.org/protobuf/runtime/protoiface"
)

type msgRouter struct {
}

func (m msgRouter) CanInvoke(ctx context.Context, typeURL string) error {
return errors.New("not implemented")
}

func (m msgRouter) InvokeTyped(ctx context.Context, req, res protoiface.MessageV1) error {
typedReq, ok := req.(*MsgEcho)
if !ok {
return fmt.Errorf("invalid msg request got %T", req)
}
typedRes, ok := res.(*MsgEchoResponse)
if !ok {
return fmt.Errorf("invalid msg response got %T", res)
}
typedRes.MsgEcho = typedReq.Msg
return nil
}

func (m msgRouter) InvokeUntyped(ctx context.Context, req protoiface.MessageV1) (res protoiface.MessageV1, err error) {
// TODO implement me
panic("implement me")
}

func MockMsgRouter() router.Service {
return msgRouter{}
}

type queryRouter struct {
}

func (q queryRouter) CanInvoke(ctx context.Context, typeURL string) error {
return errors.New("do not call")
}

func (q queryRouter) InvokeTyped(ctx context.Context, req, res protoiface.MessageV1) error {
typedReq, ok := req.(*QueryEchoRequest)
if !ok {
return fmt.Errorf("invalid query request got %T", req)
}
typedRes, ok := res.(*QueryEchoResponse)
if !ok {
return fmt.Errorf("invalid query response got %T", res)
}
typedRes.MsgEcho = typedReq.Msg
return nil
}

func (q queryRouter) InvokeUntyped(ctx context.Context, req protoiface.MessageV1) (res protoiface.MessageV1, err error) {
typedReq, ok := req.(*QueryEchoRequest)
if !ok {
return nil, fmt.Errorf("invalid request")
}
return &QueryEchoResponse{MsgEcho: typedReq.Msg}, nil
}

func MockQueryRouter() router.Service {
return queryRouter{}
}
Loading
Loading