diff --git a/x/stake/keeper.go b/x/stake/keeper.go index f6aa01c63..8cce331cf 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -94,12 +94,14 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) { // update the list ordered by voting power if oldFound { + // retain the old validator height, even though stake has changed + candidate.ValidatorHeight = oldCandidate.ValidatorHeight store.Delete(GetValidatorKey(address, oldCandidate.Assets, oldCandidate.ValidatorHeight, k.cdc)) + } else { + // update the validator block height + candidate.ValidatorHeight = ctx.BlockHeight() } - // update the validator block height - candidate.ValidatorHeight = ctx.BlockHeight() - // update the candidate record bz, err = k.cdc.MarshalBinary(candidate) if err != nil { diff --git a/x/stake/keeper_test.go b/x/stake/keeper_test.go index baaa2ec8c..85b2d72ba 100644 --- a/x/stake/keeper_test.go +++ b/x/stake/keeper_test.go @@ -254,7 +254,7 @@ func TestGetValidators(t *testing.T) { assert.Equal(t, candidates[4].Address, validators[0].Address, "%v", validators) assert.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) assert.Equal(t, int64(0), validators[0].Height, "%v", validators) - assert.Equal(t, int64(10), validators[1].Height, "%v", validators) + assert.Equal(t, int64(0), validators[1].Height, "%v", validators) // no change in voting power - no change in sort ctx = ctx.WithBlockHeight(20) @@ -274,8 +274,8 @@ func TestGetValidators(t *testing.T) { keeper.setCandidate(ctx, candidates[4]) validators = keeper.GetValidators(ctx) require.Equal(t, len(validators), n, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) - assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) + assert.Equal(t, candidates[4].Address, validators[0].Address, "%v", validators) + assert.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) // reset assets / heights candidates[3].Assets = sdk.NewRat(300)