Skip to content

Commit

Permalink
Merge PR cosmos#4717: update x/slashing to match module spec
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze authored and alexanderbez committed Jul 19, 2019
1 parent 72ce6df commit 52edb03
Show file tree
Hide file tree
Showing 40 changed files with 567 additions and 495 deletions.
1 change: 1 addition & 0 deletions .pending/improvements/sdk/4717-refactor-slashi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4717 refactor `x/slashing` to match the new module spec
2 changes: 1 addition & 1 deletion docs/spec/slashing/03_messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ handleMsgUnjail(tx TxUnjail)
if !validator.Jailed
fail with "Validator not jailed, cannot unjail"
info = getValidatorSigningInfo(operator)
info = GetValidatorSigningInfo(operator)
if info.Tombstoned
fail with "Tombstoned validator cannot be unjailed"
if block time < info.JailedUntil
Expand Down
2 changes: 1 addition & 1 deletion docs/spec/slashing/05_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ now-bonded validator, which `StartHeight` of the current block.
```
onValidatorBonded(address sdk.ValAddress)
signingInfo, found = getValidatorSigningInfo(address)
signingInfo, found = GetValidatorSigningInfo(address)
if !found {
signingInfo = ValidatorSigningInfo {
StartHeight : CurrentHeight,
Expand Down
1 change: 1 addition & 0 deletions x/params/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (

var (
// functions aliases
NewParamSetPair = subspace.NewParamSetPair
NewSubspace = subspace.NewSubspace
NewKeyTable = subspace.NewKeyTable
DefaultTestComponents = subspace.DefaultTestComponents
Expand Down
11 changes: 8 additions & 3 deletions x/params/subspace/paramset.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package subspace

// Used for associating paramsubspace key and field of param structs
// ParamSetPair is used for associating paramsubspace key and field of param structs
type ParamSetPair struct {
Key []byte
Value interface{}
}

// Slice of KeyFieldPair
// NewParamSetPair creates a new ParamSetPair instance
func NewParamSetPair(key []byte, value interface{}) ParamSetPair {
return ParamSetPair{key, value}
}

// ParamSetPairs Slice of KeyFieldPair
type ParamSetPairs []ParamSetPair

// Interface for structs containing parameters for a module
// ParamSet defines an interface for structs containing parameters for a module
type ParamSet interface {
ParamSetPairs() ParamSetPairs
}
11 changes: 6 additions & 5 deletions x/slashing/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// slashing begin block functionality
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, sk Keeper) {
// BeginBlocker check for infraction evidence or downtime of validators
// on every begin block
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {
// Iterate over all the validators which *should* have signed this block
// store whether or not they have actually signed it and slash/unbond any
// which have missed too many blocks in a row (downtime slashing)
for _, voteInfo := range req.LastCommitInfo.GetVotes() {
sk.HandleValidatorSignature(ctx, voteInfo.Validator.Address, voteInfo.Validator.Power, voteInfo.SignedLastBlock)
k.HandleValidatorSignature(ctx, voteInfo.Validator.Address, voteInfo.Validator.Power, voteInfo.SignedLastBlock)
}

// Iterate through any newly discovered evidence of infraction
Expand All @@ -24,9 +25,9 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, sk Keeper) {
for _, evidence := range req.ByzantineValidators {
switch evidence.Type {
case tmtypes.ABCIEvidenceTypeDuplicateVote:
sk.HandleDoubleSign(ctx, evidence.Validator.Address, evidence.Height, evidence.Time, evidence.Validator.Power)
k.HandleDoubleSign(ctx, evidence.Validator.Address, evidence.Height, evidence.Time, evidence.Validator.Power)
default:
sk.Logger(ctx).Error(fmt.Sprintf("ignored unknown evidence type: %s", evidence.Type))
k.Logger(ctx).Error(fmt.Sprintf("ignored unknown evidence type: %s", evidence.Type))
}
}
}
11 changes: 6 additions & 5 deletions x/slashing/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import (
abci "github.com/tendermint/tendermint/abci/types"

sdk "github.com/cosmos/cosmos-sdk/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/staking"
)

func TestBeginBlocker(t *testing.T) {
ctx, ck, sk, _, keeper := createTestInput(t, DefaultParams())
ctx, ck, sk, _, keeper := slashingkeeper.CreateTestInput(t, DefaultParams())
power := int64(100)
amt := sdk.TokensFromConsensusPower(power)
addr, pk := addrs[2], pks[2]
addr, pk := slashingkeeper.Addrs[2], slashingkeeper.Pks[2]

// bond the validator
got := staking.NewHandler(sk)(ctx, NewTestMsgCreateValidator(addr, pk, amt))
got := staking.NewHandler(sk)(ctx, slashingkeeper.NewTestMsgCreateValidator(addr, pk, amt))
require.True(t, got.IsOK())
staking.EndBlocker(ctx, sk)
require.Equal(
t, ck.GetCoins(ctx, sdk.AccAddress(addr)),
sdk.NewCoins(sdk.NewCoin(sk.GetParams(ctx).BondDenom, initTokens.Sub(amt))),
sdk.NewCoins(sdk.NewCoin(sk.GetParams(ctx).BondDenom, slashingkeeper.InitTokens.Sub(amt))),
)
require.Equal(t, amt, sk.Validator(ctx, addr).GetBondedTokens())

Expand All @@ -44,7 +45,7 @@ func TestBeginBlocker(t *testing.T) {
}
BeginBlocker(ctx, req, keeper)

info, found := keeper.getValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
info, found := keeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
require.True(t, found)
require.Equal(t, ctx.BlockHeight(), info.StartHeight)
require.Equal(t, int64(1), info.IndexOffset)
Expand Down
17 changes: 12 additions & 5 deletions x/slashing/alias.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// nolint
// autogenerated code using github.com/rigelrozanski/multitool
// aliases generated for the following subdirectories:
// ALIASGEN: github.com/cosmos/cosmos-sdk/x/slashing/types
// ALIASGEN: github.com/cosmos/cosmos-sdk/x/slashing/internal/keeper
// ALIASGEN: github.com/cosmos/cosmos-sdk/x/slashing/internal/types
package slashing

import (
"github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/slashing/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing/internal/types"
)

const (
Expand All @@ -20,17 +22,19 @@ const (
StoreKey = types.StoreKey
RouterKey = types.RouterKey
QuerierRoute = types.QuerierRoute
QueryParameters = types.QueryParameters
QuerySigningInfo = types.QuerySigningInfo
QuerySigningInfos = types.QuerySigningInfos
DefaultParamspace = types.DefaultParamspace
DefaultMaxEvidenceAge = types.DefaultMaxEvidenceAge
DefaultSignedBlocksWindow = types.DefaultSignedBlocksWindow
DefaultDowntimeJailDuration = types.DefaultDowntimeJailDuration
QueryParameters = types.QueryParameters
QuerySigningInfo = types.QuerySigningInfo
QuerySigningInfos = types.QuerySigningInfos
)

var (
// functions aliases
NewKeeper = keeper.NewKeeper
NewQuerier = keeper.NewQuerier
RegisterCodec = types.RegisterCodec
ErrNoValidatorForAddress = types.ErrNoValidatorForAddress
ErrBadValidatorAddr = types.ErrBadValidatorAddr
Expand All @@ -40,6 +44,7 @@ var (
ErrSelfDelegationTooLowToUnjail = types.ErrSelfDelegationTooLowToUnjail
ErrNoSigningInfoFound = types.ErrNoSigningInfoFound
NewGenesisState = types.NewGenesisState
NewMissedBlock = types.NewMissedBlock
DefaultGenesisState = types.DefaultGenesisState
ValidateGenesis = types.ValidateGenesis
GetValidatorSigningInfoKey = types.GetValidatorSigningInfoKey
Expand Down Expand Up @@ -73,6 +78,8 @@ var (
)

type (
Hooks = keeper.Hooks
Keeper = keeper.Keeper
CodeType = types.CodeType
GenesisState = types.GenesisState
MissedBlock = types.MissedBlock
Expand Down
4 changes: 3 additions & 1 deletion x/slashing/app_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// nolint
// DONTCOVER
package slashing

import (
Expand Down Expand Up @@ -97,7 +99,7 @@ func checkValidator(t *testing.T, mapp *mock.App, keeper staking.Keeper,
func checkValidatorSigningInfo(t *testing.T, mapp *mock.App, keeper Keeper,
addr sdk.ConsAddress, expFound bool) ValidatorSigningInfo {
ctxCheck := mapp.BaseApp.NewContext(true, abci.Header{})
signingInfo, found := keeper.getValidatorSigningInfo(ctxCheck, addr)
signingInfo, found := keeper.GetValidatorSigningInfo(ctxCheck, addr)
require.Equal(t, expFound, found)
return signingInfo
}
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/slashing/internal/types"
)

// GetQueryCmd returns the cli query commands for this module
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/slashing/internal/types"
)

// GetTxCmd returns the transaction commands for this module
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/slashing/internal/types"
)

func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/slashing/internal/types"
)

func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
Expand Down
20 changes: 7 additions & 13 deletions x/slashing/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package slashing

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/slashing/internal/types"
"github.com/cosmos/cosmos-sdk/x/staking/exported"
)

Expand All @@ -11,7 +11,7 @@ import (
func InitGenesis(ctx sdk.Context, keeper Keeper, stakingKeeper types.StakingKeeper, data types.GenesisState) {
stakingKeeper.IterateValidators(ctx,
func(index int64, validator exported.ValidatorI) bool {
keeper.addPubkey(ctx, validator.GetConsPubKey())
keeper.AddPubkey(ctx, validator.GetConsPubKey())
return false
},
)
Expand All @@ -30,20 +30,18 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, stakingKeeper types.StakingKeep
panic(err)
}
for _, missed := range array {
keeper.setValidatorMissedBlockBitArray(ctx, address, missed.Index, missed.Missed)
keeper.SetValidatorMissedBlockBitArray(ctx, address, missed.Index, missed.Missed)
}
}

keeper.paramspace.SetParamSet(ctx, &data.Params)
keeper.SetParams(ctx, data.Params)
}

// ExportGenesis writes the current store values
// to a genesis file, which can be imported again
// with InitGenesis
func ExportGenesis(ctx sdk.Context, keeper Keeper) (data types.GenesisState) {
var params types.Params
keeper.paramspace.GetParamSet(ctx, &params)

params := keeper.GetParams(ctx)
signingInfos := make(map[string]types.ValidatorSigningInfo)
missedBlocks := make(map[string][]types.MissedBlock)
keeper.IterateValidatorSigningInfos(ctx, func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) {
Expand All @@ -52,17 +50,13 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) (data types.GenesisState) {
localMissedBlocks := []types.MissedBlock{}

keeper.IterateValidatorMissedBlockBitArray(ctx, address, func(index int64, missed bool) (stop bool) {
localMissedBlocks = append(localMissedBlocks, types.MissedBlock{index, missed})
localMissedBlocks = append(localMissedBlocks, types.NewMissedBlock(index, missed))
return false
})
missedBlocks[bechAddr] = localMissedBlocks

return false
})

return types.GenesisState{
Params: params,
SigningInfos: signingInfos,
MissedBlocks: missedBlocks,
}
return types.NewGenesisState(params, signingInfos, missedBlocks)
}
44 changes: 6 additions & 38 deletions x/slashing/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/slashing/internal/types"
)

// NewHandler creates an sdk.Handler for all the slashing type messages
func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
ctx = ctx.WithEventManager(sdk.NewEventManager())
Expand All @@ -25,45 +26,12 @@ func NewHandler(k Keeper) sdk.Handler {
// Validators must submit a transaction to unjail itself after
// having been jailed (and thus unbonded) for downtime
func handleMsgUnjail(ctx sdk.Context, msg MsgUnjail, k Keeper) sdk.Result {
validator := k.sk.Validator(ctx, msg.ValidatorAddr)
if validator == nil {
return ErrNoValidatorForAddress(k.codespace).Result()

err := k.Unjail(ctx, msg.ValidatorAddr)
if err != nil {
return err.Result()
}

// cannot be unjailed if no self-delegation exists
selfDel := k.sk.Delegation(ctx, sdk.AccAddress(msg.ValidatorAddr), msg.ValidatorAddr)
if selfDel == nil {
return ErrMissingSelfDelegation(k.codespace).Result()
}

if validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
return ErrSelfDelegationTooLowToUnjail(k.codespace).Result()
}

// cannot be unjailed if not jailed
if !validator.IsJailed() {
return ErrValidatorNotJailed(k.codespace).Result()
}

consAddr := sdk.ConsAddress(validator.GetConsPubKey().Address())

info, found := k.getValidatorSigningInfo(ctx, consAddr)
if !found {
return ErrNoValidatorForAddress(k.codespace).Result()
}

// cannot be unjailed if tombstoned
if info.Tombstoned {
return ErrValidatorJailed(k.codespace).Result()
}

// cannot be unjailed until out of jail
if ctx.BlockHeader().Time.Before(info.JailedUntil) {
return ErrValidatorJailed(k.codespace).Result()
}

k.sk.Unjail(ctx, consAddr)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
Expand Down
Loading

0 comments on commit 52edb03

Please sign in to comment.