Skip to content

Commit

Permalink
[ValSet-Pref] Add Redelegate, Withdraw cli commands and sim_msgs (#3966)
Browse files Browse the repository at this point in the history
* added new cli commands

* added cli commands

* added withdraw sim function

* added simulator functions

* working redelegation cli test
  • Loading branch information
stackman27 authored Jan 10, 2023
1 parent a693a3b commit e58c3c1
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 33 deletions.
59 changes: 53 additions & 6 deletions x/valset-pref/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func GetTxCmd() *cobra.Command {
osmocli.AddTxCmd(txCmd, NewSetValSetCmd)
osmocli.AddTxCmd(txCmd, NewDelValSetCmd)
osmocli.AddTxCmd(txCmd, NewUnDelValSetCmd)
osmocli.AddTxCmd(txCmd, NewReDelValSetCmd)
osmocli.AddTxCmd(txCmd, NewWithRewValSetCmd)
return txCmd
}

Expand All @@ -36,7 +38,7 @@ func NewDelValSetCmd() (*osmocli.TxCliDesc, *types.MsgDelegateToValidatorSet) {
return &osmocli.TxCliDesc{
Use: "delegate-valset [delegator_addr] [amount]",
Short: "Delegate tokens to existing valset using delegatorAddress and tokenAmount.",
Example: "osmosisd tx valset-pref delegate-valset osmo1... 100stake",
Example: "osmosisd tx valset-pref delegate-valset osmo1... 100stake",
NumArgs: 2,
}, &types.MsgDelegateToValidatorSet{}
}
Expand All @@ -45,17 +47,65 @@ func NewUnDelValSetCmd() (*osmocli.TxCliDesc, *types.MsgUndelegateFromValidatorS
return &osmocli.TxCliDesc{
Use: "undelegate-valset [delegator_addr] [amount]",
Short: "UnDelegate tokens from existing valset using delegatorAddress and tokenAmount.",
Example: "osmosisd tx valset-pref undelegate-valset osmo1... 100stake",
Example: "osmosisd tx valset-pref undelegate-valset osmo1... 100stake",
NumArgs: 2,
}, &types.MsgUndelegateFromValidatorSet{}
}

func NewReDelValSetCmd() (*osmocli.TxCliDesc, *types.MsgRedelegateValidatorSet) {
return &osmocli.TxCliDesc{
Use: "redelegate-valset [delegator_addr] [validators] [weights]",
Short: "Redelegate tokens from existing validators to new sets of validators",
Example: "osmosisd tx valset-pref redelegate-valset osmo1... osmovaloper1efg...,osmovaloper1hij... 0.56,0.44",
NumArgs: 3,
ParseAndBuildMsg: NewMsgReDelValidatorSetPreference,
}, &types.MsgRedelegateValidatorSet{}
}

func NewWithRewValSetCmd() (*osmocli.TxCliDesc, *types.MsgWithdrawDelegationRewards) {
return &osmocli.TxCliDesc{
Use: "withdraw-reward-valset [delegator_addr]",
Short: "Withdraw delegation reward form the new validator set.",
Example: "osmosisd tx valset-pref withdraw-valset osmo1...",
NumArgs: 1,
}, &types.MsgWithdrawDelegationRewards{}
}

func NewMsgSetValidatorSetPreference(clientCtx client.Context, args []string, fs *pflag.FlagSet) (sdk.Msg, error) {
delAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return nil, err
}

valset, err := ValidateValAddrAndWeight(args)
if err != nil {
return nil, err
}

return types.NewMsgSetValidatorSetPreference(
delAddr,
valset,
), nil
}

func NewMsgReDelValidatorSetPreference(clientCtx client.Context, args []string, fs *pflag.FlagSet) (sdk.Msg, error) {
delAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return nil, err
}

valset, err := ValidateValAddrAndWeight(args)
if err != nil {
return nil, err
}

