Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate x/staking to app wiring #12102

Merged
merged 26 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f272fc8
feat: migrate x/staking to app wiring
JeancarloBarrios May 31, 2022
0530a1c
add staking to app yaml
JeancarloBarrios May 31, 2022
15446da
modified staking module
JeancarloBarrios May 31, 2022
12d1ec4
partial simapp changes
JeancarloBarrios May 31, 2022
ebeb7c5
generated pulsar
JeancarloBarrios May 31, 2022
fbbbd46
pre merge
JeancarloBarrios Jun 2, 2022
b467dcd
Merge branch 'main' into JeancarloBarrios/migrate_staking_use_app_wiring
JeancarloBarrios Jun 2, 2022
6046ac5
simapp app
JeancarloBarrios Jun 2, 2022
39eb9a9
add depinject struct for parmeters
JeancarloBarrios Jun 2, 2022
c0eaec6
Add depinject key to bankKeeper
amaury1093 Jun 2, 2022
834fd1a
PR changes
JeancarloBarrios Jun 2, 2022
ed83c41
Update proto/cosmos/staking/module/v1/module.proto
JeancarloBarrios Jun 2, 2022
7bb92f1
Update x/staking/module.go
JeancarloBarrios Jun 2, 2022
722a05e
PR changes
JeancarloBarrios Jun 2, 2022
16a6a40
update pulsar
JeancarloBarrios Jun 2, 2022
6e8a954
PR changes
JeancarloBarrios Jun 2, 2022
7b8c22f
Merge branch 'main' into JeancarloBarrios/migrate_staking_use_app_wiring
JeancarloBarrios Jun 2, 2022
194538c
Merge branch 'main' into JeancarloBarrios/migrate_staking_use_app_wiring
JeancarloBarrios Jun 2, 2022
2005d75
merge changes
JeancarloBarrios Jun 2, 2022
1dc2759
Pass StakingKeeper by reference everywhere
kocubinski Jun 2, 2022
215b63f
change some keeper value to reference for the hooks fix
JeancarloBarrios Jun 2, 2022
cd880e4
change test tools to recieve pointer insted of instance
JeancarloBarrios Jun 2, 2022
9ec5a3c
change test tools to recieve pointer insted of instance
JeancarloBarrios Jun 2, 2022
b241d51
Fix tests, more pass by reference
kocubinski Jun 2, 2022
35440b7
change test tools to recieve pointer insted of instance
JeancarloBarrios Jun 2, 2022
d8903e2
Merge remote-tracking branch 'origin/JeancarloBarrios/migrate_staking…
kocubinski Jun 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
503 changes: 503 additions & 0 deletions api/cosmos/staking/module/v1/module.pulsar.go

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions proto/cosmos/staking/module/v1/module.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

package cosmos.staking.module.v1;

import "cosmos/app/v1alpha1/module.proto";

