-
Notifications
You must be signed in to change notification settings - Fork 44
/
store.go
89 lines (70 loc) · 2.38 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
package v2
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// MigrateStore migrates the store from version 1 to version 2.
// The migration process will fix all user and group permissions sanitizing their values.
func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
err := fixGroupsPermissions(store, cdc)
if err != nil {
return err
}
fixUsersPermissions(store)
return nil
}
// fixGroupsPermissions iterates over all the group permissions and sanitizes their values
func fixGroupsPermissions(store sdk.KVStore, cdc codec.BinaryCodec) error {
groupsStore := prefix.NewStore(store, GroupsPrefix)
iterator := groupsStore.Iterator(nil, nil)
var groups []UserGroup
for ; iterator.Valid(); iterator.Next() {
var group UserGroup
err := cdc.Unmarshal(iterator.Value(), &group)
if err != nil {
return err
}
// Sanitize the permissions
group.Permissions = SanitizePermission(group.Permissions)
groups = append(groups, group)
}
iterator.Close()
// Store the new groups
for i, group := range groups {
bz, err := cdc.Marshal(&groups[i])
if err != nil {
return err
}
store.Set(GroupStoreKey(group.SubspaceID, group.ID), bz)
}
return nil
}
type userPermissionDetails struct {
subspaceID uint64
user sdk.AccAddress
permissions Permission
}
// fixUsersPermissions iterates over all the users permissions and sanitizes their values
func fixUsersPermissions(store sdk.KVStore) {
permissionsStore := prefix.NewStore(store, UserPermissionsStorePrefix)
iterator := permissionsStore.Iterator(nil, nil)
var permissions []userPermissionDetails
for ; iterator.Valid(); iterator.Next() {
// The first 8 bytes are the subspace id (uint64 takes up 8 bytes)
// The remaining bytes are the user address
subspaceBz, addressBz := iterator.Key()[:8], iterator.Key()[8:]
permissions = append(permissions, userPermissionDetails{
subspaceID: GetSubspaceIDFromBytes(subspaceBz),
user: GetAddressBytes(addressBz),
// Sanitize the permission
permissions: SanitizePermission(UnmarshalPermission(iterator.Value())),
})
}
iterator.Close()
// Store the new permissions
for _, entry := range permissions {
store.Set(UserPermissionStoreKey(entry.subspaceID, entry.user), MarshalPermission(entry.permissions))
}
}