Skip to content
This repository was archived by the owner on Aug 1, 2024. It is now read-only.
/ cosmos-sdk Public archive
forked from cosmos/cosmos-sdk

Commit 2a7f4e4

Browse files
julienrbrttsenart
authored andcommitted
refactor!: extract AppStateFn out of simapp (cosmos#14977)
1 parent 624ebf2 commit 2a7f4e4

File tree

7 files changed

+50
-46
lines changed

7 files changed

+50
-46
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
174174

175175
### API Breaking Changes
176176

177+
* (simapp) [#14977](https://github.com/cosmos/cosmos-sdk/pull/14977) Move simulation helpers functions (`AppStateFn` and `AppStateRandomizedFn`) to `testutil/sims`. These takes an extra genesisState argument which is the default state of the app.
177178
* (x/gov) [#14720](https://github.com/cosmos/cosmos-sdk/pull/14720) Add an expedited field in the gov v1 proposal and `MsgNewMsgProposal`.
178179
* [#14847](https://github.com/cosmos/cosmos-sdk/pull/14847) App and ModuleManager methods `InitGenesis`, `ExportGenesis`, `BeginBlock` and `EndBlock` now also return an error.
179180
* (simulation) [#14728](https://github.com/cosmos/cosmos-sdk/pull/14728) Rename the `ParamChanges` field to `LegacyParamChange` and `Contents` to `LegacyProposalContents` in `simulation.SimulationState`. Additionally it adds a `ProposalMsgs` field to `simulation.SimulationState`.

crypto/keys/secp256k1/secp256k1_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Here are the specific versions used to generate the vectors.
9595
github.com/cosmos/btcutil v1.0.5
9696
github.com/cosmos/cosmos-sdk v0.46.8
9797
*/
98-
func generateKeyForCheckingConsistency() keyData {
98+
var _ = func() keyData {
9999
priv := secp256k1.GenPrivKey()
100100
encPriv := make([]byte, len(priv.Key)*2)
101101
hex.Encode(encPriv, priv.Key)

simapp/app_v2.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func NewSimApp(
202202
// For providing a custom a base account type add it below.
203203
// By default the auth module uses authtypes.ProtoBaseAccount().
204204
//
205-
// func() authtypes.AccountI { return authtypes.ProtoBaseAccount() },
205+
// func() sdk.AccountI { return authtypes.ProtoBaseAccount() },
206206

207207
//
208208
// MINT

simapp/params/params.go

-7
This file was deleted.

simapp/sim_bench_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func BenchmarkFullAppSimulation(b *testing.B) {
4949
b,
5050
os.Stdout,
5151
app.BaseApp,
52-
AppStateFn(app.AppCodec(), app.SimulationManager()),
52+
simtestutil.AppStateFn(app.AppCodec(), app.SimulationManager(), app.DefaultGenesis()),
5353
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
5454
simtestutil.SimulationOperations(app, app.AppCodec(), config),
5555
BlockedAddresses(),
@@ -104,7 +104,7 @@ func BenchmarkInvariants(b *testing.B) {
104104
b,
105105
os.Stdout,
106106
app.BaseApp,
107-
AppStateFn(app.AppCodec(), app.SimulationManager()),
107+
simtestutil.AppStateFn(app.AppCodec(), app.SimulationManager(), app.DefaultGenesis()),
108108
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
109109
simtestutil.SimulationOperations(app, app.AppCodec(), config),
110110
BlockedAddresses(),

simapp/sim_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestFullAppSimulation(t *testing.T) {
9191
t,
9292
os.Stdout,
9393
app.BaseApp,
94-
AppStateFn(app.AppCodec(), app.SimulationManager()),
94+
simtestutil.AppStateFn(app.AppCodec(), app.SimulationManager(), app.DefaultGenesis()),
9595
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
9696
simtestutil.SimulationOperations(app, app.AppCodec(), config),
9797
BlockedAddresses(),
@@ -136,7 +136,7 @@ func TestAppImportExport(t *testing.T) {
136136
t,
137137
os.Stdout,
138138
app.BaseApp,
139-
AppStateFn(app.AppCodec(), app.SimulationManager()),
139+
simtestutil.AppStateFn(app.AppCodec(), app.SimulationManager(), app.DefaultGenesis()),
140140
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
141141
simtestutil.SimulationOperations(app, app.AppCodec(), config),
142142
BlockedAddresses(),
@@ -253,7 +253,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
253253
t,
254254
os.Stdout,
255255
app.BaseApp,
256-
AppStateFn(app.AppCodec(), app.SimulationManager()),
256+
simtestutil.AppStateFn(app.AppCodec(), app.SimulationManager(), app.DefaultGenesis()),
257257
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
258258
simtestutil.SimulationOperations(app, app.AppCodec(), config),
259259
BlockedAddresses(),
@@ -301,7 +301,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
301301
t,
302302
os.Stdout,
303303
newApp.BaseApp,
304-
AppStateFn(app.AppCodec(), app.SimulationManager()),
304+
simtestutil.AppStateFn(app.AppCodec(), app.SimulationManager(), app.DefaultGenesis()),
305305
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
306306
simtestutil.SimulationOperations(newApp, newApp.AppCodec(), config),
307307
BlockedAddresses(),
@@ -356,7 +356,7 @@ func TestAppStateDeterminism(t *testing.T) {
356356
t,
357357
os.Stdout,
358358
app.BaseApp,
359-
AppStateFn(app.AppCodec(), app.SimulationManager()),
359+
simtestutil.AppStateFn(app.AppCodec(), app.SimulationManager(), app.DefaultGenesis()),
360360
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
361361
simtestutil.SimulationOperations(app, app.AppCodec(), config),
362362
BlockedAddresses(),

simapp/state.go testutil/sims/state_helpers.go

+40-30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package simapp
1+
package sims
22

33
import (
44
"encoding/json"
@@ -8,12 +8,10 @@ import (
88
"os"
99
"time"
1010

11+
"cosmossdk.io/math"
1112
cmtjson "github.com/cometbft/cometbft/libs/json"
1213
cmttypes "github.com/cometbft/cometbft/types"
1314

14-
"cosmossdk.io/math"
15-
simappparams "cosmossdk.io/simapp/params"
16-
1715
"github.com/cosmos/cosmos-sdk/codec"
1816
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
1917
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -25,11 +23,21 @@ import (
2523
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
2624
)
2725

26+
// Simulation parameter constants
27+
const (
28+
StakePerAccount = "stake_per_account"
29+
InitiallyBondedValidators = "initially_bonded_validators"
30+
)
31+
2832
// AppStateFn returns the initial application state using a genesis or the simulation parameters.
2933
// It panics if the user provides files for both of them.
3034
// If a file is not given for the genesis or the sim params, it creates a randomized one.
31-
func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simtypes.AppStateFn {
32-
return func(r *rand.Rand, accs []simtypes.Account, config simtypes.Config,
35+
// genesisState is the default genesis state of the whole app.
36+
func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager, genesisState map[string]json.RawMessage) simtypes.AppStateFn {
37+
return func(
38+
r *rand.Rand,
39+
accs []simtypes.Account,
40+
config simtypes.Config,
3341
) (appState json.RawMessage, simAccs []simtypes.Account, chainID string, genesisTimestamp time.Time) {
3442
if simcli.FlagGenesisTimeValue == 0 {
3543
genesisTimestamp = simtypes.RandTimestamp(r)
@@ -44,7 +52,10 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
4452

4553
case config.GenesisFile != "":
4654
// override the default chain-id from simapp to set it later to the config
47-
genesisDoc, accounts := AppStateFromGenesisFileFn(r, cdc, config.GenesisFile)
55+
genesisDoc, accounts, err := AppStateFromGenesisFileFn(r, cdc, config.GenesisFile)
56+
if err != nil {
57+
panic(err)
58+
}
4859

4960
if simcli.FlagGenesisTimeValue == 0 {
5061
// use genesis timestamp if no custom timestamp is provided (i.e no random timestamp)
@@ -66,11 +77,11 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
6677
if err != nil {
6778
panic(err)
6879
}
69-
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams)
80+
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams, genesisState)
7081

7182
default:
7283
appParams := make(simtypes.AppParams)
73-
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams)
84+
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams, genesisState)
7485
}
7586

7687
rawState := make(map[string]json.RawMessage)
@@ -85,8 +96,7 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
8596
}
8697

8798
stakingState := new(stakingtypes.GenesisState)
88-
err = cdc.UnmarshalJSON(stakingStateBz, stakingState)
89-
if err != nil {
99+
if err = cdc.UnmarshalJSON(stakingStateBz, stakingState); err != nil {
90100
panic(err)
91101
}
92102
// compute not bonded balance
@@ -105,8 +115,7 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
105115
panic("bank genesis state is missing")
106116
}
107117
bankState := new(banktypes.GenesisState)
108-
err = cdc.UnmarshalJSON(bankStateBz, bankState)
109-
if err != nil {
118+
if err = cdc.UnmarshalJSON(bankStateBz, bankState); err != nil {
110119
panic(err)
111120
}
112121

@@ -141,24 +150,27 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
141150
// AppStateRandomizedFn creates calls each module's GenesisState generator function
142151
// and creates the simulation params
143152
func AppStateRandomizedFn(
144-
simManager *module.SimulationManager, r *rand.Rand, cdc codec.JSONCodec,
145-
accs []simtypes.Account, genesisTimestamp time.Time, appParams simtypes.AppParams,
153+
simManager *module.SimulationManager,
154+
r *rand.Rand,
155+
cdc codec.JSONCodec,
156+
accs []simtypes.Account,
157+
genesisTimestamp time.Time,
158+
appParams simtypes.AppParams,
159+
genesisState map[string]json.RawMessage,
146160
) (json.RawMessage, []simtypes.Account) {
147161
numAccs := int64(len(accs))
148-
genesisState := ModuleBasics.DefaultGenesis(cdc)
149-
150162
// generate a random amount of initial stake coins and a random initial
151163
// number of bonded accounts
152164
var (
153165
numInitiallyBonded int64
154166
initialStake math.Int
155167
)
156168
appParams.GetOrGenerate(
157-
cdc, simappparams.StakePerAccount, &initialStake, r,
169+
cdc, StakePerAccount, &initialStake, r,
158170
func(r *rand.Rand) { initialStake = math.NewInt(r.Int63n(1e12)) },
159171
)
160172
appParams.GetOrGenerate(
161-
cdc, simappparams.InitiallyBondedValidators, &numInitiallyBonded, r,
173+
cdc, InitiallyBondedValidators, &numInitiallyBonded, r,
162174
func(r *rand.Rand) { numInitiallyBonded = int64(r.Intn(300)) },
163175
)
164176

@@ -199,23 +211,21 @@ func AppStateRandomizedFn(
199211

200212
// AppStateFromGenesisFileFn util function to generate the genesis AppState
201213
// from a genesis.json file.
202-
func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile string) (cmttypes.GenesisDoc, []simtypes.Account) {
214+
func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile string) (cmttypes.GenesisDoc, []simtypes.Account, error) {
203215
bytes, err := os.ReadFile(genesisFile)
204216
if err != nil {
205217
panic(err)
206218
}
207219

208220
var genesis cmttypes.GenesisDoc
209221
// NOTE: CometBFT uses a custom JSON decoder for GenesisDoc
210-
err = cmtjson.Unmarshal(bytes, &genesis)
211-
if err != nil {
212-
panic(err)
222+
if err = cmtjson.Unmarshal(bytes, &genesis); err != nil {
223+
return genesis, nil, err
213224
}
214225

215-
var appState GenesisState
216-
err = json.Unmarshal(genesis.AppState, &appState)
217-
if err != nil {
218-
panic(err)
226+
var appState map[string]json.RawMessage
227+
if err = json.Unmarshal(genesis.AppState, &appState); err != nil {
228+
return genesis, nil, err
219229
}
220230

221231
var authGenesis authtypes.GenesisState
@@ -235,15 +245,15 @@ func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile str
235245

236246
privKey := secp256k1.GenPrivKeyFromSecret(privkeySeed)
237247

238-
a, ok := acc.GetCachedValue().(authtypes.AccountI)
248+
a, ok := acc.GetCachedValue().(sdk.AccountI)
239249
if !ok {
240-
panic("expected account")
250+
return genesis, nil, fmt.Errorf("expected account")
241251
}
242252

243253
// create simulator accounts
244254
simAcc := simtypes.Account{PrivKey: privKey, PubKey: privKey.PubKey(), Address: a.GetAddress()}
245255
newAccs[i] = simAcc
246256
}
247257

248-
return genesis, newAccs
258+
return genesis, newAccs, nil
249259
}

0 commit comments

Comments
 (0)