From 15bd1f668f241e022925cec8de79f1696f7e3a0c Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Wed, 2 Jan 2019 18:57:13 -0500 Subject: [PATCH] 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 --- PENDING.md | 2 ++ cmd/gaia/app/export.go | 34 ++++++++++++++++++++++++------ x/slashing/genesis.go | 2 +- x/slashing/hooks.go | 4 ++-- x/slashing/slashing_period.go | 4 ++-- x/slashing/slashing_period_test.go | 10 ++++----- 6 files changed, 39 insertions(+), 17 deletions(-) diff --git a/PENDING.md b/PENDING.md index 4c383bc347a7..09ed557339b7 100644 --- a/PENDING.md +++ b/PENDING.md @@ -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 diff --git a/cmd/gaia/app/export.go b/cmd/gaia/app/export.go index 9be094a75ff6..ec2e83b2e720 100644 --- a/cmd/gaia/app/export.go +++ b/cmd/gaia/app/export.go @@ -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 + }, + ) } diff --git a/x/slashing/genesis.go b/x/slashing/genesis.go index fa4abac2f8c4..aa126f56a5dd 100644 --- a/x/slashing/genesis.go +++ b/x/slashing/genesis.go @@ -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) diff --git a/x/slashing/hooks.go b/x/slashing/hooks.go index bb7e413242b3..26770da49ace 100644 --- a/x/slashing/hooks.go +++ b/x/slashing/hooks.go @@ -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. diff --git a/x/slashing/slashing_period.go b/x/slashing/slashing_period.go index e726453dc213..ee2c135875ff 100644 --- a/x/slashing/slashing_period.go +++ b/x/slashing/slashing_period.go @@ -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, diff --git a/x/slashing/slashing_period_test.go b/x/slashing/slashing_period_test.go index bd12ef127396..522dcdf787e3 100644 --- a/x/slashing/slashing_period_test.go +++ b/x/slashing/slashing_period_test.go @@ -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