From 3cee1c238206972e5648e67150f3adc74e01df46 Mon Sep 17 00:00:00 2001 From: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> Date: Thu, 11 Jul 2024 15:03:14 +0200 Subject: [PATCH] ci: setup handler to clean up old states (#647) * ci: setup handler to clean up old states * Update app/setup_handlers.go Co-authored-by: Amit Yadav --------- Co-authored-by: Amit Yadav --- app/setup_handlers.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/app/setup_handlers.go b/app/setup_handlers.go index 60d86d8ed..6ef7cda3d 100644 --- a/app/setup_handlers.go +++ b/app/setup_handlers.go @@ -3,6 +3,8 @@ package app import ( "fmt" + wasmmodule "github.com/CosmWasm/wasmd/x/wasm" + wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" m "github.com/cosmos/cosmos-sdk/types/module" @@ -22,6 +24,37 @@ func setUpgradeHandler(app *ElysApp) { func(ctx sdk.Context, plan upgradetypes.Plan, vm m.VersionMap) (m.VersionMap, error) { app.Logger().Info("Running upgrade handler for " + version.Version) + if version.Version == "v0.38.0" || version.Version == "v999.999.999" { + // Retrieve the wasm module store key + storeKey := app.keys[wasmmodule.StoreKey] + + // Retrieve the wasm module store + store := ctx.KVStore(storeKey) + + // List of prefixes to clear + prefixes := [][]byte{ + // Account History contract + wasmtypes.GetContractStorePrefix(sdk.MustAccAddressFromBech32("elys1s37xz7tzrru2cpl96juu9lfqrsd4jh73j9slyv440q5vttx2uyesetjpne")), + } + + // Add old code keys to the list of prefixes to clear + for i := uint64(1); i < 671; i++ { + codeKey := wasmtypes.GetCodeKey(i) + // append the code key to the prefixes + prefixes = append(prefixes, codeKey) + } + + // Clear all keys in the store + for _, prefix := range prefixes { + iter := sdk.KVStorePrefixIterator(store, prefix) + defer iter.Close() + + for ; iter.Valid(); iter.Next() { + store.Delete(iter.Key()) + } + } + } + return app.mm.RunMigrations(ctx, app.configurator, vm) }, )