-
Notifications
You must be signed in to change notification settings - Fork 44
/
store.go
45 lines (36 loc) · 1.61 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
package v2
import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
poststypes "github.com/desmos-labs/desmos/v7/x/posts/types"
"github.com/desmos-labs/desmos/v7/x/reactions/types"
subspacestypes "github.com/desmos-labs/desmos/v7/x/subspaces/types"
)
type SubspacesKeeper interface {
IterateSubspaces(ctx sdk.Context, fn func(subspaces subspacestypes.Subspace) (stop bool))
}
type PostsKeeper interface {
IteratePosts(ctx sdk.Context, fn func(post poststypes.Post) (stop bool))
}
// MigrateStore performs in-place store migrations from v1 to v2
// The things done here are the following:
// 1. setting next registered reaction id
// 2. setting next reaction id for existing posts
// 3. setting the reaction params for existing subspaces
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, sk SubspacesKeeper, pk PostsKeeper, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
// Set the next registered reaction id for all the subspaces
sk.IterateSubspaces(ctx, func(subspace subspacestypes.Subspace) (stop bool) {
store.Set(types.NextRegisteredReactionIDStoreKey(subspace.ID), types.GetRegisteredReactionIDBytes(1))
params := types.DefaultReactionsParams(subspace.ID)
store.Set(types.SubspaceReactionsParamsStoreKey(subspace.ID), cdc.MustMarshal(¶ms))
return false
})
// Set the next reaction id for all the posts
pk.IteratePosts(ctx, func(post poststypes.Post) (stop bool) {
store.Set(types.NextReactionIDStoreKey(post.SubspaceID, post.ID), types.GetReactionIDBytes(1))
return false
})
return nil
}