Skip to content
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

imp: do not store total escrow when amount is zero #3585

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions modules/apps/transfer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (k Keeper) IterateDenomTraces(ctx sdk.Context, cb func(denomTrace types.Den
func (k Keeper) GetTotalEscrowForDenom(ctx sdk.Context, denom string) sdk.Coin {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.TotalEscrowForDenomKey(denom))
if bz == nil {
if len(bz) == 0 {
return sdk.NewCoin(denom, sdk.ZeroInt())
}

Expand All @@ -169,8 +169,15 @@ func (k Keeper) SetTotalEscrowForDenom(ctx sdk.Context, coin sdk.Coin) {
}

store := ctx.KVStore(k.storeKey)
key := types.TotalEscrowForDenomKey(coin.Denom)

if coin.Amount.IsZero() {
store.Delete(key)
return
}
colin-axner marked this conversation as resolved.
Show resolved Hide resolved

bz := k.cdc.MustMarshal(&sdk.IntProto{Int: coin.Amount})
store.Set(types.TotalEscrowForDenomKey(coin.Denom), bz)
store.Set(key, bz)
}

// GetAllTotalEscrowed returns the escrow information for all the denominations.
Expand Down
20 changes: 18 additions & 2 deletions modules/apps/transfer/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (suite *KeeperTestSuite) TestSetGetTotalEscrowForDenom() {
expPass bool
}{
{
"success: with 0 escrow amount",
"success: with non-zero escrow amount",
func() {},
true,
},
Expand All @@ -71,6 +71,13 @@ func (suite *KeeperTestSuite) TestSetGetTotalEscrowForDenom() {
},
true,
},
{
"success: escrow amount 0 is not stored",
func() {
expAmount = math.ZeroInt()
},
true,
},
{
"failure: setter panics with negative escrow amount",
func() {
Expand All @@ -85,7 +92,7 @@ func (suite *KeeperTestSuite) TestSetGetTotalEscrowForDenom() {

suite.Run(tc.name, func() {
suite.SetupTest() // reset
expAmount = math.ZeroInt()
expAmount = math.NewInt(100)
ctx := suite.chainA.GetContext()

tc.malleate()
Expand All @@ -94,6 +101,15 @@ func (suite *KeeperTestSuite) TestSetGetTotalEscrowForDenom() {
suite.chainA.GetSimApp().TransferKeeper.SetTotalEscrowForDenom(ctx, sdk.NewCoin(denom, expAmount))
total := suite.chainA.GetSimApp().TransferKeeper.GetTotalEscrowForDenom(ctx, denom)
suite.Require().Equal(expAmount, total.Amount)

storeKey := suite.chainA.GetSimApp().GetKey(types.ModuleName)
store := ctx.KVStore(storeKey)
key := types.TotalEscrowForDenomKey(denom)
if expAmount.IsZero() {
suite.Require().False(store.Has(key))
} else {
suite.Require().True(store.Has(key))
}
} else {
suite.Require().PanicsWithError("negative coin amount: -1", func() {
suite.chainA.GetSimApp().TransferKeeper.SetTotalEscrowForDenom(ctx, sdk.NewCoin(denom, expAmount))
Expand Down