2022-04-19 09:50:00 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2022-05-02 13:27:43 +00:00
|
|
|
"encoding/json"
|
2022-04-19 09:50:00 +00:00
|
|
|
"math/rand"
|
2022-05-02 13:27:43 +00:00
|
|
|
"os"
|
2022-04-19 09:50:00 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
2022-05-02 13:27:43 +00:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2022-06-19 09:43:41 +00:00
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
2022-04-19 09:50:00 +00:00
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
"github.com/evmos/ethermint/crypto/ethsecp256k1"
|
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
2022-04-19 09:50:00 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
var (
|
|
|
|
maxTestingAccounts = 100
|
|
|
|
seed = int64(233)
|
|
|
|
)
|
2022-04-19 09:50:00 +00:00
|
|
|
|
|
|
|
func TestRandomGenesisAccounts(t *testing.T) {
|
|
|
|
r := rand.New(rand.NewSource(seed))
|
|
|
|
accs := RandomAccounts(r, rand.Intn(maxTestingAccounts))
|
|
|
|
|
|
|
|
encodingConfig := MakeEncodingConfig()
|
2022-07-28 13:43:49 +00:00
|
|
|
appCodec := encodingConfig.Codec
|
2022-04-19 09:50:00 +00:00
|
|
|
cdc := encodingConfig.Amino
|
|
|
|
|
|
|
|
paramsKeeper := initParamsKeeper(appCodec, cdc, sdk.NewKVStoreKey(paramstypes.StoreKey), sdk.NewTransientStoreKey(paramstypes.StoreKey))
|
|
|
|
subSpace, find := paramsKeeper.GetSubspace(authtypes.ModuleName)
|
|
|
|
require.True(t, find)
|
|
|
|
accountKeeper := authkeeper.NewAccountKeeper(
|
2022-07-28 13:43:49 +00:00
|
|
|
appCodec, sdk.NewKVStoreKey(authtypes.StoreKey), subSpace, ethermint.ProtoAccount, maccPerms, sdk.GetConfig().GetBech32AccountAddrPrefix(),
|
2022-04-19 09:50:00 +00:00
|
|
|
)
|
|
|
|
authModule := auth.NewAppModule(appCodec, accountKeeper, RandomGenesisAccounts)
|
|
|
|
|
|
|
|
genesisState := simapp.NewDefaultGenesisState(appCodec)
|
|
|
|
simState := &module.SimulationState{Accounts: accs, Cdc: appCodec, Rand: r, GenState: genesisState}
|
|
|
|
authModule.GenerateGenesisState(simState)
|
|
|
|
|
|
|
|
authStateBz, find := genesisState[authtypes.ModuleName]
|
|
|
|
require.True(t, find)
|
|
|
|
|
|
|
|
authState := new(authtypes.GenesisState)
|
|
|
|
appCodec.MustUnmarshalJSON(authStateBz, authState)
|
|
|
|
accounts, err := authtypes.UnpackAccounts(authState.Accounts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, acc := range accounts {
|
|
|
|
_, ok := acc.(ethermint.EthAccountI)
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
|
|
|
}
|
2022-05-02 13:27:43 +00:00
|
|
|
|
|
|
|
func TestStateFn(t *testing.T) {
|
|
|
|
config, db, dir, logger, skip, err := simapp.SetupSimulation("leveldb-app-sim", "Simulation")
|
|
|
|
if skip {
|
|
|
|
t.Skip("skipping AppStateFn testing")
|
|
|
|
}
|
|
|
|
require.NoError(t, err, "simulation setup failed")
|
|
|
|
|
|
|
|
config.ChainID = SimAppChainID
|
|
|
|
config.Commit = true
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
require.NoError(t, os.RemoveAll(dir))
|
|
|
|
}()
|
|
|
|
|
|
|
|
app := NewEthermintApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), simapp.EmptyAppOptions{}, fauxMerkleModeOpt)
|
|
|
|
require.Equal(t, appName, app.Name())
|
|
|
|
|
|
|
|
appStateFn := StateFn(app.AppCodec(), app.SimulationManager())
|
|
|
|
r := rand.New(rand.NewSource(seed))
|
|
|
|
accounts := RandomAccounts(r, rand.Intn(maxTestingAccounts))
|
|
|
|
appState, _, _, _ := appStateFn(r, accounts, config)
|
|
|
|
|
|
|
|
rawState := make(map[string]json.RawMessage)
|
|
|
|
err = json.Unmarshal(appState, &rawState)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
stakingStateBz, ok := rawState[stakingtypes.ModuleName]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
stakingState := new(stakingtypes.GenesisState)
|
|
|
|
app.AppCodec().MustUnmarshalJSON(stakingStateBz, stakingState)
|
|
|
|
bondDenom := stakingState.Params.BondDenom
|
|
|
|
|
|
|
|
evmStateBz, ok := rawState[evmtypes.ModuleName]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
evmState := new(evmtypes.GenesisState)
|
|
|
|
app.AppCodec().MustUnmarshalJSON(evmStateBz, evmState)
|
|
|
|
require.Equal(t, bondDenom, evmState.Params.EvmDenom)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRandomAccounts(t *testing.T) {
|
|
|
|
r := rand.New(rand.NewSource(seed))
|
|
|
|
accounts := RandomAccounts(r, rand.Intn(maxTestingAccounts))
|
|
|
|
for _, acc := range accounts {
|
|
|
|
_, ok := acc.PrivKey.(*ethsecp256k1.PrivKey)
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
|
|
|
}
|