// Module is the config object of the staking module.
message Module {
option (cosmos.app.v1alpha1.module) = {
go_import: "github.com/cosmos/cosmos-sdk/x/staking"
};
}
33 changes: 13 additions & 20 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ type SimApp struct {
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper stakingkeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
MintKeeper mintkeeper.Keeper
DistrKeeper distrkeeper.Keeper
Expand Down Expand Up @@ -224,6 +224,7 @@ func NewSimApp(
&app.AccountKeeper,
&app.BankKeeper,
&app.FeeGrantKeeper,
&app.StakingKeeper,
)
if err != nil {
panic(err)
Expand All @@ -232,7 +233,7 @@ func NewSimApp(
app.App = appBuilder.Build(logger, db, traceStore, baseAppOptions...)

app.keys = sdk.NewKVStoreKeys(
stakingtypes.StoreKey, minttypes.StoreKey, distrtypes.StoreKey,
minttypes.StoreKey, distrtypes.StoreKey,
slashingtypes.StoreKey, govtypes.StoreKey, upgradetypes.StoreKey,
evidencetypes.StoreKey, authzkeeper.StoreKey, nftkeeper.StoreKey,
group.StoreKey,
Expand All @@ -249,28 +250,22 @@ func NewSimApp(

initParamsKeeper(app.ParamsKeeper)

// add keepers
stakingKeeper := stakingkeeper.NewKeeper(
app.appCodec, app.keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName),
)
app.MintKeeper = mintkeeper.NewKeeper(
app.appCodec, app.keys[minttypes.StoreKey], app.GetSubspace(minttypes.ModuleName), &stakingKeeper,
app.appCodec, app.keys[minttypes.StoreKey], app.GetSubspace(minttypes.ModuleName), app.StakingKeeper,
app.AccountKeeper, app.BankKeeper, authtypes.FeeCollectorName,
)
app.DistrKeeper = distrkeeper.NewKeeper(
app.appCodec, app.keys[distrtypes.StoreKey], app.GetSubspace(distrtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
&stakingKeeper, authtypes.FeeCollectorName,
app.StakingKeeper, authtypes.FeeCollectorName,
)
app.SlashingKeeper = slashingkeeper.NewKeeper(
app.appCodec, app.keys[slashingtypes.StoreKey], &stakingKeeper, app.GetSubspace(slashingtypes.ModuleName),
app.appCodec, app.keys[slashingtypes.StoreKey], app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName),
)
app.CrisisKeeper = crisiskeeper.NewKeeper(
app.GetSubspace(crisistypes.ModuleName), invCheckPeriod, app.BankKeeper, authtypes.FeeCollectorName,
)

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.StakingKeeper = *stakingKeeper.SetHooks(
app.StakingKeeper.SetHooks(
stakingtypes.NewMultiStakingHooks(app.DistrKeeper.Hooks(), app.SlashingKeeper.Hooks()),
)

Expand All @@ -296,7 +291,7 @@ func NewSimApp(
*/
govKeeper := govkeeper.NewKeeper(
app.appCodec, app.keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
&stakingKeeper, govRouter, app.MsgServiceRouter(), govConfig,
app.StakingKeeper, govRouter, app.MsgServiceRouter(), govConfig,
)

app.GovKeeper = *govKeeper.SetHooks(
Expand All @@ -311,7 +306,7 @@ func NewSimApp(

// create evidence keeper with router
evidenceKeeper := evidencekeeper.NewKeeper(
app.appCodec, app.keys[evidencetypes.StoreKey], &app.StakingKeeper, app.SlashingKeeper,
app.appCodec, app.keys[evidencetypes.StoreKey], app.StakingKeeper, app.SlashingKeeper,
)
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper
Expand All @@ -333,9 +328,8 @@ func NewSimApp(
crisis.NewAppModule(&app.CrisisKeeper, skipGenesisInvariants),
gov.NewAppModule(app.appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(app.appCodec, app.MintKeeper, app.AccountKeeper, nil),
slashing.NewAppModule(app.appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
distr.NewAppModule(app.appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
staking.NewAppModule(app.appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
slashing.NewAppModule(app.appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, *app.StakingKeeper),
distr.NewAppModule(app.appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, *app.StakingKeeper),
upgrade.NewAppModule(app.UpgradeKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
authzmodule.NewAppModule(app.appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
Expand Down Expand Up @@ -384,8 +378,8 @@ func NewSimApp(
gov.NewAppModule(app.appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(app.appCodec, app.MintKeeper, app.AccountKeeper, nil),
staking.NewAppModule(app.appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
distr.NewAppModule(app.appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
slashing.NewAppModule(app.appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
distr.NewAppModule(app.appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, *app.StakingKeeper),
slashing.NewAppModule(app.appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, *app.StakingKeeper),
params.NewAppModule(app.ParamsKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
authzmodule.NewAppModule(app.appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
Expand Down Expand Up @@ -582,7 +576,6 @@ func GetMaccPerms() map[string][]string {

// initParamsKeeper init params keeper and its subspaces
func initParamsKeeper(paramsKeeper paramskeeper.Keeper) {
paramsKeeper.Subspace(stakingtypes.ModuleName)
paramsKeeper.Subspace(minttypes.ModuleName)
paramsKeeper.Subspace(distrtypes.ModuleName)
paramsKeeper.Subspace(slashingtypes.ModuleName)
Expand Down
4 changes: 4 additions & 0 deletions simapp/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ modules:
config:
"@type": cosmos.capability.module.v1.Module
seal_keeper: true

- name: staking
config:
"@type": cosmos.staking.module.v1.Module
4 changes: 2 additions & 2 deletions simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (app *SimApp) ExportAppStateAndValidators(
return servertypes.ExportedApp{}, err
}

validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
validators, err := staking.WriteValidators(ctx, *app.StakingKeeper)
return servertypes.ExportedApp{
AppState: appState,
Validators: validators,
Expand Down Expand Up @@ -163,7 +163,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []

// Iterate through validators by power descending, reset bond heights, and
// update bond intra-tx counters.
store := ctx.KVStore(app.keys[stakingtypes.StoreKey])
store := ctx.KVStore(app.GetKey(stakingtypes.StoreKey))
iter := sdk.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
counter := int16(0)

Expand Down
6 changes: 3 additions & 3 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ type bankInputs struct {
type bankOutputs struct {
depinject.Out

Keeper keeper.Keeper `key:"cosmos.bank.v1.Keeper"`
Module runtime.AppModuleWrapper
BankKeeper keeper.Keeper `key:"cosmos.bank.v1.Keeper"`
Module runtime.AppModuleWrapper
}

func provideModule(in bankInputs) bankOutputs {
Expand All @@ -254,5 +254,5 @@ func provideModule(in bankInputs) bankOutputs {

bankKeeper := keeper.NewBaseKeeper(in.Cdc, in.Key, in.AccountKeeper, in.Subspace, blockedAddresses)
m := NewAppModule(in.Cdc, bankKeeper, in.AccountKeeper)
return bankOutputs{Keeper: bankKeeper, Module: runtime.WrapAppModule(m)}
return bankOutputs{BankKeeper: bankKeeper, Module: runtime.WrapAppModule(m)}
}
4 changes: 2 additions & 2 deletions x/staking/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (

// BeginBlocker will persist the current header and validator set as a historical entry
// and prune the oldest entry based on the HistoricalEntries parameter
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
func BeginBlocker(ctx sdk.Context, k *keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

k.TrackHistoricalInfo(ctx)
}

// Called every block, update validator set
func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate {
func EndBlocker(ctx sdk.Context, k *keeper.Keeper) []abci.ValidatorUpdate {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

return k.BlockValidatorUpdates(ctx)
Expand Down
2 changes: 1 addition & 1 deletion x/staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// Querier is used as Keeper will have duplicate methods if used directly, and gRPC names take precedence over keeper
type Querier struct {
Keeper
*Keeper
}

var _ types.QueryServer = Querier{}
Expand Down
12 changes: 6 additions & 6 deletions x/staking/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// RegisterInvariants registers all staking invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
func RegisterInvariants(ir sdk.InvariantRegistry, k *Keeper) {
ir.RegisterRoute(types.ModuleName, "module-accounts",
ModuleAccountInvariants(k))
ir.RegisterRoute(types.ModuleName, "nonnegative-power",
Expand All @@ -21,7 +21,7 @@ func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
}

// AllInvariants runs all invariants of the staking module.
func AllInvariants(k Keeper) sdk.Invariant {
func AllInvariants(k *Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
res, stop := ModuleAccountInvariants(k)(ctx)
if stop {
Expand All @@ -44,7 +44,7 @@ func AllInvariants(k Keeper) sdk.Invariant {

// ModuleAccountInvariants checks that the bonded and notBonded ModuleAccounts pools
// reflects the tokens actively bonded and not bonded
func ModuleAccountInvariants(k Keeper) sdk.Invariant {
func ModuleAccountInvariants(k *Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
bonded := sdk.ZeroInt()
notBonded := sdk.ZeroInt()
Expand Down Expand Up @@ -91,7 +91,7 @@ func ModuleAccountInvariants(k Keeper) sdk.Invariant {
}

// NonNegativePowerInvariant checks that all stored validators have >= 0 power.
func NonNegativePowerInvariant(k Keeper) sdk.Invariant {
func NonNegativePowerInvariant(k *Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
Expand Down Expand Up @@ -126,7 +126,7 @@ func NonNegativePowerInvariant(k Keeper) sdk.Invariant {
}

// PositiveDelegationInvariant checks that all stored delegations have > 0 shares.
func PositiveDelegationInvariant(k Keeper) sdk.Invariant {
func PositiveDelegationInvariant(k *Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
Expand Down Expand Up @@ -156,7 +156,7 @@ func PositiveDelegationInvariant(k Keeper) sdk.Invariant {
// DelegatorSharesInvariant checks whether all the delegator shares which persist
// in the delegator object add up to the correct total delegator shares
// amount stored in each validator.
func DelegatorSharesInvariant(k Keeper) sdk.Invariant {
func DelegatorSharesInvariant(k *Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
Expand Down
8 changes: 3 additions & 5 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Keeper struct {
func NewKeeper(
cdc codec.BinaryCodec, key storetypes.StoreKey, ak types.AccountKeeper, bk types.BankKeeper,
ps paramtypes.Subspace,
) Keeper {
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
Expand All @@ -47,7 +47,7 @@ func NewKeeper(
panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
}

return Keeper{
Copy link
Member

@kocubinski kocubinski Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fundamentally this change is required so that when InitGenesis iterates over the ModuleManager the StakingKeeper which is retrieved (by reference) is the same area of memory which the post construction SetHooks mutation modifies.

This is the reason for now passing the Keeper around everywhere by reference.

Not a huge fan of this, but it seems it will be up for refactor when we get to x/distribution and x/slashing, or possibly sooner. Those modules can provide their StakingHooks for this module to order as BaseAppOptions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

return &Keeper{
storeKey: key,
cdc: cdc,
authKeeper: ak,
Expand All @@ -63,14 +63,12 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
}

// Set the validator hooks
func (k *Keeper) SetHooks(sh types.StakingHooks) *Keeper {
Copy link
Member

@kocubinski kocubinski Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern makes little sense to me given the name SetHooks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add an API-breaking changelog entry, iirc Osmosis is using SetHooks pretty heavily

func (k *Keeper) SetHooks(sh types.StakingHooks) {
if k.hooks != nil {
panic("cannot set validator hooks twice")
}

k.hooks = sh

return k
}

// Load the last total validator power.
Expand Down
4 changes: 2 additions & 2 deletions x/staking/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
keeper *Keeper
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
func NewMigrator(keeper *Keeper) Migrator {
return Migrator{
keeper: keeper,
}
Expand Down
4 changes: 2 additions & 2 deletions x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import (
)

type msgServer struct {
Keeper
*Keeper
}

// NewMsgServerImpl returns an implementation of the bank MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
func NewMsgServerImpl(keeper *Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}

Expand Down
Loading