Skip to content

Commit

Permalink
feat(x/staking): autocli tx support (#17986)
Browse files Browse the repository at this point in the history
  • Loading branch information
atheeshp authored Oct 10, 2023
1 parent 4001e2b commit f149f1b
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 641 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* (testutil) [#17868](https://github.com/cosmos/cosmos-sdk/pull/17868) Added helper method `SubmitTestTx` in testutil to broadcast test txns to test e2e tests.
* (tests) [#17868](https://github.com/cosmos/cosmos-sdk/pull/17868) Added helper method `SubmitTestTx` in testutil to broadcast test txns to test e2e tests.
* (x/protocolpool) [#17657](https://github.com/cosmos/cosmos-sdk/pull/17657) Create a new `x/protocolpool` module that is responsible for handling community pool funds. This module is split out into a new module from x/distribution.
* (baseapp) [#16581](https://github.com/cosmos/cosmos-sdk/pull/16581) Implement Optimistic Execution as an experimental feature (not enabled by default).
* (client/keys) [#17639](https://github.com/cosmos/cosmos-sdk/pull/17639) Allows using and saving public keys encoded as base64
Expand All @@ -66,6 +66,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* (testutil) [#17986](https://github.com/cosmos/cosmos-sdk/pull/17986) `MsgRedelegateExec`, `MsgUnbondExec` has been removed because of AutoCLI migration.
* (testutil) [#17868](https://github.com/cosmos/cosmos-sdk/pull/17868) `MsgSendExec` has been removed because of AutoCLI migration.
* (x/distribution) [#17657](https://github.com/cosmos/cosmos-sdk/pull/17657) The `FundCommunityPool` and `DistributeFromFeePool` keeper methods are now removed from x/distribution.
* (x/distribution) [#17657](https://github.com/cosmos/cosmos-sdk/pull/17657) The distribution module keeper now takes a new argument `PoolKeeper` in addition.
Expand Down
28 changes: 9 additions & 19 deletions tests/e2e/authz/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client/flags"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
Expand All @@ -28,7 +27,7 @@ import (
govtestutil "github.com/cosmos/cosmos-sdk/x/gov/client/testutil"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
stakingcli "github.com/cosmos/cosmos-sdk/x/staking/client/cli"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

type E2ETestSuite struct {
Expand Down Expand Up @@ -178,12 +177,6 @@ var (
typeMsgVote = sdk.MsgTypeURL(&govv1.MsgVote{})
)

func execDelegate(val *network.Validator, args []string) (testutil.BufferWriter, error) {
cmd := stakingcli.NewDelegateCmd(addresscodec.NewBech32Codec("cosmosvaloper"), addresscodec.NewBech32Codec("cosmos"))
clientCtx := val.ClientCtx
return clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
}

func (s *E2ETestSuite) TestExecAuthorizationWithExpiration() {
val := s.network.Validators[0]
grantee := s.grantee[0]
Expand Down Expand Up @@ -777,17 +770,14 @@ func (s *E2ETestSuite) TestExecUndelegateAuthorization() {
s.Require().NoError(s.network.WaitForNextBlock())

// delegating stakes to validator
_, err = execDelegate(
val,
[]string{
val.ValAddress.String(),
"100stake",
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
},
)
msg := &stakingtypes.MsgDelegate{
DelegatorAddress: val.Address.String(),
ValidatorAddress: val.ValAddress.String(),
Amount: sdk.NewCoin("stake", math.NewInt(100)),
}

_, err = clitestutil.SubmitTestTx(val.ClientCtx, msg, val.Address, clitestutil.TestTxConfig{})

s.Require().NoError(err)

tokens := sdk.NewCoins(
Expand Down
44 changes: 19 additions & 25 deletions tests/e2e/distribution/withdraw_all_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import (
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/distribution/client/cli"
stakingcli "github.com/cosmos/cosmos-sdk/x/staking/client/cli"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

type WithdrawAllTestSuite struct {
Expand Down Expand Up @@ -80,42 +79,37 @@ func (s *WithdrawAllTestSuite) TestNewWithdrawAllRewardsGenerateOnly() {
require.NoError(s.network.WaitForNextBlock())

// delegate 500 tokens to validator1
args := []string{
val.ValAddress.String(),
sdk.NewCoin(s.cfg.BondDenom, math.NewInt(500)).String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
msg := &stakingtypes.MsgDelegate{
DelegatorAddress: newAddr.String(),
ValidatorAddress: val.ValAddress.String(),
Amount: sdk.NewCoin("stake", math.NewInt(500)),
}
cmd := stakingcli.NewDelegateCmd(clientCtx.InterfaceRegistry.SigningContext().ValidatorAddressCodec(), clientCtx.InterfaceRegistry.SigningContext().AddressCodec())
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)

_, err = clitestutil.SubmitTestTx(val.ClientCtx, msg, newAddr, clitestutil.TestTxConfig{})
require.NoError(err)
require.NoError(s.network.WaitForNextBlock())

// delegate 500 tokens to validator2
args = []string{
val1.ValAddress.String(),
sdk.NewCoin(s.cfg.BondDenom, math.NewInt(500)).String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
msg2 := &stakingtypes.MsgDelegate{
DelegatorAddress: newAddr.String(),
ValidatorAddress: val1.ValAddress.String(),
Amount: sdk.NewCoin("stake", math.NewInt(500)),
}
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)

_, err = clitestutil.SubmitTestTx(val.ClientCtx, msg2, newAddr, clitestutil.TestTxConfig{})
require.NoError(err)
require.NoError(s.network.WaitForNextBlock())

var out testutil.BufferWriter
err = s.network.RetryForBlocks(func() error {
args = []string{
args := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=true", flags.FlagGenerateOnly),
fmt.Sprintf("--%s=1", cli.FlagMaxMessagesPerTx),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
}
cmd = cli.NewWithdrawAllRewardsCmd(address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
cmd := cli.NewWithdrawAllRewardsCmd(address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
if err != nil {
return err
Expand All @@ -130,16 +124,16 @@ func (s *WithdrawAllTestSuite) TestNewWithdrawAllRewardsGenerateOnly() {
}, 3)
require.NoError(err)

args = []string{
args := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=true", flags.FlagGenerateOnly),
fmt.Sprintf("--%s=2", cli.FlagMaxMessagesPerTx),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
}
cmd = cli.NewWithdrawAllRewardsCmd(address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
cmd := cli.NewWithdrawAllRewardsCmd(address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
require.NoError(err)
// expect 1 transaction in the generated file when --max-msgs in a tx set 2, since there are only delegations.
s.Require().Equal(1, len(strings.Split(strings.Trim(out.String(), "\n"), "\n")))
Expand Down
66 changes: 14 additions & 52 deletions tests/e2e/staking/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testutil
import (
"context"
"errors"
"fmt"
"testing"

"github.com/cometbft/cometbft/proto/tendermint/crypto"
Expand All @@ -12,15 +11,13 @@ import (

"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client/flags"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

type E2ETestSuite struct {
Expand All @@ -44,44 +41,6 @@ func (s *E2ETestSuite) SetupSuite() {
var err error
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)

unbond, err := sdk.ParseCoinNormalized("10stake")
s.Require().NoError(err)

val := s.network.Validators[0]
val2 := s.network.Validators[1]

// redelegate
out, err := MsgRedelegateExec(
val.ClientCtx,
val.Address,
val.ValAddress,
val2.ValAddress,
unbond,
fmt.Sprintf("--%s=%d", flags.FlagGas, 300000),
)
s.Require().NoError(err)
var txRes sdk.TxResponse
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txRes))
s.Require().Equal(uint32(0), txRes.Code)
s.Require().NoError(s.network.WaitForNextBlock())

unbondingAmount := sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(5))

// unbonding the amount
out, err = MsgUnbondExec(val.ClientCtx, val.Address, val.ValAddress, unbondingAmount)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txRes))
s.Require().Equal(uint32(0), txRes.Code)
s.Require().NoError(s.network.WaitForNextBlock())

// unbonding the amount
out, err = MsgUnbondExec(val.ClientCtx, val.Address, val.ValAddress, unbondingAmount)
s.Require().NoError(err)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &txRes))
s.Require().Equal(uint32(0), txRes.Code)
s.Require().NoError(s.network.WaitForNextBlock())
}

func (s *E2ETestSuite) TearDownSuite() {
Expand Down Expand Up @@ -119,16 +78,19 @@ func (s *E2ETestSuite) TestBlockResults() {
require.NoError(err)
require.NoError(s.network.WaitForNextBlock())

// Use CLI to create a delegation from the new account to validator `val`.
cmd := cli.NewDelegateCmd(addresscodec.NewBech32Codec("cosmosvaloper"), addresscodec.NewBech32Codec("cosmos"))
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, []string{
val.ValAddress.String(),
sdk.NewCoin(s.cfg.BondDenom, math.NewInt(150)).String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, math.NewInt(10))).String()),
})
msgDel := &stakingtypes.MsgDelegate{
DelegatorAddress: newAddr.String(),
ValidatorAddress: val.ValAddress.String(),
Amount: sdk.NewCoin(s.cfg.BondDenom, math.NewInt(150)),
}

// create a delegation from the new account to validator `val`.
_, err = clitestutil.SubmitTestTx(
val.ClientCtx,
msgDel,
newAddr,
clitestutil.TestTxConfig{},
)
require.NoError(err)
require.NoError(s.network.WaitForNextBlock())

Expand Down
52 changes: 0 additions & 52 deletions tests/e2e/staking/test_helpers.go

This file was deleted.

38 changes: 38 additions & 0 deletions x/staking/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,44 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
},
Tx: &autocliv1.ServiceCommandDescriptor{
Service: stakingv1beta.Msg_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Delegate",
Use: "delegate [validator-addr] [amount] --from [delegator_address]",
Short: "Delegate liquid tokens to a validator",
Long: "Delegate an amount of liquid coins to a validator from your wallet.",
Example: fmt.Sprintf("%s tx staking delegate cosmosvaloper... 1000stake --from mykey", version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "validator_address"}, {ProtoField: "amount"}},
},
{
RpcMethod: "BeginRedelegate",
Use: "redelegate [src-validator-addr] [dst-validator-addr] [amount] --from [delegator]",
Short: "Generate multisig signatures for transactions generated offline",
Long: "Redelegate an amount of illiquid staking tokens from one validator to another.",
Example: fmt.Sprintf(`%s tx staking redelegate cosmosvaloper... cosmosvaloper... 100stake --from mykey`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "validator_src_address"}, {ProtoField: "validator_dst_address"}, {ProtoField: "amount"}},
},
{
RpcMethod: "Undelegate",
Use: "unbond [validator-addr] [amount] --from [delegator_address]",
Short: "Unbond shares from a validator",
Long: "Unbond an amount of bonded shares from a validator.",
Example: fmt.Sprintf(`%s tx staking unbond cosmosvaloper... 100stake --from mykey`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "validator_address"}, {ProtoField: "amount"}},
},
{
RpcMethod: "CancelUnbondingDelegation",
Use: "cancel-unbond [validator-addr] [amount] [creation-height]",
Short: "Cancel unbonding delegation and delegate back to the validator",
Example: fmt.Sprintf(`%s tx staking cancel-unbond cosmosvaloper... 100stake 2 --from mykey`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "validator_address"}, {ProtoField: "amount"}, {ProtoField: "creation_height"}},
},
{
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
},
},
EnhanceCustomCommand: true,
},
}
}
Loading

0 comments on commit f149f1b

Please sign in to comment.