Skip to content

Commit

Permalink
Add migration
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonWeng committed Feb 28, 2023
1 parent 26d48a7 commit fe586b2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
24 changes: 24 additions & 0 deletions x/oracle/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
27 changes: 27 additions & 0 deletions x/oracle/keeper/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion x/oracle/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {}
Expand Down
4 changes: 2 additions & 2 deletions x/oracle/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit fe586b2

Please sign in to comment.