Skip to content

Commit

Permalink
fix: nil consensus params in BeginBlock during migration (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code committed Aug 5, 2024
1 parent 909b01b commit de4d336
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
9 changes: 9 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ func (app *App) Name() string {

// BeginBlocker application updates every begin block
func (app *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
consParams := ctx.ConsensusParams()
if consParams == nil || consParams.Block == nil {
baseAppLegacySS, found := app.ParamsKeeper.GetSubspace(baseapp.Paramspace)
if !found {
panic("consensus params not found")
}
cp := baseapp.GetConsensusParams(ctx, baseAppLegacySS)
ctx = ctx.WithConsensusParams(cp)
}
return app.mm.BeginBlock(ctx, req)
}

Expand Down
1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,5 +620,6 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(evmtypes.ModuleName).WithKeyTable(v0evmtypes.ParamKeyTable()) // nolint: staticcheck
paramsKeeper.Subspace(feemarkettypes.ModuleName).WithKeyTable(feemarkettypes.ParamKeyTable())

paramsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable())
return paramsKeeper
}
22 changes: 19 additions & 3 deletions app/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"os"
"path/filepath"
"testing"
"time"

dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
Expand All @@ -27,7 +29,7 @@ func Test_UpgradeAndMigrate(t *testing.T) {
fxtypes.SetConfig(true)

home := filepath.Join(os.Getenv("HOME"), "tmp")
chainId := fxtypes.MainnetChainId
chainId := fxtypes.TestnetChainId // The upgrade test is not related to chainId, do not modify it

db, err := dbm.NewDB("application", dbm.GoLevelDBBackend, filepath.Join(home, "data"))
require.NoError(t, err)
Expand All @@ -41,9 +43,23 @@ func Test_UpgradeAndMigrate(t *testing.T) {

ctx := newContext(t, myApp, chainId)

myApp.UpgradeKeeper.ApplyUpgrade(ctx, upgradetypes.Plan{
require.NoError(t, myApp.UpgradeKeeper.ScheduleUpgrade(ctx, upgradetypes.Plan{
Name: nextversion.Upgrade.UpgradeName,
Height: ctx.BlockHeight() + 5,
Height: ctx.BlockHeight() + 1,
}))

header := ctx.BlockHeader()
header.Height = header.Height + 1
header.Time = time.Now().UTC()
require.NotPanics(t, func() {
myApp.BeginBlock(abci.RequestBeginBlock{
Header: header,
})
})
require.NotPanics(t, func() {
myApp.EndBlock(abci.RequestEndBlock{
Height: header.Height,
})
})
}

Expand Down
6 changes: 4 additions & 2 deletions app/upgrades/v7/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
autytypes "github.com/cosmos/cosmos-sdk/x/auth/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/functionx/fx-core/v7/app/keepers"
Expand All @@ -17,7 +16,10 @@ import (
func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, app *keepers.AppKeepers) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// Migrate Tendermint consensus parameters from x/params module to a dedicated x/consensus module.
baseAppLegacySS := app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable())
baseAppLegacySS, found := app.ParamsKeeper.GetSubspace(baseapp.Paramspace)
if !found {
panic("baseapp subspace not found")
}
baseapp.MigrateParams(ctx, baseAppLegacySS, &app.ConsensusParamsKeeper)

cacheCtx, commit := ctx.CacheContext()
Expand Down
2 changes: 1 addition & 1 deletion x/gov/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (keeper Keeper) EndBlocker(ctx sdk.Context) {
))

logger.Info(
"proposal did not meet minimum deposit; deleted",
"proposal did not meet the minimum deposit requirement and has been deleted",
"proposal", proposal.Id,
"min_deposit", sdk.NewCoins(keeper.GetMinDeposit(ctx, fxgovtypes.ExtractMsgTypeURL(proposal.Messages))...).String(),
"total_deposit", sdk.NewCoins(proposal.TotalDeposit...).String(),
Expand Down

0 comments on commit de4d336

Please sign in to comment.