Skip to content

Commit

Permalink
perf: Refactor Coins/Validate to avoid unnecessary map (backport #14163
Browse files Browse the repository at this point in the history
…) (#14182)

Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
3 people authored Dec 8, 2022
1 parent 50f8de4 commit 4997976
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (genesis) [#14149](https://github.com/cosmos/cosmos-sdk/pull/14149) Add `simd genesis` command, which contains all genesis-related sub-commands.

### Improvements

* (types) [#14163](https://github.com/cosmos/cosmos-sdk/pull/14163) Refactor `(coins Coins) Validate()` to avoid unnecessary map.

## [v0.47.0-alpha2](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.47.0-alpha2) - 2022-12-06

### Improvements
Expand Down
11 changes: 4 additions & 7 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,26 +247,23 @@ func (coins Coins) Validate() error {
}

lowDenom := coins[0].Denom
seenDenoms := make(map[string]bool)
seenDenoms[lowDenom] = true

for _, coin := range coins[1:] {
if seenDenoms[coin.Denom] {
return fmt.Errorf("duplicate denomination %s", coin.Denom)
}
if err := ValidateDenom(coin.Denom); err != nil {
return err
}
if coin.Denom <= lowDenom {
if coin.Denom < lowDenom {
return fmt.Errorf("denomination %s is not sorted", coin.Denom)
}
if coin.Denom == lowDenom {
return fmt.Errorf("duplicate denomination %s", coin.Denom)
}
if !coin.IsPositive() {
return fmt.Errorf("coin %s amount is not positive", coin.Denom)
}

// we compare each coin against the last denom
lowDenom = coin.Denom
seenDenoms[coin.Denom] = true
}

return nil
Expand Down
29 changes: 28 additions & 1 deletion types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,15 @@ func (s *coinTestSuite) TestCoins_Validate() {
},
false,
},
{
"bad sort (3)",
sdk.Coins{
{"gas", math.OneInt()},
{"tree", math.OneInt()},
{"gas", math.OneInt()},
},
false,
},
{
"non-positive amount (1)",
sdk.Coins{
Expand All @@ -797,14 +806,32 @@ func (s *coinTestSuite) TestCoins_Validate() {
false,
},
{
"duplicate denomination",
"duplicate denomination (1)",
sdk.Coins{
{"gas", math.OneInt()},
{"gas", math.OneInt()},
{"mineral", math.OneInt()},
},
false,
},
{
"duplicate denomination (2)",
sdk.Coins{
{"gold", math.OneInt()},
{"gold", math.OneInt()},
},
false,
},
{
"duplicate denomination (3)",
sdk.Coins{
{"gas", math.OneInt()},
{"mineral", math.OneInt()},
{"silver", math.OneInt()},
{"silver", math.OneInt()},
},
false,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 4997976

Please sign in to comment.