Skip to content

Commit

Permalink
Added migration methods for provider store
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Mar 12, 2022
1 parent 4808d77 commit eba2e4c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 23 deletions.
4 changes: 2 additions & 2 deletions x/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions x/provider/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 4 additions & 4 deletions x/provider/keeper/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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)
})

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/provider/keeper/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions x/provider/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions x/provider/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}
13 changes: 3 additions & 10 deletions x/provider/types/keys.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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())...)
}

0 comments on commit eba2e4c

Please sign in to comment.