Merge PR #3189: Reset slashing periods on export

* Reset slashing periods on export
* Rename old slashing period setter to SetValidatorSlashingPeriod
* Update export to reset slashing periods
* Fix cons address
* Remove old slashing period reset
* Add pending log entry
* Update slashing period tests
This commit is contained in:
Alexander Bezobchuk 2019-01-02 18:57:13 -05:00 committed by Christopher Goes
parent 990f3ab4c1
commit 15bd1f668f
6 changed files with 39 additions and 17 deletions

View File

@ -84,6 +84,8 @@ BUG FIXES
* \#3181 Correctly reset total accum update height and jailed-validator bond height / unbonding height on export-for-zero-height
* [\#3172](https://github.com/cosmos/cosmos-sdk/pull/3172) Fix parsing `gaiad.toml`
when it already exists.
* [#3187](https://github.com/cosmos/cosmos-sdk/issues/3187) Fix `gaiad export`
by resetting each validator's slashing period.
* SDK

View File

@ -128,32 +128,52 @@ func (app *GaiaApp) prepForZeroHeightGenesis(ctx sdk.Context) {
return false
})
// iterate through validators by power descending, reset bond height, update bond intra-tx counter
// Iterate through validators by power descending, reset bond heights, and
// update bond intra-tx counters.
store := ctx.KVStore(app.keyStake)
iter := sdk.KVStoreReversePrefixIterator(store, stake.ValidatorsKey)
counter := int16(0)
var valConsAddrs []sdk.ConsAddress
for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(iter.Key()[1:])
validator, found := app.stakeKeeper.GetValidator(ctx, addr)
if !found {
panic("expected validator, not found")
}
validator.BondHeight = 0
validator.UnbondingHeight = 0
valConsAddrs = append(valConsAddrs, validator.ConsAddress())
app.stakeKeeper.SetValidator(ctx, validator)
counter++
}
iter.Close()
/* Handle slashing state. */
// we have to clear the slashing periods, since they reference heights
// remove all existing slashing periods and recreate one for each validator
app.slashingKeeper.DeleteValidatorSlashingPeriods(ctx)
for _, valConsAddr := range valConsAddrs {
sp := slashing.ValidatorSlashingPeriod{
ValidatorAddr: valConsAddr,
StartHeight: 0,
EndHeight: 0,
SlashedSoFar: sdk.ZeroDec(),
}
app.slashingKeeper.SetValidatorSlashingPeriod(ctx, sp)
}
// reset start height on signing infos
app.slashingKeeper.IterateValidatorSigningInfos(ctx, func(addr sdk.ConsAddress, info slashing.ValidatorSigningInfo) (stop bool) {
info.StartHeight = 0
app.slashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
return false
})
app.slashingKeeper.IterateValidatorSigningInfos(
ctx,
func(addr sdk.ConsAddress, info slashing.ValidatorSigningInfo) (stop bool) {
info.StartHeight = 0
app.slashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
return false
},
)
}

View File

@ -98,7 +98,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState, sdata types.
}
for _, slashingPeriod := range data.SlashingPeriods {
keeper.addOrUpdateValidatorSlashingPeriod(ctx, slashingPeriod)
keeper.SetValidatorSlashingPeriod(ctx, slashingPeriod)
}
keeper.paramspace.SetParamSet(ctx, &data.Params)

View File

@ -28,14 +28,14 @@ func (k Keeper) onValidatorBonded(ctx sdk.Context, address sdk.ConsAddress, _ sd
EndHeight: 0,
SlashedSoFar: sdk.ZeroDec(),
}
k.addOrUpdateValidatorSlashingPeriod(ctx, slashingPeriod)
k.SetValidatorSlashingPeriod(ctx, slashingPeriod)
}
// Mark the slashing period as having ended when a validator begins unbonding
func (k Keeper) onValidatorBeginUnbonding(ctx sdk.Context, address sdk.ConsAddress, _ sdk.ValAddress) {
slashingPeriod := k.getValidatorSlashingPeriodForHeight(ctx, address, ctx.BlockHeight())
slashingPeriod.EndHeight = ctx.BlockHeight()
k.addOrUpdateValidatorSlashingPeriod(ctx, slashingPeriod)
k.SetValidatorSlashingPeriod(ctx, slashingPeriod)
}
// When a validator is created, add the address-pubkey relation.

