ensure akava balances are backed by, not equal to (#1230)

module balance
This commit is contained in:
Ruaridh 2022-04-28 16:01:51 +01:00 committed by GitHub
parent 30a071f83b
commit ab7b3ec82f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -24,10 +24,13 @@ func AllInvariants(bankK types.BankKeeper, k Keeper) sdk.Invariant {
} }
} }
// FullyBackedInvariant ensures all minor balances are backed exactly by the coins in the module account. // FullyBackedInvariant ensures all minor balances are backed by the coins in the module account.
//
// The module balance can be greater than the sum of all minor balances. This can happen in rare cases
// where the evm module burns tokens.
func FullyBackedInvariant(bankK types.BankKeeper, k Keeper) sdk.Invariant { func FullyBackedInvariant(bankK types.BankKeeper, k Keeper) sdk.Invariant {
broken := false broken := false
message := sdk.FormatInvariant(types.ModuleName, "fully backed broken", "minor balances do not match module account") message := sdk.FormatInvariant(types.ModuleName, "fully backed broken", "sum of minor balances greater than module account")
return func(ctx sdk.Context) (string, bool) { return func(ctx sdk.Context) (string, bool) {
@ -40,7 +43,7 @@ func FullyBackedInvariant(bankK types.BankKeeper, k Keeper) sdk.Invariant {
bankAddr := authtypes.NewModuleAddress(types.ModuleName) bankAddr := authtypes.NewModuleAddress(types.ModuleName)
bankBalance := bankK.GetBalance(ctx, bankAddr, CosmosDenom).Amount.Mul(ConversionMultiplier) bankBalance := bankK.GetBalance(ctx, bankAddr, CosmosDenom).Amount.Mul(ConversionMultiplier)
broken = !totalMinorBalances.Equal(bankBalance) broken = totalMinorBalances.GT(bankBalance)
return message, broken return message, broken
} }

View File

@ -87,11 +87,11 @@ func (suite *invariantTestSuite) TestFullyBackedInvariant() {
_, broken = suite.runInvariant("fully-backed", keeper.FullyBackedInvariant) _, broken = suite.runInvariant("fully-backed", keeper.FullyBackedInvariant)
suite.Equal(false, broken) suite.Equal(false, broken)
// break invariant by decreasing minor balances without decreasing module balance // break invariant by increasing total minor balances above module balance
suite.Keeper.RemoveBalance(suite.Ctx, suite.Addrs[0], sdk.OneInt()) suite.Keeper.AddBalance(suite.Ctx, suite.Addrs[0], sdk.OneInt())
message, broken := suite.runInvariant("fully-backed", keeper.FullyBackedInvariant) message, broken := suite.runInvariant("fully-backed", keeper.FullyBackedInvariant)
suite.Equal("evmutil: fully backed broken invariant\nminor balances do not match module account\n", message) suite.Equal("evmutil: fully backed broken invariant\nsum of minor balances greater than module account\n", message)
suite.Equal(true, broken) suite.Equal(true, broken)
} }