Skip to content

Commit

Permalink
Refactor x/evidence to ADR-031 (#7538)
Browse files Browse the repository at this point in the history
* Refactor x/evidence to ADR-031

* Add hash in response

* Update changelog

* Update x/evidence/keeper/keeper.go

* Update proto/cosmos/evidence/v1beta1/tx.proto

Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>

* Use msgServer struct

Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
  • Loading branch information
amaury1093 and blushi authored Oct 16, 2020
1 parent 18ef33c commit 58eabcb
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 40 deletions.
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 {
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

0 comments on commit 58eabcb

Please sign in to comment.