-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #2122: Implement slashing period
* Update PENDING.md * SlashingPeriod struct * Seperate keys.go, constant prefixes * Make linter happy * Update Gopkg.lock * Seek slashing period by infraction height * Slashing period hooks * Slashing period unit tests; bugfix * Add simple hook tests * Add sdk.ValidatorHooks interface * No-op hooks * Real hooks * Fix iteration direction & duplicate key, update Gaia * Correctly simulate past validator set signatures * Tiny rename * Update dep; 'make format' * Add quick slashing period functionality test * Additional unit tests * Use current validators when selected * Panic in the right place * Address @rigelrozanski comments * Fix linter errors * Address @melekes suggestion * Rename hook * Update for new bech32 types * 'make format'
- Loading branch information
1 parent
e1981d4
commit 1204857
Showing
25 changed files
with
494 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package slashing | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// Create a new slashing period when a validator is bonded | ||
func (k Keeper) onValidatorBonded(ctx sdk.Context, address sdk.ConsAddress) { | ||
slashingPeriod := ValidatorSlashingPeriod{ | ||
ValidatorAddr: address, | ||
StartHeight: ctx.BlockHeight(), | ||
EndHeight: 0, | ||
SlashedSoFar: sdk.ZeroDec(), | ||
} | ||
k.addOrUpdateValidatorSlashingPeriod(ctx, slashingPeriod) | ||
} | ||
|
||
// Mark the slashing period as having ended when a validator begins unbonding | ||
func (k Keeper) onValidatorBeginUnbonding(ctx sdk.Context, address sdk.ConsAddress) { | ||
slashingPeriod := k.getValidatorSlashingPeriodForHeight(ctx, address, ctx.BlockHeight()) | ||
slashingPeriod.EndHeight = ctx.BlockHeight() | ||
k.addOrUpdateValidatorSlashingPeriod(ctx, slashingPeriod) | ||
} | ||
|
||
// Wrapper struct for sdk.ValidatorHooks | ||
type ValidatorHooks struct { | ||
k Keeper | ||
} | ||
|
||
// Assert implementation | ||
var _ sdk.ValidatorHooks = ValidatorHooks{} | ||
|
||
// Return a sdk.ValidatorHooks interface over the wrapper struct | ||
func (k Keeper) ValidatorHooks() sdk.ValidatorHooks { | ||
return ValidatorHooks{k} | ||
} | ||
|
||
// Implements sdk.ValidatorHooks | ||
func (v ValidatorHooks) OnValidatorBonded(ctx sdk.Context, address sdk.ConsAddress) { | ||
v.k.onValidatorBonded(ctx, address) | ||
} | ||
|
||
// Implements sdk.ValidatorHooks | ||
func (v ValidatorHooks) OnValidatorBeginUnbonding(ctx sdk.Context, address sdk.ConsAddress) { | ||
v.k.onValidatorBeginUnbonding(ctx, address) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package slashing | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
func TestHookOnValidatorBonded(t *testing.T) { | ||
ctx, _, _, _, keeper := createTestInput(t) | ||
addr := sdk.ConsAddress(addrs[0]) | ||
keeper.onValidatorBonded(ctx, addr) | ||
period := keeper.getValidatorSlashingPeriodForHeight(ctx, addr, ctx.BlockHeight()) | ||
require.Equal(t, ValidatorSlashingPeriod{addr, ctx.BlockHeight(), 0, sdk.ZeroDec()}, period) | ||
} | ||
|
||
func TestHookOnValidatorBeginUnbonding(t *testing.T) { | ||
ctx, _, _, _, keeper := createTestInput(t) | ||
addr := sdk.ConsAddress(addrs[0]) | ||
keeper.onValidatorBonded(ctx, addr) | ||
keeper.onValidatorBeginUnbonding(ctx, addr) | ||
period := keeper.getValidatorSlashingPeriodForHeight(ctx, addr, ctx.BlockHeight()) | ||
require.Equal(t, ValidatorSlashingPeriod{addr, ctx.BlockHeight(), ctx.BlockHeight(), sdk.ZeroDec()}, period) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.