Skip to content

Commit

Permalink
[1658]: In IndexedAddrAmts.Add, if the coins to add is zero, don't do…
Browse files Browse the repository at this point in the history
… anything.
  • Loading branch information
SpicyLemon committed Oct 13, 2023
1 parent 2dcbd45 commit 994815e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions x/exchange/fulfillment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
69 changes: 69 additions & 0 deletions x/exchange/fulfillment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 994815e

Please sign in to comment.