github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/testing/app.go (about)

     1  package ibctesting
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  
     8  	ibc "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core"
     9  
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client"
    11  	authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/bank"
    13  
    14  	//cryptocodec "github.com/fibonacci-chain/fbc/app/crypto/ethsecp256k1"
    15  
    16  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    17  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    18  	authexported "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/exported"
    19  
    20  	//authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    21  
    22  	capabilitykeeper "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/capability/keeper"
    23  	stakingtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/staking/types"
    24  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    25  	"github.com/fibonacci-chain/fbc/libs/tendermint/libs/log"
    26  	tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types"
    27  	dbm "github.com/fibonacci-chain/fbc/libs/tm-db"
    28  	"github.com/fibonacci-chain/fbc/x/evm"
    29  	evmtypes "github.com/fibonacci-chain/fbc/x/evm/types"
    30  	stakingkeeper "github.com/fibonacci-chain/fbc/x/staking"
    31  	"github.com/stretchr/testify/require"
    32  
    33  	bam "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/baseapp"
    34  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/keeper"
    35  	"github.com/fibonacci-chain/fbc/libs/ibc-go/testing/simapp"
    36  )
    37  
    38  var DefaultTestingAppInit func() (TestingApp, map[string]json.RawMessage) = SetupTestingApp
    39  
    40  // IBC application testing ports
    41  
    42  type TestingApp interface {
    43  	abci.Application
    44  	TxConfig() client.TxConfig
    45  
    46  	// ibc-go additions
    47  	GetBaseApp() *bam.BaseApp
    48  	GetStakingKeeper() stakingkeeper.Keeper
    49  	GetIBCKeeper() *keeper.Keeper
    50  	GetFacadedKeeper() *ibc.Keeper
    51  	GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper
    52  	//GetTxConfig() client.TxConfig
    53  
    54  	// Implemented by SimApp
    55  	AppCodec() *codec.CodecProxy
    56  
    57  	// Implemented by BaseApp
    58  	LastCommitID() sdk.CommitID
    59  	LastBlockHeight() int64
    60  }
    61  
    62  func SetupTestingApp() (TestingApp, map[string]json.RawMessage) {
    63  	db := dbm.NewMemDB()
    64  	//encCdc := simapp.MakeTestEncodingConfig()
    65  	app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, 5)
    66  	return app, simapp.NewDefaultGenesisState()
    67  }
    68  
    69  // SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts
    70  // that also act as delegators. For simplicity, each validator is bonded with a delegation
    71  // of one consensus engine unit (10^6) in the default token of the simapp from first genesis
    72  // account. A Nop logger is set in SimApp.
    73  func SetupWithGenesisValSet(t *testing.T, chainId string, valSet *tmtypes.ValidatorSet, genAccs []authexported.GenesisAccount, balances ...sdk.Coins) TestingApp {
    74  	app, genesisState := DefaultTestingAppInit()
    75  	// set genesis accounts
    76  	authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
    77  
    78  	genesisState[authtypes.ModuleName] = app.AppCodec().GetCdc().MustMarshalJSON(authGenesis)
    79  	var err error
    80  	if err != nil {
    81  		panic("SetupWithGenesisValSet marshal error")
    82  	}
    83  	//var genesisState2 authtypes.GenesisState
    84  
    85  	validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
    86  	delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))
    87  
    88  	bondAmt := sdk.NewInt(1000000)
    89  
    90  	for _, val := range valSet.Validators {
    91  		//pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey)
    92  		//require.NoError(t, err)
    93  		//pkAny, err := codectypes.NewAnyWithValue(pk)
    94  		//require.NoError(t, err)
    95  		validator := stakingtypes.Validator{
    96  			OperatorAddress:         sdk.ValAddress(val.Address),
    97  			ConsPubKey:              val.PubKey,
    98  			Jailed:                  false,
    99  			Status:                  sdk.Bonded,
   100  			Tokens:                  bondAmt,
   101  			DelegatorShares:         sdk.OneDec(),
   102  			Description:             stakingtypes.Description{},
   103  			UnbondingHeight:         int64(0),
   104  			UnbondingCompletionTime: time.Unix(0, 0).UTC(),
   105  			//UnbondingTime:     time.Unix(0, 0).UTC(),
   106  			Commission:        stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
   107  			MinSelfDelegation: sdk.ZeroInt(),
   108  		}
   109  		validators = append(validators, validator)
   110  		delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec()))
   111  
   112  	}
   113  	// set validators and delegations
   114  	stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
   115  	genesisState[stakingtypes.ModuleName] = app.AppCodec().GetCdc().MustMarshalJSON(stakingGenesis)
   116  
   117  	totalSupply := sdk.NewCoins()
   118  	for _, b := range balances {
   119  		//add genesis acc tokens and delegated tokens to total supply
   120  		totalSupply = totalSupply.Add(b.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))...)
   121  	}
   122  
   123  	balances = append(balances, sdk.Coins{
   124  		sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000000),
   125  	})
   126  
   127  	bankGenesis := bank.DefaultGenesisState()
   128  	genesisState[bank.ModuleName] = app.AppCodec().GetCdc().MustMarshalJSON(bankGenesis)
   129  
   130  	evmGenesis := evmtypes.DefaultGenesisState()
   131  	evmGenesis.Params.EnableCall = true
   132  	evmGenesis.Params.EnableCreate = true
   133  	genesisState[evm.ModuleName] = app.AppCodec().GetCdc().MustMarshalJSON(evmGenesis)
   134  
   135  	stateBytes, err := json.MarshalIndent(genesisState, "", " ")
   136  	require.NoError(t, err)
   137  	// init chain will set the validator set and initialize the genesis accounts
   138  	app.InitChain(
   139  		abci.RequestInitChain{
   140  			Validators:      []abci.ValidatorUpdate{},
   141  			ConsensusParams: simapp.DefaultConsensusParams,
   142  			AppStateBytes:   stateBytes,
   143  			ChainId:         chainId,
   144  		},
   145  	)
   146  
   147  	// commit genesis changes
   148  	app.Commit(abci.RequestCommit{})
   149  	// app.Commit()
   150  	app.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{
   151  		Height:             app.LastBlockHeight() + 1,
   152  		AppHash:            app.LastCommitID().Hash,
   153  		ValidatorsHash:     valSet.Hash(app.LastBlockHeight() + 1),
   154  		NextValidatorsHash: valSet.Hash(app.LastBlockHeight() + 1),
   155  	}}) //app.Commit(abci.RequestCommit{})
   156  
   157  	return app
   158  }