From 4e86810a27b6a6a81d9e2586627a4ce046ec0b6a Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Wed, 3 Jul 2019 18:12:43 +0200 Subject: [PATCH] Merge PR #4674: Expose simapp genState generators and utils --- .../improvements/sdk/4676-expose-simapp-f | 1 + simapp/sim_test.go | 50 +++++++++---------- simapp/utils.go | 39 +++++++++------ simapp/utils_test.go | 30 +++++------ 4 files changed, 64 insertions(+), 56 deletions(-) create mode 100644 .pending/improvements/sdk/4676-expose-simapp-f diff --git a/.pending/improvements/sdk/4676-expose-simapp-f b/.pending/improvements/sdk/4676-expose-simapp-f new file mode 100644 index 000000000000..7c14d654efab --- /dev/null +++ b/.pending/improvements/sdk/4676-expose-simapp-f @@ -0,0 +1 @@ +#4674 Export `Simapp` genState generators and util functions by making them public diff --git a/simapp/sim_test.go b/simapp/sim_test.go index 3025a004f52b..d85645beae26 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -73,11 +73,11 @@ func getSimulateFromSeedInput(tb testing.TB, w io.Writer, app *SimApp) ( testing.TB, io.Writer, *baseapp.BaseApp, simulation.AppStateFn, int64, simulation.WeightedOperations, sdk.Invariants, int, int, bool, bool, bool) { - return tb, w, app.BaseApp, appStateFn, seed, + return tb, w, app.BaseApp, AppStateFn, seed, testAndRunTxs(app), invariants(app), numBlocks, blockSize, commit, lean, onOperation } -func appStateFromGenesisFileFn( +func AppStateFromGenesisFileFn( r *rand.Rand, _ []simulation.Account, _ time.Time, ) (json.RawMessage, []simulation.Account, string) { @@ -143,15 +143,15 @@ func appStateRandomizedFn( `, amount, numInitiallyBonded, ) - genGenesisAccounts(cdc, r, accs, genesisTimestamp, amount, numInitiallyBonded, genesisState) - genAuthGenesisState(cdc, r, appParams, genesisState) - genBankGenesisState(cdc, r, appParams, genesisState) - genSupplyGenesisState(cdc, amount, numInitiallyBonded, int64(len(accs)), genesisState) - genGovGenesisState(cdc, r, appParams, genesisState) - genMintGenesisState(cdc, r, appParams, genesisState) - genDistrGenesisState(cdc, r, appParams, genesisState) - stakingGen := genStakingGenesisState(cdc, r, accs, amount, numAccs, numInitiallyBonded, appParams, genesisState) - genSlashingGenesisState(cdc, r, stakingGen, appParams, genesisState) + GenGenesisAccounts(cdc, r, accs, genesisTimestamp, amount, numInitiallyBonded, genesisState) + GenAuthGenesisState(cdc, r, appParams, genesisState) + GenBankGenesisState(cdc, r, appParams, genesisState) + GenSupplyGenesisState(cdc, amount, numInitiallyBonded, int64(len(accs)), genesisState) + GenGovGenesisState(cdc, r, appParams, genesisState) + GenMintGenesisState(cdc, r, appParams, genesisState) + GenDistrGenesisState(cdc, r, appParams, genesisState) + stakingGen := GenStakingGenesisState(cdc, r, accs, amount, numAccs, numInitiallyBonded, appParams, genesisState) + GenSlashingGenesisState(cdc, r, stakingGen, appParams, genesisState) appState, err := MakeCodec().MarshalJSON(genesisState) if err != nil { @@ -161,7 +161,7 @@ func appStateRandomizedFn( return appState, accs, "simulation" } -func appStateFn( +func AppStateFn( r *rand.Rand, accs []simulation.Account, genesisTimestamp time.Time, ) (appState json.RawMessage, simAccs []simulation.Account, chainID string) { @@ -172,7 +172,7 @@ func appStateFn( panic("cannot provide both a genesis file and a params file") case genesisFile != "": - appState, simAccs, chainID = appStateFromGenesisFileFn(r, accs, genesisTimestamp) + appState, simAccs, chainID = AppStateFromGenesisFileFn(r, accs, genesisTimestamp) case paramsFile != "": appParams := make(simulation.AppParams) @@ -192,7 +192,7 @@ func appStateFn( return appState, simAccs, chainID } -func genAuthGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState map[string]json.RawMessage) { +func GenAuthGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState map[string]json.RawMessage) { authGenesis := auth.NewGenesisState( auth.NewParams( func(r *rand.Rand) uint64 { @@ -242,7 +242,7 @@ func genAuthGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams genesisState[auth.ModuleName] = cdc.MustMarshalJSON(authGenesis) } -func genBankGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState map[string]json.RawMessage) { +func GenBankGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState map[string]json.RawMessage) { bankGenesis := bank.NewGenesisState( func(r *rand.Rand) bool { var v bool @@ -258,7 +258,7 @@ func genBankGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams genesisState[bank.ModuleName] = cdc.MustMarshalJSON(bankGenesis) } -func genSupplyGenesisState(cdc *codec.Codec, amount, numInitiallyBonded, numAccs int64, genesisState map[string]json.RawMessage) { +func GenSupplyGenesisState(cdc *codec.Codec, amount, numInitiallyBonded, numAccs int64, genesisState map[string]json.RawMessage) { totalSupply := sdk.NewInt(amount * (numAccs + numInitiallyBonded)) supplyGenesis := supply.NewGenesisState( supply.NewSupply(sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupply))), @@ -268,7 +268,7 @@ func genSupplyGenesisState(cdc *codec.Codec, amount, numInitiallyBonded, numAccs genesisState[supply.ModuleName] = cdc.MustMarshalJSON(supplyGenesis) } -func genGenesisAccounts( +func GenGenesisAccounts( cdc *codec.Codec, r *rand.Rand, accs []simulation.Account, genesisTimestamp time.Time, amount, numInitiallyBonded int64, genesisState map[string]json.RawMessage, @@ -326,7 +326,7 @@ func genGenesisAccounts( genesisState[genaccounts.ModuleName] = cdc.MustMarshalJSON(genesisAccounts) } -func genGovGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState map[string]json.RawMessage) { +func GenGovGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState map[string]json.RawMessage) { var vp time.Duration ap.GetOrGenerate(cdc, simulation.VotingParamsVotingPeriod, &vp, r, func(r *rand.Rand) { @@ -379,7 +379,7 @@ func genGovGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState[gov.ModuleName] = cdc.MustMarshalJSON(govGenesis) } -func genMintGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState map[string]json.RawMessage) { +func GenMintGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState map[string]json.RawMessage) { mintGenesis := mint.NewGenesisState( mint.InitialMinter( func(r *rand.Rand) sdk.Dec { @@ -433,7 +433,7 @@ func genMintGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams genesisState[mint.ModuleName] = cdc.MustMarshalJSON(mintGenesis) } -func genDistrGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState map[string]json.RawMessage) { +func GenDistrGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState map[string]json.RawMessage) { distrGenesis := distr.GenesisState{ FeePool: distr.InitialFeePool(), CommunityTax: func(r *rand.Rand) sdk.Dec { @@ -466,7 +466,7 @@ func genDistrGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParam genesisState[distr.ModuleName] = cdc.MustMarshalJSON(distrGenesis) } -func genSlashingGenesisState( +func GenSlashingGenesisState( cdc *codec.Codec, r *rand.Rand, stakingGen staking.GenesisState, ap simulation.AppParams, genesisState map[string]json.RawMessage, ) { @@ -522,7 +522,7 @@ func genSlashingGenesisState( genesisState[slashing.ModuleName] = cdc.MustMarshalJSON(slashingGenesis) } -func genStakingGenesisState( +func GenStakingGenesisState( cdc *codec.Codec, r *rand.Rand, accs []simulation.Account, amount, numAccs, numInitiallyBonded int64, ap simulation.AppParams, genesisState map[string]json.RawMessage, ) staking.GenesisState { @@ -944,7 +944,7 @@ func TestAppImportExport(t *testing.T) { storeB := ctxB.KVStore(storeKeyB) kvA, kvB, count, equal := sdk.DiffKVStores(storeA, storeB, prefixes) fmt.Printf("Compared %d key/value pairs between %s and %s\n", count, storeKeyA, storeKeyB) - require.True(t, equal, getSimulationLog(storeKeyA.Name(), app.cdc, newApp.cdc, kvA, kvB)) + require.True(t, equal, GetSimulationLog(storeKeyA.Name(), app.cdc, newApp.cdc, kvA, kvB)) } } @@ -1039,7 +1039,7 @@ func TestAppStateDeterminism(t *testing.T) { // Run randomized simulation simulation.SimulateFromSeed( - t, os.Stdout, app.BaseApp, appStateFn, seed, + t, os.Stdout, app.BaseApp, AppStateFn, seed, testAndRunTxs(app), []sdk.Invariant{}, 50, @@ -1071,7 +1071,7 @@ func BenchmarkInvariants(b *testing.B) { // 2. Run parameterized simulation (w/o invariants) _, err := simulation.SimulateFromSeed( - b, ioutil.Discard, app.BaseApp, appStateFn, seed, testAndRunTxs(app), + b, ioutil.Discard, app.BaseApp, AppStateFn, seed, testAndRunTxs(app), []sdk.Invariant{}, numBlocks, blockSize, commit, lean, onOperation, ) if err != nil { diff --git a/simapp/utils.go b/simapp/utils.go index 415520909348..a3397f8186b6 100644 --- a/simapp/utils.go +++ b/simapp/utils.go @@ -36,9 +36,9 @@ func NewSimAppUNSAFE(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLat return gapp, gapp.keyMain, gapp.keyStaking, gapp.stakingKeeper } -// getSimulationLog unmarshals the KVPair's Value to the corresponding type based on the +// GetSimulationLog unmarshals the KVPair's Value to the corresponding type based on the // each's module store key and the prefix bytes of the KVPair's key. -func getSimulationLog(storeName string, cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) (log string) { +func GetSimulationLog(storeName string, cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) (log string) { log = fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvA.Key, kvA.Value, kvB.Key, kvB.Value) if len(kvA.Value) == 0 && len(kvB.Value) == 0 { @@ -47,25 +47,26 @@ func getSimulationLog(storeName string, cdcA, cdcB *codec.Codec, kvA, kvB cmn.KV switch storeName { case auth.StoreKey: - return decodeAccountStore(cdcA, cdcB, kvA, kvB) + return DecodeAccountStore(cdcA, cdcB, kvA, kvB) case mint.StoreKey: - return decodeMintStore(cdcA, cdcB, kvA, kvB) + return DecodeMintStore(cdcA, cdcB, kvA, kvB) case staking.StoreKey: - return decodeStakingStore(cdcA, cdcB, kvA, kvB) + return DecodeStakingStore(cdcA, cdcB, kvA, kvB) case slashing.StoreKey: - return decodeSlashingStore(cdcA, cdcB, kvA, kvB) + return DecodeSlashingStore(cdcA, cdcB, kvA, kvB) case gov.StoreKey: - return decodeGovStore(cdcA, cdcB, kvA, kvB) + return DecodeGovStore(cdcA, cdcB, kvA, kvB) case distribution.StoreKey: - return decodeDistributionStore(cdcA, cdcB, kvA, kvB) + return DecodeDistributionStore(cdcA, cdcB, kvA, kvB) case supply.StoreKey: - return decodeSupplyStore(cdcA, cdcB, kvA, kvB) + return DecodeSupplyStore(cdcA, cdcB, kvA, kvB) default: return } } -func decodeAccountStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +// DecodeAccountStore unmarshals the KVPair's Value to the corresponding auth type +func DecodeAccountStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], auth.AddressStoreKeyPrefix): var accA, accB auth.Account @@ -82,7 +83,8 @@ func decodeAccountStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { } } -func decodeMintStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +// DecodeMintStore unmarshals the KVPair's Value to the corresponding mint type +func DecodeMintStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key, mint.MinterKey): var minterA, minterB mint.Minter @@ -94,7 +96,8 @@ func decodeMintStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { } } -func decodeDistributionStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +// DecodeDistributionStore unmarshals the KVPair's Value to the corresponding distribution type +func DecodeDistributionStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], distribution.FeePoolKey): var feePoolA, feePoolB distribution.FeePool @@ -149,7 +152,8 @@ func decodeDistributionStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) strin } } -func decodeStakingStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +// DecodeStakingStore unmarshals the KVPair's Value to the corresponding staking type +func DecodeStakingStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], staking.PoolKey): var poolA, poolB staking.Pool @@ -199,7 +203,8 @@ func decodeStakingStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { } } -func decodeSlashingStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +// DecodeSlashingStore unmarshals the KVPair's Value to the corresponding slashing type +func DecodeSlashingStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], slashing.ValidatorSigningInfoKey): var infoA, infoB slashing.ValidatorSigningInfo @@ -226,7 +231,8 @@ func decodeSlashingStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { } } -func decodeGovStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +// DecodeGovStore unmarshals the KVPair's Value to the corresponding gov type +func DecodeGovStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], gov.ProposalsKeyPrefix): var proposalA, proposalB gov.Proposal @@ -258,7 +264,8 @@ func decodeGovStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { } } -func decodeSupplyStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { +// DecodeSupplyStore unmarshals the KVPair's Value to the corresponding supply type +func DecodeSupplyStore(cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) string { switch { case bytes.Equal(kvA.Key[:1], supply.SupplyKey): var supplyA, supplyB supply.Supply diff --git a/simapp/utils_test.go b/simapp/utils_test.go index 78eb1be748a3..8f806760c00b 100644 --- a/simapp/utils_test.go +++ b/simapp/utils_test.go @@ -63,7 +63,7 @@ func TestGetSimulationLog(t *testing.T) { for _, tt := range tests { t.Run(tt.store, func(t *testing.T) { - require.NotPanics(t, func() { getSimulationLog(tt.store, cdc, cdc, tt.kvPair, tt.kvPair) }, tt.store) + require.NotPanics(t, func() { GetSimulationLog(tt.store, cdc, cdc, tt.kvPair, tt.kvPair) }, tt.store) }) } } @@ -91,9 +91,9 @@ func TestDecodeAccountStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { decodeAccountStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeAccountStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, decodeAccountStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeAccountStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } @@ -119,9 +119,9 @@ func TestDecodeMintStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { decodeMintStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeMintStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, decodeMintStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeMintStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } @@ -172,9 +172,9 @@ func TestDecodeDistributionStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { decodeDistributionStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeDistributionStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, decodeDistributionStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeDistributionStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } @@ -216,9 +216,9 @@ func TestDecodeStakingStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { decodeStakingStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeStakingStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, decodeStakingStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeStakingStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } @@ -251,9 +251,9 @@ func TestDecodeSlashingStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { decodeSlashingStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeSlashingStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, decodeSlashingStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeSlashingStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } @@ -294,9 +294,9 @@ func TestDecodeGovStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { decodeGovStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeGovStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, decodeGovStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeGovStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) } }) } @@ -324,9 +324,9 @@ func TestDecodeSupplyStore(t *testing.T) { t.Run(tt.name, func(t *testing.T) { switch i { case len(tests) - 1: - require.Panics(t, func() { decodeSupplyStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) + require.Panics(t, func() { DecodeSupplyStore(cdc, cdc, kvPairs[i], kvPairs[i]) }, tt.name) default: - require.Equal(t, tt.expectedLog, decodeSupplyStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) + require.Equal(t, tt.expectedLog, DecodeSupplyStore(cdc, cdc, kvPairs[i], kvPairs[i]), tt.name) } }) }