cosmos-sdk/x/slashing/tick.go

59 lines
1.8 KiB
Go
Raw Normal View History

2018-05-23 13:25:56 -07:00
package slashing
import (
"encoding/binary"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
tmtypes "github.com/tendermint/tendermint/types"
2018-05-23 13:25:56 -07:00
)
func NewBeginBlocker(sk Keeper) sdk.BeginBlocker {
return func(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
2018-05-28 15:10:52 -07:00
// Tag the height
2018-05-23 13:25:56 -07:00
heightBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(heightBytes, uint64(req.Header.Height))
tags := sdk.NewTags("height", heightBytes)
2018-05-28 15:10:52 -07:00
// Deal with any equivocation evidence
2018-05-23 13:25:56 -07:00
for _, evidence := range req.ByzantineValidators {
var pk crypto.PubKey
sk.cdc.MustUnmarshalBinaryBare(evidence.PubKey, &pk)
switch string(evidence.Type) {
case tmtypes.DUPLICATE_VOTE:
2018-05-23 13:25:56 -07:00
sk.handleDoubleSign(ctx, evidence.Height, evidence.Time, pk)
default:
ctx.Logger().With("module", "x/slashing").Error(fmt.Sprintf("Ignored unknown evidence type: %s", string(evidence.Type)))
}
}
2018-05-28 15:10:52 -07:00
// Figure out which validators were absent
absent := make(map[crypto.PubKey]struct{})
2018-05-23 13:25:56 -07:00
for _, pubkey := range req.AbsentValidators {
var pk crypto.PubKey
sk.cdc.MustUnmarshalBinaryBare(pubkey, &pk)
absent[pk] = struct{}{}
2018-05-23 13:25:56 -07:00
}
2018-05-28 15:10:52 -07:00
// Iterate over all the validators which *should* have signed this block
2018-05-23 13:25:56 -07:00
sk.stakeKeeper.IterateValidatorsBonded(ctx, func(_ int64, validator sdk.Validator) (stop bool) {
pubkey := validator.GetPubKey()
2018-05-28 23:32:39 -07:00
present := true
if _, ok := absent[pubkey]; ok {
2018-05-28 23:32:39 -07:00
present = false
}
2018-05-28 23:32:39 -07:00
sk.handleValidatorSignature(ctx, pubkey, present)
2018-05-23 13:25:56 -07:00
return false
})
2018-05-28 15:10:52 -07:00
// Return the begin block response
// TODO Return something composable, so other modules can also have BeginBlockers
// TODO Add some more tags so clients can track slashing events
2018-05-23 13:25:56 -07:00
return abci.ResponseBeginBlock{
Tags: tags.ToKVPairs(),
}
}
}