Skip to content

Commit

Permalink
move simulation params back to modules (#4839)
Browse files Browse the repository at this point in the history
move simulation params back to modules (#4839)
  • Loading branch information
fedekunze authored Aug 5, 2019
1 parent 77d8bd9 commit 52c18e0
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 132 deletions.
35 changes: 35 additions & 0 deletions x/auth/simulation/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package simulation

import "math/rand"

// Simulation parameter constants
const (
MaxMemoChars = "max_memo_characters"
TxSigLimit = "tx_sig_limit"
TxSizeCostPerByte = "tx_size_cost_per_byte"
SigVerifyCostED25519 = "sig_verify_cost_ed25519"
SigVerifyCostSECP256K1 = "sig_verify_cost_secp256k1"
)

// GenParams generates random auth parameters
func GenParams(paramSims map[string]func(r *rand.Rand) interface{}) {
paramSims[MaxMemoChars] = func(r *rand.Rand) interface{} {
return uint64(RandIntBetween(r, 100, 200))
}

paramSims[TxSigLimit] = func(r *rand.Rand) interface{} {
return uint64(r.Intn(7) + 1)
}

paramSims[TxSizeCostPerByte] = func(r *rand.Rand) interface{} {
return uint64(RandIntBetween(r, 5, 15))
}

paramSims[SigVerifyCostED25519] = func(r *rand.Rand) interface{} {
return uint64(RandIntBetween(r, 500, 1000))
}

paramSims[SigVerifyCostSECP256K1] = func(r *rand.Rand) interface{} {
return uint64(RandIntBetween(r, 500, 1000))
}
}
15 changes: 15 additions & 0 deletions x/bank/simulation/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package simulation

import "math/rand"

// Simulation parameter constants
const (
SendEnabled = "send_enabled"
)

// GenParams generates random bank parameters
func GenParams(paramSims map[string]func(r *rand.Rand) interface{}) {
paramSims[SendEnabled] = func(r *rand.Rand) interface{} {
return r.Int63n(2) == 0
}
}
7 changes: 4 additions & 3 deletions x/distribution/simulation/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov"
govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
simutil "github.com/cosmos/cosmos-sdk/x/simulation/util"
)

// SimulateMsgSetWithdrawAddress generates a MsgSetWithdrawAddress with random values.
Expand Down Expand Up @@ -95,15 +96,15 @@ func SimulateCommunityPoolSpendProposalContent(k distribution.Keeper) govsim.Con
balance := k.GetFeePool(ctx).CommunityPool
if len(balance) > 0 {
denomIndex := r.Intn(len(balance))
amount, goErr := simulation.RandPositiveInt(r, balance[denomIndex].Amount.TruncateInt())
amount, goErr := simutil.RandPositiveInt(r, balance[denomIndex].Amount.TruncateInt())
if goErr == nil {
denom := balance[denomIndex].Denom
coins = sdk.NewCoins(sdk.NewCoin(denom, amount.Mul(sdk.NewInt(2))))
}
}
return distribution.NewCommunityPoolSpendProposal(
simulation.RandStringOfLength(r, 10),
simulation.RandStringOfLength(r, 100),
simutil.RandStringOfLength(r, 10),
simutil.RandStringOfLength(r, 100),
recipientAcc.Address,
coins,
)
Expand Down
30 changes: 30 additions & 0 deletions x/distribution/simulation/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package simulation

import (
"math/rand"

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


// Simulation parameter constants
const (
CommunityTax = "community_tax"
BaseProposerReward = "base_proposer_reward"
BonusProposerReward = "bonus_proposer_reward"
)

// GenParams generates random distribution parameters
func GenParams(paramSims map[string]func(r *rand.Rand) interface{}) {
paramSims[CommunityTax] = func(r *rand.Rand) interface{} {
return sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(int64(r.Intn(30)), 2))
}

paramSims[BaseProposerReward] = func(r *rand.Rand) interface{} {
return sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(int64(r.Intn(30)), 2))
}

paramSims[BonusProposerReward] = func(r *rand.Rand) interface{} {
return sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(int64(r.Intn(30)), 2))
}
}
5 changes: 3 additions & 2 deletions x/gov/simulation/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/simulation"
simutil "github.com/cosmos/cosmos-sdk/x/simulation/util"
)

