-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor x/auth/vesting to use ADR-031 (#7551)
* update auth/vesting module to use proto msg services * rm accidental tmp files * Update x/auth/vesting/msg_server.go Co-authored-by: Marie Gauthier <[email protected]> * golangci-lint fix Co-authored-by: Marie Gauthier <[email protected]> Co-authored-by: Aaron Craelius <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
65ba5a0
commit 67c1553
Showing
4 changed files
with
356 additions
and
97 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,26 @@ | ||
package vesting | ||
|
||
import ( | ||
"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/auth/keeper" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/vesting/types" | ||
) | ||
|
||
// NewHandler returns a handler for x/auth message types. | ||
func NewHandler(ak keeper.AccountKeeper, bk types.BankKeeper) sdk.Handler { | ||
msgServer := NewMsgServerImpl(ak, bk) | ||
|
||
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { | ||
ctx = ctx.WithEventManager(sdk.NewEventManager()) | ||
|
||
switch msg := msg.(type) { | ||
case *types.MsgCreateVestingAccount: | ||
return handleMsgCreateVestingAccount(ctx, ak, bk, msg) | ||
res, err := msgServer.CreateVestingAccount(sdk.WrapSDKContext(ctx), msg) | ||
return sdk.WrapServiceResult(ctx, res, err) | ||
|
||
default: | ||
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", types.ModuleName, msg) | ||
} | ||
} | ||
} | ||
|
||
func handleMsgCreateVestingAccount(ctx sdk.Context, ak keeper.AccountKeeper, bk types.BankKeeper, msg *types.MsgCreateVestingAccount) (*sdk.Result, error) { | ||
if err := bk.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 bk.BlockedAddr(to) { | ||
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.ToAddress) | ||
} | ||
|
||
if acc := ak.GetAccount(ctx, to); acc != nil { | ||
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "account %s already exists", msg.ToAddress) | ||
} | ||
|
||
baseAccount := ak.NewAccountWithAddress(ctx, to) | ||
if _, ok := baseAccount.(*authtypes.BaseAccount); !ok { | ||
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid account type; expected: BaseAccount, got: %T", baseAccount) | ||
} | ||
|
||
baseVestingAccount := types.NewBaseVestingAccount(baseAccount.(*authtypes.BaseAccount), msg.Amount.Sort(), msg.EndTime) | ||
|
||
var acc authtypes.AccountI | ||
|
||
if msg.Delayed { | ||
acc = types.NewDelayedVestingAccountRaw(baseVestingAccount) | ||
} else { | ||
acc = types.NewContinuousVestingAccountRaw(baseVestingAccount, ctx.BlockTime().Unix()) | ||
} | ||
|
||
ak.SetAccount(ctx, acc) | ||
|
||
defer func() { | ||
telemetry.IncrCounter(1, "new", "account") | ||
|
||
for _, a := range msg.Amount { | ||
if a.Amount.IsInt64() { | ||
telemetry.SetGaugeWithLabels( | ||
[]string{"tx", "msg", "create_vesting_account"}, | ||
float32(a.Amount.Int64()), | ||
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)}, | ||
) | ||
} | ||
} | ||
}() | ||
|
||
err = bk.SendCoins(ctx, from, to, msg.Amount) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
), | ||
) | ||
|
||
return &sdk.Result{Events: ctx.EventManager().ABCIEvents()}, nil | ||
} |
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,100 @@ | ||
package vesting | ||
|
||
import ( | ||
"context" | ||
|
||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
|
||
"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/auth/keeper" | ||
"github.com/cosmos/cosmos-sdk/x/auth/vesting/types" | ||
) | ||
|
||
type msgServer struct { | ||
keeper.AccountKeeper | ||
types.BankKeeper | ||
} | ||
|
||
// NewMsgServerImpl returns an implementation of the vesting MsgServer interface, | ||
// wrapping the corresponding AccountKeeper and BankKeeper. | ||
func NewMsgServerImpl(k keeper.AccountKeeper, bk types.BankKeeper) types.MsgServer { | ||
return &msgServer{AccountKeeper: k, BankKeeper: bk} | ||
} | ||
|
||
var _ types.MsgServer = msgServer{} | ||
|
||
func (s msgServer) CreateVestingAccount(goCtx context.Context, msg *types.MsgCreateVestingAccount) (*types.MsgCreateVestingAccountResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
ak := s.AccountKeeper | ||
bk := s.BankKeeper | ||
|
||
if err := bk.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 bk.BlockedAddr(to) { | ||
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.ToAddress) | ||
} | ||
|
||
if acc := ak.GetAccount(ctx, to); acc != nil { | ||
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "account %s already exists", msg.ToAddress) | ||
} | ||
|
||
baseAccount := ak.NewAccountWithAddress(ctx, to) | ||
if _, ok := baseAccount.(*authtypes.BaseAccount); !ok { | ||
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid account type; expected: BaseAccount, got: %T", baseAccount) | ||
} | ||
|
||
baseVestingAccount := types.NewBaseVestingAccount(baseAccount.(*authtypes.BaseAccount), msg.Amount.Sort(), msg.EndTime) | ||
|
||
var acc authtypes.AccountI | ||
|
||
if msg.Delayed { | ||
acc = types.NewDelayedVestingAccountRaw(baseVestingAccount) | ||
} else { | ||
acc = types.NewContinuousVestingAccountRaw(baseVestingAccount, ctx.BlockTime().Unix()) | ||
} | ||
|
||
ak.SetAccount(ctx, acc) | ||
|
||
defer func() { | ||
telemetry.IncrCounter(1, "new", "account") | ||
|
||
for _, a := range msg.Amount { | ||
if a.Amount.IsInt64() { | ||
telemetry.SetGaugeWithLabels( | ||
[]string{"tx", "msg", "create_vesting_account"}, | ||
float32(a.Amount.Int64()), | ||
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)}, | ||
) | ||
} | ||
} | ||
}() | ||
|
||
err = bk.SendCoins(ctx, from, to, msg.Amount) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
), | ||
) | ||
|
||
return &types.MsgCreateVestingAccountResponse{}, nil | ||
} |
Oops, something went wrong.