|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "time" |
| 6 | + |
| 7 | + cometbftdb "github.com/cometbft/cometbft-db" |
| 8 | + abci "github.com/cometbft/cometbft/abci/types" |
| 9 | + "github.com/cometbft/cometbft/crypto/secp256k1" |
| 10 | + "github.com/cometbft/cometbft/libs/log" |
| 11 | + tmtypes "github.com/cometbft/cometbft/types" |
| 12 | + codectypes "github.com/cosmos/cosmos-sdk/codec/types" |
| 13 | + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" |
| 14 | + "github.com/cosmos/cosmos-sdk/testutil/mock" |
| 15 | + "github.com/cosmos/cosmos-sdk/testutil/sims" |
| 16 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 17 | + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" |
| 18 | + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" |
| 19 | + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" |
| 20 | +) |
| 21 | + |
| 22 | +func GenesisStateWithValSet(app *App) GenesisState { |
| 23 | + privVal := mock.NewPV() |
| 24 | + pubKey, _ := privVal.GetPubKey() |
| 25 | + validator := tmtypes.NewValidator(pubKey, 1) |
| 26 | + valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator}) |
| 27 | + |
| 28 | + // generate genesis account |
| 29 | + senderPrivKey := secp256k1.GenPrivKey() |
| 30 | + senderPrivKey.PubKey().Address() |
| 31 | + acc := authtypes.NewBaseAccountWithAddress(senderPrivKey.PubKey().Address().Bytes()) |
| 32 | + |
| 33 | + ////////////////////// |
| 34 | + balances := []banktypes.Balance{} |
| 35 | + genesisState := NewDefaultGenesisState(app.AppCodec) |
| 36 | + genAccs := []authtypes.GenesisAccount{acc} |
| 37 | + authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) |
| 38 | + genesisState[authtypes.ModuleName] = app.AppCodec.MustMarshalJSON(authGenesis) |
| 39 | + |
| 40 | + validators := make([]stakingtypes.Validator, 0, len(valSet.Validators)) |
| 41 | + delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators)) |
| 42 | + |
| 43 | + bondAmt := sdk.DefaultPowerReduction |
| 44 | + initValPowers := []abci.ValidatorUpdate{} |
| 45 | + |
| 46 | + for _, val := range valSet.Validators { |
| 47 | + pk, _ := cryptocodec.FromTmPubKeyInterface(val.PubKey) |
| 48 | + pkAny, _ := codectypes.NewAnyWithValue(pk) |
| 49 | + validator := stakingtypes.Validator{ |
| 50 | + OperatorAddress: sdk.ValAddress(val.Address).String(), |
| 51 | + ConsensusPubkey: pkAny, |
| 52 | + Jailed: false, |
| 53 | + Status: stakingtypes.Bonded, |
| 54 | + Tokens: bondAmt, |
| 55 | + DelegatorShares: sdk.OneDec(), |
| 56 | + Description: stakingtypes.Description{}, |
| 57 | + UnbondingHeight: int64(0), |
| 58 | + UnbondingTime: time.Unix(0, 0).UTC(), |
| 59 | + Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), |
| 60 | + MinSelfDelegation: sdk.NewInt(0), |
| 61 | + } |
| 62 | + validators = append(validators, validator) |
| 63 | + delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) |
| 64 | + |
| 65 | + // add initial validator powers so consumer InitGenesis runs correctly |
| 66 | + pub, _ := val.ToProto() |
| 67 | + initValPowers = append(initValPowers, abci.ValidatorUpdate{ |
| 68 | + Power: val.VotingPower, |
| 69 | + PubKey: pub.PubKey, |
| 70 | + }) |
| 71 | + } |
| 72 | + // set validators and delegations |
| 73 | + stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) |
| 74 | + genesisState[stakingtypes.ModuleName] = app.AppCodec.MustMarshalJSON(stakingGenesis) |
| 75 | + |
| 76 | + totalSupply := sdk.NewCoins() |
| 77 | + for _, b := range balances { |
| 78 | + // add genesis acc tokens to total supply |
| 79 | + totalSupply = totalSupply.Add(b.Coins...) |
| 80 | + } |
| 81 | + |
| 82 | + for range delegations { |
| 83 | + // add delegated tokens to total supply |
| 84 | + totalSupply = totalSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)) |
| 85 | + } |
| 86 | + |
| 87 | + // add bonded amount to bonded pool module account |
| 88 | + balances = append(balances, banktypes.Balance{ |
| 89 | + Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(), |
| 90 | + Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)}, |
| 91 | + }) |
| 92 | + |
| 93 | + // update total supply |
| 94 | + bankGenesis := banktypes.NewGenesisState( |
| 95 | + banktypes.DefaultGenesisState().Params, |
| 96 | + balances, |
| 97 | + totalSupply, |
| 98 | + []banktypes.Metadata{}, |
| 99 | + []banktypes.SendEnabled{}, |
| 100 | + ) |
| 101 | + genesisState[banktypes.ModuleName] = app.AppCodec.MustMarshalJSON(bankGenesis) |
| 102 | + |
| 103 | + _, err := tmtypes.PB2TM.ValidatorUpdates(initValPowers) |
| 104 | + if err != nil { |
| 105 | + panic("failed to get vals") |
| 106 | + } |
| 107 | + |
| 108 | + return genesisState |
| 109 | +} |
| 110 | + |
| 111 | +func GetTestApp() *App { |
| 112 | + db := cometbftdb.NewMemDB() |
| 113 | + testApp := New( |
| 114 | + log.NewNopLogger(), |
| 115 | + db, |
| 116 | + nil, |
| 117 | + true, |
| 118 | + map[int64]bool{}, |
| 119 | + sims.EmptyAppOptions{}, |
| 120 | + ) |
| 121 | + |
| 122 | + genesisState := GenesisStateWithValSet(&testApp) |
| 123 | + stateBytes, err := json.MarshalIndent(genesisState, "", " ") |
| 124 | + if err != nil { |
| 125 | + panic(err) |
| 126 | + } |
| 127 | + |
| 128 | + testApp.InitChain( |
| 129 | + abci.RequestInitChain{ |
| 130 | + ChainId: "", |
| 131 | + Validators: []abci.ValidatorUpdate{}, |
| 132 | + ConsensusParams: sims.DefaultConsensusParams, |
| 133 | + AppStateBytes: stateBytes, |
| 134 | + }, |
| 135 | + ) |
| 136 | + |
| 137 | + return &testApp |
| 138 | +} |
0 commit comments