Skip to content

Commit

Permalink
feat: allow custom authority and inflation function when using app-wi…
Browse files Browse the repository at this point in the history
…ring (#12660)
  • Loading branch information
julienrbrt authored Jul 21, 2022
1 parent 345e81a commit b72d12e
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 80 deletions.
18 changes: 16 additions & 2 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 8 additions & 7 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 0 additions & 11 deletions x/bank/types/authority.go

This file was deleted.

18 changes: 13 additions & 5 deletions x/crisis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
Expand Down
16 changes: 12 additions & 4 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -264,14 +266,20 @@ 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,
in.AccountKeeper,
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)
Expand Down
21 changes: 15 additions & 6 deletions x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -261,18 +264,24 @@ 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,
in.StakingKeeper,
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)}
}
89 changes: 49 additions & 40 deletions x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -223,43 +258,17 @@ 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,
Module: runtime.WrapAppModule(m),
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,
)
}
18 changes: 13 additions & 5 deletions x/upgrade/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)}

Expand Down

0 comments on commit b72d12e

Please sign in to comment.