Check correct usage of Quo()
hallazzang opened this issue · 5 comments
We should check whether the denominator is zero in many places where Quo()
is used.
Example:
Before:
if orderAmt.Quo(orderPrice).TruncateInt().IsZero() {
continue
}
After:
if types.IsDenominatorZero(orderAmt, orderPrice).Quo(orderPrice).TruncateInt().IsZero() {
continue
}
func IsDenominatorZero(x, y sdk.Dec) sdk.Dec {
if y.IsZero() {
return sdk.NewDec(0)
}
return x
}
I feel like this is extreme clutter so pulling the SDK types file locally and modifying the quo function to not throw an error (and instead return 0) when big int Quo returns infinite
@lukerhoads Thank you for your suggestions!
Returning 0 when the denominator is 0 might be a viable solution, but there are some cases where returning other values makes sense.
Then we can think of another solution:
func SafeQuo(a, b, c sdk.Dec) sdk.Dec {
if b.IsZero() {
return c
}
return a.Quo(b)
}
So this will return c
if b
is 0, thus c
is acting like a default value for infinite cases.
In this way, we can choose what to be returned from the safe quotient method.
But changing every code where Quo()
is used would be a huge work, so I'm gonna review the code and pick where to patch first.
Ahh, this is a much better solution. I appreciate the feedback, please let me know if I can help out at all.
Next time I will make sure to enable golang highlighting too haha
Thanks
Me and @dongsam checked codes that use Quo
thoroughly and found no serious problems other than in invariant checks for now.
I think we can close this issue.