Address PR comments

This commit is contained in:
Jack Zampolin 2018-12-11 13:04:59 -08:00
parent 206566f043
commit 6f08a0e76a
2 changed files with 157 additions and 113 deletions

View File

@ -13,14 +13,11 @@ import (
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
p2p "github.com/tendermint/tendermint/p2p"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
client "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/mintkey"
tests "github.com/cosmos/cosmos-sdk/tests"
@ -123,86 +120,26 @@ func TestVersion(t *testing.T) {
func TestNodeStatus(t *testing.T) {
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
defer cleanup()
// node info
res, body := Request(t, port, "GET", "/node_info", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var nodeInfo p2p.DefaultNodeInfo
err := cdc.UnmarshalJSON([]byte(body), &nodeInfo)
require.Nil(t, err, "Couldn't parse node info")
require.NotEqual(t, p2p.DefaultNodeInfo{}, nodeInfo, "res: %v", res)
// syncing
res, body = Request(t, port, "GET", "/syncing", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
// we expect that there is no other node running so the syncing state is "false"
require.Equal(t, "false", body)
getNodeInfo(t, port)
getSyncStatus(t, port, false)
}
func TestBlock(t *testing.T) {
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
defer cleanup()
var resultBlock ctypes.ResultBlock
res, body := Request(t, port, "GET", "/blocks/latest", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err := cdc.UnmarshalJSON([]byte(body), &resultBlock)
require.Nil(t, err, "Couldn't parse block")
require.NotEqual(t, ctypes.ResultBlock{}, resultBlock)
// --
res, body = Request(t, port, "GET", "/blocks/2", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err = codec.Cdc.UnmarshalJSON([]byte(body), &resultBlock)
require.Nil(t, err, "Couldn't parse block")
require.NotEqual(t, ctypes.ResultBlock{}, resultBlock)
// --
res, body = Request(t, port, "GET", "/blocks/1000000000", nil)
require.Equal(t, http.StatusNotFound, res.StatusCode, body)
getBlock(t, port, -1, false)
getBlock(t, port, 2, false)
getBlock(t, port, 100000000, true)
}
func TestValidators(t *testing.T) {
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
defer cleanup()
var resultVals rpc.ResultValidatorsOutput
res, body := Request(t, port, "GET", "/validatorsets/latest", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err := cdc.UnmarshalJSON([]byte(body), &resultVals)
require.Nil(t, err, "Couldn't parse validatorset")
require.NotEqual(t, rpc.ResultValidatorsOutput{}, resultVals)
resultVals := getValidatorSets(t, port, -1, false)
require.Contains(t, resultVals.Validators[0].Address.String(), "cosmosvaloper")
require.Contains(t, resultVals.Validators[0].PubKey, "cosmosvalconspub")
// --
res, body = Request(t, port, "GET", "/validatorsets/2", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err = cdc.UnmarshalJSON([]byte(body), &resultVals)
require.Nil(t, err, "Couldn't parse validatorset")
require.NotEqual(t, rpc.ResultValidatorsOutput{}, resultVals)
// --
res, body = Request(t, port, "GET", "/validatorsets/1000000000", nil)
require.Equal(t, http.StatusNotFound, res.StatusCode, body)
getValidatorSets(t, port, 2, false)
getValidatorSets(t, port, 10000000, true)
}
func TestCoinSend(t *testing.T) {
@ -222,7 +159,7 @@ func TestCoinSend(t *testing.T) {
initialBalance := acc.GetCoins()
// create TX
receiveAddr, resultTx := doSend(t, port, seed, name1, pw, addr)
receiveAddr, resultTx := doTransfer(t, port, seed, name1, pw, addr)
tests.WaitForHeight(resultTx.Height+1, port)
// check if tx was committed
@ -246,29 +183,29 @@ func TestCoinSend(t *testing.T) {
require.Equal(t, int64(1), mycoins.Amount.Int64())
// test failure with too little gas
res, body, _ = doSendWithGas(t, port, seed, name1, pw, addr, "100", 0, false, false)
res, body, _ = doTransferWithGas(t, port, seed, name1, pw, addr, "100", 0, false, false)
require.Equal(t, http.StatusInternalServerError, res.StatusCode, body)
// test failure with negative gas
res, body, _ = doSendWithGas(t, port, seed, name1, pw, addr, "-200", 0, false, false)
res, body, _ = doTransferWithGas(t, port, seed, name1, pw, addr, "-200", 0, false, false)
require.Equal(t, http.StatusBadRequest, res.StatusCode, body)
// test failure with 0 gas
res, body, _ = doSendWithGas(t, port, seed, name1, pw, addr, "0", 0, false, false)
res, body, _ = doTransferWithGas(t, port, seed, name1, pw, addr, "0", 0, false, false)
require.Equal(t, http.StatusInternalServerError, res.StatusCode, body)
// test failure with wrong adjustment
res, body, _ = doSendWithGas(t, port, seed, name1, pw, addr, "simulate", 0.1, false, false)
res, body, _ = doTransferWithGas(t, port, seed, name1, pw, addr, "simulate", 0.1, false, false)
require.Equal(t, http.StatusInternalServerError, res.StatusCode, body)
// run simulation and test success with estimated gas
res, body, _ = doSendWithGas(t, port, seed, name1, pw, addr, "", 0, true, false)
res, body, _ = doTransferWithGas(t, port, seed, name1, pw, addr, "", 0, true, false)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var responseBody struct {
GasEstimate int64 `json:"gas_estimate"`
}
require.Nil(t, json.Unmarshal([]byte(body), &responseBody))
res, body, _ = doSendWithGas(t, port, seed, name1, pw, addr, fmt.Sprintf("%v", responseBody.GasEstimate), 0, false, false)
res, body, _ = doTransferWithGas(t, port, seed, name1, pw, addr, fmt.Sprintf("%v", responseBody.GasEstimate), 0, false, false)
require.Equal(t, http.StatusOK, res.StatusCode, body)
}
@ -279,7 +216,7 @@ func TestCoinSendGenerateSignAndBroadcast(t *testing.T) {
acc := getAccount(t, port, addr)
// generate TX
res, body, _ := doSendWithGas(t, port, seed, name1, pw, addr, "simulate", 0, false, true)
res, body, _ := doTransferWithGas(t, port, seed, name1, pw, addr, "simulate", 0, false, true)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var msg auth.StdTx
require.Nil(t, cdc.UnmarshalJSON([]byte(body), &msg))
@ -288,11 +225,11 @@ func TestCoinSendGenerateSignAndBroadcast(t *testing.T) {
require.Equal(t, msg.Msgs[0].GetSigners(), []sdk.AccAddress{addr})
require.Equal(t, 0, len(msg.Signatures))
gasEstimate := msg.Fee.Gas
accnum := acc.GetAccountNumber()
sequence := acc.GetSequence()
// sign tx
var signedMsg auth.StdTx
accnum := acc.GetAccountNumber()
sequence := acc.GetSequence()
payload := authrest.SignBody{
Tx: msg,
@ -352,13 +289,12 @@ func TestTxs(t *testing.T) {
require.Equal(t, emptyTxs, txs)
// create tx
receiveAddr, resultTx := doSend(t, port, seed, name1, pw, addr)
receiveAddr, resultTx := doTransfer(t, port, seed, name1, pw, addr)
tests.WaitForHeight(resultTx.Height+1, port)
// check if tx is queryable
txs = getTransactions(t, port, fmt.Sprintf("tx.hash=%s", resultTx.Hash))
require.Len(t, txs, 1)
require.Equal(t, resultTx.Hash, txs[0].Hash)
tx := getTransaction(t, port, resultTx.Hash.String())
require.Equal(t, resultTx.Hash, tx.Hash)
// query sender
txs = getTransactions(t, port, fmt.Sprintf("sender=%s", addr.String()))
@ -378,26 +314,16 @@ func TestPoolParamsQuery(t *testing.T) {
defaultParams := stake.DefaultParams()
res, body := Request(t, port, "GET", "/stake/parameters", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var params stake.Params
err := cdc.UnmarshalJSON([]byte(body), &params)
require.Nil(t, err)
params := getStakeParams(t, port)
require.True(t, defaultParams.Equal(params))
res, body = Request(t, port, "GET", "/stake/pool", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.NotNil(t, body)
pool := getStakePool(t, port)
initialPool := stake.InitialPool()
initialPool.LooseTokens = initialPool.LooseTokens.Add(sdk.NewDec(100))
initialPool.BondedTokens = initialPool.BondedTokens.Add(sdk.NewDec(100)) // Delegate tx on GaiaAppGenState
initialPool.LooseTokens = initialPool.LooseTokens.Add(sdk.NewDec(int64(50))) // freeFermionsAcc = 50 on GaiaAppGenState
var pool stake.Pool
err = cdc.UnmarshalJSON([]byte(body), &pool)
require.Nil(t, err)
require.Equal(t, initialPool.BondedTokens, pool.BondedTokens)
require.Equal(t, initialPool.LooseTokens, pool.LooseTokens)
}

View File

@ -15,12 +15,12 @@ import (
"strings"
"testing"
"github.com/tendermint/tendermint/crypto/secp256k1"
cryptoKeys "github.com/cosmos/cosmos-sdk/crypto/keys"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/slashing"
stakeTypes "github.com/cosmos/cosmos-sdk/x/stake/types"
"github.com/tendermint/tendermint/crypto/secp256k1"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client"
@ -420,24 +420,92 @@ func Request(t *testing.T, port, method, path string, payload []byte) (*http.Res
// ICS 0 - Tendermint
// ----------------------------------------------------------------------
// GET /node_info The properties of the connected node
// SEE TestNodeStatus
func getNodeInfo(t *testing.T, port string) p2p.DefaultNodeInfo {
res, body := Request(t, port, "GET", "/node_info", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var nodeInfo p2p.DefaultNodeInfo
err := cdc.UnmarshalJSON([]byte(body), &nodeInfo)
require.Nil(t, err, "Couldn't parse node info")
require.NotEqual(t, p2p.DefaultNodeInfo{}, nodeInfo, "res: %v", res)
return nodeInfo
}
// GET /syncing Syncing state of node
// SEE TestNodeStatus
func getSyncStatus(t *testing.T, port string, syncing bool) {
res, body := Request(t, port, "GET", "/syncing", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
if syncing {
require.Equal(t, "true", body)
return
}
require.Equal(t, "false", body)
}
// GET /blocks/latest Get the latest block
// SEE TestBlock
// GET /blocks/{height} Get a block at a certain height
// SEE TestBlock
func getBlock(t *testing.T, port string, height int, expectFail bool) ctypes.ResultBlock {
var url string
if height > 0 {
url = fmt.Sprintf("/blocks/%d", height)
} else {
url = "/blocks/latest"
}
var resultBlock ctypes.ResultBlock
// GET /validatorsets/latest Get the latest validator set
// SEE TestValidators
res, body := Request(t, port, "GET", url, nil)
if expectFail {
require.Equal(t, http.StatusNotFound, res.StatusCode, body)
return resultBlock
}
require.Equal(t, http.StatusOK, res.StatusCode, body)
err := cdc.UnmarshalJSON([]byte(body), &resultBlock)
require.Nil(t, err, "Couldn't parse block")
require.NotEqual(t, ctypes.ResultBlock{}, resultBlock)
return resultBlock
}
// GET /validatorsets/{height} Get a validator set a certain height
// SEE TestValidators
// GET /validatorsets/latest Get the latest validator set
func getValidatorSets(t *testing.T, port string, height int, expectFail bool) rpc.ResultValidatorsOutput {
var url string
if height > 0 {
url = fmt.Sprintf("/validatorsets/%d", height)
} else {
url = "/validatorsets/latest"
}
var resultVals rpc.ResultValidatorsOutput
res, body := Request(t, port, "GET", url, nil)
if expectFail {
require.Equal(t, http.StatusNotFound, res.StatusCode, body)
return resultVals
}
require.Equal(t, http.StatusOK, res.StatusCode, body)
err := cdc.UnmarshalJSON([]byte(body), &resultVals)
require.Nil(t, err, "Couldn't parse validatorset")
require.NotEqual(t, rpc.ResultValidatorsOutput{}, resultVals)
return resultVals
}
// GET /txs/{hash} get tx by hash
func getTransaction(t *testing.T, port string, hash string) tx.Info {
var tx tx.Info
res, body := Request(t, port, "GET", fmt.Sprintf("/txs/%s", hash), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err := cdc.UnmarshalJSON([]byte(body), &tx)
require.NoError(t, err)
return tx
}
// POST /txs broadcast txs
// GET /txs search transactions
@ -571,14 +639,47 @@ func getAccount(t *testing.T, port string, addr sdk.AccAddress) auth.Account {
// ICS 20 - Tokens
// ----------------------------------------------------------------------
// TODO: TEST THE FOLLOWING ROUTES
// POST /tx/sign Sign a Tx
func doSign(t *testing.T, port, name, password, chainID string, accnum, sequence uint64, msg auth.StdTx) auth.StdTx {
var signedMsg auth.StdTx
payload := authrest.SignBody{
Tx: msg,
LocalAccountName: name,
Password: password,
ChainID: chainID,
AccountNumber: accnum,
Sequence: sequence,
}
json, err := cdc.MarshalJSON(payload)
require.Nil(t, err)
res, body := Request(t, port, "POST", "/tx/sign", json)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.Nil(t, cdc.UnmarshalJSON([]byte(body), &signedMsg))
return signedMsg
}
// POST /tx/broadcast Send a signed Tx
func doBroadcast(t *testing.T, port string, msg auth.StdTx) ctypes.ResultBroadcastTxCommit {
tx := broadcastReq{Tx: msg, Return: "block"}
req, err := cdc.MarshalJSON(tx)
require.Nil(t, err)
res, body := Request(t, port, "POST", "/tx/broadcast", req)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var resultTx ctypes.ResultBroadcastTxCommit
require.Nil(t, cdc.UnmarshalJSON([]byte(body), &resultTx))
return resultTx
}
type broadcastReq struct {
Tx auth.StdTx `json:"tx"`
Return string `json:"return"`
}
// GET /bank/balances/{address} Get the account balances
// POST /bank/accounts/{address}/transfers Send coins (build -> sign -> send)
func doSend(t *testing.T, port, seed, name, password string, addr sdk.AccAddress) (receiveAddr sdk.AccAddress, resultTx ctypes.ResultBroadcastTxCommit) {
res, body, receiveAddr := doSendWithGas(t, port, seed, name, password, addr, "", 0, false, false)
func doTransfer(t *testing.T, port, seed, name, password string, addr sdk.AccAddress) (receiveAddr sdk.AccAddress, resultTx ctypes.ResultBroadcastTxCommit) {
res, body, receiveAddr := doTransferWithGas(t, port, seed, name, password, addr, "", 0, false, false)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err := cdc.UnmarshalJSON([]byte(body), &resultTx)
@ -587,7 +688,7 @@ func doSend(t *testing.T, port, seed, name, password string, addr sdk.AccAddress
return receiveAddr, resultTx
}
func doSendWithGas(t *testing.T, port, seed, name, password string, addr sdk.AccAddress, gas string,
func doTransferWithGas(t *testing.T, port, seed, name, password string, addr sdk.AccAddress, gas string,
gasAdjustment float64, simulate, generateOnly bool) (
res *http.Response, body string, receiveAddr sdk.AccAddress) {
@ -935,10 +1036,26 @@ func getValidatorRedelegations(t *testing.T, port string, validatorAddr sdk.ValA
}
// GET /stake/pool Get the current state of the staking pool
// SEE TestPoolParamsQuery
func getStakePool(t *testing.T, port string) stake.Pool {
res, body := Request(t, port, "GET", "/stake/pool", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.NotNil(t, body)
var pool stake.Pool
err := cdc.UnmarshalJSON([]byte(body), &pool)
require.Nil(t, err)
return pool
}
// GET /stake/parameters Get the current staking parameter values
// SEE TestPoolParamsQuery
func getStakeParams(t *testing.T, port string) stake.Params {
res, body := Request(t, port, "GET", "/stake/parameters", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var params stake.Params
err := cdc.UnmarshalJSON([]byte(body), &params)
require.Nil(t, err)
return params
}
// ----------------------------------------------------------------------
// ICS 22 - Gov
@ -1230,6 +1347,7 @@ func getSigningInfo(t *testing.T, port string, validatorPubKey string) slashing.
return signingInfo
}
// TODO: Test this functionality, it is not currently in any of the tests
// POST /slashing/validators/{validatorAddr}/unjail Unjail a jailed validator
func doUnjail(t *testing.T, port, seed, name, password string,
valAddr sdk.ValAddress) (resultTx ctypes.ResultBroadcastTxCommit) {