github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evidence/handler_test.go (about)

     1  package evidence_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	"github.com/fibonacci-chain/fbc/x/evidence"
     8  	"github.com/fibonacci-chain/fbc/x/evidence/internal/types"
     9  
    10  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    11  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto/ed25519"
    12  	"github.com/stretchr/testify/suite"
    13  )
    14  
    15  type HandlerTestSuite struct {
    16  	suite.Suite
    17  
    18  	ctx     sdk.Context
    19  	handler sdk.Handler
    20  	keeper  evidence.Keeper
    21  }
    22  
    23  func (suite *HandlerTestSuite) SetupTest() {
    24  	checkTx := false
    25  	app := MakeFBChainApp()
    26  	// get the app's codec and register custom testing types
    27  	cdc := app.Codec()
    28  	cdc.RegisterConcrete(types.TestEquivocationEvidence{}, "test/TestEquivocationEvidence", nil)
    29  
    30  	// recreate keeper in order to use custom testing types
    31  	evidenceKeeper := evidence.NewKeeper(
    32  		cdc, app.GetKey(evidence.StoreKey), app.GetSubspace(evidence.ModuleName), app.StakingKeeper, app.SlashingKeeper,
    33  	)
    34  	router := evidence.NewRouter()
    35  	router = router.AddRoute(types.TestEvidenceRouteEquivocation, types.TestEquivocationHandler(*evidenceKeeper))
    36  	evidenceKeeper.SetRouter(router)
    37  
    38  	suite.ctx = app.BaseApp.NewContext(checkTx, abci.Header{Height: 1})
    39  	suite.handler = evidence.NewHandler(*evidenceKeeper)
    40  	suite.keeper = *evidenceKeeper
    41  }
    42  
    43  func (suite *HandlerTestSuite) TestMsgSubmitEvidence_Valid() {
    44  	pk := ed25519.GenPrivKey()
    45  	sv := types.TestVote{
    46  		ValidatorAddress: pk.PubKey().Address(),
    47  		Height:           11,
    48  		Round:            0,
    49  	}
    50  
    51  	sig, err := pk.Sign(sv.SignBytes(suite.ctx.ChainID()))
    52  	suite.NoError(err)
    53  	sv.Signature = sig
    54  
    55  	s := sdk.AccAddress("test")
    56  	e := types.TestEquivocationEvidence{
    57  		Power:      100,
    58  		TotalPower: 100000,
    59  		PubKey:     pk.PubKey(),
    60  		VoteA:      sv,
    61  		VoteB:      sv,
    62  	}
    63  
    64  	ctx := suite.ctx.WithIsCheckTx(false)
    65  	msg := evidence.NewMsgSubmitEvidence(e, s)
    66  	res, err := suite.handler(ctx, msg)
    67  	suite.NoError(err)
    68  	suite.NotNil(res)
    69  	suite.Equal(e.Hash().Bytes(), res.Data)
    70  }
    71  
    72  func (suite *HandlerTestSuite) TestMsgSubmitEvidence_Invalid() {
    73  	pk := ed25519.GenPrivKey()
    74  	sv := types.TestVote{
    75  		ValidatorAddress: pk.PubKey().Address(),
    76  		Height:           11,
    77  		Round:            0,
    78  	}
    79  
    80  	sig, err := pk.Sign(sv.SignBytes(suite.ctx.ChainID()))
    81  	suite.NoError(err)
    82  	sv.Signature = sig
    83  
    84  	s := sdk.AccAddress("test")
    85  	e := types.TestEquivocationEvidence{
    86  		Power:      100,
    87  		TotalPower: 100000,
    88  		PubKey:     pk.PubKey(),
    89  		VoteA:      sv,
    90  		VoteB:      types.TestVote{Height: 10, Round: 1},
    91  	}
    92  
    93  	ctx := suite.ctx.WithIsCheckTx(false)
    94  	msg := evidence.NewMsgSubmitEvidence(e, s)
    95  	res, err := suite.handler(ctx, msg)
    96  	suite.Error(err)
    97  	suite.Nil(res)
    98  }
    99  
   100  func TestHandlerTestSuite(t *testing.T) {
   101  	suite.Run(t, new(HandlerTestSuite))
   102  }