refactor(evidence): move ValidateBasic logic to msgServer (#15753)

## Description

ref: https://github.com/cosmos/cosmos-sdk/issues/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)
This commit is contained in:
Julien Robert 2023-04-08 13:37:00 +02:00 committed by GitHub
parent 117a4268df
commit d09068f529
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 85 deletions

View File

@ -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

View File

@ -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=

View File

@ -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 {
@ -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
}

View File

@ -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,

View File

@ -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"
)
@ -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 {
@ -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
}

View File

@ -1,60 +0,0 @@
package types_test
import (
"testing"
"time"
"cosmossdk.io/x/evidence/exported"
"cosmossdk.io/x/evidence/types"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
)
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
}
func TestMsgSubmitEvidence(t *testing.T) {
pk := ed25519.GenPrivKey()
submitter := sdk.AccAddress("test________________")
testCases := []struct {
msg sdk.Msg
submitter sdk.AccAddress
expectErr bool
}{
{
testMsgSubmitEvidence(t, &types.Equivocation{
Height: 0,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: pk.PubKey().Address().String(),
}, submitter),
submitter,
true,
},
{
testMsgSubmitEvidence(t, &types.Equivocation{
Height: 10,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: pk.PubKey().Address().String(),
}, submitter),
submitter,
false,
},
}
for i, tc := range testCases {
require.Equal(t, sdk.MsgTypeURL(&types.MsgSubmitEvidence{}), sdk.MsgTypeURL(tc.msg), "unexpected result for tc #%d", i)
require.Equal(t, tc.expectErr, tc.msg.ValidateBasic() != nil, "unexpected result for tc #%d", i)
if !tc.expectErr {
require.Equal(t, tc.msg.GetSigners(), []sdk.AccAddress{tc.submitter}, "unexpected result for tc #%d", i)
}
}
}