-
Notifications
You must be signed in to change notification settings - Fork 204
/
migrations.go
49 lines (39 loc) · 1.5 KB
/
migrations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package v2
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
icacallbacktypes "github.com/Stride-Labs/stride/v6/x/icacallbacks/types"
)
func migrateCallbacks(store sdk.KVStore, cdc codec.BinaryCodec) error {
icacallbackStore := prefix.NewStore(store, []byte(icacallbacktypes.CallbackDataKeyPrefix))
iter := icacallbackStore.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
// Deserialize the callback data
var oldCallbackData icacallbacktypes.CallbackData
err := cdc.Unmarshal(iter.Value(), &oldCallbackData)
if err != nil {
return sdkerrors.Wrapf(err, "unable to unmarshal callback data")
}
// Convert the callback data
// This will only convert the callback data args, of which the serialization has changed
newCallbackData, err := convertCallbackData(oldCallbackData)
if err != nil {
return sdkerrors.Wrapf(err, "unable to convert callback data to new schema")
}
newCallbackDataBz, err := cdc.Marshal(&newCallbackData)
if err != nil {
return sdkerrors.Wrapf(err, "unable to marshal callback data")
}
// Set new value on store.
icacallbackStore.Set(iter.Key(), newCallbackDataBz)
}
return nil
}
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
return migrateCallbacks(store, cdc)
}