From 6ed552a38c91c764d0dc54e747fd90ff520c5ab4 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Mon, 27 Aug 2018 20:19:50 +0200 Subject: [PATCH] Panic in the right place --- x/slashing/slashing_period.go | 10 +++++++--- x/slashing/slashing_period_test.go | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/x/slashing/slashing_period.go b/x/slashing/slashing_period.go index 396cf83cb2fa..a6971f211097 100644 --- a/x/slashing/slashing_period.go +++ b/x/slashing/slashing_period.go @@ -12,6 +12,13 @@ func (k Keeper) capBySlashingPeriod(ctx sdk.Context, address sdk.ValAddress, fra // Calculate total amount to be slashed slashingPeriod := k.getValidatorSlashingPeriodForHeight(ctx, address, infractionHeight) + + // Sanity check + if slashingPeriod.EndHeight > 0 && slashingPeriod.EndHeight < infractionHeight { + panic(fmt.Sprintf("slashing period ended before infraction: infraction height %d, slashing period ended at %d", infractionHeight, slashingPeriod.EndHeight)) + } + + // Calculate the total slash amount totalToSlash := sdk.MaxDec(slashingPeriod.SlashedSoFar, fraction) // Calculate remainder @@ -36,9 +43,6 @@ func (k Keeper) getValidatorSlashingPeriodForHeight(ctx sdk.Context, address sdk panic("expected to find slashing period, but none was found") } slashingPeriod = k.unmarshalSlashingPeriodKeyValue(iterator.Key(), iterator.Value()) - if slashingPeriod.EndHeight > 0 && slashingPeriod.EndHeight < height { - panic(fmt.Sprintf("slashing period ended before infraction: infraction height %d, slashing period ended at %d", height, slashingPeriod.EndHeight)) - } return } diff --git a/x/slashing/slashing_period_test.go b/x/slashing/slashing_period_test.go index 3e3edfaf54f2..2adc5b75a3e5 100644 --- a/x/slashing/slashing_period_test.go +++ b/x/slashing/slashing_period_test.go @@ -31,7 +31,7 @@ func TestGetSetValidatorSlashingPeriod(t *testing.T) { // Get after end height (panic) newPeriod.EndHeight = int64(4) keeper.setValidatorSlashingPeriod(ctx, newPeriod) - require.Panics(t, func() { keeper.getValidatorSlashingPeriodForHeight(ctx, addr, height) }) + require.Panics(t, func() { keeper.capBySlashingPeriod(ctx, addr, sdk.ZeroDec(), height) }) // Back to old end height newPeriod.EndHeight = height + 10 keeper.setValidatorSlashingPeriod(ctx, newPeriod)