Skip to content
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

fix: Add SafeSub for sdk.Coin #11630

Merged
merged 9 commits into from Apr 14, 2022
Merged
15 changes: 12 additions & 3 deletions types/coin.go
Expand Up @@ -118,16 +118,25 @@ func (coin Coin) AddAmount(amount Int) Coin {
// Sub subtracts amounts of two coins with same denom. If the coins differ in denom
// then it panics.
func (coin Coin) Sub(coinB Coin) Coin {
res, err := coin.SafeSub(coinB)
if err != nil {
panic(err)
}

return res
}

func (coin Coin) SafeSub(coinB Coin) (Coin, error) {
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
if coin.Denom != coinB.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom))
return Coin{}, fmt.Errorf("invalid coin denoms: %s, %s", coin.Denom, coinB.Denom)
}

res := Coin{coin.Denom, coin.Amount.Sub(coinB.Amount)}
if res.IsNegative() {
panic("negative coin amount")
return Coin{}, fmt.Errorf("negative coin amount")
}
atheeshp marked this conversation as resolved.
Show resolved Hide resolved

return res
return res, nil
}

// SubAmount subtracts an amount from the Coin.
Expand Down
24 changes: 24 additions & 0 deletions types/coin_test.go
Expand Up @@ -505,6 +505,30 @@ func (s *coinTestSuite) TestSubCoins() {
}
}

func (s *coinTestSuite) TestSafeSubCoin() {
cases := []struct {
inputOne sdk.Coin
inputTwo sdk.Coin
expected sdk.Coin
expErrMsg string
}{
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom2, 1), sdk.NewInt64Coin(testDenom1, 1), "invalid coin denoms"},
{sdk.NewInt64Coin(testDenom1, 10), sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 9), ""},
{sdk.NewInt64Coin(testDenom1, 5), sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom1, 5), ""},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 5), sdk.Coin{}, "negative coin amount"},
}

for _, tc := range cases {
tc := tc
res, err := tc.inputOne.SafeSub(tc.inputTwo)
if err != nil {
s.Require().Contains(err.Error(), tc.expErrMsg)
return
}
s.Require().Equal(tc.expected, res)
}
}

func (s *coinTestSuite) TestCoins_Validate() {
testCases := []struct {
name string
Expand Down
8 changes: 3 additions & 5 deletions x/staking/types/authz.go
Expand Up @@ -103,12 +103,10 @@ func (a StakeAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptRe
Updated: &StakeAuthorization{Validators: a.GetValidators(), AuthorizationType: a.GetAuthorizationType()}}, nil
}

// check sufficient balance exists.
if _, isNegative := sdk.NewCoins(*a.MaxTokens).SafeSub(sdk.NewCoins(amount)); isNegative {
return authz.AcceptResponse{}, sdkerrors.ErrInsufficientFunds.Wrapf("amount is more than max tokens")
limitLeft, err := a.MaxTokens.SafeSub(amount)
if err != nil {
return authz.AcceptResponse{}, sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "negative coin amount")
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
}

limitLeft := a.MaxTokens.Sub(amount)
if limitLeft.IsZero() {
return authz.AcceptResponse{Accept: true, Delete: true}, nil
}
Expand Down