-
Notifications
You must be signed in to change notification settings - Fork 1
/
v5_to_v6.go
57 lines (53 loc) · 1.87 KB
/
v5_to_v6.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
50
51
52
53
54
55
56
57
package migrations
import (
"errors"
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/fibonacci-chain/core/x/dex/keeper"
"github.com/fibonacci-chain/core/x/dex/types"
)
// This migration updates contract info to match the new data format
func V5ToV6(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error {
for i := 0; i < 256; i++ {
if err := updateContractInfoForBytePrefix(ctx, storeKey, byte(i)); err != nil {
return err
}
}
return nil
}
// assuming no dependency exists at the time of this migration
func updateContractInfoForBytePrefix(ctx sdk.Context, storeKey sdk.StoreKey, b byte) error {
store := prefix.NewStore(
ctx.KVStore(storeKey),
[]byte(keeper.ContractPrefixKey),
)
iterator := sdk.KVStorePrefixIterator(store, []byte{b})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
oldContractInfoBytes := iterator.Value()
oldContractInfo := types.LegacyContractInfo{}
if err := oldContractInfo.Unmarshal(oldContractInfoBytes); err != nil {
ctx.Logger().Error(fmt.Sprintf("Failed to unmarshal contract info for %s", iterator.Key()))
return err
}
if oldContractInfo.DependentContractAddrs != nil {
ctx.Logger().Error(fmt.Sprintf("Contract info of %s has dependencies!", iterator.Key()))
return errors.New("contract has unexpected dependencies")
}
newContractInfo := types.ContractInfo{
CodeId: oldContractInfo.CodeId,
ContractAddr: oldContractInfo.ContractAddr,
NeedHook: oldContractInfo.NeedHook,
NeedOrderMatching: oldContractInfo.NeedOrderMatching,
}
bz, err := newContractInfo.Marshal()
if err != nil {
ctx.Logger().Error(fmt.Sprintf("Failed to marshal contract info for %s", iterator.Key()))
return err
}
store.Set([]byte(newContractInfo.ContractAddr), bz)
}
return nil
}