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

refactor: remove dependency x/module -> sdk through InterfaceRegistry #19652

Merged
merged 7 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
30 changes: 8 additions & 22 deletions codec/types/interface_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"fmt"
"reflect"

"github.com/cosmos/gogoproto/jsonpb"
"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"

"github.com/cosmos/gogoproto/jsonpb"
"github.com/cosmos/gogoproto/proto"
)

var protoMessageType = reflect.TypeOf((*proto.Message)(nil)).Elem()
Expand All @@ -33,24 +36,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 +144,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 +174,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
15 changes: 5 additions & 10 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ 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"
)

// Deprecated: use the embed extension interfaces instead, when needed.
type AppModuleBasic interface {
HasName
HasRegisterInterfaces
appmodule.HasRegisterInterfaces
HasGRPCGateway
HasAminoCodec
}
Expand All @@ -58,7 +58,7 @@ type AppModule interface {
appmodule.AppModule

HasName
HasRegisterInterfaces
appmodule.HasRegisterInterfaces
}

// HasName allows the module to provide its own name for legacy purposes.
Expand All @@ -82,11 +82,6 @@ type HasAminoCodec interface {
RegisterLegacyAminoCodec(*codec.LegacyAmino)
}

// HasRegisterInterfaces is the interface for modules to register their msg types.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get a small changelog for that?

type HasRegisterInterfaces interface {
RegisterInterfaces(types.InterfaceRegistry)
}

// HasGRPCGateway is the interface for modules to register their gRPC gateway routes.
type HasGRPCGateway interface {
RegisterGRPCGatewayRoutes(client.Context, *runtime.ServeMux)
Expand Down Expand Up @@ -317,9 +312,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 @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"

"cosmossdk.io/core/registry"
"github.com/spf13/cobra"
"google.golang.org/grpc"

Expand All @@ -13,7 +14,6 @@ import (

"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
Loading
Loading