From b72d12e5e89515834bfa5efe641ee04f1d8f82a5 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 21 Jul 2022 12:28:21 +0200 Subject: [PATCH] feat: allow custom authority and inflation function when using app-wiring (#12660) --- simapp/app.go | 18 +++++++- x/bank/module.go | 15 ++++--- x/bank/types/authority.go | 11 ----- x/crisis/module.go | 18 +++++--- x/distribution/module.go | 16 +++++-- x/mint/module.go | 21 ++++++--- x/slashing/module.go | 89 +++++++++++++++++++++------------------ x/upgrade/module.go | 18 +++++--- 8 files changed, 126 insertions(+), 80 deletions(-) delete mode 100644 x/bank/types/authority.go diff --git a/simapp/app.go b/simapp/app.go index 34432a1aac98..b0689eaf7d1d 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -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, diff --git a/x/bank/module.go b/x/bank/module.go index 647671b39205..a25dcfbb32fb 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -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( diff --git a/x/bank/types/authority.go b/x/bank/types/authority.go deleted file mode 100644 index 0be891346263..000000000000 --- a/x/bank/types/authority.go +++ /dev/null @@ -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() -} diff --git a/x/crisis/module.go b/x/crisis/module.go index 0df2d64d900c..0bf0e49bbcf0 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -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)) diff --git a/x/distribution/module.go b/x/distribution/module.go index 6f4b9f84aec4..5b5eb0ac02e7 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -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) diff --git a/x/mint/module.go b/x/mint/module.go index dbd91bac8cfd..4d9781f3d437 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -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)} } diff --git a/x/slashing/module.go b/x/slashing/module.go index 773b920f55f9..d554ff32e8c6 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -184,7 +184,39 @@ func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { BeginBlocker(ctx, req, am.keeper) } -// _____________________________________________________________________________________ +// AppModuleSimulation functions + +// GenerateGenesisState creates a randomized GenState of the slashing module. +func (AppModule) GenerateGenesisState(simState *module.SimulationState) { + simulation.RandomizedGenState(simState) +} + +// ProposalContents doesn't return any content functions for governance proposals. +func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent { + return nil +} + +// RandomizedParams creates randomized slashing param changes for the simulator. +func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { + return []simtypes.ParamChange{} +} + +// RegisterStoreDecoder registers a decoder for slashing module's types +func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc) +} + +// WeightedOperations returns the all the slashing module operations with their respective weights. +func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { + return simulation.WeightedOperations( + simState.AppParams, simState.Cdc, + am.accountKeeper, am.bankKeeper, am.keeper, am.stakingKeeper, + ) +} + +// ============================================================================ +// New App Wiring Setup +// ============================================================================ func init() { appmodule.Register( @@ -203,12 +235,15 @@ func provideModuleBasic() runtime.AppModuleBasicWrapper { 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"` + 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 @@ -223,7 +258,13 @@ type slashingOutputs struct { } func provideModule(in slashingInputs) slashingOutputs { - k := keeper.NewKeeper(in.Cdc, in.LegacyAmino, in.Key, in.StakingKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String()) + 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, @@ -231,35 +272,3 @@ func provideModule(in slashingInputs) slashingOutputs { Hooks: staking.StakingHooksWrapper{StakingHooks: k.Hooks()}, } } - -// _____________________________________________________________________________________ - -// AppModuleSimulation functions - -// GenerateGenesisState creates a randomized GenState of the slashing module. -func (AppModule) GenerateGenesisState(simState *module.SimulationState) { - simulation.RandomizedGenState(simState) -} - -// ProposalContents doesn't return any content functions for governance proposals. -func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent { - return nil -} - -// RandomizedParams creates randomized slashing param changes for the simulator. -func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { - return []simtypes.ParamChange{} -} - -// RegisterStoreDecoder registers a decoder for slashing module's types -func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { - sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc) -} - -// WeightedOperations returns the all the slashing module operations with their respective weights. -func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { - return simulation.WeightedOperations( - simState.AppParams, simState.Cdc, - am.accountKeeper, am.bankKeeper, am.keeper, am.stakingKeeper, - ) -} diff --git a/x/upgrade/module.go b/x/upgrade/module.go index 011885f0eec9..a3138ddf8079 100644 --- a/x/upgrade/module.go +++ b/x/upgrade/module.go @@ -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)}