Skip to content

Commit

Permalink
refactor: simplify logic for asserting invariant
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner committed Apr 27, 2023
1 parent 4361d2b commit 43a06e3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 29 deletions.
27 changes: 7 additions & 20 deletions modules/apps/transfer/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,27 @@ func AllInvariants(k *Keeper) sdk.Invariant {
// each denom is not smaller than the amount stored in the state entry.
func TotalEscrowPerDenomInvariants(k *Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
broken bool
totalEscrowedInAccounts sdk.Coins
)
var actualTotalEscrowed sdk.Coins

totalEscrowedInState := k.GetAllTotalEscrowed(ctx)
expectedTotalEscrowed := k.GetAllTotalEscrowed(ctx)

portID := k.GetPort(ctx)
transferChannels := k.channelKeeper.GetAllChannelsWithPortPrefix(ctx, portID)
for _, channel := range transferChannels {
escrowAddress := types.GetEscrowAddress(portID, channel.ChannelId)
escrowBalances := k.bankKeeper.GetAllBalances(ctx, escrowAddress)

totalEscrowedInAccounts = totalEscrowedInAccounts.Add(escrowBalances...)
actualTotalEscrowed = actualTotalEscrowed.Add(escrowBalances...)
}

for _, expectedEscrow := range totalEscrowedInState {
if found, actualEscrow := totalEscrowedInAccounts.Find(expectedEscrow.GetDenom()); found {
if expectedEscrow.Amount.GT(actualEscrow.Amount) {
broken = true
msg += fmt.Sprintf("\tdenom: %s, actual escrow (%s) is < expected escrow (%s)\n", expectedEscrow.GetDenom(), actualEscrow.Amount, expectedEscrow.Amount)
}
}
}

if broken {
// the total amount for each denom in escrow should be >= the amount stored in state for each denom
// the actual escrowed amount must be greater than or equal to the expected amount for all denominations
if !actualTotalEscrowed.IsAllGTE(expectedTotalEscrowed) {
return sdk.FormatInvariant(
types.ModuleName,
"total escrow per denom invariance",
fmt.Sprintf("found denom(s) with total escrow amount lower than expected:\n%s", msg)), broken
fmt.Sprintf("found denom(s) with total escrow amount lower than expected:\nactual total escrowed: %s\nexpected total escrowed: %s", actualTotalEscrowed, expectedTotalEscrowed)), true
}

return "", broken
return "", false
}
}
12 changes: 3 additions & 9 deletions modules/apps/transfer/keeper/invariants_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper_test

import (
"fmt"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -39,7 +37,6 @@ func (suite *KeeperTestSuite) TestTotalEscrowPerDenomInvariant() {
suite.SetupTest() // reset
path := NewTransferPath(suite.chainA, suite.chainB)
suite.coordinator.Setup(path)
ctx := suite.chainA.GetContext()

amount := math.NewInt(100)

Expand All @@ -54,23 +51,20 @@ func (suite *KeeperTestSuite) TestTotalEscrowPerDenomInvariant() {
suite.chainA.GetTimeoutHeight(), 0, "",
)

res, err := suite.chainA.GetSimApp().TransferKeeper.Transfer(sdk.WrapSDKContext(ctx), msg)
res, err := suite.chainA.SendMsgs(msg)
suite.Require().NoError(err)
suite.Require().NotNil(res)

tc.malleate()

out, broken := keeper.TotalEscrowPerDenomInvariants(&suite.chainA.GetSimApp().TransferKeeper)(ctx)
out, broken := keeper.TotalEscrowPerDenomInvariants(&suite.chainA.GetSimApp().TransferKeeper)(suite.chainA.GetContext())

if tc.expPass {
suite.Require().False(broken)
suite.Require().Empty(out)
} else {
suite.Require().True(broken)

escrow := suite.chainA.GetSimApp().TransferKeeper.GetTotalEscrowForDenom(ctx, sdk.DefaultBondDenom)
expOut := fmt.Sprintf("denom: %s, actual escrow (%s) is < expected escrow (%s)\n", sdk.DefaultBondDenom, amount, escrow)
suite.Require().Contains(out, expOut)
suite.Require().NotEmpty(out)
}
})
}
Expand Down

0 comments on commit 43a06e3

Please sign in to comment.