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 extra check in vesting #15373

Merged
merged 7 commits into from
Mar 14, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not be backported to v0.46.

if err := d.bankKeeper.IsSendEnabledCoins(ctx, coins...); err != nil {
return fmt.Errorf("cannot tip these coins: %w", err)
}
Comment on lines +53 to +55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Why not move the IsSendEnabledCoins check directly into SendCoins and avoid having to have this check elsewhere potentially missing it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made sense to me, however we don't know how people are using our API and expect them to work.
Do you think that behavior change on such a core feature is acceptable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would imagine if a coin cannot be sent, as dictated by SendEnabled, then I'd expect SendCoins to fail/error.

Copy link
Member Author

@julienrbrt julienrbrt Mar 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree. However, we don't know if module devs made another (even though wrong) assumption.
Say you have a module that allows an authority only to moves send disabled denom, then this change in the SDK would break it.
It is safer keeping the status quo, imho.

Copy link
Contributor

@alexanderbez alexanderbez Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that breaks the safety model then and introduces a misuse of the APIs. If such a case exists, then I would introduce a new SendCoinsUnsafe API or something similar that bypasses such checks.

Just my 2 cents. Not a hill I'll die on, so I'm happy to approve this PR if you really thing this is the ideal approach.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am more afraid to break, people's code by a change of code behavior.
I've looked deeper when is used IsSendEnabledCoins, and it is usually used way before calling SendCoins and before creating accounts in vesting, or in bank send in the SDK.
SendCoins on the other hand, is very, very much used and very commonly without that check: https://cs.github.com/?scopeName=All+repos&scope=&q=.SendCoins%28+language%3AGo.

I am going to merge it now, but this is maybe something we need to talk about. Personally, I agree with you, SendCoinsFromModuleToAccount checks if it is a blocked account, for instance, so we do have safety checks in another bank send method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM


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
}

Comment on lines +168 to +171
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we update changelog?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I'll do that in the backport PR, just to have on conflict less to fix in v0.47 :)

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More a question, what is the point in changing these

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compacter code but that's it

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