2019-03-28 16:27:47 -07:00
|
|
|
package keeper
|
2018-07-17 11:50:30 -07:00
|
|
|
|
|
|
|
import (
|
2018-11-04 20:44:43 -08:00
|
|
|
"bytes"
|
2018-09-09 08:34:09 -07:00
|
|
|
"fmt"
|
2018-07-17 11:50:30 -07:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-06-05 10:42:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/exported"
|
2019-03-28 16:27:47 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-07-17 11:50:30 -07:00
|
|
|
)
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// RegisterInvariants registers all staking invariants
|
|
|
|
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
|
|
|
|
ir.RegisterRoute(types.ModuleName, "module-accounts",
|
|
|
|
ModuleAccountInvariants(k))
|
2019-05-16 08:25:32 -07:00
|
|
|
ir.RegisterRoute(types.ModuleName, "nonnegative-power",
|
2019-03-28 16:27:47 -07:00
|
|
|
NonNegativePowerInvariant(k))
|
2019-05-16 08:25:32 -07:00
|
|
|
ir.RegisterRoute(types.ModuleName, "positive-delegation",
|
2019-03-28 16:27:47 -07:00
|
|
|
PositiveDelegationInvariant(k))
|
2019-05-16 08:25:32 -07:00
|
|
|
ir.RegisterRoute(types.ModuleName, "delegator-shares",
|
2019-03-28 16:27:47 -07:00
|
|
|
DelegatorSharesInvariant(k))
|
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
// AllInvariants runs all invariants of the staking module.
|
2019-06-28 13:11:27 -07:00
|
|
|
func AllInvariants(k Keeper) sdk.Invariant {
|
2019-07-11 03:56:43 -07:00
|
|
|
return func(ctx sdk.Context) (string, bool) {
|
|
|
|
res, stop := ModuleAccountInvariants(k)(ctx)
|
|
|
|
if stop {
|
|
|
|
return res, stop
|
2018-09-09 08:34:09 -07:00
|
|
|
}
|
2018-11-04 20:44:43 -08:00
|
|
|
|
2019-07-11 03:56:43 -07:00
|
|
|
res, stop = NonNegativePowerInvariant(k)(ctx)
|
|
|
|
if stop {
|
|
|
|
return res, stop
|
2018-09-09 08:34:09 -07:00
|
|
|
}
|
2018-11-04 20:44:43 -08:00
|
|
|
|
2019-07-11 03:56:43 -07:00
|
|
|
res, stop = PositiveDelegationInvariant(k)(ctx)
|
|
|
|
if stop {
|
|
|
|
return res, stop
|
2018-11-26 04:13:47 -08:00
|
|
|
}
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
return DelegatorSharesInvariant(k)(ctx)
|
2018-07-17 11:50:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// ModuleAccountInvariants checks that the bonded and notBonded ModuleAccounts pools
|
|
|
|
// reflects the tokens actively bonded and not bonded
|
|
|
|
func ModuleAccountInvariants(k Keeper) sdk.Invariant {
|
2019-07-11 03:56:43 -07:00
|
|
|
return func(ctx sdk.Context) (string, bool) {
|
2019-06-28 13:11:27 -07:00
|
|
|
bonded := sdk.ZeroInt()
|
|
|
|
notBonded := sdk.ZeroInt()
|
|
|
|
bondedPool := k.GetBondedPool(ctx)
|
|
|
|
notBondedPool := k.GetNotBondedPool(ctx)
|
|
|
|
bondDenom := k.BondDenom(ctx)
|
2018-07-17 11:50:30 -07:00
|
|
|
|
2019-06-05 10:42:25 -07:00
|
|
|
k.IterateValidators(ctx, func(_ int64, validator exported.ValidatorI) bool {
|
2018-07-17 11:50:30 -07:00
|
|
|
switch validator.GetStatus() {
|
|
|
|
case sdk.Bonded:
|
2019-06-28 13:11:27 -07:00
|
|
|
bonded = bonded.Add(validator.GetTokens())
|
2019-01-02 12:29:47 -08:00
|
|
|
case sdk.Unbonding, sdk.Unbonded:
|
2019-06-28 13:11:27 -07:00
|
|
|
notBonded = notBonded.Add(validator.GetTokens())
|
|
|
|
default:
|
|
|
|
panic("invalid validator status")
|
2018-07-17 11:50:30 -07:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2018-07-18 00:42:18 -07:00
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
k.IterateUnbondingDelegations(ctx, func(_ int64, ubd types.UnbondingDelegation) bool {
|
|
|
|
for _, entry := range ubd.Entries {
|
|
|
|
notBonded = notBonded.Add(entry.Balance)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2018-10-18 12:58:57 -07:00
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
poolBonded := k.bankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom)
|
|
|
|
poolNotBonded := k.bankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom)
|
|
|
|
broken := !poolBonded.Amount.Equal(bonded) || !poolNotBonded.Amount.Equal(notBonded)
|
2018-07-18 00:42:18 -07:00
|
|
|
|
|
|
|
// Bonded tokens should equal sum of tokens with bonded validators
|
2019-06-28 13:11:27 -07:00
|
|
|
// Not-bonded tokens should equal unbonding delegations plus tokens on unbonded validators
|
2019-07-11 03:56:43 -07:00
|
|
|
return sdk.FormatInvariant(types.ModuleName, "bonded and not bonded module account coins", fmt.Sprintf(
|
|
|
|
"\tPool's bonded tokens: %v\n"+
|
|
|
|
"\tsum of bonded tokens: %v\n"+
|
|
|
|
"not bonded token invariance:\n"+
|
|
|
|
"\tPool's not bonded tokens: %v\n"+
|
|
|
|
"\tsum of not bonded tokens: %v\n"+
|
|
|
|
"module accounts total (bonded + not bonded):\n"+
|
|
|
|
"\tModule Accounts' tokens: %v\n"+
|
|
|
|
"\tsum tokens: %v\n",
|
2019-08-05 11:21:44 -07:00
|
|
|
poolBonded, bonded, poolNotBonded, notBonded, poolBonded.Add(poolNotBonded), bonded.Add(notBonded))), broken
|
2018-07-17 11:50:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 16:04:52 -08:00
|
|
|
// NonNegativePowerInvariant checks that all stored validators have >= 0 power.
|
2019-03-28 16:27:47 -07:00
|
|
|
func NonNegativePowerInvariant(k Keeper) sdk.Invariant {
|
2019-07-11 03:56:43 -07:00
|
|
|
return func(ctx sdk.Context) (string, bool) {
|
2020-05-02 12:26:59 -07:00
|
|
|
var (
|
|
|
|
msg string
|
|
|
|
broken bool
|
|
|
|
)
|
2019-07-11 03:56:43 -07:00
|
|
|
|
2018-11-04 20:44:43 -08:00
|
|
|
iterator := k.ValidatorsPowerStoreIterator(ctx)
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
|
|
validator, found := k.GetValidator(ctx, iterator.Value())
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("validator record not found for address: %X\n", iterator.Value()))
|
2018-09-09 08:34:09 -07:00
|
|
|
}
|
2018-11-04 20:44:43 -08:00
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
powerKey := types.GetValidatorsByPowerIndexKey(validator)
|
2018-11-04 20:44:43 -08:00
|
|
|
|
|
|
|
if !bytes.Equal(iterator.Key(), powerKey) {
|
2019-07-11 03:56:43 -07:00
|
|
|
broken = true
|
|
|
|
msg += fmt.Sprintf("power store invariance:\n\tvalidator.Power: %v"+
|
2019-07-15 09:56:38 -07:00
|
|
|
"\n\tkey should be: %v\n\tkey in store: %v\n",
|
2019-06-12 08:57:47 -07:00
|
|
|
validator.GetConsensusPower(), powerKey, iterator.Key())
|
2018-11-04 20:44:43 -08:00
|
|
|
}
|
2018-12-07 16:04:52 -08:00
|
|
|
|
2019-01-02 12:29:47 -08:00
|
|
|
if validator.Tokens.IsNegative() {
|
2019-07-11 03:56:43 -07:00
|
|
|
broken = true
|
2019-07-15 09:56:38 -07:00
|
|
|
msg += fmt.Sprintf("\tnegative tokens for validator: %v\n", validator)
|
2018-12-07 16:04:52 -08:00
|
|
|
}
|
2018-11-04 20:44:43 -08:00
|
|
|
}
|
|
|
|
iterator.Close()
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-08-05 11:21:44 -07:00
|
|
|
return sdk.FormatInvariant(types.ModuleName, "nonnegative power", fmt.Sprintf("found invalid validator powers\n%s", msg)), broken
|
2018-07-17 11:50:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 04:13:47 -08:00
|
|
|
// PositiveDelegationInvariant checks that all stored delegations have > 0 shares.
|
2019-03-28 16:27:47 -07:00
|
|
|
func PositiveDelegationInvariant(k Keeper) sdk.Invariant {
|
2019-07-11 03:56:43 -07:00
|
|
|
return func(ctx sdk.Context) (string, bool) {
|
2020-05-02 12:26:59 -07:00
|
|
|
var (
|
|
|
|
msg string
|
|
|
|
count int
|
|
|
|
)
|
2019-07-11 03:56:43 -07:00
|
|
|
|
2018-11-26 04:13:47 -08:00
|
|
|
delegations := k.GetAllDelegations(ctx)
|
|
|
|
for _, delegation := range delegations {
|
|
|
|
if delegation.Shares.IsNegative() {
|
2019-07-15 09:56:38 -07:00
|
|
|
count++
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-07-11 03:56:43 -07:00
|
|
|
msg += fmt.Sprintf("\tdelegation with negative shares: %+v\n", delegation)
|
2018-11-26 04:13:47 -08:00
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2018-11-26 04:13:47 -08:00
|
|
|
if delegation.Shares.IsZero() {
|
2019-07-15 09:56:38 -07:00
|
|
|
count++
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-07-11 03:56:43 -07:00
|
|
|
msg += fmt.Sprintf("\tdelegation with zero shares: %+v\n", delegation)
|
2018-11-26 04:13:47 -08:00
|
|
|
}
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-07-15 09:56:38 -07:00
|
|
|
broken := count != 0
|
2018-11-26 04:13:47 -08:00
|
|
|
|
2019-07-11 03:56:43 -07:00
|
|
|
return sdk.FormatInvariant(types.ModuleName, "positive delegations", fmt.Sprintf(
|
2019-08-05 11:21:44 -07:00
|
|
|
"%d invalid delegations found\n%s", count, msg)), broken
|
2018-11-26 04:13:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DelegatorSharesInvariant checks whether all the delegator shares which persist
|
|
|
|
// in the delegator object add up to the correct total delegator shares
|
2019-07-11 03:56:43 -07:00
|
|
|
// amount stored in each validator.
|
2019-03-28 16:27:47 -07:00
|
|
|
func DelegatorSharesInvariant(k Keeper) sdk.Invariant {
|
2019-07-11 03:56:43 -07:00
|
|
|
return func(ctx sdk.Context) (string, bool) {
|
2020-05-02 12:26:59 -07:00
|
|
|
var (
|
|
|
|
msg string
|
|
|
|
broken bool
|
|
|
|
)
|
2019-07-11 03:56:43 -07:00
|
|
|
|
2018-11-26 04:13:47 -08:00
|
|
|
validators := k.GetAllValidators(ctx)
|
|
|
|
for _, validator := range validators {
|
|
|
|
valTotalDelShares := validator.GetDelegatorShares()
|
|
|
|
totalDelShares := sdk.ZeroDec()
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2018-11-26 04:13:47 -08:00
|
|
|
delegations := k.GetValidatorDelegations(ctx, validator.GetOperator())
|
|
|
|
for _, delegation := range delegations {
|
|
|
|
totalDelShares = totalDelShares.Add(delegation.Shares)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valTotalDelShares.Equal(totalDelShares) {
|
2019-07-11 03:56:43 -07:00
|
|
|
broken = true
|
|
|
|
msg += fmt.Sprintf("broken delegator shares invariance:\n"+
|
2018-11-26 04:13:47 -08:00
|
|
|
"\tvalidator.DelegatorShares: %v\n"+
|
2019-07-15 09:56:38 -07:00
|
|
|
"\tsum of Delegator.Shares: %v\n", valTotalDelShares, totalDelShares)
|
2018-11-26 04:13:47 -08:00
|
|
|
}
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-08-05 11:21:44 -07:00
|
|
|
return sdk.FormatInvariant(types.ModuleName, "delegator shares", msg), broken
|
2018-11-26 04:13:47 -08:00
|
|
|
}
|
|
|
|
}
|