Skip to content

Commit

Permalink
Merge pull request #16 from iqlusioninc/sam/lsm-sim-tests
Browse files Browse the repository at this point in the history
LSM simulation tests
  • Loading branch information
sampocs authored Jun 28, 2023
2 parents 5dcb6ee + 6158c69 commit 1b4d7e8
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 146 deletions.
13 changes: 7 additions & 6 deletions simapp/params/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ const (
DefaultWeightMsgDelegate int = 100
DefaultWeightMsgUndelegate int = 100
DefaultWeightMsgBeginRedelegate int = 100
DefaultWeightMsgCancelUnbondingDelegation int = 100
DefaultWeightMsgTokenizeShares int = 100
DefaultWeightMsgRedeemTokensforShares int = 100
DefaultWeightMsgTransferTokenizeShareRecord int = 50
DefaultWeightMsgEnableTokenizeShares int = 100
DefaultWeightMsgDisableTokenizeShares int = 100
DefaultWeightMsgCancelUnbondingDelegation int = 5
DefaultWeightMsgValidatorBond int = 100
DefaultWeightMsgTokenizeShares int = 25
DefaultWeightMsgRedeemTokensforShares int = 25
DefaultWeightMsgTransferTokenizeShareRecord int = 5
DefaultWeightMsgEnableTokenizeShares int = 1
DefaultWeightMsgDisableTokenizeShares int = 1

DefaultWeightCommunitySpendProposal int = 5
DefaultWeightTextProposal int = 5
Expand Down
4 changes: 3 additions & 1 deletion x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,9 @@ func (k msgServer) RedeemTokensForShares(goCtx context.Context, msg *types.MsgRe
_, found = k.GetDelegation(ctx, record.GetModuleAddress(), valAddr)
if !found {
if k.hooks != nil {
return nil, k.hooks.BeforeTokenizeShareRecordRemoved(ctx, record.Id)
if err := k.hooks.BeforeTokenizeShareRecordRemoved(ctx, record.Id); err != nil {
return nil, err
}
}

err = k.DeleteTokenizeShareRecord(ctx, record.Id)
Expand Down
61 changes: 46 additions & 15 deletions x/staking/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/simulation"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

// Simulation parameter constants
const (
unbondingTime = "unbonding_time"
maxValidators = "max_validators"
historicalEntries = "historical_entries"
UnbondingTime = "unbonding_time"
MaxValidators = "max_validators"
HistoricalEntries = "historical_entries"
ValidatorBondFactor = "validator_bond_factor"
GlobalLiquidStakingCap = "global_liquid_staking_cap"
ValidatorLiquidStakingCap = "validator_liquid_staking_cap"
)

// genUnbondingTime returns randomized UnbondingTime
Expand All @@ -36,41 +40,68 @@ func getHistEntries(r *rand.Rand) uint32 {
return uint32(r.Intn(int(types.DefaultHistoricalEntries + 1)))
}

// getGlobalLiquidStakingCap returns randomized GlobalLiquidStakingCap between 0-1.
func getGlobalLiquidStakingCap(r *rand.Rand) sdk.Dec {
return simtypes.RandomDecAmount(r, sdk.OneDec())
}

// getValidatorLiquidStakingCap returns randomized ValidatorLiquidStakingCap between 0-1.
func getValidatorLiquidStakingCap(r *rand.Rand) sdk.Dec {
return simtypes.RandomDecAmount(r, sdk.OneDec())
}

// getValidatorBondFactor returns randomized ValidatorBondCap between -1 and 300.
func getValidatorBondFactor(r *rand.Rand) sdk.Dec {
return sdk.NewDec(int64(simtypes.RandIntBetween(r, -1, 300)))
}

// RandomizedGenState generates a random GenesisState for staking
func RandomizedGenState(simState *module.SimulationState) {
// params
var (
unbondTime time.Duration
maxVals uint32
histEntries uint32
unbondingTime time.Duration
maxValidators uint32
historicalEntries uint32
validatorBondFactor sdk.Dec
globalLiquidStakingCap sdk.Dec
validatorLiquidStakingCap sdk.Dec
)

simState.AppParams.GetOrGenerate(
simState.Cdc, unbondingTime, &unbondTime, simState.Rand,
func(r *rand.Rand) { unbondTime = genUnbondingTime(r) },
simState.Cdc, UnbondingTime, &unbondingTime, simState.Rand,
func(r *rand.Rand) { unbondingTime = genUnbondingTime(r) },
)

simState.AppParams.GetOrGenerate(
simState.Cdc, maxValidators, &maxVals, simState.Rand,
func(r *rand.Rand) { maxVals = genMaxValidators(r) },
simState.Cdc, MaxValidators, &maxValidators, simState.Rand,
func(r *rand.Rand) { maxValidators = genMaxValidators(r) },
)

simState.AppParams.GetOrGenerate(
simState.Cdc, historicalEntries, &histEntries, simState.Rand,
func(r *rand.Rand) { histEntries = getHistEntries(r) },
simState.Cdc, HistoricalEntries, &historicalEntries, simState.Rand,
func(r *rand.Rand) { historicalEntries = getHistEntries(r) },
)
simState.AppParams.GetOrGenerate(
simState.Cdc, ValidatorBondFactor, &validatorBondFactor, simState.Rand,
func(r *rand.Rand) { validatorBondFactor = getValidatorBondFactor(r) },
)
simState.AppParams.GetOrGenerate(
simState.Cdc, GlobalLiquidStakingCap, &globalLiquidStakingCap, simState.Rand,
func(r *rand.Rand) { globalLiquidStakingCap = getGlobalLiquidStakingCap(r) },
)
simState.AppParams.GetOrGenerate(
simState.Cdc, ValidatorLiquidStakingCap, &validatorLiquidStakingCap, simState.Rand,
func(r *rand.Rand) { validatorLiquidStakingCap = getValidatorLiquidStakingCap(r) },
)

// NOTE: the slashing module need to be defined after the staking module on the
// NewSimulationManager constructor for this to work
simState.UnbondTime = unbondTime
simState.UnbondTime = unbondingTime
params := types.NewParams(
simState.UnbondTime,
maxVals,
maxValidators,
7,
histEntries,
historicalEntries,
sdk.DefaultBondDenom,
validatorBondFactor,
globalLiquidStakingCap,
Expand Down
Loading

0 comments on commit 1b4d7e8

Please sign in to comment.