Skip to content

Commit

Permalink
refactor: x/evidence audit changes (#14092)
Browse files Browse the repository at this point in the history
## Description

ref: #14063 



---

### 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
likhita-809 authored Dec 1, 2022
1 parent b9e0a40 commit 37a9bc3
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 29 deletions.
12 changes: 8 additions & 4 deletions api/cosmos/evidence/v1beta1/evidence.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions api/cosmos/evidence/v1beta1/tx.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions proto/cosmos/evidence/v1beta1/evidence.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ message Equivocation {
option (gogoproto.goproto_getters) = false;
option (gogoproto.equal) = false;

// height is the equivocation height.
int64 height = 1;

// time is the equivocation time.
google.protobuf.Timestamp time = 2
[(gogoproto.nullable) = false, (amino.dont_omitempty) = true, (gogoproto.stdtime) = true];

// power is the equivocation validator power.
int64 power = 3;

// consensus_address is the equivocation validator consensus address.
string consensus_address = 4 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}
3 changes: 3 additions & 0 deletions proto/cosmos/evidence/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ message MsgSubmitEvidence {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// submitter is the signer account address of evidence.
string submitter = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// evidence defines the evidence of misbehavior.
google.protobuf.Any evidence = 2 [(cosmos_proto.accepts_interface) = "Evidence"];
}

Expand Down
2 changes: 2 additions & 0 deletions x/evidence/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func QueryEvidenceCmd() func(*cobra.Command, []string) error {
}
}

