2020-04-29 08:52:30 -07:00
|
|
|
// +build cli_test
|
|
|
|
|
|
|
|
package cli_test
|
|
|
|
|
|
|
|
import (
|
2020-04-29 13:46:10 -07:00
|
|
|
"fmt"
|
2020-04-29 19:36:34 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-04-29 08:52:30 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/tests"
|
2020-05-01 13:16:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/tests/cli"
|
2020-04-29 08:52:30 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-05-04 06:55:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/client/testutil"
|
2020-04-29 08:52:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCLISend(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-05-01 13:16:17 -07:00
|
|
|
f := cli.InitFixtures(t)
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
// start simd server
|
|
|
|
proc := f.SDStart()
|
2020-05-04 06:55:16 -07:00
|
|
|
t.Cleanup(func() { proc.Stop(false) })
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
// Save key addresses for later uspackage testse
|
2020-05-01 13:16:17 -07:00
|
|
|
fooAddr := f.KeyAddress(cli.KeyFoo)
|
|
|
|
barAddr := f.KeyAddress(cli.KeyBar)
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
startTokens := sdk.TokensFromConsensusPower(50)
|
2020-05-04 06:55:16 -07:00
|
|
|
require.Equal(t, startTokens, testutil.QueryBalances(f, fooAddr).AmountOf(cli.Denom))
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
sendTokens := sdk.TokensFromConsensusPower(10)
|
|
|
|
|
|
|
|
// It does not allow to send in offline mode
|
2020-05-04 06:55:16 -07:00
|
|
|
success, _, stdErr := testutil.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "-y", "--offline")
|
2020-04-29 08:52:30 -07:00
|
|
|
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
|
2020-05-04 06:55:16 -07:00
|
|
|
testutil.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "-y")
|
2020-04-29 08:52:30 -07:00
|
|
|
tests.WaitForNextNBlocksTM(1, f.Port)
|
|
|
|
|
|
|
|
// Ensure account balances match expected
|
2020-05-21 14:29:34 -07:00
|
|
|
require.Equal(t, sendTokens.String(), testutil.QueryBalances(f, barAddr).AmountOf(cli.Denom).String())
|
|
|
|
require.Equal(t, startTokens.Sub(sendTokens).String(), testutil.QueryBalances(f, fooAddr).AmountOf(cli.Denom).String())
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
// Test --dry-run
|
2020-05-04 06:55:16 -07:00
|
|
|
success, _, _ = testutil.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "--dry-run")
|
2020-04-29 08:52:30 -07:00
|
|
|
require.True(t, success)
|
|
|
|
|
|
|
|
// Test --generate-only
|
2020-05-04 06:55:16 -07:00
|
|
|
success, stdout, stderr := testutil.TxSend(
|
2020-05-01 13:16:17 -07:00
|
|
|
f, fooAddr.String(), barAddr, sdk.NewCoin(cli.Denom, sendTokens), "--generate-only=true",
|
2020-04-29 08:52:30 -07:00
|
|
|
)
|
|
|
|
require.Empty(t, stderr)
|
|
|
|
require.True(t, success)
|
2020-05-01 13:16:17 -07:00
|
|
|
msg := cli.UnmarshalStdTx(f.T, f.Cdc, stdout)
|
2020-04-29 08:52:30 -07:00
|
|
|
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
|
2020-05-04 06:55:16 -07:00
|
|
|
require.Equal(t, startTokens.Sub(sendTokens), testutil.QueryBalances(f, fooAddr).AmountOf(cli.Denom))
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
// test autosequencing
|
2020-05-04 06:55:16 -07:00
|
|
|
testutil.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "-y")
|
2020-04-29 08:52:30 -07:00
|
|
|
tests.WaitForNextNBlocksTM(1, f.Port)
|
|
|
|
|
|
|
|
// Ensure account balances match expected
|
2020-05-04 06:55:16 -07:00
|
|
|
require.Equal(t, sendTokens.MulRaw(2), testutil.QueryBalances(f, barAddr).AmountOf(cli.Denom))
|
|
|
|
require.Equal(t, startTokens.Sub(sendTokens.MulRaw(2)), testutil.QueryBalances(f, fooAddr).AmountOf(cli.Denom))
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
// test memo
|
2020-05-04 06:55:16 -07:00
|
|
|
testutil.TxSend(f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.Denom, sendTokens), "--memo='testmemo'", "-y")
|
2020-04-29 08:52:30 -07:00
|
|
|
tests.WaitForNextNBlocksTM(1, f.Port)
|
|
|
|
|
|
|
|
// Ensure account balances match expected
|
2020-05-04 06:55:16 -07:00
|
|
|
require.Equal(t, sendTokens.MulRaw(3), testutil.QueryBalances(f, barAddr).AmountOf(cli.Denom))
|
|
|
|
require.Equal(t, startTokens.Sub(sendTokens.MulRaw(3)), testutil.QueryBalances(f, fooAddr).AmountOf(cli.Denom))
|
2020-04-29 08:52:30 -07:00
|
|
|
|
|
|
|
f.Cleanup()
|
|
|
|
}
|
2020-04-29 13:46:10 -07:00
|
|
|
|
|
|
|
func TestCLIMinimumFees(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-05-01 13:16:17 -07:00
|
|
|
f := cli.InitFixtures(t)
|
2020-04-29 13:46:10 -07:00
|
|
|
|
|
|
|
// start simd server with minimum fees
|
|
|
|
minGasPrice, _ := sdk.NewDecFromStr("0.000006")
|
|
|
|
fees := fmt.Sprintf(
|
|
|
|
"--minimum-gas-prices=%s,%s",
|
2020-05-01 13:16:17 -07:00
|
|
|
sdk.NewDecCoinFromDec(cli.FeeDenom, minGasPrice),
|
|
|
|
sdk.NewDecCoinFromDec(cli.Fee2Denom, minGasPrice),
|
2020-04-29 13:46:10 -07:00
|
|
|
)
|
|
|
|
proc := f.SDStart(fees)
|
2020-05-04 06:55:16 -07:00
|
|
|
t.Cleanup(func() { proc.Stop(false) })
|
2020-04-29 13:46:10 -07:00
|
|
|
|
2020-05-01 13:16:17 -07:00
|
|
|
barAddr := f.KeyAddress(cli.KeyBar)
|
2020-04-29 13:46:10 -07:00
|
|
|
|
|
|
|
// Send a transaction that will get rejected
|
2020-05-04 06:55:16 -07:00
|
|
|
success, stdOut, _ := testutil.TxSend(f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.Fee2Denom, 10), "-y")
|
2020-04-29 13:46:10 -07:00
|
|
|
require.Contains(t, stdOut, "insufficient fees")
|
|
|
|
require.True(f.T, success)
|
|
|
|
tests.WaitForNextNBlocksTM(1, f.Port)
|
|
|
|
|
|
|
|
// Ensure tx w/ correct fees pass
|
2020-05-01 13:16:17 -07:00
|
|
|
txFees := fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(cli.FeeDenom, 2))
|
2020-05-04 06:55:16 -07:00
|
|
|
success, _, _ = testutil.TxSend(f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.Fee2Denom, 10), txFees, "-y")
|
2020-04-29 13:46:10 -07:00
|
|
|
require.True(f.T, success)
|
|
|
|
tests.WaitForNextNBlocksTM(1, f.Port)
|
|
|
|
|
|
|
|
// Ensure tx w/ improper fees fails
|
2020-05-01 13:16:17 -07:00
|
|
|
txFees = fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(cli.FeeDenom, 1))
|
2020-05-04 06:55:16 -07:00
|
|
|
success, _, _ = testutil.TxSend(f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.FooDenom, 10), txFees, "-y")
|
2020-04-29 13:46:10 -07:00
|
|
|
require.Contains(t, stdOut, "insufficient fees")
|
|
|
|
require.True(f.T, success)
|
|
|
|
|
|
|
|
// Cleanup testing directories
|
|
|
|
f.Cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCLIGasPrices(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-05-01 13:16:17 -07:00
|
|
|
f := cli.InitFixtures(t)
|
2020-04-29 13:46:10 -07:00
|
|
|
|
|
|
|
// start simd server with minimum fees
|
|
|
|
minGasPrice, _ := sdk.NewDecFromStr("0.000006")
|
2020-05-01 13:16:17 -07:00
|
|
|
proc := f.SDStart(fmt.Sprintf("--minimum-gas-prices=%s", sdk.NewDecCoinFromDec(cli.FeeDenom, minGasPrice)))
|
2020-05-04 06:55:16 -07:00
|
|
|
t.Cleanup(func() { proc.Stop(false) })
|
2020-04-29 13:46:10 -07:00
|
|
|
|
2020-05-01 13:16:17 -07:00
|
|
|
barAddr := f.KeyAddress(cli.KeyBar)
|
2020-04-29 13:46:10 -07:00
|
|
|
|
|
|
|
// insufficient gas prices (tx fails)
|
|
|
|
badGasPrice, _ := sdk.NewDecFromStr("0.000003")
|
2020-05-04 06:55:16 -07:00
|
|
|
success, stdOut, _ := testutil.TxSend(
|
2020-05-01 13:16:17 -07:00
|
|
|
f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.FooDenom, 50),
|
|
|
|
fmt.Sprintf("--gas-prices=%s", sdk.NewDecCoinFromDec(cli.FeeDenom, badGasPrice)), "-y")
|
2020-04-29 13:46:10 -07:00
|
|
|
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)
|
2020-05-04 06:55:16 -07:00
|
|
|
success, _, _ = testutil.TxSend(
|
2020-05-01 13:16:17 -07:00
|
|
|
f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.FooDenom, 50),
|
|
|
|
fmt.Sprintf("--gas-prices=%s", sdk.NewDecCoinFromDec(cli.FeeDenom, minGasPrice)), "-y")
|
2020-04-29 13:46:10 -07:00
|
|
|
require.True(t, success)
|
|
|
|
|
|
|
|
// wait for a block confirmation
|
|
|
|
tests.WaitForNextNBlocksTM(1, f.Port)
|
|
|
|
|
|
|
|
f.Cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCLIFeesDeduction(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-05-01 13:16:17 -07:00
|
|
|
f := cli.InitFixtures(t)
|
2020-04-29 13:46:10 -07:00
|
|
|
|
|
|
|
// start simd server with minimum fees
|
|
|
|
minGasPrice, _ := sdk.NewDecFromStr("0.000006")
|
2020-05-01 13:16:17 -07:00
|
|
|
proc := f.SDStart(fmt.Sprintf("--minimum-gas-prices=%s", sdk.NewDecCoinFromDec(cli.FeeDenom, minGasPrice)))
|
2020-05-04 06:55:16 -07:00
|
|
|
t.Cleanup(func() { proc.Stop(false) })
|
2020-04-29 13:46:10 -07:00
|
|
|
|
|
|
|
// Save key addresses for later use
|
2020-05-01 13:16:17 -07:00
|
|
|
fooAddr := f.KeyAddress(cli.KeyFoo)
|
|
|
|
barAddr := f.KeyAddress(cli.KeyBar)
|
2020-04-29 13:46:10 -07:00
|
|
|
|
2020-05-04 06:55:16 -07:00
|
|
|
fooAmt := testutil.QueryBalances(f, fooAddr).AmountOf(cli.FooDenom)
|
2020-04-29 13:46:10 -07:00
|
|
|
|
|
|
|
// test simulation
|
2020-05-04 06:55:16 -07:00
|
|
|
success, _, _ := testutil.TxSend(
|
2020-05-01 13:16:17 -07:00
|
|
|
f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.FooDenom, 1000),
|
|
|
|
fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(cli.FeeDenom, 2)), "--dry-run")
|
2020-04-29 13:46:10 -07:00
|
|
|
require.True(t, success)
|
|
|
|
|
|
|
|
// Wait for a block
|
|
|
|
tests.WaitForNextNBlocksTM(1, f.Port)
|
|
|
|
|
|
|
|
// ensure state didn't change
|
2020-05-04 06:55:16 -07:00
|
|
|
require.Equal(t, fooAmt.Int64(), testutil.QueryBalances(f, fooAddr).AmountOf(cli.FooDenom).Int64())
|
2020-04-29 13:46:10 -07:00
|
|
|
|
|
|
|
// insufficient funds (coins + fees) tx fails
|
|
|
|
largeCoins := sdk.TokensFromConsensusPower(10000000)
|
2020-05-04 06:55:16 -07:00
|
|
|
success, stdOut, _ := testutil.TxSend(
|
2020-05-01 13:16:17 -07:00
|
|
|
f, cli.KeyFoo, barAddr, sdk.NewCoin(cli.FooDenom, largeCoins),
|
|
|
|
fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(cli.FeeDenom, 2)), "-y")
|
2020-04-29 13:46:10 -07:00
|
|
|
require.Contains(t, stdOut, "insufficient funds")
|
|
|
|
require.True(t, success)
|
|
|
|
|
|
|
|
// Wait for a block
|
|
|
|
tests.WaitForNextNBlocksTM(1, f.Port)
|
|
|
|
|
|
|
|
// ensure state didn't change
|
2020-05-04 06:55:16 -07:00
|
|
|
require.Equal(t, fooAmt.Int64(), testutil.QueryBalances(f, fooAddr).AmountOf(cli.FooDenom).Int64())
|
2020-04-29 13:46:10 -07:00
|
|
|
|
|
|
|
// test success (transfer = coins + fees)
|
2020-05-04 06:55:16 -07:00
|
|
|
success, _, _ = testutil.TxSend(
|
2020-05-01 13:16:17 -07:00
|
|
|
f, cli.KeyFoo, barAddr, sdk.NewInt64Coin(cli.FooDenom, 500),
|
|
|
|
fmt.Sprintf("--fees=%s", sdk.NewInt64Coin(cli.FeeDenom, 2)), "-y")
|
2020-04-29 13:46:10 -07:00
|
|
|
require.True(t, success)
|
|
|
|
|
|
|
|
f.Cleanup()
|
|
|
|
}
|
2020-05-05 06:51:35 -07:00
|
|
|
|
|
|
|
func TestCLIQuerySupply(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
f := cli.InitFixtures(t)
|
|
|
|
|
|
|
|
// start simd server
|
|
|
|
proc := f.SDStart()
|
2020-05-05 18:16:37 -07:00
|
|
|
t.Cleanup(func() { proc.Stop(false) })
|
2020-05-05 06:51:35 -07:00
|
|
|
|
|
|
|
totalSupply := testutil.QueryTotalSupply(f)
|
|
|
|
totalSupplyOf := testutil.QueryTotalSupplyOf(f, cli.FooDenom)
|
|
|
|
|
|
|
|
require.Equal(t, cli.TotalCoins, totalSupply)
|
|
|
|
require.True(sdk.IntEq(t, cli.TotalCoins.AmountOf(cli.FooDenom), totalSupplyOf))
|
|
|
|
|
|
|
|
f.Cleanup()
|
|
|
|
}
|