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: add endblocker with valsetupdate type #15829

Merged
merged 10 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
23 changes: 18 additions & 5 deletions runtime/app.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package runtime

import (
"context"
"encoding/json"
"fmt"

abci "github.com/cometbft/cometbft/abci/types"
"golang.org/x/exp/slices"

runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"

storetypes "cosmossdk.io/store/types"
abci "github.com/cometbft/cometbft/abci/types"
"golang.org/x/exp/slices"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -52,6 +51,8 @@ type App struct {
// initChainer is the init chainer function defined by the app config.
// this is only required if the chain wants to add special InitChainer logic.
initChainer sdk.InitChainer

ValSetUpdate []abci.ValidatorUpdate
Copy link
Member

Choose a reason for hiding this comment

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

why is this needed?

}

// RegisterModules registers the provided modules with the module manager and
Expand Down Expand Up @@ -122,7 +123,19 @@ func (a *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) (abci.Re

// EndBlocker application updates every end block
func (a *App) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) (abci.ResponseEndBlock, error) {
return a.ModuleManager.EndBlock(ctx, req)
endblock, err := a.ModuleManager.EndBlock(ctx, req)
Fixed Show fixed Hide fixed
if err != nil {
return abci.ResponseEndBlock{}, err
}
endblock.ValidatorUpdates = a.ValSetUpdate

return endblock, nil
}

// SetValidatorUpdates sets the validator updates for the next block.
// CONTRACT: this must be called for modules that are updating the validator set
func (a *App) SetValidatorUpdates(ctx context.Context, valset []abci.ValidatorUpdate) {
a.ValSetUpdate = valset
}

// InitChainer initializes the chain.
Expand Down
4 changes: 4 additions & 0 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,7 @@ func ProvideTransientStoreService(key depinject.ModuleKey, app *AppBuilder) stor
func ProvideEventService() event.Service {
return EventService{}
}

func ProvideSetValidatorService(ab *AppBuilder) ValidatorUpdateService {
return ab.app
}
2 changes: 1 addition & 1 deletion x/feegrant/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions x/staking/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

// 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) {
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 {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)
Expand Down
28 changes: 28 additions & 0 deletions x/staking/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package keeper

import (
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

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

k.TrackHistoricalInfo(ctx)
Fixed Show fixed Hide fixed
}

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

if k.validatorService == nil {
panic("validator service not set")
Fixed Show fixed Hide fixed
}

k.validatorService.SetValidatorUpdates(ctx, k.BlockValidatorUpdates(ctx))
Fixed Show fixed Hide fixed
}
21 changes: 15 additions & 6 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand All @@ -27,6 +28,8 @@ type Keeper struct {
bankKeeper types.BankKeeper
hooks types.StakingHooks
authority string

validatorService runtime.ValidatorUpdateService
}

// NewKeeper creates a new staking Keeper instance
Expand All @@ -36,6 +39,7 @@ func NewKeeper(
ak types.AccountKeeper,
bk types.BankKeeper,
authority string,
vs runtime.ValidatorUpdateService,
) *Keeper {
// ensure bonded and not bonded module accounts are set
if addr := ak.GetModuleAddress(types.BondedPoolName); addr == nil {
Expand All @@ -46,18 +50,23 @@ func NewKeeper(
panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
}

if vs == nil {
panic("validator service not set")
}

// ensure that authority is a valid AccAddress
if _, err := ak.StringToBytes(authority); err != nil {
panic("authority is not a valid acc address")
}

return &Keeper{
storeKey: key,
cdc: cdc,
authKeeper: ak,
bankKeeper: bk,
hooks: nil,
authority: authority,
storeKey: key,
cdc: cdc,
authKeeper: ak,
bankKeeper: bk,
hooks: nil,
authority: authority,
validatorService: vs,
}
}

Expand Down
25 changes: 14 additions & 11 deletions x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@ import (
"fmt"
"sort"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/staking/exported"

abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"

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

store "cosmossdk.io/store/types"
abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
"github.com/cosmos/cosmos-sdk/x/staking/exported"
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/simulation"
"github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -191,13 +189,15 @@ func (AppModule) ConsensusVersion() uint64 { return consensusVersion }
// BeginBlock returns the begin blocker for the staking module.
func (am AppModule) BeginBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
BeginBlocker(c, am.keeper)

am.keeper.BeginBlocker(c)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods

path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call path flow from Begin/EndBlock to a panic call
return nil
}

// EndBlock returns the end blocker for the staking module. It returns no validator
// updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
// am.keeper.EndBlocker(ctx) // TODO uncomment
return EndBlocker(ctx, am.keeper)
}

Expand All @@ -218,6 +218,8 @@ type ModuleInputs struct {
Cdc codec.Codec
Key *store.KVStoreKey

Vs runtime.ValidatorUpdateService

// LegacySubspace is used solely for migration of x/params managed parameters
LegacySubspace exported.Subspace
}
Expand All @@ -243,6 +245,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
in.AccountKeeper,
in.BankKeeper,
authority.String(),
in.Vs,
)
m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.LegacySubspace)
return ModuleOutputs{StakingKeeper: k, Module: m}
Expand Down