// queryEvidence queries for a single evidence by the given hash.
func queryEvidence(clientCtx client.Context, hash string) error {
queryClient := types.NewQueryClient(clientCtx)

Expand All @@ -74,6 +75,7 @@ func queryEvidence(clientCtx client.Context, hash string) error {
return clientCtx.PrintProto(res.Evidence)
}

// queryAllEvidence returns all evidences.
func queryAllEvidence(clientCtx client.Context, pageReq *query.PageRequest) error {
queryClient := types.NewQueryClient(clientCtx)

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions x/evidence/client/evidence_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type (
}
)

// NewEvidenceHandler returns an EvidenceHandler.
func NewEvidenceHandler(cliHandler CLIHandlerFn) EvidenceHandler {
return EvidenceHandler{
CLIHandler: cliHandler,
Expand Down
21 changes: 16 additions & 5 deletions x/evidence/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
Expand All @@ -18,23 +19,31 @@ func (suite *KeeperTestSuite) TestQueryEvidence() {
msg string
malleate func()
expPass bool
expErrMsg string
posttests func(res *types.QueryEvidenceResponse)
}{
{
"empty request",
"invalid request with empty evidence hash",
func() {
req = &types.QueryEvidenceRequest{}
req = &types.QueryEvidenceRequest{Hash: ""}
},
false,
"invalid request; hash is empty",
func(res *types.QueryEvidenceResponse) {},
},
{
"invalid request with empty evidence hash",
"evidence not found",
func() {
req = &types.QueryEvidenceRequest{Hash: ""}
numEvidence := 1
evidence = suite.populateEvidence(suite.ctx, numEvidence)
evidenceHash := evidence[0].Hash().String()
reqHash := strings.Repeat("a", len(evidenceHash))
req = types.NewQueryEvidenceRequest(reqHash)
},
false,
func(res *types.QueryEvidenceResponse) {},
"not found",
func(res *types.QueryEvidenceResponse) {
},
},
{
"success",
Expand All @@ -44,6 +53,7 @@ func (suite *KeeperTestSuite) TestQueryEvidence() {
req = types.NewQueryEvidenceRequest(evidence[0].Hash().String())
},
true,
"",
func(res *types.QueryEvidenceResponse) {
var evi exported.Evidence
err := suite.encCfg.InterfaceRegistry.UnpackAny(res.Evidence, &evi)
Expand All @@ -66,6 +76,7 @@ func (suite *KeeperTestSuite) TestQueryEvidence() {
suite.Require().NotNil(res)
} else {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.expErrMsg)
suite.Require().Nil(res)
}

Expand Down
7 changes: 4 additions & 3 deletions x/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package keeper_test
import (
"time"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand All @@ -17,9 +21,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/testutil"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

type InfractionTestSuite struct {
Expand Down
1 change: 1 addition & 0 deletions x/evidence/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Keeper struct {
slashingKeeper types.SlashingKeeper
}

// NewKeeper creates a new Keeper object.
func NewKeeper(
cdc codec.BinaryCodec, storeKey storetypes.StoreKey, stakingKeeper types.StakingKeeper,
slashingKeeper types.SlashingKeeper,
Expand Down
23 changes: 15 additions & 8 deletions x/evidence/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/testutil"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/evidence"
evidencetestutil "github.com/cosmos/cosmos-sdk/x/evidence/testutil"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/evidence"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
evidencetestutil "github.com/cosmos/cosmos-sdk/x/evidence/testutil"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/stretchr/testify/suite"
)

var (
Expand Down Expand Up @@ -81,14 +81,15 @@ type KeeperTestSuite struct {
stakingKeeper *evidencetestutil.MockStakingKeeper
queryClient types.QueryClient
encCfg moduletestutil.TestEncodingConfig
msgServer types.MsgServer
}

func (suite *KeeperTestSuite) SetupTest() {
encCfg := moduletestutil.MakeTestEncodingConfig(evidence.AppModuleBasic{})
key := sdk.NewKVStoreKey(types.StoreKey)
tkey := sdk.NewTransientStoreKey("evidence_transient_store")
testCtx := testutil.DefaultContext(key, tkey)
suite.ctx = testCtx
testCtx := testutil.DefaultContextWithDB(suite.T(), key, tkey)
suite.ctx = testCtx.Ctx

ctrl := gomock.NewController(suite.T())

Expand All @@ -111,7 +112,8 @@ func (suite *KeeperTestSuite) SetupTest() {
router := types.NewRouter()
router = router.AddRoute(types.RouteEquivocation, testEquivocationHandler(evidenceKeeper))
evidenceKeeper.SetRouter(router)
suite.ctx = testCtx.WithBlockHeader(tmproto.Header{Height: 1})

suite.ctx = testCtx.Ctx.WithBlockHeader(tmproto.Header{Height: 1})
suite.encCfg = moduletestutil.MakeTestEncodingConfig(evidence.AppModuleBasic{})

suite.accountKeeper = accountKeeper
Expand All @@ -120,6 +122,11 @@ func (suite *KeeperTestSuite) SetupTest() {
types.RegisterQueryServer(queryHelper, evidenceKeeper)
suite.queryClient = types.NewQueryClient(queryHelper)
suite.evidenceKeeper = *evidenceKeeper

suite.Require().Equal(testCtx.Ctx.Logger().With("module", "x/"+types.ModuleName),
suite.evidenceKeeper.Logger(testCtx.Ctx))

suite.msgServer = keeper.NewMsgServerImpl(suite.evidenceKeeper)
}

func (suite *KeeperTestSuite) populateEvidence(ctx sdk.Context, numEvidence int) []exported.Evidence {
Expand Down
65 changes: 65 additions & 0 deletions x/evidence/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package keeper_test

import (
"time"

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

func (s *KeeperTestSuite) TestSubmitEvidence() {
pk := ed25519.GenPrivKey()

e := &types.Equivocation{
Height: 1,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
}

validEvidence, err := types.NewMsgSubmitEvidence(sdk.AccAddress(valAddresses[0]), e)
s.Require().NoError(err)

e2 := &types.Equivocation{
Height: 0,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: sdk.ConsAddress(pk.PubKey().Address().Bytes()).String(),
}

invalidEvidence, err := types.NewMsgSubmitEvidence(sdk.AccAddress(valAddresses[0]), e2)
s.Require().NoError(err)

testCases := []struct {
name string
req *types.MsgSubmitEvidence
expErr bool
expErrMsg string
}{
{
name: "invalid evidence with height 0",
req: invalidEvidence,
expErr: true,
expErrMsg: "invalid equivocation height",
},
{
name: "valid evidence",
req: validEvidence,
expErr: false,
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
_, err := s.msgServer.SubmitEvidence(s.ctx, tc.req)
if tc.expErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expErrMsg)
} else {
s.Require().NoError(err)
}
})
}
}
4 changes: 3 additions & 1 deletion x/evidence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type AppModuleBasic struct {
evidenceHandlers []eviclient.EvidenceHandler // eviclient evidence submission handlers
}

// NewAppModuleBasic crates a AppModuleBasic without the codec.
// NewAppModuleBasic creates a AppModuleBasic without the codec.
func NewAppModuleBasic(evidenceHandlers ...eviclient.EvidenceHandler) AppModuleBasic {
return AppModuleBasic{
evidenceHandlers: evidenceHandlers,
Expand Down Expand Up @@ -100,6 +100,7 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd()
}

// RegisterInterfaces registers the evidence module's interface types
func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
types.RegisterInterfaces(registry)
}
Expand All @@ -115,6 +116,7 @@ type AppModule struct {
keeper keeper.Keeper
}

// NewAppModule creates a new AppModule object.
func NewAppModule(keeper keeper.Keeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
Expand Down
1 change: 1 addition & 0 deletions x/evidence/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&Equivocation{}, "cosmos-sdk/Equivocation", nil)
}

// RegisterInterfaces registers the interfaces types with the interface registry.
func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSubmitEvidence{})
registry.RegisterInterface(
Expand Down
12 changes: 8 additions & 4 deletions x/evidence/types/evidence.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/evidence/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type QueryAllEvidenceParams struct {
Limit int `json:"limit" yaml:"limit"`
}

// NewQueryAllEvidenceParams creates a new instance to query all evidence params.
func NewQueryAllEvidenceParams(page, limit int) QueryAllEvidenceParams {
return QueryAllEvidenceParams{Page: page, Limit: limit}
}
6 changes: 4 additions & 2 deletions x/evidence/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 37a9bc3

Please sign in to comment.