2018-05-28 14:24:58 -07:00
|
|
|
package slashing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2018-05-31 18:02:10 -07:00
|
|
|
"fmt"
|
2018-05-28 14:24:58 -07:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2018-05-28 17:58:43 -07:00
|
|
|
// Stored by *validator* address (not owner address)
|
2018-05-31 18:02:10 -07:00
|
|
|
func (k Keeper) getValidatorSigningInfo(ctx sdk.Context, address sdk.Address) (info ValidatorSigningInfo, found bool) {
|
2018-05-28 14:24:58 -07:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2018-05-31 18:02:10 -07:00
|
|
|
bz := store.Get(GetValidatorSigningInfoKey(address))
|
2018-05-28 14:24:58 -07:00
|
|
|
if bz == nil {
|
|
|
|
found = false
|
2018-05-30 16:37:38 -07:00
|
|
|
return
|
2018-05-28 14:24:58 -07:00
|
|
|
}
|
2018-05-30 16:37:38 -07:00
|
|
|
k.cdc.MustUnmarshalBinary(bz, &info)
|
|
|
|
found = true
|
2018-05-28 14:24:58 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:58:43 -07:00
|
|
|
// Stored by *validator* address (not owner address)
|
2018-05-31 18:02:10 -07:00
|
|
|
func (k Keeper) setValidatorSigningInfo(ctx sdk.Context, address sdk.Address, info ValidatorSigningInfo) {
|
2018-05-28 14:24:58 -07:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
bz := k.cdc.MustMarshalBinary(info)
|
2018-05-31 18:02:10 -07:00
|
|
|
store.Set(GetValidatorSigningInfoKey(address), bz)
|
2018-05-28 14:24:58 -07:00
|
|
|
}
|
|
|
|
|
2018-05-28 17:58:43 -07:00
|
|
|
// Stored by *validator* address (not owner address)
|
2018-05-28 14:24:58 -07:00
|
|
|
func (k Keeper) getValidatorSigningBitArray(ctx sdk.Context, address sdk.Address, index int64) (signed bool) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2018-05-31 18:02:10 -07:00
|
|
|
bz := store.Get(GetValidatorSigningBitArrayKey(address, index))
|
2018-05-28 14:24:58 -07:00
|
|
|
if bz == nil {
|
|
|
|
// lazy: treat empty key as unsigned
|
|
|
|
signed = false
|
2018-05-30 16:37:38 -07:00
|
|
|
return
|
2018-05-28 14:24:58 -07:00
|
|
|
}
|
2018-05-30 16:37:38 -07:00
|
|
|
k.cdc.MustUnmarshalBinary(bz, &signed)
|
2018-05-28 14:24:58 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:58:43 -07:00
|
|
|
// Stored by *validator* address (not owner address)
|
2018-05-28 14:24:58 -07:00
|
|
|
func (k Keeper) setValidatorSigningBitArray(ctx sdk.Context, address sdk.Address, index int64, signed bool) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
bz := k.cdc.MustMarshalBinary(signed)
|
2018-05-31 18:02:10 -07:00
|
|
|
store.Set(GetValidatorSigningBitArrayKey(address, index), bz)
|
2018-05-28 14:24:58 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 18:02:10 -07:00
|
|
|
// Signing info for a validator
|
|
|
|
type ValidatorSigningInfo struct {
|
2018-05-28 14:39:57 -07:00
|
|
|
StartHeight int64 `json:"start_height"` // height at which validator was first a candidate OR was unrevoked
|
|
|
|
IndexOffset int64 `json:"index_offset"` // index offset into signed block bit array
|
|
|
|
JailedUntil int64 `json:"jailed_until"` // timestamp validator cannot be unrevoked until
|
|
|
|
SignedBlocksCounter int64 `json:"signed_blocks_counter"` // signed blocks counter (to avoid scanning the array every time)
|
2018-05-28 14:24:58 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 18:02:10 -07:00
|
|
|
// Return human readable signing info
|
|
|
|
func (i ValidatorSigningInfo) HumanReadableString() string {
|
|
|
|
return fmt.Sprintf("Start height: %d, index offset: %d, jailed until: %d, signed blocks counter: %d",
|
|
|
|
i.StartHeight, i.IndexOffset, i.JailedUntil, i.SignedBlocksCounter)
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:58:43 -07:00
|
|
|
// Stored by *validator* address (not owner address)
|
2018-05-31 18:02:10 -07:00
|
|
|
func GetValidatorSigningInfoKey(v sdk.Address) []byte {
|
2018-05-28 14:24:58 -07:00
|
|
|
return append([]byte{0x01}, v.Bytes()...)
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:58:43 -07:00
|
|
|
// Stored by *validator* address (not owner address)
|
2018-05-31 18:02:10 -07:00
|
|
|
func GetValidatorSigningBitArrayKey(v sdk.Address, i int64) []byte {
|
2018-05-28 14:24:58 -07:00
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(b, uint64(i))
|
|
|
|
return append([]byte{0x02}, append(v.Bytes(), b...)...)
|
|
|
|
}
|