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
Next Next commit
Refactor x/evidence to ADR-031
  • Loading branch information
amaury1093 committed Oct 14, 2020
commit 0e89d1b93ddcbaa45b6b0b1044abd46211235b19
10 changes: 10 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 {
// Send submits an arbitrary Evidence of misbehavior such as equivocation or
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// 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,6 @@ message MsgSubmitEvidence {
string submitter = 1;
google.protobuf.Any evidence = 2 [(cosmos_proto.accepts_interface) = "Evidence"];
}

// MsgSubmitEvidenceResponse defines the Msg/SubmitEvidence response type.
message MsgSubmitEvidenceResponse {}
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
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
28 changes: 4 additions & 24 deletions x/evidence/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,22 @@ 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 {
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 := k.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
}
4 changes: 2 additions & 2 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,7 +113,7 @@ 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)
msg := tc.msg.(exported.MsgSubmitEvidenceI)
suite.Require().Equal(msg.GetEvidence().Hash().Bytes(), res.Data, "invalid hash; tc #%d", i)
}
}
Expand Down
4 changes: 2 additions & 2 deletions x/evidence/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func (k Keeper) GetEvidenceHandler(evidenceRoute string) (types.Handler, error)
return k.router.GetRoute(evidenceRoute), nil
}

// SubmitEvidence attempts to match evidence against the keepers router and execute
// submitEvidence attempts to match evidence against the keepers router and execute
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// the corresponding registered Evidence Handler. An error is returned if no
// registered Handler exists or if the Handler fails. Otherwise, the evidence is
// persisted.
func (k Keeper) SubmitEvidence(ctx sdk.Context, evidence exported.Evidence) error {
func (k Keeper) SubmitEvidenceI(ctx sdk.Context, evidence exported.Evidence) error {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func (k Keeper) SubmitEvidenceI(ctx sdk.Context, evidence exported.Evidence) error {
func (k Keeper) SubmitEvidence(ctx sdk.Context, evidence exported.Evidence) error {

Let's revert this. There is no EvidenceI interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay, i'll add a msgServer struct then

if _, ok := k.GetEvidence(ctx, evidence.Hash()); ok {
return sdkerrors.Wrap(types.ErrEvidenceExists, evidence.Hash().String())
}
Expand Down
10 changes: 5 additions & 5 deletions x/evidence/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (suite *KeeperTestSuite) populateEvidence(ctx sdk.Context, numEvidence int)
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
}

suite.Nil(suite.app.EvidenceKeeper.SubmitEvidence(ctx, evidence[i]))
suite.Nil(suite.app.EvidenceKeeper.SubmitEvidenceI(ctx, evidence[i]))
}

return evidence
Expand Down Expand Up @@ -147,7 +147,7 @@ func (suite *KeeperTestSuite) TestSubmitValidEvidence() {
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
}

suite.Nil(suite.app.EvidenceKeeper.SubmitEvidence(ctx, e))
suite.Nil(suite.app.EvidenceKeeper.SubmitEvidenceI(ctx, e))

res, ok := suite.app.EvidenceKeeper.GetEvidence(ctx, e.Hash())
suite.True(ok)
Expand All @@ -165,8 +165,8 @@ func (suite *KeeperTestSuite) TestSubmitValidEvidence_Duplicate() {
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
}

suite.Nil(suite.app.EvidenceKeeper.SubmitEvidence(ctx, e))
suite.Error(suite.app.EvidenceKeeper.SubmitEvidence(ctx, e))
suite.Nil(suite.app.EvidenceKeeper.SubmitEvidenceI(ctx, e))
suite.Error(suite.app.EvidenceKeeper.SubmitEvidenceI(ctx, e))

res, ok := suite.app.EvidenceKeeper.GetEvidence(ctx, e.Hash())
suite.True(ok)
Expand All @@ -183,7 +183,7 @@ func (suite *KeeperTestSuite) TestSubmitInvalidEvidence() {
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
}

suite.Error(suite.app.EvidenceKeeper.SubmitEvidence(ctx, e))
suite.Error(suite.app.EvidenceKeeper.SubmitEvidenceI(ctx, e))

res, ok := suite.app.EvidenceKeeper.GetEvidence(ctx, e.Hash())
suite.False(ok)
Expand Down
30 changes: 30 additions & 0 deletions x/evidence/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package keeper

import (
"context"

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

var _ types.MsgServer = Keeper{}

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

evidence := msg.GetEvidence()
if err := k.SubmitEvidenceI(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{}, 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