View File

@ -28,7 +28,7 @@ func (k Keeper) capBySlashingPeriod(ctx sdk.Context, address sdk.ConsAddress, fr
// Update the slashing period struct
slashingPeriod.SlashedSoFar = totalToSlash
k.addOrUpdateValidatorSlashingPeriod(ctx, slashingPeriod)
k.SetValidatorSlashingPeriod(ctx, slashingPeriod)
return
}
@ -80,7 +80,7 @@ func (k Keeper) DeleteValidatorSlashingPeriods(ctx sdk.Context) {
// This function sets a validator slashing period for a particular validator,
// start height, end height, and current slashed-so-far total, or updates
// an existing slashing period for the same validator and start height.
func (k Keeper) addOrUpdateValidatorSlashingPeriod(ctx sdk.Context, slashingPeriod ValidatorSlashingPeriod) {
func (k Keeper) SetValidatorSlashingPeriod(ctx sdk.Context, slashingPeriod ValidatorSlashingPeriod) {
slashingPeriodValue := ValidatorSlashingPeriodValue{
EndHeight: slashingPeriod.EndHeight,
SlashedSoFar: slashingPeriod.SlashedSoFar,

View File

@ -19,7 +19,7 @@ func TestGetSetValidatorSlashingPeriod(t *testing.T) {
EndHeight: height + 10,
SlashedSoFar: sdk.ZeroDec(),
}
keeper.addOrUpdateValidatorSlashingPeriod(ctx, newPeriod)
keeper.SetValidatorSlashingPeriod(ctx, newPeriod)
// Get at start height
retrieved := keeper.getValidatorSlashingPeriodForHeight(ctx, addr, height)
@ -34,12 +34,12 @@ func TestGetSetValidatorSlashingPeriod(t *testing.T) {
// Get after end height (panic)
newPeriod.EndHeight = int64(4)
keeper.addOrUpdateValidatorSlashingPeriod(ctx, newPeriod)
keeper.SetValidatorSlashingPeriod(ctx, newPeriod)
require.Panics(t, func() { keeper.capBySlashingPeriod(ctx, addr, sdk.ZeroDec(), height) })
// Back to old end height
newPeriod.EndHeight = height + 10
keeper.addOrUpdateValidatorSlashingPeriod(ctx, newPeriod)
keeper.SetValidatorSlashingPeriod(ctx, newPeriod)
// Set a new, later period
anotherPeriod := ValidatorSlashingPeriod{
@ -48,7 +48,7 @@ func TestGetSetValidatorSlashingPeriod(t *testing.T) {
EndHeight: height + 11,
SlashedSoFar: sdk.ZeroDec(),
}
keeper.addOrUpdateValidatorSlashingPeriod(ctx, anotherPeriod)
keeper.SetValidatorSlashingPeriod(ctx, anotherPeriod)
// Old period retrieved for prior height
retrieved = keeper.getValidatorSlashingPeriodForHeight(ctx, addr, height)
@ -69,7 +69,7 @@ func TestValidatorSlashingPeriodCap(t *testing.T) {
EndHeight: height + 10,
SlashedSoFar: sdk.ZeroDec(),
}
keeper.addOrUpdateValidatorSlashingPeriod(ctx, newPeriod)
keeper.SetValidatorSlashingPeriod(ctx, newPeriod)
half := sdk.NewDec(1).Quo(sdk.NewDec(2))
// First slash should be full