From fe586b2bdd835f825c8a1d04a51f61ffbdce1def Mon Sep 17 00:00:00 2001 From: Brandon Weng <18161326+BrandonWeng@users.noreply.github.com> Date: Tue, 28 Feb 2023 09:41:02 -0500 Subject: [PATCH] Add migration --- app/upgrades.go | 2 +- x/oracle/keeper/migrations.go | 24 ++++++++++++++++++++++++ x/oracle/keeper/migrations_test.go | 27 +++++++++++++++++++++++++++ x/oracle/module.go | 3 ++- x/oracle/simulation/operations.go | 4 ++-- 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/app/upgrades.go b/app/upgrades.go index 9316e59db6..9382461f68 100644 --- a/app/upgrades.go +++ b/app/upgrades.go @@ -52,7 +52,7 @@ var upgradesList = []string{ "2.0.29beta", "2.0.32beta", "2.0.36beta", - // TODO: Next upgrade need to update oracle vote sucess counter: https://github.com/sei-protocol/sei-chain/pull/621 + // TODO: Next upgrade need to update oracle vote success counter: https://github.com/sei-protocol/sei-chain/pull/621 } func (app App) RegisterUpgradeHandlers() { diff --git a/x/oracle/keeper/migrations.go b/x/oracle/keeper/migrations.go index 3b42ddb6fb..9243cdf6f1 100644 --- a/x/oracle/keeper/migrations.go +++ b/x/oracle/keeper/migrations.go @@ -70,3 +70,27 @@ func (m Migrator) Migrate4to5(ctx sdk.Context) error { } return nil } + +func (m Migrator) Migrate5To6(ctx sdk.Context) error { + // Do a one time backfill for success count in the vote penalty counter + store := ctx.KVStore(m.keeper.storeKey) + + // previously the data was stored as uint64, now it is VotePenaltyCounter proto + iter := sdk.KVStorePrefixIterator(store, types.VotePenaltyCounterKey) + defer iter.Close() + for ; iter.Valid(); iter.Next() { + var votePenaltyCounter types.VotePenaltyCounter + m.keeper.cdc.MustUnmarshal(iter.Value(), &votePenaltyCounter) + slashWindow := m.keeper.GetParams(ctx).SlashWindow + totalPenaltyCount := votePenaltyCounter.MissCount + votePenaltyCounter.AbstainCount + successCount := ((uint64)(ctx.BlockHeight()) % slashWindow) - totalPenaltyCount + newVotePenaltyCounter := types.VotePenaltyCounter{ + MissCount: votePenaltyCounter.MissCount, + AbstainCount: votePenaltyCounter.AbstainCount, + SuccessCount: successCount, + } + bz := m.keeper.cdc.MustMarshal(&newVotePenaltyCounter) + store.Set(iter.Key(), bz) + } + return nil +} diff --git a/x/oracle/keeper/migrations_test.go b/x/oracle/keeper/migrations_test.go index 40cbba16e2..88d2664fa5 100644 --- a/x/oracle/keeper/migrations_test.go +++ b/x/oracle/keeper/migrations_test.go @@ -125,3 +125,30 @@ func TestMigrate4to5(t *testing.T) { require.Equal(t, store.Has(genPrevoteKey(addr)), false) require.Equal(t, store.Has(genPrevoteKey(ValAddrs[1])), false) } + +func TestMigrate5to6(t *testing.T) { + input := CreateTestInput(t) + + addr := ValAddrs[0] + input.Ctx.KVStore(input.OracleKeeper.storeKey) + input.OracleKeeper.SetVotePenaltyCounter( + input.Ctx, + addr, + 12, + 13, + 0, + ) + + // Migrate store + m := NewMigrator(input.OracleKeeper) + input.Ctx = input.Ctx.WithBlockHeight(int64(input.OracleKeeper.GetParams(input.Ctx).SlashWindow) + 10000) + m.Migrate5To6(input.Ctx) + + // Get rate + votePenaltyCounter := input.OracleKeeper.GetVotePenaltyCounter(input.Ctx, addr) + require.Equal(t, types.VotePenaltyCounter{ + MissCount: 12, + AbstainCount: 13, + SuccessCount: 9975, + }, votePenaltyCounter) +} diff --git a/x/oracle/module.go b/x/oracle/module.go index 55d2452d5d..77633491de 100644 --- a/x/oracle/module.go +++ b/x/oracle/module.go @@ -143,6 +143,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { _ = cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3) _ = cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4) _ = cfg.RegisterMigration(types.ModuleName, 4, m.Migrate4to5) + _ = cfg.RegisterMigration(types.ModuleName, 5, m.Migrate5To6) } // InitGenesis performs genesis initialization for the oracle module. It returns @@ -163,7 +164,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw } // ConsensusVersion implements AppModule/ConsensusVersion. -func (AppModule) ConsensusVersion() uint64 { return 5 } +func (AppModule) ConsensusVersion() uint64 { return 6 } // BeginBlock returns the begin blocker for the oracle module. func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} diff --git a/x/oracle/simulation/operations.go b/x/oracle/simulation/operations.go index 3988c4b164..2028e73625 100644 --- a/x/oracle/simulation/operations.go +++ b/x/oracle/simulation/operations.go @@ -65,7 +65,7 @@ func WeightedOperations( } // SimulateMsgAggregateExchangeRateVote generates a MsgAggregateExchangeRateVote with random values. -//nolint: funlen +// nolint: funlen func SimulateMsgAggregateExchangeRateVote(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, @@ -122,7 +122,7 @@ func SimulateMsgAggregateExchangeRateVote(ak types.AccountKeeper, bk types.BankK } // SimulateMsgDelegateFeedConsent generates a MsgDelegateFeedConsent with random values. -//nolint: funlen +// nolint: funlen func SimulateMsgDelegateFeedConsent(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,