diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cddf3ce52ff3..1d56cc8ea976 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -49,10 +49,14 @@ jobs: file: ./coverage.txt # optional fail_ci_if_error: true if: "env.GIT_DIFF != ''" - cli-test: + integration-tests: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + - name: build-sim + run: | + make build-sim + if: "env.GIT_DIFF != ''" - name: cli-test run: | - make cli-test \ No newline at end of file + make test-integration diff --git a/Makefile b/Makefile index 618e46f639f1..dae876965a3a 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,6 @@ HTTPS_GIT := https://github.com/cosmos/cosmos-sdk.git DOCKER_BUF := docker run -v $(shell pwd):/workspace --workdir /workspace bufbuild/buf export GO111MODULE = on -export BUILDDIR # The below include contains the tools and runsim targets. include contrib/devtools/Makefile @@ -28,13 +27,9 @@ build: go.sum @go build -mod=readonly ./... build-sim: go.sum -ifeq ($(OS),Windows_NT) - go build -mod=readonly $(BUILD_FLAGS) -o build/simd.exe ./simapp/cmd/simd - go build -mod=readonly $(BUILD_FLAGS) -o build/simcli.exe ./simapp/cmd/simcli -else - go build -mod=readonly $(BUILD_FLAGS) -o build/simd ./simapp/cmd/simd - go build -mod=readonly $(BUILD_FLAGS) -o build/simcli ./simapp/cmd/simcli -endif + mkdir -p $(BUILDDIR) + go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR) ./simapp/cmd/simd + go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR) ./simapp/cmd/simcli .PHONY: \ build \ @@ -128,6 +123,10 @@ test-unit: test-race: @VERSION=$(VERSION) go test -mod=readonly -race $(PACKAGES_NOSIMULATION) +test-integration: build-sim + BUILDDIR=$(BUILDDIR) go test -mod=readonly -p 4 `go list ./tests/cli/...` -tags=-tags='ledger test_ledger_mock cli_test' + BUILDDIR=$(BUILDDIR) go test -mod=readonly -p 4 `go list ./x/.../client/cli_test/...` -tags=-tags='ledger test_ledger_mock cli_test' + .PHONY: test test-all test-ledger-mock test-ledger test-unit test-race test-sim-nondeterminism: @@ -168,10 +167,6 @@ test-sim-benchmark-invariants: -Enabled=true -NumBlocks=1000 -BlockSize=200 \ -Period=1 -Commit=true -Seed=57 -v -timeout 24h -cli-test: build-sim - @go test -mod=readonly -p 4 `go list ./tests/cli/tests/...` -tags=cli_test -v - @go test -mod=readonly -p 4 `go list ./x/.../client/cli_test/...` -tags=cli_test -v - .PHONY: \ test-sim-nondeterminism \ test-sim-custom-genesis-fast \ @@ -181,7 +176,7 @@ test-sim-custom-genesis-multi-seed \ test-sim-multi-seed-short \ test-sim-multi-seed-long \ test-sim-benchmark-invariants \ -cli-test +test-integration SIM_NUM_BLOCKS ?= 500 SIM_BLOCK_SIZE ?= 200 diff --git a/tests/cli/helpers/constants.go b/tests/cli/constants.go similarity index 93% rename from tests/cli/helpers/constants.go rename to tests/cli/constants.go index c634f3cc5a19..9068d44464ea 100644 --- a/tests/cli/helpers/constants.go +++ b/tests/cli/constants.go @@ -1,4 +1,4 @@ -package helpers +package cli const ( Denom = "stake" diff --git a/tests/cli/tests/doc.go b/tests/cli/doc.go similarity index 83% rename from tests/cli/tests/doc.go rename to tests/cli/doc.go index 84f19f32a526..fca4bd79fd79 100644 --- a/tests/cli/tests/doc.go +++ b/tests/cli/doc.go @@ -1,3 +1,3 @@ -package tests +package cli // package tests runs integration tests which make use of CLI commands. diff --git a/tests/cli/helpers/executors.go b/tests/cli/executors.go similarity index 98% rename from tests/cli/helpers/executors.go rename to tests/cli/executors.go index cb4e011b94b6..3f7e81475332 100644 --- a/tests/cli/helpers/executors.go +++ b/tests/cli/executors.go @@ -1,4 +1,4 @@ -package helpers +package cli import ( "fmt" diff --git a/tests/cli/helpers/fixtures.go b/tests/cli/fixtures.go similarity index 93% rename from tests/cli/helpers/fixtures.go rename to tests/cli/fixtures.go index 4bea6976f281..564514ddc114 100644 --- a/tests/cli/helpers/fixtures.go +++ b/tests/cli/fixtures.go @@ -1,4 +1,4 @@ -package helpers +package cli import ( "io/ioutil" @@ -16,7 +16,9 @@ import ( "github.com/cosmos/cosmos-sdk/simapp" ) -var cdc = std.MakeCodec(simapp.ModuleBasics) +var ( + cdc = std.MakeCodec(simapp.ModuleBasics) +) // Fixtures is used to setup the testing environment type Fixtures struct { @@ -46,7 +48,9 @@ func NewFixtures(t *testing.T) *Fixtures { require.NoError(t, err) buildDir := os.Getenv("BUILDDIR") - require.NotNil(t, buildDir) + if buildDir == "" { + t.Skip("builddir is empty, skipping") + } return &Fixtures{ T: t, diff --git a/tests/cli/helpers/helpers.go b/tests/cli/helpers.go similarity index 98% rename from tests/cli/helpers/helpers.go rename to tests/cli/helpers.go index e2bc3cad6867..eebcc336bcf5 100644 --- a/tests/cli/helpers/helpers.go +++ b/tests/cli/helpers.go @@ -1,4 +1,4 @@ -package helpers +package cli import ( "encoding/json" @@ -19,21 +19,21 @@ import ( ) var ( - totalCoins = sdk.NewCoins( + 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( + 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( + VestingCoins = sdk.NewCoins( sdk.NewCoin(FeeDenom, sdk.TokensFromConsensusPower(500000)), ) ) diff --git a/tests/cli/helpers/init.go b/tests/cli/init.go similarity index 90% rename from tests/cli/helpers/init.go rename to tests/cli/init.go index 5bcd0996d581..3a6efa98ccb7 100644 --- a/tests/cli/helpers/init.go +++ b/tests/cli/init.go @@ -1,4 +1,4 @@ -package helpers +package cli import ( "fmt" @@ -42,10 +42,10 @@ func InitFixtures(t *testing.T) (f *Fixtures) { f.CLIConfig("trust-node", "true") // start an account with tokens - f.AddGenesisAccount(f.KeyAddress(KeyFoo), startCoins) + f.AddGenesisAccount(f.KeyAddress(KeyFoo), StartCoins) f.AddGenesisAccount( - f.KeyAddress(KeyVesting), startCoins, - fmt.Sprintf("--vesting-amount=%s", vestingCoins), + 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()), ) diff --git a/tests/cli/tests/keys_test.go b/tests/cli/keys_test.go similarity index 84% rename from tests/cli/tests/keys_test.go rename to tests/cli/keys_test.go index 3abbf0878f2c..0a6c64ab3d3f 100644 --- a/tests/cli/tests/keys_test.go +++ b/tests/cli/keys_test.go @@ -1,36 +1,35 @@ // +build cli_test -package tests +package cli_test import ( "fmt" "testing" + "github.com/cosmos/cosmos-sdk/tests/cli" "github.com/stretchr/testify/require" - - "github.com/cosmos/cosmos-sdk/tests/cli/helpers" ) func TestCLIKeysAddMultisig(t *testing.T) { t.Parallel() - f := helpers.InitFixtures(t) + f := cli.InitFixtures(t) // key names order does not matter f.KeysAdd("msig1", "--multisig-threshold=2", - fmt.Sprintf("--multisig=%s,%s", helpers.KeyBar, helpers.KeyBaz)) + 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", helpers.KeyBaz, helpers.KeyBar)) + 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", helpers.KeyBar, helpers.KeyBaz), + fmt.Sprintf("--multisig=%s,%s", cli.KeyBar, cli.KeyBaz), "--nosort") f.KeysAdd("msig4", "--multisig-threshold=2", - fmt.Sprintf("--multisig=%s,%s", helpers.KeyBaz, helpers.KeyBar), + fmt.Sprintf("--multisig=%s,%s", cli.KeyBaz, cli.KeyBar), "--nosort") require.NotEqual(t, f.KeysShow("msig3").Address, f.KeysShow("msig4").Address) @@ -40,7 +39,7 @@ func TestCLIKeysAddMultisig(t *testing.T) { func TestCLIKeysAddRecover(t *testing.T) { t.Parallel() - f := helpers.InitFixtures(t) + f := cli.InitFixtures(t) exitSuccess, _, _ := f.KeysAddRecover("empty-mnemonic", "") require.False(t, exitSuccess) @@ -55,7 +54,7 @@ func TestCLIKeysAddRecover(t *testing.T) { func TestCLIKeysAddRecoverHDPath(t *testing.T) { t.Parallel() - f := helpers.InitFixtures(t) + 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()) diff --git a/tests/cli/simd_test.go b/tests/cli/simd_test.go new file mode 100644 index 000000000000..b0860fdf0d4f --- /dev/null +++ b/tests/cli/simd_test.go @@ -0,0 +1,128 @@ +package cli_test + +import ( + "fmt" + "github.com/cosmos/cosmos-sdk/std" + "github.com/cosmos/cosmos-sdk/tests/cli" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/stretchr/testify/require" + tmtypes "github.com/tendermint/tendermint/types" + "io/ioutil" + "path/filepath" + "testing" +) + +func TestSimdCollectGentxs(t *testing.T) { + 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) + + // Configure json output + f.CLIConfig("output", "json") + + // 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 TestSimdAddGenesisAccount(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) + + // Configure json output + f.CLIConfig("output", "json") + + // 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 := std.NewAppCodec(f.Cdc) + + accounts := auth.GetGenesisStateFromAppState(appCodec, genesisState).Accounts + balances := bank.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 TestValidateGenesis(t *testing.T) { + t.Parallel() + f := cli.InitFixtures(t) + + // start simd server + proc := f.SDStart() + defer proc.Stop(false) + + f.ValidateGenesis() + + // Cleanup testing directories + f.Cleanup() +} diff --git a/x/bank/client/cli_test/helpers.go b/x/bank/client/cli_test/helpers.go index 525e1eb50266..5722e5abed6a 100644 --- a/x/bank/client/cli_test/helpers.go +++ b/x/bank/client/cli_test/helpers.go @@ -8,23 +8,23 @@ import ( clientkeys "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/tests" - "github.com/cosmos/cosmos-sdk/tests/cli/helpers" + "github.com/cosmos/cosmos-sdk/tests/cli" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" ) // TxSend is simcli tx send -func TxSend(f *helpers.Fixtures, from string, to sdk.AccAddress, amount sdk.Coin, flags ...string) (bool, string, string) { +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.SimcliBinary, from, to, amount, f.Flags()) - return helpers.ExecuteWriteRetStdStreams(f.T, helpers.AddFlags(cmd, flags), clientkeys.DefaultKeyPass) + return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass) } // QueryAccount is simcli query account -func QueryAccount(f *helpers.Fixtures, address sdk.AccAddress, flags ...string) auth.BaseAccount { +func QueryAccount(f *cli.Fixtures, address sdk.AccAddress, flags ...string) auth.BaseAccount { cmd := fmt.Sprintf("%s query account %s %v", f.SimcliBinary, address, f.Flags()) - out, _ := tests.ExecuteT(f.T, helpers.AddFlags(cmd, flags), "") + out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "") var initRes map[string]json.RawMessage err := json.Unmarshal([]byte(out), &initRes) @@ -40,9 +40,9 @@ func QueryAccount(f *helpers.Fixtures, address sdk.AccAddress, flags ...string) // QueryBalances executes the bank query balances command for a given address and // flag set. -func QueryBalances(f *helpers.Fixtures, address sdk.AccAddress, flags ...string) sdk.Coins { +func QueryBalances(f *cli.Fixtures, address sdk.AccAddress, flags ...string) sdk.Coins { cmd := fmt.Sprintf("%s query bank balances %s %v", f.SimcliBinary, address, f.Flags()) - out, _ := tests.ExecuteT(f.T, helpers.AddFlags(cmd, flags), "") + out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "") var balances sdk.Coins diff --git a/x/bank/client/cli_test/send_test.go b/x/bank/client/cli_test/send_test.go index 85fca27d92f0..153f6245d86f 100644 --- a/x/bank/client/cli_test/send_test.go +++ b/x/bank/client/cli_test/send_test.go @@ -9,111 +9,111 @@ import ( "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/tests" - "github.com/cosmos/cosmos-sdk/tests/cli/helpers" + "github.com/cosmos/cosmos-sdk/tests/cli" sdk "github.com/cosmos/cosmos-sdk/types" bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/cli_test" ) func TestCLISend(t *testing.T) { t.Parallel() - f := helpers.InitFixtures(t) + f := cli.InitFixtures(t) // start simd server proc := f.SDStart() defer proc.Stop(false) // Save key addresses for later uspackage testse - fooAddr := f.KeyAddress(helpers.KeyFoo) - barAddr := f.KeyAddress(helpers.KeyBar) + fooAddr := f.KeyAddress(cli.KeyFoo) + barAddr := f.KeyAddress(cli.KeyBar) startTokens := sdk.TokensFromConsensusPower(50) - require.Equal(t, startTokens, bankcli.QueryBalances(f, fooAddr).AmountOf(helpers.Denom)) + require.Equal(t, startTokens, bankcli.QueryBalances(f, fooAddr).AmountOf(cli.Denom)) sendTokens := sdk.TokensFromConsensusPower(10) // It does not allow to send in offline mode - success, _, stdErr := bankcli.TxSend(f, helpers.KeyFoo, barAddr, sdk.NewCoin(helpers.Denom, sendTokens), "-y", "--offline") + success, _, stdErr := bankcli.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "-y", "--offline") require.Contains(t, stdErr, "no RPC client is defined in offline mode") require.False(f.T, success) tests.WaitForNextNBlocksTM(1, f.Port) // Send some tokens from one account to the other - bankcli.TxSend(f, helpers.KeyFoo, barAddr, sdk.NewCoin(helpers.Denom, sendTokens), "-y") + bankcli.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "-y") tests.WaitForNextNBlocksTM(1, f.Port) // Ensure account balances match expected - require.Equal(t, sendTokens, bankcli.QueryBalances(f, barAddr).AmountOf(helpers.Denom)) - require.Equal(t, startTokens.Sub(sendTokens), bankcli.QueryBalances(f, fooAddr).AmountOf(helpers.Denom)) + require.Equal(t, sendTokens, bankcli.QueryBalances(f, barAddr).AmountOf(cli.Denom)) + require.Equal(t, startTokens.Sub(sendTokens), bankcli.QueryBalances(f, fooAddr).AmountOf(cli.Denom)) // Test --dry-run - success, _, _ = bankcli.TxSend(f, helpers.KeyFoo, barAddr, sdk.NewCoin(helpers.Denom, sendTokens), "--dry-run") + success, _, _ = bankcli.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "--dry-run") require.True(t, success) // Test --generate-only success, stdout, stderr := bankcli.TxSend( - f, fooAddr.String(), barAddr, sdk.NewCoin(helpers.Denom, sendTokens), "--generate-only=true", + f, fooAddr.String(), barAddr, sdk.NewCoin(cli.Denom, sendTokens), "--generate-only=true", ) require.Empty(t, stderr) require.True(t, success) - msg := helpers.UnmarshalStdTx(f.T, f.Cdc, stdout) + msg := cli.UnmarshalStdTx(f.T, f.Cdc, stdout) t.Log(msg) require.NotZero(t, msg.Fee.Gas) require.Len(t, msg.Msgs, 1) require.Len(t, msg.GetSignatures(), 0) // Check state didn't change - require.Equal(t, startTokens.Sub(sendTokens), bankcli.QueryBalances(f, fooAddr).AmountOf(helpers.Denom)) + require.Equal(t, startTokens.Sub(sendTokens), bankcli.QueryBalances(f, fooAddr).AmountOf(cli.Denom)) // test autosequencing - bankcli.TxSend(f, helpers.KeyFoo, barAddr, sdk.NewCoin(helpers.Denom, sendTokens), "-y") + bankcli.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "-y") tests.WaitForNextNBlocksTM(1, f.Port) // Ensure account balances match expected - require.Equal(t, sendTokens.MulRaw(2), bankcli.QueryBalances(f, barAddr).AmountOf(helpers.Denom)) - require.Equal(t, startTokens.Sub(sendTokens.MulRaw(2)), bankcli.QueryBalances(f, fooAddr).AmountOf(helpers.Denom)) + require.Equal(t, sendTokens.MulRaw(2), bankcli.QueryBalances(f, barAddr).AmountOf(cli.Denom)) + require.Equal(t, startTokens.Sub(sendTokens.MulRaw(2)), bankcli.QueryBalances(f, fooAddr).AmountOf(cli.Denom)) // test memo - bankcli.TxSend(f, helpers.KeyFoo, barAddr, sdk.NewCoin(helpers.Denom, sendTokens), "--memo='testmemo'", "-y") + bankcli.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "--memo='testmemo'", "-y") tests.WaitForNextNBlocksTM(1, f.Port) // Ensure account balances match expected - require.Equal(t, sendTokens.MulRaw(3), bankcli.QueryBalances(f, barAddr).AmountOf(helpers.Denom)) - require.Equal(t, startTokens.Sub(sendTokens.MulRaw(3)), bankcli.QueryBalances(f, fooAddr).AmountOf(helpers.Denom)) + require.Equal(t, sendTokens.MulRaw(3), bankcli.QueryBalances(f, barAddr).AmountOf(cli.Denom)) + require.Equal(t, startTokens.Sub(sendTokens.MulRaw(3)), bankcli.QueryBalances(f, fooAddr).AmountOf(cli.Denom)) f.Cleanup() } func TestCLIMinimumFees(t *testing.T) { t.Parallel() - f := helpers.InitFixtures(t) + f := cli.InitFixtures(t) // start simd server with minimum fees minGasPrice, _ := sdk.NewDecFromStr("0.000006") fees := fmt.Sprintf( "--minimum-gas-prices=%s,%s", - sdk.NewDecCoinFromDec(helpers.FeeDenom, minGasPrice), - sdk.NewDecCoinFromDec(helpers.Fee2Denom, minGasPrice), + sdk.NewDecCoinFromDec(cli.FeeDenom, minGasPrice), + sdk.NewDecCoinFromDec(cli.Fee2Denom, minGasPrice), ) proc := f.SDStart(fees) defer proc.Stop(false) - barAddr := f.KeyAddress(helpers.KeyBar) + barAddr := f.KeyAddress(cli.KeyBar) // Send a transaction that will get rejected - success, stdOut, _ := bankcli.TxSend(f, helpers.KeyFoo, barAddr, sdk.NewInt64Coin(helpers.Fee2Denom, 10), "-y") + success, stdOut, _ := bankcli.TxSend(f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.Fee2Denom, 10), "-y") require.Contains(t, stdOut, "insufficient fees") require.True(f.T, success) tests.WaitForNextNBlocksTM(1, f.Port) // Ensure tx w/ correct fees pass - txFees := fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(helpers.FeeDenom, 2)) - success, _, _ = bankcli.TxSend(f, helpers.KeyFoo, barAddr, sdk.NewInt64Coin(helpers.Fee2Denom, 10), txFees, "-y") + txFees := fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(cli.FeeDenom, 2)) + success, _, _ = bankcli.TxSend(f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.Fee2Denom, 10), txFees, "-y") require.True(f.T, success) tests.WaitForNextNBlocksTM(1, f.Port) // Ensure tx w/ improper fees fails - txFees = fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(helpers.FeeDenom, 1)) - success, _, _ = bankcli.TxSend(f, helpers.KeyFoo, barAddr, sdk.NewInt64Coin(helpers.FooDenom, 10), txFees, "-y") + txFees = fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(cli.FeeDenom, 1)) + success, _, _ = bankcli.TxSend(f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.FooDenom, 10), txFees, "-y") require.Contains(t, stdOut, "insufficient fees") require.True(f.T, success) @@ -123,20 +123,20 @@ func TestCLIMinimumFees(t *testing.T) { func TestCLIGasPrices(t *testing.T) { t.Parallel() - f := helpers.InitFixtures(t) + f := cli.InitFixtures(t) // start simd server with minimum fees minGasPrice, _ := sdk.NewDecFromStr("0.000006") - proc := f.SDStart(fmt.Sprintf("--minimum-gas-prices=%s", sdk.NewDecCoinFromDec(helpers.FeeDenom, minGasPrice))) + proc := f.SDStart(fmt.Sprintf("--minimum-gas-prices=%s", sdk.NewDecCoinFromDec(cli.FeeDenom, minGasPrice))) defer proc.Stop(false) - barAddr := f.KeyAddress(helpers.KeyBar) + barAddr := f.KeyAddress(cli.KeyBar) // insufficient gas prices (tx fails) badGasPrice, _ := sdk.NewDecFromStr("0.000003") success, stdOut, _ := bankcli.TxSend( - f, helpers.KeyFoo, barAddr, sdk.NewInt64Coin(helpers.FooDenom, 50), - fmt.Sprintf("--gas-prices=%s", sdk.NewDecCoinFromDec(helpers.FeeDenom, badGasPrice)), "-y") + f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.FooDenom, 50), + fmt.Sprintf("--gas-prices=%s", sdk.NewDecCoinFromDec(cli.FeeDenom, badGasPrice)), "-y") require.Contains(t, stdOut, "insufficient fees") require.True(t, success) @@ -145,8 +145,8 @@ func TestCLIGasPrices(t *testing.T) { // sufficient gas prices (tx passes) success, _, _ = bankcli.TxSend( - f, helpers.KeyFoo, barAddr, sdk.NewInt64Coin(helpers.FooDenom, 50), - fmt.Sprintf("--gas-prices=%s", sdk.NewDecCoinFromDec(helpers.FeeDenom, minGasPrice)), "-y") + f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.FooDenom, 50), + fmt.Sprintf("--gas-prices=%s", sdk.NewDecCoinFromDec(cli.FeeDenom, minGasPrice)), "-y") require.True(t, success) // wait for a block confirmation @@ -157,36 +157,36 @@ func TestCLIGasPrices(t *testing.T) { func TestCLIFeesDeduction(t *testing.T) { t.Parallel() - f := helpers.InitFixtures(t) + f := cli.InitFixtures(t) // start simd server with minimum fees minGasPrice, _ := sdk.NewDecFromStr("0.000006") - proc := f.SDStart(fmt.Sprintf("--minimum-gas-prices=%s", sdk.NewDecCoinFromDec(helpers.FeeDenom, minGasPrice))) + proc := f.SDStart(fmt.Sprintf("--minimum-gas-prices=%s", sdk.NewDecCoinFromDec(cli.FeeDenom, minGasPrice))) defer proc.Stop(false) // Save key addresses for later use - fooAddr := f.KeyAddress(helpers.KeyFoo) - barAddr := f.KeyAddress(helpers.KeyBar) + fooAddr := f.KeyAddress(cli.KeyFoo) + barAddr := f.KeyAddress(cli.KeyBar) - fooAmt := bankcli.QueryBalances(f, fooAddr).AmountOf(helpers.FooDenom) + fooAmt := bankcli.QueryBalances(f, fooAddr).AmountOf(cli.FooDenom) // test simulation success, _, _ := bankcli.TxSend( - f, helpers.KeyFoo, barAddr, sdk.NewInt64Coin(helpers.FooDenom, 1000), - fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(helpers.FeeDenom, 2)), "--dry-run") + f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.FooDenom, 1000), + fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(cli.FeeDenom, 2)), "--dry-run") require.True(t, success) // Wait for a block tests.WaitForNextNBlocksTM(1, f.Port) // ensure state didn't change - require.Equal(t, fooAmt.Int64(), bankcli.QueryBalances(f, fooAddr).AmountOf(helpers.FooDenom).Int64()) + require.Equal(t, fooAmt.Int64(), bankcli.QueryBalances(f, fooAddr).AmountOf(cli.FooDenom).Int64()) // insufficient funds (coins + fees) tx fails largeCoins := sdk.TokensFromConsensusPower(10000000) success, stdOut, _ := bankcli.TxSend( - f, helpers.KeyFoo, barAddr, sdk.NewCoin(helpers.FooDenom, largeCoins), - fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(helpers.FeeDenom, 2)), "-y") + f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.FooDenom, largeCoins), + fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(cli.FeeDenom, 2)), "-y") require.Contains(t, stdOut, "insufficient funds") require.True(t, success) @@ -194,12 +194,12 @@ func TestCLIFeesDeduction(t *testing.T) { tests.WaitForNextNBlocksTM(1, f.Port) // ensure state didn't change - require.Equal(t, fooAmt.Int64(), bankcli.QueryBalances(f, fooAddr).AmountOf(helpers.FooDenom).Int64()) + require.Equal(t, fooAmt.Int64(), bankcli.QueryBalances(f, fooAddr).AmountOf(cli.FooDenom).Int64()) // test success (transfer = coins + fees) success, _, _ = bankcli.TxSend( - f, helpers.KeyFoo, barAddr, sdk.NewInt64Coin(helpers.FooDenom, 500), - fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(helpers.FeeDenom, 2)), "-y") + f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.FooDenom, 500), + fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(cli.FeeDenom, 2)), "-y") require.True(t, success) f.Cleanup() diff --git a/x/distribution/client/cli_test/distr_test.go b/x/distribution/client/cli_test/distr_test.go new file mode 100644 index 000000000000..e3dbc4b516bc --- /dev/null +++ b/x/distribution/client/cli_test/distr_test.go @@ -0,0 +1,55 @@ +package cli_test + +import ( + "github.com/cosmos/cosmos-sdk/tests/cli" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + tmtypes "github.com/tendermint/tendermint/types" + + sdk "github.com/cosmos/cosmos-sdk/types" + distrcli "github.com/cosmos/cosmos-sdk/x/distribution/client/cli_test" + "github.com/cosmos/cosmos-sdk/x/mint" +) + +func TestCliWithdrawRewards(t *testing.T) { + t.Parallel() + f := cli.InitFixtures(t) + + genesisState := f.GenesisState() + inflationMin := sdk.MustNewDecFromStr("1.0") + var mintData mint.GenesisState + f.Cdc.UnmarshalJSON(genesisState[mint.ModuleName], &mintData) + mintData.Minter.Inflation = inflationMin + mintData.Params.InflationMin = inflationMin + mintData.Params.InflationMax = sdk.MustNewDecFromStr("1.0") + mintDataBz, err := f.Cdc.MarshalJSON(mintData) + require.NoError(t, err) + genesisState[mint.ModuleName] = mintDataBz + + genFile := filepath.Join(f.SimdHome, "config", "genesis.json") + genDoc, err := tmtypes.GenesisDocFromFile(genFile) + require.NoError(t, err) + genDoc.AppState, err = f.Cdc.MarshalJSON(genesisState) + require.NoError(t, genDoc.SaveAs(genFile)) + + // start simd server + proc := f.SDStart() + defer proc.Stop(false) + + fooAddr := f.KeyAddress(cli.KeyFoo) + rewards := distrcli.QueryRewards(f, fooAddr) + require.Equal(t, 1, len(rewards.Rewards)) + require.NotNil(t, rewards.Total) + + fooVal := sdk.ValAddress(fooAddr) + success := distrcli.TxWithdrawRewards(f, fooVal, fooAddr.String(), "-y") + require.True(t, success) + + rewards = distrcli.QueryRewards(f, fooAddr) + require.Equal(t, 1, len(rewards.Rewards)) + + require.Nil(t, rewards.Total) + f.Cleanup() +} diff --git a/x/distribution/client/cli_test/helpers.go b/x/distribution/client/cli_test/helpers.go new file mode 100644 index 000000000000..5322165f4c1c --- /dev/null +++ b/x/distribution/client/cli_test/helpers.go @@ -0,0 +1,30 @@ +package cli + +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" + "github.com/cosmos/cosmos-sdk/x/distribution" +) + +func TxWithdrawRewards(f *cli.Fixtures, valAddr sdk.ValAddress, from string, flags ...string) bool { + cmd := fmt.Sprintf("%s tx distribution withdraw-rewards %s %v --keyring-backend=test --from=%s", f.SimcliBinary, valAddr, f.Flags(), from) + return cli.ExecuteWrite(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass) +} + +// QueryRewards returns the rewards of a delegator +func QueryRewards(f *cli.Fixtures, delAddr sdk.AccAddress, flags ...string) distribution.QueryDelegatorTotalRewardsResponse { + cmd := fmt.Sprintf("%s query distribution rewards %s %s", f.SimcliBinary, delAddr, f.Flags()) + res, errStr := tests.ExecuteT(f.T, cmd, "") + require.Empty(f.T, errStr) + + var rewards distribution.QueryDelegatorTotalRewardsResponse + err := f.Cdc.UnmarshalJSON([]byte(res), &rewards) + require.NoError(f.T, err) + return rewards +} diff --git a/x/staking/client/cli_test/helpers.go b/x/staking/client/cli_test/helpers.go index 5d772591389f..57d67a7f8546 100644 --- a/x/staking/client/cli_test/helpers.go +++ b/x/staking/client/cli_test/helpers.go @@ -7,32 +7,32 @@ import ( clientkeys "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/tests" - "github.com/cosmos/cosmos-sdk/tests/cli/helpers" + "github.com/cosmos/cosmos-sdk/tests/cli" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/staking" ) // TxStakingCreateValidator is simcli tx staking create-validator -func TxStakingCreateValidator(f *helpers.Fixtures, from, consPubKey string, amount sdk.Coin, flags ...string) (bool, string, string) { +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.SimcliBinary, 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 helpers.ExecuteWriteRetStdStreams(f.T, helpers.AddFlags(cmd, flags), clientkeys.DefaultKeyPass) + return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass) } // TxStakingUnbond is simcli tx staking unbond -func TxStakingUnbond(f *helpers.Fixtures, from, shares string, validator sdk.ValAddress, flags ...string) bool { +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.SimcliBinary, validator, shares, from, f.Flags()) - return helpers.ExecuteWrite(f.T, helpers.AddFlags(cmd, flags), clientkeys.DefaultKeyPass) + return cli.ExecuteWrite(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass) } // QueryStakingValidator is simcli query staking validator -func QueryStakingValidator(f *helpers.Fixtures, valAddr sdk.ValAddress, flags ...string) staking.Validator { +func QueryStakingValidator(f *cli.Fixtures, valAddr sdk.ValAddress, flags ...string) staking.Validator { cmd := fmt.Sprintf("%s query staking validator %s %v", f.SimcliBinary, valAddr, f.Flags()) - out, _ := tests.ExecuteT(f.T, helpers.AddFlags(cmd, flags), "") + out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "") var validator staking.Validator err := f.Cdc.UnmarshalJSON([]byte(out), &validator) @@ -41,9 +41,9 @@ func QueryStakingValidator(f *helpers.Fixtures, valAddr sdk.ValAddress, flags .. } // QueryStakingUnbondingDelegationsFrom is simcli query staking unbonding-delegations-from -func QueryStakingUnbondingDelegationsFrom(f *helpers.Fixtures, valAddr sdk.ValAddress, flags ...string) []staking.UnbondingDelegation { +func QueryStakingUnbondingDelegationsFrom(f *cli.Fixtures, valAddr sdk.ValAddress, flags ...string) []staking.UnbondingDelegation { cmd := fmt.Sprintf("%s query staking unbonding-delegations-from %s %v", f.SimcliBinary, valAddr, f.Flags()) - out, _ := tests.ExecuteT(f.T, helpers.AddFlags(cmd, flags), "") + out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "") var ubds []staking.UnbondingDelegation err := f.Cdc.UnmarshalJSON([]byte(out), &ubds) @@ -52,9 +52,9 @@ func QueryStakingUnbondingDelegationsFrom(f *helpers.Fixtures, valAddr sdk.ValAd } // QueryStakingDelegationsTo is simcli query staking delegations-to -func QueryStakingDelegationsTo(f *helpers.Fixtures, valAddr sdk.ValAddress, flags ...string) []staking.Delegation { +func QueryStakingDelegationsTo(f *cli.Fixtures, valAddr sdk.ValAddress, flags ...string) []staking.Delegation { cmd := fmt.Sprintf("%s query staking delegations-to %s %v", f.SimcliBinary, valAddr, f.Flags()) - out, _ := tests.ExecuteT(f.T, helpers.AddFlags(cmd, flags), "") + out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "") var delegations []staking.Delegation err := f.Cdc.UnmarshalJSON([]byte(out), &delegations) @@ -63,9 +63,9 @@ func QueryStakingDelegationsTo(f *helpers.Fixtures, valAddr sdk.ValAddress, flag } // QueryStakingPool is simcli query staking pool -func QueryStakingPool(f *helpers.Fixtures, flags ...string) staking.Pool { +func QueryStakingPool(f *cli.Fixtures, flags ...string) staking.Pool { cmd := fmt.Sprintf("%s query staking pool %v", f.SimcliBinary, f.Flags()) - out, _ := tests.ExecuteT(f.T, helpers.AddFlags(cmd, flags), "") + out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "") var pool staking.Pool err := f.Cdc.UnmarshalJSON([]byte(out), &pool) @@ -74,9 +74,9 @@ func QueryStakingPool(f *helpers.Fixtures, flags ...string) staking.Pool { } // QueryStakingParameters is simcli query staking parameters -func QueryStakingParameters(f *helpers.Fixtures, flags ...string) staking.Params { +func QueryStakingParameters(f *cli.Fixtures, flags ...string) staking.Params { cmd := fmt.Sprintf("%s query staking params %v", f.SimcliBinary, f.Flags()) - out, _ := tests.ExecuteT(f.T, helpers.AddFlags(cmd, flags), "") + out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "") var params staking.Params err := f.Cdc.UnmarshalJSON([]byte(out), ¶ms) diff --git a/x/staking/client/cli_test/staking_test.go b/x/staking/client/cli_test/staking_test.go index 785c98bc6fb2..380d6011e698 100644 --- a/x/staking/client/cli_test/staking_test.go +++ b/x/staking/client/cli_test/staking_test.go @@ -9,7 +9,7 @@ import ( "github.com/tendermint/tendermint/crypto/ed25519" "github.com/cosmos/cosmos-sdk/tests" - "github.com/cosmos/cosmos-sdk/tests/cli/helpers" + "github.com/cosmos/cosmos-sdk/tests/cli" sdk "github.com/cosmos/cosmos-sdk/types" bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/cli_test" stakingcli "github.com/cosmos/cosmos-sdk/x/staking/client/cli_test" @@ -17,44 +17,44 @@ import ( func TestCLICreateValidator(t *testing.T) { t.Parallel() - f := helpers.InitFixtures(t) + f := cli.InitFixtures(t) // start simd server proc := f.SDStart() defer proc.Stop(false) - barAddr := f.KeyAddress(helpers.KeyBar) + barAddr := f.KeyAddress(cli.KeyBar) barVal := sdk.ValAddress(barAddr) consPubKey := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, ed25519.GenPrivKey().PubKey()) sendTokens := sdk.TokensFromConsensusPower(10) - bankcli.TxSend(f, helpers.KeyFoo, barAddr, sdk.NewCoin(helpers.Denom, sendTokens), "-y") + bankcli.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "-y") tests.WaitForNextNBlocksTM(1, f.Port) - require.Equal(t, sendTokens, bankcli.QueryBalances(f, barAddr).AmountOf(helpers.Denom)) + require.Equal(t, sendTokens, bankcli.QueryBalances(f, barAddr).AmountOf(cli.Denom)) //Generate a create validator transaction and ensure correctness - success, stdout, stderr := stakingcli.TxStakingCreateValidator(f, barAddr.String(), consPubKey, sdk.NewInt64Coin(helpers.Denom, 2), "--generate-only") + success, stdout, stderr := stakingcli.TxStakingCreateValidator(f, barAddr.String(), consPubKey, sdk.NewInt64Coin(cli.Denom, 2), "--generate-only") require.True(f.T, success) require.Empty(f.T, stderr) - msg := helpers.UnmarshalStdTx(f.T, f.Cdc, stdout) + msg := cli.UnmarshalStdTx(f.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 newValTokens := sdk.TokensFromConsensusPower(2) - success, _, _ = stakingcli.TxStakingCreateValidator(f, barAddr.String(), consPubKey, sdk.NewCoin(helpers.Denom, newValTokens), "--dry-run") + success, _, _ = stakingcli.TxStakingCreateValidator(f, barAddr.String(), consPubKey, sdk.NewCoin(cli.Denom, newValTokens), "--dry-run") require.True(t, success) // Create the validator - stakingcli.TxStakingCreateValidator(f, helpers.KeyBar, consPubKey, sdk.NewCoin(helpers.Denom, newValTokens), "-y") + stakingcli.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), bankcli.QueryBalances(f, barAddr).AmountOf(helpers.Denom)) + require.Equal(t, sendTokens.Sub(newValTokens), bankcli.QueryBalances(f, barAddr).AmountOf(cli.Denom)) // Ensure that validator state is as expected validator := stakingcli.QueryStakingValidator(f, barVal) @@ -68,7 +68,7 @@ func TestCLICreateValidator(t *testing.T) { // unbond a single share unbondAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(1)) - success = stakingcli.TxStakingUnbond(f, helpers.KeyBar, unbondAmt.String(), barVal, "-y") + success = stakingcli.TxStakingUnbond(f, cli.KeyBar, unbondAmt.String(), barVal, "-y") require.True(t, success) tests.WaitForNextNBlocksTM(1, f.Port)