Skip to content

Commit

Permalink
Add CLI tests for fee deduction (#6088)
Browse files Browse the repository at this point in the history
* 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 <atheesh1>
Co-authored-by: kaustubhkapatral <54210167+kaustubhkapatral@users.noreply.github.com>
Co-authored-by: Aaron Craelius <aaron@regen.network>
Co-authored-by: anilCSE <anil@vitwit.com>
  • Loading branch information
4 people authored Apr 29, 2020
1 parent dc1a21a commit b854c48
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 3 deletions.
4 changes: 2 additions & 2 deletions tests/cli/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions tests/cli/tests/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
123 changes: 123 additions & 0 deletions x/bank/client/cli_test/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion x/staking/client/cli_test/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit b854c48

Please sign in to comment.