-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Refactor x/bank according to ADR 031 #7520
Merged
+586
−109
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2df86c3
Refactor x/bank according to ADR 031
aaronc 3208fd1
Add comment
aaronc 107b871
Update comment
aaronc 67a5419
Add comment
aaronc 34db518
Add tests, address edge cases
aaronc b07ab39
Imports
aaronc 8ac9c22
Merge branch 'master' into aaronc/7500-x-bank
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/armon/go-metrics" | ||
|
||
"github.com/cosmos/cosmos-sdk/telemetry" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/cosmos/cosmos-sdk/x/bank/types" | ||
) | ||
|
||
type msgServer struct { | ||
Keeper | ||
} | ||
|
||
// NewMsgServerImpl returns an implementation of the bank MsgServer interface | ||
// for the provided Keeper. | ||
func NewMsgServerImpl(keeper Keeper) types.MsgServer { | ||
return &msgServer{Keeper: keeper} | ||
} | ||
|
||
var _ types.MsgServer = msgServer{} | ||
|
||
func (k msgServer) Send(goCtx context.Context, msg *types.MsgSend) (*types.MsgSendResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
if err := k.SendEnabledCoins(ctx, msg.Amount...); err != nil { | ||
return nil, err | ||
} | ||
|
||
from, err := sdk.AccAddressFromBech32(msg.FromAddress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
to, err := sdk.AccAddressFromBech32(msg.ToAddress) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if k.BlockedAddr(to) { | ||
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.ToAddress) | ||
} | ||
|
||
err = k.SendCoins(ctx, from, to, msg.Amount) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func() { | ||
for _, a := range msg.Amount { | ||
telemetry.SetGaugeWithLabels( | ||
[]string{"tx", "msg", "send"}, | ||
float32(a.Amount.Int64()), | ||
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)}, | ||
) | ||
} | ||
}() | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
), | ||
) | ||
|
||
return &types.MsgSendResponse{}, nil | ||
} | ||
|
||
func (k msgServer) MultiSend(goCtx context.Context, msg *types.MsgMultiSend) (*types.MsgMultiSendResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
// NOTE: totalIn == totalOut should already have been checked | ||
for _, in := range msg.Inputs { | ||
if err := k.SendEnabledCoins(ctx, in.Coins...); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
for _, out := range msg.Outputs { | ||
accAddr, err := sdk.AccAddressFromBech32(out.Address) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if k.BlockedAddr(accAddr) { | ||
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive transactions", out.Address) | ||
} | ||
} | ||
|
||
err := k.InputOutputCoins(ctx, msg.Inputs, msg.Outputs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
), | ||
) | ||
|
||
return &types.MsgMultiSendResponse{}, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why implement
MsgServer
on a separate struct, and not on Keeper itself?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the weird way bank has a keeper interface. Other modules should do what you're saying. And we should refactor bank in the near future anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^ cc @atheeshp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in few cases we still needed this implementation,
https://github.com/cosmos/cosmos-sdk/pull/7524/files#diff-825d33eeaa8e743955334330c753008b9af67e55c61d03b348556a246b9b4bddR94
in this scenario few of the keeper functions are colliding with the msg sever functions. wdyt @amaurymartiny, @aaronc ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use a separate struct wrapping the keeper then