From f9877508e5f2af5d3e5d0ec235502cdbbddd70d1 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Mon, 15 Oct 2018 23:58:39 +0200 Subject: [PATCH] Address @rigelrozanski comments --- x/slashing/keeper.go | 9 +++++---- x/slashing/keeper_test.go | 13 +++++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/x/slashing/keeper.go b/x/slashing/keeper.go index 73734198d..861391ac2 100644 --- a/x/slashing/keeper.go +++ b/x/slashing/keeper.go @@ -112,16 +112,17 @@ func (k Keeper) handleValidatorSignature(ctx sdk.Context, addr crypto.Address, p // That way we avoid needing to read/write the whole array each time previous := k.getValidatorMissedBlockBitArray(ctx, consAddr, index) missed := !signed - if previous == missed { - // Array value at this index has not changed, no need to update counter - } else if !previous && missed { + switch { + case !previous && missed: // Array value has changed from not missed to missed, increment counter k.setValidatorMissedBlockBitArray(ctx, consAddr, index, true) signInfo.MissedBlocksCounter++ - } else if previous && !missed { + case previous && !missed: // Array value has changed from missed to not missed, decrement counter k.setValidatorMissedBlockBitArray(ctx, consAddr, index, false) signInfo.MissedBlocksCounter-- + default: + // Array value at this index has not changed, no need to update counter } if missed { diff --git a/x/slashing/keeper_test.go b/x/slashing/keeper_test.go index 0af3936b2..6ad5c3792 100644 --- a/x/slashing/keeper_test.go +++ b/x/slashing/keeper_test.go @@ -375,6 +375,7 @@ func TestHandleAlreadyJailed(t *testing.T) { func TestValidatorDippingInAndOut(t *testing.T) { // initial setup + // keeperTestParams set the SignedBlocksWindow to 1000 and MaxMissedBlocksPerWindow to 500 ctx, _, sk, _, keeper := createTestInput(t, keeperTestParams()) params := sk.GetParams(ctx) params.MaxValidators = 1 @@ -426,8 +427,9 @@ func TestValidatorDippingInAndOut(t *testing.T) { validator, _ = sk.GetValidator(ctx, addr) require.Equal(t, sdk.Bonded, validator.Status) - // validator misses 501 blocks - for ; height < int64(1201); height++ { + // validator misses 500 more blocks, 501 total + latest := height + for ; height < latest+500; height++ { ctx = ctx.WithBlockHeight(height) keeper.handleValidatorSignature(ctx, val.Address(), newAmt, false) } @@ -437,6 +439,7 @@ func TestValidatorDippingInAndOut(t *testing.T) { validator, _ = sk.GetValidator(ctx, addr) require.Equal(t, sdk.Unbonding, validator.Status) + // check all the signing information signInfo, found := keeper.getValidatorSigningInfo(ctx, consAddr) require.True(t, found) require.Equal(t, int64(0), signInfo.MissedBlocksCounter) @@ -454,14 +457,16 @@ func TestValidatorDippingInAndOut(t *testing.T) { // validator rejoins and starts signing again sk.Unjail(ctx, consAddr) keeper.handleValidatorSignature(ctx, val.Address(), newAmt, true) + height++ - // validator should not be kicked + // validator should not be kicked since we reset counter/array when it was jailed stake.EndBlocker(ctx, sk) validator, _ = sk.GetValidator(ctx, addr) require.Equal(t, sdk.Bonded, validator.Status) // validator misses 501 blocks - for height = int64(5001); height < int64(5503); height++ { + latest = height + for ; height < latest+501; height++ { ctx = ctx.WithBlockHeight(height) keeper.handleValidatorSignature(ctx, val.Address(), newAmt, false) }