x/upgrade: remove alias.go usage (#6382)

* x/upgrade: remove alias.go usage

* Simplify code

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
dauTT 2020-06-10 20:54:15 +02:00 committed by GitHub
parent 79c308ae4a
commit 82afd52981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 89 deletions

View File

@ -38,6 +38,8 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/upgrade" "github.com/cosmos/cosmos-sdk/x/upgrade"
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client" upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
) )
const appName = "SimApp" const appName = "SimApp"
@ -119,7 +121,7 @@ type SimApp struct {
DistrKeeper distr.Keeper DistrKeeper distr.Keeper
GovKeeper gov.Keeper GovKeeper gov.Keeper
CrisisKeeper crisis.Keeper CrisisKeeper crisis.Keeper
UpgradeKeeper upgrade.Keeper UpgradeKeeper upgradekeeper.Keeper
ParamsKeeper params.Keeper ParamsKeeper params.Keeper
IBCKeeper *ibc.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly IBCKeeper *ibc.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
EvidenceKeeper evidence.Keeper EvidenceKeeper evidence.Keeper
@ -152,7 +154,7 @@ func NewSimApp(
keys := sdk.NewKVStoreKeys( keys := sdk.NewKVStoreKeys(
auth.StoreKey, bank.StoreKey, staking.StoreKey, auth.StoreKey, bank.StoreKey, staking.StoreKey,
mint.StoreKey, distr.StoreKey, slashing.StoreKey, mint.StoreKey, distr.StoreKey, slashing.StoreKey,
gov.StoreKey, params.StoreKey, ibc.StoreKey, upgrade.StoreKey, gov.StoreKey, params.StoreKey, ibc.StoreKey, upgradetypes.StoreKey,
evidence.StoreKey, transfer.StoreKey, capability.StoreKey, evidence.StoreKey, transfer.StoreKey, capability.StoreKey,
) )
tkeys := sdk.NewTransientStoreKeys(params.TStoreKey) tkeys := sdk.NewTransientStoreKeys(params.TStoreKey)
@ -212,14 +214,14 @@ func NewSimApp(
app.CrisisKeeper = crisis.NewKeeper( app.CrisisKeeper = crisis.NewKeeper(
app.subspaces[crisis.ModuleName], invCheckPeriod, app.BankKeeper, auth.FeeCollectorName, app.subspaces[crisis.ModuleName], invCheckPeriod, app.BankKeeper, auth.FeeCollectorName,
) )
app.UpgradeKeeper = upgrade.NewKeeper(skipUpgradeHeights, keys[upgrade.StoreKey], appCodec, homePath) app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath)
// register the proposal types // register the proposal types
govRouter := gov.NewRouter() govRouter := gov.NewRouter()
govRouter.AddRoute(gov.RouterKey, gov.ProposalHandler). govRouter.AddRoute(gov.RouterKey, gov.ProposalHandler).
AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)). AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)).
AddRoute(distr.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)). AddRoute(distr.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)).
AddRoute(upgrade.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)) AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper))
app.GovKeeper = gov.NewKeeper( app.GovKeeper = gov.NewKeeper(
appCodec, keys[gov.StoreKey], app.subspaces[gov.ModuleName], app.AccountKeeper, app.BankKeeper, appCodec, keys[gov.StoreKey], app.subspaces[gov.ModuleName], app.AccountKeeper, app.BankKeeper,
&stakingKeeper, govRouter, &stakingKeeper, govRouter,
@ -286,7 +288,7 @@ func NewSimApp(
// CanWithdrawInvariant invariant. // CanWithdrawInvariant invariant.
// NOTE: staking module is required if HistoricalEntries param > 0 // NOTE: staking module is required if HistoricalEntries param > 0
app.mm.SetOrderBeginBlockers( app.mm.SetOrderBeginBlockers(
upgrade.ModuleName, mint.ModuleName, distr.ModuleName, slashing.ModuleName, upgradetypes.ModuleName, mint.ModuleName, distr.ModuleName, slashing.ModuleName,
evidence.ModuleName, staking.ModuleName, ibc.ModuleName, evidence.ModuleName, staking.ModuleName, ibc.ModuleName,
) )
app.mm.SetOrderEndBlockers(crisis.ModuleName, gov.ModuleName, staking.ModuleName) app.mm.SetOrderEndBlockers(crisis.ModuleName, gov.ModuleName, staking.ModuleName)

View File

@ -6,6 +6,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
) )
// BeginBlock will check if there is a scheduled plan and if it is ready to be executed. // BeginBlock will check if there is a scheduled plan and if it is ready to be executed.
@ -16,7 +17,7 @@ import (
// The purpose is to ensure the binary is switched EXACTLY at the desired block, and to allow // 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) // 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 // skipUpgradeHeightArray is a set of block heights for which the upgrade must be skipped
func BeginBlocker(k Keeper, ctx sdk.Context, _ abci.RequestBeginBlock) { func BeginBlocker(k keeper.Keeper, ctx sdk.Context, _ abci.RequestBeginBlock) {
plan, found := k.GetUpgradePlan(ctx) plan, found := k.GetUpgradePlan(ctx)
if !found { if !found {
return return

View File

@ -22,11 +22,13 @@ import (
"github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/gov" "github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/upgrade" "github.com/cosmos/cosmos-sdk/x/upgrade"
"github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
) )
type TestSuite struct { type TestSuite struct {
module module.AppModule module module.AppModule
keeper upgrade.Keeper keeper keeper.Keeper
querier sdk.Querier querier sdk.Querier
handler gov.Handler handler gov.Handler
ctx sdk.Context ctx sdk.Context
@ -61,28 +63,28 @@ func setupTest(height int64, skip map[int64]bool) TestSuite {
func TestRequireName(t *testing.T) { func TestRequireName(t *testing.T) {
s := setupTest(10, map[int64]bool{}) s := setupTest(10, map[int64]bool{})
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{}})
require.NotNil(t, err) require.NotNil(t, err)
require.True(t, errors.Is(sdkerrors.ErrInvalidRequest, err), err) require.True(t, errors.Is(sdkerrors.ErrInvalidRequest, err), err)
} }
func TestRequireFutureTime(t *testing.T) { func TestRequireFutureTime(t *testing.T) {
s := setupTest(10, map[int64]bool{}) s := setupTest(10, map[int64]bool{})
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Time: s.ctx.BlockHeader().Time}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Time: s.ctx.BlockHeader().Time}})
require.NotNil(t, err) require.NotNil(t, err)
require.True(t, errors.Is(sdkerrors.ErrInvalidRequest, err), err) require.True(t, errors.Is(sdkerrors.ErrInvalidRequest, err), err)
} }
func TestRequireFutureBlock(t *testing.T) { func TestRequireFutureBlock(t *testing.T) {
s := setupTest(10, map[int64]bool{}) s := setupTest(10, map[int64]bool{})
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Height: s.ctx.BlockHeight()}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: s.ctx.BlockHeight()}})
require.NotNil(t, err) require.NotNil(t, err)
require.True(t, errors.Is(sdkerrors.ErrInvalidRequest, err), err) require.True(t, errors.Is(sdkerrors.ErrInvalidRequest, err), err)
} }
func TestCantSetBothTimeAndHeight(t *testing.T) { func TestCantSetBothTimeAndHeight(t *testing.T) {
s := setupTest(10, map[int64]bool{}) s := setupTest(10, map[int64]bool{})
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Time: time.Now(), Height: s.ctx.BlockHeight() + 1}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Time: time.Now(), Height: s.ctx.BlockHeight() + 1}})
require.NotNil(t, err) require.NotNil(t, err)
require.True(t, errors.Is(sdkerrors.ErrInvalidRequest, err), err) require.True(t, errors.Is(sdkerrors.ErrInvalidRequest, err), err)
} }
@ -90,7 +92,7 @@ func TestCantSetBothTimeAndHeight(t *testing.T) {
func TestDoTimeUpgrade(t *testing.T) { func TestDoTimeUpgrade(t *testing.T) {
s := setupTest(10, map[int64]bool{}) s := setupTest(10, map[int64]bool{})
t.Log("Verify can schedule an upgrade") t.Log("Verify can schedule an upgrade")
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Time: time.Now()}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Time: time.Now()}})
require.Nil(t, err) require.Nil(t, err)
VerifyDoUpgrade(t) VerifyDoUpgrade(t)
@ -99,7 +101,7 @@ func TestDoTimeUpgrade(t *testing.T) {
func TestDoHeightUpgrade(t *testing.T) { func TestDoHeightUpgrade(t *testing.T) {
s := setupTest(10, map[int64]bool{}) s := setupTest(10, map[int64]bool{})
t.Log("Verify can schedule an upgrade") t.Log("Verify can schedule an upgrade")
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Height: s.ctx.BlockHeight() + 1}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: s.ctx.BlockHeight() + 1}})
require.Nil(t, err) require.Nil(t, err)
VerifyDoUpgrade(t) VerifyDoUpgrade(t)
@ -108,9 +110,9 @@ func TestDoHeightUpgrade(t *testing.T) {
func TestCanOverwriteScheduleUpgrade(t *testing.T) { func TestCanOverwriteScheduleUpgrade(t *testing.T) {
s := setupTest(10, map[int64]bool{}) s := setupTest(10, map[int64]bool{})
t.Log("Can overwrite plan") t.Log("Can overwrite plan")
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "bad_test", Height: s.ctx.BlockHeight() + 10}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "bad_test", Height: s.ctx.BlockHeight() + 10}})
require.Nil(t, err) require.Nil(t, err)
err = s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Height: s.ctx.BlockHeight() + 1}}) err = s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: s.ctx.BlockHeight() + 1}})
require.Nil(t, err) require.Nil(t, err)
VerifyDoUpgrade(t) VerifyDoUpgrade(t)
@ -126,7 +128,7 @@ func VerifyDoUpgrade(t *testing.T) {
}) })
t.Log("Verify that the upgrade can be successfully applied with a handler") t.Log("Verify that the upgrade can be successfully applied with a handler")
s.keeper.SetUpgradeHandler("test", func(ctx sdk.Context, plan upgrade.Plan) {}) s.keeper.SetUpgradeHandler("test", func(ctx sdk.Context, plan types.Plan) {})
require.NotPanics(t, func() { require.NotPanics(t, func() {
s.module.BeginBlock(newCtx, req) s.module.BeginBlock(newCtx, req)
}) })
@ -142,7 +144,7 @@ func VerifyDoUpgradeWithCtx(t *testing.T, newCtx sdk.Context, proposalName strin
}) })
t.Log("Verify that the upgrade can be successfully applied with a handler") t.Log("Verify that the upgrade can be successfully applied with a handler")
s.keeper.SetUpgradeHandler(proposalName, func(ctx sdk.Context, plan upgrade.Plan) {}) s.keeper.SetUpgradeHandler(proposalName, func(ctx sdk.Context, plan types.Plan) {})
require.NotPanics(t, func() { require.NotPanics(t, func() {
s.module.BeginBlock(newCtx, req) s.module.BeginBlock(newCtx, req)
}) })
@ -154,7 +156,7 @@ func TestHaltIfTooNew(t *testing.T) {
s := setupTest(10, map[int64]bool{}) s := setupTest(10, map[int64]bool{})
t.Log("Verify that we don't panic with registered plan not in database at all") t.Log("Verify that we don't panic with registered plan not in database at all")
var called int var called int
s.keeper.SetUpgradeHandler("future", func(ctx sdk.Context, plan upgrade.Plan) { called++ }) s.keeper.SetUpgradeHandler("future", func(ctx sdk.Context, plan types.Plan) { called++ })
newCtx := s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1).WithBlockTime(time.Now()) newCtx := s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1).WithBlockTime(time.Now())
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()} req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
@ -164,7 +166,7 @@ func TestHaltIfTooNew(t *testing.T) {
require.Equal(t, 0, called) require.Equal(t, 0, called)
t.Log("Verify we panic if we have a registered handler ahead of time") t.Log("Verify we panic if we have a registered handler ahead of time")
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "future", Height: s.ctx.BlockHeight() + 3}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "future", Height: s.ctx.BlockHeight() + 3}})
require.NoError(t, err) require.NoError(t, err)
require.Panics(t, func() { require.Panics(t, func() {
s.module.BeginBlock(newCtx, req) s.module.BeginBlock(newCtx, req)
@ -185,7 +187,7 @@ func TestHaltIfTooNew(t *testing.T) {
func VerifyCleared(t *testing.T, newCtx sdk.Context) { func VerifyCleared(t *testing.T, newCtx sdk.Context) {
t.Log("Verify that the upgrade plan has been cleared") t.Log("Verify that the upgrade plan has been cleared")
bz, err := s.querier(newCtx, []string{upgrade.QueryCurrent}, abci.RequestQuery{}) bz, err := s.querier(newCtx, []string{types.QueryCurrent}, abci.RequestQuery{})
require.NoError(t, err) require.NoError(t, err)
require.Nil(t, bz) require.Nil(t, bz)
} }
@ -193,10 +195,10 @@ func VerifyCleared(t *testing.T, newCtx sdk.Context) {
func TestCanClear(t *testing.T) { func TestCanClear(t *testing.T) {
s := setupTest(10, map[int64]bool{}) s := setupTest(10, map[int64]bool{})
t.Log("Verify upgrade is scheduled") t.Log("Verify upgrade is scheduled")
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Time: time.Now()}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Time: time.Now()}})
require.Nil(t, err) require.Nil(t, err)
err = s.handler(s.ctx, &upgrade.CancelSoftwareUpgradeProposal{Title: "cancel"}) err = s.handler(s.ctx, &types.CancelSoftwareUpgradeProposal{Title: "cancel"})
require.Nil(t, err) require.Nil(t, err)
VerifyCleared(t, s.ctx) VerifyCleared(t, s.ctx)
@ -204,11 +206,11 @@ func TestCanClear(t *testing.T) {
func TestCantApplySameUpgradeTwice(t *testing.T) { func TestCantApplySameUpgradeTwice(t *testing.T) {
s := setupTest(10, map[int64]bool{}) s := setupTest(10, map[int64]bool{})
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Time: time.Now()}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Time: time.Now()}})
require.Nil(t, err) require.Nil(t, err)
VerifyDoUpgrade(t) VerifyDoUpgrade(t)
t.Log("Verify an executed upgrade \"test\" can't be rescheduled") t.Log("Verify an executed upgrade \"test\" can't be rescheduled")
err = s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Time: time.Now()}}) err = s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Time: time.Now()}})
require.NotNil(t, err) require.NotNil(t, err)
require.True(t, errors.Is(sdkerrors.ErrInvalidRequest, err), err) require.True(t, errors.Is(sdkerrors.ErrInvalidRequest, err), err)
} }
@ -228,11 +230,11 @@ func TestPlanStringer(t *testing.T) {
require.Equal(t, `Upgrade Plan require.Equal(t, `Upgrade Plan
Name: test Name: test
Time: 2020-01-01T00:00:00Z Time: 2020-01-01T00:00:00Z
Info: `, upgrade.Plan{Name: "test", Time: ti}.String()) Info: `, types.Plan{Name: "test", Time: ti}.String())
require.Equal(t, `Upgrade Plan require.Equal(t, `Upgrade Plan
Name: test Name: test
Height: 100 Height: 100
Info: `, upgrade.Plan{Name: "test", Height: 100}.String()) Info: `, types.Plan{Name: "test", Height: 100}.String())
} }
func VerifyNotDone(t *testing.T, newCtx sdk.Context, name string) { func VerifyNotDone(t *testing.T, newCtx sdk.Context, name string) {
@ -279,7 +281,7 @@ func TestSkipUpgradeSkippingAll(t *testing.T) {
newCtx := s.ctx newCtx := s.ctx
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()} req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Height: skipOne}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: skipOne}})
require.NoError(t, err) require.NoError(t, err)
t.Log("Verify if skip upgrade flag clears upgrade plan in both cases") t.Log("Verify if skip upgrade flag clears upgrade plan in both cases")
@ -291,7 +293,7 @@ func TestSkipUpgradeSkippingAll(t *testing.T) {
}) })
t.Log("Verify a second proposal also is being cleared") t.Log("Verify a second proposal also is being cleared")
err = s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop2", Plan: upgrade.Plan{Name: "test2", Height: skipTwo}}) err = s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop2", Plan: types.Plan{Name: "test2", Height: skipTwo}})
require.NoError(t, err) require.NoError(t, err)
newCtx = newCtx.WithBlockHeight(skipTwo) newCtx = newCtx.WithBlockHeight(skipTwo)
@ -316,7 +318,7 @@ func TestUpgradeSkippingOne(t *testing.T) {
newCtx := s.ctx newCtx := s.ctx
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()} req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Height: skipOne}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: skipOne}})
require.Nil(t, err) require.Nil(t, err)
t.Log("Verify if skip upgrade flag clears upgrade plan in one case and does upgrade on another") t.Log("Verify if skip upgrade flag clears upgrade plan in one case and does upgrade on another")
@ -329,7 +331,7 @@ func TestUpgradeSkippingOne(t *testing.T) {
}) })
t.Log("Verify the second proposal is not skipped") t.Log("Verify the second proposal is not skipped")
err = s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop2", Plan: upgrade.Plan{Name: "test2", Height: skipTwo}}) err = s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop2", Plan: types.Plan{Name: "test2", Height: skipTwo}})
require.Nil(t, err) require.Nil(t, err)
// Setting block height of proposal test2 // Setting block height of proposal test2
newCtx = newCtx.WithBlockHeight(skipTwo) newCtx = newCtx.WithBlockHeight(skipTwo)
@ -351,7 +353,7 @@ func TestUpgradeSkippingOnlyTwo(t *testing.T) {
newCtx := s.ctx newCtx := s.ctx
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()} req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Height: skipOne}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: skipOne}})
require.Nil(t, err) require.Nil(t, err)
t.Log("Verify if skip upgrade flag clears upgrade plan in both cases and does third upgrade") t.Log("Verify if skip upgrade flag clears upgrade plan in both cases and does third upgrade")
@ -364,7 +366,7 @@ func TestUpgradeSkippingOnlyTwo(t *testing.T) {
}) })
// A new proposal with height in skipUpgradeHeights // A new proposal with height in skipUpgradeHeights
err = s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop2", Plan: upgrade.Plan{Name: "test2", Height: skipTwo}}) err = s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop2", Plan: types.Plan{Name: "test2", Height: skipTwo}})
require.Nil(t, err) require.Nil(t, err)
// Setting block height of proposal test2 // Setting block height of proposal test2
newCtx = newCtx.WithBlockHeight(skipTwo) newCtx = newCtx.WithBlockHeight(skipTwo)
@ -373,7 +375,7 @@ func TestUpgradeSkippingOnlyTwo(t *testing.T) {
}) })
t.Log("Verify a new proposal is not skipped") t.Log("Verify a new proposal is not skipped")
err = s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop3", Plan: upgrade.Plan{Name: "test3", Height: skipThree}}) err = s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop3", Plan: types.Plan{Name: "test3", Height: skipThree}})
require.Nil(t, err) require.Nil(t, err)
newCtx = newCtx.WithBlockHeight(skipThree) newCtx = newCtx.WithBlockHeight(skipThree)
VerifyDoUpgradeWithCtx(t, newCtx, "test3") VerifyDoUpgradeWithCtx(t, newCtx, "test3")
@ -388,7 +390,7 @@ func TestUpgradeWithoutSkip(t *testing.T) {
s := setupTest(10, map[int64]bool{}) s := setupTest(10, map[int64]bool{})
newCtx := s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1).WithBlockTime(time.Now()) newCtx := s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1).WithBlockTime(time.Now())
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()} req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
err := s.handler(s.ctx, &upgrade.SoftwareUpgradeProposal{Title: "prop", Plan: upgrade.Plan{Name: "test", Height: s.ctx.BlockHeight() + 1}}) err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "prop", Plan: types.Plan{Name: "test", Height: s.ctx.BlockHeight() + 1}})
require.Nil(t, err) require.Nil(t, err)
t.Log("Verify if upgrade happens without skip upgrade") t.Log("Verify if upgrade happens without skip upgrade")
require.Panics(t, func() { require.Panics(t, func() {

View File

@ -1,40 +0,0 @@
package upgrade
import (
"github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)
const (
ModuleName = types.ModuleName
RouterKey = types.RouterKey
StoreKey = types.StoreKey
QuerierKey = types.QuerierKey
PlanByte = types.PlanByte
DoneByte = types.DoneByte
ProposalTypeSoftwareUpgrade = types.ProposalTypeSoftwareUpgrade
ProposalTypeCancelSoftwareUpgrade = types.ProposalTypeCancelSoftwareUpgrade
QueryCurrent = types.QueryCurrent
QueryApplied = types.QueryApplied
)
var (
// functions aliases
RegisterCodec = types.RegisterCodec
PlanKey = types.PlanKey
NewSoftwareUpgradeProposal = types.NewSoftwareUpgradeProposal
NewCancelSoftwareUpgradeProposal = types.NewCancelSoftwareUpgradeProposal
NewQueryAppliedParams = types.NewQueryAppliedParams
UpgradeStoreLoader = types.UpgradeStoreLoader
NewKeeper = keeper.NewKeeper
NewQuerier = keeper.NewQuerier
)
type (
UpgradeHandler = types.UpgradeHandler // nolint
Plan = types.Plan
SoftwareUpgradeProposal = types.SoftwareUpgradeProposal
CancelSoftwareUpgradeProposal = types.CancelSoftwareUpgradeProposal
QueryAppliedParams = types.QueryAppliedParams
Keeper = keeper.Keeper
)

View File

@ -4,18 +4,20 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
) )
// NewSoftwareUpgradeProposalHandler creates a governance handler to manage new proposal types. // NewSoftwareUpgradeProposalHandler creates a governance handler to manage new proposal types.
// It enables SoftwareUpgradeProposal to propose an Upgrade, and CancelSoftwareUpgradeProposal // It enables SoftwareUpgradeProposal to propose an Upgrade, and CancelSoftwareUpgradeProposal
// to abort a previously voted upgrade. // to abort a previously voted upgrade.
func NewSoftwareUpgradeProposalHandler(k Keeper) govtypes.Handler { func NewSoftwareUpgradeProposalHandler(k keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error { return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) { switch c := content.(type) {
case *SoftwareUpgradeProposal: case *types.SoftwareUpgradeProposal:
return handleSoftwareUpgradeProposal(ctx, k, c) return handleSoftwareUpgradeProposal(ctx, k, c)
case *CancelSoftwareUpgradeProposal: case *types.CancelSoftwareUpgradeProposal:
return handleCancelSoftwareUpgradeProposal(ctx, k, c) return handleCancelSoftwareUpgradeProposal(ctx, k, c)
default: default:
@ -24,11 +26,11 @@ func NewSoftwareUpgradeProposalHandler(k Keeper) govtypes.Handler {
} }
} }
func handleSoftwareUpgradeProposal(ctx sdk.Context, k Keeper, p *SoftwareUpgradeProposal) error { func handleSoftwareUpgradeProposal(ctx sdk.Context, k keeper.Keeper, p *types.SoftwareUpgradeProposal) error {
return k.ScheduleUpgrade(ctx, p.Plan) return k.ScheduleUpgrade(ctx, p.Plan)
} }
func handleCancelSoftwareUpgradeProposal(ctx sdk.Context, k Keeper, _ *CancelSoftwareUpgradeProposal) error { func handleCancelSoftwareUpgradeProposal(ctx sdk.Context, k keeper.Keeper, _ *types.CancelSoftwareUpgradeProposal) error {
k.ClearUpgradePlan(ctx) k.ClearUpgradePlan(ctx)
return nil return nil
} }

View File

@ -18,14 +18,15 @@ import (
"github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/upgrade/client/cli" "github.com/cosmos/cosmos-sdk/x/upgrade/client/cli"
"github.com/cosmos/cosmos-sdk/x/upgrade/client/rest" "github.com/cosmos/cosmos-sdk/x/upgrade/client/rest"
types "github.com/cosmos/cosmos-sdk/x/upgrade/types" "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
) )
// module codec // module codec
var moduleCdc = codec.New() var moduleCdc = codec.New()
func init() { func init() {
RegisterCodec(moduleCdc) types.RegisterCodec(moduleCdc)
} }
var ( var (
@ -39,12 +40,12 @@ type AppModuleBasic struct{}
// Name returns the ModuleName // Name returns the ModuleName
func (AppModuleBasic) Name() string { func (AppModuleBasic) Name() string {
return ModuleName return types.ModuleName
} }
// RegisterCodec registers the upgrade types on the amino codec // RegisterCodec registers the upgrade types on the amino codec
func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
RegisterCodec(cdc) types.RegisterCodec(cdc)
} }
// RegisterRESTRoutes registers all REST query handlers // RegisterRESTRoutes registers all REST query handlers
@ -59,8 +60,8 @@ func (AppModuleBasic) GetQueryCmd(clientCtx client.Context) *cobra.Command {
Short: "Querying commands for the upgrade module", Short: "Querying commands for the upgrade module",
} }
queryCmd.AddCommand(flags.GetCommands( queryCmd.AddCommand(flags.GetCommands(
cli.GetPlanCmd(StoreKey, clientCtx.Codec), cli.GetPlanCmd(types.StoreKey, clientCtx.Codec),
cli.GetAppliedHeightCmd(StoreKey, clientCtx.Codec), cli.GetAppliedHeightCmd(types.StoreKey, clientCtx.Codec),
)...) )...)
return queryCmd return queryCmd
@ -83,11 +84,11 @@ func (b AppModuleBasic) RegisterInterfaceTypes(registry codectypes.InterfaceRegi
// AppModule implements the sdk.AppModule interface // AppModule implements the sdk.AppModule interface
type AppModule struct { type AppModule struct {
AppModuleBasic AppModuleBasic
keeper Keeper keeper keeper.Keeper
} }
// NewAppModule creates a new AppModule object // NewAppModule creates a new AppModule object
func NewAppModule(keeper Keeper) AppModule { func NewAppModule(keeper keeper.Keeper) AppModule {
return AppModule{ return AppModule{
AppModuleBasic: AppModuleBasic{}, AppModuleBasic: AppModuleBasic{},
keeper: keeper, keeper: keeper,
@ -101,11 +102,11 @@ func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
func (AppModule) Route() *sdk.Route { return nil } func (AppModule) Route() *sdk.Route { return nil }
// QuerierRoute returns the route we respond to for abci queries // QuerierRoute returns the route we respond to for abci queries
func (AppModule) QuerierRoute() string { return QuerierKey } func (AppModule) QuerierRoute() string { return types.QuerierKey }
// NewQuerierHandler registers a query handler to respond to the module-specific queries // NewQuerierHandler registers a query handler to respond to the module-specific queries
func (am AppModule) NewQuerierHandler() sdk.Querier { func (am AppModule) NewQuerierHandler() sdk.Querier {
return NewQuerier(am.keeper) return keeper.NewQuerier(am.keeper)
} }
func (am AppModule) RegisterQueryService(grpc.Server) {} func (am AppModule) RegisterQueryService(grpc.Server) {}