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
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -175,6 +175,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* [\#11630](https://github.com/cosmos/cosmos-sdk/pull/11630) Added SafeSub method to avoid panic in Sub method for sdk.Coin
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
* [\#11511](https://github.com/cosmos/cosmos-sdk/pull/11511) Add api server flags to start command.
* [\#11484](https://github.com/cosmos/cosmos-sdk/pull/11484) Implement getter for keyring backend option.
* [\#11449](https://github.com/cosmos/cosmos-sdk/pull/11449) Improved error messages when node isn't synced.
Expand Down
20 changes: 15 additions & 5 deletions types/coin.go
Expand Up @@ -115,19 +115,29 @@ func (coin Coin) AddAmount(amount Int) Coin {
return Coin{coin.Denom, coin.Amount.Add(amount)}
}

// Sub subtracts amounts of two coins with same denom. If the coins differ in denom
// then it panics.
// Sub subtracts amounts of two coins with same denom and panics on error.
func (coin Coin) Sub(coinB Coin) Coin {
res, err := coin.SafeSub(coinB)
if err != nil {
panic(err)
}

return res
}

// SafeSub safely subtracts the amounts of two coins. It returns an error if the coins differ
// in denom or subtraction results in negative coin denom.
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