You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
types: Coins.safeAdd is more complex than needed with separated 2 different counters for indices of the various slices being added, and hard to read nor check for correctness
#6727
Closed
4 tasks
odeke-em opened this issue
Jul 15, 2020
· 1 comment
and there is one case that isn't tested, which is when higher lexicographic values of denomination are passed in, in the first Coin slice than the other.
If we asked a random person off the street to describe this algorithm and what it does, they'll likely take a while and stare at the code without figuring out what it does.
I believe we can totally simplify this logic by following the purpose and name of the algorithm and can thus simplify it
We want to add coins of the same denomination to each other
With that we don't need to special case conditions and this becomes just a grouping problem by a map, mapping denominations to their sums and that's it and here is the logic to simplify it
Coming here from an audit, I noticed the logic in Coins.safeAdd is quite hard to follow and uses 2 different counters for index i.e.
cosmos-sdk/types/coin.go
Lines 237 to 288 in 6f928e1
and there is one case that isn't tested, which is when higher lexicographic values of denomination are passed in, in the first Coin slice than the other.
If we asked a random person off the street to describe this algorithm and what it does, they'll likely take a while and stare at the code without figuring out what it does.
I believe we can totally simplify this logic by following the purpose and name of the algorithm and can thus simplify it
With that we don't need to special case conditions and this becomes just a grouping problem by a map, mapping denominations to their sums and that's it and here is the logic to simplify it
For Admin Use
The text was updated successfully, but these errors were encountered: