From 865d473eb432ffdfd4f75638cea7e16af2bd17a0 Mon Sep 17 00:00:00 2001 From: colin axner Date: Mon, 5 Aug 2019 11:21:44 -0700 Subject: [PATCH] Merge PR #4832: print all failed invariants only --- .pending/improvements/simulation/4824-PrintAllInvaria | 1 + types/invariant.go | 8 +++----- x/bank/internal/keeper/invariants.go | 2 +- x/distribution/keeper/invariants.go | 8 ++++---- x/gov/invariants.go | 2 +- x/simulation/util.go | 6 +----- x/staking/keeper/invariants.go | 8 ++++---- x/supply/internal/keeper/invariants.go | 2 +- 8 files changed, 16 insertions(+), 21 deletions(-) create mode 100644 .pending/improvements/simulation/4824-PrintAllInvaria diff --git a/.pending/improvements/simulation/4824-PrintAllInvaria b/.pending/improvements/simulation/4824-PrintAllInvaria new file mode 100644 index 0000000000..fd23692f93 --- /dev/null +++ b/.pending/improvements/simulation/4824-PrintAllInvaria @@ -0,0 +1 @@ +# 4824 PrintAllInvariants flag will print all failed invariants \ No newline at end of file diff --git a/types/invariant.go b/types/invariant.go index fd90bd2b02..1e7cae7f8b 100644 --- a/types/invariant.go +++ b/types/invariant.go @@ -16,9 +16,7 @@ type InvariantRegistry interface { RegisterRoute(moduleName, route string, invar Invariant) } -// FormatInvariant returns a standardized invariant message along with -// 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%sinvariant broken: %v\n", - module, name, msg, broken), broken +// FormatInvariant returns a standardized invariant message. +func FormatInvariant(module, name, msg string) string { + return fmt.Sprintf("%s: %s invariant\n%s\n", module, name, msg) } diff --git a/x/bank/internal/keeper/invariants.go b/x/bank/internal/keeper/invariants.go index fe7e6368b3..0be1e8f58f 100644 --- a/x/bank/internal/keeper/invariants.go +++ b/x/bank/internal/keeper/invariants.go @@ -32,6 +32,6 @@ func NonnegativeBalanceInvariant(ak types.AccountKeeper) sdk.Invariant { broken := count != 0 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 } } diff --git a/x/distribution/keeper/invariants.go b/x/distribution/keeper/invariants.go index 6d85714757..52b735ec22 100644 --- a/x/distribution/keeper/invariants.go +++ b/x/distribution/keeper/invariants.go @@ -57,7 +57,7 @@ func NonNegativeOutstandingInvariant(k Keeper) sdk.Invariant { broken := count != 0 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()) 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", 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) + expected, valCount, len(dels), slashCount, count)), broken } } @@ -153,6 +153,6 @@ func ModuleAccountInvariant(k Keeper) sdk.Invariant { return sdk.FormatInvariant(types.ModuleName, "ModuleAccount coins", fmt.Sprintf("\texpected ModuleAccount coins: %s\n"+ "\tdistribution ModuleAccount coins: %s\n", - expectedInt, macc.GetCoins()), broken) + expectedInt, macc.GetCoins())), broken } } diff --git a/x/gov/invariants.go b/x/gov/invariants.go index 25d2ac661b..9c98262f5a 100644 --- a/x/gov/invariants.go +++ b/x/gov/invariants.go @@ -35,6 +35,6 @@ func ModuleAccountInvariant(keeper Keeper) sdk.Invariant { return sdk.FormatInvariant(types.ModuleName, "deposits", fmt.Sprintf("\tgov ModuleAccount coins: %s\n\tsum of deposit amounts: %s\n", - macc.GetCoins(), expectedDeposits), broken) + macc.GetCoins(), expectedDeposits)), broken } } diff --git a/x/simulation/util.go b/x/simulation/util.go index 0eeabe01e0..5ed572b94d 100644 --- a/x/simulation/util.go +++ b/x/simulation/util.go @@ -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}) - var broken bool var invariantResults []string for i := 0; i < len(invs); i++ { res, stop := invs[i](ctx) if stop { - broken = true - invariantResults = append(invariantResults, res) - } else if allInvariants { invariantResults = append(invariantResults, res) } } - if broken { + if len(invariantResults) > 0 { fmt.Printf("Invariants broken after %s\n\n", event) for _, res := range invariantResults { fmt.Printf("%s\n", res) diff --git a/x/staking/keeper/invariants.go b/x/staking/keeper/invariants.go index 5880f41e70..e89cfab1d0 100644 --- a/x/staking/keeper/invariants.go +++ b/x/staking/keeper/invariants.go @@ -89,7 +89,7 @@ func ModuleAccountInvariants(k Keeper) sdk.Invariant { "module accounts total (bonded + not bonded):\n"+ "\tModule Accounts' 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() - 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 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) } } - return sdk.FormatInvariant(types.ModuleName, "delegator shares", msg, broken) + return sdk.FormatInvariant(types.ModuleName, "delegator shares", msg), broken } } diff --git a/x/supply/internal/keeper/invariants.go b/x/supply/internal/keeper/invariants.go index 9a4ebb2a79..d72bf4e013 100644 --- a/x/supply/internal/keeper/invariants.go +++ b/x/supply/internal/keeper/invariants.go @@ -37,6 +37,6 @@ func TotalSupply(k Keeper) sdk.Invariant { fmt.Sprintf( "\tsum of accounts coins: %v\n"+ "\tsupply.Total: %v\n", - expectedTotal, supply.GetTotal()), broken) + expectedTotal, supply.GetTotal())), broken } }