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

     1  package staking
     2  
     3  import (
     4  	"bufio"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/client/utils"
    11  
    12  	"github.com/fibonacci-chain/fbc/libs/tendermint/libs/log"
    13  	"github.com/fibonacci-chain/fbc/x/staking/keeper"
    14  	"github.com/fibonacci-chain/fbc/x/staking/types"
    15  
    16  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    17  	"github.com/stretchr/testify/require"
    18  
    19  	cliLcd "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/lcd"
    20  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    21  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/mock"
    22  )
    23  
    24  // getMockApp returns an initialized mock application for this module.
    25  func getMockApp(t *testing.T) (*mock.App, keeper.MockStakingKeeper) {
    26  	mApp := mock.NewApp()
    27  
    28  	//RegisterCodec(mApp.Cdc)
    29  
    30  	_, accKeeper, mKeeper := CreateTestInput(t, false, SufficientInitPower)
    31  	keeper := mKeeper.Keeper
    32  
    33  	mApp.Router().AddRoute(RouterKey, NewHandler(keeper))
    34  	mApp.SetEndBlocker(getEndBlocker(keeper))
    35  	mApp.SetInitChainer(getInitChainer(mApp, keeper, accKeeper, mKeeper.SupplyKeeper))
    36  
    37  	require.NoError(t, mApp.CompleteSetup(mKeeper.StoreKey, mKeeper.TkeyStoreKey))
    38  	return mApp, mKeeper
    39  }
    40  
    41  // getEndBlocker returns a staking endblocker.
    42  func getEndBlocker(keeper Keeper) sdk.EndBlocker {
    43  	return func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
    44  		validatorUpdates := EndBlocker(ctx, keeper)
    45  
    46  		return abci.ResponseEndBlock{
    47  			ValidatorUpdates: validatorUpdates,
    48  		}
    49  	}
    50  }
    51  
    52  // getInitChainer initializes the chainer of the mock app and sets the genesis
    53  // state. It returns an empty ResponseInitChain.
    54  func getInitChainer(mapp *mock.App, keeper Keeper, accKeeper types.AccountKeeper,
    55  	supKeeper types.SupplyKeeper) sdk.InitChainer {
    56  	return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
    57  		mapp.InitChainer(ctx, req)
    58  
    59  		stakingGenesis := DefaultGenesisState()
    60  		validators := InitGenesis(ctx, keeper, accKeeper, supKeeper, stakingGenesis)
    61  
    62  		return abci.ResponseInitChain{
    63  			Validators: validators,
    64  		}
    65  	}
    66  }
    67  
    68  type MockInvariantRegistry struct{}
    69  
    70  func (ir MockInvariantRegistry) RegisterRoute(moduleName, route string, invar sdk.Invariant) {}
    71  
    72  //__________________________________________________________________________________________
    73  
    74  func TestAppSmoke(t *testing.T) {
    75  	mApp, mKeeper := getMockApp(t)
    76  	appModule := NewAppModule(mKeeper.Keeper, mKeeper.AccKeeper, mKeeper.SupplyKeeper)
    77  
    78  	// Const Info
    79  	require.True(t, appModule.Name() == ModuleName)
    80  	require.True(t, appModule.Route() == RouterKey)
    81  	require.True(t, appModule.QuerierRoute() == QuerierRoute)
    82  	require.True(t, appModule.GetQueryCmd(mApp.Cdc.GetCdc()) != nil)
    83  	require.True(t, appModule.GetTxCmd(mApp.Cdc.GetCdc()) != nil)
    84  
    85  	appModule.RegisterCodec(mApp.Cdc.GetCdc())
    86  	appModule.RegisterInvariants(MockInvariantRegistry{})
    87  	rs := cliLcd.NewRestServer(mApp.Cdc, nil, nil)
    88  	appModule.RegisterRESTRoutes(rs.CliCtx, rs.Mux)
    89  	handler := appModule.NewHandler()
    90  	require.True(t, handler != nil)
    91  	querior := appModule.NewQuerierHandler()
    92  	require.True(t, querior != nil)
    93  
    94  	// Extra Helper
    95  	appModule.CreateValidatorMsgHelpers("0.0.0.0")
    96  	cliCtx := context.NewCLIContext().WithCodec(mApp.Cdc.GetCdc())
    97  	inBuf := bufio.NewReader(os.Stdin)
    98  	txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(mApp.Cdc.GetCdc()))
    99  	appModule.BuildCreateValidatorMsg(cliCtx, txBldr)
   100  
   101  	// Initialization for genesis
   102  	defaultGen := appModule.DefaultGenesis()
   103  	err := appModule.ValidateGenesis(defaultGen)
   104  	require.True(t, err == nil)
   105  
   106  	illegalData := []byte{}
   107  	err = appModule.ValidateGenesis(illegalData)
   108  	require.Error(t, err)
   109  
   110  	// Basic abci test
   111  	header := abci.Header{ChainID: keeper.TestChainID, Height: 0}
   112  	ctx := sdk.NewContext(mKeeper.MountedStore, header, false, log.NewNopLogger())
   113  	validatorUpdates := appModule.InitGenesis(ctx, defaultGen)
   114  	require.True(t, len(validatorUpdates) == 0)
   115  	exportedGenesis := appModule.ExportGenesis(ctx)
   116  	require.True(t, exportedGenesis != nil)
   117  
   118  	// Begin & End Block
   119  	appModule.BeginBlock(ctx, abci.RequestBeginBlock{})
   120  	appModule.EndBlock(ctx, abci.RequestEndBlock{})
   121  
   122  }