-
Notifications
You must be signed in to change notification settings - Fork 402
/
store.go
95 lines (76 loc) · 2.69 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
package v3
import (
"context"
"encoding/binary"
corestoretypes "cosmossdk.io/core/store"
"cosmossdk.io/store/prefix"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/CosmWasm/wasmd/x/wasm/types"
)
// StoreCodeInfoFn stores code info
type StoreCodeInfoFn func(ctx context.Context, codeID uint64, codeInfo types.CodeInfo)
// Keeper abstract keeper
type wasmKeeper interface {
SetParams(ctx context.Context, ps types.Params) error
}
// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper wasmKeeper
storeCodeInfoFn StoreCodeInfoFn
}
// NewMigrator returns a new Migrator.
func NewMigrator(k wasmKeeper, fn StoreCodeInfoFn) Migrator {
return Migrator{keeper: k, storeCodeInfoFn: fn}
}
// Migrate3to4 migrates from version 3 to 4.
func (m Migrator) Migrate3to4(ctx sdk.Context, storeService corestoretypes.KVStoreService, cdc codec.BinaryCodec) error {
var legacyParams Params
store := storeService.OpenKVStore(ctx)
bz, err := store.Get(types.ParamsKey)
if err != nil {
return err
}
if bz != nil {
cdc.MustUnmarshal(bz, &legacyParams)
newParams := types.Params{}
newParams.CodeUploadAccess = updateAccessConfig(legacyParams.CodeUploadAccess)
if legacyParams.InstantiateDefaultPermission == AccessTypeOnlyAddress {
newParams.InstantiateDefaultPermission = types.AccessTypeAnyOfAddresses
} else {
newParams.InstantiateDefaultPermission = types.AccessType(legacyParams.InstantiateDefaultPermission)
}
err := m.keeper.SetParams(ctx, newParams)
if err != nil {
return err
}
}
prefixStore := prefix.NewStore(runtime.KVStoreAdapter(store), types.CodeKeyPrefix)
iter := prefixStore.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var legacyCodeInfo CodeInfo
cdc.MustUnmarshal(iter.Value(), &legacyCodeInfo)
newAccessConfig := updateAccessConfig(legacyCodeInfo.InstantiateConfig)
newCodeInfo := types.CodeInfo{
CodeHash: legacyCodeInfo.CodeHash,
Creator: legacyCodeInfo.Creator,
InstantiateConfig: newAccessConfig,
}
m.storeCodeInfoFn(ctx, binary.BigEndian.Uint64(iter.Key()), newCodeInfo)
}
return nil
}
func updateAccessConfig(legacyAccessConfig AccessConfig) types.AccessConfig {
newAccessConfig := types.AccessConfig{}
switch legacyAccessConfig.Permission {
case AccessTypeOnlyAddress:
newAccessConfig.Permission = types.AccessTypeAnyOfAddresses
newAccessConfig.Addresses = []string{legacyAccessConfig.Address}
default:
newAccessConfig.Permission = types.AccessType(legacyAccessConfig.Permission)
newAccessConfig.Addresses = legacyAccessConfig.Addresses
}
return newAccessConfig
}