Skip to content

Commit

Permalink
refactor(evidence): move ValidateBasic logic to msgServer (#15753)
Browse files Browse the repository at this point in the history
## Description

ref: #15648

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

* [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
* [ ] added `!` to the type prefix if API or client breaking change
* [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
* [ ] provided a link to the relevant issue or specification
* [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules)
* [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
* [ ] added a changelog entry to `CHANGELOG.md`
* [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
* [ ] updated the relevant documentation or specification
* [ ] reviewed "Files changed" and left comments if necessary
* [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

* [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
* [ ] confirmed `!` in the type prefix if API or client breaking change
* [ ] confirmed all author checklist items have been addressed 
* [ ] reviewed state machine logic
* [ ] reviewed API design and naming
* [ ] reviewed documentation is accurate
* [ ] reviewed tests and test coverage
* [ ] manually tested (if applicable)
  • Loading branch information
julienrbrt committed Apr 8, 2023
1 parent 117a426 commit d09068f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 85 deletions.
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 {
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.

0 comments on commit d09068f

Please sign in to comment.