From b854c485e41882f91c5a432239cb400add3c100e Mon Sep 17 00:00:00 2001 From: SaReN Date: Thu, 30 Apr 2020 02:16:10 +0530 Subject: [PATCH] Add CLI tests for fee deduction (#6088) * Added cli integration base setup * Added cmd to simapp * Fixed ci-lint issues * Fixed ci-lint issues * Addressed changes in Makefile * Updated simd to latest * Removed testnet and replay commands * Modified tx command for simcli * Did code cleanup * Removed duplication in Makefile * Refactored cli_test * Added build-sim to Makefile * Added test-cli to circleci * Added tests for staking txns * Addressed format issues * refctored tests code * Added tests for send, staking * Removed test_hepers file * Moved test_cover to contrib * Added codec in fixtures * Migrated tests to respective modules * Exported helper methods * Moved helpers to bank * Added codec to fixtures * Migrated tests to modules * Removed auth helpers from staking * Did minor code cleanup * Added test-cli to Makefile * Updated github actions * Did code refactor * Fixed github actions for cli-test * Added tests for recover keys and fee deduction * Did minor code cleanup * Added build flag to cli_tests * Moved cli_test to tests * Modified path in Makefile * Updated codec std in fixtures * Added doc for cli tests * Remove ibc genesis validation * Fix issue number * Added missing imports * Modified naming for test functions Co-authored-by: atheesh Co-authored-by: kaustubhkapatral <54210167+kaustubhkapatral@users.noreply.github.com> Co-authored-by: Aaron Craelius Co-authored-by: anilCSE --- tests/cli/helpers/helpers.go | 4 +- tests/cli/tests/keys_test.go | 35 ++++++ x/bank/client/cli_test/send_test.go | 123 ++++++++++++++++++++++ x/staking/client/cli_test/staking_test.go | 2 +- 4 files changed, 161 insertions(+), 3 deletions(-) diff --git a/tests/cli/helpers/helpers.go b/tests/cli/helpers/helpers.go index d25852edd3b3..e2bc3cad6867 100644 --- a/tests/cli/helpers/helpers.go +++ b/tests/cli/helpers/helpers.go @@ -168,13 +168,13 @@ func (f *Fixtures) CLIConfig(key, value string, flags ...string) { ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags)) } -// TxBroadcast is gaiacli tx broadcast +// TxBroadcast is simcli tx broadcast func (f *Fixtures) TxBroadcast(fileName string, flags ...string) (bool, string, string) { cmd := fmt.Sprintf("%s tx broadcast %v %v", f.SimcliBinary, f.Flags(), fileName) return ExecuteWriteRetStdStreams(f.T, AddFlags(cmd, flags), clientkeys.DefaultKeyPass) } -// TxEncode is gaiacli tx encode +// TxEncode is simcli tx encode func (f *Fixtures) TxEncode(fileName string, flags ...string) (bool, string, string) { cmd := fmt.Sprintf("%s tx encode %v %v", f.SimcliBinary, f.Flags(), fileName) return ExecuteWriteRetStdStreams(f.T, AddFlags(cmd, flags), clientkeys.DefaultKeyPass) diff --git a/tests/cli/tests/keys_test.go b/tests/cli/tests/keys_test.go index f419890a3818..3abbf0878f2c 100644 --- a/tests/cli/tests/keys_test.go +++ b/tests/cli/tests/keys_test.go @@ -37,3 +37,38 @@ func TestCLIKeysAddMultisig(t *testing.T) { // Cleanup testing directories f.Cleanup() } + +func TestCLIKeysAddRecover(t *testing.T) { + t.Parallel() + f := helpers.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.Parallel() + f := helpers.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/x/bank/client/cli_test/send_test.go b/x/bank/client/cli_test/send_test.go index a4cd6fbd11ee..2f65393c099f 100644 --- a/x/bank/client/cli_test/send_test.go +++ b/x/bank/client/cli_test/send_test.go @@ -3,6 +3,7 @@ package cli_test import ( + "fmt" "github.com/cosmos/cosmos-sdk/tests" "github.com/cosmos/cosmos-sdk/tests/cli/helpers" sdk "github.com/cosmos/cosmos-sdk/types" @@ -79,3 +80,125 @@ func TestCLISend(t *testing.T) { f.Cleanup() } + +func TestCLIMinimumFees(t *testing.T) { + t.Parallel() + f := helpers.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), + ) + proc := f.SDStart(fees) + defer proc.Stop(false) + + barAddr := f.KeyAddress(helpers.KeyBar) + + // Send a transaction that will get rejected + success, stdOut, _ := bankcli.TxSend(f, helpers.KeyFoo, barAddr, sdk.NewInt64Coin(helpers.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") + 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") + require.Contains(t, stdOut, "insufficient fees") + require.True(f.T, success) + + // Cleanup testing directories + f.Cleanup() +} + +func TestCLIGasPrices(t *testing.T) { + t.Parallel() + f := helpers.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))) + defer proc.Stop(false) + + barAddr := f.KeyAddress(helpers.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") + require.Contains(t, stdOut, "insufficient fees") + require.True(t, success) + + // wait for a block confirmation + tests.WaitForNextNBlocksTM(1, f.Port) + + // 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") + require.True(t, success) + + // wait for a block confirmation + tests.WaitForNextNBlocksTM(1, f.Port) + + f.Cleanup() +} + +func TestCLIFeesDeduction(t *testing.T) { + t.Parallel() + f := helpers.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))) + defer proc.Stop(false) + + // Save key addresses for later use + fooAddr := f.KeyAddress(helpers.KeyFoo) + barAddr := f.KeyAddress(helpers.KeyBar) + + fooAmt := bankcli.QueryBalances(f, fooAddr).AmountOf(helpers.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") + 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()) + + // 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") + require.Contains(t, stdOut, "insufficient funds") + 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()) + + // 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") + require.True(t, success) + + f.Cleanup() +} diff --git a/x/staking/client/cli_test/staking_test.go b/x/staking/client/cli_test/staking_test.go index b801e7dddf87..de8b9835148a 100644 --- a/x/staking/client/cli_test/staking_test.go +++ b/x/staking/client/cli_test/staking_test.go @@ -17,7 +17,7 @@ func TestCLICreateValidator(t *testing.T) { t.Parallel() f := helpers.InitFixtures(t) - // start gaiad server + // start simd server proc := f.SDStart() defer proc.Stop(false)