Skip to content

Commit

Permalink
refactor(runtime/v2): remove dependency on sdk (cosmos#20389)
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski authored May 16, 2024
1 parent 53925ef commit e034766
Show file tree
Hide file tree
Showing 19 changed files with 258 additions and 637 deletions.
45 changes: 45 additions & 0 deletions codec/depinject.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package codec

import (
"fmt"

"github.com/cosmos/gogoproto/proto"

authmodulev1 "cosmossdk.io/api/cosmos/auth/module/v1"
stakingmodulev1 "cosmossdk.io/api/cosmos/staking/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/legacy"
"cosmossdk.io/depinject"
"cosmossdk.io/x/tx/signing"

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

Expand Down Expand Up @@ -45,3 +51,42 @@ func ProvideLegacyAmino() legacy.Amino {
func ProvideProtoCodec(interfaceRegistry types.InterfaceRegistry) *ProtoCodec {
return NewProtoCodec(interfaceRegistry)
}

type AddressCodecInputs struct {
depinject.In

AuthConfig *authmodulev1.Module `optional:"true"`
StakingConfig *stakingmodulev1.Module `optional:"true"`

AddressCodecFactory func() address.Codec `optional:"true"`
ValidatorAddressCodecFactory func() address.ValidatorAddressCodec `optional:"true"`
ConsensusAddressCodecFactory func() address.ConsensusAddressCodec `optional:"true"`
}

// ProvideAddressCodec provides an address.Codec to the container for any
// modules that want to do address string <> bytes conversion.
func ProvideAddressCodec(in AddressCodecInputs) (address.Codec, address.ValidatorAddressCodec, address.ConsensusAddressCodec) {
if in.AddressCodecFactory != nil && in.ValidatorAddressCodecFactory != nil && in.ConsensusAddressCodecFactory != nil {
return in.AddressCodecFactory(), in.ValidatorAddressCodecFactory(), in.ConsensusAddressCodecFactory()
}

if in.AuthConfig == nil || in.AuthConfig.Bech32Prefix == "" {
panic("auth config bech32 prefix cannot be empty if no custom address codec is provided")
}

if in.StakingConfig == nil {
in.StakingConfig = &stakingmodulev1.Module{}
}

if in.StakingConfig.Bech32PrefixValidator == "" {
in.StakingConfig.Bech32PrefixValidator = fmt.Sprintf("%svaloper", in.AuthConfig.Bech32Prefix)
}

if in.StakingConfig.Bech32PrefixConsensus == "" {
in.StakingConfig.Bech32PrefixConsensus = fmt.Sprintf("%svalcons", in.AuthConfig.Bech32Prefix)
}

return addresscodec.NewBech32Codec(in.AuthConfig.Bech32Prefix),
addresscodec.NewBech32Codec(in.StakingConfig.Bech32PrefixValidator),
addresscodec.NewBech32Codec(in.StakingConfig.Bech32PrefixConsensus)
}
9 changes: 9 additions & 0 deletions core/appmodule/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package appmodule

import (
"context"
"encoding/json"
"io"

"cosmossdk.io/core/appmodule/v2"
Expand Down Expand Up @@ -29,6 +30,14 @@ type HasGenesisAuto interface {
ExportGenesis(context.Context, GenesisTarget) error
}

// HasGenesisBasics is the legacy interface for stateless genesis methods.
type HasGenesisBasics interface {
HasName

DefaultGenesis() json.RawMessage
ValidateGenesis(json.RawMessage) error
}

// GenesisSource is a source for genesis data in JSON format. It may abstract over a
// single JSON object or separate files for each field in a JSON object that can
// be streamed over. Modules should open a separate io.ReadCloser for each field that
Expand Down
12 changes: 12 additions & 0 deletions core/appmodule/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"google.golang.org/grpc"

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

// AppModule is a tag interface for app module implementations to use as a basis
Expand Down Expand Up @@ -64,3 +65,14 @@ type HasPrecommit interface {
appmodule.AppModule
Precommit(context.Context) error
}

// HasName is an extension interface that must return the appmodule.AppModule's Name.
type HasName interface {
Name() string
}

// HasAminoCodec is an extension interface that module must implement to support JSON encoding and decoding of its types
// through amino. This is used in genesis & the CLI client.
type HasAminoCodec interface {
RegisterLegacyAminoCodec(legacy.Amino)
}
6 changes: 6 additions & 0 deletions core/appmodule/v2/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ type HasGenesis interface {
InitGenesis(ctx context.Context, data json.RawMessage) error
ExportGenesis(ctx context.Context) (json.RawMessage, error)
}

type HasABCIGenesis interface {
DefaultGenesis() json.RawMessage
InitGenesis(ctx context.Context, data json.RawMessage) ([]ValidatorUpdate, error)
}

type GenesisDecoder interface {
DecodeGenesisJSON(data json.RawMessage) ([]json.RawMessage, error)
}
5 changes: 3 additions & 2 deletions crypto/codec/proto.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package codec

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

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand All @@ -10,7 +11,7 @@ import (
)

// RegisterInterfaces registers the sdk.Tx interface.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
func RegisterInterfaces(registry registry.InterfaceRegistrar) {
var pk *cryptotypes.PubKey
registry.RegisterInterface("cosmos.crypto.PubKey", pk)
registry.RegisterImplementations(pk, &ed25519.PubKey{})
Expand Down
5 changes: 3 additions & 2 deletions crypto/keys/secp256r1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"crypto/elliptic"
"fmt"

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

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)

Expand All @@ -30,6 +31,6 @@ func init() {
}

// RegisterInterfaces adds secp256r1 PubKey to pubkey registry
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
func RegisterInterfaces(registry registry.InterfaceRegistrar) {
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &PubKey{})
}
45 changes: 1 addition & 44 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import (

runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
authmodulev1 "cosmossdk.io/api/cosmos/auth/module/v1"
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
stakingmodulev1 "cosmossdk.io/api/cosmos/staking/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/app"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/comet"
Expand All @@ -28,7 +25,6 @@ import (

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/types/module"
Expand Down Expand Up @@ -98,6 +94,7 @@ func init() {
codec.ProvideInterfaceRegistry,
codec.ProvideLegacyAmino,
codec.ProvideProtoCodec,
codec.ProvideAddressCodec,
ProvideKVStoreKey,
ProvideTransientStoreKey,
ProvideMemoryStoreKey,
Expand All @@ -106,7 +103,6 @@ func init() {
ProvideTransientStoreService,
ProvideModuleManager,
ProvideAppVersionModifier,
ProvideAddressCodec,
ProvideCometService,
),
appconfig.Invoke(SetupAppBuilder),
Expand Down Expand Up @@ -265,42 +261,3 @@ func ProvideAppVersionModifier(app *AppBuilder) app.VersionModifier {
func ProvideCometService() comet.Service {
return NewContextAwareCometInfoService()
}

type AddressCodecInputs struct {
depinject.In

AuthConfig *authmodulev1.Module `optional:"true"`
StakingConfig *stakingmodulev1.Module `optional:"true"`

AddressCodecFactory func() address.Codec `optional:"true"`
ValidatorAddressCodecFactory func() address.ValidatorAddressCodec `optional:"true"`
ConsensusAddressCodecFactory func() address.ConsensusAddressCodec `optional:"true"`
}

// ProvideAddressCodec provides an address.Codec to the container for any
// modules that want to do address string <> bytes conversion.
func ProvideAddressCodec(in AddressCodecInputs) (address.Codec, address.ValidatorAddressCodec, address.ConsensusAddressCodec) {
if in.AddressCodecFactory != nil && in.ValidatorAddressCodecFactory != nil && in.ConsensusAddressCodecFactory != nil {
return in.AddressCodecFactory(), in.ValidatorAddressCodecFactory(), in.ConsensusAddressCodecFactory()
}

if in.AuthConfig == nil || in.AuthConfig.Bech32Prefix == "" {
panic("auth config bech32 prefix cannot be empty if no custom address codec is provided")
}

if in.StakingConfig == nil {
in.StakingConfig = &stakingmodulev1.Module{}
}

if in.StakingConfig.Bech32PrefixValidator == "" {
in.StakingConfig.Bech32PrefixValidator = fmt.Sprintf("%svaloper", in.AuthConfig.Bech32Prefix)
}

if in.StakingConfig.Bech32PrefixConsensus == "" {
in.StakingConfig.Bech32PrefixConsensus = fmt.Sprintf("%svalcons", in.AuthConfig.Bech32Prefix)
}

return addresscodec.NewBech32Codec(in.AuthConfig.Bech32Prefix),
addresscodec.NewBech32Codec(in.StakingConfig.Bech32PrefixValidator),
addresscodec.NewBech32Codec(in.StakingConfig.Bech32PrefixConsensus)
}
14 changes: 6 additions & 8 deletions runtime/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import (
runtimev2 "cosmossdk.io/api/cosmos/app/runtime/v2"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
coreappmanager "cosmossdk.io/core/app"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/stf"

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

var _ AppI[transaction.Tx] = (*App)(nil)
Expand Down Expand Up @@ -62,11 +61,10 @@ type App struct {
appConfig *appv1alpha1.Config

// modules configuration
storeKeys []string
interfaceRegistry codectypes.InterfaceRegistry
cdc codec.Codec
amino *codec.LegacyAmino
moduleManager *MM
storeKeys []string
interfaceRegistrar registry.InterfaceRegistrar
amino legacy.Amino
moduleManager *MM
}

// Logger returns the app logger.
Expand Down
17 changes: 11 additions & 6 deletions runtime/v2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"fmt"
"io"

"cosmossdk.io/core/appmodule"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/stf"
"cosmossdk.io/server/v2/stf/branch"
rootstore "cosmossdk.io/store/v2/root"

sdkmodule "github.com/cosmos/cosmos-sdk/types/module"
)

// AppBuilder is a type that is injected into a container by the runtime/v2 module
Expand All @@ -39,18 +38,18 @@ func (a *AppBuilder) DefaultGenesis() map[string]json.RawMessage {
// This is the primary hook for integrating with modules which are not registered using the app config.
func (a *AppBuilder) RegisterModules(modules ...appmodulev2.AppModule) error {
for _, appModule := range modules {
if mod, ok := appModule.(sdkmodule.HasName); ok {
if mod, ok := appModule.(appmodule.HasName); ok {
name := mod.Name()
if _, ok := a.app.moduleManager.modules[name]; ok {
return fmt.Errorf("module named %q already exists", name)
}
a.app.moduleManager.modules[name] = appModule

if mod, ok := appModule.(appmodulev2.HasRegisterInterfaces); ok {
mod.RegisterInterfaces(a.app.interfaceRegistry)
mod.RegisterInterfaces(a.app.interfaceRegistrar)
}

if mod, ok := appModule.(sdkmodule.HasAminoCodec); ok {
if mod, ok := appModule.(appmodule.HasAminoCodec); ok {
mod.RegisterLegacyAminoCodec(a.app.amino)
}
}
Expand Down Expand Up @@ -179,7 +178,13 @@ func AppBuilderWithTxValidator(txValidators func(ctx context.Context, tx transac

// AppBuilderWithPostTxExec sets logic that will be executed after each transaction.
// When not provided, a no-op function will be used.
func AppBuilderWithPostTxExec(postTxExec func(ctx context.Context, tx transaction.Tx, success bool) error) AppBuilderOption {
func AppBuilderWithPostTxExec(
postTxExec func(
ctx context.Context,
tx transaction.Tx,
success bool,
) error,
) AppBuilderOption {
return func(a *AppBuilder) {
a.postTxExec = postTxExec
}
Expand Down
Loading

0 comments on commit e034766

Please sign in to comment.