Simulation Genesis template leads to nondeterminism
Describe the bug A clear and concise description of what the bug is.
Scaffolding a new list or map with simulation enabled may add something like this in the corresponding module's GenerateGenesisState function:
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
accs := make([]string, len(simState.Accounts))
for i, acc := range simState.Accounts {
accs[i] = acc.Address.String()
}
moduleGenesis := types.GenesisState{
Params: types.DefaultParams(),
TypeList: []types.TestType{
{
ID: 0,
Creator: sample.AccAddress(),
},
{
ID: 1,
Creator: sample.AccAddress(),
},
},
TypeCount: 2,
// this line is used by starport scaffolding # simapp/module/genesisState
}
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&orgGenesis)
}
Running the same seed multiple times will yield a different AppHash. This is because of the usage of sample.AccAddress instead of some deterministic selection from the existing accounts. These types in the state will most likely go unused in the simulation anyways, since they are currently created by being owned by non-existent on-chain accounts.
Most state-modifying functions on objects in the store will check if an object is owned by the msg's creating address. In the case of these genesis items, most of these msgs will fail.
I recommend modifying this template to something like:
// GenerateGenesisState creates a randomized GenState of the module
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
accs := make([]string, len(simState.Accounts))
for i, acc := range simState.Accounts {
accs[i] = acc.Address.String()
}
typeList := make([]types.TestType, simState.Rand.Intn(10))
for i, _ := range typeList {
typeList[i] = types.TestType{
Creator: accs[simState.Rand.Intn(len(accs)-1)],
ID: uint64(i),
}
}
moduleGenesis := types.GenesisState{
Params: types.DefaultParams(),
TypeList: typeList,
TypeCount: uint64(len(orgList)),
// this line is used by starport scaffolding # simapp/module/genesisState
}
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&moduleGenesis)
}