Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Oracle Slashing #340

Merged
merged 1 commit into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x/oracle/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func EndBlocker(ctx sdk.Context, k Keeper) {

// Do slash who did miss voting over threshold and
// reset miss counters of all validators at the last block of slash window
if core.IsPeriodLastBlock(ctx, params.VotePeriod*params.SlashWindow) {
if core.IsPeriodLastBlock(ctx, params.SlashWindow) {
SlashAndResetMissCounters(ctx, k)
}

Expand Down
6 changes: 6 additions & 0 deletions x/oracle/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ func (k Keeper) SetMissCounter(ctx sdk.Context, operator sdk.ValAddress, missCou
store.Set(types.GetMissCounterKey(operator), bz)
}

// DeleteMissCounter removes miss counter for the validator
func (k Keeper) DeleteMissCounter(ctx sdk.Context, operator sdk.ValAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetMissCounterKey(operator))
}

// IterateMissCounters iterates over the miss counters and performs a callback function.
func (k Keeper) IterateMissCounters(ctx sdk.Context,
handler func(operator sdk.ValAddress, missCounter int64) (stop bool)) {
Expand Down
4 changes: 4 additions & 0 deletions x/oracle/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ func TestMissCounter(t *testing.T) {
input.OracleKeeper.SetMissCounter(input.Ctx, ValAddrs[0], missCounter)
counter = input.OracleKeeper.GetMissCounter(input.Ctx, ValAddrs[0])
require.Equal(t, missCounter, counter)

input.OracleKeeper.DeleteMissCounter(input.Ctx, ValAddrs[0])
counter = input.OracleKeeper.GetMissCounter(input.Ctx, ValAddrs[0])
require.Equal(t, int64(0), counter)
}

func TestIterateMissCounters(t *testing.T) {
Expand Down
29 changes: 27 additions & 2 deletions x/oracle/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,38 @@ func TestSlashAndResetMissCounters(t *testing.T) {
SlashAndResetMissCounters(input.Ctx, input.OracleKeeper)
staking.EndBlocker(input.Ctx, input.StakingKeeper)

validator := input.StakingKeeper.Validator(input.Ctx, keeper.ValAddrs[0])
validator, _ := input.StakingKeeper.GetValidator(input.Ctx, keeper.ValAddrs[0])
require.Equal(t, stakingAmt, validator.GetBondedTokens())

// Case 2, slash
input.OracleKeeper.SetMissCounter(input.Ctx, keeper.ValAddrs[0], votePeriodsPerWindow-minValidVotes+1)
SlashAndResetMissCounters(input.Ctx, input.OracleKeeper)
validator = input.StakingKeeper.Validator(input.Ctx, keeper.ValAddrs[0])
validator, _ = input.StakingKeeper.GetValidator(input.Ctx, keeper.ValAddrs[0])
require.Equal(t, stakingAmt.Sub(slashFraction.MulInt(stakingAmt).TruncateInt()), validator.GetBondedTokens())
require.True(t, validator.IsJailed())

// Case 3, slash unbonded validator
validator, _ = input.StakingKeeper.GetValidator(input.Ctx, keeper.ValAddrs[0])
validator.Status = sdk.Unbonded
validator.Jailed = false
validator.Tokens = stakingAmt
input.StakingKeeper.SetValidator(input.Ctx, validator)

input.OracleKeeper.SetMissCounter(input.Ctx, keeper.ValAddrs[0], votePeriodsPerWindow-minValidVotes+1)
SlashAndResetMissCounters(input.Ctx, input.OracleKeeper)
validator, _ = input.StakingKeeper.GetValidator(input.Ctx, keeper.ValAddrs[0])
require.Equal(t, stakingAmt, validator.Tokens)
require.False(t, validator.IsJailed())

// Case 4, slash jailed validator
validator, _ = input.StakingKeeper.GetValidator(input.Ctx, keeper.ValAddrs[0])
validator.Status = sdk.Bonded
validator.Jailed = true
validator.Tokens = stakingAmt
input.StakingKeeper.SetValidator(input.Ctx, validator)

input.OracleKeeper.SetMissCounter(input.Ctx, keeper.ValAddrs[0], votePeriodsPerWindow-minValidVotes+1)
SlashAndResetMissCounters(input.Ctx, input.OracleKeeper)
validator, _ = input.StakingKeeper.GetValidator(input.Ctx, keeper.ValAddrs[0])
require.Equal(t, stakingAmt, validator.Tokens)
}
14 changes: 8 additions & 6 deletions x/oracle/slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ func SlashAndResetMissCounters(ctx sdk.Context, k Keeper) {
// Penalize the validator whose the valid vote rate is smaller than min threshold
if validVoteRate.LT(minValidPerWindow) {
validator := k.StakingKeeper.Validator(ctx, operator)
k.StakingKeeper.Slash(
ctx, validator.GetConsAddr(),
distributionHeight, validator.GetConsensusPower(), slashFraction,
)
k.StakingKeeper.Jail(ctx, validator.GetConsAddr())
if validator.IsBonded() && !validator.IsJailed() {
k.StakingKeeper.Slash(
ctx, validator.GetConsAddr(),
distributionHeight, validator.GetConsensusPower(), slashFraction,
)
k.StakingKeeper.Jail(ctx, validator.GetConsAddr())
}
}

k.SetMissCounter(ctx, operator, 0)
k.DeleteMissCounter(ctx, operator)
return false
})
}