-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
aljo242
committed
Jul 27, 2022
1 parent
78f1b73
commit e9c0c18
Showing
2 changed files
with
125 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package testutil | ||
|
||
import ( | ||
"time" | ||
|
||
"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/simapp" | ||
"github.com/cosmos/cosmos-sdk/testutil/mock" | ||
sdk "github.com/cosmos/cosmos-sdk/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" | ||
spnapp "github.com/tendermint/spn/app" | ||
"github.com/tendermint/spn/cmd" | ||
"github.com/tendermint/tendermint/libs/log" | ||
tmtypes "github.com/tendermint/tendermint/types" | ||
dbm "github.com/tendermint/tm-db" | ||
) | ||
|
||
func GenApp(withGenesis bool, invCheckPeriod uint) (*spnapp.App, spnapp.GenesisState) { | ||
db := dbm.NewMemDB() | ||
encCdc := cmd.MakeEncodingConfig(spnapp.ModuleBasics) | ||
app := spnapp.New( | ||
log.NewNopLogger(), | ||
db, | ||
nil, | ||
true, | ||
map[int64]bool{}, | ||
simapp.DefaultNodeHome, | ||
invCheckPeriod, | ||
encCdc, | ||
simapp.EmptyAppOptions{}) | ||
|
||
originalApp := app.(*spnapp.App) | ||
if withGenesis { | ||
genesisState := spnapp.NewDefaultGenesisState(encCdc.Marshaler) | ||
privVal := mock.NewPV() | ||
pubKey, _ := privVal.GetPubKey() | ||
// create validator set with single validator | ||
validator := tmtypes.NewValidator(pubKey, 1) | ||
valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator}) | ||
|
||
// generate genesis account | ||
senderPrivKey := secp256k1.GenPrivKey() | ||
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0) | ||
balances := banktypes.Balance{ | ||
Address: acc.GetAddress().String(), | ||
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))), | ||
} | ||
genesisState = genesisStateWithValSet(encCdc.Marshaler, genesisState, valSet, []authtypes.GenesisAccount{acc}, balances) | ||
return originalApp, genesisState | ||
} | ||
|
||
return originalApp, spnapp.GenesisState{} | ||
} | ||
|
||
func genesisStateWithValSet( | ||
cdc codec.Codec, | ||
genesisState spnapp.GenesisState, | ||
valSet *tmtypes.ValidatorSet, | ||
genAccs []authtypes.GenesisAccount, | ||
balances ...banktypes.Balance, | ||
) spnapp.GenesisState { // set genesis accounts | ||
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) | ||
genesisState[authtypes.ModuleName] = cdc.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, _ := cryptocodec.FromTmPubKeyInterface(val.PubKey) | ||
pkAny, _ := codectypes.NewAnyWithValue(pk) | ||
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] = cdc.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] = cdc.MustMarshalJSON(bankGenesis) | ||
|
||
return genesisState | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters