cosmos-sdk/x/slashing/tick.go

55 lines
1.6 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
)
// slash begin blocker functionality
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, sk Keeper) (tags sdk.Tags) {
2018-05-28 15:10:52 -07:00
// Tag the height
heightBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(heightBytes, uint64(req.Header.Height))
2018-05-28 15:10:52 -07:00
// TODO Add some more tags so clients can track slashing events
tags = sdk.NewTags("height", heightBytes)
// Deal with any equivocation evidence
for _, evidence := range req.ByzantineValidators {
var pk crypto.PubKey
sk.cdc.MustUnmarshalBinaryBare(evidence.PubKey, &pk)
switch string(evidence.Type) {
case tmtypes.DUPLICATE_VOTE:
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-23 13:25:56 -07:00
}
}
// Figure out which validators were absent
absent := make(map[crypto.PubKey]struct{})
for _, pubkey := range req.AbsentValidators {
var pk crypto.PubKey
sk.cdc.MustUnmarshalBinaryBare(pubkey, &pk)
absent[pk] = struct{}{}
}
// Iterate over all the validators which *should* have signed this block
2018-06-01 15:27:37 -07:00
sk.validatorSet.IterateValidatorsBonded(ctx, func(_ int64, validator sdk.Validator) (stop bool) {
pubkey := validator.GetPubKey()
present := true
if _, ok := absent[pubkey]; ok {
present = false
}
sk.handleValidatorSignature(ctx, pubkey, present)
return false
})
return
2018-05-23 13:25:56 -07:00
}