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

     1  package keeper_test
     2  
     3  import (
     4  	"encoding/hex"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/fibonacci-chain/fbc/app"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    10  	"github.com/fibonacci-chain/fbc/libs/tendermint/libs/log"
    11  	dbm "github.com/fibonacci-chain/fbc/libs/tm-db"
    12  
    13  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    14  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply"
    15  	"github.com/fibonacci-chain/fbc/x/evidence"
    16  	"github.com/fibonacci-chain/fbc/x/evidence/exported"
    17  	"github.com/fibonacci-chain/fbc/x/evidence/internal/keeper"
    18  	"github.com/fibonacci-chain/fbc/x/evidence/internal/types"
    19  
    20  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    21  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto"
    22  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto/ed25519"
    23  	"github.com/stretchr/testify/suite"
    24  )
    25  
    26  var (
    27  	pubkeys = []crypto.PubKey{
    28  		newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB50"),
    29  		newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"),
    30  		newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"),
    31  	}
    32  
    33  	valAddresses = []sdk.ValAddress{
    34  		sdk.ValAddress(pubkeys[0].Address()),
    35  		sdk.ValAddress(pubkeys[1].Address()),
    36  		sdk.ValAddress(pubkeys[2].Address()),
    37  	}
    38  
    39  	initAmt   = sdk.NewIntFromUint64(20000)
    40  	initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt))
    41  )
    42  
    43  func newPubKey(pk string) (res crypto.PubKey) {
    44  	pkBytes, err := hex.DecodeString(pk)
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  
    49  	var pubkey ed25519.PubKeyEd25519
    50  	copy(pubkey[:], pkBytes)
    51  
    52  	return pubkey
    53  }
    54  
    55  type KeeperTestSuite struct {
    56  	suite.Suite
    57  
    58  	ctx     sdk.Context
    59  	querier sdk.Querier
    60  	keeper  keeper.Keeper
    61  	app     *app.FBChainApp
    62  }
    63  
    64  func MakeFBChainApp() *app.FBChainApp {
    65  	genesisState := app.NewDefaultGenesisState()
    66  	db := dbm.NewMemDB()
    67  	fbapp := app.NewFBChainApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, 0)
    68  
    69  	stateBytes, err := codec.MarshalJSONIndent(fbapp.Codec(), genesisState)
    70  	if err != nil {
    71  		panic(err)
    72  	}
    73  	fbapp.InitChain(
    74  		abci.RequestInitChain{
    75  			Validators:    []abci.ValidatorUpdate{},
    76  			AppStateBytes: stateBytes,
    77  		},
    78  	)
    79  	return fbapp
    80  }
    81  
    82  func (suite *KeeperTestSuite) SetupTest() {
    83  	checkTx := false
    84  
    85  	app := MakeFBChainApp()
    86  	// get the app's codec and register custom testing types
    87  	cdc := app.Codec()
    88  	cdc.RegisterConcrete(types.TestEquivocationEvidence{}, "test/TestEquivocationEvidence", nil)
    89  
    90  	// recreate keeper in order to use custom testing types
    91  	evidenceKeeper := evidence.NewKeeper(
    92  		cdc, app.GetKey(evidence.StoreKey), app.GetSubspace(evidence.ModuleName), app.StakingKeeper, app.SlashingKeeper,
    93  	)
    94  	router := evidence.NewRouter()
    95  	router = router.AddRoute(types.TestEvidenceRouteEquivocation, types.TestEquivocationHandler(*evidenceKeeper))
    96  	evidenceKeeper.SetRouter(router)
    97  
    98  	suite.ctx = app.BaseApp.NewContext(checkTx, abci.Header{Height: 1})
    99  	suite.querier = keeper.NewQuerier(*evidenceKeeper)
   100  	suite.keeper = *evidenceKeeper
   101  	suite.app = app
   102  }
   103  
   104  func (suite *KeeperTestSuite) populateEvidence(ctx sdk.Context, numEvidence int) []exported.Evidence {
   105  	evidence := make([]exported.Evidence, numEvidence)
   106  
   107  	for i := 0; i < numEvidence; i++ {
   108  		pk := ed25519.GenPrivKey()
   109  		sv := types.TestVote{
   110  			ValidatorAddress: pk.PubKey().Address(),
   111  			Height:           int64(i),
   112  			Round:            0,
   113  		}
   114  
   115  		sig, err := pk.Sign(sv.SignBytes(ctx.ChainID()))
   116  		suite.NoError(err)
   117  		sv.Signature = sig
   118  
   119  		evidence[i] = types.TestEquivocationEvidence{
   120  			Power:      100,
   121  			TotalPower: 100000,
   122  			PubKey:     pk.PubKey(),
   123  			VoteA:      sv,
   124  			VoteB:      sv,
   125  		}
   126  
   127  		suite.Nil(suite.keeper.SubmitEvidence(ctx, evidence[i]))
   128  	}
   129  
   130  	return evidence
   131  }
   132  
   133  func (suite *KeeperTestSuite) populateValidators(ctx sdk.Context) {
   134  	// add accounts and set total supply
   135  	totalSupplyAmt := initAmt.MulRaw(int64(len(valAddresses)))
   136  	totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupplyAmt))
   137  	suite.app.SupplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply))
   138  
   139  	for _, addr := range valAddresses {
   140  		_, err := suite.app.BankKeeper.AddCoins(ctx, sdk.AccAddress(addr), initCoins)
   141  		suite.NoError(err)
   142  	}
   143  }
   144  
   145  func (suite *KeeperTestSuite) TestSubmitValidEvidence() {
   146  	ctx := suite.ctx.WithIsCheckTx(false)
   147  	pk := ed25519.GenPrivKey()
   148  	sv := types.TestVote{
   149  		ValidatorAddress: pk.PubKey().Address(),
   150  		Height:           11,
   151  		Round:            0,
   152  	}
   153  
   154  	sig, err := pk.Sign(sv.SignBytes(ctx.ChainID()))
   155  	suite.NoError(err)
   156  	sv.Signature = sig
   157  
   158  	e := types.TestEquivocationEvidence{
   159  		Power:      100,
   160  		TotalPower: 100000,
   161  		PubKey:     pk.PubKey(),
   162  		VoteA:      sv,
   163  		VoteB:      sv,
   164  	}
   165  
   166  	suite.Nil(suite.keeper.SubmitEvidence(ctx, e))
   167  
   168  	res, ok := suite.keeper.GetEvidence(ctx, e.Hash())
   169  	suite.True(ok)
   170  	suite.Equal(e, res)
   171  }
   172  
   173  func (suite *KeeperTestSuite) TestSubmitValidEvidence_Duplicate() {
   174  	ctx := suite.ctx.WithIsCheckTx(false)
   175  	pk := ed25519.GenPrivKey()
   176  	sv := types.TestVote{
   177  		ValidatorAddress: pk.PubKey().Address(),
   178  		Height:           11,
   179  		Round:            0,
   180  	}
   181  
   182  	sig, err := pk.Sign(sv.SignBytes(ctx.ChainID()))
   183  	suite.NoError(err)
   184  	sv.Signature = sig
   185  
   186  	e := types.TestEquivocationEvidence{
   187  		Power:      100,
   188  		TotalPower: 100000,
   189  		PubKey:     pk.PubKey(),
   190  		VoteA:      sv,
   191  		VoteB:      sv,
   192  	}
   193  
   194  	suite.Nil(suite.keeper.SubmitEvidence(ctx, e))
   195  	suite.Error(suite.keeper.SubmitEvidence(ctx, e))
   196  
   197  	res, ok := suite.keeper.GetEvidence(ctx, e.Hash())
   198  	suite.True(ok)
   199  	suite.Equal(e, res)
   200  }
   201  
   202  func (suite *KeeperTestSuite) TestSubmitInvalidEvidence() {
   203  	ctx := suite.ctx.WithIsCheckTx(false)
   204  	pk := ed25519.GenPrivKey()
   205  	e := types.TestEquivocationEvidence{
   206  		Power:      100,
   207  		TotalPower: 100000,
   208  		PubKey:     pk.PubKey(),
   209  		VoteA: types.TestVote{
   210  			ValidatorAddress: pk.PubKey().Address(),
   211  			Height:           10,
   212  			Round:            0,
   213  		},
   214  		VoteB: types.TestVote{
   215  			ValidatorAddress: pk.PubKey().Address(),
   216  			Height:           11,
   217  			Round:            0,
   218  		},
   219  	}
   220  
   221  	suite.Error(suite.keeper.SubmitEvidence(ctx, e))
   222  
   223  	res, ok := suite.keeper.GetEvidence(ctx, e.Hash())
   224  	suite.False(ok)
   225  	suite.Nil(res)
   226  }
   227  
   228  func (suite *KeeperTestSuite) TestIterateEvidence() {
   229  	ctx := suite.ctx.WithIsCheckTx(false)
   230  	numEvidence := 100
   231  	suite.populateEvidence(ctx, numEvidence)
   232  
   233  	evidence := suite.keeper.GetAllEvidence(ctx)
   234  	suite.Len(evidence, numEvidence)
   235  }
   236  
   237  func (suite *KeeperTestSuite) TestGetEvidenceHandler() {
   238  	handler, err := suite.keeper.GetEvidenceHandler(types.TestEquivocationEvidence{}.Route())
   239  	suite.NoError(err)
   240  	suite.NotNil(handler)
   241  
   242  	handler, err = suite.keeper.GetEvidenceHandler("invalidHandler")
   243  	suite.Error(err)
   244  	suite.Nil(handler)
   245  }
   246  
   247  func TestKeeperTestSuite(t *testing.T) {
   248  	suite.Run(t, new(KeeperTestSuite))
   249  }