Skip to content

Commit

Permalink
Panic in the right place
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Aug 27, 2018
1 parent 527bbbd commit 6ed552a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions x/slashing/slashing_period.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion x/slashing/slashing_period_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6ed552a

Please sign in to comment.