-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[R4R] Add Safety Measures to Coin/Coins #2797
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #2797 +/- ##
==========================================
Coverage ? 56.94%
==========================================
Files ? 120
Lines ? 8281
Branches ? 0
==========================================
Hits ? 4716
Misses ? 3247
Partials ? 318 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @alexanderbez, some comments:
return nil, sdk.ErrInsufficientFee(fmt.Sprintf("invalid fee amount: %s", feeAmount)).Result() | ||
} | ||
|
||
newCoins, ok := coins.SafeMinus(feeAmount) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we not using plain Minus
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because Minus
will panic on any negative amount! So here want to gracefully return the error. As mentioned in a similar conversation above, I think we can do IsAllLTE
here instead and then once sure, we can safely do subtraction.
oldCoins := getCoins(ctx, am, addr) | ||
newCoins := oldCoins.Minus(amt) | ||
if !newCoins.IsNotNegative() { | ||
newCoins, hasNeg := oldCoins.SafeMinus(amt) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A very few minor comments, otherwise LGTM.
types/coin.go
Outdated
diff := coins.Minus(coinsB) | ||
if len(diff) == 0 { | ||
diff, hasNeg := coins.SafeMinus(coinsB) | ||
if len(diff) == 0 || hasNeg { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is weird semantically. False isn't safe to return, esp when people might use !A.IsAllGT(B) as a synonym for A.IsAllLTE(B), etc.
types/coin.go
Outdated
func (coins Coins) IsAllGTE(coinsB Coins) bool { | ||
diff := coins.Minus(coinsB) | ||
diff, hasNeg := coins.SafeMinus(coinsB) | ||
if hasNeg { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
So initial attempts to move to unsigned integers caused headaches in implementation (e.g.
(A) - (B)
should panic) under given time constraints 😞 . So I see this PR as a big step in improving safety, but an ultimate refactor should still take place (#1273) to utilize unsigned integers.That being said, this PR does the following:
int
type, but adds safety checks and measures (panics) whenever amounts are negative.Minus
panics on any negative coin,SafeMinus
returns a bool if a negative coin exists.IsValid
to check if not positive.Equal
to obey theassociativity
axiom.plus
method to obey arithmetic laws.Uint
type.TL;DR Review
int.go
,coin.go
, andante.go
🍍closes: #2776
Targeted PR against correct branch (see CONTRIBUTING.md)
Linked to github-issue with discussion and accepted design OR link to spec that describes this work.
Wrote tests
Updated relevant documentation (
docs/
)Added entries in
PENDING.md
with issue #rereviewed
Files changed
in the github PR explorerFor Admin Use: