CLI/Tests: Remove Fixtures (#6799)

* remove fixtures

* setup tests

* update x/mint

* cli: update x/staking commands

* tests: convert x/staking CLI tests

* tests: fix x/auth CLI tests

* cli updates

* fix buiild

* fix build

* Update x/gov/client/cli/cli_test.go

Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>

* remove GenerateOrBroadcastTx

* move TestCLIQueryConn

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
This commit is contained in:
Alexander Bezobchuk
2020-07-21 13:54:07 +00:00
committed by GitHub
co-authored by Amaury Martiny Federico Kunze
parent 6e7ce48b31
commit 0ccc48d2a3
38 changed files with 637 additions and 2025 deletions
+31 -47
View File
@@ -7,7 +7,6 @@ import (
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
@@ -15,18 +14,19 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
codec2 "github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/tests/cli"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
cli2 "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authcli "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authtest "github.com/cosmos/cosmos-sdk/x/auth/client/testutil"
"github.com/cosmos/cosmos-sdk/x/auth/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/testutil"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
@@ -176,7 +176,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
)
s.Require().NoError(err)
normalGeneratedStdTx := cli.UnmarshalStdTx(s.T(), val1.ClientCtx.JSONMarshaler, normalGeneratedTx.String())
normalGeneratedStdTx := unmarshalStdTx(s.T(), val1.ClientCtx.JSONMarshaler, normalGeneratedTx.String())
s.Require().Equal(normalGeneratedStdTx.Fee.Gas, uint64(flags.DefaultGasLimit))
s.Require().Equal(len(normalGeneratedStdTx.Msgs), 1)
s.Require().Equal(0, len(normalGeneratedStdTx.GetSignatures()))
@@ -197,7 +197,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
)
s.Require().NoError(err)
limitedGasStdTx := cli.UnmarshalStdTx(s.T(), val1.ClientCtx.JSONMarshaler, limitedGasGeneratedTx.String())
limitedGasStdTx := unmarshalStdTx(s.T(), val1.ClientCtx.JSONMarshaler, limitedGasGeneratedTx.String())
s.Require().Equal(limitedGasStdTx.Fee.Gas, uint64(100))
s.Require().Equal(len(limitedGasStdTx.Msgs), 1)
s.Require().Equal(0, len(limitedGasStdTx.GetSignatures()))
@@ -218,7 +218,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
)
s.Require().NoError(err)
finalStdTx := cli.UnmarshalStdTx(s.T(), val1.ClientCtx.JSONMarshaler, finalGeneratedTx.String())
finalStdTx := unmarshalStdTx(s.T(), val1.ClientCtx.JSONMarshaler, finalGeneratedTx.String())
s.Require().Equal(uint64(flags.DefaultGasLimit), finalStdTx.Fee.Gas)
s.Require().Equal(len(finalStdTx.Msgs), 1)
@@ -246,7 +246,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
signedTx, err := authtest.TxSignExec(val1.ClientCtx, val1.Address, unsignedTxFile.Name())
s.Require().NoError(err)
signedFinalTx := cli.UnmarshalStdTx(s.T(), val1.ClientCtx.JSONMarshaler, signedTx.String())
signedFinalTx := unmarshalStdTx(s.T(), val1.ClientCtx.JSONMarshaler, signedTx.String())
s.Require().Equal(len(signedFinalTx.Msgs), 1)
s.Require().Equal(1, len(signedFinalTx.GetSignatures()))
s.Require().Equal(val1.Address.String(), signedFinalTx.GetSigners()[0].String())
@@ -268,7 +268,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
var coins sdk.Coins
err = val1.ClientCtx.JSONMarshaler.UnmarshalJSON(resp.Bytes(), &coins)
s.Require().NoError(err)
s.Require().Equal(startTokens, coins.AmountOf(cli.Denom))
s.Require().Equal(startTokens, coins.AmountOf(s.cfg.BondDenom))
// Test broadcast
@@ -276,16 +276,14 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
res, err = authtest.TxBroadcastExec(val1.ClientCtx, signedTxFile.Name(), "--offline")
s.Require().EqualError(err, "cannot broadcast tx during offline mode")
err = waitForNextBlock(s.network)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
// Broadcast correct transaction.
val1.ClientCtx.BroadcastMode = flags.BroadcastBlock
res, err = authtest.TxBroadcastExec(val1.ClientCtx, signedTxFile.Name())
s.Require().NoError(err)
err = waitForNextBlock(s.network)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
// Ensure destiny account state
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, account.GetAddress())
@@ -293,7 +291,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
err = val1.ClientCtx.JSONMarshaler.UnmarshalJSON(resp.Bytes(), &coins)
s.Require().NoError(err)
s.Require().Equal(sendTokens, coins.AmountOf(cli.Denom))
s.Require().Equal(sendTokens, coins.AmountOf(s.cfg.BondDenom))
// Ensure origin account state
resp, err = bankcli.QueryBalancesExec(val1.ClientCtx, val1.Address)
@@ -301,7 +299,7 @@ func (s *IntegrationTestSuite) TestCLISendGenerateSignAndBroadcast() {
err = val1.ClientCtx.JSONMarshaler.UnmarshalJSON(resp.Bytes(), &coins)
s.Require().NoError(err)
s.Require().Equal(sdk.NewInt(389999990), coins.AmountOf(cli.Denom))
s.Require().Equal(sdk.NewInt(389999990), coins.AmountOf(s.cfg.BondDenom))
}
func (s *IntegrationTestSuite) TestCLIMultisignInsufficientCosigners() {
@@ -330,7 +328,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignInsufficientCosigners() {
val1.Address,
multisigInfo.GetAddress(),
sdk.NewCoins(
sdk.NewInt64Coin(cli.Denom, 10),
sdk.NewInt64Coin(s.cfg.BondDenom, 10),
),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
@@ -339,8 +337,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignInsufficientCosigners() {
)
s.Require().NoError(err)
err = waitForNextBlock(s.network)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
// Generate multisig transaction.
multiGeneratedTx, err := bankcli.MsgSendExec(
@@ -348,7 +345,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignInsufficientCosigners() {
multisigInfo.GetAddress(),
val1.Address,
sdk.NewCoins(
sdk.NewInt64Coin(cli.Denom, 5),
sdk.NewInt64Coin(s.cfg.BondDenom, 5),
),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
@@ -415,7 +412,7 @@ func (s *IntegrationTestSuite) TestCLIEncode() {
decodedTx, err := authtest.TxDecodeExec(val1.ClientCtx, trimmedBase64)
s.Require().NoError(err)
theTx := cli.UnmarshalStdTx(s.T(), val1.ClientCtx.JSONMarshaler, decodedTx.String())
theTx := unmarshalStdTx(s.T(), val1.ClientCtx.JSONMarshaler, decodedTx.String())
s.Require().Equal("deadbeef", theTx.Memo)
}
@@ -440,7 +437,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignSortSignatures() {
s.Require().NoError(err)
// Send coins from validator to multisig.
sendTokens := sdk.NewInt64Coin(cli.Denom, 10)
sendTokens := sdk.NewInt64Coin(s.cfg.BondDenom, 10)
_, err = bankcli.MsgSendExec(
val1.ClientCtx,
val1.Address,
@@ -455,8 +452,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignSortSignatures() {
)
s.Require().NoError(err)
err = waitForNextBlock(s.network)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
resp, err := bankcli.QueryBalancesExec(val1.ClientCtx, multisigInfo.GetAddress())
s.Require().NoError(err)
@@ -464,7 +460,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignSortSignatures() {
var coins sdk.Coins
err = val1.ClientCtx.JSONMarshaler.UnmarshalJSON(resp.Bytes(), &coins)
s.Require().NoError(err)
s.Require().Equal(sendTokens.Amount, coins.AmountOf(cli.Denom))
s.Require().Equal(sendTokens.Amount, coins.AmountOf(s.cfg.BondDenom))
// Generate multisig transaction.
multiGeneratedTx, err := bankcli.MsgSendExec(
@@ -472,7 +468,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignSortSignatures() {
multisigInfo.GetAddress(),
val1.Address,
sdk.NewCoins(
sdk.NewInt64Coin(cli.Denom, 5),
sdk.NewInt64Coin(s.cfg.BondDenom, 5),
),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
@@ -514,8 +510,7 @@ func (s *IntegrationTestSuite) TestCLIMultisignSortSignatures() {
_, err = authtest.TxBroadcastExec(val1.ClientCtx, signedTxFile.Name())
s.Require().NoError(err)
err = waitForNextBlock(s.network)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
}
func (s *IntegrationTestSuite) TestCLIMultisign() {
@@ -539,7 +534,7 @@ func (s *IntegrationTestSuite) TestCLIMultisign() {
s.Require().NoError(err)
// Send coins from validator to multisig.
sendTokens := sdk.NewInt64Coin(cli.Denom, 10)
sendTokens := sdk.NewInt64Coin(s.cfg.BondDenom, 10)
_, err = bankcli.MsgSendExec(
val1.ClientCtx,
val1.Address,
@@ -553,8 +548,7 @@ func (s *IntegrationTestSuite) TestCLIMultisign() {
fmt.Sprintf("--gas=%d", flags.DefaultGasLimit),
)
err = waitForNextBlock(s.network)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
resp, err := bankcli.QueryBalancesExec(val1.ClientCtx, multisigInfo.GetAddress())
s.Require().NoError(err)
@@ -562,7 +556,7 @@ func (s *IntegrationTestSuite) TestCLIMultisign() {
var coins sdk.Coins
err = val1.ClientCtx.JSONMarshaler.UnmarshalJSON(resp.Bytes(), &coins)
s.Require().NoError(err)
s.Require().Equal(sendTokens.Amount, coins.AmountOf(cli.Denom))
s.Require().Equal(sendTokens.Amount, coins.AmountOf(s.cfg.BondDenom))
// Generate multisig transaction.
multiGeneratedTx, err := bankcli.MsgSendExec(
@@ -570,7 +564,7 @@ func (s *IntegrationTestSuite) TestCLIMultisign() {
multisigInfo.GetAddress(),
val1.Address,
sdk.NewCoins(
sdk.NewInt64Coin(cli.Denom, 5),
sdk.NewInt64Coin(s.cfg.BondDenom, 5),
),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
@@ -618,15 +612,14 @@ func (s *IntegrationTestSuite) TestCLIMultisign() {
_, err = authtest.TxBroadcastExec(val1.ClientCtx, signedTxFile.Name())
s.Require().NoError(err)
err = waitForNextBlock(s.network)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
}
func TestGetBroadcastCommand_OfflineFlag(t *testing.T) {
clientCtx := client.Context{}.WithOffline(true)
clientCtx = clientCtx.WithTxConfig(simappparams.MakeEncodingConfig().TxConfig)
cmd := cli2.GetBroadcastCommand()
cmd := authcli.GetBroadcastCommand()
_ = testutil.ApplyMockIODiscardOutErr(cmd)
cmd.SetArgs([]string{fmt.Sprintf("--%s=true", flags.FlagOffline), ""})
@@ -640,7 +633,7 @@ func TestGetBroadcastCommand_WithoutOfflineFlag(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
cmd := cli2.GetBroadcastCommand()
cmd := authcli.GetBroadcastCommand()
testDir, cleanFunc := testutil.NewTestCaseDir(t)
t.Cleanup(cleanFunc)
@@ -662,16 +655,7 @@ func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
func waitForNextBlock(network *network.Network) error {
lastBlock, err := network.LatestHeight()
if err != nil {
return err
}
_, err = network.WaitForHeightWithTimeout(lastBlock+1, 10*time.Second)
if err != nil {
return err
}
return err
func unmarshalStdTx(t require.TestingT, c codec.JSONMarshaler, s string) (stdTx authtypes.StdTx) {
require.Nil(t, c.UnmarshalJSON([]byte(s), &stdTx))
return stdTx
}
-31
View File
@@ -7,13 +7,7 @@ import (
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/client"
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
)
@@ -30,28 +24,3 @@ func QueryBalancesExec(clientCtx client.Context, address fmt.Stringer, extraArgs
return clitestutil.ExecTestCLICmd(clientCtx, bankcli.GetBalancesCmd(), args)
}
// ----------------------------------------------------------------------------
// TODO: REMOVE ALL FUNCTIONS BELOW.
//
// REF: https://github.com/cosmos/cosmos-sdk/issues/6571
// ----------------------------------------------------------------------------
// TxSend is simcli tx send
func TxSend(f *cli.Fixtures, from string, to sdk.AccAddress, amount sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx send --keyring-backend=test %s %s %s %v", f.SimdBinary, from, to, amount, f.Flags())
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// QueryBalances executes the bank query balances command for a given address and
// flag set.
func QueryBalances(f *cli.Fixtures, address sdk.AccAddress, flags ...string) sdk.Coins {
cmd := fmt.Sprintf("%s query bank balances %s %v", f.SimdBinary, address, f.Flags())
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
var balances sdk.Coins
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &balances), "out %v\n", out)
return balances
}
+5
View File
@@ -123,6 +123,7 @@ $ %s tx distribution withdraw-rewards cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fx
cmd.Flags().Bool(FlagCommission, false, "Withdraw the validator's commission in addition to the rewards")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
@@ -167,6 +168,7 @@ $ %s tx distribution withdraw-all-rewards --from mykey
cmd.Flags().Int(FlagMaxMessagesPerTx, MaxMessagesPerTxDefault, "Limit the number of messages per tx (0 for unlimited)")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
@@ -207,6 +209,7 @@ $ %s tx distribution set-withdraw-addr cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
@@ -247,6 +250,7 @@ $ %s tx distribution fund-community-pool 100uatom --from mykey
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
@@ -315,5 +319,6 @@ Where proposal.json contains:
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
+151 -147
View File
@@ -1,160 +1,164 @@
// +build cli_test
package cli_test
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests/cli"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
banktestutils "github.com/cosmos/cosmos-sdk/x/bank/client/testutil"
"github.com/cosmos/cosmos-sdk/x/gov/client/testutil"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
func TestCLISubmitProposal(t *testing.T) {
t.SkipNow() // TODO: Bring back once viper is refactored.
t.Parallel()
f := cli.InitFixtures(t)
type IntegrationTestSuite struct {
suite.Suite
// start simd server
proc := f.SDStart()
t.Cleanup(func() { proc.Stop(false) })
testutil.QueryGovParamDeposit(f)
testutil.QueryGovParamVoting(f)
testutil.QueryGovParamTallying(f)
fooAddr := f.KeyAddress(cli.KeyFoo)
startTokens := sdk.TokensFromConsensusPower(50)
require.Equal(t, startTokens, banktestutils.QueryBalances(f, fooAddr).AmountOf(sdk.DefaultBondDenom))
proposalsQuery := testutil.QueryGovProposals(f)
require.Empty(t, proposalsQuery)
// Test submit generate only for submit proposal
proposalTokens := sdk.TokensFromConsensusPower(5)
success, stdout, stderr := testutil.TxGovSubmitProposal(f,
fooAddr.String(), "Text", "Test", "test", sdk.NewCoin(cli.Denom, proposalTokens), "--generate-only", "-y")
require.True(t, success)
require.Empty(t, stderr)
msg := cli.UnmarshalStdTx(t, f.Cdc, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Equal(t, len(msg.Msgs), 1)
require.Equal(t, 0, len(msg.GetSignatures()))
// Test --dry-run
success, _, _ = testutil.TxGovSubmitProposal(f, cli.KeyFoo, "Text", "Test", "test", sdk.NewCoin(cli.Denom, proposalTokens), "--dry-run")
require.True(t, success)
// Create the proposal
testutil.TxGovSubmitProposal(f, cli.KeyFoo, "Text", "Test", "test", sdk.NewCoin(cli.Denom, proposalTokens), "-y")
tests.WaitForNextNBlocksTM(1, f.Port)
// Ensure transaction events can be queried
searchResult := f.QueryTxs(1, 50, "message.action=submit_proposal", fmt.Sprintf("message.sender=%s", fooAddr))
require.Len(t, searchResult.Txs, 1)
// Ensure deposit was deducted
require.Equal(t, startTokens.Sub(proposalTokens), banktestutils.QueryBalances(f, fooAddr).AmountOf(cli.Denom))
// Ensure propsal is directly queryable
proposal1 := testutil.QueryGovProposal(f, 1)
require.Equal(t, uint64(1), proposal1.ProposalID)
require.Equal(t, types.StatusDepositPeriod, proposal1.Status)
// Ensure query proposals returns properly
proposalsQuery = testutil.QueryGovProposals(f)
require.Equal(t, uint64(1), proposalsQuery[0].ProposalID)
// Query the deposits on the proposal
deposit := testutil.QueryGovDeposit(f, 1, fooAddr)
require.Equal(t, proposalTokens, deposit.Amount.AmountOf(cli.Denom))
// Test deposit generate only
depositTokens := sdk.TokensFromConsensusPower(10)
success, stdout, stderr = testutil.TxGovDeposit(f, 1, fooAddr.String(), sdk.NewCoin(cli.Denom, depositTokens), "--generate-only")
require.True(t, success)
require.Empty(t, stderr)
msg = cli.UnmarshalStdTx(t, f.Cdc, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Equal(t, len(msg.Msgs), 1)
require.Equal(t, 0, len(msg.GetSignatures()))
// Run the deposit transaction
testutil.TxGovDeposit(f, 1, cli.KeyFoo, sdk.NewCoin(cli.Denom, depositTokens), "-y")
tests.WaitForNextNBlocksTM(1, f.Port)
// test query deposit
deposits := testutil.QueryGovDeposits(f, 1)
require.Len(t, deposits, 1)
require.Equal(t, proposalTokens.Add(depositTokens), deposits[0].Amount.AmountOf(cli.Denom))
// Ensure querying the deposit returns the proper amount
deposit = testutil.QueryGovDeposit(f, 1, fooAddr)
require.Equal(t, proposalTokens.Add(depositTokens), deposit.Amount.AmountOf(cli.Denom))
// Ensure events are set on the transaction
searchResult = f.QueryTxs(1, 50, "message.action=deposit", fmt.Sprintf("message.sender=%s", fooAddr))
require.Len(t, searchResult.Txs, 1)
// Ensure account has expected amount of funds
require.Equal(t, startTokens.Sub(proposalTokens.Add(depositTokens)), banktestutils.QueryBalances(f, fooAddr).AmountOf(cli.Denom))
// Fetch the proposal and ensure it is now in the voting period
proposal1 = testutil.QueryGovProposal(f, 1)
require.Equal(t, uint64(1), proposal1.ProposalID)
require.Equal(t, types.StatusVotingPeriod, proposal1.Status)
// Test vote generate only
success, stdout, stderr = testutil.TxGovVote(f, 1, types.OptionYes, fooAddr.String(), "--generate-only")
require.True(t, success)
require.Empty(t, stderr)
msg = cli.UnmarshalStdTx(t, f.Cdc, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Equal(t, len(msg.Msgs), 1)
require.Equal(t, 0, len(msg.GetSignatures()))
// Vote on the proposal
testutil.TxGovVote(f, 1, types.OptionYes, cli.KeyFoo, "-y")
tests.WaitForNextNBlocksTM(1, f.Port)
// Query the vote
vote := testutil.QueryGovVote(f, 1, fooAddr)
require.Equal(t, uint64(1), vote.ProposalID)
require.Equal(t, types.OptionYes, vote.Option)
// Query the votes
votes := testutil.QueryGovVotes(f, 1)
require.Len(t, votes, 1)
require.Equal(t, uint64(1), votes[0].ProposalID)
require.Equal(t, types.OptionYes, votes[0].Option)
// Ensure events are applied to voting transaction properly
searchResult = f.QueryTxs(1, 50, "message.action=vote", fmt.Sprintf("message.sender=%s", fooAddr))
require.Len(t, searchResult.Txs, 1)
// Ensure no proposals in deposit period
proposalsQuery = testutil.QueryGovProposals(f, "--status=DepositPeriod")
require.Empty(t, proposalsQuery)
// Ensure the proposal returns as in the voting period
proposalsQuery = testutil.QueryGovProposals(f, "--status=VotingPeriod")
require.Equal(t, uint64(1), proposalsQuery[0].ProposalID)
// submit a second test proposal
testutil.TxGovSubmitProposal(f, cli.KeyFoo, "Text", "Apples", "test", sdk.NewCoin(cli.Denom, proposalTokens), "-y")
tests.WaitForNextNBlocksTM(1, f.Port)
// Test limit on proposals query
proposalsQuery = testutil.QueryGovProposals(f, "--limit=2")
require.Len(t, proposalsQuery, 2)
require.Equal(t, uint64(1), proposalsQuery[0].ProposalID)
f.Cleanup()
cfg network.Config
network *network.Network
}
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
cfg := network.DefaultConfig()
cfg.NumValidators = 1
s.cfg = cfg
s.network = network.New(s.T(), cfg)
_, err := s.network.WaitForHeight(1)
s.Require().NoError(err)
}
func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}
func (s *IntegrationTestSuite) TestNewCmdSubmitProposal() {
val := s.network.Validators[0]
invalidPropFile, err := ioutil.TempFile(os.TempDir(), "invalid_text_proposal.*.json")
s.Require().NoError(err)
defer os.Remove(invalidPropFile.Name())
invalidProp := `{
"title": "",
"description": "Where is the title!?",
"type": "Text",
"deposit": "-324foocoin"
}`
_, err = invalidPropFile.WriteString(invalidProp)
s.Require().NoError(err)
validPropFile, err := ioutil.TempFile(os.TempDir(), "valid_text_proposal.*.json")
s.Require().NoError(err)
defer os.Remove(validPropFile.Name())
validProp := fmt.Sprintf(`{
"title": "Text Proposal",
"description": "Hello, World!",
"type": "Text",
"deposit": "%s"
}`, sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(5431)))
_, err = validPropFile.WriteString(validProp)
s.Require().NoError(err)
testCases := []struct {
name string
args []string
expectErr bool
respType fmt.Stringer
expectedCode uint32
}{
{
"invalid proposal (file)",
[]string{
fmt.Sprintf("--%s=%s", cli.FlagProposal, invalidPropFile.Name()),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
true, nil, 0,
},
{
"invalid proposal",
[]string{
fmt.Sprintf("--%s='Where is the title!?'", cli.FlagDescription),
fmt.Sprintf("--%s=%s", cli.FlagProposalType, types.ProposalTypeText),
fmt.Sprintf("--%s=%s", cli.FlagDeposit, sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(5431)).String()),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
true, nil, 0,
},
{
"valid transaction (file)",
[]string{
fmt.Sprintf("--%s=%s", cli.FlagProposal, validPropFile.Name()),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
false, &sdk.TxResponse{}, 0,
},
{
"valid transaction",
[]string{
fmt.Sprintf("--%s='Text Proposal'", cli.FlagTitle),
fmt.Sprintf("--%s='Where is the title!?'", cli.FlagDescription),
fmt.Sprintf("--%s=%s", cli.FlagProposalType, types.ProposalTypeText),
fmt.Sprintf("--%s=%s", cli.FlagDeposit, sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(5431)).String()),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
false, &sdk.TxResponse{}, 0,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.NewCmdSubmitProposal()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
out.Reset()
cmd.SetArgs(tc.args)
s.Require().NoError(s.network.WaitForNextBlock())
err := cmd.ExecuteContext(ctx)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
txResp := tc.respType.(*sdk.TxResponse)
s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
}
})
}
}
func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
+1 -1
View File
@@ -15,7 +15,7 @@ func parseSubmitProposalFlags(fs *pflag.FlagSet) (*proposal, error) {
proposalFile, _ := fs.GetString(FlagProposal)
if proposalFile == "" {
proposalType, _ := fs.GetString(flagProposalType)
proposalType, _ := fs.GetString(FlagProposalType)
proposal.Title, _ = fs.GetString(FlagTitle)
proposal.Description, _ = fs.GetString(FlagDescription)
+1 -1
View File
@@ -55,7 +55,7 @@ func TestParseSubmitProposalFlags(t *testing.T) {
fs.Set(FlagProposal, "")
fs.Set(FlagTitle, proposal1.Title)
fs.Set(FlagDescription, proposal1.Description)
fs.Set(flagProposalType, proposal1.Type)
fs.Set(FlagProposalType, proposal1.Type)
fs.Set(FlagDeposit, proposal1.Deposit)
proposal2, err := parseSubmitProposalFlags(fs)
+14 -14
View File
@@ -20,7 +20,7 @@ import (
const (
FlagTitle = "title"
FlagDescription = "description"
flagProposalType = "type"
FlagProposalType = "type"
FlagDeposit = "deposit"
flagVoter = "voter"
flagDepositor = "depositor"
@@ -41,7 +41,7 @@ type proposal struct {
var ProposalFlags = []string{
FlagTitle,
FlagDescription,
flagProposalType,
FlagProposalType,
FlagDeposit,
}
@@ -110,7 +110,7 @@ $ %s tx gov submit-proposal --title="Test Proposal" --description="My awesome pr
proposal, err := parseSubmitProposalFlags(cmd.Flags())
if err != nil {
return err
return fmt.Errorf("failed to parse proposal: %w", err)
}
amount, err := sdk.ParseCoins(proposal.Deposit)
@@ -120,24 +120,24 @@ $ %s tx gov submit-proposal --title="Test Proposal" --description="My awesome pr
content := types.ContentFromProposalType(proposal.Title, proposal.Description, proposal.Type)
msg, err := types.NewMsgSubmitProposal(content, amount, clientCtx.FromAddress)
msg, err := types.NewMsgSubmitProposal(content, amount, clientCtx.GetFromAddress())
if err != nil {
return err
return fmt.Errorf("invalid message: %w", err)
}
if err = msg.ValidateBasic(); err != nil {
return err
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTx(clientCtx, msg)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(FlagTitle, "", "title of proposal")
cmd.Flags().String(FlagDescription, "", "description of proposal")
cmd.Flags().String(flagProposalType, "", "proposalType of proposal, types: text/parameter_change/software_upgrade")
cmd.Flags().String(FlagDeposit, "", "deposit of proposal")
cmd.Flags().String(FlagProposal, "", "proposal file path (if this path is given, other proposal flags are ignored)")
cmd.Flags().String(FlagTitle, "", "The proposal title")
cmd.Flags().String(FlagDescription, "", "The proposal description")
cmd.Flags().String(FlagProposalType, "", "The proposal Type")
cmd.Flags().String(FlagDeposit, "", "The proposal deposit")
cmd.Flags().String(FlagProposal, "", "Proposal file path (if this path is given, other proposal flags are ignored)")
flags.AddTxFlagsToCmd(cmd)
return cmd
@@ -187,7 +187,7 @@ $ %s tx gov deposit 1 10stake --from mykey
return err
}
return tx.GenerateOrBroadcastTx(clientCtx, msg)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
@@ -242,7 +242,7 @@ $ %s tx gov vote 1 yes --from mykey
return err
}
return tx.GenerateOrBroadcastTx(clientCtx, msg)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
-173
View File
@@ -1,173 +0,0 @@
package testutil
import (
"fmt"
"strings"
"github.com/stretchr/testify/require"
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
//___________________________________________________________________________________
// simcli query gov
// QueryGovParamDeposit is simcli query gov param deposit
func QueryGovParamDeposit(f *cli.Fixtures) types.DepositParams {
cmd := fmt.Sprintf("%s query gov param deposit %s", f.SimdBinary, f.Flags())
out, _ := tests.ExecuteT(f.T, cmd, "")
var depositParam types.DepositParams
err := f.Cdc.UnmarshalJSON([]byte(out), &depositParam)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return depositParam
}
// QueryGovParamVoting is simcli query gov param voting
func QueryGovParamVoting(f *cli.Fixtures) types.VotingParams {
cmd := fmt.Sprintf("%s query gov param voting %s", f.SimdBinary, f.Flags())
out, _ := tests.ExecuteT(f.T, cmd, "")
var votingParam types.VotingParams
err := f.Cdc.UnmarshalJSON([]byte(out), &votingParam)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return votingParam
}
// QueryGovParamTallying is simcli query gov param tallying
func QueryGovParamTallying(f *cli.Fixtures) types.TallyParams {
cmd := fmt.Sprintf("%s query gov param tallying %s", f.SimdBinary, f.Flags())
out, _ := tests.ExecuteT(f.T, cmd, "")
var tallyingParam types.TallyParams
err := f.Cdc.UnmarshalJSON([]byte(out), &tallyingParam)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return tallyingParam
}
// QueryGovProposal is simcli query gov proposal
func QueryGovProposal(f *cli.Fixtures, proposalID int, flags ...string) types.Proposal {
cmd := fmt.Sprintf("%s query gov proposal %d %v", f.SimdBinary, proposalID, f.Flags())
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
var proposal types.Proposal
err := f.Cdc.UnmarshalJSON([]byte(out), &proposal)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return proposal
}
// QueryGovProposals is simcli query gov proposals
func QueryGovProposals(f *cli.Fixtures, flags ...string) types.Proposals {
cmd := fmt.Sprintf("%s query gov proposals %v", f.SimdBinary, f.Flags())
stdout, stderr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
if strings.Contains(stderr, "no matching proposals found") {
return types.Proposals{}
}
require.Empty(f.T, stderr)
var out types.Proposals
err := f.Cdc.UnmarshalJSON([]byte(stdout), &out)
require.NoError(f.T, err)
return out
}
// QueryGovVote is simcli query gov vote
func QueryGovVote(f *cli.Fixtures, proposalID int, voter sdk.AccAddress, flags ...string) types.Vote {
cmd := fmt.Sprintf("%s query gov vote %d %s %v", f.SimdBinary, proposalID, voter, f.Flags())
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
var vote types.Vote
err := f.Cdc.UnmarshalJSON([]byte(out), &vote)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return vote
}
// QueryGovVotes is simcli query gov votes
func QueryGovVotes(f *cli.Fixtures, proposalID int, flags ...string) []types.Vote {
cmd := fmt.Sprintf("%s query gov votes %d %v", f.SimdBinary, proposalID, f.Flags())
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
var votes []types.Vote
err := f.Cdc.UnmarshalJSON([]byte(out), &votes)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return votes
}
// QueryGovDeposit is simcli query gov deposit
func QueryGovDeposit(f *cli.Fixtures, proposalID int, depositor sdk.AccAddress, flags ...string) types.Deposit {
cmd := fmt.Sprintf("%s query gov deposit %d %s %v", f.SimdBinary, proposalID, depositor, f.Flags())
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
var deposit types.Deposit
err := f.Cdc.UnmarshalJSON([]byte(out), &deposit)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return deposit
}
// QueryGovDeposits is simcli query gov deposits
func QueryGovDeposits(f *cli.Fixtures, propsalID int, flags ...string) []types.Deposit {
cmd := fmt.Sprintf("%s query gov deposits %d %v", f.SimdBinary, propsalID, f.Flags())
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
var deposits []types.Deposit
err := f.Cdc.UnmarshalJSON([]byte(out), &deposits)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return deposits
}
//___________________________________________________________________________________
// simcli tx gov
// TxGovSubmitProposal is simcli tx gov submit-proposal
func TxGovSubmitProposal(f *cli.Fixtures, from, typ, title, description string, deposit sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx gov submit-proposal %v --keyring-backend=test --from=%s --type=%s",
f.SimdBinary, f.Flags(), from, typ)
cmd += fmt.Sprintf(" --title=%s --description=%s --deposit=%s", title, description, deposit)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovDeposit is simcli tx gov deposit
func TxGovDeposit(f *cli.Fixtures, proposalID int, from string, amount sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx gov deposit %d %s --keyring-backend=test --from=%s %v",
f.SimdBinary, proposalID, amount, from, f.Flags())
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovVote is simcli tx gov vote
func TxGovVote(f *cli.Fixtures, proposalID int, option types.VoteOption, from string, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx gov vote %d %s --keyring-backend=test --from=%s %v",
f.SimdBinary, proposalID, option, from, f.Flags())
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovSubmitParamChangeProposal executes a CLI parameter change proposal
// submission.
func TxGovSubmitParamChangeProposal(f *cli.Fixtures,
from, proposalPath string, deposit sdk.Coin, flags ...string,
) (bool, string, string) {
cmd := fmt.Sprintf(
"%s tx gov submit-proposal param-change %s --keyring-backend=test --from=%s %v",
f.SimdBinary, proposalPath, from, f.Flags(),
)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovSubmitCommunityPoolSpendProposal executes a CLI community pool spend proposal
// submission.
func TxGovSubmitCommunityPoolSpendProposal(f *cli.Fixtures,
from, proposalPath string, deposit sdk.Coin, flags ...string,
) (bool, string, string) {
cmd := fmt.Sprintf(
"%s tx gov submit-proposal community-pool-spend %s --keyring-backend=test --from=%s %v",
f.SimdBinary, proposalPath, from, f.Flags(),
)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
+177 -17
View File
@@ -1,30 +1,190 @@
// +build cli_test
package cli_test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/cosmos/cosmos-sdk/tests/cli"
"github.com/cosmos/cosmos-sdk/x/mint/client/testutil"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil"
testnet "github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/mint/client/cli"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)
func TestCLIMintQueries(t *testing.T) {
t.SkipNow() // TODO: Bring back once viper is refactored.
t.Parallel()
f := cli.InitFixtures(t)
type IntegrationTestSuite struct {
suite.Suite
proc := f.SDStart()
t.Cleanup(func() { proc.Stop(false) })
cfg testnet.Config
network *testnet.Network
}
params := testutil.QueryMintingParams(f)
require.NotEmpty(t, params)
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
inflation := testutil.QueryInflation(f)
require.False(t, inflation.IsZero())
cfg := testnet.DefaultConfig()
genesisState := cfg.GenesisState
cfg.NumValidators = 1
annualProvisions := testutil.QueryAnnualProvisions(f)
require.False(t, annualProvisions.IsZero())
var mintData minttypes.GenesisState
s.Require().NoError(cfg.Codec.UnmarshalJSON(genesisState[minttypes.ModuleName], &mintData))
inflation := sdk.MustNewDecFromStr("1.0")
mintData.Minter.Inflation = inflation
mintData.Params.InflationMin = inflation
mintData.Params.InflationMax = inflation
mintDataBz, err := cfg.Codec.MarshalJSON(mintData)
s.Require().NoError(err)
genesisState[minttypes.ModuleName] = mintDataBz
cfg.GenesisState = genesisState
s.cfg = cfg
s.network = testnet.New(s.T(), cfg)
_, err = s.network.WaitForHeight(1)
s.Require().NoError(err)
}
func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}
func (s *IntegrationTestSuite) TestGetCmdQueryParams() {
val := s.network.Validators[0]
testCases := []struct {
name string
args []string
expectedOutput string
}{
{
"default output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight)},
`{"mint_denom":"stake","inflation_rate_change":"0.130000000000000000","inflation_max":"1.000000000000000000","inflation_min":"1.000000000000000000","goal_bonded":"0.670000000000000000","blocks_per_year":"6311520"}`,
},
{
"text output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
`blocks_per_year: "6311520"
goal_bonded: "0.670000000000000000"
inflation_max: "1.000000000000000000"
inflation_min: "1.000000000000000000"
inflation_rate_change: "0.130000000000000000"
mint_denom: stake`,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryParams()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
out.Reset()
cmd.SetArgs(tc.args)
s.Require().NoError(cmd.ExecuteContext(ctx))
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
})
}
}
func (s *IntegrationTestSuite) TestGetCmdQueryInflation() {
val := s.network.Validators[0]
testCases := []struct {
name string
args []string
expectedOutput string
}{
{
"default output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight)},
`"1.000000000000000000"`,
},
{
"text output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
`"1.000000000000000000"`,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryInflation()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
out.Reset()
cmd.SetArgs(tc.args)
s.Require().NoError(cmd.ExecuteContext(ctx))
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
})
}
}
func (s *IntegrationTestSuite) TestGetCmdQueryAnnualProvisions() {
val := s.network.Validators[0]
testCases := []struct {
name string
args []string
expectedOutput string
}{
{
"default output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight)},
`"500000000.000000000000000000"`,
},
{
"text output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
`"500000000.000000000000000000"`,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryAnnualProvisions()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
out.Reset()
cmd.SetArgs(tc.args)
s.Require().NoError(cmd.ExecuteContext(ctx))
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
})
}
}
func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
+1 -1
View File
@@ -52,7 +52,7 @@ func GetCmdQueryParams() *cobra.Command {
return err
}
return clientCtx.PrintOutput(res.Params)
return clientCtx.PrintOutput(res.GetParams())
},
}
-45
View File
@@ -1,45 +0,0 @@
package testutil
import (
"fmt"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)
// QueryMintingParams returns the current minting parameters
func QueryMintingParams(f *cli.Fixtures, flags ...string) types.Params {
cmd := fmt.Sprintf("%s query mint params %v", f.SimdBinary, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var params types.Params
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &params))
return params
}
// QueryInflation returns the current minting inflation value
func QueryInflation(f *cli.Fixtures, flags ...string) sdk.Dec {
cmd := fmt.Sprintf("%s query mint inflation %v", f.SimdBinary, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var inflation sdk.Dec
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &inflation))
return inflation
}
// QueryAnnualProvisions returns the current minting annual provisions value
func QueryAnnualProvisions(f *cli.Fixtures, flags ...string) sdk.Dec {
cmd := fmt.Sprintf("%s query mint annual-provisions %v", f.SimdBinary, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var annualProvisions sdk.Dec
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &annualProvisions))
return annualProvisions
}
+5 -4
View File
@@ -6,7 +6,6 @@ import (
"math/rand"
"github.com/gogo/protobuf/grpc"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
@@ -113,9 +112,11 @@ func (am AppModule) NewQuerierHandler() sdk.Querier {
return keeper.NewQuerier(am.keeper)
}
// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(grpc.Server) {}
// RegisterQueryService registers a gRPC query service to respond to the
// module-specific gRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
}
// InitGenesis performs genesis initialization for the mint module. It returns
// no validator updates.
+2 -2
View File
@@ -102,8 +102,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier {
return keeper.NewQuerier(am.keeper)
}
// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
// RegisterQueryService registers a gRPC query service to respond to the
// module-specific gRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
proposal.RegisterQueryServer(server, am.keeper)
}
-35
View File
@@ -1,35 +0,0 @@
package testutil
import (
"fmt"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests/cli"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)
// QuerySigningInfo returns the signing info for a validator
func QuerySigningInfo(f *cli.Fixtures, val string) types.ValidatorSigningInfo {
cmd := fmt.Sprintf("%s query slashing signing-info %s %s", f.SimdBinary, val, f.Flags())
res, errStr := tests.ExecuteT(f.T, cmd, "")
require.Empty(f.T, errStr)
var sinfo types.ValidatorSigningInfo
err := f.Cdc.UnmarshalJSON([]byte(res), &sinfo)
require.NoError(f.T, err)
return sinfo
}
// QuerySlashingParams returns query slashing params
func QuerySlashingParams(f *cli.Fixtures) types.Params {
cmd := fmt.Sprintf("%s query slashing params %s", f.SimdBinary, f.Flags())
res, errStr := tests.ExecuteT(f.T, cmd, "")
require.Empty(f.T, errStr)
var params types.Params
err := f.Cdc.UnmarshalJSON([]byte(res), &params)
require.NoError(f.T, err)
return params
}
+175 -183
View File
@@ -1,195 +1,187 @@
// +build cli_test
package cli_test
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests/cli"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
bankclienttestutil "github.com/cosmos/cosmos-sdk/x/bank/client/testutil"
"github.com/cosmos/cosmos-sdk/x/staking/client/testutil"
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/client/testutil"
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
)
func TestCLICreateValidator(t *testing.T) {
t.SkipNow() // Recreate when using CLI tests.
type IntegrationTestSuite struct {
suite.Suite
t.Parallel()
f := cli.InitFixtures(t)
// start simd server
proc := f.SDStart()
t.Cleanup(func() { proc.Stop(false) })
barAddr := f.KeyAddress(cli.KeyBar)
barVal := sdk.ValAddress(barAddr)
// Check for the params
params := testutil.QueryStakingParameters(f)
require.NotEmpty(t, params)
// Query for the staking pool
pool := testutil.QueryStakingPool(f)
require.NotEmpty(t, pool)
consPubKey := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, ed25519.GenPrivKey().PubKey())
sendTokens := sdk.TokensFromConsensusPower(10)
bankclienttestutil.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "-y")
tests.WaitForNextNBlocksTM(1, f.Port)
require.Equal(t, sendTokens.String(), bankclienttestutil.QueryBalances(f, barAddr).AmountOf(cli.Denom).String())
// Generate a create validator transaction and ensure correctness
success, stdout, stderr := testutil.TxStakingCreateValidator(f, barAddr.String(), consPubKey, sdk.NewInt64Coin(cli.Denom, 2), "--generate-only")
require.True(t, success)
require.Empty(t, stderr)
msg := cli.UnmarshalStdTx(t, f.Cdc, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Len(t, msg.Msgs, 1)
require.Len(t, msg.GetSignatures(), 0)
// Test --dry-run
newValTokens := sdk.TokensFromConsensusPower(2)
success, _, _ = testutil.TxStakingCreateValidator(f, barAddr.String(), consPubKey, sdk.NewCoin(cli.Denom, newValTokens), "--dry-run")
require.True(t, success)
// Create the validator
testutil.TxStakingCreateValidator(f, cli.KeyBar, consPubKey, sdk.NewCoin(cli.Denom, newValTokens), "-y")
tests.WaitForNextNBlocksTM(1, f.Port)
// Ensure funds were deducted properly
require.Equal(t, sendTokens.Sub(newValTokens), bankclienttestutil.QueryBalances(f, barAddr).AmountOf(cli.Denom))
// Ensure that validator state is as expected
validator := testutil.QueryStakingValidator(f, barVal)
require.Equal(t, validator.OperatorAddress, barVal)
require.True(sdk.IntEq(t, newValTokens, validator.Tokens))
// Query delegations to the validator
validatorDelegations := testutil.QueryStakingDelegationsTo(f, barVal)
require.Len(t, validatorDelegations, 1)
require.NotNil(t, validatorDelegations[0].Shares)
// Edit validator
// params to be changed in edit validator (NOTE: a validator can only change its commission once per day)
newMoniker := "test-moniker"
newWebsite := "https://cosmos.network"
newIdentity := "6A0D65E29A4CBC8D"
newDetails := "To-infinity-and-beyond!"
// Test --generate-only"
success, stdout, stderr = testutil.TxStakingEditValidator(f, barAddr.String(), newMoniker, newWebsite, newIdentity, newDetails, "--generate-only")
require.True(t, success)
require.True(t, success)
require.Empty(t, stderr)
msg = cli.UnmarshalStdTx(t, f.Cdc, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Len(t, msg.Msgs, 1)
require.Len(t, msg.GetSignatures(), 0)
success, _, _ = testutil.TxStakingEditValidator(f, cli.KeyBar, newMoniker, newWebsite, newIdentity, newDetails, "-y")
require.True(t, success)
tests.WaitForNextNBlocksTM(1, f.Port)
udpatedValidator := testutil.QueryStakingValidator(f, barVal)
require.Equal(t, udpatedValidator.Description.Moniker, newMoniker)
require.Equal(t, udpatedValidator.Description.Identity, newIdentity)
require.Equal(t, udpatedValidator.Description.Website, newWebsite)
require.Equal(t, udpatedValidator.Description.Details, newDetails)
// unbond a single share
validators := testutil.QueryStakingValidators(f)
require.Len(t, validators, 2)
unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(1))
success = testutil.TxStakingUnbond(f, cli.KeyBar, unbondAmt.String(), barVal, "-y")
require.True(t, success)
tests.WaitForNextNBlocksTM(1, f.Port)
// Ensure bonded staking is correct
remainingTokens := newValTokens.Sub(unbondAmt.Amount)
validator = testutil.QueryStakingValidator(f, barVal)
require.Equal(t, remainingTokens, validator.Tokens)
// Query for historical info
require.NotEmpty(t, testutil.QueryStakingHistoricalInfo(f, 1))
// Get unbonding delegations from the validator
validatorUbds := testutil.QueryStakingUnbondingDelegationsFrom(f, barVal)
require.Len(t, validatorUbds, 1)
require.Len(t, validatorUbds[0].Entries, 1)
require.Equal(t, remainingTokens.String(), validatorUbds[0].Entries[0].Balance.String())
// Query staking unbonding delegation
ubd := testutil.QueryStakingUnbondingDelegation(f, barAddr.String(), barVal.String())
require.NotEmpty(t, ubd)
// Query staking unbonding delegations
ubds := testutil.QueryStakingUnbondingDelegations(f, barAddr.String())
require.Len(t, ubds, 1)
fooAddr := f.KeyAddress(cli.KeyFoo)
delegateTokens := sdk.TokensFromConsensusPower(2)
delegateAmount := sdk.NewCoin(cli.Denom, delegateTokens)
// Delegate txn
// Generate a create validator transaction and ensure correctness
success, stdout, stderr = testutil.TxStakingDelegate(f, fooAddr.String(), barVal.String(), delegateAmount, "--generate-only")
require.True(t, success)
require.Empty(t, stderr)
msg = cli.UnmarshalStdTx(t, f.Cdc, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Len(t, msg.Msgs, 1)
require.Len(t, msg.GetSignatures(), 0)
// Delegate
success, _, err := testutil.TxStakingDelegate(f, cli.KeyFoo, barVal.String(), delegateAmount, "-y")
tests.WaitForNextNBlocksTM(1, f.Port)
require.Empty(t, err)
require.True(t, success)
// Query the delegation from foo address to barval
delegation := testutil.QueryStakingDelegation(f, fooAddr.String(), barVal)
require.NotZero(t, delegation.Shares)
// Query the delegations from foo address to barval
delegations := testutil.QueryStakingDelegations(f, barAddr.String())
require.Len(t, delegations, 1)
fooVal := sdk.ValAddress(fooAddr)
// Redelegate
success, stdout, stderr = testutil.TxStakingRedelegate(f, fooAddr.String(), barVal.String(), fooVal.String(), delegateAmount, "--generate-only")
require.True(t, success)
require.Empty(t, stderr)
msg = cli.UnmarshalStdTx(t, f.Cdc, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Len(t, msg.Msgs, 1)
require.Len(t, msg.GetSignatures(), 0)
success, _, err = testutil.TxStakingRedelegate(f, cli.KeyFoo, barVal.String(), fooVal.String(), delegateAmount, "-y")
tests.WaitForNextNBlocksTM(1, f.Port)
require.Empty(t, err)
require.True(t, success)
redelegation := testutil.QueryStakingRedelegation(f, fooAddr.String(), barVal.String(), fooVal.String())
require.Len(t, redelegation, 1)
redelegations := testutil.QueryStakingRedelegations(f, fooAddr.String())
require.Len(t, redelegations, 1)
redelegationsFrom := testutil.QueryStakingRedelegationsFrom(f, barVal.String())
require.Len(t, redelegationsFrom, 1)
f.Cleanup()
cfg network.Config
network *network.Network
}
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
cfg := network.DefaultConfig()
cfg.NumValidators = 1
s.cfg = cfg
s.network = network.New(s.T(), cfg)
_, err := s.network.WaitForHeight(1)
s.Require().NoError(err)
}
func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}
func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() {
val := s.network.Validators[0]
consPrivKey := ed25519.GenPrivKey()
consPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, consPrivKey.PubKey())
s.Require().NoError(err)
info, _, err := val.ClientCtx.Keyring.NewMnemonic("NewValidator", keyring.English, sdk.FullFundraiserPath, hd.Secp256k1)
s.Require().NoError(err)
newAddr := sdk.AccAddress(info.GetPubKey().Address())
_, err = banktestutil.MsgSendExec(
val.ClientCtx,
val.Address,
newAddr,
sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(200))), fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
)
s.Require().NoError(err)
testCases := []struct {
name string
args []string
expectErr bool
respType fmt.Stringer
expectedCode uint32
}{
{
"invalid transaction (missing amount)",
[]string{
fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity),
fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite),
fmt.Sprintf("--%s=contact@newvalidator.io", cli.FlagSecurityContact),
fmt.Sprintf("--%s='Hey, I am a new validator. Please delegate!'", cli.FlagDetails),
fmt.Sprintf("--%s=0.5", cli.FlagCommissionRate),
fmt.Sprintf("--%s=1.0", cli.FlagCommissionMaxRate),
fmt.Sprintf("--%s=0.1", cli.FlagCommissionMaxChangeRate),
fmt.Sprintf("--%s=1", cli.FlagMinSelfDelegation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
true, nil, 0,
},
{
"invalid transaction (missing pubkey)",
[]string{
fmt.Sprintf("--%s=100stake", cli.FlagAmount),
fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity),
fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite),
fmt.Sprintf("--%s=contact@newvalidator.io", cli.FlagSecurityContact),
fmt.Sprintf("--%s='Hey, I am a new validator. Please delegate!'", cli.FlagDetails),
fmt.Sprintf("--%s=0.5", cli.FlagCommissionRate),
fmt.Sprintf("--%s=1.0", cli.FlagCommissionMaxRate),
fmt.Sprintf("--%s=0.1", cli.FlagCommissionMaxChangeRate),
fmt.Sprintf("--%s=1", cli.FlagMinSelfDelegation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
true, nil, 0,
},
{
"invalid transaction (missing moniker)",
[]string{
fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKey),
fmt.Sprintf("--%s=100stake", cli.FlagAmount),
fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity),
fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite),
fmt.Sprintf("--%s=contact@newvalidator.io", cli.FlagSecurityContact),
fmt.Sprintf("--%s='Hey, I am a new validator. Please delegate!'", cli.FlagDetails),
fmt.Sprintf("--%s=0.5", cli.FlagCommissionRate),
fmt.Sprintf("--%s=1.0", cli.FlagCommissionMaxRate),
fmt.Sprintf("--%s=0.1", cli.FlagCommissionMaxChangeRate),
fmt.Sprintf("--%s=1", cli.FlagMinSelfDelegation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
true, nil, 0,
},
{
"valid transaction",
[]string{
fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKey),
fmt.Sprintf("--%s=100stake", cli.FlagAmount),
fmt.Sprintf("--%s=NewValidator", cli.FlagMoniker),
fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity),
fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite),
fmt.Sprintf("--%s=contact@newvalidator.io", cli.FlagSecurityContact),
fmt.Sprintf("--%s='Hey, I am a new validator. Please delegate!'", cli.FlagDetails),
fmt.Sprintf("--%s=0.5", cli.FlagCommissionRate),
fmt.Sprintf("--%s=1.0", cli.FlagCommissionMaxRate),
fmt.Sprintf("--%s=0.1", cli.FlagCommissionMaxChangeRate),
fmt.Sprintf("--%s=1", cli.FlagMinSelfDelegation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
false, &sdk.TxResponse{}, 0,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.NewCreateValidatorCmd()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
out.Reset()
cmd.SetArgs(tc.args)
err := cmd.ExecuteContext(ctx)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err, out.String())
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
txResp := tc.respType.(*sdk.TxResponse)
s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
}
})
}
}
func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
+27 -25
View File
@@ -28,7 +28,7 @@ var (
)
// NewTxCmd returns a root CLI command handler for all x/staking transaction commands.
func NewTxCmd(clientCtx client.Context) *cobra.Command {
func NewTxCmd() *cobra.Command {
stakingTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Staking transaction subcommands",
@@ -38,22 +38,26 @@ func NewTxCmd(clientCtx client.Context) *cobra.Command {
}
stakingTxCmd.AddCommand(
NewCreateValidatorCmd(clientCtx),
NewEditValidatorCmd(clientCtx),
NewDelegateCmd(clientCtx),
NewRedelegateCmd(clientCtx),
NewUnbondCmd(clientCtx),
NewCreateValidatorCmd(),
NewEditValidatorCmd(),
NewDelegateCmd(),
NewRedelegateCmd(),
NewUnbondCmd(),
)
return stakingTxCmd
}
func NewCreateValidatorCmd(clientCtx client.Context) *cobra.Command {
func NewCreateValidatorCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-validator",
Short: "create new validator initialized with a self-delegation to it",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx.WithInput(cmd.InOrStdin())
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadTxCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever)
@@ -61,6 +65,7 @@ func NewCreateValidatorCmd(clientCtx client.Context) *cobra.Command {
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
},
}
@@ -83,12 +88,16 @@ func NewCreateValidatorCmd(clientCtx client.Context) *cobra.Command {
return cmd
}
func NewEditValidatorCmd(clientCtx client.Context) *cobra.Command {
func NewEditValidatorCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "edit-validator",
Short: "edit an existing validator account",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := clientCtx.WithInput(cmd.InOrStdin())
clientCtx := client.GetClientContextFromCmd(cmd)
clientCtx, err := client.ReadTxCommandFlags(clientCtx, cmd.Flags())
if err != nil {
return err
}
valAddr := clientCtx.GetFromAddress()
@@ -97,13 +106,7 @@ func NewEditValidatorCmd(clientCtx client.Context) *cobra.Command {
website, _ := cmd.Flags().GetString(FlagWebsite)
security, _ := cmd.Flags().GetString(FlagSecurityContact)
details, _ := cmd.Flags().GetString(FlagDetails)
description := types.NewDescription(
moniker,
identity,
website,
security,
details,
)
description := types.NewDescription(moniker, identity, website, security, details)
var newRate *sdk.Dec
@@ -134,8 +137,7 @@ func NewEditValidatorCmd(clientCtx client.Context) *cobra.Command {
return err
}
// build and sign the transaction, then broadcast to Tendermint
return tx.GenerateOrBroadcastTx(clientCtx, msg)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
@@ -147,7 +149,7 @@ func NewEditValidatorCmd(clientCtx client.Context) *cobra.Command {
return cmd
}
func NewDelegateCmd(clientCtx client.Context) *cobra.Command {
func NewDelegateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delegate [validator-addr] [amount]",
Args: cobra.ExactArgs(2),
@@ -184,7 +186,7 @@ $ %s tx staking delegate cosmosvaloper1l2rsakp388kuv9k8qzq6lrm9taddae7fpx59wm 10
return err
}
return tx.GenerateOrBroadcastTx(clientCtx, msg)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
@@ -193,7 +195,7 @@ $ %s tx staking delegate cosmosvaloper1l2rsakp388kuv9k8qzq6lrm9taddae7fpx59wm 10
return cmd
}
func NewRedelegateCmd(clientCtx client.Context) *cobra.Command {
func NewRedelegateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "redelegate [src-validator-addr] [dst-validator-addr] [amount]",
Short: "Redelegate illiquid tokens from one validator to another",
@@ -235,7 +237,7 @@ $ %s tx staking redelegate cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
return err
}
return tx.GenerateOrBroadcastTx(clientCtx, msg)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
@@ -244,7 +246,7 @@ $ %s tx staking redelegate cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
return cmd
}
func NewUnbondCmd(clientCtx client.Context) *cobra.Command {
func NewUnbondCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "unbond [validator-addr] [amount]",
Short: "Unbond shares from a validator",
@@ -281,7 +283,7 @@ $ %s tx staking unbond cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 100s
return err
}
return tx.GenerateOrBroadcastTx(clientCtx, msg)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
-220
View File
@@ -1,220 +0,0 @@
package testutil
import (
"fmt"
"github.com/stretchr/testify/require"
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
)
// TxStakingCreateValidator is simcli tx staking create-validator
func TxStakingCreateValidator(f *cli.Fixtures, from, consPubKey string, amount sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx staking create-validator %v --keyring-backend=test --from=%s"+
" --pubkey=%s", f.SimdBinary, f.Flags(), from, consPubKey)
cmd += fmt.Sprintf(" --amount=%v --moniker=%v --commission-rate=%v", amount, from, "0.05")
cmd += fmt.Sprintf(" --commission-max-rate=%v --commission-max-change-rate=%v", "0.20", "0.10")
cmd += fmt.Sprintf(" --min-self-delegation=%v", "1")
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxStakingEditValidator is simcli tx staking update validator info
func TxStakingEditValidator(f *cli.Fixtures, from, moniker, website, identity, details string, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx staking edit-validator %v --keyring-backend=test --from=%s", f.SimdBinary, f.Flags(), from)
cmd += fmt.Sprintf(" --moniker=%v --website=%s", moniker, website)
cmd += fmt.Sprintf(" --identity=%s --details=%s", identity, details)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxStakingUnbond is simcli tx staking unbond
func TxStakingUnbond(f *cli.Fixtures, from, shares string, validator sdk.ValAddress, flags ...string) bool {
cmd := fmt.Sprintf("%s tx staking unbond --keyring-backend=test %s %v --from=%s %v",
f.SimdBinary, validator, shares, from, f.Flags())
return cli.ExecuteWrite(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxStakingDelegate is simcli tx staking delegate
func TxStakingDelegate(f *cli.Fixtures, from, valOperAddr string, amount sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx staking delegate %s %v --keyring-backend=test --from=%s %v", f.SimdBinary, valOperAddr, amount, from, f.Flags())
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxStakingRedelegate is simcli tx staking redelegate
func TxStakingRedelegate(f *cli.Fixtures, from, srcVal, dstVal string, amount sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx staking redelegate %s %s %v --keyring-backend=test --from=%s %v", f.SimdBinary, srcVal, dstVal, amount, from, f.Flags())
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// QueryStakingValidator is simcli query staking validator
func QueryStakingValidator(f *cli.Fixtures, valAddr sdk.ValAddress, flags ...string) staking.Validator {
cmd := fmt.Sprintf("%s query staking validator %s %v", f.SimdBinary, valAddr, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var validator staking.Validator
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &validator))
return validator
}
// QueryStakingValidators is simcli query staking validators
func QueryStakingValidators(f *cli.Fixtures, flags ...string) []staking.Validator {
cmd := fmt.Sprintf("%s query staking validators %v", f.SimdBinary, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var validators []staking.Validator
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &validators))
return validators
}
// QueryStakingUnbondingDelegationsFrom is simcli query staking unbonding-delegations-from
func QueryStakingUnbondingDelegationsFrom(f *cli.Fixtures, valAddr sdk.ValAddress, flags ...string) []staking.UnbondingDelegation {
cmd := fmt.Sprintf("%s query staking unbonding-delegations-from %s %v", f.SimdBinary, valAddr, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var ubds []staking.UnbondingDelegation
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &ubds))
return ubds
}
// QueryStakingDelegationsTo is simcli query staking delegations-to
func QueryStakingDelegationsTo(f *cli.Fixtures, valAddr sdk.ValAddress, flags ...string) []staking.Delegation {
cmd := fmt.Sprintf("%s query staking delegations-to %s %v", f.SimdBinary, valAddr, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var delegations []staking.Delegation
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &delegations))
return delegations
}
// QueryStakingPool is simcli query staking pool
func QueryStakingPool(f *cli.Fixtures, flags ...string) staking.Pool {
cmd := fmt.Sprintf("%s query staking pool %v", f.SimdBinary, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var pool staking.Pool
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &pool))
return pool
}
// QueryStakingParameters is simcli query staking parameters
func QueryStakingParameters(f *cli.Fixtures, flags ...string) staking.Params {
cmd := fmt.Sprintf("%s query staking params %v", f.SimdBinary, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var params staking.Params
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &params))
return params
}
// QueryStakingDelegation is simcli query staking delegation
func QueryStakingDelegation(f *cli.Fixtures, from string, valAddr sdk.ValAddress, flags ...string) staking.Delegation {
cmd := fmt.Sprintf("%s query staking delegation %s %s %v", f.SimdBinary, from, valAddr, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var delegation staking.Delegation
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &delegation))
return delegation
}
// QueryStakingDelegations is simcli query staking delegations
func QueryStakingDelegations(f *cli.Fixtures, from string, flags ...string) []staking.Delegation {
cmd := fmt.Sprintf("%s query staking delegations %s %v", f.SimdBinary, from, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var delegations []staking.Delegation
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &delegations))
return delegations
}
// QueryStakingRedelegation is simcli query staking redelegation
func QueryStakingRedelegation(f *cli.Fixtures, delAdrr, srcVal, dstVal string, flags ...string) []staking.RedelegationResponse {
cmd := fmt.Sprintf("%s query staking redelegation %v %v %v %v", f.SimdBinary, delAdrr, srcVal, dstVal, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var redelegations []staking.RedelegationResponse
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &redelegations))
return redelegations
}
// QueryStakingRedelegations is simcli query staking redelegation
func QueryStakingRedelegations(f *cli.Fixtures, delAdrr string, flags ...string) []staking.RedelegationResponse {
cmd := fmt.Sprintf("%s query staking redelegations %v %v", f.SimdBinary, delAdrr, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var redelegations []staking.RedelegationResponse
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &redelegations))
return redelegations
}
// QueryStakingRedelegationsFrom is simcli query staking redelegations-from
func QueryStakingRedelegationsFrom(f *cli.Fixtures, valAddr string, flags ...string) []staking.RedelegationResponse {
cmd := fmt.Sprintf("%s query staking redelegations-from %v %v", f.SimdBinary, valAddr, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var redelegations []staking.RedelegationResponse
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &redelegations))
return redelegations
}
// QueryStakingUnbondingDelegation is simcli query staking unbonding-delegation
func QueryStakingUnbondingDelegation(f *cli.Fixtures, delAdrr, valAddr string, flags ...string) staking.UnbondingDelegation {
cmd := fmt.Sprintf("%s query staking unbonding-delegation %v %v %v", f.SimdBinary, delAdrr, valAddr, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var ubd staking.UnbondingDelegation
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &ubd))
return ubd
}
// QueryStakingUnbondingDelegations is simcli query staking unbonding-delegations
func QueryStakingUnbondingDelegations(f *cli.Fixtures, delAdrr string, flags ...string) []staking.UnbondingDelegation {
cmd := fmt.Sprintf("%s query staking unbonding-delegations %v %v", f.SimdBinary, delAdrr, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var ubds []staking.UnbondingDelegation
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &ubds))
return ubds
}
// QueryStakingHistoricalInfo is simcli query staking historical-info
func QueryStakingHistoricalInfo(f *cli.Fixtures, height uint, flags ...string) staking.HistoricalInfo {
cmd := fmt.Sprintf("%s query staking historical-info %d %v", f.SimdBinary, height, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)
var historicalInfo staking.HistoricalInfo
require.NoError(f.T, f.Cdc.UnmarshalJSON([]byte(out), &historicalInfo))
return historicalInfo
}
+2 -2
View File
@@ -68,8 +68,8 @@ func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Rout
}
// GetTxCmd returns the root tx command for the staking module.
func (AppModuleBasic) GetTxCmd(clientCtx client.Context) *cobra.Command {
return cli.NewTxCmd(clientCtx)
func (AppModuleBasic) GetTxCmd(_ client.Context) *cobra.Command {
return cli.NewTxCmd()
}
// GetQueryCmd returns no root query command for the staking module.
+1 -1
View File
@@ -137,7 +137,7 @@ func NewCmdSubmitCancelUpgradeProposal() *cobra.Command {
return err
}
return tx.GenerateOrBroadcastTx(clientCtx, msg)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}