Skip to content

Commit

Permalink
[x/slashing] Implement Protobuf Msg Services (cosmos#7557)
Browse files Browse the repository at this point in the history
* Update x/slashing to use proto msg service

* Fix proto-gen

Co-authored-by: Aaron Craelius <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 15, 2020
1 parent 503b518 commit c59a04d
Show file tree
Hide file tree
Showing 6 changed files with 889 additions and 625 deletions.
17 changes: 13 additions & 4 deletions proto/cosmos/slashing/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ option (gogoproto.equal_all) = true;

import "gogoproto/gogo.proto";

// MsgUnjail is an sdk.Msg used for unjailing a jailed validator, thus returning
// them into the bonded validator set, so they can begin receiving provisions
// and rewards again.
// Msg defines the slashing Msg service.
service Msg {
// Unjail defines a method for unjailing a jailed validator, thus returning
// them into the bonded validator set, so they can begin receiving provisions
// and rewards again.
rpc Unjail(MsgUnjail) returns (MsgUnjailResponse);
}

// MsgUnjail defines the Msg/Unjail request type
message MsgUnjail {
option (gogoproto.goproto_getters) = false;
option (gogoproto.goproto_stringer) = true;

string validator_addr = 1 [(gogoproto.moretags) = "yaml:\"address\"", (gogoproto.jsontag) = "address"];
}
}

// MsgUnjailResponse defines the Msg/Unjail response type
message MsgUnjailResponse {}
9 changes: 5 additions & 4 deletions x/ibc/light-clients/07-tendermint/types/tendermint.pb.go

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

28 changes: 4 additions & 24 deletions x/slashing/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,15 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())

msgServer := keeper.NewMsgServerImpl(k)

switch msg := msg.(type) {
case *types.MsgUnjail:
return handleMsgUnjail(ctx, msg, k)
res, err := msgServer.Unjail(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)
}
}
}

// Validators must submit a transaction to unjail itself after
// having been jailed (and thus unbonded) for downtime
func handleMsgUnjail(ctx sdk.Context, msg *types.MsgUnjail, k keeper.Keeper) (*sdk.Result, error) {
valAddr, valErr := sdk.ValAddressFromBech32(msg.ValidatorAddr)
if valErr != nil {
return nil, valErr
}
err := k.Unjail(ctx, valAddr)
if err != nil {
return nil, err
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.ValidatorAddr),
),
)

return &sdk.Result{Events: ctx.EventManager().ABCIEvents()}, nil
}
46 changes: 46 additions & 0 deletions x/slashing/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)

type msgServer struct {
Keeper
}

// NewMsgServerImpl returns an implementation of the slashing MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}

var _ types.MsgServer = msgServer{}

// Unjail implements MsgServer.Unjail method.
// Validators must submit a transaction to unjail itself after
// having been jailed (and thus unbonded) for downtime
func (k msgServer) Unjail(goCtx context.Context, msg *types.MsgUnjail) (*types.MsgUnjailResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

valAddr, valErr := sdk.ValAddressFromBech32(msg.ValidatorAddr)
if valErr != nil {
return nil, valErr
}
err := k.Keeper.Unjail(ctx, valAddr)
if err != nil {
return nil, err
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.ValidatorAddr),
),
)

return &types.MsgUnjailResponse{}, nil
}
Loading

0 comments on commit c59a04d

Please sign in to comment.