diff --git a/types/invariant.go b/types/invariant.go index 1f06a6f6e..fd90bd2b0 100644 --- a/types/invariant.go +++ b/types/invariant.go @@ -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 } diff --git a/x/bank/internal/keeper/invariants.go b/x/bank/internal/keeper/invariants.go index 20205da05..4196a176c 100644 --- a/x/bank/internal/keeper/invariants.go +++ b/x/bank/internal/keeper/invariants.go @@ -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) } } diff --git a/x/distribution/keeper/invariants.go b/x/distribution/keeper/invariants.go index 27c8a2691..6d8571475 100644 --- a/x/distribution/keeper/invariants.go +++ b/x/distribution/keeper/invariants.go @@ -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) } } diff --git a/x/gov/invariants.go b/x/gov/invariants.go index 5d9b9572b..25d2ac661 100644 --- a/x/gov/invariants.go +++ b/x/gov/invariants.go @@ -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) } } diff --git a/x/staking/keeper/invariants.go b/x/staking/keeper/invariants.go index 9a3c75d41..5880f41e7 100644 --- a/x/staking/keeper/invariants.go +++ b/x/staking/keeper/invariants.go @@ -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) diff --git a/x/supply/internal/keeper/invariants.go b/x/supply/internal/keeper/invariants.go index 9a5bbc6a5..20ef76c8f 100644 --- a/x/supply/internal/keeper/invariants.go +++ b/x/supply/internal/keeper/invariants.go @@ -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) } }