Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency of types/module package on x/simulation #5835

Merged
merged 28 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
028875f
interface WeightedProposalContent
jgimeno Mar 19, 2020
eca372b
remove module x simulation dependency
jgimeno Mar 19, 2020
1a4df96
interface the commit
jgimeno Mar 19, 2020
f7efda2
refactor middle step
jgimeno Mar 19, 2020
381869d
refactor and test pass
jgimeno Mar 19, 2020
b0076cb
refactor to types
jgimeno Mar 19, 2020
0d6a228
fix imports
jgimeno Mar 19, 2020
22e26bb
Merge branch 'master' into jonathan/5724-module-simulation-top-level-…
jgimeno Mar 20, 2020
d939b20
move param changes to types to
jgimeno Mar 20, 2020
62e9d99
Merge branch 'jonathan/5724-module-simulation-top-level-dependency' o…
jgimeno Mar 20, 2020
5e796a2
fix types
jgimeno Mar 20, 2020
dde07b4
Merge branch 'master' into jonathan/5724-module-simulation-top-level-…
jgimeno Mar 20, 2020
d94e80d
update simulation
jgimeno Mar 20, 2020
9b18788
move paramchange back to simulation
jgimeno Mar 20, 2020
20ac782
remove unused types
jgimeno Mar 20, 2020
325f7b2
add some test coverage
jgimeno Mar 20, 2020
37fd2f7
add go mod tidy
jgimeno Mar 20, 2020
eabf5c9
update changelog
jgimeno Mar 20, 2020
c1e956d
Merge branch 'master' into jonathan/5724-module-simulation-top-level-…
jgimeno Mar 20, 2020
6129702
update with make format
jgimeno Mar 21, 2020
2b0ec64
avoid dependency in simapp for module simulation
jgimeno Mar 22, 2020
bda03c3
Merge branch 'master' into jonathan/5724-module-simulation-top-level-…
jgimeno Mar 22, 2020
ccd8a95
Update CHANGELOG.md
jgimeno Mar 22, 2020
7af823e
Update x/gov/simulation/params.go
jgimeno Mar 23, 2020
fce2ae2
Update x/staking/module.go
jgimeno Mar 23, 2020
2a9c3e7
Update x/slashing/simulation/params.go
jgimeno Mar 23, 2020
33b8011
add format
jgimeno Mar 23, 2020
9cb2343
add issue in TODO
jgimeno Mar 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions simapp/helpers/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package helpers

import (
"github.com/cosmos/cosmos-sdk/types/module"
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
"math/rand"
"time"

"github.com/tendermint/tendermint/crypto"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/simulation"
)

