-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Changes from all commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
64c76d2
Refactor RegisterQueryServices -> RegisterServices
aaronc d9b8a1d
Cleaner proto files
amaury1093 889025b
Fix tests
aaronc b3c301d
Merge branch 'aaronc/7500-register-services' of ssh://github.com/cosm…
amaury1093 e1482f2
Add MsgServer
amaury1093 6c77f06
Fix lint
amaury1093 c746a2b
Remove MsgServer from configurator for now
amaury1093 ac713d2
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am-7…
amaury1093 59ebc3b
Remove useless file
amaury1093 f18f4e3
Fix build
amaury1093 1ed29df
typo
amaury1093 0d9efc4
Add router
amaury1093 be1a26b
Fix test
amaury1093 b35ce5d
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am-7…
amaury1093 fef78cf
WIP
amaury1093 ff05343
Add router
amaury1093 c323889
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am-7…
amaury1093 13b8dbd
Remove test helper
amaury1093 ab00ebf
Add beginning of test
amaury1093 797ddd3
Move test to simapp?
amaury1093 a65ab55
ServiceMsg implement sdk.Msg
amaury1093 759b2a5
Add handler by MsgServiceRouter
amaury1093 931ca66
Correct signature
amaury1093 f21b332
Add full test
amaury1093 24fa6f0
use TxEncoder
amaury1093 12cbcec
Update baseapp/msg_service_router.go
amaury1093 b599b65
Push changes
amaury1093 0c5ce17
WIP on ServiceMsg unpacking
aaronc 14619c4
Make TestMsgService test pass
aaronc 687fc09
Fix tests
aaronc b6bec3a
Tidying up
aaronc d89f2f6
Tidying up
aaronc 82e2521
Tidying up
aaronc 42f59ff
Add JSON test
aaronc 9a038a5
Add comments
aaronc 8c56444
Tidying
aaronc a6a8132
Merge branch 'master' into am-7500-servicemsg
aaronc aa3192f
Lint
aaronc 55577f6
Register MsgRequest interface
aaronc 701f879
Rename
aaronc 84033e4
Fix tests
aaronc 3762e1d
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am-7…
amaury1093 b30a5eb
RegisterCustomTypeURL
amaury1093 715d002
Add changelog entries
amaury1093 791097a
Put in features
amaury1093 62a887f
Update baseapp/msg_service_router.go
amaury1093 98fa5d2
Update baseapp/msg_service_router.go
amaury1093 c63105d
Update baseapp/msg_service_router.go
amaury1093 2be1a61
Update baseapp/msg_service_router.go
amaury1093 105b04a
Update baseapp/msg_service_router.go
amaury1093 c306dff
Update baseapp/msg_service_router.go
amaury1093 eaebd7e
Update baseapp/msg_service_router.go
amaury1093 7cbc9f9
Address review comments
amaury1093 dfb6cf6
Merge branch 'master' into am-7500-servicemsg
amaury1093 8624ad7
Address nit
amaury1093 880c7cb
Fix lint
amaury1093 304e937
Update codec/types/interface_registry.go
amaury1093 ca9258c
godoc
amaury1093 d6120af
Merge branch 'master' into am-7500-servicemsg
alexanderbez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package baseapp | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
|
||
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 fully-qualified Msg service methods to their handler. | ||
type MsgServiceRouter struct { | ||
interfaceRegistry codectypes.InterfaceRegistry | ||
routes map[string]MsgServiceHandler | ||
} | ||
|
||
var _ gogogrpc.Server = &MsgServiceRouter{} | ||
|
||
// NewMsgServiceRouter creates a new MsgServiceRouter. | ||
func NewMsgServiceRouter() *MsgServiceRouter { | ||
return &MsgServiceRouter{ | ||
routes: map[string]MsgServiceHandler{}, | ||
} | ||
} | ||
|
||
// MsgServiceHandler defines a function type which handles Msg service message. | ||
type MsgServiceHandler = func(ctx sdk.Context, req sdk.MsgRequest) (*sdk.Result, error) | ||
|
||
// Handler returns the MsgServiceHandler for a given query route path or nil | ||
// if not found. | ||
func (msr *MsgServiceRouter) Handler(methodName string) MsgServiceHandler { | ||
return msr.routes[methodName] | ||
} | ||
|
||
// 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 | ||
|
||
// NOTE: This is how we pull the concrete request type for each handler for registering in the InterfaceRegistry. | ||
// This approach is maybe a bit hacky, but less hacky than reflecting on the handler object itself. | ||
// We use a no-op interceptor to avoid actually calling into the handler itself. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
_, _ = methodHandler(nil, context.Background(), func(i interface{}) error { | ||
msg, ok := i.(proto.Message) | ||
if !ok { | ||
// We panic here because there is no other alternative and the app cannot be initialized correctly | ||
// this should only happen if there is a problem with code generation in which case the app won't | ||
// work correctly anyway. | ||
panic(fmt.Errorf("can't register request type %T for service method %s", i, fqMethod)) | ||
} | ||
|
||
msr.interfaceRegistry.RegisterCustomTypeURL((*sdk.MsgRequest)(nil), fqMethod, msg) | ||
amaury1093 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil | ||
}, func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { | ||
return nil, nil | ||
}) | ||
|
||
msr.routes[fqMethod] = func(ctx sdk.Context, req sdk.MsgRequest) (*sdk.Result, error) { | ||
ctx = ctx.WithEventManager(sdk.NewEventManager()) | ||
|
||
// Call the method handler from the service description with the handler object. | ||
res, err := methodHandler(handler, sdk.WrapSDKContext(ctx), func(_ interface{}) error { | ||
// We don't do any decoding here because the decoding was already done. | ||
return nil | ||
}, func(goCtx context.Context, _ interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
goCtx = context.WithValue(goCtx, sdk.SdkContextKey, ctx) | ||
return handler(goCtx, req) | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resMsg, ok := res.(proto.Message) | ||
if !ok { | ||
return nil, fmt.Errorf("can't proto encode %T", resMsg) | ||
} | ||
|
||
return sdk.WrapServiceResult(ctx, resMsg, err) | ||
amaury1093 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
// SetInterfaceRegistry sets the interface registry for the router. | ||
func (msr *MsgServiceRouter) SetInterfaceRegistry(interfaceRegistry codectypes.InterfaceRegistry) { | ||
msr.interfaceRegistry = interfaceRegistry | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package baseapp_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
|
||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
|
||
"github.com/stretchr/testify/require" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/libs/log" | ||
dbm "github.com/tendermint/tm-db" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
"github.com/cosmos/cosmos-sdk/simapp" | ||
"github.com/cosmos/cosmos-sdk/testutil/testdata" | ||
"github.com/cosmos/cosmos-sdk/types/tx/signing" | ||
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
) | ||
|
||
func TestMsgService(t *testing.T) { | ||
priv, _, _ := testdata.KeyTestPubAddr() | ||
encCfg := simapp.MakeEncodingConfig() | ||
db := dbm.NewMemDB() | ||
app := baseapp.NewBaseApp("test", log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, encCfg.TxConfig.TxDecoder()) | ||
app.SetInterfaceRegistry(encCfg.InterfaceRegistry) | ||
testdata.RegisterMsgServer( | ||
app.MsgServiceRouter(), | ||
testdata.MsgServerImpl{}, | ||
) | ||
_ = app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: 1}}) | ||
|
||
msg := testdata.NewServiceMsgCreateDog(&testdata.MsgCreateDog{Dog: &testdata.Dog{Name: "Spot"}}) | ||
txBuilder := encCfg.TxConfig.NewTxBuilder() | ||
txBuilder.SetFeeAmount(testdata.NewTestFeeAmount()) | ||
txBuilder.SetGasLimit(testdata.NewTestGasLimit()) | ||
err := txBuilder.SetMsgs(msg) | ||
require.NoError(t, err) | ||
|
||
// First round: we gather all the signer infos. We use the "set empty | ||
// signature" hack to do that. | ||
sigV2 := signing.SignatureV2{ | ||
PubKey: priv.PubKey(), | ||
Data: &signing.SingleSignatureData{ | ||
SignMode: encCfg.TxConfig.SignModeHandler().DefaultMode(), | ||
Signature: nil, | ||
}, | ||
Sequence: 0, | ||
} | ||
|
||
err = txBuilder.SetSignatures(sigV2) | ||
require.NoError(t, err) | ||
|
||
// Second round: all signer infos are set, so each signer can sign. | ||
signerData := authsigning.SignerData{ | ||
ChainID: "test", | ||
AccountNumber: 0, | ||
Sequence: 0, | ||
} | ||
sigV2, err = tx.SignWithPrivKey( | ||
encCfg.TxConfig.SignModeHandler().DefaultMode(), signerData, | ||
txBuilder, priv, encCfg.TxConfig, 0) | ||
require.NoError(t, err) | ||
err = txBuilder.SetSignatures(sigV2) | ||
require.NoError(t, err) | ||
|
||
// Send the tx to the app | ||
txBytes, err := encCfg.TxConfig.TxEncoder()(txBuilder.GetTx()) | ||
require.NoError(t, err) | ||
res := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes}) | ||
require.Equal(t, abci.CodeTypeOK, res.Code, "res=%+v", res) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not introduced in this PR, but in #7518 (comment)