Skip to content

Instantly share code, notes, and snippets.

@zeroFruit
Created July 5, 2022 01:10
Show Gist options
  • Save zeroFruit/a11e41c7f6c4a874b70fda80965c6e1f to your computer and use it in GitHub Desktop.
Save zeroFruit/a11e41c7f6c4a874b70fda80965c6e1f to your computer and use it in GitHub Desktop.
Cosmos Dev Series: Cosmos-SDK-based Blockchain Upgrade - x/foo migration impl
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