Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime(v2)): add sanity checks on store upgrades #21748

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions runtime/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package runtime

import (
"context"
"errors"
"fmt"
"io"

"cosmossdk.io/core/store"
Expand Down Expand Up @@ -184,6 +186,11 @@ func KVStoreAdapter(store store.KVStore) storetypes.KVStore {
// 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 {
// sanity checks on store upgrades
if err := checkStoreUpgrade(storeUpgrades); err != nil {
panic(err)
}

return func(ms storetypes.CommitMultiStore) error {
if upgradeHeight == ms.LastCommitID().Version+1 {
// Check if the current commit version and upgrade height matches
Expand All @@ -200,3 +207,38 @@ func UpgradeStoreLoader(upgradeHeight int64, storeUpgrades *store.StoreUpgrades)
return baseapp.DefaultStoreLoader(ms)
}
}

// checkStoreUpgrade performs sanity checks on the store upgrades
func checkStoreUpgrade(storeUpgrades *store.StoreUpgrades) error {
if storeUpgrades == nil {
return errors.New("store upgrades cannot be nil")
}

// check for duplicates
var exists = make(map[string]bool)
for _, key := range storeUpgrades.Added {
if exists[key] {
return fmt.Errorf("store upgrade has duplicate key %s in added", key)
}

if storeUpgrades.IsDeleted(key) {
return fmt.Errorf("store upgrade has key %s in both added and deleted", key)
}

exists[key] = true
}
exists = make(map[string]bool)
for _, key := range storeUpgrades.Deleted {
if exists[key] {
return fmt.Errorf("store upgrade has duplicate key %s in deleted", key)
}

if storeUpgrades.IsAdded(key) {
return fmt.Errorf("store upgrade has key %s in both added and deleted", key)
}

exists[key] = true
}

return nil
}
41 changes: 41 additions & 0 deletions runtime/v2/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"errors"
"fmt"

"cosmossdk.io/core/store"
Expand Down Expand Up @@ -69,6 +70,11 @@ func DefaultStoreLoader(store Store) error {
// UpgradeStoreLoader upgrades the store if the upgrade height matches the current version, it is used as a replacement
// for the DefaultStoreLoader when there are store upgrades
func UpgradeStoreLoader(upgradeHeight int64, storeUpgrades *store.StoreUpgrades) StoreLoader {
// sanity checks on store upgrades
if err := checkStoreUpgrade(storeUpgrades); err != nil {
panic(err)
}

return func(store Store) error {
latestVersion, err := store.GetLatestVersion()
if err != nil {
Expand All @@ -88,3 +94,38 @@ func UpgradeStoreLoader(upgradeHeight int64, storeUpgrades *store.StoreUpgrades)
return DefaultStoreLoader(store)
}
}

// checkStoreUpgrade performs sanity checks on the store upgrades
func checkStoreUpgrade(storeUpgrades *store.StoreUpgrades) error {
if storeUpgrades == nil {
return errors.New("store upgrades cannot be nil")
}

// check for duplicates
var exists = make(map[string]bool)
for _, key := range storeUpgrades.Added {
if exists[key] {
return fmt.Errorf("store upgrade has duplicate key %s in added", key)
}

if storeUpgrades.IsDeleted(key) {
return fmt.Errorf("store upgrade has key %s in both added and deleted", key)
}

exists[key] = true
}
exists = make(map[string]bool)
for _, key := range storeUpgrades.Deleted {
if exists[key] {
return fmt.Errorf("store upgrade has duplicate key %s in deleted", key)
}

if storeUpgrades.IsAdded(key) {
return fmt.Errorf("store upgrade has key %s in both added and deleted", key)
}

exists[key] = true
}

return nil
}
Loading