Skip to content

Commit 3bef582

Browse files
aleem1314mergify-bot
authored and
mergify-bot
committed
feat!: add protection against accidental downgrades (#10407)
## Description Closes: #10318 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit 5622115) # Conflicts: # CHANGELOG.md # x/upgrade/keeper/keeper.go
1 parent 7ecf4d4 commit 3bef582

File tree

5 files changed

+269
-0
lines changed

5 files changed

+269
-0
lines changed

CHANGELOG.md

+116
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,122 @@ Ref: https://keepachangelog.com/en/1.0.0/
3737

3838
## [Unreleased]
3939

40+
<<<<<<< HEAD
41+
=======
42+
### Features
43+
44+
* [\#10710](https://github.com/cosmos/cosmos-sdk/pull/10710) Chain-id shouldn't be required for creating a transaction with both --generate-only and --offline flags.
45+
* [\#10703](https://github.com/cosmos/cosmos-sdk/pull/10703) Create a new grantee account, if the grantee of an authorization does not exist.
46+
* [\#10592](https://github.com/cosmos/cosmos-sdk/pull/10592) Add a `DecApproxEq` function that checks to see if `|d1 - d2| < tol` for some Dec `d1, d2, tol`.
47+
* [\#9933](https://github.com/cosmos/cosmos-sdk/pull/9933) Introduces the notion of a Cosmos "Scalar" type, which would just be simple aliases that give human-understandable meaning to the underlying type, both in Go code and in Proto definitions.
48+
* [\#9884](https://github.com/cosmos/cosmos-sdk/pull/9884) Provide a new gRPC query handler, `/cosmos/params/v1beta1/subspaces`, that allows the ability to query for all registered subspaces and their respective keys.
49+
* [\#9776](https://github.com/cosmos/cosmos-sdk/pull/9776) Add flag `staking-bond-denom` to specify the staking bond denomination value when initializing a new chain.
50+
* [\#9533](https://github.com/cosmos/cosmos-sdk/pull/9533) Added a new gRPC method, `DenomOwners`, in `x/bank` to query for all account holders of a specific denomination.
51+
* (bank) [\#9618](https://github.com/cosmos/cosmos-sdk/pull/9618) Update bank.Metadata: add URI and URIHash attributes.
52+
* (store) [\#8664](https://github.com/cosmos/cosmos-sdk/pull/8664) Implementation of ADR-038 file StreamingService
53+
* [\#9837](https://github.com/cosmos/cosmos-sdk/issues/9837) `--generate-only` flag will accept the keyname now.
54+
* [\#10326](https://github.com/cosmos/cosmos-sdk/pull/10326) `x/authz` add query all grants by granter query.
55+
* [\#10348](https://github.com/cosmos/cosmos-sdk/pull/10348) Add `fee.{payer,granter}` and `tip` fields to StdSignDoc for signing tipped transactions.
56+
* [\#10208](https://github.com/cosmos/cosmos-sdk/pull/10208) Add `TipsTxMiddleware` for transferring tips.
57+
* [\#10379](https://github.com/cosmos/cosmos-sdk/pull/10379) Add validation to `x/upgrade` CLI `software-upgrade` command `--plan-info` value.
58+
* [\#10507](https://github.com/cosmos/cosmos-sdk/pull/10507) Add middleware for tx priority.
59+
* [\#10311](https://github.com/cosmos/cosmos-sdk/pull/10311) Adds cli to use tips transactions. It adds an `--aux` flag to all CLI tx commands to generate the aux signer data (with optional tip), and a new `tx aux-to-fee` subcommand to let the fee payer gather aux signer data and broadcast the tx
60+
* [\#10430](https://github.com/cosmos/cosmos-sdk/pull/10430) ADR-040: Add store/v2 `MultiStore` implementation
61+
* [\#10947](https://github.com/cosmos/cosmos-sdk/pull/10947) Add `AllowancesByGranter` query to the feegrant module
62+
* [\#10407](https://github.com/cosmos/cosmos-sdk/pull/10407) Add validation to `x/upgrade` module's `BeginBlock` to check accidental binary downgrades
63+
64+
### API Breaking Changes
65+
66+
* [\#10950](https://github.com/cosmos/cosmos-sdk/pull/10950) Add `envPrefix` parameter to `cmd.Execute`.
67+
* (x/mint) [\#10441](https://github.com/cosmos/cosmos-sdk/pull/10441) The `NewAppModule` function now accepts an inflation calculation function as an argument.
68+
* [\#10295](https://github.com/cosmos/cosmos-sdk/pull/10295) Remove store type aliases from /types
69+
* [\#9695](https://github.com/cosmos/cosmos-sdk/pull/9695) Migrate keys from `Info` -> `Record`
70+
* Add new `codec.Codec` argument in:
71+
* `keyring.NewInMemory`
72+
* `keyring.New`
73+
* Rename:
74+
* `SavePubKey` to `SaveOfflineKey`.
75+
* `NewMultiInfo`, `NewLedgerInfo` to `NewLegacyMultiInfo`, `newLegacyLedgerInfo` respectively. Move them into `legacy_info.go`.
76+
* `NewOfflineInfo` to `newLegacyOfflineInfo` and move it to `migration_test.go`.
77+
* Return:
78+
*`keyring.Record, error` in `SaveOfflineKey`, `SaveLedgerKey`, `SaveMultiSig`, `Key` and `KeyByAddress`.
79+
*`keyring.Record` instead of `Info` in `NewMnemonic` and `List`.
80+
* Remove `algo` argument from :
81+
* `SaveOfflineKey`
82+
* Take `keyring.Record` instead of `Info` as first argument in:
83+
* `MkConsKeyOutput`
84+
* `MkValKeyOutput`
85+
* `MkAccKeyOutput`
86+
* [\#10022](https://github.com/cosmos/cosmos-sdk/pull/10022) `AuthKeeper` interface in `x/auth` now includes a function `HasAccount`.
87+
* [\#9759](https://github.com/cosmos/cosmos-sdk/pull/9759) `NewAccountKeeeper` in `x/auth` now takes an additional `bech32Prefix` argument that represents `sdk.Bech32MainPrefix`.
88+
* [\#9628](https://github.com/cosmos/cosmos-sdk/pull/9628) Rename `x/{mod}/legacy` to `x/{mod}/migrations`.
89+
* [\#9571](https://github.com/cosmos/cosmos-sdk/pull/9571) Implemented error handling for staking hooks, which now return an error on failure.
90+
* [\#9427](https://github.com/cosmos/cosmos-sdk/pull/9427) Move simapp `FundAccount` and `FundModuleAccount` to `x/bank/testutil`
91+
* (client/tx) [\#9421](https://github.com/cosmos/cosmos-sdk/pull/9421/) `BuildUnsignedTx`, `BuildSimTx`, `PrintUnsignedStdTx` functions are moved to
92+
the Tx Factory as methods.
93+
* (client/keys) [\#9407](https://github.com/cosmos/cosmos-sdk/pull/9601) Added `keys rename` CLI command and `Keyring.Rename` interface method to rename a key in the keyring.
94+
* (x/slashing) [\#9458](https://github.com/cosmos/cosmos-sdk/pull/9458) Coins burned from slashing is now returned from Slash function and included in Slash event.
95+
* [\#9246](https://github.com/cosmos/cosmos-sdk/pull/9246) The `New` method for the network package now returns an error.
96+
* [\#9519](https://github.com/cosmos/cosmos-sdk/pull/9519) `DeleteDeposits` renamed to `DeleteAndBurnDeposits`, `RefundDeposits` renamed to `RefundAndDeleteDeposits`
97+
* (codec) [\#9521](https://github.com/cosmos/cosmos-sdk/pull/9521) Removed deprecated `clientCtx.JSONCodec` from `client.Context`.
98+
* (codec) [\#9521](https://github.com/cosmos/cosmos-sdk/pull/9521) Rename `EncodingConfig.Marshaler` to `Codec`.
99+
* [\#9594](https://github.com/cosmos/cosmos-sdk/pull/9594) `RESTHandlerFn` argument is removed from the `gov/NewProposalHandler`.
100+
* [\#9594](https://github.com/cosmos/cosmos-sdk/pull/9594) `types/rest` package moved to `testutil/rest`.
101+
* [\#9432](https://github.com/cosmos/cosmos-sdk/pull/9432) `ConsensusParamsKeyTable` moved from `params/keeper` to `params/types`
102+
* [\#9576](https://github.com/cosmos/cosmos-sdk/pull/9576) Add debug error message to `sdkerrors.QueryResult` when enabled
103+
* [\#9650](https://github.com/cosmos/cosmos-sdk/pull/9650) Removed deprecated message handler implementation from the SDK modules.
104+
* [\#10248](https://github.com/cosmos/cosmos-sdk/pull/10248) Remove unused `KeyPowerReduction` variable from x/staking types.
105+
* (x/bank) [\#9832] (https://github.com/cosmos/cosmos-sdk/pull/9832) `AddressFromBalancesStore` renamed to `AddressAndDenomFromBalancesStore`.
106+
* (tests) [\#9938](https://github.com/cosmos/cosmos-sdk/pull/9938) `simapp.Setup` accepts additional `testing.T` argument.
107+
* (baseapp) [\#9920](https://github.com/cosmos/cosmos-sdk/pull/9920) BaseApp `{Check,Deliver,Simulate}Tx` methods are now replaced by a middleware stack.
108+
* Replace the Antehandler interface with the `tx.Handler` and `tx.Middleware` interfaces.
109+
* Replace `baseapp.SetAnteHandler` with `baseapp.SetTxHandler`.
110+
* Move Msg routers from BaseApp to middlewares.
111+
* Move Baseapp panic recovery into a middleware.
112+
* Rename simulation helper methods `baseapp.{Check,Deliver}` to `baseapp.Sim{Check,Deliver}**`.
113+
* (x/gov) [\#10373](https://github.com/cosmos/cosmos-sdk/pull/10373) Removed gov `keeper.{MustMarshal, MustUnmarshal}`.
114+
* [\#10348](https://github.com/cosmos/cosmos-sdk/pull/10348) StdSignBytes takes a new argument of type `*tx.Tip` for signing over tips using LEGACY_AMINO_JSON.
115+
* [\#10208](https://github.com/cosmos/cosmos-sdk/pull/10208) The `x/auth/signing.Tx` interface now also includes a new `GetTip() *tx.Tip` method for verifying tipped transactions. The `x/auth/types` expected BankKeeper interface now expects the `SendCoins` method too.
116+
* [\#10612](https://github.com/cosmos/cosmos-sdk/pull/10612) `baseapp.NewBaseApp` constructor function doesn't take the `sdk.TxDecoder` anymore. This logic has been moved into the TxDecoderMiddleware.
117+
* [\#10692](https://github.com/cosmos/cosmos-sdk/pull/10612) `SignerData` takes 2 new fields, `Address` and `PubKey`, which need to get populated when using SIGN_MODE_DIRECT_AUX.
118+
* [\#10748](https://github.com/cosmos/cosmos-sdk/pull/10748) Move legacy `x/gov` api to `v1beta1` directory.
119+
* [\#10816](https://github.com/cosmos/cosmos-sdk/pull/10816) Reuse blocked addresses from the bank module. No need to pass them to distribution.
120+
* [\#10852](https://github.com/cosmos/cosmos-sdk/pull/10852) Move `x/gov/types` to `x/gov/types/v1beta2`.
121+
* [\#10868](https://github.com/cosmos/cosmos-sdk/pull/10868), [\#10989](https://github.com/cosmos/cosmos-sdk/pull/10989) The Gov keeper accepts now 2 more mandatory arguments, the ServiceMsgRouter and a maximum proposal metadata length.
122+
123+
### Client Breaking Changes
124+
125+
* [\#9594](https://github.com/cosmos/cosmos-sdk/pull/9594) Remove legacy REST API. Please see the [REST Endpoints Migration guide](https://docs.cosmos.network/master/migrations/rest.html) to migrate to the new REST endpoints.
126+
* [\#9995](https://github.com/cosmos/cosmos-sdk/pull/9995) Increased gas cost for creating proposals.
127+
128+
### CLI Breaking Changes
129+
130+
* [\#9695](https://github.com/cosmos/cosmos-sdk/pull/9695) `<app> keys migrate` CLI command now takes no arguments
131+
* [\#9246](https://github.com/cosmos/cosmos-sdk/pull/9246) Removed the CLI flag `--setup-config-only` from the `testnet` command and added the subcommand `init-files`.
132+
* [\#9780](https://github.com/cosmos/cosmos-sdk/pull/9780) Use sigs.k8s.io for yaml, which might lead to minor YAML output changes
133+
* [\#10625](https://github.com/cosmos/cosmos-sdk/pull/10625) Rename `--fee-account` CLI flag to `--fee-granter`
134+
* [\#10684](https://github.com/cosmos/cosmos-sdk/pull/10684) Rename `edit-validator` command's `--moniker` flag to `--new-moniker`
135+
136+
### Improvements
137+
138+
* [\#10439](https://github.com/cosmos/cosmos-sdk/pull/10439) Check error for `RegisterQueryHandlerClient` in all modules `RegisterGRPCGatewayRoutes`.
139+
* [\#9780](https://github.com/cosmos/cosmos-sdk/pull/9780) Remove gogoproto `moretags` YAML annotations and add `sigs.k8s.io/yaml` for YAML marshalling.
140+
* (x/bank) [\#10134](https://github.com/cosmos/cosmos-sdk/pull/10134) Add `HasDenomMetadata` function to bank `Keeper` to check if a client coin denom metadata exists in state.
141+
* (x/bank) [\#10022](https://github.com/cosmos/cosmos-sdk/pull/10022) `BankKeeper.SendCoins` now takes less execution time.
142+
* (deps) [\#9987](https://github.com/cosmos/cosmos-sdk/pull/9987) Bump Go version minimum requirement to `1.17`
143+
* (cli) [\#9856](https://github.com/cosmos/cosmos-sdk/pull/9856) Overwrite `--sequence` and `--account-number` flags with default flag values when used with `offline=false` in `sign-batch` command.
144+
* (rosetta) [\#10001](https://github.com/cosmos/cosmos-sdk/issues/10001) Add documentation for rosetta-cli dockerfile and rename folder for the rosetta-ci dockerfile
145+
* [\#9699](https://github.com/cosmos/cosmos-sdk/pull/9699) Add `:`, `.`, `-`, and `_` as allowed characters in the default denom regular expression.
146+
* (genesis) [\#9697](https://github.com/cosmos/cosmos-sdk/pull/9697) Ensure `InitGenesis` returns with non-empty validator set.
147+
* [\#10341](https://github.com/cosmos/cosmos-sdk/pull/10341) Move from `io/ioutil` to `io` and `os` packages.
148+
* [\#10468](https://github.com/cosmos/cosmos-sdk/pull/10468) Allow futureOps to queue additional operations in simulations
149+
* [\#10625](https://github.com/cosmos/cosmos-sdk/pull/10625) Add `--fee-payer` CLI flag
150+
* (cli) [\#10683](https://github.com/cosmos/cosmos-sdk/pull/10683) In CLI, allow 1 SIGN_MODE_DIRECT signer in transactions with multiple signers.
151+
* (deps) [\#10210](https://github.com/cosmos/cosmos-sdk/pull/10210) Bump Tendermint to [v0.35.0](https://github.com/tendermint/tendermint/releases/tag/v0.35.0).
152+
* (deps) [\#10706](https://github.com/cosmos/cosmos-sdk/issues/10706) Bump rosetta-sdk-go to v0.7.2 and rosetta-cli to v0.7.3
153+
* (types/errors) [\#10779](https://github.com/cosmos/cosmos-sdk/pull/10779) Move most functionality in `types/errors` to a standalone `errors` go module, except the `RootCodespace` errors and ABCI response helpers. All functions and types that used to live in `types/errors` are now aliased so this is not a breaking change.
154+
155+
>>>>>>> 562211586 (feat!: add protection against accidental downgrades (#10407))
40156
### Bug Fixes
41157

42158
* (grpc) [\#10985](https://github.com/cosmos/cosmos-sdk/pull/10992) The `/cosmos/tx/v1beta1/txs/{hash}` endpoint returns a 404 when a tx does not exist.

x/upgrade/abci.go

+17
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@ import (
2222
// skipUpgradeHeightArray is a set of block heights for which the upgrade must be skipped
2323
func BeginBlocker(k keeper.Keeper, ctx sdk.Context, _ abci.RequestBeginBlock) {
2424
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
25+
2526
plan, found := k.GetUpgradePlan(ctx)
27+
28+
if !k.DowngradeVerified() {
29+
k.SetDowngradeVerified(true)
30+
lastAppliedPlan, _ := k.GetLastCompletedUpgrade(ctx)
31+
// This check will make sure that we are using a valid binary.
32+
// It'll panic in these cases if there is no upgrade handler registered for the last applied upgrade.
33+
// 1. If there is no scheduled upgrade.
34+
// 2. If the plan is not ready.
35+
// 3. If the plan is ready and skip upgrade height is set for current height.
36+
if !found || !plan.ShouldExecute(ctx) || (plan.ShouldExecute(ctx) && k.IsSkipHeight(ctx.BlockHeight())) {
37+
if lastAppliedPlan != "" && !k.HasHandler(lastAppliedPlan) {
38+
panic(fmt.Sprintf("Wrong app version %d, upgrade handler is missing for %s upgrade plan", ctx.ConsensusParams().Version.AppVersion, lastAppliedPlan))
39+
}
40+
}
41+
}
42+
2643
if !found {
2744
return
2845
}

x/upgrade/abci_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,70 @@ func TestDumpUpgradeInfoToFile(t *testing.T) {
411411
err = os.Remove(upgradeInfoFilePath)
412412
require.Nil(t, err)
413413
}
414+
415+
// TODO: add testcase to for `no upgrade handler is present for last applied upgrade`.
416+
func TestBinaryVersion(t *testing.T) {
417+
var skipHeight int64 = 15
418+
s := setupTest(t, 10, map[int64]bool{skipHeight: true})
419+
420+
testCases := []struct {
421+
name string
422+
preRun func() (sdk.Context, abci.RequestBeginBlock)
423+
expectPanic bool
424+
}{
425+
{
426+
"test not panic: no scheduled upgrade or applied upgrade is present",
427+
func() (sdk.Context, abci.RequestBeginBlock) {
428+
req := abci.RequestBeginBlock{Header: s.ctx.BlockHeader()}
429+
return s.ctx, req
430+
},
431+
false,
432+
},
433+
{
434+
"test not panic: upgrade handler is present for last applied upgrade",
435+
func() (sdk.Context, abci.RequestBeginBlock) {
436+
s.keeper.SetUpgradeHandler("test0", func(_ sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
437+
return vm, nil
438+
})
439+
440+
err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "Upgrade test", Plan: types.Plan{Name: "test0", Height: s.ctx.BlockHeight() + 2}})
441+
require.Nil(t, err)
442+
443+
newCtx := s.ctx.WithBlockHeight(12)
444+
s.keeper.ApplyUpgrade(newCtx, types.Plan{
445+
Name: "test0",
446+
Height: 12,
447+
})
448+
449+
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
450+
return newCtx, req
451+
},
452+
false,
453+
},
454+
{
455+
"test panic: upgrade needed",
456+
func() (sdk.Context, abci.RequestBeginBlock) {
457+
err := s.handler(s.ctx, &types.SoftwareUpgradeProposal{Title: "Upgrade test", Plan: types.Plan{Name: "test2", Height: 13}})
458+
require.Nil(t, err)
459+
460+
newCtx := s.ctx.WithBlockHeight(13)
461+
req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()}
462+
return newCtx, req
463+
},
464+
true,
465+
},
466+
}
467+
468+
for _, tc := range testCases {
469+
ctx, req := tc.preRun()
470+
if tc.expectPanic {
471+
require.Panics(t, func() {
472+
s.module.BeginBlock(ctx, req)
473+
})
474+
} else {
475+
require.NotPanics(t, func() {
476+
s.module.BeginBlock(ctx, req)
477+
})
478+
}
479+
}
480+
}

x/upgrade/keeper/keeper.go

+30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
store "github.com/cosmos/cosmos-sdk/store/types"
1818
sdk "github.com/cosmos/cosmos-sdk/types"
1919
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
20+
"github.com/cosmos/cosmos-sdk/types/kv"
2021
"github.com/cosmos/cosmos-sdk/types/module"
2122
xp "github.com/cosmos/cosmos-sdk/x/upgrade/exported"
2223
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
@@ -32,6 +33,7 @@ type Keeper struct {
3233
cdc codec.BinaryCodec // App-wide binary codec
3334
upgradeHandlers map[string]types.UpgradeHandler // map of plan name to upgrade handler
3435
versionSetter xp.ProtocolVersionSetter // implements setting the protocol version field on BaseApp
36+
downgradeVerified bool // tells if we've already sanity checked that this binary version isn't being used against an old state.
3537
}
3638

3739
// NewKeeper constructs an upgrade Keeper which requires the following arguments:
@@ -228,6 +230,23 @@ func (k Keeper) GetUpgradedConsensusState(ctx sdk.Context, lastHeight int64) ([]
228230
return bz, true
229231
}
230232

233+
// GetLastCompletedUpgrade returns the last applied upgrade name and height.
234+
func (k Keeper) GetLastCompletedUpgrade(ctx sdk.Context) (string, int64) {
235+
iter := sdk.KVStoreReversePrefixIterator(ctx.KVStore(k.storeKey), []byte{types.DoneByte})
236+
defer iter.Close()
237+
if iter.Valid() {
238+
return parseDoneKey(iter.Key()), int64(binary.BigEndian.Uint64(iter.Value()))
239+
}
240+
241+
return "", 0
242+
}
243+
244+
// parseDoneKey - split upgrade name from the done key
245+
func parseDoneKey(key []byte) string {
246+
kv.AssertKeyAtLeastLength(key, 2)
247+
return string(key[1:])
248+
}
249+
231250
// GetDoneHeight returns the height at which the given upgrade was executed
232251
func (k Keeper) GetDoneHeight(ctx sdk.Context, name string) int64 {
233252
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{types.DoneByte})
@@ -401,6 +420,7 @@ func (k Keeper) ReadUpgradeInfoFromDisk() (store.UpgradeInfo, error) {
401420
return upgradeInfo, nil
402421
}
403422

423+
<<<<<<< HEAD
404424
// upgradeInfo is stripped types.Plan structure used to dump upgrade plan data.
405425
type upgradeInfo struct {
406426
// Name has types.Plan.Name value
@@ -409,4 +429,14 @@ type upgradeInfo struct {
409429
Height int64 `json:"height,omitempty"`
410430
// Height has types.Plan.Height value
411431
Info string `json:"info,omitempty"`
432+
=======
433+
// SetDowngradeVerified updates downgradeVerified.
434+
func (k *Keeper) SetDowngradeVerified(v bool) {
435+
k.downgradeVerified = v
436+
}
437+
438+
// DowngradeVerified returns downgradeVerified.
439+
func (k Keeper) DowngradeVerified() bool {
440+
return k.downgradeVerified
441+
>>>>>>> 562211586 (feat!: add protection against accidental downgrades (#10407))
412442
}

x/upgrade/keeper/keeper_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,45 @@ func (s *KeeperTestSuite) TestMigrations() {
232232
s.Require().Equal(vmBefore["bank"]+1, vm["bank"])
233233
}
234234

235+
func (s *KeeperTestSuite) TestLastCompletedUpgrade() {
236+
keeper := s.app.UpgradeKeeper
237+
require := s.Require()
238+
239+
s.T().Log("verify empty name if applied upgrades are empty")
240+
name, height := keeper.GetLastCompletedUpgrade(s.ctx)
241+
require.Equal("", name)
242+
require.Equal(int64(0), height)
243+
244+
keeper.SetUpgradeHandler("test0", func(_ sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
245+
return vm, nil
246+
})
247+
248+
keeper.ApplyUpgrade(s.ctx, types.Plan{
249+
Name: "test0",
250+
Height: 10,
251+
})
252+
253+
s.T().Log("verify valid upgrade name and height")
254+
name, height = keeper.GetLastCompletedUpgrade(s.ctx)
255+
require.Equal("test0", name)
256+
require.Equal(int64(10), height)
257+
258+
keeper.SetUpgradeHandler("test1", func(_ sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
259+
return vm, nil
260+
})
261+
262+
newCtx := s.ctx.WithBlockHeight(15)
263+
keeper.ApplyUpgrade(newCtx, types.Plan{
264+
Name: "test1",
265+
Height: 15,
266+
})
267+
268+
s.T().Log("verify valid upgrade name and height with multiple upgrades")
269+
name, height = keeper.GetLastCompletedUpgrade(newCtx)
270+
require.Equal("test1", name)
271+
require.Equal(int64(15), height)
272+
}
273+
235274
func TestKeeperTestSuite(t *testing.T) {
236275
suite.Run(t, new(KeeperTestSuite))
237276
}

0 commit comments

Comments
 (0)