Skip to content

Commit

Permalink
feat(runtime/v2): store loader on simappv2 (backport #21704) (#21717)
Browse files Browse the repository at this point in the history
Co-authored-by: Randy Grok <98407738+randygrok@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
3 people authored Sep 13, 2024
1 parent 51b418a commit 71803b7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
### Improvements

* (genutil) [#21701](https://github.com/cosmos/cosmos-sdk/pull/21701) Improved error messages for genesis validation.
* (runtime) [#21704](https://github.com/cosmos/cosmos-sdk/pull/21704) Move `upgradetypes.StoreLoader` to runtime and alias it in upgrade for backward compatibility.

## [v0.52.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.52.0) - 2024-XX-XX

Expand Down
21 changes: 21 additions & 0 deletions runtime/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"cosmossdk.io/core/store"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -181,3 +182,23 @@ func (s kvStoreAdapter) ReverseIterator(start, end []byte) dbm.Iterator {
func KVStoreAdapter(store store.KVStore) storetypes.KVStore {
return &kvStoreAdapter{store}
}

// UpgradeStoreLoader is used to prepare baseapp with a fixed StoreLoader
// pattern. This is useful for custom upgrade loading logic.
func UpgradeStoreLoader(upgradeHeight int64, storeUpgrades *store.StoreUpgrades) baseapp.StoreLoader {
return func(ms storetypes.CommitMultiStore) error {
if upgradeHeight == ms.LastCommitID().Version+1 {
// Check if the current commit version and upgrade height matches
if len(storeUpgrades.Deleted) > 0 || len(storeUpgrades.Added) > 0 {
stup := &storetypes.StoreUpgrades{
Added: storeUpgrades.Added,
Deleted: storeUpgrades.Deleted,
}
return ms.LoadLatestVersionAndUpgrade(stup)
}
}

// Otherwise load default store loader
return baseapp.DefaultStoreLoader(ms)
}
}
4 changes: 2 additions & 2 deletions simapp/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
cosmossdk.io/depinject v1.0.0
cosmossdk.io/log v1.4.1
cosmossdk.io/math v1.3.0
cosmossdk.io/runtime/v2 v2.0.0-20240913102804-9fc6675885f9 // main
cosmossdk.io/runtime/v2 v2.0.0-20240913142214-7d6ff0df5642 // main
cosmossdk.io/server/v2 v2.0.0-20240912070812-0fc06f14104b // main
cosmossdk.io/server/v2/cometbft v0.0.0-00010101000000-000000000000
cosmossdk.io/store/v2 v2.0.0-20240906090851-36d9b25e8981 // main
Expand Down Expand Up @@ -227,7 +227,7 @@ require (
google.golang.org/genproto v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.66.1 // indirect
google.golang.org/grpc v1.66.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions simapp/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ cosmossdk.io/log v1.4.1 h1:wKdjfDRbDyZRuWa8M+9nuvpVYxrEOwbD/CA8hvhU8QM=
cosmossdk.io/log v1.4.1/go.mod h1:k08v0Pyq+gCP6phvdI6RCGhLf/r425UT6Rk/m+o74rU=
cosmossdk.io/math v1.3.0 h1:RC+jryuKeytIiictDslBP9i1fhkVm6ZDmZEoNP316zE=
cosmossdk.io/math v1.3.0/go.mod h1:vnRTxewy+M7BtXBNFybkuhSH4WfedVAAnERHgVFhp3k=
cosmossdk.io/runtime/v2 v2.0.0-20240913102804-9fc6675885f9 h1:xAPMtcHd8bW8F+Nt/lRmoe3gAD/zghEkzHdXb/5hkwY=
cosmossdk.io/runtime/v2 v2.0.0-20240913102804-9fc6675885f9/go.mod h1:bIUuDyiXe4zAa/QbOhciGdtTsmyytU8kt1FjLVtcZ1Q=
cosmossdk.io/runtime/v2 v2.0.0-20240913142214-7d6ff0df5642 h1:04qILoFdYGZX8knOWFNSvs/Pineca+uzErliyqx0t3s=
cosmossdk.io/runtime/v2 v2.0.0-20240913142214-7d6ff0df5642/go.mod h1:d0B92058w/s/Yegf0ySClPPJn+Vp7Cz+H3ORggSSGm4=
cosmossdk.io/schema v0.2.0 h1:UH5CR1DqUq8yP+5Np8PbvG4YX0zAUsTN2Qk6yThmfMk=
cosmossdk.io/schema v0.2.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
cosmossdk.io/server/v2 v2.0.0-20240912070812-0fc06f14104b h1:rG8VQF/3yAEJOQewDgrcBWwr1xKzu+VwhrILW5hui/Y=
Expand Down Expand Up @@ -1399,8 +1399,8 @@ google.golang.org/grpc v1.48.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACu
google.golang.org/grpc v1.49.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/grpc v1.50.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/grpc v1.66.1 h1:hO5qAXR19+/Z44hmvIM4dQFMSYX9XcWsByfoxutBpAM=
google.golang.org/grpc v1.66.1/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y=
google.golang.org/grpc v1.66.2 h1:3QdXkuq3Bkh7w+ywLdLvM56cmGvQHUMZpiCzt6Rqaoo=
google.golang.org/grpc v1.66.2/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
Expand Down
5 changes: 2 additions & 3 deletions simapp/v2/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/store"
"cosmossdk.io/runtime/v2"
"cosmossdk.io/x/accounts"
protocolpooltypes "cosmossdk.io/x/protocolpool/types"
upgradetypes "cosmossdk.io/x/upgrade/types"
Expand Down Expand Up @@ -40,8 +41,6 @@ func (app *SimApp[T]) RegisterUpgradeHandlers() {
Deleted: []string{"crisis"}, // The SDK discontinued the crisis module in v0.52.0
}

// configure store loader that checks if version == upgradeHeight and applies store upgrades
_ = storeUpgrades
// app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
app.SetStoreLoader(runtime.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
}
}
3 changes: 3 additions & 0 deletions x/upgrade/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ expected upgrade. It eliminiates the chances for the new binary to execute `Stor
times every time on restart. Also if there are multiple upgrades planned on same height, the `Name`
will ensure these `StoreUpgrades` takes place only in planned upgrade handler.

**Note:** The `StoreLoader` helper function for StoreUpgrades in v2 is not part of the `x/upgrade` module;
instead, you can find it in the runtime v2 module.

### Proposal

Typically, a `Plan` is proposed and submitted through governance via a proposal
Expand Down
28 changes: 3 additions & 25 deletions x/upgrade/types/storeloader.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
package types

import (
corestore "cosmossdk.io/core/store"
storetypes "cosmossdk.io/store/types"
import "github.com/cosmos/cosmos-sdk/runtime"

"github.com/cosmos/cosmos-sdk/baseapp"
)

// UpgradeStoreLoader is used to prepare baseapp with a fixed StoreLoader
// pattern. This is useful for custom upgrade loading logic.
func UpgradeStoreLoader(upgradeHeight int64, storeUpgrades *corestore.StoreUpgrades) baseapp.StoreLoader {
return func(ms storetypes.CommitMultiStore) error {
if upgradeHeight == ms.LastCommitID().Version+1 {
// Check if the current commit version and upgrade height matches
if len(storeUpgrades.Deleted) > 0 || len(storeUpgrades.Added) > 0 {
stup := &storetypes.StoreUpgrades{
Added: storeUpgrades.Added,
Deleted: storeUpgrades.Deleted,
}
return ms.LoadLatestVersionAndUpgrade(stup)
}
}

// Otherwise load default store loader
return baseapp.DefaultStoreLoader(ms)
}
}
// UpgradeStoreLoader moved to runtime package, keeping this for backwards compatibility
var UpgradeStoreLoader = runtime.UpgradeStoreLoader

0 comments on commit 71803b7

Please sign in to comment.