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(evidence): move ValidateBasic logic to msgServer #15753

Merged
merged 5 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x/evidence/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
cosmossdk.io/store v0.1.0-alpha.1.0.20230328185921-37ba88872dbc
github.com/cometbft/cometbft v0.37.0
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230330094838-d21f58c638d5
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230407193120-0a70647deaec
github.com/cosmos/gogoproto v1.4.7
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
Expand Down
4 changes: 2 additions & 2 deletions x/evidence/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ github.com/cosmos/cosmos-db v1.0.0-rc.1 h1:SjnT8B6WKMW9WEIX32qMhnEEKcI7ZP0+G1Sa9
github.com/cosmos/cosmos-db v1.0.0-rc.1/go.mod h1:Dnmk3flSf5lkwCqvvjNpoxjpXzhxnCAFzKHlbaForso=
github.com/cosmos/cosmos-proto v1.0.0-beta.3 h1:VitvZ1lPORTVxkmF2fAp3IiA61xVwArQYKXTdEcpW6o=
github.com/cosmos/cosmos-proto v1.0.0-beta.3/go.mod h1:t8IASdLaAq+bbHbjq4p960BvcTqtwuAxid3b/2rOD6I=
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230330094838-d21f58c638d5 h1:zO3mov9MaHWNnYZyQ8Wz/CZhrjfizMKvvLH41Ro/FYk=
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230330094838-d21f58c638d5/go.mod h1:aKJRE3RjbwJUFGKG+kTDLhrST9vzFr8FlsTlv4eD+80=
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230407193120-0a70647deaec h1:BE559vEyhAjljq+iyCGJsnVnpKl7QgYrJuzP4Ax1QDc=
github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230407193120-0a70647deaec/go.mod h1:lD11e/GdgJ5z2KCSN0DkXr0LFLXUrYUGIoF9cVvPU28=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
Expand Down
19 changes: 16 additions & 3 deletions x/evidence/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package keeper
import (
"context"

"cosmossdk.io/x/evidence/types"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"cosmossdk.io/x/evidence/types"
)

type msgServer struct {
Expand All @@ -22,9 +24,20 @@ 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)
if _, err := sdk.AccAddressFromBech32(msg.Submitter); err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid submitter address: %s", err)
}

evidence := msg.GetEvidence()
if evidence == nil {
return nil, errors.Wrap(types.ErrInvalidEvidence, "missing evidence")
}

if err := evidence.ValidateBasic(); err != nil {
return nil, errors.Wrapf(types.ErrInvalidEvidence, "failed basic validation: %s", err)
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := ms.Keeper.SubmitEvidence(ctx, evidence); err != nil {
return nil, err
}
Expand Down
14 changes: 14 additions & 0 deletions x/evidence/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ func (s *KeeperTestSuite) TestSubmitEvidence() {
expErr bool
expErrMsg string
}{
{
name: "invalid address",
req: &types.MsgSubmitEvidence{},
expErr: true,
expErrMsg: "invalid submitter address: empty address string is not allowed: invalid address",
},
{
name: "missing evidence",
req: &types.MsgSubmitEvidence{
Submitter: sdk.AccAddress(valAddresses[0]).String(),
},
expErr: true,
expErrMsg: "missing evidence: invalid evidence",
},
{
name: "invalid evidence with height 0",
req: invalidEvidence,
Expand Down
24 changes: 5 additions & 19 deletions x/evidence/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package types
import (
"fmt"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/x/evidence/exported"
"github.com/cosmos/gogoproto/proto"

"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)

Expand All @@ -33,23 +31,6 @@ func NewMsgSubmitEvidence(s sdk.AccAddress, evi exported.Evidence) (*MsgSubmitEv
return &MsgSubmitEvidence{Submitter: s.String(), Evidence: any}, nil
}

// ValidateBasic performs basic (non-state-dependant) validation on a MsgSubmitEvidence.
func (m MsgSubmitEvidence) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Submitter); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid submitter address: %s", err)
}

evi := m.GetEvidence()
if evi == nil {
return errorsmod.Wrap(ErrInvalidEvidence, "missing evidence")
}
if err := evi.ValidateBasic(); err != nil {
return err
}

return nil
}

// GetSignBytes returns the raw bytes a signer is expected to sign when submitting
// a MsgSubmitEvidence message.
func (m MsgSubmitEvidence) GetSignBytes() []byte {
Expand All @@ -63,10 +44,15 @@ func (m MsgSubmitEvidence) GetSigners() []sdk.AccAddress {
}

func (m MsgSubmitEvidence) GetEvidence() exported.Evidence {
if m.Evidence == nil {
Copy link
Member Author

Choose a reason for hiding this comment

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

Tests via message server catched that.

return nil
}

evi, ok := m.Evidence.GetCachedValue().(exported.Evidence)
if !ok {
return nil
}

return evi
}

Expand Down
60 changes: 0 additions & 60 deletions x/evidence/types/msgs_test.go

This file was deleted.