diff --git a/x/evidence/go.mod b/x/evidence/go.mod index c52527a8d0b6..ed8588accabe 100644 --- a/x/evidence/go.mod +++ b/x/evidence/go.mod @@ -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 diff --git a/x/evidence/go.sum b/x/evidence/go.sum index 8a7d50ce4c10..f19e42b247c4 100644 --- a/x/evidence/go.sum +++ b/x/evidence/go.sum @@ -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= diff --git a/x/evidence/keeper/msg_server.go b/x/evidence/keeper/msg_server.go index adb4fb94b659..f9a3296e43b4 100644 --- a/x/evidence/keeper/msg_server.go +++ b/x/evidence/keeper/msg_server.go @@ -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 } diff --git a/x/evidence/keeper/msg_server_test.go b/x/evidence/keeper/msg_server_test.go index 210377e11f51..2e5a29c21647 100644 --- a/x/evidence/keeper/msg_server_test.go +++ b/x/evidence/keeper/msg_server_test.go @@ -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, diff --git a/x/evidence/types/msgs.go b/x/evidence/types/msgs.go index 541cc372bd3b..10c747174d6c 100644 --- a/x/evidence/types/msgs.go +++ b/x/evidence/types/msgs.go @@ -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 } diff --git a/x/evidence/types/msgs_test.go b/x/evidence/types/msgs_test.go deleted file mode 100644 index c0c209c5d25a..000000000000 --- a/x/evidence/types/msgs_test.go +++ /dev/null @@ -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) - } - } -}