From 994815e36c94b45109e7306bfc6da2d7bfee21b2 Mon Sep 17 00:00:00 2001 From: Daniel Wedul Date: Fri, 13 Oct 2023 15:44:01 -0600 Subject: [PATCH] [1658]: In IndexedAddrAmts.Add, if the coins to add is zero, don't do anything. --- x/exchange/fulfillment.go | 3 ++ x/exchange/fulfillment_test.go | 69 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/x/exchange/fulfillment.go b/x/exchange/fulfillment.go index 36a6b2fdd3..3d4ad50f1e 100644 --- a/x/exchange/fulfillment.go +++ b/x/exchange/fulfillment.go @@ -103,6 +103,9 @@ func NewIndexedAddrAmts() *IndexedAddrAmts { // Add adds the coins to the given address. // Panics if a provided coin is invalid. func (i *IndexedAddrAmts) Add(addr string, coins ...sdk.Coin) { + if sdk.Coins(coins).IsZero() { + return + } for _, coin := range coins { if err := coin.Validate(); err != nil { panic(fmt.Errorf("cannot index and add invalid coin amount %q", coin)) diff --git a/x/exchange/fulfillment_test.go b/x/exchange/fulfillment_test.go index 61402f0b0f..23a0777ac4 100644 --- a/x/exchange/fulfillment_test.go +++ b/x/exchange/fulfillment_test.go @@ -1046,6 +1046,45 @@ func TestIndexedAddrAmts_Add(t *testing.T) { expected *IndexedAddrAmts expPanic string }{ + { + name: "empty, add zero coins", + receiver: NewIndexedAddrAmts(), + addr: "addr1", + coins: nil, + expected: NewIndexedAddrAmts(), + }, + { + name: "empty, add one coin with a zero value", + receiver: NewIndexedAddrAmts(), + addr: "addr1", + coins: []sdk.Coin{{Denom: "zero", Amount: sdkmath.ZeroInt()}}, + expected: NewIndexedAddrAmts(), + }, + { + name: "empty, add two coins with a zero value.", + receiver: NewIndexedAddrAmts(), + addr: "addr1", + coins: []sdk.Coin{ + {Denom: "zeroa", Amount: sdkmath.ZeroInt()}, + {Denom: "zerob", Amount: sdkmath.ZeroInt()}, + }, + expected: NewIndexedAddrAmts(), + }, + { + name: "empty, add three coins, only one is not zero", + receiver: NewIndexedAddrAmts(), + addr: "addr1", + coins: []sdk.Coin{ + {Denom: "coina", Amount: sdkmath.ZeroInt()}, + {Denom: "coinb", Amount: sdkmath.OneInt()}, + {Denom: "coinc", Amount: sdkmath.ZeroInt()}, + }, + expected: &IndexedAddrAmts{ + addrs: []string{"addr1"}, + amts: []sdk.Coins{coins("1coinb")}, + indexes: map[string]int{"addr1": 0}, + }, + }, { name: "empty, add one coin", receiver: NewIndexedAddrAmts(), @@ -1254,6 +1293,36 @@ func TestIndexedAddrAmts_Add(t *testing.T) { coins: negCoins, expPanic: "cannot index and add invalid coin amount \"-1neg\"", }, + { + name: "three addrs, add zero to existing", + receiver: &IndexedAddrAmts{ + addrs: []string{"addr1", "addr2", "addr3"}, + amts: []sdk.Coins{coins("1one"), coins("2two"), coins("3three")}, + indexes: map[string]int{"addr1": 0, "addr2": 1, "addr3": 2}, + }, + addr: "addr2", + coins: []sdk.Coin{{Denom: "zero", Amount: sdkmath.ZeroInt()}}, + expected: &IndexedAddrAmts{ + addrs: []string{"addr1", "addr2", "addr3"}, + amts: []sdk.Coins{coins("1one"), coins("2two"), coins("3three")}, + indexes: map[string]int{"addr1": 0, "addr2": 1, "addr3": 2}, + }, + }, + { + name: "three addrs, add zero to new", + receiver: &IndexedAddrAmts{ + addrs: []string{"addr1", "addr2", "addr3"}, + amts: []sdk.Coins{coins("1one"), coins("2two"), coins("3three")}, + indexes: map[string]int{"addr1": 0, "addr2": 1, "addr3": 2}, + }, + addr: "addr4", + coins: []sdk.Coin{{Denom: "zero", Amount: sdkmath.ZeroInt()}}, + expected: &IndexedAddrAmts{ + addrs: []string{"addr1", "addr2", "addr3"}, + amts: []sdk.Coins{coins("1one"), coins("2two"), coins("3three")}, + indexes: map[string]int{"addr1": 0, "addr2": 1, "addr3": 2}, + }, + }, } for _, tc := range tests {