Created
July 5, 2022 01:10
-
-
Save zeroFruit/a11e41c7f6c4a874b70fda80965c6e1f to your computer and use it in GitHub Desktop.
Cosmos Dev Series: Cosmos-SDK-based Blockchain Upgrade - x/foo migration impl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package v2 // migrations.go | |
func UpdateParams(ctx sdk.Context, paramStore *paramtypes.Subspace) error { | |
... | |
paramStore.Set(ctx, types.KeyValue, "foobar") | |
return nil | |
} | |
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error { | |
store := ctx.KVStore(storeKey) | |
return migrateValuesWithPrefix(store, cdc) | |
} | |
func migrateValuesWithPrefix(store sdk.KVStore, cdc codec.BinaryCodec) error { | |
oldStoreIter := store.Iterator(nil, nil) | |
for ; oldStoreIter.Valid(); oldStoreIter.Next() { | |
oldKey := oldStoreIter.Key() | |
oldVal := store.Get(oldKey) | |
newKey, newVal := migrateValue(cdc, oldKey, oldVal) | |
store.Set(newKey, newVal) | |
store.Delete(oldKey) // Delete old key, value | |
} | |
return nil | |
} | |
func migrateValue(cdc codec.BinaryCodec, oldKey []byte, oldVal []byte) (newKey []byte, newVal []byte) { | |
valWithMemo := types.ValueWithMemo{ | |
Value: string(oldVal), | |
Memo: "HELLO", // Default memo as 'HELLO' | |
} | |
newKey = types.GetValueKey(string(oldKey)) | |
newVal = cdc.MustMarshal(&valWithMemo) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment