From eba2e4c8d7cd34ec2205dc1590aa588e1815beee Mon Sep 17 00:00:00 2001 From: Srinivas Baride Date: Sat, 12 Mar 2022 22:48:29 +0530 Subject: [PATCH] Added migration methods for provider store --- x/provider/keeper/keeper.go | 4 ++-- x/provider/keeper/migrations.go | 37 +++++++++++++++++++++++++++++ x/provider/keeper/provider.go | 8 +++---- x/provider/keeper/query_server.go | 2 +- x/provider/simulation/decoder.go | 6 ++--- x/provider/simulation/operations.go | 6 ++--- x/provider/types/keys.go | 13 +++------- 7 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 x/provider/keeper/migrations.go diff --git a/x/provider/keeper/keeper.go b/x/provider/keeper/keeper.go index c234fd72..879e8892 100644 --- a/x/provider/keeper/keeper.go +++ b/x/provider/keeper/keeper.go @@ -14,13 +14,13 @@ import ( ) type Keeper struct { - cdc codec.BinaryMarshaler + cdc codec.BinaryCodec key sdk.StoreKey params paramstypes.Subspace distribution expected.DistributionKeeper } -func NewKeeper(cdc codec.BinaryMarshaler, key sdk.StoreKey, params paramstypes.Subspace) Keeper { +func NewKeeper(cdc codec.BinaryCodec, key sdk.StoreKey, params paramstypes.Subspace) Keeper { return Keeper{ cdc: cdc, key: key, diff --git a/x/provider/keeper/migrations.go b/x/provider/keeper/migrations.go new file mode 100644 index 00000000..9d01a268 --- /dev/null +++ b/x/provider/keeper/migrations.go @@ -0,0 +1,37 @@ +package keeper + +import ( + "github.com/cosmos/cosmos-sdk/store/prefix" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/sentinel-official/hub/x/provider/types" +) + +type Migrator struct { + k Keeper +} + +func NewMigrator(k Keeper) Migrator { + return Migrator{k: k} +} + +func (m Migrator) Migrate1to2(ctx sdk.Context) error { + store := m.k.Store(ctx) + return migrateProviderKeys(store) +} + +func migrateProviderKeys(parent sdk.KVStore) error { + child := prefix.NewStore(parent, types.ProviderKeyPrefix) + + iterator := child.Iterator(nil, nil) + defer iterator.Close() + + for ; iterator.Valid(); iterator.Next() { + key := types.ProviderKey(iterator.Key()) + + parent.Set(key, iterator.Value()) + child.Delete(iterator.Key()) + } + + return nil +} diff --git a/x/provider/keeper/provider.go b/x/provider/keeper/provider.go index b9c4bf1d..b69470b8 100644 --- a/x/provider/keeper/provider.go +++ b/x/provider/keeper/provider.go @@ -10,7 +10,7 @@ import ( // SetProvider is for inserting a provider into the KVStore. func (k *Keeper) SetProvider(ctx sdk.Context, provider types.Provider) { key := types.ProviderKey(provider.GetAddress()) - value := k.cdc.MustMarshalBinaryBare(&provider) + value := k.cdc.MustMarshal(&provider) store := k.Store(ctx) store.Set(key, value) @@ -34,7 +34,7 @@ func (k *Keeper) GetProvider(ctx sdk.Context, address hubtypes.ProvAddress) (pro return provider, false } - k.cdc.MustUnmarshalBinaryBare(value, &provider) + k.cdc.MustUnmarshal(value, &provider) return provider, true } @@ -52,7 +52,7 @@ func (k *Keeper) GetProviders(ctx sdk.Context, skip, limit int64) (items types.P iter.Skip(skip) iter.Limit(limit, func(iter sdk.Iterator) { var item types.Provider - k.cdc.MustUnmarshalBinaryBare(iter.Value(), &item) + k.cdc.MustUnmarshal(iter.Value(), &item) items = append(items, item) }) @@ -68,7 +68,7 @@ func (k *Keeper) IterateProviders(ctx sdk.Context, fn func(index int, item types for i := 0; iter.Valid(); iter.Next() { var provider types.Provider - k.cdc.MustUnmarshalBinaryBare(iter.Value(), &provider) + k.cdc.MustUnmarshal(iter.Value(), &provider) if stop := fn(i, provider); stop { break diff --git a/x/provider/keeper/query_server.go b/x/provider/keeper/query_server.go index b9dd21f9..f33921fd 100644 --- a/x/provider/keeper/query_server.go +++ b/x/provider/keeper/query_server.go @@ -58,7 +58,7 @@ func (q *queryServer) QueryProviders(c context.Context, req *types.QueryProvider pagination, err := query.FilteredPaginate(store, req.Pagination, func(_, value []byte, accumulate bool) (bool, error) { var item types.Provider - if err := q.cdc.UnmarshalBinaryBare(value, &item); err != nil { + if err := q.cdc.Unmarshal(value, &item); err != nil { return false, err } diff --git a/x/provider/simulation/decoder.go b/x/provider/simulation/decoder.go index 5d7d81b9..fd01bf9d 100644 --- a/x/provider/simulation/decoder.go +++ b/x/provider/simulation/decoder.go @@ -10,12 +10,12 @@ import ( "github.com/sentinel-official/hub/x/provider/types" ) -func NewStoreDecoder(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string { +func NewStoreDecoder(cdc codec.Codec) func(kvA, kvB kv.Pair) string { return func(kvA, kvB kv.Pair) string { if bytes.Equal(kvA.Key[:1], types.ProviderKeyPrefix) { var providerA, providerB types.Provider - cdc.MustUnmarshalBinaryBare(kvA.Value, &providerA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &providerB) + cdc.MustUnmarshal(kvA.Value, &providerA) + cdc.MustUnmarshal(kvB.Value, &providerB) return fmt.Sprintf("%v\n%v", providerA, providerB) } diff --git a/x/provider/simulation/operations.go b/x/provider/simulation/operations.go index 1af2fe8a..d405212c 100644 --- a/x/provider/simulation/operations.go +++ b/x/provider/simulation/operations.go @@ -24,7 +24,7 @@ var ( func WeightedOperations( params simulationtypes.AppParams, - cdc codec.JSONMarshaler, + cdc codec.JSONCodec, ak expected.AccountKeeper, bk expected.BankKeeper, k keeper.Keeper, @@ -135,7 +135,7 @@ func SimulateMsgRegisterRequest(ak expected.AccountKeeper, bk expected.BankKeepe return simulationtypes.NoOpMsg(types.ModuleName, types.TypeMsgRegisterRequest, err.Error()), nil, err } - return simulationtypes.NewOperationMsg(message, true, ""), nil, nil + return simulationtypes.NewOperationMsg(message, true, "", nil), nil, nil } } @@ -204,6 +204,6 @@ func SimulateMsgUpdateRequest(ak expected.AccountKeeper, bk expected.BankKeeper, return simulationtypes.NoOpMsg(types.ModuleName, types.TypeMsgUpdateRequest, err.Error()), nil, err } - return simulationtypes.NewOperationMsg(message, true, ""), nil, nil + return simulationtypes.NewOperationMsg(message, true, "", nil), nil, nil } } diff --git a/x/provider/types/keys.go b/x/provider/types/keys.go index 558d761e..78aa0896 100644 --- a/x/provider/types/keys.go +++ b/x/provider/types/keys.go @@ -1,9 +1,7 @@ package types import ( - "fmt" - - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/address" hubtypes "github.com/sentinel-official/hub/types" ) @@ -28,11 +26,6 @@ var ( ProviderKeyPrefix = []byte{0x10} ) -func ProviderKey(address hubtypes.ProvAddress) []byte { - v := append(ProviderKeyPrefix, address.Bytes()...) - if len(v) != 1+sdk.AddrLen { - panic(fmt.Errorf("invalid key length %d; expected %d", len(v), 1+sdk.AddrLen)) - } - - return v +func ProviderKey(addr hubtypes.ProvAddress) []byte { + return append(ProviderKeyPrefix, address.MustLengthPrefix(addr.Bytes())...) }