Skip to content

Commit

Permalink
refactor: migrate x/evidence to mocks (#12749)
Browse files Browse the repository at this point in the history
## Description

Closes: #12501



---

### 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/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
JeancarloBarrios authored Aug 1, 2022
1 parent 30c5f20 commit cc5fe49
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 60 deletions.
3 changes: 2 additions & 1 deletion scripts/mockgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ $mockgen_cmd -source=x/params/proposal_handler_test.go -package testutil -destin
$mockgen_cmd -source=x/crisis/types/expected_keepers.go -package testutil -destination x/crisis/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/auth/types/expected_keepers.go -package testutil -destination x/auth/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/auth/ante/expected_keepers.go -package testutil -destination x/auth/ante/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/bank/types/expected_keepers.go -package testutil -destination x/bank/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/bank/types/expected_keepers.go -package testutil -destination x/bank/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/evidence/types/expected_keepers.go -package testutil -destination x/evidence/testutil/expected_keepers_mocks.go
2 changes: 1 addition & 1 deletion x/evidence/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (suite *KeeperTestSuite) TestQueryEvidence() {
true,
func(res *types.QueryEvidenceResponse) {
var evi exported.Evidence
err := suite.interfaceRegistry.UnpackAny(res.Evidence, &evi)
err := suite.encCfg.InterfaceRegistry.UnpackAny(res.Evidence, &evi)
suite.Require().NoError(err)
suite.Require().NotNil(evi)
suite.Require().Equal(evi, evidence[0])
Expand Down
72 changes: 68 additions & 4 deletions x/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
package keeper_test

import (
"time"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/testutil"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"time"
)

func (suite *KeeperTestSuite) TestHandleDoubleSign() {
type InfractionTestSuite struct {
suite.Suite

ctx sdk.Context
app *runtime.App

evidenceKeeper keeper.Keeper
bankKeeper bankkeeper.Keeper
accountKeeper authkeeper.AccountKeeper
slashingKeeper slashingkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
interfaceRegistry codectypes.InterfaceRegistry

queryClient types.QueryClient
}

func (suite *InfractionTestSuite) SetupTest() {
var (
evidenceKeeper keeper.Keeper
)

app, err := simtestutil.Setup(testutil.AppConfig,
&evidenceKeeper,
&suite.interfaceRegistry,
&suite.accountKeeper,
&suite.bankKeeper,
&suite.slashingKeeper,
&suite.stakingKeeper,
)
require.NoError(suite.T(), err)

router := types.NewRouter()
router = router.AddRoute(types.RouteEquivocation, testEquivocationHandler(evidenceKeeper))
evidenceKeeper.SetRouter(router)

suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: 1})
suite.app = app

suite.evidenceKeeper = evidenceKeeper
}

func (suite *InfractionTestSuite) TestHandleDoubleSign() {
ctx := suite.ctx.WithIsCheckTx(false).WithBlockHeight(1)
suite.populateValidators(ctx)

Expand Down Expand Up @@ -75,7 +128,7 @@ func (suite *KeeperTestSuite) TestHandleDoubleSign() {
suite.Len(evidences, 1)
}

func (suite *KeeperTestSuite) TestHandleDoubleSign_TooOld() {
func (suite *InfractionTestSuite) TestHandleDoubleSign_TooOld() {
ctx := suite.ctx.WithIsCheckTx(false).WithBlockHeight(1).WithBlockTime(time.Now())
suite.populateValidators(ctx)

Expand Down Expand Up @@ -111,3 +164,14 @@ func (suite *KeeperTestSuite) TestHandleDoubleSign_TooOld() {
suite.False(suite.stakingKeeper.Validator(ctx, operatorAddr).IsJailed())
suite.False(suite.slashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(val.Address())))
}

func (suite *InfractionTestSuite) populateValidators(ctx sdk.Context) {
// add accounts and set total supply
totalSupplyAmt := initAmt.MulRaw(int64(len(valAddresses)))
totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupplyAmt))
suite.NoError(suite.bankKeeper.MintCoins(ctx, minttypes.ModuleName, totalSupply))

for _, addr := range valAddresses {
suite.NoError(suite.bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, (sdk.AccAddress)(addr), initCoins))
}
}
94 changes: 40 additions & 54 deletions x/evidence/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,22 @@ package keeper_test
import (
"encoding/hex"
"fmt"
"time"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"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"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/runtime"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/testutil"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/stretchr/testify/suite"
)

var (
Expand Down Expand Up @@ -80,49 +73,53 @@ type KeeperTestSuite struct {

ctx sdk.Context
querier sdk.Querier
app *runtime.App

evidenceKeeper keeper.Keeper
bankKeeper bankkeeper.Keeper
accountKeeper authkeeper.AccountKeeper
slashingKeeper slashingkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
interfaceRegistry codectypes.InterfaceRegistry

queryClient types.QueryClient
evidenceKeeper keeper.Keeper
bankKeeper *evidencetestutil.MockBankKeeper
accountKeeper *evidencetestutil.MockAccountKeeper
slashingKeeper *evidencetestutil.MockSlashingKeeper
stakingKeeper *evidencetestutil.MockStakingKeeper
queryClient types.QueryClient
encCfg moduletestutil.TestEncodingConfig
}

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

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

stakingKeeper := evidencetestutil.NewMockStakingKeeper(ctrl)
slashingKeeper := evidencetestutil.NewMockSlashingKeeper(ctrl)
accountKeeper := evidencetestutil.NewMockAccountKeeper(ctrl)
bankKeeper := evidencetestutil.NewMockBankKeeper(ctrl)

evidenceKeeper := keeper.NewKeeper(
encCfg.Codec,
key,
stakingKeeper,
slashingKeeper,
)

app, err := simtestutil.Setup(testutil.AppConfig,
&evidenceKeeper,
&suite.interfaceRegistry,
&suite.accountKeeper,
&suite.bankKeeper,
&suite.slashingKeeper,
&suite.stakingKeeper,
)
require.NoError(suite.T(), err)
suite.stakingKeeper = stakingKeeper
suite.slashingKeeper = slashingKeeper
suite.bankKeeper = bankKeeper

router := types.NewRouter()
router = router.AddRoute(types.RouteEquivocation, testEquivocationHandler(evidenceKeeper))
evidenceKeeper.SetRouter(router)
suite.ctx = testCtx.WithBlockHeader(tmproto.Header{Height: 1})
suite.encCfg = moduletestutil.MakeTestEncodingConfig(evidence.AppModuleBasic{})

suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: 1})
suite.app = app
suite.accountKeeper = accountKeeper

