-
Notifications
You must be signed in to change notification settings - Fork 44
/
store.go
115 lines (98 loc) · 3.21 KB
/
store.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package v200
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
v100 "github.com/desmos-labs/desmos/v2/x/profiles/legacy/v100"
"github.com/desmos-labs/desmos/v2/x/profiles/types"
)
// MigrateStore performs in-place store migrations from v1.0 to v2.0 The
// migration includes:
//
// - Change OracleRequest to have uint64 ID instead of int64
// - Change OracleRequest to have uint64 OracleScriptID instead of int64
// - Change OracleParams to have uint64 ScriptID instead of int64
// - Change OracleParams to remove FeePayer
func MigrateStore(
ctx sdk.Context, storeKey sdk.StoreKey, subspace paramstypes.Subspace,
cdc codec.BinaryCodec, legacyAmino *codec.LegacyAmino,
) error {
store := ctx.KVStore(storeKey)
err := migrateParams(ctx, subspace, legacyAmino)
if err != nil {
return err
}
err = migrateAppLinks(store, cdc)
if err != nil {
return err
}
return nil
}
func migrateAppLinks(store sdk.KVStore, cdc codec.BinaryCodec) error {
iterator := sdk.KVStorePrefixIterator(store, types.UserApplicationLinkPrefix)
defer iterator.Close()
var keys [][]byte
var newLinks []types.ApplicationLink
for ; iterator.Valid(); iterator.Next() {
var v1ApplicationLink v100.ApplicationLink
err := cdc.Unmarshal(iterator.Value(), &v1ApplicationLink)
if err != nil {
return err
}
keys = append(keys, iterator.Key())
newLinks = append(newLinks, types.NewApplicationLink(
v1ApplicationLink.User,
types.NewData(v1ApplicationLink.Data.Application, v1ApplicationLink.Data.Username),
types.ApplicationLinkState(v1ApplicationLink.State),
types.NewOracleRequest(
uint64(v1ApplicationLink.OracleRequest.ID),
uint64(v1ApplicationLink.OracleRequest.OracleScriptID),
types.NewOracleRequestCallData(
v1ApplicationLink.OracleRequest.CallData.Application,
v1ApplicationLink.OracleRequest.CallData.CallData,
),
v1ApplicationLink.OracleRequest.ClientID,
),
migrateAppLinkResult(v1ApplicationLink.Result),
v1ApplicationLink.CreationTime,
))
store.Delete(iterator.Key())
}
for index, link := range newLinks {
store.Set(keys[index], types.MustMarshalApplicationLink(cdc, link))
}
return nil
}
func migrateAppLinkResult(r *v100.Result) *types.Result {
if r == nil {
return nil
}
switch result := (r.Sum).(type) {
case *v100.Result_Success_:
return types.NewSuccessResult(result.Success.Value, result.Success.Signature)
case *v100.Result_Failed_:
return types.NewErrorResult(result.Failed.Error)
default:
panic(fmt.Errorf("invalid result type"))
}
}
// migrateParams migrates the OracleParams by removing the FeePayer field
// and converting the ScriptID from int64 to uint64
func migrateParams(ctx sdk.Context, subspace paramstypes.Subspace, legacyAmino *codec.LegacyAmino) error {
var v1Params v100.OracleParams
err := legacyAmino.UnmarshalJSON(subspace.GetRaw(ctx, types.OracleParamsKey), &v1Params)
if err != nil {
return err
}
var v2Params = types.NewOracleParams(
uint64(v1Params.ScriptID),
v1Params.AskCount,
v1Params.MinCount,
v1Params.PrepareGas,
v1Params.ExecuteGas,
v1Params.FeeAmount...,
)
subspace.Set(ctx, types.OracleParamsKey, &v2Params)
return nil
}