Merge PR #4710: Update Invariant Printing

This commit is contained in:
colin axner 2019-07-15 09:56:38 -07:00 committed by Jack Zampolin
parent 85ca553651
commit 8b49b58812
6 changed files with 32 additions and 29 deletions

View File

@ -3,8 +3,8 @@ package types
import "fmt"
// An Invariant is a function which tests a particular invariant.
// If the invariant has been broken, it should return an error
// containing a descriptive message about what happened.
// The invariant returns a descriptive message about what happened
// and a boolean indicating whether the invariant has been broken.
// The simulator will then halt and print the logs.
type Invariant func(ctx Context) (string, bool)
@ -17,8 +17,8 @@ type InvariantRegistry interface {
}
// FormatInvariant returns a standardized invariant message along with
// broken boolean.
// a boolean indicating whether the invariant has been broken.
func FormatInvariant(module, name, msg string, broken bool) (string, bool) {
return fmt.Sprintf("%s: %s invariant\n%s\nInvariant Broken: %v\n",
return fmt.Sprintf("%s: %s invariant\n%sinvariant broken: %v\n",
module, name, msg, broken), broken
}

View File

@ -17,21 +17,21 @@ func RegisterInvariants(ir sdk.InvariantRegistry, ak types.AccountKeeper) {
func NonnegativeBalanceInvariant(ak types.AccountKeeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var msg string
var amt int
var count int
accts := ak.GetAllAccounts(ctx)
for _, acc := range accts {
coins := acc.GetCoins()
if coins.IsAnyNegative() {
amt++
count++
msg += fmt.Sprintf("\t%s has a negative denomination of %s\n",
acc.GetAddress().String(),
coins.String())
}
}
broken := amt != 0
broken := count != 0
return sdk.FormatInvariant(types.ModuleName, "nonnegative-outstanding",
fmt.Sprintf("amount of negative accounts found %d\n%s", amt, msg), broken)
fmt.Sprintf("amount of negative accounts found %d\n%s", count, msg), broken)
}
}

View File

@ -43,21 +43,21 @@ func AllInvariants(k Keeper) sdk.Invariant {
func NonNegativeOutstandingInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var msg string
var amt int
var count int
var outstanding sdk.DecCoins
k.IterateValidatorOutstandingRewards(ctx, func(addr sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool) {
outstanding = rewards
if outstanding.IsAnyNegative() {
amt++
count++
msg += fmt.Sprintf("\t%v has negative outstanding coins: %v\n", addr, outstanding)
}
return false
})
broken := amt != 0
broken := count != 0
return sdk.FormatInvariant(types.ModuleName, "nonnegative outstanding",
fmt.Sprintf("found %d validators with negative outstanding rewards\n%s", amt, msg), broken)
fmt.Sprintf("found %d validators with negative outstanding rewards\n%s", count, msg), broken)
}
}
@ -99,7 +99,7 @@ func CanWithdrawInvariant(k Keeper) sdk.Invariant {
broken := len(remaining) > 0 && remaining[0].Amount.LT(sdk.ZeroDec())
return sdk.FormatInvariant(types.ModuleName, "can withdraw",
fmt.Sprintf("remaining coins: %v", remaining), broken)
fmt.Sprintf("remaining coins: %v\n", remaining), broken)
}
}
@ -126,9 +126,10 @@ func ReferenceCountInvariant(k Keeper) sdk.Invariant {
count := k.GetValidatorHistoricalReferenceCount(ctx)
broken := count != expected
return sdk.FormatInvariant(types.ModuleName, "reference count", fmt.Sprintf("unexpected number of historical rewards records: "+
"expected %v (%v vals + %v dels + %v slashes), got %v",
expected, valCount, len(dels), slashCount, count), broken)
return sdk.FormatInvariant(types.ModuleName, "reference count",
fmt.Sprintf("expected historical reference count: %d = %v validators + %v delegations + %v slashes\n"+
"total validator historical reference count: %d\n",
expected, valCount, len(dels), slashCount, count), broken)
}
}
@ -149,7 +150,9 @@ func ModuleAccountInvariant(k Keeper) sdk.Invariant {
macc := k.GetDistributionAccount(ctx)
broken := !macc.GetCoins().IsEqual(expectedInt)
return sdk.FormatInvariant(types.ModuleName, "ModuleAccount coins", fmt.Sprintf("expected ModuleAccount coins: %s\n"+
"\tdistribution ModuleAccount coins : %s", expectedInt, macc.GetCoins()), broken)
return sdk.FormatInvariant(types.ModuleName, "ModuleAccount coins",
fmt.Sprintf("\texpected ModuleAccount coins: %s\n"+
"\tdistribution ModuleAccount coins: %s\n",
expectedInt, macc.GetCoins()), broken)
}
}

