-
Notifications
You must be signed in to change notification settings - Fork 44
/
store.go
104 lines (84 loc) · 2.82 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
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"
v1 "github.com/desmos-labs/desmos/v5/x/relationships/legacy/v1"
"github.com/desmos-labs/desmos/v5/x/relationships/types"
)
// MigrateStore performs in-place store migrations from v1 to v2.
// The migration includes:
//
// - migrate all relationships keys to the new ones
// - migrate all user blocks keys to the new ones
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
err := migrateRelationships(store, cdc)
if err != nil {
return err
}
err = migrateUserBlocks(store, cdc)
if err != nil {
return err
}
return nil
}
// migrateRelationships migrates all the relationships from using the old key to using the new key
func migrateRelationships(store sdk.KVStore, cdc codec.BinaryCodec) error {
prefixStore := prefix.NewStore(store, v1.RelationshipsStorePrefix)
iterator := prefixStore.Iterator(nil, nil)
// Read all the existing relationships and the associated keys
var keys [][]byte
var relationships []types.Relationship
for ; iterator.Valid(); iterator.Next() {
keys = append(keys, append(v1.RelationshipsStorePrefix, iterator.Key()...))
var relationship types.Relationship
err := cdc.Unmarshal(iterator.Value(), &relationship)
if err != nil {
return err
}
relationships = append(relationships, relationship)
}
iterator.Close()
for i, relationship := range relationships {
// Delete the old key
store.Delete(keys[i])
bz, err := cdc.Marshal(&relationships[i])
if err != nil {
return err
}
// Store the relationship with the new key
store.Set(types.RelationshipsStoreKey(relationship.Creator, relationship.Counterparty, relationship.SubspaceID), bz)
}
return nil
}
// migrateUserBlocks migrates all the user blocks from using the old key to using the new key
func migrateUserBlocks(store sdk.KVStore, cdc codec.BinaryCodec) error {
prefixStore := prefix.NewStore(store, v1.UsersBlocksStorePrefix)
iterator := prefixStore.Iterator(nil, nil)
// Read all the existing blocks and the associated keys
var keys [][]byte
var blocks []types.UserBlock
for ; iterator.Valid(); iterator.Next() {
keys = append(keys, append(v1.UsersBlocksStorePrefix, iterator.Key()...))
var block types.UserBlock
err := cdc.Unmarshal(iterator.Value(), &block)
if err != nil {
return err
}
blocks = append(blocks, block)
}
iterator.Close()
for i, block := range blocks {
// Delete the old key
store.Delete(keys[i])
bz, err := cdc.Marshal(&blocks[i])
if err != nil {
return err
}
// Store the block with the new key
store.Set(types.UserBlockStoreKey(block.Blocker, block.Blocked, block.SubspaceID), bz)
}
return nil
}