Skip to content

Commit

Permalink
Merge PR #4832: print all failed invariants only
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner authored and alexanderbez committed Aug 5, 2019
1 parent a73b3f5 commit 865d473
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 21 deletions.
1 change: 1 addition & 0 deletions .pending/improvements/simulation/4824-PrintAllInvaria
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 4824 PrintAllInvariants flag will print all failed invariants
8 changes: 3 additions & 5 deletions types/invariant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion x/bank/internal/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
8 changes: 4 additions & 4 deletions x/distribution/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion x/gov/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 1 addition & 5 deletions x/simulation/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions x/staking/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand All @@ -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
}
}

Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion x/supply/internal/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 865d473

Please sign in to comment.