return types.NewMsgRedelegateValidatorSet(
delAddr,
valset,
), nil
}

func ValidateValAddrAndWeight(args []string) ([]types.ValidatorPreference, error) {
var valAddrs []string
valAddrs = append(valAddrs, strings.Split(args[1], ",")...)

Expand All @@ -80,8 +130,5 @@ func NewMsgSetValidatorSetPreference(clientCtx client.Context, args []string, fs
})
}

return types.NewMsgSetValidatorSetPreference(
delAddr,
valset,
), nil
return valset, nil
}
112 changes: 86 additions & 26 deletions x/valset-pref/simulation/sim_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,12 @@ import (
)

func RandomMsgSetValSetPreference(k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx, ctx sdk.Context) (*types.MsgSetValidatorSetPreference, error) {
var preferences []types.ValidatorPreference

// Start with a weight of 1
remainingWeight := sdk.NewDec(1)

// Generate random validators with random weights that sums to 1
for remainingWeight.GT(sdk.ZeroDec()) {
randValidator := RandomValidator(ctx, sim)
if randValidator == nil {
return nil, fmt.Errorf("No validator")
}

randValue, err := RandomWeight(remainingWeight)
if err != nil {
return nil, fmt.Errorf("Error with random weights")
}

remainingWeight = remainingWeight.Sub(randValue)
if !randValue.Equal(sdk.ZeroDec()) {
preferences = append(preferences, types.ValidatorPreference{
ValOperAddress: randValidator.OperatorAddress,
Weight: randValue,
})
}
preferences, err := GetRandomValAndWeights(ctx, k, sim, remainingWeight)
if err != nil {
return nil, err
}

return &types.MsgSetValidatorSetPreference{
Expand All @@ -62,7 +44,7 @@ func RandomMsgDelegateToValSet(k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx,
}, nil
}

func RandomMsgUnDelegateToValSet(k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx, ctx sdk.Context) (*types.MsgUndelegateFromValidatorSet, error) {
func RandomMsgUnDelegateFromValSet(k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx, ctx sdk.Context) (*types.MsgUndelegateFromValidatorSet, error) {
delegator := sim.RandomSimAccount()
// check if the delegator valset created
err := GetRandomExistingValSet(ctx, k, sim, delegator.Address)
Expand All @@ -71,7 +53,7 @@ func RandomMsgUnDelegateToValSet(k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx
}

// check that the delegator has delegated tokens
err = GetRandomExistingDelegation(ctx, k, sim, delegator.Address)
_, err = GetRandomExistingDelegation(ctx, k, sim, delegator.Address)
if err != nil {
return nil, err
}
Expand All @@ -83,6 +65,57 @@ func RandomMsgUnDelegateToValSet(k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx
}, nil
}

func RandomMsgReDelegateToValSet(k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx, ctx sdk.Context) (*types.MsgRedelegateValidatorSet, error) {
delegator := sim.RandomSimAccount()
// check if the delegator valset created
err := GetRandomExistingValSet(ctx, k, sim, delegator.Address)
if err != nil {
return nil, err
}

// check that the delegator has delegated tokens
_, err = GetRandomExistingDelegation(ctx, k, sim, delegator.Address)
if err != nil {
return nil, err
}

remainingWeight := sdk.NewDec(1)
preferences, err := GetRandomValAndWeights(ctx, k, sim, remainingWeight)
if err != nil {
return nil, err
}

return &types.MsgRedelegateValidatorSet{
Delegator: delegator.Address.String(),
Preferences: preferences,
}, nil
}

func RandomMsgWithdrawRewardsFromValSet(k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx, ctx sdk.Context) (*types.MsgWithdrawDelegationRewards, error) {
delegator := sim.RandomSimAccount()

err := GetRandomExistingValSet(ctx, k, sim, delegator.Address)
if err != nil {
return nil, err
}

// check that the delegator has delegated tokens
delegations, err := GetRandomExistingDelegation(ctx, k, sim, delegator.Address)
if err != nil {
return nil, err
}

delegation := delegations[rand.Intn(len(delegations))]
validator := sim.StakingKeeper().Validator(ctx, delegation.GetValidatorAddr())
if validator == nil {
return nil, fmt.Errorf("validator not found")
}

return &types.MsgWithdrawDelegationRewards{
Delegator: delegator.Address.String(),
}, nil
}

func RandomValidator(ctx sdk.Context, sim *osmosimtypes.SimCtx) *stakingtypes.Validator {
rand.Seed(time.Now().UnixNano())

Expand All @@ -94,6 +127,33 @@ func RandomValidator(ctx sdk.Context, sim *osmosimtypes.SimCtx) *stakingtypes.Va
return &validators[rand.Intn(len(validators))]
}

func GetRandomValAndWeights(ctx sdk.Context, k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx, remainingWeight sdk.Dec) ([]types.ValidatorPreference, error) {
var preferences []types.ValidatorPreference

// Generate random validators with random weights that sums to 1
for remainingWeight.GT(sdk.ZeroDec()) {
randValidator := RandomValidator(ctx, sim)
if randValidator == nil {
return nil, fmt.Errorf("No validator")
}

randValue, err := RandomWeight(remainingWeight)
if err != nil {
return nil, fmt.Errorf("Error with random weights")
}

remainingWeight = remainingWeight.Sub(randValue)
if !randValue.Equal(sdk.ZeroDec()) {
preferences = append(preferences, types.ValidatorPreference{
ValOperAddress: randValidator.OperatorAddress,
Weight: randValue,
})
}
}

return preferences, nil
}

// TODO: Change this to user GetDelegations() once #3857 gets merged, issue created
func GetRandomExistingValSet(ctx sdk.Context, k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx, delegatorAddr sdk.AccAddress) error {
// Get Valset delegations
Expand All @@ -105,14 +165,14 @@ func GetRandomExistingValSet(ctx sdk.Context, k valsetkeeper.Keeper, sim *osmosi
return nil
}

func GetRandomExistingDelegation(ctx sdk.Context, k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx, delegatorAddr sdk.AccAddress) error {
func GetRandomExistingDelegation(ctx sdk.Context, k valsetkeeper.Keeper, sim *osmosimtypes.SimCtx, delegatorAddr sdk.AccAddress) ([]stakingtypes.Delegation, error) {
// gets the existing delegation
existingDelegations := sim.StakingKeeper().GetDelegatorDelegations(ctx, delegatorAddr, math.MaxUint16)
if len(existingDelegations) == 0 {
return fmt.Errorf("No existing delegation")
return nil, fmt.Errorf("No existing delegation")
}

return nil
return existingDelegations, nil
}

// Random float point from 0-1
Expand Down
4 changes: 3 additions & 1 deletion x/valset-pref/valpref-module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ func (am AppModule) Actions() []simtypes.Action {
return []simtypes.Action{
simtypes.NewMsgBasedAction("SetValidatorSetPreference", am.keeper, simulation.RandomMsgSetValSetPreference),
simtypes.NewMsgBasedAction("MsgDelegateToValidatorSet", am.keeper, simulation.RandomMsgDelegateToValSet),
simtypes.NewMsgBasedAction("MsgUndelegateFromValidatorSet", am.keeper, simulation.RandomMsgUnDelegateToValSet),
simtypes.NewMsgBasedAction("MsgUndelegateFromValidatorSet", am.keeper, simulation.RandomMsgUnDelegateFromValSet),
simtypes.NewMsgBasedAction("MsgRedelegateValSet", am.keeper, simulation.RandomMsgReDelegateToValSet),
simtypes.NewMsgBasedAction("MsgWithdrawDelegationRewards", am.keeper, simulation.RandomMsgWithdrawRewardsFromValSet),
}
}

0 comments on commit e58c3c1

Please sign in to comment.