Skip to content

Commit

Permalink
Merge PR #3189: Reset slashing periods on export
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alexanderbez authored and cwgoes committed Jan 2, 2019
1 parent 990f3ab commit 15bd1f6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
2 changes: 2 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 27 additions & 7 deletions cmd/gaia/app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
)
}
2 changes: 1 addition & 1 deletion x/slashing/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/slashing_period.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions x/slashing/slashing_period_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 15bd1f6

Please sign in to comment.