Skip to content

Commit

Permalink
refactor(runtime/v2): remove dependency on sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed May 14, 2024
1 parent 493d486 commit f353e9b
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 601 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)
}
1 change: 0 additions & 1 deletion core/appmodule/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package appmodule

import (
"context"

"google.golang.org/grpc"

"cosmossdk.io/core/appmodule/v2"
Expand Down
14 changes: 14 additions & 0 deletions core/appmodule/v2/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ type HasGenesis interface {
InitGenesis(ctx context.Context, data json.RawMessage) error
ExportGenesis(ctx context.Context) (json.RawMessage, error)
}

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

DefaultGenesis() json.RawMessage
ValidateGenesis(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)
}
13 changes: 13 additions & 0 deletions core/appmodule/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package appmodule

import (
"context"
"cosmossdk.io/core/legacy"

"cosmossdk.io/core/registry"
"cosmossdk.io/core/transaction"
Expand Down Expand Up @@ -47,6 +48,10 @@ type HasEndBlocker interface {
EndBlock(context.Context) error
}

type HasABCIEndBlock interface {
EndBlock(context.Context) ([]ValidatorUpdate, error)
}

// HasTxValidator is the extension interface that modules should implement to run
// custom logic for validating transactions.
// It was previously known as AnteHandler/Decorator.
Expand Down Expand Up @@ -104,3 +109,11 @@ type ValidatorUpdate struct {
type HasRegisterInterfaces interface {
RegisterInterfaces(registry.InterfaceRegistrar)
}

type HasName interface {
Name() string
}

type HasAminoCodec interface {
RegisterLegacyAminoCodec(legacy.Amino)
}
4 changes: 2 additions & 2 deletions crypto/codec/proto.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 +10,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
4 changes: 2 additions & 2 deletions crypto/keys/secp256r1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
package secp256r1

import (
"cosmossdk.io/core/registry"
"crypto/elliptic"
"fmt"

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

Expand All @@ -30,6 +30,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{})
}
14 changes: 6 additions & 8 deletions runtime/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package runtime

import (
"context"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"encoding/json"
"errors"

Expand All @@ -15,9 +17,6 @@ import (
"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 @@ -2,6 +2,7 @@ package runtime

import (
"context"
"cosmossdk.io/core/appmodule"
"encoding/json"
"fmt"
"io"
Expand All @@ -13,8 +14,6 @@ import (
"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

0 comments on commit f353e9b

Please sign in to comment.