Skip to content

Commit

Permalink
refactor!: require exported app wiring providers (cosmos#13292)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc authored Oct 10, 2022
1 parent c60c556 commit f3a558c
Show file tree
Hide file tree
Showing 21 changed files with 190 additions and 177 deletions.
1 change: 1 addition & 0 deletions baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
dbm "github.com/tendermint/tm-db"

"cosmossdk.io/depinject"

baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand Down
59 changes: 28 additions & 31 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -23,29 +24,24 @@ type BaseAppOption func(*baseapp.BaseApp)
// IsManyPerContainerType indicates that this is a depinject.ManyPerContainerType.
func (b BaseAppOption) IsManyPerContainerType() {}

// appWrapper is used to pass around an instance of *App internally between
// runtime dependency inject providers that is partially constructed (no
// baseapp yet).
type appWrapper *App

func init() {
appmodule.Register(&runtimev1alpha1.Module{},
appmodule.Provide(
provideCodecs,
provideAppBuilder,
provideKVStoreKey,
provideTransientStoreKey,
provideMemoryStoreKey,
provideDeliverTx,
ProvideCodecs,
ProvideKVStoreKey,
ProvideTransientStoreKey,
ProvideMemoryStoreKey,
ProvideDeliverTx,
),
appmodule.Invoke(SetupAppBuilder),
)
}

func provideCodecs(moduleBasics map[string]AppModuleBasicWrapper) (
func ProvideCodecs(moduleBasics map[string]AppModuleBasicWrapper) (
codectypes.InterfaceRegistry,
codec.Codec,
*codec.LegacyAmino,
appWrapper,
*AppBuilder,
codec.ProtoCodecMarshaler,
*baseapp.MsgServiceRouter,
) {
Expand All @@ -64,13 +60,15 @@ func provideCodecs(moduleBasics map[string]AppModuleBasicWrapper) (

cdc := codec.NewProtoCodec(interfaceRegistry)
msgServiceRouter := baseapp.NewMsgServiceRouter()
app := &App{
storeKeys: nil,
interfaceRegistry: interfaceRegistry,
cdc: cdc,
amino: amino,
basicManager: basicManager,
msgServiceRouter: msgServiceRouter,
app := &AppBuilder{
&App{
storeKeys: nil,
interfaceRegistry: interfaceRegistry,
cdc: cdc,
amino: amino,
basicManager: basicManager,
msgServiceRouter: msgServiceRouter,
},
}

return interfaceRegistry, cdc, amino, app, cdc, msgServiceRouter
Expand All @@ -80,25 +78,24 @@ type appInputs struct {
depinject.In

Config *runtimev1alpha1.Module
App appWrapper
AppBuilder *AppBuilder
Modules map[string]AppModuleWrapper
BaseAppOptions []BaseAppOption
}

func provideAppBuilder(inputs appInputs) *AppBuilder {
func SetupAppBuilder(inputs appInputs) {
mm := &module.Manager{Modules: map[string]module.AppModule{}}
for name, wrapper := range inputs.Modules {
mm.Modules[name] = wrapper.AppModule
}
app := inputs.App
app := inputs.AppBuilder.app
app.baseAppOptions = inputs.BaseAppOptions
app.config = inputs.Config
app.ModuleManager = mm
return &AppBuilder{app: app}
}

func registerStoreKey(wrapper appWrapper, key storetypes.StoreKey) {
wrapper.storeKeys = append(wrapper.storeKeys, key)
func registerStoreKey(wrapper *AppBuilder, key storetypes.StoreKey) {
wrapper.app.storeKeys = append(wrapper.app.storeKeys, key)
}

func storeKeyOverride(config *runtimev1alpha1.Module, moduleName string) *runtimev1alpha1.StoreKeyConfig {
Expand All @@ -110,7 +107,7 @@ func storeKeyOverride(config *runtimev1alpha1.Module, moduleName string) *runtim
return nil
}

func provideKVStoreKey(config *runtimev1alpha1.Module, key depinject.ModuleKey, app appWrapper) *storetypes.KVStoreKey {
func ProvideKVStoreKey(config *runtimev1alpha1.Module, key depinject.ModuleKey, app *AppBuilder) *storetypes.KVStoreKey {
override := storeKeyOverride(config, key.Name())

var storeKeyName string
Expand All @@ -125,20 +122,20 @@ func provideKVStoreKey(config *runtimev1alpha1.Module, key depinject.ModuleKey,
return storeKey
}

func provideTransientStoreKey(key depinject.ModuleKey, app appWrapper) *storetypes.TransientStoreKey {
func ProvideTransientStoreKey(key depinject.ModuleKey, app *AppBuilder) *storetypes.TransientStoreKey {
storeKey := storetypes.NewTransientStoreKey(fmt.Sprintf("transient:%s", key.Name()))
registerStoreKey(app, storeKey)
return storeKey
}

func provideMemoryStoreKey(key depinject.ModuleKey, app appWrapper) *storetypes.MemoryStoreKey {
func ProvideMemoryStoreKey(key depinject.ModuleKey, app *AppBuilder) *storetypes.MemoryStoreKey {
storeKey := storetypes.NewMemoryStoreKey(fmt.Sprintf("memory:%s", key.Name()))
registerStoreKey(app, storeKey)
return storeKey
}

func provideDeliverTx(app appWrapper) func(abci.RequestDeliverTx) abci.ResponseDeliverTx {
func ProvideDeliverTx(appBuilder *AppBuilder) func(abci.RequestDeliverTx) abci.ResponseDeliverTx {
return func(tx abci.RequestDeliverTx) abci.ResponseDeliverTx {
return app.BaseApp.DeliverTx(tx)
return appBuilder.app.BaseApp.DeliverTx(tx)
}
}
12 changes: 6 additions & 6 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ func (AppModule) WeightedOperations(_ module.SimulationState) []simtypes.Weighte

func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(provideModuleBasic, provideModule),
appmodule.Provide(ProvideModuleBasic, ProvideModule),
)
}

func provideModuleBasic() runtime.AppModuleBasicWrapper {
func ProvideModuleBasic() runtime.AppModuleBasicWrapper {
return runtime.WrapAppModuleBasic(AppModuleBasic{})
}

type authInputs struct {
type AuthInputs struct {
depinject.In

Config *modulev1.Module
Expand All @@ -203,14 +203,14 @@ type authInputs struct {
LegacySubspace exported.Subspace `optional:"true"`
}

type authOutputs struct {
type AuthOutputs struct {
depinject.Out

AccountKeeper keeper.AccountKeeper
Module runtime.AppModuleWrapper
}

func provideModule(in authInputs) authOutputs {
func ProvideModule(in AuthInputs) AuthOutputs {
maccPerms := map[string][]string{}
for _, permission := range in.Config.ModuleAccountPermissions {
maccPerms[permission.Account] = permission.Permissions
Expand All @@ -219,5 +219,5 @@ func provideModule(in authInputs) authOutputs {
k := keeper.NewAccountKeeper(in.Cdc, in.Key, types.ProtoBaseAccount, maccPerms, in.Config.Bech32Prefix, types.NewModuleAddress(govtypes.ModuleName).String())
m := NewAppModule(in.Cdc, k, simulation.RandomGenesisAccounts, in.LegacySubspace)

return authOutputs{AccountKeeper: k, Module: runtime.WrapAppModule(m)}
return AuthOutputs{AccountKeeper: k, Module: runtime.WrapAppModule(m)}
}
12 changes: 6 additions & 6 deletions x/auth/tx/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (

func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(provideModule),
appmodule.Provide(ProvideModule),
)
}

type txInputs struct {
type TxInputs struct {
depinject.In

Config *modulev1.Module
Expand All @@ -35,14 +35,14 @@ type txInputs struct {
FeeGrantKeeper feegrantkeeper.Keeper `optional:"true"`
}

type txOutputs struct {
type TxOutputs struct {
depinject.Out

TxConfig client.TxConfig
BaseAppOption runtime.BaseAppOption
}

func provideModule(in txInputs) txOutputs {
func ProvideModule(in TxInputs) TxOutputs {
txConfig := tx.NewTxConfig(in.ProtoCodecMarshaler, tx.DefaultSignModes)

baseAppOption := func(app *baseapp.BaseApp) {
Expand Down Expand Up @@ -83,10 +83,10 @@ func provideModule(in txInputs) txOutputs {
app.SetTxDecoder(txConfig.TxDecoder())
}

return txOutputs{TxConfig: txConfig, BaseAppOption: baseAppOption}
return TxOutputs{TxConfig: txConfig, BaseAppOption: baseAppOption}
}

func newAnteHandler(txConfig client.TxConfig, in txInputs) (sdk.AnteHandler, error) {
func newAnteHandler(txConfig client.TxConfig, in TxInputs) (sdk.AnteHandler, error) {
if in.BankKeeper == nil {
return nil, fmt.Errorf("both AccountKeeper and BankKeeper are required")
}
Expand Down
12 changes: 6 additions & 6 deletions x/auth/vesting/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,29 @@ func (AppModule) ConsensusVersion() uint64 { return 1 }

func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(provideModuleBasic, provideModule),
appmodule.Provide(ProvideModuleBasic, ProvideModule),
)
}

func provideModuleBasic() runtime.AppModuleBasicWrapper {
func ProvideModuleBasic() runtime.AppModuleBasicWrapper {
return runtime.WrapAppModuleBasic(AppModuleBasic{})
}

type vestingInputs struct {
type VestingInputs struct {
depinject.In

AccountKeeper keeper.AccountKeeper
BankKeeper types.BankKeeper
}

type vestingOutputs struct {
type VestingOutputs struct {
depinject.Out

Module runtime.AppModuleWrapper
}

func provideModule(in vestingInputs) vestingOutputs {
func ProvideModule(in VestingInputs) VestingOutputs {
m := NewAppModule(in.AccountKeeper, in.BankKeeper)

return vestingOutputs{Module: runtime.WrapAppModule(m)}
return VestingOutputs{Module: runtime.WrapAppModule(m)}
}
14 changes: 7 additions & 7 deletions x/authz/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ func init() {
appmodule.Register(
&modulev1.Module{},
appmodule.Provide(
provideModuleBasic,
provideModule,
ProvideModuleBasic,
ProvideModule,
),
)
}

func provideModuleBasic() runtime.AppModuleBasicWrapper {
func ProvideModuleBasic() runtime.AppModuleBasicWrapper {
return runtime.WrapAppModuleBasic(AppModuleBasic{})
}

type authzInputs struct {
type AuthzInputs struct {
depinject.In

Key *store.KVStoreKey
Expand All @@ -180,17 +180,17 @@ type authzInputs struct {
MsgServiceRouter *baseapp.MsgServiceRouter
}

type authzOutputs struct {
type AuthzOutputs struct {
depinject.Out

AuthzKeeper keeper.Keeper
Module runtime.AppModuleWrapper
}

func provideModule(in authzInputs) authzOutputs {
func ProvideModule(in AuthzInputs) AuthzOutputs {
k := keeper.NewKeeper(in.Key, in.Cdc, in.MsgServiceRouter, in.AccountKeeper)
m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.Registry)
return authzOutputs{AuthzKeeper: k, Module: runtime.WrapAppModule(m)}
return AuthzOutputs{AuthzKeeper: k, Module: runtime.WrapAppModule(m)}
}

// ____________________________________________________________________________
Expand Down
21 changes: 11 additions & 10 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"fmt"
"time"

modulev1 "cosmossdk.io/api/cosmos/bank/module/v1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
"golang.org/x/exp/maps"

modulev1 "cosmossdk.io/api/cosmos/bank/module/v1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -196,15 +197,15 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(
provideModuleBasic,
provideModule))
ProvideModuleBasic,
ProvideModule))
}

func provideModuleBasic() runtime.AppModuleBasicWrapper {
func ProvideModuleBasic() runtime.AppModuleBasicWrapper {
return runtime.WrapAppModuleBasic(AppModuleBasic{})
}

type bankInputs struct {
type BankInputs struct {
depinject.In

ModuleKey depinject.OwnModuleKey
Expand All @@ -219,14 +220,14 @@ type bankInputs struct {
LegacySubspace exported.Subspace `optional:"true"`
}

type bankOutputs struct {
type BankOutputs struct {
depinject.Out

BankKeeper keeper.BaseKeeper
Module runtime.AppModuleWrapper
}

func provideModule(in bankInputs) bankOutputs {
func ProvideModule(in BankInputs) BankOutputs {
// Configure blocked module accounts.
//
// Default behavior for blockedAddresses is to regard any module mentioned in
Expand Down Expand Up @@ -258,5 +259,5 @@ func provideModule(in bankInputs) bankOutputs {
)
m := NewAppModule(in.Cdc, bankKeeper, in.AccountKeeper, in.LegacySubspace)

return bankOutputs{BankKeeper: bankKeeper, Module: runtime.WrapAppModule(m)}
return BankOutputs{BankKeeper: bankKeeper, Module: runtime.WrapAppModule(m)}
}
Loading

0 comments on commit f3a558c

Please sign in to comment.