breakout use of GetValidatorByConsAddr
This commit is contained in:
parent
251a81f589
commit
d36030424e
|
@ -42,6 +42,7 @@ type Validator interface {
|
||||||
GetStatus() BondStatus // status of the validator
|
GetStatus() BondStatus // status of the validator
|
||||||
GetOperator() ValAddress // operator address to receive/return validators coins
|
GetOperator() ValAddress // operator address to receive/return validators coins
|
||||||
GetPubKey() crypto.PubKey // validation pubkey
|
GetPubKey() crypto.PubKey // validation pubkey
|
||||||
|
GetConsAddr() ConsAddress // validation consensus address
|
||||||
GetPower() Dec // validation power
|
GetPower() Dec // validation power
|
||||||
GetTokens() Dec // validation tokens
|
GetTokens() Dec // validation tokens
|
||||||
GetCommission() Dec // validator commission rate
|
GetCommission() Dec // validator commission rate
|
||||||
|
@ -73,9 +74,9 @@ type ValidatorSet interface {
|
||||||
TotalPower(Context) Dec // total power of the validator set
|
TotalPower(Context) Dec // total power of the validator set
|
||||||
|
|
||||||
// slash the validator and delegators of the validator, specifying offence height, offence power, and slash fraction
|
// slash the validator and delegators of the validator, specifying offence height, offence power, and slash fraction
|
||||||
Slash(Context, crypto.PubKey, int64, int64, Dec)
|
Slash(Context, ConsAddress, int64, int64, Dec)
|
||||||
Jail(Context, crypto.PubKey) // jail a validator
|
Jail(Context, ConsAddress) // jail a validator
|
||||||
Unjail(Context, crypto.PubKey) // unjail a validator
|
Unjail(Context, ConsAddress) // unjail a validator
|
||||||
|
|
||||||
// Delegation allows for getting a particular delegation for a given validator
|
// Delegation allows for getting a particular delegation for a given validator
|
||||||
// and delegator outside the scope of the staking module.
|
// and delegator outside the scope of the staking module.
|
||||||
|
|
|
@ -155,7 +155,7 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) {
|
||||||
for _, valAddr := range nonVotingVals {
|
for _, valAddr := range nonVotingVals {
|
||||||
val := keeper.ds.GetValidatorSet().Validator(ctx, valAddr)
|
val := keeper.ds.GetValidatorSet().Validator(ctx, valAddr)
|
||||||
keeper.ds.GetValidatorSet().Slash(ctx,
|
keeper.ds.GetValidatorSet().Slash(ctx,
|
||||||
val.GetPubKey(),
|
val.GetConsAddr(),
|
||||||
ctx.BlockHeight(),
|
ctx.BlockHeight(),
|
||||||
val.GetPower().RoundInt64(),
|
val.GetPower().RoundInt64(),
|
||||||
keeper.GetTallyingProcedure(ctx).GovernancePenalty)
|
keeper.GetTallyingProcedure(ctx).GovernancePenalty)
|
||||||
|
|
|
@ -34,9 +34,9 @@ func handleMsgUnjail(ctx sdk.Context, msg MsgUnjail, k Keeper) sdk.Result {
|
||||||
return ErrValidatorNotJailed(k.codespace).Result()
|
return ErrValidatorNotJailed(k.codespace).Result()
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := sdk.ConsAddress(validator.GetPubKey().Address())
|
consAddr := sdk.ConsAddress(validator.GetPubKey().Address())
|
||||||
|
|
||||||
info, found := k.getValidatorSigningInfo(ctx, addr)
|
info, found := k.getValidatorSigningInfo(ctx, consAddr)
|
||||||
if !found {
|
if !found {
|
||||||
return ErrNoValidatorForAddress(k.codespace).Result()
|
return ErrNoValidatorForAddress(k.codespace).Result()
|
||||||
}
|
}
|
||||||
|
@ -49,9 +49,9 @@ func handleMsgUnjail(ctx sdk.Context, msg MsgUnjail, k Keeper) sdk.Result {
|
||||||
// update the starting height so the validator can't be immediately jailed
|
// update the starting height so the validator can't be immediately jailed
|
||||||
// again
|
// again
|
||||||
info.StartHeight = ctx.BlockHeight()
|
info.StartHeight = ctx.BlockHeight()
|
||||||
k.setValidatorSigningInfo(ctx, addr, info)
|
k.setValidatorSigningInfo(ctx, consAddr, info)
|
||||||
|
|
||||||
k.validatorSet.Unjail(ctx, validator.GetPubKey())
|
k.validatorSet.Unjail(ctx, consAddr)
|
||||||
|
|
||||||
tags := sdk.NewTags("action", []byte("unjail"), "validator", []byte(msg.ValidatorAddr.String()))
|
tags := sdk.NewTags("action", []byte("unjail"), "validator", []byte(msg.ValidatorAddr.String()))
|
||||||
|
|
||||||
|
|
|
@ -40,10 +40,10 @@ func (k Keeper) handleDoubleSign(ctx sdk.Context, addr crypto.Address, infractio
|
||||||
logger := ctx.Logger().With("module", "x/slashing")
|
logger := ctx.Logger().With("module", "x/slashing")
|
||||||
time := ctx.BlockHeader().Time
|
time := ctx.BlockHeader().Time
|
||||||
age := time.Sub(timestamp)
|
age := time.Sub(timestamp)
|
||||||
address := sdk.ConsAddress(addr)
|
consAddr := sdk.ConsAddress(addr)
|
||||||
pubkey, err := k.getPubkey(ctx, addr)
|
pubkey, err := k.getPubkey(ctx, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("Validator address %v not found", addr))
|
panic(fmt.Sprintf("Validator consensus-address %v not found", consAddr))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Double sign too old
|
// Double sign too old
|
||||||
|
@ -59,22 +59,22 @@ func (k Keeper) handleDoubleSign(ctx sdk.Context, addr crypto.Address, infractio
|
||||||
// Cap the amount slashed to the penalty for the worst infraction
|
// Cap the amount slashed to the penalty for the worst infraction
|
||||||
// within the slashing period when this infraction was committed
|
// within the slashing period when this infraction was committed
|
||||||
fraction := k.SlashFractionDoubleSign(ctx)
|
fraction := k.SlashFractionDoubleSign(ctx)
|
||||||
revisedFraction := k.capBySlashingPeriod(ctx, address, fraction, infractionHeight)
|
revisedFraction := k.capBySlashingPeriod(ctx, consAddr, fraction, infractionHeight)
|
||||||
logger.Info(fmt.Sprintf("Fraction slashed capped by slashing period from %v to %v", fraction, revisedFraction))
|
logger.Info(fmt.Sprintf("Fraction slashed capped by slashing period from %v to %v", fraction, revisedFraction))
|
||||||
|
|
||||||
// Slash validator
|
// Slash validator
|
||||||
k.validatorSet.Slash(ctx, pubkey, infractionHeight, power, revisedFraction)
|
k.validatorSet.Slash(ctx, consAddr, infractionHeight, power, revisedFraction)
|
||||||
|
|
||||||
// Jail validator
|
// Jail validator
|
||||||
k.validatorSet.Jail(ctx, pubkey)
|
k.validatorSet.Jail(ctx, consAddr)
|
||||||
|
|
||||||
// Set validator jail duration
|
// Set validator jail duration
|
||||||
signInfo, found := k.getValidatorSigningInfo(ctx, address)
|
signInfo, found := k.getValidatorSigningInfo(ctx, consAddr)
|
||||||
if !found {
|
if !found {
|
||||||
panic(fmt.Sprintf("Expected signing info for validator %s but not found", address))
|
panic(fmt.Sprintf("Expected signing info for validator %s but not found", consAddr))
|
||||||
}
|
}
|
||||||
signInfo.JailedUntil = time.Add(k.DoubleSignUnbondDuration(ctx))
|
signInfo.JailedUntil = time.Add(k.DoubleSignUnbondDuration(ctx))
|
||||||
k.setValidatorSigningInfo(ctx, address, signInfo)
|
k.setValidatorSigningInfo(ctx, consAddr, signInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle a validator signature, must be called once per validator per block
|
// handle a validator signature, must be called once per validator per block
|
||||||
|
@ -82,14 +82,14 @@ func (k Keeper) handleDoubleSign(ctx sdk.Context, addr crypto.Address, infractio
|
||||||
func (k Keeper) handleValidatorSignature(ctx sdk.Context, addr crypto.Address, power int64, signed bool) {
|
func (k Keeper) handleValidatorSignature(ctx sdk.Context, addr crypto.Address, power int64, signed bool) {
|
||||||
logger := ctx.Logger().With("module", "x/slashing")
|
logger := ctx.Logger().With("module", "x/slashing")
|
||||||
height := ctx.BlockHeight()
|
height := ctx.BlockHeight()
|
||||||
address := sdk.ConsAddress(addr)
|
consAddr := sdk.ConsAddress(addr)
|
||||||
pubkey, err := k.getPubkey(ctx, addr)
|
pubkey, err := k.getPubkey(ctx, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("Validator address %v not found", addr))
|
panic(fmt.Sprintf("Validator consensus-address %v not found", consAddr))
|
||||||
}
|
}
|
||||||
// Local index, so counts blocks validator *should* have signed
|
// Local index, so counts blocks validator *should* have signed
|
||||||
// Will use the 0-value default signing info if not present, except for start height
|
// Will use the 0-value default signing info if not present, except for start height
|
||||||
signInfo, found := k.getValidatorSigningInfo(ctx, address)
|
signInfo, found := k.getValidatorSigningInfo(ctx, consAddr)
|
||||||
if !found {
|
if !found {
|
||||||
// If this validator has never been seen before, construct a new SigningInfo with the correct start height
|
// If this validator has never been seen before, construct a new SigningInfo with the correct start height
|
||||||
signInfo = NewValidatorSigningInfo(height, 0, time.Unix(0, 0), 0)
|
signInfo = NewValidatorSigningInfo(height, 0, time.Unix(0, 0), 0)
|
||||||
|
@ -100,16 +100,16 @@ func (k Keeper) handleValidatorSignature(ctx sdk.Context, addr crypto.Address, p
|
||||||
// Update signed block bit array & counter
|
// Update signed block bit array & counter
|
||||||
// This counter just tracks the sum of the bit array
|
// This counter just tracks the sum of the bit array
|
||||||
// That way we avoid needing to read/write the whole array each time
|
// That way we avoid needing to read/write the whole array each time
|
||||||
previous := k.getValidatorSigningBitArray(ctx, address, index)
|
previous := k.getValidatorSigningBitArray(ctx, consAddr, index)
|
||||||
if previous == signed {
|
if previous == signed {
|
||||||
// Array value at this index has not changed, no need to update counter
|
// Array value at this index has not changed, no need to update counter
|
||||||
} else if previous && !signed {
|
} else if previous && !signed {
|
||||||
// Array value has changed from signed to unsigned, decrement counter
|
// Array value has changed from signed to unsigned, decrement counter
|
||||||
k.setValidatorSigningBitArray(ctx, address, index, false)
|
k.setValidatorSigningBitArray(ctx, consAddr, index, false)
|
||||||
signInfo.SignedBlocksCounter--
|
signInfo.SignedBlocksCounter--
|
||||||
} else if !previous && signed {
|
} else if !previous && signed {
|
||||||
// Array value has changed from unsigned to signed, increment counter
|
// Array value has changed from unsigned to signed, increment counter
|
||||||
k.setValidatorSigningBitArray(ctx, address, index, true)
|
k.setValidatorSigningBitArray(ctx, consAddr, index, true)
|
||||||
signInfo.SignedBlocksCounter++
|
signInfo.SignedBlocksCounter++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,13 +118,13 @@ func (k Keeper) handleValidatorSignature(ctx sdk.Context, addr crypto.Address, p
|
||||||
}
|
}
|
||||||
minHeight := signInfo.StartHeight + k.SignedBlocksWindow(ctx)
|
minHeight := signInfo.StartHeight + k.SignedBlocksWindow(ctx)
|
||||||
if height > minHeight && signInfo.SignedBlocksCounter < k.MinSignedPerWindow(ctx) {
|
if height > minHeight && signInfo.SignedBlocksCounter < k.MinSignedPerWindow(ctx) {
|
||||||
validator := k.validatorSet.ValidatorByConsAddr(ctx, address)
|
validator := k.validatorSet.ValidatorByConsAddr(ctx, consAddr)
|
||||||
if validator != nil && !validator.GetJailed() {
|
if validator != nil && !validator.GetJailed() {
|
||||||
// Downtime confirmed: slash and jail the validator
|
// Downtime confirmed: slash and jail the validator
|
||||||
logger.Info(fmt.Sprintf("Validator %s past min height of %d and below signed blocks threshold of %d",
|
logger.Info(fmt.Sprintf("Validator %s past min height of %d and below signed blocks threshold of %d",
|
||||||
pubkey.Address(), minHeight, k.MinSignedPerWindow(ctx)))
|
pubkey.Address(), minHeight, k.MinSignedPerWindow(ctx)))
|
||||||
k.validatorSet.Slash(ctx, pubkey, height, power, k.SlashFractionDowntime(ctx))
|
k.validatorSet.Slash(ctx, consAddr, height, power, k.SlashFractionDowntime(ctx))
|
||||||
k.validatorSet.Jail(ctx, pubkey)
|
k.validatorSet.Jail(ctx, consAddr)
|
||||||
signInfo.JailedUntil = ctx.BlockHeader().Time.Add(k.DowntimeUnbondDuration(ctx))
|
signInfo.JailedUntil = ctx.BlockHeader().Time.Add(k.DowntimeUnbondDuration(ctx))
|
||||||
} else {
|
} else {
|
||||||
// Validator was (a) not found or (b) already jailed, don't slash
|
// Validator was (a) not found or (b) already jailed, don't slash
|
||||||
|
@ -134,7 +134,7 @@ func (k Keeper) handleValidatorSignature(ctx sdk.Context, addr crypto.Address, p
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the updated signing info
|
// Set the updated signing info
|
||||||
k.setValidatorSigningInfo(ctx, address, signInfo)
|
k.setValidatorSigningInfo(ctx, consAddr, signInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddValidators adds the validators to the keepers validator addr to pubkey mapping.
|
// AddValidators adds the validators to the keepers validator addr to pubkey mapping.
|
||||||
|
|
|
@ -68,7 +68,7 @@ func handleMsgCreateValidator(ctx sdk.Context, msg types.MsgCreateValidator, k k
|
||||||
if found {
|
if found {
|
||||||
return ErrValidatorOwnerExists(k.Codespace()).Result()
|
return ErrValidatorOwnerExists(k.Codespace()).Result()
|
||||||
}
|
}
|
||||||
_, found = k.GetValidatorByConsAddr(ctx, sdk.ConsAddress(msg.PubKey.Address()))
|
_, found = k.GetValidatorByConsPubKey(ctx, msg.PubKey)
|
||||||
if found {
|
if found {
|
||||||
return ErrValidatorPubKeyExists(k.Codespace()).Result()
|
return ErrValidatorPubKeyExists(k.Codespace()).Result()
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
||||||
|
"github.com/tendermint/tendermint/crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Implements ValidatorSet
|
// Implements ValidatorSet
|
||||||
|
@ -66,6 +67,15 @@ func (k Keeper) ValidatorByConsAddr(ctx sdk.Context, addr sdk.ConsAddress) sdk.V
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get the sdk.validator for a particular pubkey
|
||||||
|
func (k Keeper) ValidatorByConsPubKey(ctx sdk.Context, consPubKey crypto.PubKey) sdk.Validator {
|
||||||
|
val, found := k.GetValidatorByConsPubKey(ctx, consPubKey)
|
||||||
|
if !found {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
// total power from the bond
|
// total power from the bond
|
||||||
func (k Keeper) TotalPower(ctx sdk.Context) sdk.Dec {
|
func (k Keeper) TotalPower(ctx sdk.Context) sdk.Dec {
|
||||||
pool := k.GetPool(ctx)
|
pool := k.GetPool(ctx)
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
types "github.com/cosmos/cosmos-sdk/x/stake/types"
|
types "github.com/cosmos/cosmos-sdk/x/stake/types"
|
||||||
"github.com/tendermint/tendermint/crypto"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Slash a validator for an infraction committed at a known height
|
// Slash a validator for an infraction committed at a known height
|
||||||
|
@ -24,7 +23,7 @@ import (
|
||||||
// not at a height in the future
|
// not at a height in the future
|
||||||
//
|
//
|
||||||
// nolint: gocyclo
|
// nolint: gocyclo
|
||||||
func (k Keeper) Slash(ctx sdk.Context, pubkey crypto.PubKey, infractionHeight int64, power int64, slashFactor sdk.Dec) {
|
func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeight int64, power int64, slashFactor sdk.Dec) {
|
||||||
logger := ctx.Logger().With("module", "x/stake")
|
logger := ctx.Logger().With("module", "x/stake")
|
||||||
|
|
||||||
if slashFactor.LT(sdk.ZeroDec()) {
|
if slashFactor.LT(sdk.ZeroDec()) {
|
||||||
|
@ -36,7 +35,7 @@ func (k Keeper) Slash(ctx sdk.Context, pubkey crypto.PubKey, infractionHeight in
|
||||||
// ref https://github.com/cosmos/cosmos-sdk/issues/1348
|
// ref https://github.com/cosmos/cosmos-sdk/issues/1348
|
||||||
// ref https://github.com/cosmos/cosmos-sdk/issues/1471
|
// ref https://github.com/cosmos/cosmos-sdk/issues/1471
|
||||||
|
|
||||||
validator, found := k.GetValidatorByConsAddr(ctx, sdk.ConsAddress(pubkey.Address()))
|
validator, found := k.GetValidatorByConsAddr(ctx, consAddr)
|
||||||
if !found {
|
if !found {
|
||||||
// If not found, the validator must have been overslashed and removed - so we don't need to do anything
|
// If not found, the validator must have been overslashed and removed - so we don't need to do anything
|
||||||
// NOTE: Correctness dependent on invariant that unbonding delegations / redelegations must also have been completely
|
// NOTE: Correctness dependent on invariant that unbonding delegations / redelegations must also have been completely
|
||||||
|
@ -44,7 +43,7 @@ func (k Keeper) Slash(ctx sdk.Context, pubkey crypto.PubKey, infractionHeight in
|
||||||
// Log the slash attempt for future reference (maybe we should tag it too)
|
// Log the slash attempt for future reference (maybe we should tag it too)
|
||||||
logger.Error(fmt.Sprintf(
|
logger.Error(fmt.Sprintf(
|
||||||
"WARNING: Ignored attempt to slash a nonexistent validator with address %s, we recommend you investigate immediately",
|
"WARNING: Ignored attempt to slash a nonexistent validator with address %s, we recommend you investigate immediately",
|
||||||
pubkey.Address()))
|
consAddr))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,36 +124,28 @@ func (k Keeper) Slash(ctx sdk.Context, pubkey crypto.PubKey, infractionHeight in
|
||||||
}
|
}
|
||||||
|
|
||||||
// jail a validator
|
// jail a validator
|
||||||
func (k Keeper) Jail(ctx sdk.Context, pubkey crypto.PubKey) {
|
func (k Keeper) Jail(ctx sdk.Context, consAddr sdk.ConsAddress) {
|
||||||
k.setJailed(ctx, pubkey, true)
|
k.setJailed(ctx, consAddr, true)
|
||||||
validatorAddr, err := sdk.ValAddressFromHex(pubkey.Address().String())
|
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
logger := ctx.Logger().With("module", "x/stake")
|
logger := ctx.Logger().With("module", "x/stake")
|
||||||
logger.Info(fmt.Sprintf("validator %s jailed", validatorAddr))
|
logger.Info(fmt.Sprintf("validator %s jailed", consAddr))
|
||||||
// TODO Return event(s), blocked on https://github.com/tendermint/tendermint/pull/1803
|
// TODO Return event(s), blocked on https://github.com/tendermint/tendermint/pull/1803
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// unjail a validator
|
// unjail a validator
|
||||||
func (k Keeper) Unjail(ctx sdk.Context, pubkey crypto.PubKey) {
|
func (k Keeper) Unjail(ctx sdk.Context, consAddr sdk.ConsAddress) {
|
||||||
k.setJailed(ctx, pubkey, false)
|
k.setJailed(ctx, consAddr, false)
|
||||||
validatorAddr, err := sdk.ValAddressFromHex(pubkey.Address().String())
|
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
}
|
|
||||||
logger := ctx.Logger().With("module", "x/stake")
|
logger := ctx.Logger().With("module", "x/stake")
|
||||||
logger.Info(fmt.Sprintf("validator %s unjailed", validatorAddr))
|
logger.Info(fmt.Sprintf("validator %s unjailed", consAddr))
|
||||||
// TODO Return event(s), blocked on https://github.com/tendermint/tendermint/pull/1803
|
// TODO Return event(s), blocked on https://github.com/tendermint/tendermint/pull/1803
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the jailed flag on a validator
|
// set the jailed flag on a validator
|
||||||
func (k Keeper) setJailed(ctx sdk.Context, pubkey crypto.PubKey, isJailed bool) {
|
func (k Keeper) setJailed(ctx sdk.Context, consAddr sdk.ConsAddress, isJailed bool) {
|
||||||
validator, found := k.GetValidatorByConsAddr(ctx, sdk.ConsAddress(pubkey.Address()))
|
validator, found := k.GetValidatorByConsAddr(ctx, sdk.ConsAddress(consAddr))
|
||||||
if !found {
|
if !found {
|
||||||
panic(fmt.Errorf("validator with pubkey %s not found, cannot set jailed to %v", pubkey, isJailed))
|
panic(fmt.Errorf("validator with consensus-Address %s not found, cannot set jailed to %v", consAddr, isJailed))
|
||||||
}
|
}
|
||||||
validator.Jailed = isJailed
|
validator.Jailed = isJailed
|
||||||
k.UpdateValidator(ctx, validator) // update validator, possibly unbonding or bonding it
|
k.UpdateValidator(ctx, validator) // update validator, possibly unbonding or bonding it
|
||||||
|
|
|
@ -27,7 +27,7 @@ func setupHelper(t *testing.T, amt int64) (sdk.Context, Keeper, types.Params) {
|
||||||
validator, pool, _ = validator.AddTokensFromDel(pool, sdk.NewInt(amt))
|
validator, pool, _ = validator.AddTokensFromDel(pool, sdk.NewInt(amt))
|
||||||
keeper.SetPool(ctx, pool)
|
keeper.SetPool(ctx, pool)
|
||||||
validator = keeper.UpdateValidator(ctx, validator)
|
validator = keeper.UpdateValidator(ctx, validator)
|
||||||
keeper.SetValidatorByPubKeyIndex(ctx, validator)
|
keeper.SetValidatorByConsAddr(ctx, validator)
|
||||||
}
|
}
|
||||||
pool = keeper.GetPool(ctx)
|
pool = keeper.GetPool(ctx)
|
||||||
|
|
||||||
|
@ -191,12 +191,12 @@ func TestSlashValidatorAtCurrentHeight(t *testing.T) {
|
||||||
fraction := sdk.NewDecWithPrec(5, 1)
|
fraction := sdk.NewDecWithPrec(5, 1)
|
||||||
|
|
||||||
oldPool := keeper.GetPool(ctx)
|
oldPool := keeper.GetPool(ctx)
|
||||||
validator, found := keeper.GetValidatorByPubKey(ctx, pk)
|
validator, found := keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
keeper.Slash(ctx, pk, ctx.BlockHeight(), 10, fraction)
|
keeper.Slash(ctx, pk, ctx.BlockHeight(), 10, fraction)
|
||||||
|
|
||||||
// read updated state
|
// read updated state
|
||||||
validator, found = keeper.GetValidatorByPubKey(ctx, pk)
|
validator, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
newPool := keeper.GetPool(ctx)
|
newPool := keeper.GetPool(ctx)
|
||||||
|
|
||||||
|
@ -227,7 +227,7 @@ func TestSlashWithUnbondingDelegation(t *testing.T) {
|
||||||
// slash validator for the first time
|
// slash validator for the first time
|
||||||
ctx = ctx.WithBlockHeight(12)
|
ctx = ctx.WithBlockHeight(12)
|
||||||
oldPool := keeper.GetPool(ctx)
|
oldPool := keeper.GetPool(ctx)
|
||||||
validator, found := keeper.GetValidatorByPubKey(ctx, pk)
|
validator, found := keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
keeper.Slash(ctx, pk, 10, 10, fraction)
|
keeper.Slash(ctx, pk, 10, 10, fraction)
|
||||||
|
|
||||||
|
@ -241,7 +241,7 @@ func TestSlashWithUnbondingDelegation(t *testing.T) {
|
||||||
// bonded tokens burned
|
// bonded tokens burned
|
||||||
require.Equal(t, int64(3), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
require.Equal(t, int64(3), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
||||||
// read updated validator
|
// read updated validator
|
||||||
validator, found = keeper.GetValidatorByPubKey(ctx, pk)
|
validator, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
// power decreased by 3 - 6 stake originally bonded at the time of infraction
|
// power decreased by 3 - 6 stake originally bonded at the time of infraction
|
||||||
// was still bonded at the time of discovery and was slashed by half, 4 stake
|
// was still bonded at the time of discovery and was slashed by half, 4 stake
|
||||||
|
@ -261,7 +261,7 @@ func TestSlashWithUnbondingDelegation(t *testing.T) {
|
||||||
// bonded tokens burned again
|
// bonded tokens burned again
|
||||||
require.Equal(t, int64(6), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
require.Equal(t, int64(6), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
||||||
// read updated validator
|
// read updated validator
|
||||||
validator, found = keeper.GetValidatorByPubKey(ctx, pk)
|
validator, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
// power decreased by 3 again
|
// power decreased by 3 again
|
||||||
require.Equal(t, sdk.NewDec(4), validator.GetPower())
|
require.Equal(t, sdk.NewDec(4), validator.GetPower())
|
||||||
|
@ -281,7 +281,7 @@ func TestSlashWithUnbondingDelegation(t *testing.T) {
|
||||||
// bonded tokens burned again
|
// bonded tokens burned again
|
||||||
require.Equal(t, int64(9), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
require.Equal(t, int64(9), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
||||||
// read updated validator
|
// read updated validator
|
||||||
validator, found = keeper.GetValidatorByPubKey(ctx, pk)
|
validator, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
// power decreased by 3 again
|
// power decreased by 3 again
|
||||||
require.Equal(t, sdk.NewDec(1), validator.GetPower())
|
require.Equal(t, sdk.NewDec(1), validator.GetPower())
|
||||||
|
@ -303,7 +303,7 @@ func TestSlashWithUnbondingDelegation(t *testing.T) {
|
||||||
// read updated validator
|
// read updated validator
|
||||||
// power decreased by 1 again, validator is out of stake
|
// power decreased by 1 again, validator is out of stake
|
||||||
// ergo validator should have been removed from the store
|
// ergo validator should have been removed from the store
|
||||||
_, found = keeper.GetValidatorByPubKey(ctx, pk)
|
_, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.False(t, found)
|
require.False(t, found)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,7 +343,7 @@ func TestSlashWithRedelegation(t *testing.T) {
|
||||||
// slash validator
|
// slash validator
|
||||||
ctx = ctx.WithBlockHeight(12)
|
ctx = ctx.WithBlockHeight(12)
|
||||||
oldPool := keeper.GetPool(ctx)
|
oldPool := keeper.GetPool(ctx)
|
||||||
validator, found := keeper.GetValidatorByPubKey(ctx, pk)
|
validator, found := keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
keeper.Slash(ctx, pk, 10, 10, fraction)
|
keeper.Slash(ctx, pk, 10, 10, fraction)
|
||||||
|
|
||||||
|
@ -357,7 +357,7 @@ func TestSlashWithRedelegation(t *testing.T) {
|
||||||
// bonded tokens burned
|
// bonded tokens burned
|
||||||
require.Equal(t, int64(5), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
require.Equal(t, int64(5), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
||||||
// read updated validator
|
// read updated validator
|
||||||
validator, found = keeper.GetValidatorByPubKey(ctx, pk)
|
validator, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
// power decreased by 2 - 4 stake originally bonded at the time of infraction
|
// power decreased by 2 - 4 stake originally bonded at the time of infraction
|
||||||
// was still bonded at the time of discovery and was slashed by half, 4 stake
|
// was still bonded at the time of discovery and was slashed by half, 4 stake
|
||||||
|
@ -367,7 +367,7 @@ func TestSlashWithRedelegation(t *testing.T) {
|
||||||
|
|
||||||
// slash the validator again
|
// slash the validator again
|
||||||
ctx = ctx.WithBlockHeight(12)
|
ctx = ctx.WithBlockHeight(12)
|
||||||
validator, found = keeper.GetValidatorByPubKey(ctx, pk)
|
validator, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
require.NotPanics(t, func() { keeper.Slash(ctx, pk, 10, 10, sdk.OneDec()) })
|
require.NotPanics(t, func() { keeper.Slash(ctx, pk, 10, 10, sdk.OneDec()) })
|
||||||
|
|
||||||
|
@ -381,14 +381,14 @@ func TestSlashWithRedelegation(t *testing.T) {
|
||||||
// seven bonded tokens burned
|
// seven bonded tokens burned
|
||||||
require.Equal(t, int64(12), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
require.Equal(t, int64(12), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
||||||
// read updated validator
|
// read updated validator
|
||||||
validator, found = keeper.GetValidatorByPubKey(ctx, pk)
|
validator, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
// power decreased by 4
|
// power decreased by 4
|
||||||
require.Equal(t, sdk.NewDec(4), validator.GetPower())
|
require.Equal(t, sdk.NewDec(4), validator.GetPower())
|
||||||
|
|
||||||
// slash the validator again, by 100%
|
// slash the validator again, by 100%
|
||||||
ctx = ctx.WithBlockHeight(12)
|
ctx = ctx.WithBlockHeight(12)
|
||||||
validator, found = keeper.GetValidatorByPubKey(ctx, pk)
|
validator, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
keeper.Slash(ctx, pk, 10, 10, sdk.OneDec())
|
keeper.Slash(ctx, pk, 10, 10, sdk.OneDec())
|
||||||
|
|
||||||
|
@ -403,14 +403,14 @@ func TestSlashWithRedelegation(t *testing.T) {
|
||||||
require.Equal(t, int64(16), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
require.Equal(t, int64(16), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
||||||
// read updated validator
|
// read updated validator
|
||||||
// validator decreased to zero power, should have been removed from the store
|
// validator decreased to zero power, should have been removed from the store
|
||||||
_, found = keeper.GetValidatorByPubKey(ctx, pk)
|
_, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.False(t, found)
|
require.False(t, found)
|
||||||
|
|
||||||
// slash the validator again, by 100%
|
// slash the validator again, by 100%
|
||||||
// no stake remains to be slashed
|
// no stake remains to be slashed
|
||||||
ctx = ctx.WithBlockHeight(12)
|
ctx = ctx.WithBlockHeight(12)
|
||||||
// validator no longer in the store
|
// validator no longer in the store
|
||||||
_, found = keeper.GetValidatorByPubKey(ctx, pk)
|
_, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.False(t, found)
|
require.False(t, found)
|
||||||
keeper.Slash(ctx, pk, 10, 10, sdk.OneDec())
|
keeper.Slash(ctx, pk, 10, 10, sdk.OneDec())
|
||||||
|
|
||||||
|
@ -425,7 +425,7 @@ func TestSlashWithRedelegation(t *testing.T) {
|
||||||
require.Equal(t, int64(16), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
require.Equal(t, int64(16), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
||||||
// read updated validator
|
// read updated validator
|
||||||
// power still zero, still not in the store
|
// power still zero, still not in the store
|
||||||
_, found = keeper.GetValidatorByPubKey(ctx, pk)
|
_, found = keeper.GetValidatorByConsPubKey(ctx, pk)
|
||||||
require.False(t, found)
|
require.False(t, found)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -472,7 +472,7 @@ func TestSlashBoth(t *testing.T) {
|
||||||
// slash validator
|
// slash validator
|
||||||
ctx = ctx.WithBlockHeight(12)
|
ctx = ctx.WithBlockHeight(12)
|
||||||
oldPool := keeper.GetPool(ctx)
|
oldPool := keeper.GetPool(ctx)
|
||||||
validator, found := keeper.GetValidatorByPubKey(ctx, PKs[0])
|
validator, found := keeper.GetValidatorByConsPubkey(ctx, PKs[0])
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
keeper.Slash(ctx, PKs[0], 10, 10, fraction)
|
keeper.Slash(ctx, PKs[0], 10, 10, fraction)
|
||||||
|
|
||||||
|
@ -488,7 +488,7 @@ func TestSlashBoth(t *testing.T) {
|
||||||
// bonded tokens burned
|
// bonded tokens burned
|
||||||
require.Equal(t, int64(3), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
require.Equal(t, int64(3), oldPool.BondedTokens.Sub(newPool.BondedTokens).RoundInt64())
|
||||||
// read updated validator
|
// read updated validator
|
||||||
validator, found = keeper.GetValidatorByPubKey(ctx, PKs[0])
|
validator, found = keeper.GetValidatorByConsPubKey(ctx, PKs[0])
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
// power not decreased, all stake was bonded since
|
// power not decreased, all stake was bonded since
|
||||||
require.Equal(t, sdk.NewDec(10), validator.GetPower())
|
require.Equal(t, sdk.NewDec(10), validator.GetPower())
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
abci "github.com/tendermint/tendermint/abci/types"
|
abci "github.com/tendermint/tendermint/abci/types"
|
||||||
|
"github.com/tendermint/tendermint/crypto"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
||||||
|
@ -57,7 +58,7 @@ func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.ValAddress) (validator ty
|
||||||
return validator, true
|
return validator, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// get a single validator by pubkey
|
// get a single validator by consensus address
|
||||||
func (k Keeper) GetValidatorByConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) (validator types.Validator, found bool) {
|
func (k Keeper) GetValidatorByConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) (validator types.Validator, found bool) {
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
opAddr := store.Get(GetValidatorByConsAddrKey(consAddr))
|
opAddr := store.Get(GetValidatorByConsAddrKey(consAddr))
|
||||||
|
@ -67,6 +68,17 @@ func (k Keeper) GetValidatorByConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress
|
||||||
return k.GetValidator(ctx, opAddr)
|
return k.GetValidator(ctx, opAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get a single validator by pubkey
|
||||||
|
func (k Keeper) GetValidatorByConsPubKey(ctx sdk.Context, consPubKey crypto.PubKey) (validator types.Validator, found bool) {
|
||||||
|
store := ctx.KVStore(k.storeKey)
|
||||||
|
consAddr := sdk.ConsAddress(consPubKey.Address())
|
||||||
|
opAddr := store.Get(GetValidatorByConsAddrKey(consAddr))
|
||||||
|
if opAddr == nil {
|
||||||
|
return validator, false
|
||||||
|
}
|
||||||
|
return k.GetValidator(ctx, opAddr)
|
||||||
|
}
|
||||||
|
|
||||||
// set the main record holding validator details
|
// set the main record holding validator details
|
||||||
func (k Keeper) SetValidator(ctx sdk.Context, validator types.Validator) {
|
func (k Keeper) SetValidator(ctx sdk.Context, validator types.Validator) {
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
|
@ -75,6 +87,7 @@ func (k Keeper) SetValidator(ctx sdk.Context, validator types.Validator) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// validator index
|
// validator index
|
||||||
|
// TODO change to SetValidatorByConsAddr? used for retrieving from ConsPubkey as well- kinda confusing
|
||||||
func (k Keeper) SetValidatorByConsAddr(ctx sdk.Context, validator types.Validator) {
|
func (k Keeper) SetValidatorByConsAddr(ctx sdk.Context, validator types.Validator) {
|
||||||
store := ctx.KVStore(k.storeKey)
|
store := ctx.KVStore(k.storeKey)
|
||||||
consAddr := sdk.ConsAddress(validator.OperatorAddr.Bytes())
|
consAddr := sdk.ConsAddress(validator.OperatorAddr.Bytes())
|
||||||
|
|
|
@ -475,6 +475,7 @@ func (v Validator) GetMoniker() string { return v.Description.Moniker }
|
||||||
func (v Validator) GetStatus() sdk.BondStatus { return v.Status }
|
func (v Validator) GetStatus() sdk.BondStatus { return v.Status }
|
||||||
func (v Validator) GetOperator() sdk.ValAddress { return v.OperatorAddr }
|
func (v Validator) GetOperator() sdk.ValAddress { return v.OperatorAddr }
|
||||||
func (v Validator) GetPubKey() crypto.PubKey { return v.ConsPubKey }
|
func (v Validator) GetPubKey() crypto.PubKey { return v.ConsPubKey }
|
||||||
|
func (v Validator) GetConsAddr() sdk.ConsAddress { return sdk.ConsAddress(v.ConsPubKey.Address()) }
|
||||||
func (v Validator) GetPower() sdk.Dec { return v.BondedTokens() }
|
func (v Validator) GetPower() sdk.Dec { return v.BondedTokens() }
|
||||||
func (v Validator) GetTokens() sdk.Dec { return v.Tokens }
|
func (v Validator) GetTokens() sdk.Dec { return v.Tokens }
|
||||||
func (v Validator) GetCommission() sdk.Dec { return v.Commission }
|
func (v Validator) GetCommission() sdk.Dec { return v.Commission }
|
||||||
|
|
Loading…
Reference in New Issue