|
1 | 1 | package app |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "os" |
| 7 | + "testing" |
6 | 8 |
|
7 | 9 | "cosmossdk.io/log" |
| 10 | + sdkmath "cosmossdk.io/math" |
8 | 11 | pruningtypes "cosmossdk.io/store/pruning/types" |
9 | 12 | "github.com/CosmWasm/wasmd/x/wasm" |
| 13 | + abci "github.com/cometbft/cometbft/abci/types" |
| 14 | + cmttypes "github.com/cometbft/cometbft/types" |
10 | 15 | dbm "github.com/cosmos/cosmos-db" |
11 | 16 | bam "github.com/cosmos/cosmos-sdk/baseapp" |
12 | 17 | "github.com/cosmos/cosmos-sdk/client/flags" |
| 18 | + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" |
13 | 19 | servertypes "github.com/cosmos/cosmos-sdk/server/types" |
| 20 | + "github.com/cosmos/cosmos-sdk/testutil/mock" |
14 | 21 | "github.com/cosmos/cosmos-sdk/testutil/network" |
15 | 22 | simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" |
| 23 | + sdk "github.com/cosmos/cosmos-sdk/types" |
16 | 24 | "github.com/cosmos/cosmos-sdk/types/module/testutil" |
| 25 | + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" |
| 26 | + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" |
| 27 | + "github.com/persistenceOne/persistenceCore/v16/app/constants" |
| 28 | + "github.com/stretchr/testify/require" |
17 | 29 | ) |
18 | 30 |
|
19 | 31 | // NewTestNetworkFixture returns a new persistenceCore AppConstructor for network simulation tests. |
@@ -47,3 +59,116 @@ func NewTestNetworkFixture() network.TestFixture { |
47 | 59 | }, |
48 | 60 | } |
49 | 61 | } |
| 62 | + |
| 63 | +func setup(withGenesis bool, t *testing.T) (*Application, GenesisState) { |
| 64 | + db := dbm.NewMemDB() |
| 65 | + |
| 66 | + appOptions := make(simtestutil.AppOptionsMap, 0) |
| 67 | + appOptions[flags.FlagHome] = t.TempDir() |
| 68 | + |
| 69 | + app := NewApplication(log.NewNopLogger(), db, nil, true, appOptions, []wasm.Option{}) |
| 70 | + if withGenesis { |
| 71 | + return app, app.DefaultGenesis() |
| 72 | + } |
| 73 | + return app, GenesisState{} |
| 74 | +} |
| 75 | + |
| 76 | +//// NewTestAppWithCustomOptions initializes a new TestApp with custom options. |
| 77 | +//func NewTestAppWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptions) *Application { |
| 78 | +// t.Helper() |
| 79 | +// |
| 80 | +// privVal := mock.NewPV() |
| 81 | +// pubKey, err := privVal.GetPubKey() |
| 82 | +// require.NoError(t, err) |
| 83 | +// // create validator set with single validator |
| 84 | +// validator := cmttypes.NewValidator(pubKey, 1) |
| 85 | +// valSet := cmttypes.NewValidatorSet([]*cmttypes.Validator{validator}) |
| 86 | +// |
| 87 | +// // generate genesis account |
| 88 | +// senderPrivKey := secp256k1.GenPrivKey() |
| 89 | +// acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0) |
| 90 | +// balance := banktypes.Balance{ |
| 91 | +// Address: acc.GetAddress().String(), |
| 92 | +// Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100000000000000))), |
| 93 | +// } |
| 94 | +// |
| 95 | +// app := NewApplication(options.Logger, options.DB, nil, true, options.AppOpts) |
| 96 | +// genesisState := app.DefaultGenesis() |
| 97 | +// genesisState, err = simtestutil.GenesisStateWithValSet(app.AppCodec(), genesisState, valSet, []authtypes.GenesisAccount{acc}, balance) |
| 98 | +// require.NoError(t, err) |
| 99 | +// |
| 100 | +// if !isCheckTx { |
| 101 | +// // init chain must be called to stop deliverState from being nil |
| 102 | +// stateBytes, err := cmtjson.MarshalIndent(genesisState, "", " ") |
| 103 | +// require.NoError(t, err) |
| 104 | +// |
| 105 | +// // Initialize the chain |
| 106 | +// _, err = app.InitChain(&abci.RequestInitChain{ |
| 107 | +// Validators: []abci.ValidatorUpdate{}, |
| 108 | +// ConsensusParams: simtestutil.DefaultConsensusParams, |
| 109 | +// AppStateBytes: stateBytes, |
| 110 | +// }) |
| 111 | +// require.NoError(t, err) |
| 112 | +// } |
| 113 | +// |
| 114 | +// return app |
| 115 | +//} |
| 116 | + |
| 117 | +// Setup initializes a new TestApp. A Nop logger is set in TestApp. |
| 118 | +func Setup(t *testing.T) *Application { |
| 119 | + t.Helper() |
| 120 | + |
| 121 | + privVal := mock.NewPV() |
| 122 | + pubKey, err := privVal.GetPubKey() |
| 123 | + require.NoError(t, err) |
| 124 | + |
| 125 | + // create validator set with single validator |
| 126 | + validator := cmttypes.NewValidator(pubKey, 1) |
| 127 | + valSet := cmttypes.NewValidatorSet([]*cmttypes.Validator{validator}) |
| 128 | + |
| 129 | + // generate genesis account |
| 130 | + senderPrivKey := secp256k1.GenPrivKey() |
| 131 | + acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0) |
| 132 | + balance := banktypes.Balance{ |
| 133 | + Address: acc.GetAddress().String(), |
| 134 | + Coins: sdk.NewCoins(sdk.NewCoin(constants.BondDenom, sdkmath.NewInt(100000000000000))), |
| 135 | + } |
| 136 | + |
| 137 | + app := SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, balance) |
| 138 | + |
| 139 | + return app |
| 140 | +} |
| 141 | + |
| 142 | +// SetupWithGenesisValSet initializes a new TestApp with a validator set and genesis accounts |
| 143 | +// that also act as delegators. For simplicity, each validator is bonded with a delegation |
| 144 | +// of one consensus engine unit in the default token of the TestApp from first genesis |
| 145 | +// account. A Nop logger is set in TestApp. |
| 146 | +func SetupWithGenesisValSet(t *testing.T, valSet *cmttypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *Application { |
| 147 | + t.Helper() |
| 148 | + |
| 149 | + app, genesisState := setup(true, t) |
| 150 | + genesisState, err := simtestutil.GenesisStateWithValSet(app.AppCodec(), genesisState, valSet, genAccs, balances...) |
| 151 | + require.NoError(t, err) |
| 152 | + |
| 153 | + stateBytes, err := json.MarshalIndent(genesisState, "", " ") |
| 154 | + require.NoError(t, err) |
| 155 | + |
| 156 | + // init chain will set the validator set and initialize the genesis accounts |
| 157 | + _, err = app.InitChain(&abci.RequestInitChain{ |
| 158 | + Validators: []abci.ValidatorUpdate{}, |
| 159 | + ConsensusParams: simtestutil.DefaultConsensusParams, |
| 160 | + AppStateBytes: stateBytes, |
| 161 | + }, |
| 162 | + ) |
| 163 | + require.NoError(t, err) |
| 164 | + |
| 165 | + require.NoError(t, err) |
| 166 | + _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{ |
| 167 | + Height: app.LastBlockHeight() + 1, |
| 168 | + Hash: app.LastCommitID().Hash, |
| 169 | + NextValidatorsHash: valSet.Hash(), |
| 170 | + }) |
| 171 | + require.NoError(t, err) |
| 172 | + |
| 173 | + return app |
| 174 | +} |
0 commit comments