refactor: Use CoreAPI when possible (#15496)

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Marko <marbar3778@yahoo.com>
This commit is contained in:
Facundo Medica 2023-03-23 19:40:09 -03:00 committed by GitHub
parent b44caab53a
commit 05dcf860eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 178 additions and 163 deletions

View File

@ -6,6 +6,7 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"cosmossdk.io/depinject"
@ -90,7 +91,10 @@ func NewAppModule(ak keeper.AccountKeeper, bk types.BankKeeper) AppModule {
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasServices = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -99,8 +103,9 @@ func (am AppModule) IsOnePerModuleType() {}
func (am AppModule) IsAppModule() {}
// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), NewMsgServerImpl(am.accountKeeper, am.bankKeeper))
func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
types.RegisterMsgServer(registrar, NewMsgServerImpl(am.accountKeeper, am.bankKeeper))
return nil
}
// InitGenesis performs a no-op.

View File

@ -6,9 +6,7 @@ import (
)
// BeginBlocker is called at the beginning of every block
func BeginBlocker(ctx sdk.Context, keeper keeper.Keeper) {
func BeginBlocker(ctx sdk.Context, keeper keeper.Keeper) error {
// delete all the mature grants
if err := keeper.DequeueAndDeleteExpiredGrants(ctx); err != nil {
panic(err)
}
return keeper.DequeueAndDeleteExpiredGrants(ctx)
}

View File

@ -31,7 +31,6 @@ import (
)
var (
_ module.BeginBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
@ -121,7 +120,10 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak authz.AccountKeeper,
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -154,8 +156,9 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) ConsensusVersion() uint64 { return 2 }
// BeginBlock returns the begin blocker for the authz module.
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
BeginBlocker(ctx, am.keeper)
func (am AppModule) BeginBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
return BeginBlocker(c, am.keeper)
}
func init() {

View File

@ -10,6 +10,7 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"google.golang.org/grpc"
storetypes "cosmossdk.io/core/store"
@ -88,7 +89,10 @@ type AppModule struct {
keeper keeper.Keeper
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasServices = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -97,9 +101,10 @@ func (am AppModule) IsOnePerModuleType() {}
func (am AppModule) IsAppModule() {}
// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper))
func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(registrar, keeper.NewQuerier(am.keeper))
return nil
}
// NewAppModule creates a new AppModule object

View File

@ -1,6 +1,7 @@
package crisis
import (
"context"
"encoding/json"
"fmt"
"time"
@ -35,10 +36,7 @@ import (
// ConsensusVersion defines the current x/crisis module consensus version.
const ConsensusVersion = 2
var (
_ module.EndBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)
var _ module.AppModuleBasic = AppModuleBasic{}
// Module init related flags
const (
@ -120,7 +118,10 @@ func NewAppModule(keeper *keeper.Keeper, skipGenesisInvariants bool, ss exported
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasEndBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -175,9 +176,10 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
// EndBlock returns the end blocker for the crisis module. It returns no validator
// updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
EndBlocker(ctx, *am.keeper)
return []abci.ValidatorUpdate{}
func (am AppModule) EndBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
EndBlocker(c, *am.keeper)
return nil
}
// App Wiring Setup

View File

@ -3,8 +3,6 @@ package distribution
import (
"time"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
@ -13,22 +11,23 @@ import (
// BeginBlocker sets the proposer for determining distribution during endblock
// and distribute rewards for the previous block.
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) error {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
// determine the total power signing the block
var previousTotalPower int64
for _, voteInfo := range req.LastCommitInfo.GetVotes() {
for _, voteInfo := range ctx.VoteInfos() {
previousTotalPower += voteInfo.Validator.Power
}
// TODO this is Tendermint-dependent
// ref https://github.com/cosmos/cosmos-sdk/issues/3095
if ctx.BlockHeight() > 1 {
k.AllocateTokens(ctx, previousTotalPower, req.LastCommitInfo.GetVotes())
k.AllocateTokens(ctx, previousTotalPower, ctx.VoteInfos())
}
// record the proposer for when we payout on the next block
consAddr := sdk.ConsAddress(req.Header.ProposerAddress)
consAddr := sdk.ConsAddress(ctx.BlockHeader().ProposerAddress)
k.SetPreviousProposerConsAddr(ctx, consAddr)
return nil
}

View File

@ -35,7 +35,6 @@ import (
const ConsensusVersion = 3
var (
_ module.BeginBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
@ -121,7 +120,10 @@ func NewAppModule(
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -174,8 +176,9 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
// BeginBlock returns the begin blocker for the distribution module.
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
BeginBlocker(ctx, req, am.keeper)
func (am AppModule) BeginBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
return BeginBlocker(c, am.keeper)
}
// AppModuleSimulation functions

View File

@ -8,6 +8,7 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"google.golang.org/grpc"
modulev1 "cosmossdk.io/api/cosmos/evidence/module/v1"
"cosmossdk.io/core/appmodule"
@ -122,7 +123,10 @@ func NewAppModule(keeper keeper.Keeper) AppModule {
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasServices = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -136,9 +140,10 @@ func (am AppModule) Name() string {
}
// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(registrar, am.keeper)
return nil
}
// InitGenesis performs the evidence module's genesis initialization It returns

View File

@ -31,7 +31,6 @@ import (
)
var (
_ module.EndBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
@ -129,7 +128,10 @@ func NewAppModule(cdc codec.Codec, ak feegrant.AccountKeeper, bk feegrant.BankKe
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasEndBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -171,9 +173,10 @@ func (AppModule) ConsensusVersion() uint64 { return 2 }
// EndBlock returns the end blocker for the feegrant module. It returns no validator
// updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
EndBlocker(ctx, am.keeper)
return []abci.ValidatorUpdate{}
func (am AppModule) EndBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
EndBlocker(c, am.keeper)
return nil
}
func init() {

View File

@ -39,7 +39,6 @@ import (
const ConsensusVersion = 5
var (
_ module.EndBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
@ -146,7 +145,10 @@ func NewAppModule(
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasEndBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -323,9 +325,10 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
// EndBlock returns the end blocker for the gov module. It returns no validator
// updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
EndBlocker(ctx, am.keeper)
return []abci.ValidatorUpdate{}
func (am AppModule) EndBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
EndBlocker(c, am.keeper)
return nil
}
// AppModuleSimulation functions

View File

@ -7,12 +7,10 @@ import (
// EndBlocker called at every block, updates proposal's `FinalTallyResult` and
// prunes expired proposals.
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
func EndBlocker(ctx sdk.Context, k keeper.Keeper) error {
if err := k.TallyProposalsAtVPEnd(ctx); err != nil {
panic(err)
return err
}
if err := k.PruneProposals(ctx); err != nil {
panic(err)
}
return k.PruneProposals(ctx)
}

View File

@ -32,7 +32,6 @@ import (
const ConsensusVersion = 2
var (
_ module.EndBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
@ -56,7 +55,10 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak group.AccountKeeper,
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasEndBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -155,9 +157,9 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
// EndBlock implements the group module's EndBlock.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
EndBlocker(ctx, am.keeper)
return []abci.ValidatorUpdate{}
func (am AppModule) EndBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
return EndBlocker(c, am.keeper)
}
// ____________________________________________________________________________

View File

@ -10,7 +10,7 @@ import (
)
// BeginBlocker mints new tokens for the previous block.
func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculationFn) {
func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculationFn) error {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
// fetch stored minter & params
@ -30,13 +30,13 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculatio
err := k.MintCoins(ctx, mintedCoins)
if err != nil {
panic(err)
return err
}
// send the minted coins to the fee collector account
err = k.AddCollectedFees(ctx, mintedCoins)
if err != nil {
panic(err)
return err
}
if mintedCoin.Amount.IsInt64() {
@ -52,4 +52,6 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculatio
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
),
)
return nil
}

View File

@ -35,7 +35,6 @@ import (
const ConsensusVersion = 2
var (
_ module.BeginBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
@ -130,7 +129,10 @@ func NewAppModule(
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -177,8 +179,9 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
// BeginBlock returns the begin blocker for the mint module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
BeginBlocker(ctx, am.keeper, am.inflationCalculator)
func (am AppModule) BeginBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
return BeginBlocker(c, am.keeper, am.inflationCalculator)
}
// AppModuleSimulation functions

View File

@ -7,6 +7,7 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
@ -46,9 +47,10 @@ func (AppModuleBasic) Name() string {
// RegisterServices registers a gRPC query service to respond to the
// module-specific gRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
nft.RegisterMsgServer(cfg.MsgServer(), am.keeper)
nft.RegisterQueryServer(cfg.QueryServer(), am.keeper)
func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
nft.RegisterMsgServer(registrar, am.keeper)
nft.RegisterQueryServer(registrar, am.keeper)
return nil
}
// RegisterLegacyAminoCodec registers the nft module's types for the given codec.
@ -113,7 +115,10 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak nft.AccountKeeper, b
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasServices = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}

View File

@ -3,8 +3,6 @@ package slashing
import (
"time"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
@ -13,13 +11,13 @@ import (
// BeginBlocker check for infraction evidence or downtime of validators
// on every begin block
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
// Iterate over all the validators which *should* have signed this block
// store whether or not they have actually signed it and slash/unbond any
// which have missed too many blocks in a row (downtime slashing)
for _, voteInfo := range req.LastCommitInfo.GetVotes() {
for _, voteInfo := range ctx.VoteInfos() {
k.HandleValidatorSignature(ctx, voteInfo.Validator.Address, voteInfo.Validator.Power, voteInfo.SignedLastBlock)
}
}

View File

@ -58,17 +58,12 @@ func TestBeginBlocker(t *testing.T) {
Power: power,
}
// mark the validator as having signed
req := abci.RequestBeginBlock{
LastCommitInfo: abci.CommitInfo{
Votes: []abci.VoteInfo{{
Validator: val,
SignedLastBlock: true,
}},
},
}
ctx = ctx.WithVoteInfos([]abci.VoteInfo{{
Validator: val,
SignedLastBlock: true,
}})
slashing.BeginBlocker(ctx, req, slashingKeeper)
slashing.BeginBlocker(ctx, slashingKeeper)
info, found := slashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
require.True(t, found)
@ -81,32 +76,24 @@ func TestBeginBlocker(t *testing.T) {
// for 1000 blocks, mark the validator as having signed
for ; height < slashingKeeper.SignedBlocksWindow(ctx); height++ {
ctx = ctx.WithBlockHeight(height)
req = abci.RequestBeginBlock{
LastCommitInfo: abci.CommitInfo{
Votes: []abci.VoteInfo{{
Validator: val,
SignedLastBlock: true,
}},
},
}
ctx = ctx.WithBlockHeight(height).
WithVoteInfos([]abci.VoteInfo{{
Validator: val,
SignedLastBlock: true,
}})
slashing.BeginBlocker(ctx, req, slashingKeeper)
slashing.BeginBlocker(ctx, slashingKeeper)
}
// for 500 blocks, mark the validator as having not signed
for ; height < ((slashingKeeper.SignedBlocksWindow(ctx) * 2) - slashingKeeper.MinSignedPerWindow(ctx) + 1); height++ {
ctx = ctx.WithBlockHeight(height)
req = abci.RequestBeginBlock{
LastCommitInfo: abci.CommitInfo{
Votes: []abci.VoteInfo{{
Validator: val,
SignedLastBlock: false,
}},
},
}
ctx = ctx.WithBlockHeight(height).
WithVoteInfos([]abci.VoteInfo{{
Validator: val,
SignedLastBlock: false,
}})
slashing.BeginBlocker(ctx, req, slashingKeeper)
slashing.BeginBlocker(ctx, slashingKeeper)
}
// end block

View File

@ -35,7 +35,6 @@ import (
const ConsensusVersion = 3
var (
_ module.BeginBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
@ -120,7 +119,10 @@ func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper,
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -168,8 +170,10 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
// BeginBlock returns the begin blocker for the slashing module.
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
BeginBlocker(ctx, req, am.keeper)
func (am AppModule) BeginBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
BeginBlocker(c, am.keeper)
return nil
}
// AppModuleSimulation functions

View File

@ -38,7 +38,6 @@ const (
)
var (
_ module.BeginBlockAppModule = AppModule{}
_ module.EndBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
@ -128,7 +127,10 @@ func NewAppModule(
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -183,8 +185,10 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) ConsensusVersion() uint64 { return consensusVersion }
// BeginBlock returns the begin blocker for the staking module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
BeginBlocker(ctx, am.keeper)
func (am AppModule) BeginBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
BeginBlocker(c, am.keeper)
return nil
}
// EndBlock returns the end blocker for the staking module. It returns no validator

View File

@ -4,8 +4,6 @@ import (
"fmt"
"time"
abci "github.com/cometbft/cometbft/abci/types"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/upgrade/keeper"
"cosmossdk.io/x/upgrade/types"
@ -22,7 +20,7 @@ import (
// The purpose is to ensure the binary is switched EXACTLY at the desired block, and to allow
// a migration to be executed if needed upon this switch (migration defined in the new binary)
// skipUpgradeHeightArray is a set of block heights for which the upgrade must be skipped
func BeginBlocker(k *keeper.Keeper, ctx sdk.Context, _ abci.RequestBeginBlock) {
func BeginBlocker(k *keeper.Keeper, ctx sdk.Context) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
plan, found := k.GetUpgradePlan(ctx)

View File

@ -6,11 +6,11 @@ import (
"testing"
"time"
abci "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/upgrade"
@ -31,7 +31,7 @@ import (
type TestSuite struct {
suite.Suite
module module.BeginBlockAppModule
module appmodule.HasBeginBlocker
keeper *keeper.Keeper
handler govtypesv1beta1.Handler
ctx sdk.Context
@ -102,9 +102,8 @@ func VerifyDoUpgrade(t *testing.T) {
t.Log("Verify that a panic happens at the upgrade height")
newCtx := s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1).WithBlockTime(time.Now())
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
require.Panics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
t.Log("Verify that the upgrade can be successfully applied with a handler")
@ -112,7 +111,7 @@ func VerifyDoUpgrade(t *testing.T) {
return vm, nil
})
require.NotPanics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
VerifyCleared(t, newCtx)
@ -120,9 +119,8 @@ func VerifyDoUpgrade(t *testing.T) {
func VerifyDoUpgradeWithCtx(t *testing.T, newCtx sdk.Context, proposalName string) {
t.Log("Verify that a panic happens at the upgrade height")
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
require.Panics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
t.Log("Verify that the upgrade can be successfully applied with a handler")
@ -130,7 +128,7 @@ func VerifyDoUpgradeWithCtx(t *testing.T, newCtx sdk.Context, proposalName strin
return vm, nil
})
require.NotPanics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
VerifyCleared(t, newCtx)
@ -146,9 +144,8 @@ func TestHaltIfTooNew(t *testing.T) {
})
newCtx := s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1).WithBlockTime(time.Now())
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
require.NotPanics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
require.Equal(t, 0, called)
@ -156,16 +153,15 @@ func TestHaltIfTooNew(t *testing.T) {
err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "future", Height: s.ctx.BlockHeight() + 3}}) //nolint:staticcheck // we're testing deprecated code
require.NoError(t, err)
require.Panics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
require.Equal(t, 0, called)
t.Log("Verify we no longer panic if the plan is on time")
futCtx := s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 3).WithBlockTime(time.Now())
req = abci.RequestBeginBlock{Header: futCtx.BlockHeader()}
require.NotPanics(t, func() {
s.module.BeginBlock(futCtx, req)
s.module.BeginBlock(futCtx)
})
require.Equal(t, 1, called)
@ -206,9 +202,8 @@ func TestCantApplySameUpgradeTwice(t *testing.T) {
func TestNoSpuriousUpgrades(t *testing.T) {
s := setupTest(t, 10, map[int64]bool{})
t.Log("Verify that no upgrade panic is triggered in the BeginBlocker when we haven't scheduled an upgrade")
req := abci.RequestBeginBlock{Header: s.ctx.BlockHeader()}
require.NotPanics(t, func() {
s.module.BeginBlock(s.ctx, req)
s.module.BeginBlock(s.ctx)
})
}
@ -258,7 +253,6 @@ func TestSkipUpgradeSkippingAll(t *testing.T) {
newCtx := s.ctx
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: skipOne}}) //nolint:staticcheck // we're testing deprecated code
require.NoError(t, err)
@ -267,7 +261,7 @@ func TestSkipUpgradeSkippingAll(t *testing.T) {
newCtx = newCtx.WithBlockHeight(skipOne)
require.NotPanics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
t.Log("Verify a second proposal also is being cleared")
@ -276,7 +270,7 @@ func TestSkipUpgradeSkippingAll(t *testing.T) {
newCtx = newCtx.WithBlockHeight(skipTwo)
require.NotPanics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
// To ensure verification is being done only after both upgrades are cleared
@ -295,7 +289,6 @@ func TestUpgradeSkippingOne(t *testing.T) {
newCtx := s.ctx
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: skipOne}}) //nolint:staticcheck // we're testing deprecated code
require.NoError(t, err)
@ -305,7 +298,7 @@ func TestUpgradeSkippingOne(t *testing.T) {
// Setting block height of proposal test
newCtx = newCtx.WithBlockHeight(skipOne)
require.NotPanics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
t.Log("Verify the second proposal is not skipped")
@ -330,7 +323,6 @@ func TestUpgradeSkippingOnlyTwo(t *testing.T) {
newCtx := s.ctx
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: skipOne}}) //nolint:staticcheck // we're testing deprecated code
require.NoError(t, err)
@ -340,7 +332,7 @@ func TestUpgradeSkippingOnlyTwo(t *testing.T) {
// Setting block height of proposal test
newCtx = newCtx.WithBlockHeight(skipOne)
require.NotPanics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
// A new proposal with height in skipUpgradeHeights
@ -349,7 +341,7 @@ func TestUpgradeSkippingOnlyTwo(t *testing.T) {
// Setting block height of proposal test2
newCtx = newCtx.WithBlockHeight(skipTwo)
require.NotPanics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
t.Log("Verify a new proposal is not skipped")
@ -367,12 +359,11 @@ func TestUpgradeSkippingOnlyTwo(t *testing.T) {
func TestUpgradeWithoutSkip(t *testing.T) {
s := setupTest(t, 10, map[int64]bool{})
newCtx := s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1).WithBlockTime(time.Now())
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: s.ctx.BlockHeight() + 1}}) //nolint:staticcheck // we're testing deprecated code
require.NoError(t, err)
t.Log("Verify if upgrade happens without skip upgrade")
require.Panics(t, func() {
s.module.BeginBlock(newCtx, req)
s.module.BeginBlock(newCtx)
})
VerifyDoUpgrade(t)
@ -417,20 +408,19 @@ func TestBinaryVersion(t *testing.T) {
testCases := []struct {
name string
preRun func() (sdk.Context, abci.RequestBeginBlock)
preRun func() sdk.Context
expectPanic bool
}{
{
"test not panic: no scheduled upgrade or applied upgrade is present",
func() (sdk.Context, abci.RequestBeginBlock) {
req := abci.RequestBeginBlock{Header: s.ctx.BlockHeader()}
return s.ctx, req
func() sdk.Context {
return s.ctx
},
false,
},
{
"test not panic: upgrade handler is present for last applied upgrade",
func() (sdk.Context, abci.RequestBeginBlock) {
func() sdk.Context {
s.keeper.SetUpgradeHandler("test0", func(_ sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
return vm, nil
})
@ -444,34 +434,32 @@ func TestBinaryVersion(t *testing.T) {
Height: 12,
})
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
return newCtx, req
return newCtx
},
false,
},
{
"test panic: upgrade needed",
func() (sdk.Context, abci.RequestBeginBlock) {
func() sdk.Context {
err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "Upgrade test", Plan: types.Plan{Name: "test2", Height: 13}}) //nolint:staticcheck // we're testing deprecated code
require.NoError(t, err)
newCtx := s.ctx.WithBlockHeight(13)
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
return newCtx, req
return newCtx
},
true,
},
}
for _, tc := range testCases {
ctx, req := tc.preRun()
ctx := tc.preRun()
if tc.expectPanic {
require.Panics(t, func() {
s.module.BeginBlock(ctx, req)
s.module.BeginBlock(ctx)
})
} else {
require.NotPanics(t, func() {
s.module.BeginBlock(ctx, req)
s.module.BeginBlock(ctx)
})
}
}
@ -503,9 +491,8 @@ func TestDowngradeVerification(t *testing.T) {
})
// successful upgrade.
req := abci.RequestBeginBlock{Header: ctx.BlockHeader()}
require.NotPanics(t, func() {
m.BeginBlock(ctx, req)
m.BeginBlock(ctx)
})
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
@ -552,14 +539,13 @@ func TestDowngradeVerification(t *testing.T) {
tc.preRun(k, ctx, name)
}
req := abci.RequestBeginBlock{Header: ctx.BlockHeader()}
if tc.expectPanic {
require.Panics(t, func() {
m.BeginBlock(ctx, req)
m.BeginBlock(ctx)
}, name)
} else {
require.NotPanics(t, func() {
m.BeginBlock(ctx, req)
m.BeginBlock(ctx)
}, name)
}
}

View File

@ -41,10 +41,7 @@ func init() {
// ConsensusVersion defines the current x/upgrade module consensus version.
const ConsensusVersion uint64 = 2
var (
_ module.BeginBlockAppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)
var _ module.AppModuleBasic = AppModuleBasic{}
// AppModuleBasic implements the sdk.AppModuleBasic interface
type AppModuleBasic struct{}
@ -95,7 +92,10 @@ func NewAppModule(keeper *keeper.Keeper) AppModule {
}
}
var _ appmodule.AppModule = AppModule{}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasBeginBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
@ -154,8 +154,10 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
// BeginBlock calls the upgrade module hooks
//
// CONTRACT: this is registered in BeginBlocker *before* all other modules' BeginBlock functions
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
BeginBlocker(am.keeper, ctx, req)
func (am AppModule) BeginBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
BeginBlocker(am.keeper, c)
return nil
}
//