diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f3d48cfb43d6..85575903e11c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -188,24 +188,3 @@ jobs: with: file: ./coverage.txt if: "env.GIT_DIFF != ''" - - integration-tests: - runs-on: ubuntu-latest - timeout-minutes: 10 - steps: - - uses: actions/checkout@v2 - - uses: technote-space/get-diff-action@v1 - id: git_diff - with: - SUFFIX_FILTER: | - .go - .mod - .sum - - name: build-simd - run: | - make build-simd - if: "env.GIT_DIFF != ''" - - name: cli-test - run: | - make test-integration - if: "env.GIT_DIFF != ''" diff --git a/Makefile b/Makefile index 7cd1b481713c..9897adc8cbeb 100644 --- a/Makefile +++ b/Makefile @@ -188,9 +188,6 @@ test-unit: test-race: @VERSION=$(VERSION) go test -mod=readonly -race $(PACKAGES_NOSIMULATION) -test-integration: build-simd - BUILDDIR=$(BUILDDIR) go test -mod=readonly -p 4 -tags='ledger test_ledger_mock cli_test' -run ^TestCLI `go list ./.../cli/...` - .PHONY: test test-all test-ledger-mock test-ledger test-unit test-race test-sim-nondeterminism: @@ -239,8 +236,7 @@ test-sim-after-import \ test-sim-custom-genesis-multi-seed \ test-sim-multi-seed-short \ test-sim-multi-seed-long \ -test-sim-benchmark-invariants \ -test-integration +test-sim-benchmark-invariants SIM_NUM_BLOCKS ?= 500 SIM_BLOCK_SIZE ?= 200 diff --git a/client/context_test.go b/client/context_test.go index 4b3498cffe6a..accca7dcff99 100644 --- a/client/context_test.go +++ b/client/context_test.go @@ -2,20 +2,20 @@ package client_test import ( "bytes" + "context" "os" "testing" - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/testutil/testdata" - - "github.com/cosmos/cosmos-sdk/crypto/keyring" - "github.com/spf13/viper" "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + "github.com/cosmos/cosmos-sdk/testutil/network" + "github.com/cosmos/cosmos-sdk/testutil/testdata" ) func TestMain(m *testing.M) { @@ -100,3 +100,16 @@ value: x: "10" `, string(buf.Bytes())) } + +func TestCLIQueryConn(t *testing.T) { + cfg := network.DefaultConfig() + cfg.NumValidators = 1 + + n := network.New(t, cfg) + defer n.Cleanup() + + testClient := testdata.NewTestServiceClient(n.Validators[0].ClientCtx) + res, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"}) + require.NoError(t, err) + require.Equal(t, "hello", res.Message) +} diff --git a/client/tx/factory.go b/client/tx/factory.go index 44f73034afd8..779daadd5402 100644 --- a/client/tx/factory.go +++ b/client/tx/factory.go @@ -1,10 +1,7 @@ package tx import ( - "io" - "github.com/spf13/pflag" - "github.com/spf13/viper" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" @@ -78,47 +75,6 @@ func NewFactoryCLI(clientCtx client.Context, flagSet *pflag.FlagSet) Factory { return f } -// TODO: Remove in favor of NewFactoryCLI -func NewFactoryFromDeprecated(input io.Reader) Factory { - kb, err := keyring.New( - sdk.KeyringServiceName(), - viper.GetString(flags.FlagKeyringBackend), - viper.GetString(flags.FlagHome), - input, - ) - if err != nil { - panic(err) - } - - signModeStr := viper.GetString(flags.FlagSignMode) - signMode := signing.SignMode_SIGN_MODE_UNSPECIFIED - switch signModeStr { - case signModeDirect: - signMode = signing.SignMode_SIGN_MODE_DIRECT - case signModeAminoJSON: - signMode = signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON - } - - gasSetting, _ := flags.ParseGasSetting(viper.GetString(flags.FlagGas)) - - f := Factory{ - keybase: kb, - chainID: viper.GetString(flags.FlagChainID), - accountNumber: viper.GetUint64(flags.FlagAccountNumber), - sequence: viper.GetUint64(flags.FlagSequence), - gas: gasSetting.Gas, - simulateAndExecute: gasSetting.Simulate, - gasAdjustment: viper.GetFloat64(flags.FlagGasAdjustment), - memo: viper.GetString(flags.FlagMemo), - signMode: signMode, - } - - f = f.WithFees(viper.GetString(flags.FlagFees)) - f = f.WithGasPrices(viper.GetString(flags.FlagGasPrices)) - - return f -} - func (f Factory) AccountNumber() uint64 { return f.accountNumber } func (f Factory) Sequence() uint64 { return f.sequence } func (f Factory) Gas() uint64 { return f.gas } diff --git a/client/tx/tx.go b/client/tx/tx.go index 18de4eadacc4..17f1e232ed44 100644 --- a/client/tx/tx.go +++ b/client/tx/tx.go @@ -29,15 +29,6 @@ func GenerateOrBroadcastTxCLI(clientCtx client.Context, flagSet *pflag.FlagSet, return GenerateOrBroadcastTxWithFactory(clientCtx, txf, msgs...) } -// GenerateOrBroadcastTx will either generate and print and unsigned transaction -// or sign it and broadcast it returning an error upon failure. -// -// TODO: Remove in favor of GenerateOrBroadcastTxCLI -func GenerateOrBroadcastTx(clientCtx client.Context, msgs ...sdk.Msg) error { - txf := NewFactoryFromDeprecated(clientCtx.Input).WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever) - return GenerateOrBroadcastTxWithFactory(clientCtx, txf, msgs...) -} - // GenerateOrBroadcastTxWithFactory will either generate and print and unsigned transaction // or sign it and broadcast it returning an error upon failure. func GenerateOrBroadcastTxWithFactory(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error { diff --git a/contrib/test_cover.sh b/contrib/test_cover.sh index 338bea07d212..24f7804b5162 100644 --- a/contrib/test_cover.sh +++ b/contrib/test_cover.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -e -PKGS=$(go list ./... | grep -v '/simapp' | grep -v '/cli_test') +PKGS=$(go list ./... | grep -v '/simapp') set -e echo "mode: atomic" > coverage.txt diff --git a/tests/cli/constants.go b/tests/cli/constants.go deleted file mode 100644 index 9068d44464ea..000000000000 --- a/tests/cli/constants.go +++ /dev/null @@ -1,13 +0,0 @@ -package cli - -const ( - Denom = "stake" - KeyFoo = "foo" - KeyBar = "bar" - FooDenom = "footoken" - FeeDenom = "feetoken" - Fee2Denom = "fee2token" - KeyBaz = "baz" - KeyVesting = "vesting" - KeyFooBarBaz = "foobarbaz" -) diff --git a/tests/cli/doc.go b/tests/cli/doc.go deleted file mode 100644 index fca4bd79fd79..000000000000 --- a/tests/cli/doc.go +++ /dev/null @@ -1,3 +0,0 @@ -package cli - -// package tests runs integration tests which make use of CLI commands. diff --git a/tests/cli/executors.go b/tests/cli/executors.go deleted file mode 100644 index 3f7e81475332..000000000000 --- a/tests/cli/executors.go +++ /dev/null @@ -1,49 +0,0 @@ -package cli - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/require" - - "github.com/cosmos/cosmos-sdk/tests" -) - -func ExecuteWriteCheckErr(t *testing.T, cmdStr string, writes ...string) { - require.True(t, ExecuteWrite(t, cmdStr, writes...)) -} - -func ExecuteWrite(t *testing.T, cmdStr string, writes ...string) (exitSuccess bool) { - exitSuccess, _, _ = ExecuteWriteRetStdStreams(t, cmdStr, writes...) - return -} - -func ExecuteWriteRetStdStreams(t *testing.T, cmdStr string, writes ...string) (bool, string, string) { - proc := tests.GoExecuteT(t, cmdStr) - - // Enables use of interactive commands - for _, write := range writes { - _, err := proc.StdinPipe.Write([]byte(write + "\n")) - require.NoError(t, err) - } - - // Read both stdout and stderr from the process - stdout, stderr, err := proc.ReadAll() - if err != nil { - fmt.Println("Err on proc.ReadAll()", err, cmdStr) - } - - // Log output. - if len(stdout) > 0 { - t.Log("Stdout:", string(stdout)) - } - if len(stderr) > 0 { - t.Log("Stderr:", string(stderr)) - } - - // Wait for process to exit - proc.Wait() - - // Return succes, stdout, stderr - return proc.ExitState.Success(), string(stdout), string(stderr) -} diff --git a/tests/cli/fixtures.go b/tests/cli/fixtures.go deleted file mode 100644 index 0d2b04938537..000000000000 --- a/tests/cli/fixtures.go +++ /dev/null @@ -1,81 +0,0 @@ -package cli - -import ( - "io/ioutil" - "os" - "path/filepath" - "testing" - - "github.com/stretchr/testify/require" - - tmtypes "github.com/tendermint/tendermint/types" - - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/server" - "github.com/cosmos/cosmos-sdk/simapp" - "github.com/cosmos/cosmos-sdk/simapp/params" -) - -// Fixtures is used to setup the testing environment -type Fixtures struct { - BuildDir string - RootDir string - SimdBinary string - ChainID string - RPCAddr string - Port string - SimdHome string - SimcliHome string - P2PAddr string - Cdc *codec.Codec - EncodingConfig params.EncodingConfig - T *testing.T -} - -// NewFixtures creates a new instance of Fixtures with many vars set -func NewFixtures(t *testing.T) *Fixtures { - tmpDir, err := ioutil.TempDir("", "sdk_integration_"+t.Name()+"_") - require.NoError(t, err) - - servAddr, port, err := server.FreeTCPAddr() - require.NoError(t, err) - - p2pAddr, _, err := server.FreeTCPAddr() - require.NoError(t, err) - - buildDir := os.Getenv("BUILDDIR") - if buildDir == "" { - t.Skip("builddir is empty, skipping") - } - - encodingConfig := simapp.MakeEncodingConfig() - - return &Fixtures{ - T: t, - BuildDir: buildDir, - RootDir: tmpDir, - SimdBinary: filepath.Join(buildDir, "simd"), - SimdHome: filepath.Join(tmpDir, ".simd"), - SimcliHome: filepath.Join(tmpDir, ".simcli"), - RPCAddr: servAddr, - P2PAddr: p2pAddr, - Cdc: encodingConfig.Amino, - EncodingConfig: encodingConfig, - Port: port, - } -} - -// GenesisFile returns the path of the genesis file -func (f Fixtures) GenesisFile() string { - return filepath.Join(f.SimdHome, "config", "genesis.json") -} - -// GenesisFile returns the application's genesis state -func (f Fixtures) GenesisState() simapp.GenesisState { - genDoc, err := tmtypes.GenesisDocFromFile(f.GenesisFile()) - require.NoError(f.T, err) - - var appState simapp.GenesisState - require.NoError(f.T, f.Cdc.UnmarshalJSON(genDoc.AppState, &appState)) - return appState -} diff --git a/tests/cli/grpc_query_test.go b/tests/cli/grpc_query_test.go deleted file mode 100644 index e3d89c9bcf53..000000000000 --- a/tests/cli/grpc_query_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package cli - -import ( - "context" - "testing" - - "github.com/stretchr/testify/require" - - "github.com/cosmos/cosmos-sdk/testutil/network" - "github.com/cosmos/cosmos-sdk/testutil/testdata" -) - -func TestCLIQueryConn(t *testing.T) { - cfg := network.DefaultConfig() - cfg.NumValidators = 1 - - n := network.New(t, cfg) - defer n.Cleanup() - - testClient := testdata.NewTestServiceClient(n.Validators[0].ClientCtx) - res, err := testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"}) - require.NoError(t, err) - require.Equal(t, "hello", res.Message) -} diff --git a/tests/cli/helpers.go b/tests/cli/helpers.go deleted file mode 100644 index 945b2f3decd3..000000000000 --- a/tests/cli/helpers.go +++ /dev/null @@ -1,198 +0,0 @@ -package cli - -import ( - "encoding/json" - "fmt" - "os" - "path/filepath" - "strings" - - "github.com/stretchr/testify/require" - - clientkeys "github.com/cosmos/cosmos-sdk/client/keys" - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - "github.com/cosmos/cosmos-sdk/tests" - sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" -) - -var ( - TotalCoins = sdk.NewCoins( - sdk.NewCoin(Fee2Denom, sdk.TokensFromConsensusPower(2000000)), - sdk.NewCoin(FeeDenom, sdk.TokensFromConsensusPower(2000000)), - sdk.NewCoin(FooDenom, sdk.TokensFromConsensusPower(2000)), - sdk.NewCoin(Denom, sdk.TokensFromConsensusPower(300).Add(sdk.NewInt(12))), // add coins from inflation - ) - - StartCoins = sdk.NewCoins( - sdk.NewCoin(Fee2Denom, sdk.TokensFromConsensusPower(1000000)), - sdk.NewCoin(FeeDenom, sdk.TokensFromConsensusPower(1000000)), - sdk.NewCoin(FooDenom, sdk.TokensFromConsensusPower(1000)), - sdk.NewCoin(Denom, sdk.TokensFromConsensusPower(150)), - ) - - VestingCoins = sdk.NewCoins( - sdk.NewCoin(FeeDenom, sdk.TokensFromConsensusPower(500000)), - ) -) - -//___________________________________________________________________________________ -// simd - -// UnsafeResetAll is simd unsafe-reset-all -func (f *Fixtures) UnsafeResetAll(flags ...string) { - cmd := fmt.Sprintf("%s --home=%s unsafe-reset-all", f.SimdBinary, f.SimdHome) - ExecuteWrite(f.T, AddFlags(cmd, flags)) - err := os.RemoveAll(filepath.Join(f.SimdHome, "config", "gentx")) - require.NoError(f.T, err) -} - -// SDInit is simd init -// NOTE: SDInit sets the ChainID for the Fixtures instance -func (f *Fixtures) SDInit(moniker string, flags ...string) { - cmd := fmt.Sprintf("%s init --overwrite --home=%s %s", f.SimdBinary, f.SimdHome, moniker) - _, stderr := tests.ExecuteT(f.T, AddFlags(cmd, flags), clientkeys.DefaultKeyPass) - - var chainID string - var initRes map[string]json.RawMessage - - err := json.Unmarshal([]byte(stderr), &initRes) - require.NoError(f.T, err) - - err = json.Unmarshal(initRes["chain_id"], &chainID) - require.NoError(f.T, err) - - f.ChainID = chainID -} - -// AddGenesisAccount is simd add-genesis-account -func (f *Fixtures) AddGenesisAccount(address sdk.AccAddress, coins sdk.Coins, flags ...string) { - cmd := fmt.Sprintf("%s add-genesis-account %s %s --home=%s --keyring-backend=test", f.SimdBinary, address, coins, f.SimdHome) - ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags)) -} - -// GenTx is simd gentx -func (f *Fixtures) GenTx(name string, flags ...string) { - cmd := fmt.Sprintf("%s gentx --name=%s --home=%s --keyring-backend=test --chain-id=%s", f.SimdBinary, name, f.SimdHome, f.ChainID) - ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags)) -} - -// CollectGenTxs is simd collect-gentxs -func (f *Fixtures) CollectGenTxs(flags ...string) { - cmd := fmt.Sprintf("%s collect-gentxs --home=%s", f.SimdBinary, f.SimdHome) - ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags)) -} - -// SDStart runs simd start with the appropriate flags and returns a process -func (f *Fixtures) SDStart(flags ...string) *tests.Process { - cmd := fmt.Sprintf("%s start --home=%s --rpc.laddr=%v --p2p.laddr=%v", f.SimdBinary, f.SimdHome, f.RPCAddr, f.P2PAddr) - proc := tests.GoExecuteTWithStdout(f.T, AddFlags(cmd, flags)) - tests.WaitForTMStart(f.Port) - tests.WaitForNextNBlocksTM(1, f.Port) - return proc -} - -// SDTendermint returns the results of simd tendermint [query] -func (f *Fixtures) SDTendermint(query string) string { - cmd := fmt.Sprintf("%s tendermint %s --home=%s", f.SimdBinary, query, f.SimdHome) - success, stdout, stderr := ExecuteWriteRetStdStreams(f.T, cmd) - require.Empty(f.T, stderr) - require.True(f.T, success) - return strings.TrimSpace(stdout) -} - -// ValidateGenesis runs simd validate-genesis -func (f *Fixtures) ValidateGenesis() { - cmd := fmt.Sprintf("%s validate-genesis --home=%s", f.SimdBinary, f.SimdHome) - ExecuteWriteCheckErr(f.T, cmd) -} - -//___________________________________________________________________________________ -// simcli keys - -// KeysDelete is simcli keys delete -func (f *Fixtures) KeysDelete(name string, flags ...string) { - cmd := fmt.Sprintf("%s keys delete --keyring-backend=test --home=%s %s", f.SimdBinary, - f.SimcliHome, name) - ExecuteWrite(f.T, AddFlags(cmd, append(append(flags, "-y"), "-f"))) -} - -// KeysAdd is simcli keys add -func (f *Fixtures) KeysAdd(name string, flags ...string) { - cmd := fmt.Sprintf("%s keys add --keyring-backend=test --home=%s %s", f.SimdBinary, - f.SimcliHome, name) - ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags)) -} - -// KeysAddRecover prepares simcli keys add --recover -func (f *Fixtures) KeysAddRecover(name, mnemonic string, flags ...string) (exitSuccess bool, stdout, stderr string) { - cmd := fmt.Sprintf("%s keys add --keyring-backend=test --home=%s --recover %s", - f.SimdBinary, f.SimcliHome, name) - return ExecuteWriteRetStdStreams(f.T, AddFlags(cmd, flags), mnemonic) -} - -// KeysAddRecoverHDPath prepares simcli keys add --recover --account --index -func (f *Fixtures) KeysAddRecoverHDPath(name, mnemonic string, account uint32, index uint32, flags ...string) { - cmd := fmt.Sprintf("%s keys add --keyring-backend=test --home=%s --recover %s --account %d"+ - " --index %d", f.SimdBinary, f.SimcliHome, name, account, index) - ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags), mnemonic) -} - -// KeysShow is simcli keys show -func (f *Fixtures) KeysShow(name string, flags ...string) keyring.KeyOutput { - cmd := fmt.Sprintf("%s keys show --keyring-backend=test --home=%s %s --output=json", f.SimdBinary, f.SimcliHome, name) - out, _ := tests.ExecuteT(f.T, AddFlags(cmd, flags), "") - var ko keyring.KeyOutput - err := clientkeys.UnmarshalJSON([]byte(out), &ko) - require.NoError(f.T, err) - return ko -} - -// KeyAddress returns the SDK account address from the key -func (f *Fixtures) KeyAddress(name string) sdk.AccAddress { - ko := f.KeysShow(name) - accAddr, err := sdk.AccAddressFromBech32(ko.Address) - require.NoError(f.T, err) - return accAddr -} - -//___________________________________________________________________________________ -// simcli query txs - -// QueryTxs is simcli query txs -func (f *Fixtures) QueryTxs(page, limit int, events ...string) *sdk.SearchTxsResult { - cmd := fmt.Sprintf("%s query txs --page=%d --limit=%d --events='%s' %v", - f.SimdBinary, page, limit, buildEventsQueryString(events), f.Flags()) - out, _ := tests.ExecuteT(f.T, cmd, "") - var result sdk.SearchTxsResult - - err := f.Cdc.UnmarshalJSON([]byte(out), &result) - require.NoError(f.T, err, "out %v\n, err %v", out, err) - return &result -} - -//utils - -func AddFlags(cmd string, flags []string) string { - for _, f := range flags { - cmd += " " + f - } - return strings.TrimSpace(cmd) -} - -func UnmarshalStdTx(t require.TestingT, c codec.JSONMarshaler, s string) (stdTx authtypes.StdTx) { - require.Nil(t, c.UnmarshalJSON([]byte(s), &stdTx)) - return -} - -func buildEventsQueryString(events []string) string { - return strings.Join(events, "&") -} - -func MarshalStdTx(t require.TestingT, c *codec.Codec, stdTx authtypes.StdTx) []byte { - bz, err := c.MarshalBinaryBare(stdTx) - require.NoError(t, err) - - return bz -} diff --git a/tests/cli/init.go b/tests/cli/init.go deleted file mode 100644 index 418122975c0a..000000000000 --- a/tests/cli/init.go +++ /dev/null @@ -1,60 +0,0 @@ -package cli - -import ( - "fmt" - "os" - "testing" - "time" - - "github.com/stretchr/testify/require" -) - -// InitFixtures is called at the beginning of a test and initializes a chain -// with 1 validator. -func InitFixtures(t *testing.T) (f *Fixtures) { - f = NewFixtures(t) - - // reset test state - f.UnsafeResetAll() - - // ensure keystore has foo and bar keys - f.KeysDelete(KeyFoo) - f.KeysDelete(KeyBar) - f.KeysDelete(KeyBar) - f.KeysDelete(KeyFooBarBaz) - f.KeysAdd(KeyFoo) - f.KeysAdd(KeyBar) - f.KeysAdd(KeyBaz) - f.KeysAdd(KeyVesting) - f.KeysAdd(KeyFooBarBaz, "--multisig-threshold=2", fmt.Sprintf("--multisig=%s,%s,%s", KeyFoo, KeyBar, KeyBaz)) - - // NOTE: SDInit sets the ChainID - f.SDInit(KeyFoo) - - // start an account with tokens - f.AddGenesisAccount(f.KeyAddress(KeyFoo), StartCoins) - f.AddGenesisAccount( - f.KeyAddress(KeyVesting), StartCoins, - fmt.Sprintf("--vesting-amount=%s", VestingCoins), - fmt.Sprintf("--vesting-start-time=%d", time.Now().UTC().UnixNano()), - fmt.Sprintf("--vesting-end-time=%d", time.Now().Add(60*time.Second).UTC().UnixNano()), - ) - - f.GenTx(KeyFoo) - f.CollectGenTxs() - - return f -} - -// Cleanup is meant to be run at the end of a test to clean up an remaining test state -func (f *Fixtures) Cleanup(dirs ...string) { - clean := append(dirs, f.RootDir) - for _, d := range clean { - require.NoError(f.T, os.RemoveAll(d)) - } -} - -// Flags returns the flags necessary for making most CLI calls -func (f *Fixtures) Flags() string { - return fmt.Sprintf("--home=%s --node=%s --chain-id=%s --output=json", f.SimcliHome, f.RPCAddr, f.ChainID) -} diff --git a/tests/cli/keys_test.go b/tests/cli/keys_test.go deleted file mode 100644 index 8ec3b3fb5c70..000000000000 --- a/tests/cli/keys_test.go +++ /dev/null @@ -1,77 +0,0 @@ -// +build cli_test - -package cli_test - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/require" - - "github.com/cosmos/cosmos-sdk/tests/cli" -) - -func TestCLIKeysAddMultisig(t *testing.T) { - t.SkipNow() // TODO: Bring back once viper is refactored. - t.Parallel() - f := cli.InitFixtures(t) - - // key names order does not matter - f.KeysAdd("msig1", "--multisig-threshold=2", - fmt.Sprintf("--multisig=%s,%s", cli.KeyBar, cli.KeyBaz)) - ke1Address1 := f.KeysShow("msig1").Address - f.KeysDelete("msig1") - - f.KeysAdd("msig2", "--multisig-threshold=2", - fmt.Sprintf("--multisig=%s,%s", cli.KeyBaz, cli.KeyBar)) - require.Equal(t, ke1Address1, f.KeysShow("msig2").Address) - f.KeysDelete("msig2") - - f.KeysAdd("msig3", "--multisig-threshold=2", - fmt.Sprintf("--multisig=%s,%s", cli.KeyBar, cli.KeyBaz), - "--nosort") - f.KeysAdd("msig4", "--multisig-threshold=2", - fmt.Sprintf("--multisig=%s,%s", cli.KeyBaz, cli.KeyBar), - "--nosort") - require.NotEqual(t, f.KeysShow("msig3").Address, f.KeysShow("msig4").Address) - - // Cleanup testing directories - f.Cleanup() -} - -func TestCLIKeysAddRecover(t *testing.T) { - t.SkipNow() // TODO: Bring back once viper is refactored. - t.Parallel() - f := cli.InitFixtures(t) - - exitSuccess, _, _ := f.KeysAddRecover("empty-mnemonic", "") - require.False(t, exitSuccess) - - exitSuccess, _, _ = f.KeysAddRecover("test-recover", "dentist task convince chimney quality leave banana trade firm crawl eternal easily") - require.True(t, exitSuccess) - require.Equal(t, "cosmos1qcfdf69js922qrdr4yaww3ax7gjml6pdds46f4", f.KeyAddress("test-recover").String()) - - // Cleanup testing directories - f.Cleanup() -} - -func TestCLIKeysAddRecoverHDPath(t *testing.T) { - t.SkipNow() // TODO: Bring back once viper is refactored. - t.Parallel() - f := cli.InitFixtures(t) - - f.KeysAddRecoverHDPath("test-recoverHD1", "dentist task convince chimney quality leave banana trade firm crawl eternal easily", 0, 0) - require.Equal(t, "cosmos1qcfdf69js922qrdr4yaww3ax7gjml6pdds46f4", f.KeyAddress("test-recoverHD1").String()) - - f.KeysAddRecoverHDPath("test-recoverH2", "dentist task convince chimney quality leave banana trade firm crawl eternal easily", 1, 5) - require.Equal(t, "cosmos1pdfav2cjhry9k79nu6r8kgknnjtq6a7rykmafy", f.KeyAddress("test-recoverH2").String()) - - f.KeysAddRecoverHDPath("test-recoverH3", "dentist task convince chimney quality leave banana trade firm crawl eternal easily", 1, 17) - require.Equal(t, "cosmos1909k354n6wl8ujzu6kmh49w4d02ax7qvlkv4sn", f.KeyAddress("test-recoverH3").String()) - - f.KeysAddRecoverHDPath("test-recoverH4", "dentist task convince chimney quality leave banana trade firm crawl eternal easily", 2, 17) - require.Equal(t, "cosmos1v9plmhvyhgxk3th9ydacm7j4z357s3nhtwsjat", f.KeyAddress("test-recoverH4").String()) - - // Cleanup testing directories - f.Cleanup() -} diff --git a/tests/cli/simd_test.go b/tests/cli/simd_test.go deleted file mode 100644 index 51eab8e4a0f4..000000000000 --- a/tests/cli/simd_test.go +++ /dev/null @@ -1,127 +0,0 @@ -// +build cli_test - -package cli_test - -import ( - "fmt" - "io/ioutil" - "path/filepath" - "testing" - - "github.com/stretchr/testify/require" - tmtypes "github.com/tendermint/tendermint/types" - - "github.com/cosmos/cosmos-sdk/tests/cli" - sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" -) - -func TestCLISimdCollectGentxs(t *testing.T) { - t.SkipNow() // TODO: Bring back once viper is refactored. - t.Parallel() - var customMaxBytes, customMaxGas int64 = 99999999, 1234567 - f := cli.NewFixtures(t) - - // Initialise temporary directories - gentxDir, err := ioutil.TempDir("", "") - gentxDoc := filepath.Join(gentxDir, "gentx.json") - require.NoError(t, err) - - // Reset testing path - f.UnsafeResetAll() - - // Initialize keys - f.KeysAdd(cli.KeyFoo) - - // Run init - f.SDInit(cli.KeyFoo) - - // Customise genesis.json - - genFile := f.GenesisFile() - genDoc, err := tmtypes.GenesisDocFromFile(genFile) - require.NoError(t, err) - genDoc.ConsensusParams.Block.MaxBytes = customMaxBytes - genDoc.ConsensusParams.Block.MaxGas = customMaxGas - genDoc.SaveAs(genFile) - - // Add account to genesis.json - f.AddGenesisAccount(f.KeyAddress(cli.KeyFoo), cli.StartCoins) - - // Write gentx file - f.GenTx(cli.KeyFoo, fmt.Sprintf("--output-document=%s", gentxDoc)) - - // Collect gentxs from a custom directory - f.CollectGenTxs(fmt.Sprintf("--gentx-dir=%s", gentxDir)) - - genDoc, err = tmtypes.GenesisDocFromFile(genFile) - require.NoError(t, err) - require.Equal(t, genDoc.ConsensusParams.Block.MaxBytes, customMaxBytes) - require.Equal(t, genDoc.ConsensusParams.Block.MaxGas, customMaxGas) - - f.Cleanup(gentxDir) -} - -func TestCLISimdAddGenesisAccount(t *testing.T) { - t.Parallel() - f := cli.NewFixtures(t) - - // Reset testing path - f.UnsafeResetAll() - - // Initialize keys - f.KeysDelete(cli.KeyFoo) - f.KeysDelete(cli.KeyBar) - f.KeysDelete(cli.KeyBaz) - f.KeysAdd(cli.KeyFoo) - f.KeysAdd(cli.KeyBar) - f.KeysAdd(cli.KeyBaz) - - // Run init - f.SDInit(cli.KeyFoo) - - // Add account to genesis.json - bazCoins := sdk.Coins{ - sdk.NewInt64Coin("acoin", 1000000), - sdk.NewInt64Coin("bcoin", 1000000), - } - - f.AddGenesisAccount(f.KeyAddress(cli.KeyFoo), cli.StartCoins) - f.AddGenesisAccount(f.KeyAddress(cli.KeyBar), bazCoins) - - genesisState := f.GenesisState() - - appCodec := f.EncodingConfig.Marshaler - - accounts := authtypes.GetGenesisStateFromAppState(appCodec, genesisState).Accounts - balances := banktypes.GetGenesisStateFromAppState(f.Cdc, genesisState).Balances - balancesSet := make(map[string]sdk.Coins) - - for _, b := range balances { - balancesSet[b.GetAddress().String()] = b.Coins - } - - require.Equal(t, accounts[0].GetAddress(), f.KeyAddress(cli.KeyFoo)) - require.Equal(t, accounts[1].GetAddress(), f.KeyAddress(cli.KeyBar)) - require.True(t, balancesSet[accounts[0].GetAddress().String()].IsEqual(cli.StartCoins)) - require.True(t, balancesSet[accounts[1].GetAddress().String()].IsEqual(bazCoins)) - - // Cleanup testing directories - f.Cleanup() -} - -func TestCLIValidateGenesis(t *testing.T) { - t.SkipNow() - t.Parallel() - f := cli.InitFixtures(t) - - // start simd server - proc := f.SDStart() - t.Cleanup(func() { proc.Stop(false) }) - - f.ValidateGenesis() - - // Cleanup testing directories - f.Cleanup() -} diff --git a/tests/gobash.go b/tests/gobash.go deleted file mode 100644 index 3d4166dd30d6..000000000000 --- a/tests/gobash.go +++ /dev/null @@ -1,114 +0,0 @@ -package tests - -import ( - "fmt" - "io" - "io/ioutil" - "strings" - "testing" - - "github.com/stretchr/testify/require" -) - -// ExecuteT executes the command, pipes any input to STDIN and return STDOUT, -// logging STDOUT/STDERR to t. -func ExecuteT(t *testing.T, cmd, input string) (stdout, stderr string) { - t.Log("Running", cmd) - - // split cmd to name and args - split := strings.Split(cmd, " ") - require.True(t, len(split) > 0, "no command provided") - name, args := split[0], []string(nil) - if len(split) > 1 { - args = split[1:] - } - - proc, err := StartProcess("", name, args) - require.NoError(t, err) - - // if input is provided, pass it to STDIN and close the pipe - if input != "" { - _, err = io.WriteString(proc.StdinPipe, input) - require.NoError(t, err) - proc.StdinPipe.Close() - } - - outbz, errbz, err := proc.ReadAll() - if err != nil { - fmt.Println("Err on proc.ReadAll()", err, args) - } - - proc.Wait() - - if len(outbz) > 0 { - t.Log("Stdout:", string(outbz)) - } - - if len(errbz) > 0 { - t.Log("Stderr:", string(errbz)) - } - - stdout = strings.Trim(string(outbz), "\n") - stderr = strings.Trim(string(errbz), "\n") - - return stdout, stderr -} - -// Execute the command, launch goroutines to log stdout/err to t. -// Caller should wait for .Wait() or .Stop() to terminate. -func GoExecuteT(t *testing.T, cmd string) (proc *Process) { - t.Log("Running", cmd) - - // Split cmd to name and args. - split := strings.Split(cmd, " ") - require.True(t, len(split) > 0, "no command provided") - name, args := split[0], []string(nil) - if len(split) > 1 { - args = split[1:] - } - - // Start process. - proc, err := StartProcess("", name, args) - require.NoError(t, err) - return proc -} - -// Same as GoExecuteT but spawns a go routine to ReadAll off stdout. -func GoExecuteTWithStdout(t *testing.T, cmd string) (proc *Process) { - t.Log("Running", cmd) - - // Split cmd to name and args. - split := strings.Split(cmd, " ") - require.True(t, len(split) > 0, "no command provided") - name, args := split[0], []string(nil) - if len(split) > 1 { - args = split[1:] - } - - // Start process. - proc, err := CreateProcess("", name, args) - require.NoError(t, err) - - // Without this, the test halts ?! - // (theory: because stdout and/or err aren't connected to anything the process halts) - go func(proc *Process) { - _, err := ioutil.ReadAll(proc.StdoutPipe) - if err != nil { - fmt.Println("-------------ERR-----------------------", err) - return - } - }(proc) - - go func(proc *Process) { - _, err := ioutil.ReadAll(proc.StderrPipe) - if err != nil { - fmt.Println("-------------ERR-----------------------", err) - return - } - }(proc) - - err = proc.Cmd.Start() - require.NoError(t, err) - proc.Pid = proc.Cmd.Process.Pid - return proc -} diff --git a/tests/process.go b/tests/process.go deleted file mode 100644 index 08f975cf8291..000000000000 --- a/tests/process.go +++ /dev/null @@ -1,117 +0,0 @@ -package tests - -import ( - "io" - "io/ioutil" - "os" - "os/exec" - "time" -) - -// execution process -type Process struct { - ExecPath string - Args []string - Pid int - StartTime time.Time - EndTime time.Time - Cmd *exec.Cmd `json:"-"` - ExitState *os.ProcessState `json:"-"` - StdinPipe io.WriteCloser `json:"-"` - StdoutPipe io.ReadCloser `json:"-"` - StderrPipe io.ReadCloser `json:"-"` -} - -// dir: The working directory. If "", os.Getwd() is used. -// name: Command name -// args: Args to command. (should not include name) -func StartProcess(dir string, name string, args []string) (*Process, error) { - proc, err := CreateProcess(dir, name, args) - if err != nil { - return nil, err - } - // cmd start - if err := proc.Cmd.Start(); err != nil { - return nil, err - } - proc.Pid = proc.Cmd.Process.Pid - - return proc, nil -} - -// Same as StartProcess but doesn't start the process -func CreateProcess(dir string, name string, args []string) (*Process, error) { - var cmd = exec.Command(name, args...) // is not yet started. - // cmd dir - if dir == "" { - pwd, err := os.Getwd() - if err != nil { - panic(err) - } - cmd.Dir = pwd - } else { - cmd.Dir = dir - } - // cmd stdin - stdin, err := cmd.StdinPipe() - if err != nil { - return nil, err - } - - stdout, err := cmd.StdoutPipe() - if err != nil { - return nil, err - } - - stderr, err := cmd.StderrPipe() - if err != nil { - return nil, err - } - - proc := &Process{ - ExecPath: name, - Args: args, - StartTime: time.Now(), - Cmd: cmd, - ExitState: nil, - StdinPipe: stdin, - StdoutPipe: stdout, - StderrPipe: stderr, - } - return proc, nil -} - -// stop the process -func (proc *Process) Stop(kill bool) error { - if kill { - // fmt.Printf("Killing process %v\n", proc.Cmd.Process) - return proc.Cmd.Process.Kill() - } - return proc.Cmd.Process.Signal(os.Interrupt) -} - -// wait for the process -func (proc *Process) Wait() { - err := proc.Cmd.Wait() - if err != nil { - // fmt.Printf("Process exit: %v\n", err) - if exitError, ok := err.(*exec.ExitError); ok { - proc.ExitState = exitError.ProcessState - } - } - proc.ExitState = proc.Cmd.ProcessState - proc.EndTime = time.Now() // TODO make this goroutine-safe -} - -// ReadAll calls ioutil.ReadAll on the StdoutPipe and StderrPipe. -func (proc *Process) ReadAll() (stdout []byte, stderr []byte, err error) { - outbz, err := ioutil.ReadAll(proc.StdoutPipe) - if err != nil { - return nil, nil, err - } - errbz, err := ioutil.ReadAll(proc.StderrPipe) - if err != nil { - return nil, nil, err - } - return outbz, errbz, nil -} diff --git a/tests/util.go b/tests/util.go deleted file mode 100644 index 129c5d12a227..000000000000 --- a/tests/util.go +++ /dev/null @@ -1,120 +0,0 @@ -package tests - -import ( - "fmt" - "net/http" - "time" - - rpchttp "github.com/tendermint/tendermint/rpc/client/http" - ctypes "github.com/tendermint/tendermint/rpc/core/types" - - "github.com/cosmos/cosmos-sdk/codec" -) - -// Wait for N tendermint blocks to pass using the Tendermint RPC -// on localhost -func WaitForNextNBlocksTM(n int64, port string) { - // get the latest block and wait for n more - url := fmt.Sprintf("http://localhost:%v", port) - cl, err := rpchttp.New(url, "/websocket") - if err != nil { - panic(fmt.Sprintf("failed to create Tendermint HTTP client: %s", err)) - } - - var height int64 - - resBlock, err := cl.Block(nil) - if err != nil || resBlock.Block == nil { - // wait for the first block to exist - WaitForHeightTM(1, port) - height = 1 + n - } else { - height = resBlock.Block.Height + n - } - - waitForHeightTM(height, url) -} - -// Wait for the given height from the Tendermint RPC -// on localhost -func WaitForHeightTM(height int64, port string) { - url := fmt.Sprintf("http://localhost:%v", port) - waitForHeightTM(height, url) -} - -func waitForHeightTM(height int64, url string) { - cl, err := rpchttp.New(url, "/websocket") - if err != nil { - panic(fmt.Sprintf("failed to create Tendermint HTTP client: %s", err)) - } - - for { - // get url, try a few times - var resBlock *ctypes.ResultBlock - var err error - INNER: - for i := 0; i < 5; i++ { - resBlock, err = cl.Block(nil) - if err == nil { - break INNER - } - time.Sleep(time.Millisecond * 200) - } - if err != nil { - panic(err) - } - - if resBlock.Block != nil && resBlock.Block.Height >= height { - return - } - - time.Sleep(time.Millisecond * 100) - } -} - -// wait for tendermint to start by querying tendermint -func WaitForTMStart(port string) { - url := fmt.Sprintf("http://localhost:%v/block", port) - WaitForStart(url) -} - -// WaitForStart waits for the node to start by pinging the url -// every 100ms for 10s until it returns 200. If it takes longer than 5s, -// it panics. -func WaitForStart(url string) { - var err error - - // ping the status endpoint a few times a second - // for a few seconds until we get a good response. - // otherwise something probably went wrong - for i := 0; i < 100; i++ { - time.Sleep(time.Millisecond * 100) - - var res *http.Response - res, err = http.Get(url) // nolint:gosec - if err != nil || res == nil { - continue - } - // body, _ := ioutil.ReadAll(res.Body) - // fmt.Println("BODY", string(body)) - err = res.Body.Close() - if err != nil { - panic(err) - } - - if res.StatusCode == http.StatusOK { - // good! - return - } - } - // still haven't started up?! panic! - panic(err) -} - -var cdc = codec.New() - -func init() { - ctypes.RegisterAmino(cdc.Amino) -} - -//DONTCOVER diff --git a/testutil/network/network.go b/testutil/network/network.go index 46e27bf08143..9c6621bc0f3d 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -334,13 +334,6 @@ func New(t *testing.T, cfg Config) *Network { return network } -// WaitForHeight performs a blocking check where it waits for a block to be -// committed after a given block. If that height is not reached within a timeout, -// an error is returned. Regardless, the latest height queried is returned. -func (n *Network) WaitForHeight(h int64) (int64, error) { - return n.WaitForHeightWithTimeout(h, 10*time.Second) -} - // LatestHeight returns the latest height of the network or an error if the // query fails or no validators exist. func (n *Network) LatestHeight() (int64, error) { @@ -356,6 +349,13 @@ func (n *Network) LatestHeight() (int64, error) { return status.SyncInfo.LatestBlockHeight, nil } +// WaitForHeight performs a blocking check where it waits for a block to be +// committed after a given block. If that height is not reached within a timeout, +// an error is returned. Regardless, the latest height queried is returned. +func (n *Network) WaitForHeight(h int64) (int64, error) { + return n.WaitForHeightWithTimeout(h, 10*time.Second) +} + // WaitForHeightWithTimeout is the same as WaitForHeight except the caller can // provide a custom timeout. func (n *Network) WaitForHeightWithTimeout(h int64, t time.Duration) (int64, error) { @@ -386,6 +386,22 @@ func (n *Network) WaitForHeightWithTimeout(h int64, t time.Duration) (int64, err } } +// WaitForNextBlock waits for the next block to be committed, returning an error +// upon failure. +func (n *Network) WaitForNextBlock() error { + lastBlock, err := n.LatestHeight() + if err != nil { + return err + } + + _, err = n.WaitForHeight(lastBlock + 1) + if err != nil { + return err + } + + return err +} + // Cleanup removes the root testing (temporary) directory and stops both the // Tendermint and API services. It allows other callers to create and start // test networks. This method must be called when a test is finished, typically diff --git a/x/auth/client/cli/cli_test.go b/x/auth/client/cli/cli_test.go index 714050dfdd89..97949ba3a6f8 100644 --- a/x/auth/client/cli/cli_test.go +++ b/x/auth/client/cli/cli_test.go @@ -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 } diff --git a/x/bank/client/testutil/cli_helpers.go b/x/bank/client/testutil/cli_helpers.go index b2fd1e22851d..b79ca349dc28 100644 --- a/x/bank/client/testutil/cli_helpers.go +++ b/x/bank/client/testutil/cli_helpers.go @@ -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 -} diff --git a/x/distribution/client/cli/tx.go b/x/distribution/client/cli/tx.go index 576f1b84b491..4df081a99f90 100644 --- a/x/distribution/client/cli/tx.go +++ b/x/distribution/client/cli/tx.go @@ -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 } diff --git a/x/gov/client/cli/cli_test.go b/x/gov/client/cli/cli_test.go index 3d7057043577..0b707fe012f9 100644 --- a/x/gov/client/cli/cli_test.go +++ b/x/gov/client/cli/cli_test.go @@ -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) - - // 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() +type IntegrationTestSuite struct { + suite.Suite + + 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)) } diff --git a/x/gov/client/cli/parse.go b/x/gov/client/cli/parse.go index 6bdfe0463c4d..90066b645abe 100644 --- a/x/gov/client/cli/parse.go +++ b/x/gov/client/cli/parse.go @@ -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) diff --git a/x/gov/client/cli/parse_test.go b/x/gov/client/cli/parse_test.go index 37e41f57c56d..40f37b2024e2 100644 --- a/x/gov/client/cli/parse_test.go +++ b/x/gov/client/cli/parse_test.go @@ -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) diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index d7145ec6b4ac..e581ec39f45f 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -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) }, } diff --git a/x/gov/client/testutil/helpers.go b/x/gov/client/testutil/helpers.go deleted file mode 100644 index c39486389382..000000000000 --- a/x/gov/client/testutil/helpers.go +++ /dev/null @@ -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) -} diff --git a/x/mint/client/cli/cli_test.go b/x/mint/client/cli/cli_test.go index 18eb708a4557..674dee27bd9d 100644 --- a/x/mint/client/cli/cli_test.go +++ b/x/mint/client/cli/cli_test.go @@ -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 + + cfg testnet.Config + network *testnet.Network +} + +func (s *IntegrationTestSuite) SetupSuite() { + s.T().Log("setting up integration test suite") + + cfg := testnet.DefaultConfig() + genesisState := cfg.GenesisState + cfg.NumValidators = 1 - proc := f.SDStart() - t.Cleanup(func() { proc.Stop(false) }) + var mintData minttypes.GenesisState + s.Require().NoError(cfg.Codec.UnmarshalJSON(genesisState[minttypes.ModuleName], &mintData)) - params := testutil.QueryMintingParams(f) - require.NotEmpty(t, params) + inflation := sdk.MustNewDecFromStr("1.0") + mintData.Minter.Inflation = inflation + mintData.Params.InflationMin = inflation + mintData.Params.InflationMax = inflation - inflation := testutil.QueryInflation(f) - require.False(t, inflation.IsZero()) + 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())) + }) + } +} - annualProvisions := testutil.QueryAnnualProvisions(f) - require.False(t, annualProvisions.IsZero()) +func TestIntegrationTestSuite(t *testing.T) { + suite.Run(t, new(IntegrationTestSuite)) } diff --git a/x/mint/client/cli/query.go b/x/mint/client/cli/query.go index f5c8578aa37e..7390cbce38a3 100644 --- a/x/mint/client/cli/query.go +++ b/x/mint/client/cli/query.go @@ -52,7 +52,7 @@ func GetCmdQueryParams() *cobra.Command { return err } - return clientCtx.PrintOutput(res.Params) + return clientCtx.PrintOutput(res.GetParams()) }, } diff --git a/x/mint/client/testutil/helpers.go b/x/mint/client/testutil/helpers.go deleted file mode 100644 index 1d59ff6b925e..000000000000 --- a/x/mint/client/testutil/helpers.go +++ /dev/null @@ -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), ¶ms)) - 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 -} diff --git a/x/mint/module.go b/x/mint/module.go index 013ca97124da..013daeac70c2 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -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. diff --git a/x/params/module.go b/x/params/module.go index 148113e60ecd..6c0f8323f30c 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -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) } diff --git a/x/slashing/client/testutil/helpers.go b/x/slashing/client/testutil/helpers.go deleted file mode 100644 index b28f66f478e1..000000000000 --- a/x/slashing/client/testutil/helpers.go +++ /dev/null @@ -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), ¶ms) - require.NoError(f.T, err) - return params -} diff --git a/x/staking/client/cli/cli_test.go b/x/staking/client/cli/cli_test.go index 20ce2a74b35b..b6e44511d295 100644 --- a/x/staking/client/cli/cli_test.go +++ b/x/staking/client/cli/cli_test.go @@ -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. - - t.Parallel() - f := cli.InitFixtures(t) +type IntegrationTestSuite struct { + suite.Suite + + 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()) + } + }) + } +} - // 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() +func TestIntegrationTestSuite(t *testing.T) { + suite.Run(t, new(IntegrationTestSuite)) } diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index ed71db655f7d..0e0d7e9c76c2 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -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) }, } diff --git a/x/staking/client/testutil/helpers.go b/x/staking/client/testutil/helpers.go deleted file mode 100644 index 7e10b4fda717..000000000000 --- a/x/staking/client/testutil/helpers.go +++ /dev/null @@ -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), ¶ms)) - - 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 -} diff --git a/x/staking/module.go b/x/staking/module.go index db9476f2606f..252ad1e529f4 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -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. diff --git a/x/upgrade/client/cli/tx.go b/x/upgrade/client/cli/tx.go index 437a998d924d..eaa85cfc5148 100644 --- a/x/upgrade/client/cli/tx.go +++ b/x/upgrade/client/cli/tx.go @@ -137,7 +137,7 @@ func NewCmdSubmitCancelUpgradeProposal() *cobra.Command { return err } - return tx.GenerateOrBroadcastTx(clientCtx, msg) + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) }, }