-
Notifications
You must be signed in to change notification settings - Fork 633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
transfer (total escrow): add invariant for total escrow #3506
Merged
colin-axner
merged 5 commits into
feat/total-escrow-state-entry
from
carlos/add-invariant-total-escrow
May 4, 2023
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc6db2b
add invariant for total escrow
4361d2b
address review comment
43a06e3
refactor: simplify logic for asserting invariant
colin-axner d3d3bda
fix: use safeAdd instead of append
colin-axner 87240a3
Update modules/apps/transfer/keeper/keeper.go
colin-axner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" | ||
) | ||
|
||
// RegisterInvariants registers all transfer invariants | ||
func RegisterInvariants(ir sdk.InvariantRegistry, k *Keeper) { | ||
ir.RegisterRoute(types.ModuleName, "total-escrow-per-denom", | ||
TotalEscrowPerDenomInvariants(k)) | ||
} | ||
|
||
// AllInvariants runs all invariants of the transfer module. | ||
func AllInvariants(k *Keeper) sdk.Invariant { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should add a call to this in the tests? |
||
return func(ctx sdk.Context) (string, bool) { | ||
return TotalEscrowPerDenomInvariants(k)(ctx) | ||
} | ||
} | ||
|
||
// TotalEscrowPerDenomInvariants checks that the total amount escrowed for | ||
// 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 | ||
) | ||
|
||
totalEscrowedInState := k.GetAllTotalEscrowed(ctx) | ||
colin-axner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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...) | ||
} | ||
|
||
for _, expectedEscrow := range totalEscrowedInState { | ||
if found, actualEscrow := totalEscrowedInAccounts.Find(expectedEscrow.GetDenom()); found { | ||
broken = expectedEscrow.Amount.GT(actualEscrow.Amount) | ||
crodriguezvega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
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 | ||
} | ||
|
||
return "", broken | ||
DimitrisJim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"cosmossdk.io/math" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper" | ||
"github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestTotalEscrowPerDenomInvariant() { | ||
testCases := []struct { | ||
name string | ||
malleate func() | ||
expPass bool | ||
}{ | ||
{ | ||
"success", | ||
func() {}, | ||
true, | ||
}, | ||
{ | ||
"fails with broken invariant", | ||
func() { | ||
// set amount for denom higher than actual value in escrow | ||
amount := math.NewInt(200) | ||
suite.chainA.GetSimApp().TransferKeeper.SetTotalEscrowForDenom(suite.chainA.GetContext(), sdk.DefaultBondDenom, amount) | ||
}, | ||
false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
suite.Run(tc.name, func() { | ||
suite.SetupTest() // reset | ||
path := NewTransferPath(suite.chainA, suite.chainB) | ||
suite.coordinator.Setup(path) | ||
ctx := suite.chainA.GetContext() | ||
|
||
amount := math.NewInt(100) | ||
|
||
// send coins from chain A to chain B so that we have them in escrow | ||
coin := sdk.NewCoin(sdk.DefaultBondDenom, amount) | ||
msg := types.NewMsgTransfer( | ||
path.EndpointA.ChannelConfig.PortID, | ||
path.EndpointA.ChannelID, | ||
coin, | ||
suite.chainA.SenderAccount.GetAddress().String(), | ||
suite.chainB.SenderAccount.GetAddress().String(), | ||
suite.chainA.GetTimeoutHeight(), 0, "", | ||
) | ||
|
||
res, err := suite.chainA.GetSimApp().TransferKeeper.Transfer(sdk.WrapSDKContext(ctx), msg) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
|
||
tc.malleate() | ||
|
||
out, broken := keeper.TotalEscrowPerDenomInvariants(&suite.chainA.GetSimApp().TransferKeeper)(ctx) | ||
|
||
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming this string is arbitrary and doesn't hold too much significance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't seem to be treated as very significant in the sdk equivalent impl.