Skip to content

Commit

Permalink
fix: add extra check in vesting (#15373)
Browse files Browse the repository at this point in the history
## Description

Closes: #XXXX



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
julienrbrt committed Mar 14, 2023
1 parent e90271a commit 721c24a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
9 changes: 8 additions & 1 deletion x/auth/posthandler/tips.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package posthandler

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -47,5 +49,10 @@ func (d tipDecorator) transferTip(ctx sdk.Context, sdkTx sdk.Tx) error {
return err
}

return d.bankKeeper.SendCoins(ctx, tipper, tipTx.FeePayer(), tipTx.GetTip().Amount)
coins := tipTx.GetTip().Amount
if err := d.bankKeeper.IsSendEnabledCoins(ctx, coins...); err != nil {
return fmt.Errorf("cannot tip these coins: %w", err)
}

return d.bankKeeper.SendCoins(ctx, tipper, tipTx.FeePayer(), coins)
}
19 changes: 19 additions & 0 deletions x/auth/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/auth/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

// BankKeeper defines the contract needed for supply related APIs (noalias)
type BankKeeper interface {
IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error
SendCoins(ctx sdk.Context, from, to sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
}
14 changes: 7 additions & 7 deletions x/auth/vesting/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func (s msgServer) CreateVestingAccount(goCtx context.Context, msg *types.MsgCre
}
}()

err = bk.SendCoins(ctx, from, to, msg.Amount)
if err != nil {
if err = bk.SendCoins(ctx, from, to, msg.Amount); err != nil {
return nil, err
}

Expand Down Expand Up @@ -135,8 +134,7 @@ func (s msgServer) CreatePermanentLockedAccount(goCtx context.Context, msg *type
}
}()

err = bk.SendCoins(ctx, from, to, msg.Amount)
if err != nil {
if err = bk.SendCoins(ctx, from, to, msg.Amount); err != nil {
return nil, err
}

Expand All @@ -163,11 +161,14 @@ func (s msgServer) CreatePeriodicVestingAccount(goCtx context.Context, msg *type
}

var totalCoins sdk.Coins

for _, period := range msg.VestingPeriods {
totalCoins = totalCoins.Add(period.Amount...)
}

if err := bk.IsSendEnabledCoins(ctx, totalCoins...); err != nil {
return nil, err
}

baseAccount := authtypes.NewBaseAccountWithAddress(to)
baseAccount = ak.NewAccount(ctx, baseAccount).(*authtypes.BaseAccount)
vestingAccount := types.NewPeriodicVestingAccount(baseAccount, totalCoins.Sort(), msg.StartTime, msg.VestingPeriods)
Expand All @@ -188,8 +189,7 @@ func (s msgServer) CreatePeriodicVestingAccount(goCtx context.Context, msg *type
}
}()

err = bk.SendCoins(ctx, from, to, totalCoins)
if err != nil {
if err = bk.SendCoins(ctx, from, to, totalCoins); err != nil {
return nil, err
}

Expand Down
14 changes: 9 additions & 5 deletions x/auth/vesting/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,15 @@ func (s *VestingTestSuite) TestCreatePermanentLockedAccount() {
}

func (s *VestingTestSuite) TestCreatePeriodicVestingAccount() {
testCases := map[string]struct {
testCases := []struct {
name string
preRun func()
input *vestingtypes.MsgCreatePeriodicVestingAccount
expErr bool
expErrMsg string
}{
"create for existing account": {
{
name: "create for existing account",
preRun: func() {
toAcc := s.accountKeeper.NewAccountWithAddress(s.ctx, to1Addr)
s.accountKeeper.SetAccount(s.ctx, toAcc)
Expand All @@ -213,8 +215,10 @@ func (s *VestingTestSuite) TestCreatePeriodicVestingAccount() {
expErr: true,
expErrMsg: "already exists",
},
"create a valid periodic vesting account": {
{
name: "create a valid periodic vesting account",
preRun: func() {
s.bankKeeper.EXPECT().IsSendEnabledCoins(gomock.Any(), periodCoin.Add(fooCoin)).Return(nil)
s.bankKeeper.EXPECT().SendCoins(gomock.Any(), fromAddr, to2Addr, gomock.Any()).Return(nil)
},
input: vestingtypes.NewMsgCreatePeriodicVestingAccount(
Expand All @@ -237,8 +241,8 @@ func (s *VestingTestSuite) TestCreatePeriodicVestingAccount() {
},
}

for name, tc := range testCases {
s.Run(name, func() {
for _, tc := range testCases {
s.Run(tc.name, func() {
tc.preRun()
_, err := s.msgServer.CreatePeriodicVestingAccount(s.ctx, tc.input)
if tc.expErr {
Expand Down

0 comments on commit 721c24a

Please sign in to comment.