// SimAppChainID hardcoded chainID for simulation
Expand All @@ -29,7 +29,7 @@ func GenTx(msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accnums
// create a random length memo
r := rand.New(rand.NewSource(time.Now().UnixNano()))

memo := simulation.RandStringOfLength(r, simulation.RandIntBetween(r, 0, 100))
memo := module.RandStringOfLength(r, module.RandIntBetween(r, 0, 100))

for i, p := range priv {
// use a empty chainID for ease of testing
Expand Down
20 changes: 10 additions & 10 deletions simapp/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
// It panics if the user provides files for both of them.
// If a file is not given for the genesis or the sim params, it creates a randomized one.
func AppStateFn(cdc *codec.Codec, simManager *module.SimulationManager) simulation.AppStateFn {
return func(r *rand.Rand, accs []simulation.Account, config simulation.Config,
) (appState json.RawMessage, simAccs []simulation.Account, chainID string, genesisTimestamp time.Time) {
return func(r *rand.Rand, accs []module.Account, config simulation.Config,
) (appState json.RawMessage, simAccs []module.Account, chainID string, genesisTimestamp time.Time) {

if FlagGenesisTimeValue == 0 {
genesisTimestamp = simulation.RandTimestamp(r)
genesisTimestamp = module.RandTimestamp(r)
} else {
genesisTimestamp = time.Unix(FlagGenesisTimeValue, 0)
}
Expand All @@ -50,7 +50,7 @@ func AppStateFn(cdc *codec.Codec, simManager *module.SimulationManager) simulati
simAccs = accounts

case config.ParamsFile != "":
appParams := make(simulation.AppParams)
appParams := make(module.AppParams)
bz, err := ioutil.ReadFile(config.ParamsFile)
if err != nil {
panic(err)
Expand All @@ -60,7 +60,7 @@ func AppStateFn(cdc *codec.Codec, simManager *module.SimulationManager) simulati
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams)

default:
appParams := make(simulation.AppParams)
appParams := make(module.AppParams)
appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams)
}

Expand All @@ -72,8 +72,8 @@ func AppStateFn(cdc *codec.Codec, simManager *module.SimulationManager) simulati
// and creates the simulation params
func AppStateRandomizedFn(
simManager *module.SimulationManager, r *rand.Rand, cdc *codec.Codec,
accs []simulation.Account, genesisTimestamp time.Time, appParams simulation.AppParams,
) (json.RawMessage, []simulation.Account) {
accs []module.Account, genesisTimestamp time.Time, appParams module.AppParams,
) (json.RawMessage, []module.Account) {
numAccs := int64(len(accs))
genesisState := NewDefaultGenesisState()

Expand Down Expand Up @@ -125,7 +125,7 @@ func AppStateRandomizedFn(

// AppStateFromGenesisFileFn util function to generate the genesis AppState
// from a genesis.json file.
func AppStateFromGenesisFileFn(r io.Reader, cdc *codec.Codec, genesisFile string) (tmtypes.GenesisDoc, []simulation.Account) {
func AppStateFromGenesisFileFn(r io.Reader, cdc *codec.Codec, genesisFile string) (tmtypes.GenesisDoc, []module.Account) {
bytes, err := ioutil.ReadFile(genesisFile)
if err != nil {
panic(err)
Expand All @@ -142,7 +142,7 @@ func AppStateFromGenesisFileFn(r io.Reader, cdc *codec.Codec, genesisFile string
cdc.MustUnmarshalJSON(appState[auth.ModuleName], &authGenesis)
}

newAccs := make([]simulation.Account, len(authGenesis.Accounts))
newAccs := make([]module.Account, len(authGenesis.Accounts))
for i, acc := range authGenesis.Accounts {
// Pick a random private key, since we don't know the actual key
// This should be fine as it's only used for mock Tendermint validators
Expand All @@ -155,7 +155,7 @@ func AppStateFromGenesisFileFn(r io.Reader, cdc *codec.Codec, genesisFile string
privKey := secp256k1.GenPrivKeySecp256k1(privkeySeed)

// create simulator accounts
simAcc := simulation.Account{PrivKey: privKey, PubKey: privKey.PubKey(), Address: acc.GetAddress()}
simAcc := module.Account{PrivKey: privKey, PubKey: privKey.PubKey(), Address: acc.GetAddress()}
newAccs[i] = simAcc
}

Expand Down
4 changes: 2 additions & 2 deletions simapp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func SetupSimulation(dirPrefix, dbName string) (simulation.Config, dbm.DB, strin

// SimulationOperations retrieves the simulation params from the provided file path
// and returns all the modules weighted operations
func SimulationOperations(app App, cdc *codec.Codec, config simulation.Config) []simulation.WeightedOperation {
func SimulationOperations(app App, cdc *codec.Codec, config simulation.Config) []module.WeightedOperation {
simState := module.SimulationState{
AppParams: make(simulation.AppParams),
AppParams: make(module.AppParams),
Cdc: cdc,
}

Expand Down
2 changes: 1 addition & 1 deletion x/simulation/account.go → types/module/account.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package simulation
package module

import (
"math/rand"
Expand Down
17 changes: 9 additions & 8 deletions x/simulation/account_test.go → types/module/account_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package simulation_test
package module_test

import (
"math/rand"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/types/module"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/simulation"
)

func TestRandomAccounts(t *testing.T) {
Expand All @@ -26,14 +27,14 @@ func TestRandomAccounts(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := simulation.RandomAccounts(r, tt.n)
got := module.RandomAccounts(r, tt.n)
require.Equal(t, tt.want, len(got))
if tt.n == 0 {
return
}
acc, i := simulation.RandomAcc(r, got)
acc, i := module.RandomAcc(r, got)
require.True(t, acc.Equals(got[i]))
accFound, found := simulation.FindAccount(got, acc.Address)
accFound, found := module.FindAccount(got, acc.Address)
require.True(t, found)
require.True(t, accFound.Equals(acc))
})
Expand All @@ -43,9 +44,9 @@ func TestRandomAccounts(t *testing.T) {
func TestFindAccountEmptySlice(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
accs := simulation.RandomAccounts(r, 1)
accs := module.RandomAccounts(r, 1)
require.Equal(t, 1, len(accs))
acc, found := simulation.FindAccount(nil, accs[0].Address)
acc, found := module.FindAccount(nil, accs[0].Address)
require.False(t, found)
require.Nil(t, acc.Address)
require.Nil(t, acc.PrivKey)
Expand All @@ -68,7 +69,7 @@ func TestRandomFees(t *testing.T) {
tt := tt

t.Run(tt.name, func(t *testing.T) {
got, err := simulation.RandomFees(r, sdk.Context{}, tt.spendableCoins)
got, err := module.RandomFees(r, sdk.Context{}, tt.spendableCoins)
if (err != nil) != tt.wantErr {
t.Errorf("RandomFees() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
1 change: 0 additions & 1 deletion types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (

"github.com/gorilla/mux"
"github.com/spf13/cobra"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client/context"
Expand Down
2 changes: 1 addition & 1 deletion x/simulation/rand_util.go → types/module/rand_util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package simulation
package module

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package simulation_test
package module_test

import (
"math/rand"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/types/module"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/simulation"
)

func TestRandSubsetCoins(t *testing.T) {
Expand All @@ -25,7 +26,7 @@ func TestRandSubsetCoins(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := simulation.RandSubsetCoins(tt.r, tt.coins)
got := module.RandSubsetCoins(tt.r, tt.coins)
gotStringRep := got.String()
sortedStringRep := got.Sort().String()
require.Equal(t, gotStringRep, sortedStringRep)
Expand All @@ -49,7 +50,7 @@ func TestRandStringOfLength(t *testing.T) {
tt := tt

t.Run(tt.name, func(t *testing.T) {
got := simulation.RandStringOfLength(r, tt.n)
got := module.RandStringOfLength(r, tt.n)
require.Equal(t, tt.want, len(got))
})
}
Expand Down
39 changes: 19 additions & 20 deletions types/module/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/simulation"
)

// AppModuleSimulation defines the standard functions that every module should expose
Expand All @@ -18,16 +17,16 @@ type AppModuleSimulation interface {
GenerateGenesisState(input *SimulationState)

// content functions used to simulate governance proposals
ProposalContents(simState SimulationState) []simulation.WeightedProposalContent
ProposalContents(simState SimulationState) []WeightedProposalContent

// randomized module parameters for param change proposals
RandomizedParams(r *rand.Rand) []simulation.ParamChange
RandomizedParams(r *rand.Rand) []ParamChange

// register a func to decode the each module's defined types from their corresponding store key
RegisterStoreDecoder(sdk.StoreDecoderRegistry)

// simulation operations (i.e msgs) with their respective weight
WeightedOperations(simState SimulationState) []simulation.WeightedOperation
WeightedOperations(simState SimulationState) []WeightedOperation
}

// SimulationManager defines a simulation manager that provides the high level utility
Expand All @@ -49,8 +48,8 @@ func NewSimulationManager(modules ...AppModuleSimulation) *SimulationManager {

// GetProposalContents returns each module's proposal content generator function
// with their default operation weight and key.
func (sm *SimulationManager) GetProposalContents(simState SimulationState) []simulation.WeightedProposalContent {
wContents := make([]simulation.WeightedProposalContent, 0, len(sm.Modules))
func (sm *SimulationManager) GetProposalContents(simState SimulationState) []WeightedProposalContent {
wContents := make([]WeightedProposalContent, 0, len(sm.Modules))
for _, module := range sm.Modules {
wContents = append(wContents, module.ProposalContents(simState)...)
}
Expand All @@ -75,7 +74,7 @@ func (sm *SimulationManager) GenerateGenesisStates(simState *SimulationState) {

// GenerateParamChanges generates randomized contents for creating params change
// proposal transactions
func (sm *SimulationManager) GenerateParamChanges(seed int64) (paramChanges []simulation.ParamChange) {
func (sm *SimulationManager) GenerateParamChanges(seed int64) (paramChanges []ParamChange) {
r := rand.New(rand.NewSource(seed))

for _, module := range sm.Modules {
Expand All @@ -86,8 +85,8 @@ func (sm *SimulationManager) GenerateParamChanges(seed int64) (paramChanges []si
}

// WeightedOperations returns all the modules' weighted operations of an application
func (sm *SimulationManager) WeightedOperations(simState SimulationState) []simulation.WeightedOperation {
wOps := make([]simulation.WeightedOperation, 0, len(sm.Modules))
func (sm *SimulationManager) WeightedOperations(simState SimulationState) []WeightedOperation {
wOps := make([]WeightedOperation, 0, len(sm.Modules))
for _, module := range sm.Modules {
wOps = append(wOps, module.WeightedOperations(simState)...)
}
Expand All @@ -98,15 +97,15 @@ func (sm *SimulationManager) WeightedOperations(simState SimulationState) []simu
// SimulationState is the input parameters used on each of the module's randomized
// GenesisState generator function
type SimulationState struct {
AppParams simulation.AppParams
Cdc *codec.Codec // application codec
Rand *rand.Rand // random number
GenState map[string]json.RawMessage // genesis state
Accounts []simulation.Account // simulation accounts
InitialStake int64 // initial coins per account
NumBonded int64 // number of initially bonded accounts
GenTimestamp time.Time // genesis timestamp
UnbondTime time.Duration // staking unbond time stored to use it as the slashing maximum evidence duration
ParamChanges []simulation.ParamChange // simulated parameter changes from modules
Contents []simulation.WeightedProposalContent // proposal content generator functions with their default weight and app sim key
AppParams AppParams
Cdc *codec.Codec // application codec
Rand *rand.Rand // random number
GenState map[string]json.RawMessage // genesis state
Accounts []Account // simulation accounts
InitialStake int64 // initial coins per account
NumBonded int64 // number of initially bonded accounts
GenTimestamp time.Time // genesis timestamp
UnbondTime time.Duration // staking unbond time stored to use it as the slashing maximum evidence duration
ParamChanges []ParamChange // simulated parameter changes from modules
Contents []WeightedProposalContent // proposal content generator functions with their default weight and app sim key
}
Loading