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

Refactor x/evidence to ADR-031 #7538

Merged
merged 9 commits into from
Oct 16, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Client Breaking

* (x/staking) [\#7499](https://github.com/cosmos/cosmos-sdk/pull/7499) `BondStatus` is now a protobuf `enum` instead of an `int32`, and JSON serialized using its protobuf name, so expect names like `BOND_STATUS_UNBONDING` as opposed to `Unbonding`.
* (x/evidence) [\#7538](https://github.com/cosmos/cosmos-sdk/pull/7538) The ABCI's `Result.Data` field of `MsgSubmitEvidence` does not contain the raw evidence's hash, but the encoded `MsgSubmitEvidenceResponse` struct.

### API Breaking

Expand Down
13 changes: 13 additions & 0 deletions proto/cosmos/evidence/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import "gogoproto/gogo.proto";
import "google/protobuf/any.proto";
import "cosmos_proto/cosmos.proto";

// Msg defines the evidence Msg service.
service Msg {
// SubmitEvidence submits an arbitrary Evidence of misbehavior such as equivocation or
// counterfactual signing.
rpc SubmitEvidence(MsgSubmitEvidence) returns (MsgSubmitEvidenceResponse);
}

// MsgSubmitEvidence represents a message that supports submitting arbitrary
// Evidence of misbehavior such as equivocation or counterfactual signing.
message MsgSubmitEvidence {
Expand All @@ -17,3 +24,9 @@ message MsgSubmitEvidence {
string submitter = 1;
google.protobuf.Any evidence = 2 [(cosmos_proto.accepts_interface) = "Evidence"];
}

// MsgSubmitEvidenceResponse defines the Msg/SubmitEvidence response type.
message MsgSubmitEvidenceResponse {
// hash defines the hash of the evidence.
bytes hash = 4;
}
4 changes: 2 additions & 2 deletions x/bank/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

// NewHandler returns a handler for "bank" type messages.
func NewHandler(k keeper.Keeper) sdk.Handler {
msgServer := keeper.NewMsgServerImpl(k)

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.MsgSend:
res, err := msgServer.Send(sdk.WrapSDKContext(ctx), msg)
Expand Down
4 changes: 2 additions & 2 deletions x/evidence/exported/evidence.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ type ValidatorEvidence interface {
GetTotalPower() int64
}

// MsgSubmitEvidence defines the specific interface a concrete message must
// MsgSubmitEvidenceI defines the specific interface a concrete message must
// implement in order to process submitted evidence. The concrete MsgSubmitEvidence
// must be defined at the application-level.
type MsgSubmitEvidence interface {
type MsgSubmitEvidenceI interface {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we still need an interface here?

Copy link
Member

Choose a reason for hiding this comment

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

And why the renaming?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why do we still need an interface here?

basically:

  • MsgSubmitEvidence is the wire type
  • MsgSubmitEvidenceI is the domain type, which the keeper manipulates

not strictly necessary, but i also don't want to introduce too many refactors here

And why the renaming?

it's confusing to have an interface and a struct having the same name.

sdk.Msg

GetEvidence() Evidence
Expand Down
30 changes: 6 additions & 24 deletions x/evidence/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,24 @@ package evidence
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
)

// NewHandler returns a handler for evidence messages.
func NewHandler(k keeper.Keeper) sdk.Handler {
msgServer := keeper.NewMsgServerImpl(k)

return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case exported.MsgSubmitEvidence:
return handleMsgSubmitEvidence(ctx, k, msg)
case *types.MsgSubmitEvidence:
res, err := msgServer.SubmitEvidence(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 handleMsgSubmitEvidence(ctx sdk.Context, k keeper.Keeper, msg exported.MsgSubmitEvidence) (*sdk.Result, error) {
evidence := msg.GetEvidence()
if err := k.SubmitEvidence(ctx, evidence); err != nil {
return nil, err
}

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

return &sdk.Result{
Data: evidence.Hash(),
Events: ctx.EventManager().ABCIEvents(),
}, nil
}
9 changes: 6 additions & 3 deletions x/evidence/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type HandlerTestSuite struct {
app *simapp.SimApp
}

func testMsgSubmitEvidence(r *require.Assertions, e exported.Evidence, s sdk.AccAddress) exported.MsgSubmitEvidence {
func testMsgSubmitEvidence(r *require.Assertions, e exported.Evidence, s sdk.AccAddress) exported.MsgSubmitEvidenceI {
msg, err := types.NewMsgSubmitEvidence(s, e)
r.NoError(err)
return msg
Expand Down Expand Up @@ -113,8 +113,11 @@ func (suite *HandlerTestSuite) TestMsgSubmitEvidence() {
suite.Require().NoError(err, "unexpected error; tc #%d", i)
suite.Require().NotNil(res, "expected non-nil result; tc #%d", i)

msg := tc.msg.(exported.MsgSubmitEvidence)
suite.Require().Equal(msg.GetEvidence().Hash().Bytes(), res.Data, "invalid hash; tc #%d", i)
msg := tc.msg.(exported.MsgSubmitEvidenceI)

var resultData types.MsgSubmitEvidenceResponse
suite.app.AppCodec().UnmarshalBinaryBare(res.Data, &resultData)
suite.Require().Equal(msg.GetEvidence().Hash().Bytes(), resultData.Hash, "invalid hash; tc #%d", i)
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions x/evidence/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/evidence/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{}

// SubmitEvidence implements the MsgServer.SubmitEvidence method.
func (ms msgServer) SubmitEvidence(goCtx context.Context, msg *types.MsgSubmitEvidence) (*types.MsgSubmitEvidenceResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

evidence := msg.GetEvidence()
if err := ms.Keeper.SubmitEvidence(ctx, evidence); err != nil {
return nil, err
}

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

return &types.MsgSubmitEvidenceResponse{
Hash: evidence.Hash(),
}, nil
}
2 changes: 1 addition & 1 deletion x/evidence/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
var (
_ sdk.Msg = &MsgSubmitEvidence{}
_ types.UnpackInterfacesMessage = MsgSubmitEvidence{}
_ exported.MsgSubmitEvidence = &MsgSubmitEvidence{}
_ exported.MsgSubmitEvidenceI = &MsgSubmitEvidence{}
)

// NewMsgSubmitEvidence returns a new MsgSubmitEvidence with a signer/submitter.
Expand Down
2 changes: 1 addition & 1 deletion x/evidence/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/evidence/types"
)

func testMsgSubmitEvidence(t *testing.T, e exported.Evidence, s sdk.AccAddress) exported.MsgSubmitEvidence {
func testMsgSubmitEvidence(t *testing.T, e exported.Evidence, s sdk.AccAddress) exported.MsgSubmitEvidenceI {
msg, err := types.NewMsgSubmitEvidence(s, e)
require.NoError(t, err)
return msg
Expand Down
Loading