Skip to content

Commit

Permalink
Added migration methods for deposit store
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Mar 12, 2022
1 parent 5a3cfc5 commit 4808d77
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
8 changes: 4 additions & 4 deletions x/deposit/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// SetDeposit is for inserting a deposit into KVStore.
func (k *Keeper) SetDeposit(ctx sdk.Context, deposit types.Deposit) {
key := types.DepositKey(deposit.GetAddress())
value := k.cdc.MustMarshalBinaryBare(&deposit)
value := k.cdc.MustMarshal(&deposit)

store := k.Store(ctx)
store.Set(key, value)
Expand All @@ -26,7 +26,7 @@ func (k *Keeper) GetDeposit(ctx sdk.Context, address sdk.AccAddress) (deposit ty
return deposit, false
}

k.cdc.MustUnmarshalBinaryBare(value, &deposit)
k.cdc.MustUnmarshal(value, &deposit)
return deposit, true
}

Expand All @@ -44,7 +44,7 @@ func (k *Keeper) GetDeposits(ctx sdk.Context, skip, limit int64) (items types.De
iter.Skip(skip)
iter.Limit(limit, func(iter sdk.Iterator) {
var item types.Deposit
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &item)
k.cdc.MustUnmarshal(iter.Value(), &item)
items = append(items, item)
})

Expand Down Expand Up @@ -137,7 +137,7 @@ func (k *Keeper) IterateDeposits(ctx sdk.Context, fn func(index int, item types.

for i := 0; iterator.Valid(); iterator.Next() {
var deposit types.Deposit
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &deposit)
k.cdc.MustUnmarshal(iterator.Value(), &deposit)

if stop := fn(i, deposit); stop {
break
Expand Down
4 changes: 2 additions & 2 deletions x/deposit/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

type Keeper struct {
key sdk.StoreKey
cdc codec.BinaryMarshaler
cdc codec.BinaryCodec
bank expected.BankKeeper
}

func NewKeeper(cdc codec.BinaryMarshaler, key sdk.StoreKey) Keeper {
func NewKeeper(cdc codec.BinaryCodec, key sdk.StoreKey) Keeper {
return Keeper{
key: key,
cdc: cdc,
Expand Down
37 changes: 37 additions & 0 deletions x/deposit/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/deposit/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 migrateDepositKeys(store)
}

func migrateDepositKeys(parent sdk.KVStore) error {
child := prefix.NewStore(parent, types.DepositKeyPrefix)

iterator := child.Iterator(nil, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
key := types.DepositKey(iterator.Key())

parent.Set(key, iterator.Value())
child.Delete(iterator.Key())
}

return nil
}
2 changes: 1 addition & 1 deletion x/deposit/keeper/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (q *queryServer) QueryDeposits(c context.Context, req *types.QueryDepositsR

pagination, err := query.FilteredPaginate(store, req.Pagination, func(_, value []byte, accumulate bool) (bool, error) {
var item types.Deposit
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/deposit/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/deposit/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.DepositKeyPrefix) {
var depositA, depositB types.Deposit
cdc.MustUnmarshalBinaryBare(kvA.Value, &depositA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &depositB)
cdc.MustUnmarshal(kvA.Value, &depositA)
cdc.MustUnmarshal(kvB.Value, &depositB)

return fmt.Sprintf("%v\n%v", depositA, depositB)
}
Expand Down
12 changes: 3 additions & 9 deletions x/deposit/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package types

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)

const (
Expand All @@ -20,11 +19,6 @@ var (
DepositKeyPrefix = []byte{0x10}
)

func DepositKey(address sdk.AccAddress) []byte {
v := append(DepositKeyPrefix, 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 DepositKey(addr sdk.AccAddress) []byte {
return append(DepositKeyPrefix, address.MustLengthPrefix(addr.Bytes())...)
}

0 comments on commit 4808d77

Please sign in to comment.