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

Add ADR 031 BaseApp and codec infrastructure #7519

Merged
merged 59 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
64c76d2
Refactor RegisterQueryServices -> RegisterServices
aaronc Oct 12, 2020
d9b8a1d
Cleaner proto files
amaury1093 Oct 12, 2020
889025b
Fix tests
aaronc Oct 12, 2020
b3c301d
Merge branch 'aaronc/7500-register-services' of ssh://github.com/cosm…
amaury1093 Oct 12, 2020
e1482f2
Add MsgServer
amaury1093 Oct 12, 2020
6c77f06
Fix lint
amaury1093 Oct 12, 2020
c746a2b
Remove MsgServer from configurator for now
amaury1093 Oct 12, 2020
ac713d2
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am-7…
amaury1093 Oct 12, 2020
59ebc3b
Remove useless file
amaury1093 Oct 12, 2020
f18f4e3
Fix build
amaury1093 Oct 12, 2020
1ed29df
typo
amaury1093 Oct 12, 2020
0d9efc4
Add router
amaury1093 Oct 13, 2020
be1a26b
Fix test
amaury1093 Oct 13, 2020
b35ce5d
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am-7…
amaury1093 Oct 13, 2020
fef78cf
WIP
amaury1093 Oct 13, 2020
ff05343
Add router
amaury1093 Oct 13, 2020
c323889
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am-7…
amaury1093 Oct 13, 2020
13b8dbd
Remove test helper
amaury1093 Oct 13, 2020
ab00ebf
Add beginning of test
amaury1093 Oct 13, 2020
797ddd3
Move test to simapp?
amaury1093 Oct 13, 2020
a65ab55
ServiceMsg implement sdk.Msg
amaury1093 Oct 13, 2020
759b2a5
Add handler by MsgServiceRouter
amaury1093 Oct 13, 2020
931ca66
Correct signature
amaury1093 Oct 13, 2020
f21b332
Add full test
amaury1093 Oct 13, 2020
24fa6f0
use TxEncoder
amaury1093 Oct 13, 2020
12cbcec
Update baseapp/msg_service_router.go
amaury1093 Oct 13, 2020
b599b65
Push changes
amaury1093 Oct 13, 2020
0c5ce17
WIP on ServiceMsg unpacking
aaronc Oct 13, 2020
14619c4
Make TestMsgService test pass
aaronc Oct 13, 2020
687fc09
Fix tests
aaronc Oct 13, 2020
b6bec3a
Tidying up
aaronc Oct 13, 2020
d89f2f6
Tidying up
aaronc Oct 13, 2020
82e2521
Tidying up
aaronc Oct 13, 2020
42f59ff
Add JSON test
aaronc Oct 13, 2020
9a038a5
Add comments
aaronc Oct 13, 2020
8c56444
Tidying
aaronc Oct 13, 2020
a6a8132
Merge branch 'master' into am-7500-servicemsg
aaronc Oct 13, 2020
aa3192f
Lint
aaronc Oct 13, 2020
55577f6
Register MsgRequest interface
aaronc Oct 13, 2020
701f879
Rename
aaronc Oct 13, 2020
84033e4
Fix tests
aaronc Oct 13, 2020
3762e1d
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am-7…
amaury1093 Oct 14, 2020
b30a5eb
RegisterCustomTypeURL
amaury1093 Oct 14, 2020
715d002
Add changelog entries
amaury1093 Oct 14, 2020
791097a
Put in features
amaury1093 Oct 14, 2020
62a887f
Update baseapp/msg_service_router.go
amaury1093 Oct 14, 2020
98fa5d2
Update baseapp/msg_service_router.go
amaury1093 Oct 14, 2020
c63105d
Update baseapp/msg_service_router.go
amaury1093 Oct 14, 2020
2be1a61
Update baseapp/msg_service_router.go
amaury1093 Oct 14, 2020
105b04a
Update baseapp/msg_service_router.go
amaury1093 Oct 14, 2020
c306dff
Update baseapp/msg_service_router.go
amaury1093 Oct 14, 2020
eaebd7e
Update baseapp/msg_service_router.go
amaury1093 Oct 14, 2020
7cbc9f9
Address review comments
amaury1093 Oct 14, 2020
dfb6cf6
Merge branch 'master' into am-7500-servicemsg
amaury1093 Oct 14, 2020
8624ad7
Address nit
amaury1093 Oct 14, 2020
880c7cb
Fix lint
amaury1093 Oct 14, 2020
304e937
Update codec/types/interface_registry.go
amaury1093 Oct 15, 2020
ca9258c
godoc
amaury1093 Oct 15, 2020
d6120af
Merge branch 'master' into am-7500-servicemsg
alexanderbez Oct 15, 2020
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
47 changes: 27 additions & 20 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ type (
// BaseApp reflects the ABCI application implementation.
type BaseApp struct { // nolint: maligned
// initialized on creation
logger log.Logger
name string // application name from abci.Info
db dbm.DB // common DB backend
cms sdk.CommitMultiStore // Main (uncached) state
storeLoader StoreLoader // function to handle store loading, may be overridden with SetStoreLoader()
router sdk.Router // handle any kind of message
queryRouter sdk.QueryRouter // router for redirecting query calls
grpcQueryRouter *GRPCQueryRouter // router for redirecting gRPC query calls
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx
logger log.Logger
name string // application name from abci.Info
db dbm.DB // common DB backend
cms sdk.CommitMultiStore // Main (uncached) state
storeLoader StoreLoader // function to handle store loading, may be overridden with SetStoreLoader()
router sdk.Router // handle any kind of message
queryRouter sdk.QueryRouter // router for redirecting query calls
grpcQueryRouter *GRPCQueryRouter // router for redirecting gRPC query calls
msgServiceRouter *MsgServiceRouter // router for redirecting Msg service messages
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx

anteHandler sdk.AnteHandler // ante handler for fee and auth
initChainer sdk.InitChainer // initialize state with validators and state blob
Expand Down Expand Up @@ -136,16 +137,17 @@ func NewBaseApp(
name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecoder, options ...func(*BaseApp),
) *BaseApp {
app := &BaseApp{
logger: logger,
name: name,
db: db,
cms: store.NewCommitMultiStore(db),
storeLoader: DefaultStoreLoader,
router: NewRouter(),
queryRouter: NewQueryRouter(),
grpcQueryRouter: NewGRPCQueryRouter(),
txDecoder: txDecoder,
fauxMerkleMode: false,
logger: logger,
name: name,
db: db,
cms: store.NewCommitMultiStore(db),
storeLoader: DefaultStoreLoader,
router: NewRouter(),
queryRouter: NewQueryRouter(),
grpcQueryRouter: NewGRPCQueryRouter(),
msgServiceRouter: NewMsgServiceRouter(),
txDecoder: txDecoder,
fauxMerkleMode: false,
}

for _, option := range options {
Expand Down Expand Up @@ -683,7 +685,12 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*s
}

msgRoute := msg.Route()
handler := app.router.Route(ctx, msgRoute)
// First, check if the MsgService router handles the route.
handler := app.MsgServiceRouter().Route(msgRoute)
// If not, then check if the legacy router handles the route.
if handler == nil {
handler = app.router.Route(ctx, msgRoute)
}
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

if handler == nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized message route: %s; message index: %d", msgRoute, i)
Expand Down
6 changes: 3 additions & 3 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1692,9 +1692,9 @@ func TestQuery(t *testing.T) {

func TestGRPCQuery(t *testing.T) {
grpcQueryOpt := func(bapp *BaseApp) {
testdata.RegisterTestServiceServer(
testdata.RegisterQueryServer(
bapp.GRPCQueryRouter(),
testdata.TestServiceImpl{},
testdata.QueryImpl{},
)
}

Expand All @@ -1711,7 +1711,7 @@ func TestGRPCQuery(t *testing.T) {

reqQuery := abci.RequestQuery{
Data: reqBz,
Path: "/testdata.TestService/SayHello",
Path: "/testdata.Query/SayHello",
}

resQuery := app.Query(reqQuery)
Expand Down
9 changes: 4 additions & 5 deletions baseapp/grpcrouter_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
gocontext "context"
"fmt"

"github.com/cosmos/cosmos-sdk/codec/types"

gogogrpc "github.com/gogo/protobuf/grpc"
abci "github.com/tendermint/tendermint/abci/types"
"google.golang.org/grpc"

"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -22,6 +21,9 @@ type QueryServiceTestHelper struct {
ctx sdk.Context
}

var _ gogogrpc.Server = &QueryServiceTestHelper{}
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
var _ gogogrpc.ClientConn = &QueryServiceTestHelper{}

// NewQueryServerTestHelper creates a new QueryServiceTestHelper that wraps
// the provided sdk.Context
func NewQueryServerTestHelper(ctx sdk.Context, interfaceRegistry types.InterfaceRegistry) *QueryServiceTestHelper {
Expand Down Expand Up @@ -62,6 +64,3 @@ func (q *QueryServiceTestHelper) Invoke(_ gocontext.Context, method string, args
func (q *QueryServiceTestHelper) NewStream(gocontext.Context, *grpc.StreamDesc, string, ...grpc.CallOption) (grpc.ClientStream, error) {
return nil, fmt.Errorf("not supported")
}

var _ gogogrpc.Server = &QueryServiceTestHelper{}
var _ gogogrpc.ClientConn = &QueryServiceTestHelper{}
4 changes: 2 additions & 2 deletions baseapp/grpcrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func TestGRPCRouter(t *testing.T) {
qr := NewGRPCQueryRouter()
interfaceRegistry := testdata.NewTestInterfaceRegistry()
qr.SetInterfaceRegistry(interfaceRegistry)
testdata.RegisterTestServiceServer(qr, testdata.TestServiceImpl{})
testdata.RegisterQueryServer(qr, testdata.QueryImpl{})
helper := &QueryServiceTestHelper{
GRPCQueryRouter: qr,
ctx: sdk.Context{}.WithContext(context.Background()),
}
client := testdata.NewTestServiceClient(helper)
client := testdata.NewQueryClient(helper)

res, err := client.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
require.Nil(t, err)
Expand Down
4 changes: 4 additions & 0 deletions baseapp/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package baseapp

// MsgServiceRouter returns the MsgServiceRouter of a BaseApp.
func (app *BaseApp) MsgServiceRouter() *MsgServiceRouter { return app.msgServiceRouter }
83 changes: 83 additions & 0 deletions baseapp/msg_service_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package baseapp

import (
"fmt"

gogogrpc "github.com/gogo/protobuf/grpc"
"google.golang.org/grpc"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// MsgServiceRouter routes Msg Service fully-qualified service methods to their
// handler.
type MsgServiceRouter struct {
interfaceRegistry codectypes.InterfaceRegistry
routes map[string]sdk.Handler
}

var _ gogogrpc.Server = &MsgServiceRouter{}

// NewMsgServiceRouter creates a new MsgServiceRouter.
func NewMsgServiceRouter() *MsgServiceRouter {
return &MsgServiceRouter{
routes: map[string]sdk.Handler{},
}
}

// sdk.Handler defines a function type which handles Msg service message.
// It's similar to sdk.Handler, but with simplified version of `Msg`, without
// `Route()`, `Type()` and `GetSignBytes()`.
type MsgServiceHandler = func(ctx sdk.Context, msgRequest sdk.MsgRequest) (*sdk.Result, error)
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

// Route returns the sdk.Handler for a given query route path or nil
// if not found.
func (msr *MsgServiceRouter) Route(path string) sdk.Handler {
handler, found := msr.routes[path]
if !found {
return nil
}

return handler
}

// RegisterService implements the gRPC Server.RegisterService method. sd is a gRPC
// service description, handler is an object which implements that gRPC service.
func (msr *MsgServiceRouter) RegisterService(sd *grpc.ServiceDesc, handler interface{}) {
// adds a top-level query handler based on the gRPC service name
for _, method := range sd.Methods {
fqMethod := fmt.Sprintf("/%s/%s", sd.ServiceName, method.MethodName)
methodHandler := method.Handler

msr.routes[fqMethod] = func(ctx sdk.Context, _ sdk.Msg) (*sdk.Result, error) {
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// call the method handler from the service description with the handler object,
// a wrapped sdk.Context with proto-unmarshaled data from the ABCI request data
res, err := methodHandler(handler, sdk.WrapSDKContext(ctx), func(i interface{}) error {
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
if msr.interfaceRegistry != nil {
return codectypes.UnpackInterfaces(i, msr.interfaceRegistry)
}
return nil
}, nil)
if err != nil {
return nil, err
}

// proto marshal the result bytes
resBytes, err := protoCodec.Marshal(res)
if err != nil {
return nil, err
}

// return the result bytes as the response value
return &sdk.Result{
Data: resBytes,
}, nil
}
}
}

// SetInterfaceRegistry sets the interface registry for the router.
func (msr *MsgServiceRouter) SetInterfaceRegistry(interfaceRegistry codectypes.InterfaceRegistry) {
msr.interfaceRegistry = interfaceRegistry
}
2 changes: 1 addition & 1 deletion client/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestCLIQueryConn(t *testing.T) {
n := network.New(t, cfg)
defer n.Cleanup()

testClient := testdata.NewTestServiceClient(n.Validators[0].ClientCtx)
testClient := testdata.NewQueryClient(n.Validators[0].ClientCtx)
res, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
require.NoError(t, err)
require.Equal(t, "hello", res.Message)
Expand Down
2 changes: 1 addition & 1 deletion client/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *IntegrationTestSuite) TestGRPCQuery() {
val0 := s.network.Validators[0]

// gRPC query to test service should work
testClient := testdata.NewTestServiceClient(val0.ClientCtx)
testClient := testdata.NewQueryClient(val0.ClientCtx)
testRes, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
s.Require().NoError(err)
s.Require().Equal("hello", testRes.Message)
Expand Down
2 changes: 1 addition & 1 deletion scripts/protocgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ done

# generate codec/testdata proto code
protoc -I "proto" -I "third_party/proto" -I "testutil/testdata" --gocosmos_out=plugins=interfacetype+grpc,\
Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types:. ./testutil/testdata/proto.proto
Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types:. ./testutil/testdata/*.proto

# move proto files to the right places
cp -r github.com/cosmos/cosmos-sdk/* ./
Expand Down
2 changes: 1 addition & 1 deletion server/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *IntegrationTestSuite) TestGRPCServer() {
s.Require().NoError(err)

// gRPC query to test service should work
testClient := testdata.NewTestServiceClient(conn)
testClient := testdata.NewQueryClient(conn)
testRes, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
s.Require().NoError(err)
s.Require().Equal("hello", testRes.Message)
Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func NewSimApp(
app.mm.RegisterServices(module.NewConfigurator(app.GRPCQueryRouter()))

// add test gRPC service for testing gRPC queries in isolation
testdata.RegisterTestServiceServer(app.GRPCQueryRouter(), testdata.TestServiceImpl{})
testdata.RegisterQueryServer(app.GRPCQueryRouter(), testdata.QueryImpl{})

// create the simulation manager and define the order of the modules for deterministic simulations
//
Expand Down
38 changes: 38 additions & 0 deletions simapp/msg_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package simapp_test

import (
"os"
"testing"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)

func TestMsgService(t *testing.T) {
db := dbm.NewMemDB()
encCfg := simapp.MakeEncodingConfig()
msgServiceOpt := func(bapp *baseapp.BaseApp) {
testdata.RegisterMsgServer(
bapp.MsgServiceRouter(),
testdata.MsgImpl{},
)
}
app := simapp.NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 0, encCfg, msgServiceOpt)

msg, err := testdata.NewServiceMsgCreateDog(&testdata.MsgCreateDog{Dog: &testdata.Dog{Name: "Spot"}})
require.NoError(t, err)

txf := tx.Factory{}.
WithTxConfig(encCfg.TxConfig).
WithAccountNumber(50).
WithSequence(23).
WithFees("50stake").
WithMemo("memo").
WithChainID("test-chain")
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"github.com/cosmos/cosmos-sdk/codec/types"
)

type TestServiceImpl struct{}
type QueryImpl struct{}

func (e TestServiceImpl) TestAny(_ context.Context, request *TestAnyRequest) (*TestAnyResponse, error) {
var _ QueryServer = QueryImpl{}

func (e QueryImpl) TestAny(_ context.Context, request *TestAnyRequest) (*TestAnyResponse, error) {
animal, ok := request.AnyAnimal.GetCachedValue().(Animal)
if !ok {
return nil, fmt.Errorf("expected Animal")
Expand All @@ -28,17 +30,15 @@ func (e TestServiceImpl) TestAny(_ context.Context, request *TestAnyRequest) (*T
}}, nil
}

func (e TestServiceImpl) Echo(_ context.Context, req *EchoRequest) (*EchoResponse, error) {
func (e QueryImpl) Echo(_ context.Context, req *EchoRequest) (*EchoResponse, error) {
return &EchoResponse{Message: req.Message}, nil
}

func (e TestServiceImpl) SayHello(_ context.Context, request *SayHelloRequest) (*SayHelloResponse, error) {
func (e QueryImpl) SayHello(_ context.Context, request *SayHelloRequest) (*SayHelloResponse, error) {
greeting := fmt.Sprintf("Hello %s!", request.Name)
return &SayHelloResponse{Greeting: greeting}, nil
}

var _ TestServiceServer = TestServiceImpl{}

var _ types.UnpackInterfacesMessage = &TestAnyRequest{}

func (m *TestAnyRequest) UnpackInterfaces(unpacker types.AnyUnpacker) error {
Expand Down
16 changes: 16 additions & 0 deletions testutil/testdata/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package testdata

import (
"context"
)

type MsgImpl struct{}

var _ MsgServer = MsgImpl{}

// CreateDog implements the MsgServer interface.
func (m MsgImpl) CreateDog(_ context.Context, msg *MsgCreateDog) (*MsgCreateDogResponse, error) {
return &MsgCreateDogResponse{
Name: msg.Dog.Name,
}, nil
}
Loading