Skip to content

Commit

Permalink
Remove jailed validators from the power store entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Oct 2, 2018
1 parent dbd2c0a commit 54d5ce8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
1 change: 1 addition & 0 deletions x/stake/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func TestValidatorByPowerIndex(t *testing.T) {
require.True(t, found)
require.Equal(t, sdk.Unbonding, validator.Status) // ensure is unbonding
require.Equal(t, int64(500000), validator.Tokens.RoundInt64()) // ensure tokens slashed
keeper.Unjail(ctx, consAddr0)

// the old power record should have been deleted as the power changed
require.False(t, keep.ValidatorByPowerIndexExists(ctx, keeper, power))
Expand Down
9 changes: 7 additions & 2 deletions x/stake/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func TestUndelegateSelfDelegation(t *testing.T) {
keeper.SetDelegation(ctx, selfDelegation)

// create a second delegation to this validator
keeper.DeleteValidatorByPowerIndex(ctx, validator, pool)
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
Expand Down Expand Up @@ -283,6 +284,7 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) {
keeper.SetDelegation(ctx, selfDelegation)

// create a second delegation to this validator
keeper.DeleteValidatorByPowerIndex(ctx, validator, pool)
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
Expand Down Expand Up @@ -359,6 +361,7 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) {
keeper.SetDelegation(ctx, selfDelegation)

// create a second delegation to this validator
keeper.DeleteValidatorByPowerIndex(ctx, validator, pool)
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
Expand Down Expand Up @@ -586,6 +589,7 @@ func TestRedelegateFromUnbondingValidator(t *testing.T) {
keeper.SetDelegation(ctx, selfDelegation)

// create a second delegation to this validator
keeper.DeleteValidatorByPowerIndex(ctx, validator, pool)
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
require.Equal(t, int64(10), issuedShares.RoundInt64())
keeper.SetPool(ctx, pool)
Expand Down Expand Up @@ -619,7 +623,7 @@ func TestRedelegateFromUnbondingValidator(t *testing.T) {

// end block
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
require.Equal(t, 2, len(updates))
require.Equal(t, 1, len(updates))

validator, found := keeper.GetValidator(ctx, addrVals[0])
require.True(t, found)
Expand Down Expand Up @@ -669,6 +673,7 @@ func TestRedelegateFromUnbondedValidator(t *testing.T) {
keeper.SetDelegation(ctx, selfDelegation)

// create a second delegation to this validator
keeper.DeleteValidatorByPowerIndex(ctx, validator, pool)
validator, pool, issuedShares = validator.AddTokensFromDel(pool, sdk.NewInt(10))
validator.BondIntraTxCounter = 1
require.Equal(t, int64(10), issuedShares.RoundInt64())
Expand Down Expand Up @@ -704,7 +709,7 @@ func TestRedelegateFromUnbondedValidator(t *testing.T) {

// end block
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
require.Equal(t, 2, len(updates))
require.Equal(t, 1, len(updates))

validator, found := keeper.GetValidator(ctx, addrVals[0])
require.True(t, found)
Expand Down
10 changes: 1 addition & 9 deletions x/stake/keeper/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,14 @@ func getValidatorPowerRank(validator types.Validator, pool types.Pool) []byte {
potentialPower := validator.Tokens
powerBytes := []byte(potentialPower.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first)

jailedBytes := make([]byte, 1)
if validator.Jailed {
jailedBytes[0] = byte(0x00)
} else {
jailedBytes[0] = byte(0x01)
}

// heightBytes and counterBytes represent strings like powerBytes does
heightBytes := make([]byte, binary.MaxVarintLen64)
binary.BigEndian.PutUint64(heightBytes, ^uint64(validator.BondHeight)) // invert height (older validators first)
counterBytes := make([]byte, 2)
binary.BigEndian.PutUint16(counterBytes, ^uint16(validator.BondIntraTxCounter)) // invert counter (first txns have priority)

return append(append(append(append(
return append(append(append(
ValidatorsByPowerIndexKey,
jailedBytes...),
powerBytes...),
heightBytes...),
counterBytes...)
Expand Down
20 changes: 8 additions & 12 deletions x/stake/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab
operator := sdk.ValAddress(iterator.Value())
validator := k.mustGetValidator(ctx, operator)

// jailed validators are ranked last, so if we get to a jailed validator
// we have no more bonded validators
// TODO we can remove this if we remove jailed validators from the power store
// likewise for zero-power validators, which we never bond
if validator.Jailed || validator.Tokens.Equal(sdk.ZeroDec()) {
if validator.Jailed {
panic("should never retrieve a jailed validator from the power store")
}

// if we get to a zero-power validator (which we don't bond),
// there are no more possible bonded validators
if validator.Tokens.Equal(sdk.ZeroDec()) {
break
}

Expand Down Expand Up @@ -140,12 +142,9 @@ func (k Keeper) JailValidator(ctx sdk.Context, validator types.Validator) {
}

pool := k.GetPool(ctx)
k.DeleteValidatorByPowerIndex(ctx, validator, pool)
validator.Jailed = true
k.SetValidator(ctx, validator)
k.SetValidatorByPowerIndex(ctx, validator, pool)

// TODO we should be able to just delete the index, and only set it again once unjailed
k.DeleteValidatorByPowerIndex(ctx, validator, pool)
}

// remove a validator from jail
Expand All @@ -155,14 +154,11 @@ func (k Keeper) UnjailValidator(ctx sdk.Context, validator types.Validator) {
}

pool := k.GetPool(ctx)
k.DeleteValidatorByPowerIndex(ctx, validator, pool)
validator.Jailed = false
k.SetValidator(ctx, validator)
k.SetValidatorByPowerIndex(ctx, validator, pool)
}

//________________________________________________________________________________________________

// perform all the store operations for when a validator status becomes bonded
func (k Keeper) bondValidator(ctx sdk.Context, validator types.Validator) types.Validator {

Expand Down
4 changes: 4 additions & 0 deletions x/stake/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func (k Keeper) SetValidatorByConsAddr(ctx sdk.Context, validator types.Validato

// validator index
func (k Keeper) SetValidatorByPowerIndex(ctx sdk.Context, validator types.Validator, pool types.Pool) {
// jailed validators are not kept in the power index
if validator.Jailed {
return
}
store := ctx.KVStore(k.storeKey)
store.Set(GetBondedValidatorsByPowerIndexKey(validator, pool), validator.OperatorAddr)
}
Expand Down

0 comments on commit 54d5ce8

Please sign in to comment.