feat: allow custom authority and inflation function when using app-wiring (#12660)

This commit is contained in:
Julien Robert 2022-07-21 12:28:21 +02:00 committed by GitHub
parent 345e81aa32
commit b72d12e5e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 136 additions and 90 deletions

View File

@ -193,8 +193,22 @@ func NewSimApp(
app = &SimApp{}
appBuilder *runtime.AppBuilder
// merge the app.yaml and the appOpts in one config
appConfig = depinject.Configs(AppConfig, depinject.Supply(appOpts))
// merge the AppConfig and other configuration in one config
appConfig = depinject.Configs(
AppConfig,
depinject.Supply(
// supply the application options
appOpts,
// for providing a custom inflaction function for x/mint
// add here your custom function that implements the minttypes.InflationCalculationFn interface.
// for providing a custom authority to a module simply add it below. By default the governance module is the default authority.
// map[string]sdk.AccAddress{
// minttypes.ModuleName: authtypes.NewModuleAddress(authtypes.ModuleName),
// },
),
)
)
if err := depinject.Inject(appConfig,

View File

@ -228,12 +228,13 @@ func provideModuleBasic() runtime.AppModuleBasicWrapper {
type bankInputs struct {
depinject.In
Config *modulev1.Module
Cdc codec.Codec
Key *store.KVStoreKey
ModuleKey depinject.OwnModuleKey
Config *modulev1.Module
Cdc codec.Codec
Key *store.KVStoreKey
AccountKeeper types.AccountKeeper
Authority types.BankAuthority `optional:"true"`
Authority map[string]sdk.AccAddress `optional:"true"`
// LegacySubspace is used solely for migration of x/params managed parameters
LegacySubspace exported.Subspace
@ -263,10 +264,10 @@ func provideModule(in bankInputs) bankOutputs {
}
}
authority := in.Authority
if authority == nil || len(authority) == 0 {
authority, ok := in.Authority[depinject.ModuleKey(in.ModuleKey).Name()]
if !ok {
// default to governance authority if not provided
authority = types.BankAuthority(authtypes.NewModuleAddress(govtypes.ModuleName))
authority = authtypes.NewModuleAddress(govtypes.ModuleName)
}
bankKeeper := keeper.NewBaseKeeper(

View File

@ -1,11 +0,0 @@
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
type BankAuthority sdk.AccAddress
func (a BankAuthority) String() string {
return sdk.AccAddress(a).String()
}

View File

@ -197,10 +197,12 @@ func init() {
type crisisInputs struct {
depinject.In
Config *modulev1.Module
Key *store.KVStoreKey
Cdc codec.Codec
AppOpts servertypes.AppOptions `optional:"true"`
ModuleKey depinject.OwnModuleKey
Config *modulev1.Module
Key *store.KVStoreKey
Cdc codec.Codec
AppOpts servertypes.AppOptions `optional:"true"`
Authority map[string]sdk.AccAddress `optional:"true"`
BankKeeper types.SupplyKeeper
@ -227,13 +229,19 @@ func provideModule(in crisisInputs) crisisOutputs {
feeCollectorName = authtypes.FeeCollectorName
}
authority, ok := in.Authority[depinject.ModuleKey(in.ModuleKey).Name()]
if !ok {
// default to governance authority if not provided
authority = authtypes.NewModuleAddress(govtypes.ModuleName)
}
k := keeper.NewKeeper(
in.Cdc,
in.Key,
invalidCheckPeriod,
in.BankKeeper,
feeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authority.String(),
)
skipGenesisInvariants := cast.ToBool(in.AppOpts.Get(FlagSkipGenesisInvariants))

View File

@ -237,9 +237,11 @@ func provideModuleBasic() runtime.AppModuleBasicWrapper {
type distrInputs struct {
depinject.In
Config *modulev1.Module
Key *store.KVStoreKey
Cdc codec.Codec
ModuleKey depinject.OwnModuleKey
Config *modulev1.Module
Key *store.KVStoreKey
Cdc codec.Codec
Authority map[string]sdk.AccAddress `optional:"true"`
AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
@ -264,6 +266,12 @@ func provideModule(in distrInputs) distrOutputs {
feeCollectorName = authtypes.FeeCollectorName
}
authority, ok := in.Authority[depinject.ModuleKey(in.ModuleKey).Name()]
if !ok {
// default to governance authority if not provided
authority = authtypes.NewModuleAddress(govtypes.ModuleName)
}
k := keeper.NewKeeper(
in.Cdc,
in.Key,
@ -271,7 +279,7 @@ func provideModule(in distrInputs) distrOutputs {
in.BankKeeper,
in.StakingKeeper,
feeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authority.String(),
)
m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.LegacySubspace)

View File

@ -236,9 +236,12 @@ func provideModuleBasic() runtime.AppModuleBasicWrapper {
type mintInputs struct {
depinject.In
Config *modulev1.Module
Key *store.KVStoreKey
Cdc codec.Codec
ModuleKey depinject.OwnModuleKey
Config *modulev1.Module
Key *store.KVStoreKey
Cdc codec.Codec
Authority map[string]sdk.AccAddress `optional:"true"`
InflationCalculationFn types.InflationCalculationFn `optional:"true"`
// LegacySubspace is used solely for migration of x/params managed parameters
LegacySubspace exported.Subspace
@ -261,6 +264,12 @@ func provideModule(in mintInputs) mintOutputs {
feeCollectorName = authtypes.FeeCollectorName
}
authority, ok := in.Authority[depinject.ModuleKey(in.ModuleKey).Name()]
if !ok {
// default to governance authority if not provided
authority = authtypes.NewModuleAddress(govtypes.ModuleName)
}
k := keeper.NewKeeper(
in.Cdc,
in.Key,
@ -268,11 +277,11 @@ func provideModule(in mintInputs) mintOutputs {
in.AccountKeeper,
in.BankKeeper,
feeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authority.String(),
)
// TODO: allow to set inflation calculation function
m := NewAppModule(in.Cdc, k, in.AccountKeeper, nil, in.LegacySubspace)
// when no inflation calculation function is provided it will use the default types.DefaultInflationCalculationFn
m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.InflationCalculationFn, in.LegacySubspace)
return mintOutputs{MintKeeper: k, Module: runtime.WrapAppModule(m)}
}

View File

@ -184,56 +184,6 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
BeginBlocker(ctx, req, am.keeper)
}
// _____________________________________________________________________________________
func init() {
appmodule.Register(
&modulev1.Module{},
appmodule.Provide(
provideModuleBasic,
provideModule,
),
)
}
func provideModuleBasic() runtime.AppModuleBasicWrapper {
return runtime.WrapAppModuleBasic(AppModuleBasic{})
}
type slashingInputs struct {
depinject.In
Key *store.KVStoreKey
Cdc codec.Codec
LegacyAmino *codec.LegacyAmino
AccountKeeper types.AccountKeeper `key:"cosmos.auth.v1.AccountKeeper"`
BankKeeper types.BankKeeper `key:"cosmos.bank.v1.Keeper"`
StakingKeeper types.StakingKeeper `key:"cosmos.staking.v1.Keeper"`
// LegacySubspace is used solely for migration of x/params managed parameters
LegacySubspace exported.Subspace
}
type slashingOutputs struct {
depinject.Out
Keeper keeper.Keeper
Module runtime.AppModuleWrapper
Hooks staking.StakingHooksWrapper
}
func provideModule(in slashingInputs) slashingOutputs {
k := keeper.NewKeeper(in.Cdc, in.LegacyAmino, in.Key, in.StakingKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String())
m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.LegacySubspace)
return slashingOutputs{
Keeper: k,
Module: runtime.WrapAppModule(m),
Hooks: staking.StakingHooksWrapper{StakingHooks: k.Hooks()},
}
}
// _____________________________________________________________________________________
// AppModuleSimulation functions
// GenerateGenesisState creates a randomized GenState of the slashing module.
@ -263,3 +213,62 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
am.accountKeeper, am.bankKeeper, am.keeper, am.stakingKeeper,
)
}
// ============================================================================
// New App Wiring Setup
// ============================================================================
func init() {
appmodule.Register(
&modulev1.Module{},
appmodule.Provide(
provideModuleBasic,
provideModule,
),
)
}
func provideModuleBasic() runtime.AppModuleBasicWrapper {
return runtime.WrapAppModuleBasic(AppModuleBasic{})
}
type slashingInputs struct {
depinject.In
ModuleKey depinject.OwnModuleKey
Key *store.KVStoreKey
Cdc codec.Codec
LegacyAmino *codec.LegacyAmino
Authority map[string]sdk.AccAddress `optional:"true"`
AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
StakingKeeper types.StakingKeeper
// LegacySubspace is used solely for migration of x/params managed parameters
LegacySubspace exported.Subspace
}
type slashingOutputs struct {
depinject.Out
Keeper keeper.Keeper
Module runtime.AppModuleWrapper
Hooks staking.StakingHooksWrapper
}
func provideModule(in slashingInputs) slashingOutputs {
authority, ok := in.Authority[depinject.ModuleKey(in.ModuleKey).Name()]
if !ok {
// default to governance authority if not provided
authority = authtypes.NewModuleAddress(govtypes.ModuleName)
}
k := keeper.NewKeeper(in.Cdc, in.LegacyAmino, in.Key, in.StakingKeeper, authority.String())
m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.LegacySubspace)
return slashingOutputs{
Keeper: k,
Module: runtime.WrapAppModule(m),
Hooks: staking.StakingHooksWrapper{StakingHooks: k.Hooks()},
}
}

View File

@ -168,11 +168,13 @@ func provideModuleBasic() runtime.AppModuleBasicWrapper {
type upgradeInputs struct {
depinject.In
Config *modulev1.Module
Key *store.KVStoreKey
Cdc codec.Codec
ModuleKey depinject.OwnModuleKey
Config *modulev1.Module
Key *store.KVStoreKey
Cdc codec.Codec
AppOpts servertypes.AppOptions `optional:"true"`
AppOpts servertypes.AppOptions `optional:"true"`
Authority map[string]sdk.AccAddress `optional:"true"`
}
type upgradeOutputs struct {
@ -197,8 +199,14 @@ func provideModule(in upgradeInputs) upgradeOutputs {
homePath = cast.ToString(in.AppOpts.Get(flags.FlagHome))
}
authority, ok := in.Authority[depinject.ModuleKey(in.ModuleKey).Name()]
if !ok {
// default to governance authority if not provided
authority = authtypes.NewModuleAddress(govtypes.ModuleName)
}
// set the governance module account as the authority for conducting upgrades
k := keeper.NewKeeper(skipUpgradeHeights, in.Key, in.Cdc, homePath, nil, authtypes.NewModuleAddress(govtypes.ModuleName).String())
k := keeper.NewKeeper(skipUpgradeHeights, in.Key, in.Cdc, homePath, nil, authority.String())
m := NewAppModule(k)
gh := govv1beta1.HandlerRoute{RouteKey: types.RouterKey, Handler: NewSoftwareUpgradeProposalHandler(k)}