2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2022-04-19 09:50:00 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2022-07-11 12:42:59 +00:00
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
|
|
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
2022-04-19 09:50:00 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2022-07-28 13:43:49 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/mock"
|
2022-04-19 09:50:00 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2022-07-11 12:42:59 +00:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2022-04-19 09:50:00 +00:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-11-29 11:41:40 +00:00
|
|
|
"github.com/evmos/ethermint/encoding"
|
2022-04-19 09:50:00 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
dbm "github.com/tendermint/tm-db"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultConsensusParams defines the default Tendermint consensus params used in
|
|
|
|
// EthermintApp testing.
|
|
|
|
var DefaultConsensusParams = &abci.ConsensusParams{
|
|
|
|
Block: &abci.BlockParams{
|
|
|
|
MaxBytes: 200000,
|
|
|
|
MaxGas: -1, // no limit
|
|
|
|
},
|
|
|
|
Evidence: &tmproto.EvidenceParams{
|
|
|
|
MaxAgeNumBlocks: 302400,
|
|
|
|
MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration
|
|
|
|
MaxBytes: 10000,
|
|
|
|
},
|
|
|
|
Validator: &tmproto.ValidatorParams{
|
|
|
|
PubKeyTypes: []string{
|
|
|
|
tmtypes.ABCIPubKeyTypeEd25519,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup initializes a new EthermintApp. A Nop logger is set in EthermintApp.
|
|
|
|
func Setup(isCheckTx bool, patchGenesis func(*EthermintApp, simapp.GenesisState) simapp.GenesisState) *EthermintApp {
|
2022-07-11 12:42:59 +00:00
|
|
|
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 {
|
2022-08-26 10:30:55 +00:00
|
|
|
app := NewEthermintApp(log.NewNopLogger(),
|
|
|
|
db,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
map[int64]bool{},
|
|
|
|
DefaultNodeHome,
|
|
|
|
5,
|
|
|
|
encoding.MakeConfig(ModuleBasics),
|
|
|
|
simapp.EmptyAppOptions{})
|
2022-04-19 09:50:00 +00:00
|
|
|
if !isCheckTx {
|
|
|
|
// init chain must be called to stop deliverState from being nil
|
2022-07-11 12:42:59 +00:00
|
|
|
genesisState := NewTestGenesisState(app.AppCodec())
|
2022-04-19 09:50:00 +00:00
|
|
|
if patchGenesis != nil {
|
|
|
|
genesisState = patchGenesis(app, genesisState)
|
|
|
|
}
|
|
|
|
|
|
|
|
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the chain
|
|
|
|
app.InitChain(
|
|
|
|
abci.RequestInitChain{
|
|
|
|
ChainId: "ethermint_9000-1",
|
|
|
|
Validators: []abci.ValidatorUpdate{},
|
|
|
|
ConsensusParams: DefaultConsensusParams,
|
|
|
|
AppStateBytes: stateBytes,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:42:59 +00:00
|
|
|
// NewTestGenesisState generate genesis state with single validator
|
|
|
|
func NewTestGenesisState(codec codec.Codec) simapp.GenesisState {
|
2022-07-28 13:43:49 +00:00
|
|
|
privVal := mock.NewPV()
|
|
|
|
pubKey, err := privVal.GetPubKey()
|
2022-07-11 12:42:59 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-28 13:43:49 +00:00
|
|
|
// create validator set with single validator
|
|
|
|
validator := tmtypes.NewValidator(pubKey, 1)
|
2022-07-11 12:42:59 +00:00
|
|
|
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
|
|
|
|
}
|