View File

@ -34,7 +34,7 @@ func ModuleAccountInvariant(keeper Keeper) sdk.Invariant {
broken := !macc.GetCoins().IsEqual(expectedDeposits)
return sdk.FormatInvariant(types.ModuleName, "deposits",
fmt.Sprintf("\tgov ModuleAccount coins: %s\n\tsum of deposit amounts: %s",
fmt.Sprintf("\tgov ModuleAccount coins: %s\n\tsum of deposit amounts: %s\n",
macc.GetCoins(), expectedDeposits), broken)
}
}

View File

@ -112,17 +112,17 @@ func NonNegativePowerInvariant(k Keeper) sdk.Invariant {
if !bytes.Equal(iterator.Key(), powerKey) {
broken = true
msg += fmt.Sprintf("power store invariance:\n\tvalidator.Power: %v"+
"\n\tkey should be: %v\n\tkey in store: %v",
"\n\tkey should be: %v\n\tkey in store: %v\n",
validator.GetConsensusPower(), powerKey, iterator.Key())
}
if validator.Tokens.IsNegative() {
broken = true
msg += fmt.Sprintf("negative tokens for validator: %v", validator)
msg += fmt.Sprintf("\tnegative tokens for validator: %v\n", validator)
}
}
iterator.Close()
return sdk.FormatInvariant(types.ModuleName, "nonnegative power", msg, broken)
return sdk.FormatInvariant(types.ModuleName, "nonnegative power", fmt.Sprintf("found invalid validator powers\n%s", msg), broken)
}
}
@ -130,23 +130,23 @@ func NonNegativePowerInvariant(k Keeper) sdk.Invariant {
func PositiveDelegationInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var msg string
var amt int
var count int
delegations := k.GetAllDelegations(ctx)
for _, delegation := range delegations {
if delegation.Shares.IsNegative() {
amt++
count++
msg += fmt.Sprintf("\tdelegation with negative shares: %+v\n", delegation)
}
if delegation.Shares.IsZero() {
amt++
count++
msg += fmt.Sprintf("\tdelegation with zero shares: %+v\n", delegation)
}
}
broken := amt != 0
broken := count != 0
return sdk.FormatInvariant(types.ModuleName, "positive delegations", fmt.Sprintf(
"%d invalid delegations found\n%s", amt, msg), broken)
"%d invalid delegations found\n%s", count, msg), broken)
}
}
@ -173,7 +173,7 @@ func DelegatorSharesInvariant(k Keeper) sdk.Invariant {
broken = true
msg += fmt.Sprintf("broken delegator shares invariance:\n"+
"\tvalidator.DelegatorShares: %v\n"+
"\tsum of Delegator.Shares: %v", valTotalDelShares, totalDelShares)
"\tsum of Delegator.Shares: %v\n", valTotalDelShares, totalDelShares)
}
}
return sdk.FormatInvariant(types.ModuleName, "delegator shares", msg, broken)

View File

@ -36,7 +36,7 @@ func TotalSupply(k Keeper) sdk.Invariant {
return sdk.FormatInvariant(types.ModuleName, "total supply",
fmt.Sprintf(
"\tsum of accounts coins: %v\n"+
"\tsupply.Total: %v",
"\tsupply.Total: %v\n",
expectedTotal, supply.Total), broken)
}
}