for i, addr := range valAddresses {
addr := sdk.AccAddress(addr)
suite.accountKeeper.SetAccount(suite.ctx, authtypes.NewBaseAccount(addr, pubkeys[i], uint64(i), 0))
}

queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.interfaceRegistry)
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.encCfg.InterfaceRegistry)
types.RegisterQueryServer(queryHelper, evidenceKeeper)
suite.queryClient = types.NewQueryClient(queryHelper)
suite.evidenceKeeper = evidenceKeeper
suite.evidenceKeeper = *evidenceKeeper
}

func (suite *KeeperTestSuite) populateEvidence(ctx sdk.Context, numEvidence int) []exported.Evidence {
Expand All @@ -144,17 +141,6 @@ func (suite *KeeperTestSuite) populateEvidence(ctx sdk.Context, numEvidence int)
return evidence
}

func (suite *KeeperTestSuite) populateValidators(ctx sdk.Context) {
// add accounts and set total supply
totalSupplyAmt := initAmt.MulRaw(int64(len(valAddresses)))
totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupplyAmt))
suite.NoError(suite.bankKeeper.MintCoins(ctx, minttypes.ModuleName, totalSupply))

for _, addr := range valAddresses {
suite.NoError(suite.bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, (sdk.AccAddress)(addr), initCoins))
}
}

func (suite *KeeperTestSuite) TestSubmitValidEvidence() {
ctx := suite.ctx.WithIsCheckTx(false)
pk := ed25519.GenPrivKey()
Expand Down
Loading

0 comments on commit cc5fe49

Please sign in to comment.