Merge PR #4832: print all failed invariants only

This commit is contained in:
colin axner 2019-08-05 11:21:44 -07:00 committed by Alexander Bezobchuk
parent a73b3f5e97
commit 865d473eb4
8 changed files with 16 additions and 21 deletions

View File

@ -0,0 +1 @@
# 4824 PrintAllInvariants flag will print all failed invariants

View File

@ -16,9 +16,7 @@ type InvariantRegistry interface {
RegisterRoute(moduleName, route string, invar Invariant) RegisterRoute(moduleName, route string, invar Invariant)
} }
// FormatInvariant returns a standardized invariant message along with // FormatInvariant returns a standardized invariant message.
// a boolean indicating whether the invariant has been broken. func FormatInvariant(module, name, msg string) string {
func FormatInvariant(module, name, msg string, broken bool) (string, bool) { return fmt.Sprintf("%s: %s invariant\n%s\n", module, name, msg)
return fmt.Sprintf("%s: %s invariant\n%sinvariant broken: %v\n",
module, name, msg, broken), broken
} }

View File

@ -32,6 +32,6 @@ func NonnegativeBalanceInvariant(ak types.AccountKeeper) sdk.Invariant {
broken := count != 0 broken := count != 0
return sdk.FormatInvariant(types.ModuleName, "nonnegative-outstanding", return sdk.FormatInvariant(types.ModuleName, "nonnegative-outstanding",
fmt.Sprintf("amount of negative accounts found %d\n%s", count, msg), broken) fmt.Sprintf("amount of negative accounts found %d\n%s", count, msg)), broken
} }
} }

View File

@ -57,7 +57,7 @@ func NonNegativeOutstandingInvariant(k Keeper) sdk.Invariant {
broken := count != 0 broken := count != 0
return sdk.FormatInvariant(types.ModuleName, "nonnegative outstanding", return sdk.FormatInvariant(types.ModuleName, "nonnegative outstanding",
fmt.Sprintf("found %d validators with negative outstanding rewards\n%s", count, 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()) broken := len(remaining) > 0 && remaining[0].Amount.LT(sdk.ZeroDec())
return sdk.FormatInvariant(types.ModuleName, "can withdraw", return sdk.FormatInvariant(types.ModuleName, "can withdraw",
fmt.Sprintf("remaining coins: %v\n", remaining), broken) fmt.Sprintf("remaining coins: %v\n", remaining)), broken
} }
} }
@ -129,7 +129,7 @@ func ReferenceCountInvariant(k Keeper) sdk.Invariant {
return sdk.FormatInvariant(types.ModuleName, "reference count", return sdk.FormatInvariant(types.ModuleName, "reference count",
fmt.Sprintf("expected historical reference count: %d = %v validators + %v delegations + %v slashes\n"+ fmt.Sprintf("expected historical reference count: %d = %v validators + %v delegations + %v slashes\n"+
"total validator historical reference count: %d\n", "total validator historical reference count: %d\n",
expected, valCount, len(dels), slashCount, count), broken) expected, valCount, len(dels), slashCount, count)), broken
} }
} }
@ -153,6 +153,6 @@ func ModuleAccountInvariant(k Keeper) sdk.Invariant {
return sdk.FormatInvariant(types.ModuleName, "ModuleAccount coins", return sdk.FormatInvariant(types.ModuleName, "ModuleAccount coins",
fmt.Sprintf("\texpected ModuleAccount coins: %s\n"+ fmt.Sprintf("\texpected ModuleAccount coins: %s\n"+
"\tdistribution ModuleAccount coins: %s\n", "\tdistribution ModuleAccount coins: %s\n",
expectedInt, macc.GetCoins()), broken) expectedInt, macc.GetCoins())), broken
} }
} }

View File

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

View File

@ -18,19 +18,15 @@ func assertAllInvariants(t *testing.T, app *baseapp.BaseApp, invs sdk.Invariants
ctx := app.NewContext(false, abci.Header{Height: app.LastBlockHeight() + 1}) ctx := app.NewContext(false, abci.Header{Height: app.LastBlockHeight() + 1})
var broken bool
var invariantResults []string var invariantResults []string
for i := 0; i < len(invs); i++ { for i := 0; i < len(invs); i++ {
res, stop := invs[i](ctx) res, stop := invs[i](ctx)
if stop { if stop {
broken = true
invariantResults = append(invariantResults, res)
} else if allInvariants {
invariantResults = append(invariantResults, res) invariantResults = append(invariantResults, res)
} }
} }
if broken { if len(invariantResults) > 0 {
fmt.Printf("Invariants broken after %s\n\n", event) fmt.Printf("Invariants broken after %s\n\n", event)
for _, res := range invariantResults { for _, res := range invariantResults {
fmt.Printf("%s\n", res) fmt.Printf("%s\n", res)

View File

@ -89,7 +89,7 @@ func ModuleAccountInvariants(k Keeper) sdk.Invariant {
"module accounts total (bonded + not bonded):\n"+ "module accounts total (bonded + not bonded):\n"+
"\tModule Accounts' tokens: %v\n"+ "\tModule Accounts' tokens: %v\n"+
"\tsum tokens: %v\n", "\tsum tokens: %v\n",
poolBonded, bonded, poolNotBonded, notBonded, poolBonded.Add(poolNotBonded), bonded.Add(notBonded)), broken) poolBonded, bonded, poolNotBonded, notBonded, poolBonded.Add(poolNotBonded), bonded.Add(notBonded))), broken
} }
} }
@ -122,7 +122,7 @@ func NonNegativePowerInvariant(k Keeper) sdk.Invariant {
} }
} }
iterator.Close() iterator.Close()
return sdk.FormatInvariant(types.ModuleName, "nonnegative power", fmt.Sprintf("found invalid validator powers\n%s", msg), broken) return sdk.FormatInvariant(types.ModuleName, "nonnegative power", fmt.Sprintf("found invalid validator powers\n%s", msg)), broken
} }
} }
@ -146,7 +146,7 @@ func PositiveDelegationInvariant(k Keeper) sdk.Invariant {
broken := count != 0 broken := count != 0
return sdk.FormatInvariant(types.ModuleName, "positive delegations", fmt.Sprintf( return sdk.FormatInvariant(types.ModuleName, "positive delegations", fmt.Sprintf(
"%d invalid delegations found\n%s", count, msg), broken) "%d invalid delegations found\n%s", count, msg)), broken
} }
} }
@ -176,6 +176,6 @@ func DelegatorSharesInvariant(k Keeper) sdk.Invariant {
"\tsum of Delegator.Shares: %v\n", valTotalDelShares, totalDelShares) "\tsum of Delegator.Shares: %v\n", valTotalDelShares, totalDelShares)
} }
} }
return sdk.FormatInvariant(types.ModuleName, "delegator shares", msg, broken) return sdk.FormatInvariant(types.ModuleName, "delegator shares", msg), broken
} }
} }

View File

@ -37,6 +37,6 @@ func TotalSupply(k Keeper) sdk.Invariant {
fmt.Sprintf( fmt.Sprintf(
"\tsum of accounts coins: %v\n"+ "\tsum of accounts coins: %v\n"+
"\tsupply.Total: %v\n", "\tsupply.Total: %v\n",
expectedTotal, supply.GetTotal()), broken) expectedTotal, supply.GetTotal())), broken
} }
} }