Skip to content

Commit

Permalink
Merge PR #4674: Expose simapp genState generators and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze authored and alexanderbez committed Jul 3, 2019
1 parent b0a359c commit 4e86810
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 56 deletions.
1 change: 1 addition & 0 deletions .pending/improvements/sdk/4676-expose-simapp-f
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4674 Export `Simapp` genState generators and util functions by making them public
50 changes: 25 additions & 25 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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 {
Expand All @@ -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) {

Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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))),
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}

}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
39 changes: 23 additions & 16 deletions simapp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions simapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down Expand Up @@ -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)
}
})
}
Expand All @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
}
})
}
Expand Down

0 comments on commit 4e86810

Please sign in to comment.