imp(all): setup a validator in the test genesis (#1171)
It's disabled in cosmos-sdk 0.46's InitGenesis to have zero validators, prepare for the upgrade of cosmos-sdk 0.46.
This commit is contained in:
parent
3d2f72a153
commit
b7c85beba3
@ -1,7 +1,6 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -9,7 +8,6 @@ import (
|
|||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/simapp"
|
"github.com/cosmos/cosmos-sdk/simapp"
|
||||||
|
|
||||||
abci "github.com/tendermint/tendermint/abci/types"
|
|
||||||
"github.com/tendermint/tendermint/libs/log"
|
"github.com/tendermint/tendermint/libs/log"
|
||||||
dbm "github.com/tendermint/tm-db"
|
dbm "github.com/tendermint/tm-db"
|
||||||
|
|
||||||
@ -18,24 +16,11 @@ import (
|
|||||||
|
|
||||||
func TestEthermintAppExport(t *testing.T) {
|
func TestEthermintAppExport(t *testing.T) {
|
||||||
db := dbm.NewMemDB()
|
db := dbm.NewMemDB()
|
||||||
app := NewEthermintApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{})
|
app := SetupWithDB(false, nil, db)
|
||||||
|
|
||||||
genesisState := NewDefaultGenesisState()
|
|
||||||
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Initialize the chain
|
|
||||||
app.InitChain(
|
|
||||||
abci.RequestInitChain{
|
|
||||||
ChainId: "ethermint_9000-1",
|
|
||||||
Validators: []abci.ValidatorUpdate{},
|
|
||||||
AppStateBytes: stateBytes,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
app.Commit()
|
app.Commit()
|
||||||
|
|
||||||
// Making a new app object with the db, so that initchain hasn't been called
|
// Making a new app object with the db, so that initchain hasn't been called
|
||||||
app2 := NewEthermintApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{})
|
app2 := NewEthermintApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{})
|
||||||
_, err = app2.ExportAppStateAndValidators(false, []string{})
|
_, err := app2.ExportAppStateAndValidators(false, []string{})
|
||||||
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
|
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ func BenchmarkEthermintApp_ExportAppStateAndValidators(b *testing.B) {
|
|||||||
db := dbm.NewMemDB()
|
db := dbm.NewMemDB()
|
||||||
app := NewEthermintApp(log.NewTMLogger(io.Discard), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{})
|
app := NewEthermintApp(log.NewTMLogger(io.Discard), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{})
|
||||||
|
|
||||||
genesisState := NewDefaultGenesisState()
|
genesisState := NewTestGenesisState(app.AppCodec())
|
||||||
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
|
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
|
101
app/utils.go
101
app/utils.go
@ -6,10 +6,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||||
|
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
||||||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
||||||
"github.com/cosmos/cosmos-sdk/simapp"
|
"github.com/cosmos/cosmos-sdk/simapp"
|
||||||
"github.com/cosmos/cosmos-sdk/types/module"
|
"github.com/cosmos/cosmos-sdk/types/module"
|
||||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||||
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||||
|
|
||||||
"github.com/evmos/ethermint/encoding"
|
"github.com/evmos/ethermint/encoding"
|
||||||
@ -50,11 +53,15 @@ var DefaultConsensusParams = &abci.ConsensusParams{
|
|||||||
|
|
||||||
// Setup initializes a new EthermintApp. A Nop logger is set in EthermintApp.
|
// Setup initializes a new EthermintApp. A Nop logger is set in EthermintApp.
|
||||||
func Setup(isCheckTx bool, patchGenesis func(*EthermintApp, simapp.GenesisState) simapp.GenesisState) *EthermintApp {
|
func Setup(isCheckTx bool, patchGenesis func(*EthermintApp, simapp.GenesisState) simapp.GenesisState) *EthermintApp {
|
||||||
db := dbm.NewMemDB()
|
return SetupWithDB(isCheckTx, patchGenesis, dbm.NewMemDB())
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetupWithDB initializes a new EthermintApp. A Nop logger is set in EthermintApp.
|
||||||
|
func SetupWithDB(isCheckTx bool, patchGenesis func(*EthermintApp, simapp.GenesisState) simapp.GenesisState, db dbm.DB) *EthermintApp {
|
||||||
app := NewEthermintApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 5, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{})
|
app := NewEthermintApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 5, encoding.MakeConfig(ModuleBasics), simapp.EmptyAppOptions{})
|
||||||
if !isCheckTx {
|
if !isCheckTx {
|
||||||
// init chain must be called to stop deliverState from being nil
|
// init chain must be called to stop deliverState from being nil
|
||||||
genesisState := NewDefaultGenesisState()
|
genesisState := NewTestGenesisState(app.AppCodec())
|
||||||
if patchGenesis != nil {
|
if patchGenesis != nil {
|
||||||
genesisState = patchGenesis(app, genesisState)
|
genesisState = patchGenesis(app, genesisState)
|
||||||
}
|
}
|
||||||
@ -167,3 +174,93 @@ func StateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simtypes
|
|||||||
return appState, simAccs, chainID, genesisTimestamp
|
return appState, simAccs, chainID, genesisTimestamp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewTestGenesisState generate genesis state with single validator
|
||||||
|
func NewTestGenesisState(codec codec.Codec) simapp.GenesisState {
|
||||||
|
privVal := ed25519.GenPrivKey()
|
||||||
|
// create validator set with single validator
|
||||||
|
tmPk, err := cryptocodec.ToTmPubKeyInterface(privVal.PubKey())
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
validator := tmtypes.NewValidator(tmPk, 1)
|
||||||
|
valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})
|
||||||
|
|
||||||
|
// generate genesis account
|
||||||
|
senderPrivKey := secp256k1.GenPrivKey()
|
||||||
|
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
|
||||||
|
balance := banktypes.Balance{
|
||||||
|
Address: acc.GetAddress().String(),
|
||||||
|
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
|
||||||
|
}
|
||||||
|
|
||||||
|
genesisState := NewDefaultGenesisState()
|
||||||
|
return genesisStateWithValSet(codec, genesisState, valSet, []authtypes.GenesisAccount{acc}, balance)
|
||||||
|
}
|
||||||
|
|
||||||
|
func genesisStateWithValSet(codec codec.Codec, genesisState simapp.GenesisState,
|
||||||
|
valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount,
|
||||||
|
balances ...banktypes.Balance,
|
||||||
|
) simapp.GenesisState {
|
||||||
|
// set genesis accounts
|
||||||
|
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
|
||||||
|
genesisState[authtypes.ModuleName] = codec.MustMarshalJSON(authGenesis)
|
||||||
|
|
||||||
|
validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
|
||||||
|
delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))
|
||||||
|
|
||||||
|
bondAmt := sdk.DefaultPowerReduction
|
||||||
|
|
||||||
|
for _, val := range valSet.Validators {
|
||||||
|
pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
pkAny, err := codectypes.NewAnyWithValue(pk)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
validator := stakingtypes.Validator{
|
||||||
|
OperatorAddress: sdk.ValAddress(val.Address).String(),
|
||||||
|
ConsensusPubkey: pkAny,
|
||||||
|
Jailed: false,
|
||||||
|
Status: stakingtypes.Bonded,
|
||||||
|
Tokens: bondAmt,
|
||||||
|
DelegatorShares: sdk.OneDec(),
|
||||||
|
Description: stakingtypes.Description{},
|
||||||
|
UnbondingHeight: int64(0),
|
||||||
|
UnbondingTime: time.Unix(0, 0).UTC(),
|
||||||
|
Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
|
||||||
|
MinSelfDelegation: sdk.ZeroInt(),
|
||||||
|
}
|
||||||
|
validators = append(validators, validator)
|
||||||
|
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec()))
|
||||||
|
|
||||||
|
}
|
||||||
|
// set validators and delegations
|
||||||
|
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
|
||||||
|
genesisState[stakingtypes.ModuleName] = codec.MustMarshalJSON(stakingGenesis)
|
||||||
|
|
||||||
|
totalSupply := sdk.NewCoins()
|
||||||
|
for _, b := range balances {
|
||||||
|
// add genesis acc tokens to total supply
|
||||||
|
totalSupply = totalSupply.Add(b.Coins...)
|
||||||
|
}
|
||||||
|
|
||||||
|
for range delegations {
|
||||||
|
// add delegated tokens to total supply
|
||||||
|
totalSupply = totalSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))
|
||||||
|
}
|
||||||
|
|
||||||
|
// add bonded amount to bonded pool module account
|
||||||
|
balances = append(balances, banktypes.Balance{
|
||||||
|
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
|
||||||
|
Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)},
|
||||||
|
})
|
||||||
|
|
||||||
|
// update total supply
|
||||||
|
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{})
|
||||||
|
genesisState[banktypes.ModuleName] = codec.MustMarshalJSON(bankGenesis)
|
||||||
|
|
||||||
|
return genesisState
|
||||||
|
}
|
||||||
|
@ -87,7 +87,7 @@ func (suite *EvmTestSuite) DoSetupTest(t require.TestingT) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
coins := sdk.NewCoins(sdk.NewCoin(types.DefaultEVMDenom, sdk.NewInt(100000000000000)))
|
coins := sdk.NewCoins(sdk.NewCoin(types.DefaultEVMDenom, sdk.NewInt(100000000000000)))
|
||||||
genesisState := app.ModuleBasics.DefaultGenesis(suite.app.AppCodec())
|
genesisState := app.NewTestGenesisState(suite.app.AppCodec())
|
||||||
b32address := sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), priv.PubKey().Address().Bytes())
|
b32address := sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), priv.PubKey().Address().Bytes())
|
||||||
balances := []banktypes.Balance{
|
balances := []banktypes.Balance{
|
||||||
{
|
{
|
||||||
@ -99,9 +99,12 @@ func (suite *EvmTestSuite) DoSetupTest(t require.TestingT) {
|
|||||||
Coins: coins,
|
Coins: coins,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// Update total supply
|
var bankGenesis banktypes.GenesisState
|
||||||
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, sdk.NewCoins(sdk.NewCoin(types.DefaultEVMDenom, sdk.NewInt(200000000000000))), []banktypes.Metadata{})
|
suite.app.AppCodec().MustUnmarshalJSON(genesisState[banktypes.ModuleName], &bankGenesis)
|
||||||
genesisState[banktypes.ModuleName] = suite.app.AppCodec().MustMarshalJSON(bankGenesis)
|
// Update balances and total supply
|
||||||
|
bankGenesis.Balances = append(bankGenesis.Balances, balances...)
|
||||||
|
bankGenesis.Supply = bankGenesis.Supply.Add(coins...).Add(coins...)
|
||||||
|
genesisState[banktypes.ModuleName] = suite.app.AppCodec().MustMarshalJSON(&bankGenesis)
|
||||||
|
|
||||||
stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ")
|
stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -110,18 +110,19 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {
|
|||||||
if suite.mintFeeCollector {
|
if suite.mintFeeCollector {
|
||||||
// mint some coin to fee collector
|
// mint some coin to fee collector
|
||||||
coins := sdk.NewCoins(sdk.NewCoin(types.DefaultEVMDenom, sdk.NewInt(int64(params.TxGas)-1)))
|
coins := sdk.NewCoins(sdk.NewCoin(types.DefaultEVMDenom, sdk.NewInt(int64(params.TxGas)-1)))
|
||||||
genesisState := app.ModuleBasics.DefaultGenesis(suite.app.AppCodec())
|
genesisState := app.NewTestGenesisState(suite.app.AppCodec())
|
||||||
balances := []banktypes.Balance{
|
balances := []banktypes.Balance{
|
||||||
{
|
{
|
||||||
Address: suite.app.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName).String(),
|
Address: suite.app.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName).String(),
|
||||||
Coins: coins,
|
Coins: coins,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// update total supply
|
var bankGenesis banktypes.GenesisState
|
||||||
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, sdk.NewCoins(sdk.NewCoin(types.DefaultEVMDenom, sdk.NewInt((int64(params.TxGas)-1)))), []banktypes.Metadata{})
|
suite.app.AppCodec().MustUnmarshalJSON(genesisState[banktypes.ModuleName], &bankGenesis)
|
||||||
bz := suite.app.AppCodec().MustMarshalJSON(bankGenesis)
|
// Update balances and total supply
|
||||||
require.NotNil(t, bz)
|
bankGenesis.Balances = append(bankGenesis.Balances, balances...)
|
||||||
genesisState[banktypes.ModuleName] = suite.app.AppCodec().MustMarshalJSON(bankGenesis)
|
bankGenesis.Supply = bankGenesis.Supply.Add(coins...)
|
||||||
|
genesisState[banktypes.ModuleName] = suite.app.AppCodec().MustMarshalJSON(&bankGenesis)
|
||||||
|
|
||||||
// we marshal the genesisState of all module to a byte array
|
// we marshal the genesisState of all module to a byte array
|
||||||
stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ")
|
stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ")
|
||||||
|
@ -493,7 +493,7 @@ func setupChain(localMinGasPricesStr string) {
|
|||||||
baseapp.SetMinGasPrices(localMinGasPricesStr),
|
baseapp.SetMinGasPrices(localMinGasPricesStr),
|
||||||
)
|
)
|
||||||
|
|
||||||
genesisState := app.NewDefaultGenesisState()
|
genesisState := app.NewTestGenesisState(newapp.AppCodec())
|
||||||
genesisState[types.ModuleName] = newapp.AppCodec().MustMarshalJSON(types.DefaultGenesisState())
|
genesisState[types.ModuleName] = newapp.AppCodec().MustMarshalJSON(types.DefaultGenesisState())
|
||||||
|
|
||||||
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
|
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
|
||||||
|
Loading…
Reference in New Issue
Block a user