Skip to content

Commit

Permalink
refactor: remove dependency x/module -> sdk through InterfaceRegistry (
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski authored Mar 6, 2024
1 parent 25aea8a commit 4edf6b2
Show file tree
Hide file tree
Showing 56 changed files with 283 additions and 234 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
* (types) [#18607](https://github.com/cosmos/cosmos-sdk/pull/18607) Removed address verifier from global config, moved verifier function to bech32 codec.
* (server) [#18909](https://github.com/cosmos/cosmos-sdk/pull/18909) Remove configuration endpoint on grpc reflection endpoint in favour of auth module bech32prefix endpoint already exposed.
* (crypto) [#19541](https://github.com/cosmos/cosmos-sdk/pull/19541) The deprecated `FromTmProtoPublicKey`, `ToTmProtoPublicKey`, `FromTmPubKeyInterface` and `ToTmPubKeyInterface` functions have been removed. Use their replacements (`Cmt` instead of `Tm`) instead.
* (types) [#19652](https://github.com/cosmos/cosmos-sdk/pull/19652)
* Moved`types/module.HasRegisterInterfaces` to `cosmossdk.io/core`.
* Moved `RegisterInterfaces` and `RegisterImplementations` from `InterfaceRegistry` to `cosmossdk.io/core/registry.LegacyRegistry` interface.

### Client Breaking Changes

Expand Down
6 changes: 6 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ If your module requires a message server or query server, it should be passed in
+govKeeper := govkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[govtypes.StoreKey]), logger, runtime.EnvWithRouterService(app.GRPCQueryRouter(), app.MsgServiceRouter())), app.AuthKeeper, app.BankKeeper, app.StakingKeeper, app.PoolKeeper, govConfig, authtypes.NewModuleAddress(govtypes.ModuleName).String())
```

The signature of the extension interface `HasRegisterInterfaces` has been changed to accept a `cosmossdk.io/core/registry.LegacyRegistry` instead of a `codec.InterfaceRegistry`. `HasRegisterInterfaces` is now a part of `cosmossdk.io/core/appmodule`. Modules should update their `HasRegisterInterfaces` implementation to accept a `cosmossdk.io/core/registry.LegacyRegistry` interface.

```diff
-func (AppModule) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
+func (AppModule) RegisterInterfaces(registry registry.LegacyRegistry) {
```

##### Dependency Injection

Expand Down
25 changes: 5 additions & 20 deletions codec/types/interface_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/cosmos/gogoproto/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/runtime/protoiface"

"cosmossdk.io/core/registry"
"cosmossdk.io/x/tx/signing"
)

Expand All @@ -33,24 +35,7 @@ type AnyUnpacker interface {
type InterfaceRegistry interface {
AnyUnpacker
jsonpb.AnyResolver

// RegisterInterface associates protoName as the public name for the
// interface passed in as iface. This is to be used primarily to create
// a public facing registry of interface implementations for clients.
// protoName should be a well-chosen public facing name that remains stable.
// RegisterInterface takes an optional list of impls to be registered
// as implementations of iface.
//
// Ex:
// registry.RegisterInterface("cosmos.base.v1beta1.Msg", (*sdk.Msg)(nil))
RegisterInterface(protoName string, iface interface{}, impls ...proto.Message)

// RegisterImplementations registers impls as concrete implementations of
// the interface iface.
//
// Ex:
// registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSend{}, &MsgMultiSend{})
RegisterImplementations(iface interface{}, impls ...proto.Message)
registry.LegacyRegistry

// ListAllInterfaces list the type URLs of all registered interfaces.
ListAllInterfaces() []string
Expand Down Expand Up @@ -158,7 +143,7 @@ func NewInterfaceRegistryWithOptions(options InterfaceRegistryOptions) (Interfac
}, nil
}

func (registry *interfaceRegistry) RegisterInterface(protoName string, iface interface{}, impls ...proto.Message) {
func (registry *interfaceRegistry) RegisterInterface(protoName string, iface interface{}, impls ...protoiface.MessageV1) {
typ := reflect.TypeOf(iface)
if typ.Elem().Kind() != reflect.Interface {
panic(fmt.Errorf("%T is not an interface type", iface))
Expand Down Expand Up @@ -188,7 +173,7 @@ func (registry *interfaceRegistry) EnsureRegistered(impl interface{}) error {
//
// This function PANICs if different concrete types are registered under the
// same typeURL.
func (registry *interfaceRegistry) RegisterImplementations(iface interface{}, impls ...proto.Message) {
func (registry *interfaceRegistry) RegisterImplementations(iface interface{}, impls ...protoiface.MessageV1) {
for _, impl := range impls {
typeURL := MsgTypeURL(impl)
registry.registerImpl(iface, typeURL, impl)
Expand Down
5 changes: 4 additions & 1 deletion core/appmodule/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/protobuf/runtime/protoiface"

appmodule "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/appmodule/v2"
)

// AppModule is a tag interface for app module implementations to use as a basis
Expand Down Expand Up @@ -70,6 +70,9 @@ type HasBeginBlocker = appmodule.HasBeginBlocker
// custom logic after transaction processing in a block.
type HasEndBlocker = appmodule.HasEndBlocker

// HasRegisterInterfaces is the interface for modules to register their msg types.
type HasRegisterInterfaces = appmodule.HasRegisterInterfaces

// MsgHandlerRouter is implemented by the runtime provider.
type MsgHandlerRouter interface {
// RegisterHandler is called by modules to register msg handler functions.
Expand Down
6 changes: 6 additions & 0 deletions core/appmodule/v2/appmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package appmodule
import (
"context"

"cosmossdk.io/core/registry"
"cosmossdk.io/core/transaction"
)

Expand Down Expand Up @@ -90,3 +91,8 @@ type ValidatorUpdate struct {
PubKeyType string
Power int64 // updated power of the validtor
}

// HasRegisterInterfaces is the interface for modules to register their msg types.
type HasRegisterInterfaces interface {
RegisterInterfaces(registry.LegacyRegistry)
}
25 changes: 25 additions & 0 deletions core/registry/legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package registry

import (
"google.golang.org/protobuf/runtime/protoiface"
)

type LegacyRegistry interface {
// RegisterInterface associates protoName as the public name for the
// interface passed in as iface. This is to be used primarily to create
// a public facing registry of interface implementations for clients.
// protoName should be a well-chosen public facing name that remains stable.
// RegisterInterface takes an optional list of impls to be registered
// as implementations of iface.
//
// Ex:
// registry.RegisterInterface("cosmos.base.v1beta1.Msg", (*sdk.Msg)(nil))
RegisterInterface(protoName string, iface interface{}, impls ...protoiface.MessageV1)

// RegisterImplementations registers impls as concrete implementations of
// the interface iface.
//
// Ex:
// registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSend{}, &MsgMultiSend{})
RegisterImplementations(iface interface{}, impls ...protoiface.MessageV1)
}
6 changes: 3 additions & 3 deletions testutil/mock/types_mock_appmodule.go

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

10 changes: 5 additions & 5 deletions testutil/mock/types_module_module.go

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

20 changes: 10 additions & 10 deletions types/module/core_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/genesis"
"cosmossdk.io/core/registry"
storetypes "cosmossdk.io/store/types"

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

var (
_ appmodule.AppModule = coreAppModuleAdaptor{}

_ HasName = coreAppModuleAdaptor{}
_ HasAminoCodec = coreAppModuleAdaptor{}
_ HasGRPCGateway = coreAppModuleAdaptor{}
_ HasRegisterInterfaces = coreAppModuleAdaptor{}
_ HasABCIGenesis = coreAppModuleAdaptor{}
_ HasServices = coreAppModuleAdaptor{}
_ HasName = coreAppModuleAdaptor{}
_ HasAminoCodec = coreAppModuleAdaptor{}
_ HasGRPCGateway = coreAppModuleAdaptor{}
_ appmodule.HasRegisterInterfaces = coreAppModuleAdaptor{}
_ HasABCIGenesis = coreAppModuleAdaptor{}
_ HasServices = coreAppModuleAdaptor{}
)

// CoreAppModuleAdaptor wraps the core API module as an AppModule that this version of the SDK can use.
Expand Down Expand Up @@ -171,11 +171,11 @@ func (c coreAppModuleAdaptor) RegisterGRPCGatewayRoutes(ctx client.Context, mux
}

// RegisterInterfaces implements HasRegisterInterfaces
func (c coreAppModuleAdaptor) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
func (c coreAppModuleAdaptor) RegisterInterfaces(reg registry.LegacyRegistry) {
if mod, ok := c.module.(interface {
RegisterInterfaces(registry codectypes.InterfaceRegistry)
RegisterInterfaces(registry.LegacyRegistry)
}); ok {
mod.RegisterInterfaces(registry)
mod.RegisterInterfaces(reg)
}
}

Expand Down
10 changes: 4 additions & 6 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ import (

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/genesis"
"cosmossdk.io/core/registry"
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down Expand Up @@ -83,9 +83,7 @@ type HasAminoCodec interface {
}

// HasRegisterInterfaces is the interface for modules to register their msg types.
type HasRegisterInterfaces interface {
RegisterInterfaces(types.InterfaceRegistry)
}
type HasRegisterInterfaces appmodule.HasRegisterInterfaces

// HasGRPCGateway is the interface for modules to register their gRPC gateway routes.
type HasGRPCGateway interface {
Expand Down Expand Up @@ -317,9 +315,9 @@ func (m *Manager) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
}

// RegisterInterfaces registers all module interface types
func (m *Manager) RegisterInterfaces(registry types.InterfaceRegistry) {
func (m *Manager) RegisterInterfaces(registry registry.LegacyRegistry) {
for _, b := range m.Modules {
if mod, ok := b.(HasRegisterInterfaces); ok {
if mod, ok := b.(appmodule.HasRegisterInterfaces); ok {
mod.RegisterInterfaces(registry)
}
}
Expand Down
5 changes: 3 additions & 2 deletions types/msgservice/msg_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"cosmossdk.io/core/registry"

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

// RegisterMsgServiceDesc registers all type_urls from Msg services described
// in `sd` into the registry.
func RegisterMsgServiceDesc(registry codectypes.InterfaceRegistry, sd *grpc.ServiceDesc) {
func RegisterMsgServiceDesc(registry registry.LegacyRegistry, sd *grpc.ServiceDesc) {
fdBytesUnzipped := unzip(proto.FileDescriptor(sd.Metadata.(string)))
if fdBytesUnzipped == nil {
panic(fmt.Errorf("error unzipping file description for MsgService %s", sd.ServiceName))
Expand Down
5 changes: 3 additions & 2 deletions x/accounts/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

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

"cosmossdk.io/collections"
"cosmossdk.io/core/address"
Expand Down Expand Up @@ -56,8 +57,8 @@ type SignerProvider interface {
}

type InterfaceRegistry interface {
RegisterInterface(name string, iface any, impls ...gogoproto.Message)
RegisterImplementations(iface any, impls ...gogoproto.Message)
RegisterInterface(name string, iface any, impls ...protoiface.MessageV1)
RegisterImplementations(iface any, impls ...protoiface.MessageV1)
}

func NewKeeper(
Expand Down
4 changes: 2 additions & 2 deletions x/accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"google.golang.org/grpc"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/registry"
"cosmossdk.io/x/accounts/cli"
v1 "cosmossdk.io/x/accounts/v1"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/msgservice"
Expand Down Expand Up @@ -52,7 +52,7 @@ func (m AppModule) IsAppModule() {}

func (AppModule) Name() string { return ModuleName }

func (m AppModule) RegisterInterfaces(registry types.InterfaceRegistry) {
func (m AppModule) RegisterInterfaces(registry registry.LegacyRegistry) {
msgservice.RegisterMsgServiceDesc(registry, v1.MsgServiceDesc())
}

Expand Down
4 changes: 2 additions & 2 deletions x/accounts/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ var _ InterfaceRegistry = (*interfaceRegistry)(nil)

type interfaceRegistry struct{}

func (i interfaceRegistry) RegisterInterface(string, any, ...gogoproto.Message) {}
func (i interfaceRegistry) RegisterInterface(string, any, ...protoiface.MessageV1) {}

func (i interfaceRegistry) RegisterImplementations(any, ...gogoproto.Message) {}
func (i interfaceRegistry) RegisterImplementations(any, ...protoiface.MessageV1) {}

func newKeeper(t *testing.T, accounts ...implementation.AccountCreatorFunc) (Keeper, context.Context) {
t.Helper()
Expand Down
4 changes: 2 additions & 2 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"google.golang.org/grpc"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/registry"
"cosmossdk.io/x/auth/keeper"
"cosmossdk.io/x/auth/simulation"
"cosmossdk.io/x/auth/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
)
Expand Down Expand Up @@ -71,7 +71,7 @@ func (AppModule) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *gwrunt
}

// RegisterInterfaces registers interfaces and implementations of the auth module.
func (AppModule) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
func (AppModule) RegisterInterfaces(registry registry.LegacyRegistry) {
types.RegisterInterfaces(registry)
}

Expand Down
Loading

0 comments on commit 4edf6b2

Please sign in to comment.