cosmos-sdk/x/evidence/handler_test.go
Anil Kumar Kammari d55c1a2665
Change address from bytes to bech32 strings (#7242)
* init

* Fix bank proto messages

* missing conversions

* remove casttype for addresses

* Fix tests

* Fix consaddress

* more test fixes

* Fix tests

* fixed tests

* migrate missing proto declarations

* format

* Fix format

* Fix alignment

* Fix more tests

* Fix ibc merge issue

* Fix fmt

* Fix more tests

* Fix missing address declarations

* Fix staking tests

* Fix more tests

* Fix config

* fixed tests

* Fix more tests

* Update staking grpc tests

* Fix merge issue

* fixed failing tests in x/distr

* fixed sim tests

* fixed failing tests

* Fix bugs

* Add logs

* fixed slashing issue

* Fix staking grpc tests

* Fix all bank tests :)

* Fix tests in distribution

* Fix more tests in distr

* Fix slashing tests

* Fix statking tests

* Fix evidence tests

* Fix gov tests

* Fix bug in create vesting account

* Fix test

* remove fmt

* fixed gov tests

* fixed x/ibc tests

* fixed x/ibc-transfer tests

* fixed staking tests

* fixed staking tests

* fixed test

* fixed distribution issue

* fix pagination test

* fmt

* lint

* fix build

* fix format

* revert tally tests

* revert tally tests

* lint

* Fix sim test

* revert

* revert

* fixed tally issue

* fix tests

* revert

* fmt

* refactor

* remove `GetAddress()`

* remove fmt

* revert fmt.Striger usage

* Fix tests

* Fix rest test

* disable interfacer lint check

* make proto-format

* add nolint rule

* remove stray println

Co-authored-by: aleem1314 <aleem.md789@gmail.com>
Co-authored-by: atheesh <atheesh@vitwit.com>
2020-09-25 10:25:37 +00:00

125 lines
3.1 KiB
Go

package evidence_test
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/evidence"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
)
type HandlerTestSuite struct {
suite.Suite
handler sdk.Handler
app *simapp.SimApp
}
func testMsgSubmitEvidence(r *require.Assertions, e exported.Evidence, s sdk.AccAddress) exported.MsgSubmitEvidence {
msg, err := types.NewMsgSubmitEvidence(s, e)
r.NoError(err)
return msg
}
func testEquivocationHandler(k interface{}) types.Handler {
return func(ctx sdk.Context, e exported.Evidence) error {
if err := e.ValidateBasic(); err != nil {
return err
}
ee, ok := e.(*types.Equivocation)
if !ok {
return fmt.Errorf("unexpected evidence type: %T", e)
}
if ee.Height%2 == 0 {
return fmt.Errorf("unexpected even evidence height: %d", ee.Height)
}
return nil
}
}
func (suite *HandlerTestSuite) SetupTest() {
checkTx := false
app := simapp.Setup(checkTx)
// recreate keeper in order to use custom testing types
evidenceKeeper := keeper.NewKeeper(
app.AppCodec(), app.GetKey(types.StoreKey), app.StakingKeeper, app.SlashingKeeper,
)
router := types.NewRouter()
router = router.AddRoute(types.RouteEquivocation, testEquivocationHandler(*evidenceKeeper))
evidenceKeeper.SetRouter(router)
app.EvidenceKeeper = *evidenceKeeper
suite.handler = evidence.NewHandler(*evidenceKeeper)
suite.app = app
}
func (suite *HandlerTestSuite) TestMsgSubmitEvidence() {
pk := ed25519.GenPrivKey()
s := sdk.AccAddress("test________________")
testCases := []struct {
msg sdk.Msg
expectErr bool
}{
{
testMsgSubmitEvidence(
suite.Require(),
&types.Equivocation{
Height: 11,
Time: time.Now().UTC(),
Power: 100,
ConsensusAddress: pk.PubKey().Address().String(),
},
s,
),
false,
},
{
testMsgSubmitEvidence(
suite.Require(),
&types.Equivocation{
Height: 10,
Time: time.Now().UTC(),
Power: 100,
ConsensusAddress: pk.PubKey().Address().String(),
},
s,
),
true,
},
}
for i, tc := range testCases {
ctx := suite.app.BaseApp.NewContext(false, tmproto.Header{Height: suite.app.LastBlockHeight() + 1})
res, err := suite.handler(ctx, tc.msg)
if tc.expectErr {
suite.Require().Error(err, "expected error; tc #%d", i)
} else {
suite.Require().NoError(err, "unexpected error; tc #%d", i)
suite.Require().NotNil(res, "expected non-nil result; tc #%d", i)
msg := tc.msg.(exported.MsgSubmitEvidence)
suite.Require().Equal(msg.GetEvidence().Hash().Bytes(), res.Data, "invalid hash; tc #%d", i)
}
}
}
func TestHandlerTestSuite(t *testing.T) {
suite.Run(t, new(HandlerTestSuite))
}