// ContentSimulator defines a function type alias for generating random proposal
Expand Down Expand Up @@ -109,8 +110,8 @@ func simulateHandleMsgSubmitProposal(msg gov.MsgSubmitProposal, handler sdk.Hand
// SimulateTextProposalContent returns random text proposal content.
func SimulateTextProposalContent(r *rand.Rand, _ *baseapp.BaseApp, _ sdk.Context, _ []simulation.Account) gov.Content {
return gov.NewTextProposal(
simulation.RandStringOfLength(r, 140),
simulation.RandStringOfLength(r, 5000),
simutil.RandStringOfLength(r, 140),
simutil.RandStringOfLength(r, 5000),
)
}

Expand Down
40 changes: 40 additions & 0 deletions x/gov/simulation/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package simulation

import (
"math/rand"
"time"

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

// Simulation parameter constants
const (
DepositParamsMinDeposit = "deposit_params_min_deposit"
VotingParamsVotingPeriod = "voting_params_voting_period"
TallyParamsQuorum = "tally_params_quorum"
TallyParamsThreshold = "tally_params_threshold"
TallyParamsVeto = "tally_params_veto"
)

// GenParams generates random gov parameters
func GenParams(paramSims map[string]func(r *rand.Rand) interface{}) {
paramSims[DepositParamsMinDeposit] = func(r *rand.Rand) interface{} {
return sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, int64(RandIntBetween(r, 1, 1e3)))}
}

paramSims[VotingParamsVotingPeriod] = func(r *rand.Rand) interface{} {
return time.Duration(RandIntBetween(r, 1, 2*60*60*24*2)) * time.Second
}

paramSims[TallyParamsQuorum] = func(r *rand.Rand) interface{} {
return sdk.NewDecWithPrec(int64(RandIntBetween(r, 334, 500)), 3)
}

paramSims[TallyParamsThreshold] = func(r *rand.Rand) interface{} {
return sdk.NewDecWithPrec(int64(RandIntBetween(r, 450, 550)), 3)
}

paramSims[TallyParamsVeto] = func(r *rand.Rand) interface{} {
return sdk.NewDecWithPrec(int64(RandIntBetween(r, 250, 334)), 3)
}
}
39 changes: 39 additions & 0 deletions x/mint/simulation/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package simulation

import (
"math/rand"

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

// Simulation parameter constants
const (
InflationRateChange = "inflation_rate_change"
Inflation = "inflation"
InflationMax = "inflation_max"
InflationMin = "inflation_min"
GoalBonded = "goal_bonded"
)

// GenParams generates random gov parameters
func GenParams(paramSims map[string]func(r *rand.Rand) interface{}) {
paramSims[InflationRateChange] = func(r *rand.Rand) interface{} {
return sdk.NewDecWithPrec(int64(r.Intn(99)), 2)
}

paramSims[Inflation] = func(r *rand.Rand) interface{} {
return sdk.NewDecWithPrec(int64(r.Intn(99)), 2)
}

paramSims[InflationMax] = func(r *rand.Rand) interface{} {
return sdk.NewDecWithPrec(20, 2)
}

paramSims[InflationMin] = func(r *rand.Rand) interface{} {
return sdk.NewDecWithPrec(7, 2)
}

paramSims[GoalBonded] = func(r *rand.Rand) interface{} {
return sdk.NewDecWithPrec(67, 2)
}
}
9 changes: 5 additions & 4 deletions x/params/simulation/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/simulation"
simutil "github.com/cosmos/cosmos-sdk/x/simulation/util"
)

type simParamChange struct {
Expand Down Expand Up @@ -111,7 +112,7 @@ var paramChangePool = []simParamChange{
}

pc := make(map[string]string)
numChanges := simulation.RandIntBetween(r, 1, len(changes))
numChanges := simutil.RandIntBetween(r, 1, len(changes))
for i := 0; i < numChanges; i++ {
c := changes[r.Intn(len(changes))]

Expand Down Expand Up @@ -159,7 +160,7 @@ var paramChangePool = []simParamChange{
// It will generate a ParameterChangeProposal object with anywhere between 1 and
// 3 parameter changes all of which have random, but valid values.
func SimulateParamChangeProposalContent(r *rand.Rand, _ *baseapp.BaseApp, _ sdk.Context, _ []simulation.Account) gov.Content {
numChanges := simulation.RandIntBetween(r, 1, len(paramChangePool)/2)
numChanges := simutil.RandIntBetween(r, 1, len(paramChangePool)/2)
paramChanges := make([]params.ParamChange, numChanges, numChanges)
paramChangesKeys := make(map[string]struct{})

Expand All @@ -178,8 +179,8 @@ func SimulateParamChangeProposalContent(r *rand.Rand, _ *baseapp.BaseApp, _ sdk.
}

return params.NewParameterChangeProposal(
simulation.RandStringOfLength(r, 140),
simulation.RandStringOfLength(r, 5000),
simutil.RandStringOfLength(r, 140),
simutil.RandStringOfLength(r, 5000),
paramChanges,
)
}
20 changes: 20 additions & 0 deletions x/simulation/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// nolint
// autogenerated code using github.com/rigelrozanski/multitool
// aliases generated for the following subdirectories:
// ALIASGEN: github.com/cosmos/cosmos-sdk/x/simulation/util
package simulation

import (
"github.com/cosmos/cosmos-sdk/x/simulation/util"
)

var (
// functions aliases
RandStringOfLength = util.RandStringOfLength
RandPositiveInt = util.RandPositiveInt
RandomAmount = util.RandomAmount
RandomDecAmount = util.RandomDecAmount
RandTimestamp = util.RandTimestamp
RandIntBetween = util.RandIntBetween
DeriveRand = util.DeriveRand
)
Loading

0 comments on commit 52c18e0

Please sign in to comment.