Skip to content

Commit

Permalink
changing method of migration for UserRewardInfo (#722)
Browse files Browse the repository at this point in the history
* changing method of migration for UserRewardInfo

* masterchef migration

* commenting migration of user reward infos

* commenting migration of user reward infos

* Update x/masterchef/keeper/user_reward_info.go

Co-authored-by: Amit Yadav <amy29981@gmail.com>

---------

Co-authored-by: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com>
Co-authored-by: Amit Yadav <amy29981@gmail.com>
  • Loading branch information
3 people authored Aug 15, 2024
1 parent 5583131 commit f0783c0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
61 changes: 35 additions & 26 deletions x/masterchef/keeper/user_reward_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import (
)

func (k Keeper) SetUserRewardInfo(ctx sdk.Context, userReward types.UserRewardInfo) {
store := ctx.KVStore(k.storeKey)
//store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshal(&userReward)
key := types.GetUserRewardInfoKey(userReward.GetUserAccount(), userReward.GetPoolId(), userReward.GetRewardDenom())
//key := types.GetUserRewardInfoKey(userReward.GetUserAccount(), userReward.GetPoolId(), userReward.GetRewardDenom())
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.LegacyUserRewardInfoKeyPrefix))
key := types.LegacyUserRewardInfoKey(userReward.User, userReward.PoolId, userReward.RewardDenom)
store.Set(key, b)
}

func (k Keeper) GetUserRewardInfo(ctx sdk.Context, user sdk.AccAddress, poolId uint64, rewardDenom string) (val types.UserRewardInfo, found bool) {
store := ctx.KVStore(k.storeKey)
key := types.GetUserRewardInfoKey(user, poolId, rewardDenom)
//store := ctx.KVStore(k.storeKey)
//key := types.GetUserRewardInfoKey(user, poolId, rewardDenom)
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.LegacyUserRewardInfoKeyPrefix))
key := types.LegacyUserRewardInfoKey(user.String(), poolId, rewardDenom)
b := store.Get(key)
if b == nil {
return val, false
Expand All @@ -25,15 +29,17 @@ func (k Keeper) GetUserRewardInfo(ctx sdk.Context, user sdk.AccAddress, poolId u
return val, true
}

func (k Keeper) RemoveUserRewardInfo(ctx sdk.Context, user sdk.AccAddress, poolId uint64, rewardDenom string) {
store := ctx.KVStore(k.storeKey)
key := types.GetUserRewardInfoKey(user, poolId, rewardDenom)
store.Delete(key)
}
//func (k Keeper) RemoveUserRewardInfo(ctx sdk.Context, user sdk.AccAddress, poolId uint64, rewardDenom string) {
// store := ctx.KVStore(k.storeKey)
// key := types.GetUserRewardInfoKey(user, poolId, rewardDenom)
// store.Delete(key)
//}

func (k Keeper) GetAllUserRewardInfos(ctx sdk.Context) (list []types.UserRewardInfo) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.UserRewardInfoKeyPrefix)
//store := ctx.KVStore(k.storeKey)
//iterator := sdk.KVStorePrefixIterator(store, types.UserRewardInfoKeyPrefix)
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.LegacyUserRewardInfoKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

Expand All @@ -47,23 +53,26 @@ func (k Keeper) GetAllUserRewardInfos(ctx sdk.Context) (list []types.UserRewardI
}

// remove after migration
func (k Keeper) DeleteLegacyUserRewardInfo(ctx sdk.Context, user string, poolId uint64, rewardDenom string) {
func (k Keeper) RemoveUserRewardInfo(ctx sdk.Context, user string, poolId uint64, rewardDenom string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.LegacyUserRewardInfoKeyPrefix))
store.Delete(types.LegacyUserRewardInfoKey(user, poolId, rewardDenom))
}

// remove after migration
func (k Keeper) GetAllLegacyUserRewardInfos(ctx sdk.Context) (list []types.UserRewardInfo) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.LegacyUserRewardInfoKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.UserRewardInfo
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}

return
}
// TODO: Optimise user reward info KV
//func (k Keeper) MigrateFromV2UserRewardInfos(ctx sdk.Context) {
// store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.LegacyUserRewardInfoKeyPrefix))
// iterator := sdk.KVStorePrefixIterator(store, []byte{})
//
// defer iterator.Close()
//
// for ; iterator.Valid(); iterator.Next() {
// var legacyUserRewardInfo types.UserRewardInfo
// k.cdc.MustUnmarshal(iterator.Value(), &legacyUserRewardInfo)
//
// k.SetUserRewardInfo(ctx, legacyUserRewardInfo)
// k.DeleteLegacyUserRewardInfo(ctx, legacyUserRewardInfo.User, legacyUserRewardInfo.PoolId, legacyUserRewardInfo.RewardDenom)
// }
//
// return
//}
2 changes: 1 addition & 1 deletion x/masterchef/keeper/user_reward_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestUserRewardInfo(t *testing.T) {
rewardInfosStored := app.MasterchefKeeper.GetAllUserRewardInfos(ctx)
require.Len(t, rewardInfosStored, 3)

app.MasterchefKeeper.RemoveUserRewardInfo(ctx, userRewardInfos[0].GetUserAccount(), userRewardInfos[0].PoolId, userRewardInfos[0].RewardDenom)
app.MasterchefKeeper.RemoveUserRewardInfo(ctx, userRewardInfos[0].User, userRewardInfos[0].PoolId, userRewardInfos[0].RewardDenom)
rewardInfosStored = app.MasterchefKeeper.GetAllUserRewardInfos(ctx)
require.Len(t, rewardInfosStored, 2)
}
6 changes: 1 addition & 5 deletions x/masterchef/migrations/v3_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ func (m Migrator) V3Migration(ctx sdk.Context) error {
m.keeper.SetParams(ctx, params)
m.keeper.DeleteLegacyParams(ctx)

legacyUserRewardInfos := m.keeper.GetAllLegacyUserRewardInfos(ctx)
for _, legacyUserRewardInfo := range legacyUserRewardInfos {
m.keeper.SetUserRewardInfo(ctx, legacyUserRewardInfo)
m.keeper.DeleteLegacyUserRewardInfo(ctx, legacyUserRewardInfo.User, legacyUserRewardInfo.PoolId, legacyUserRewardInfo.RewardDenom)
}
//m.keeper.MigrateFromV2UserRewardInfos(ctx)

legacyPoolInfos := m.keeper.GetAllLegacyPoolInfos(ctx)
for _, legacyPoolInfo := range legacyPoolInfos {
Expand Down
4 changes: 2 additions & 2 deletions x/masterchef/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := migrations.NewMigrator(am.keeper)
err := cfg.RegisterMigration(types.ModuleName, 1, m.V2Migration)
err := cfg.RegisterMigration(types.ModuleName, 2, m.V3Migration)
if err != nil {
panic(err)
}
Expand All @@ -147,7 +147,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 2 }
func (AppModule) ConsensusVersion() uint64 { return 3 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down

0 comments on commit f0783c0

Please sign in to comment.