cosmos-sdk/client/lcd/lcd_test.go

663 lines
20 KiB
Go
Raw Normal View History

2018-03-05 08:41:50 -08:00
package lcd
import (
"bytes"
"encoding/hex"
2018-03-05 08:41:50 -08:00
"encoding/json"
2018-03-10 10:54:14 -08:00
"fmt"
"io/ioutil"
"net"
2018-03-05 08:41:50 -08:00
"net/http"
"os"
2018-03-10 09:31:52 -08:00
"regexp"
2018-03-05 08:41:50 -08:00
"testing"
2018-03-09 02:03:15 -08:00
2018-03-17 17:42:18 -07:00
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2018-03-17 17:42:18 -07:00
abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
2018-03-17 17:42:18 -07:00
cryptoKeys "github.com/tendermint/go-crypto/keys"
tmcfg "github.com/tendermint/tendermint/config"
nm "github.com/tendermint/tendermint/node"
p2p "github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/proxy"
2018-03-17 17:42:18 -07:00
ctypes "github.com/tendermint/tendermint/rpc/core/types"
tmrpc "github.com/tendermint/tendermint/rpc/lib/server"
tmtypes "github.com/tendermint/tendermint/types"
2018-04-06 17:25:08 -07:00
pvm "github.com/tendermint/tendermint/types/priv_validator"
"github.com/tendermint/tmlibs/cli"
2018-03-17 17:42:18 -07:00
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
2018-03-17 17:42:18 -07:00
client "github.com/cosmos/cosmos-sdk/client"
keys "github.com/cosmos/cosmos-sdk/client/keys"
2018-05-20 18:46:09 -07:00
gapp "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
tests "github.com/cosmos/cosmos-sdk/tests"
2018-03-17 17:42:18 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
2018-05-14 17:43:46 -07:00
"github.com/cosmos/cosmos-sdk/x/stake"
2018-03-17 17:42:18 -07:00
)
var (
coinDenom = "steak"
2018-03-17 17:42:18 -07:00
coinAmount = int64(10000000)
validatorAddr1 = ""
validatorAddr2 = ""
2018-05-14 17:43:46 -07:00
2018-03-17 17:42:18 -07:00
// XXX bad globals
2018-04-18 12:39:32 -07:00
name = "test"
password = "0123456789"
2018-03-17 17:42:18 -07:00
port string // XXX: but it's the int ...
seed string
sendAddr string
2018-03-05 08:41:50 -08:00
)
func TestKeys(t *testing.T) {
// empty keys
2018-03-17 17:42:18 -07:00
// XXX: the test comes with a key setup
/*
res, body := request(t, port, "GET", "/keys", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
assert.Equal(t, "[]", body, "Expected an empty array")
*/
2018-03-05 08:41:50 -08:00
2018-03-10 09:31:52 -08:00
// get seed
2018-03-17 17:42:18 -07:00
res, body := request(t, port, "GET", "/keys/seed", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-17 17:42:18 -07:00
newSeed := body
2018-03-10 09:31:52 -08:00
reg, err := regexp.Compile(`([a-z]+ ){12}`)
require.Nil(t, err)
match := reg.MatchString(seed)
assert.True(t, match, "Returned seed has wrong foramt", seed)
2018-03-17 17:42:18 -07:00
newName := "test_newname"
newPassword := "0987654321"
2018-03-10 08:15:56 -08:00
// add key
2018-03-17 17:42:18 -07:00
var jsonStr = []byte(fmt.Sprintf(`{"name":"test_fail", "password":"%s"}`, password))
2018-03-12 08:31:27 -07:00
res, body = request(t, port, "POST", "/keys", jsonStr)
2018-03-10 09:31:52 -08:00
assert.Equal(t, http.StatusBadRequest, res.StatusCode, "Account creation should require a seed")
2018-03-10 09:31:52 -08:00
2018-03-17 17:42:18 -07:00
jsonStr = []byte(fmt.Sprintf(`{"name":"%s", "password":"%s", "seed": "%s"}`, newName, newPassword, newSeed))
2018-03-12 08:31:27 -07:00
res, body = request(t, port, "POST", "/keys", jsonStr)
2018-03-10 09:31:52 -08:00
require.Equal(t, http.StatusOK, res.StatusCode, body)
addr := body
2018-03-10 09:31:52 -08:00
assert.Len(t, addr, 40, "Returned address has wrong format", addr)
2018-03-05 08:41:50 -08:00
// existing keys
2018-03-12 08:31:27 -07:00
res, body = request(t, port, "GET", "/keys", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-17 17:42:18 -07:00
var m [2]keys.KeyOutput
2018-04-07 10:56:49 -07:00
err = cdc.UnmarshalJSON([]byte(body), &m)
2018-03-12 08:31:27 -07:00
require.Nil(t, err)
2018-03-05 08:41:50 -08:00
sendAddrAcc, _ := sdk.GetAccAddressHex(sendAddr)
addrAcc, _ := sdk.GetAccAddressHex(addr)
2018-03-17 17:42:18 -07:00
assert.Equal(t, m[0].Name, name, "Did not serve keys name correctly")
assert.Equal(t, m[0].Address, sendAddrAcc, "Did not serve keys Address correctly")
2018-03-17 17:42:18 -07:00
assert.Equal(t, m[1].Name, newName, "Did not serve keys name correctly")
assert.Equal(t, m[1].Address, addrAcc, "Did not serve keys Address correctly")
2018-03-05 08:41:50 -08:00
// select key
2018-03-17 17:42:18 -07:00
keyEndpoint := fmt.Sprintf("/keys/%s", newName)
res, body = request(t, port, "GET", keyEndpoint, nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-05 08:41:50 -08:00
var m2 keys.KeyOutput
2018-04-07 10:56:49 -07:00
err = cdc.UnmarshalJSON([]byte(body), &m2)
require.Nil(t, err)
2018-03-05 08:41:50 -08:00
2018-03-17 17:42:18 -07:00
assert.Equal(t, newName, m2.Name, "Did not serve keys name correctly")
assert.Equal(t, addrAcc, m2.Address, "Did not serve keys Address correctly")
2018-03-05 08:41:50 -08:00
// update key
2018-03-17 17:42:18 -07:00
jsonStr = []byte(fmt.Sprintf(`{"old_password":"%s", "new_password":"12345678901"}`, newPassword))
res, body = request(t, port, "PUT", keyEndpoint, jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-05 08:41:50 -08:00
// here it should say unauthorized as we changed the password before
2018-03-17 17:42:18 -07:00
res, body = request(t, port, "PUT", keyEndpoint, jsonStr)
require.Equal(t, http.StatusUnauthorized, res.StatusCode, body)
2018-03-05 08:41:50 -08:00
// delete key
jsonStr = []byte(`{"password":"12345678901"}`)
2018-03-17 17:42:18 -07:00
res, body = request(t, port, "DELETE", keyEndpoint, jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-12 07:47:26 -07:00
}
2018-03-05 08:41:50 -08:00
2018-03-09 02:54:19 -08:00
func TestVersion(t *testing.T) {
// node info
2018-03-12 08:31:27 -07:00
res, body := request(t, port, "GET", "/version", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-09 02:54:19 -08:00
2018-03-10 09:36:15 -08:00
reg, err := regexp.Compile(`\d+\.\d+\.\d+(-dev)?`)
require.Nil(t, err)
match := reg.MatchString(body)
assert.True(t, match, body)
2018-03-09 02:54:19 -08:00
}
2018-03-09 02:03:15 -08:00
func TestNodeStatus(t *testing.T) {
2018-03-08 06:50:12 -08:00
2018-03-09 02:03:15 -08:00
// node info
2018-03-12 08:31:27 -07:00
res, body := request(t, port, "GET", "/node_info", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-08 06:50:12 -08:00
var nodeInfo p2p.NodeInfo
2018-04-07 10:56:49 -07:00
err := cdc.UnmarshalJSON([]byte(body), &nodeInfo)
2018-03-08 07:06:40 -08:00
require.Nil(t, err, "Couldn't parse node info")
2018-03-08 06:50:12 -08:00
assert.NotEqual(t, p2p.NodeInfo{}, nodeInfo, "res: %v", res)
2018-03-09 02:03:15 -08:00
// syncing
2018-03-12 08:31:27 -07:00
res, body = request(t, port, "GET", "/syncing", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-09 02:03:15 -08:00
// we expect that there is no other node running so the syncing state is "false"
// we c
assert.Equal(t, "false", body)
}
2018-03-09 02:03:15 -08:00
func TestBlock(t *testing.T) {
tests.WaitForHeight(2, port)
2018-03-14 03:43:31 -07:00
2018-03-13 08:42:29 -07:00
var resultBlock ctypes.ResultBlock
res, body := request(t, port, "GET", "/blocks/latest", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-04-07 10:56:49 -07:00
err := cdc.UnmarshalJSON([]byte(body), &resultBlock)
2018-03-13 08:42:29 -07:00
require.Nil(t, err, "Couldn't parse block")
2018-03-13 08:42:29 -07:00
assert.NotEqual(t, ctypes.ResultBlock{}, resultBlock)
2018-03-10 08:35:21 -08:00
// --
2018-03-13 08:42:29 -07:00
res, body = request(t, port, "GET", "/blocks/1", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-13 08:42:29 -07:00
err = json.Unmarshal([]byte(body), &resultBlock)
2018-03-10 11:14:13 -08:00
require.Nil(t, err, "Couldn't parse block")
assert.NotEqual(t, ctypes.ResultBlock{}, resultBlock)
2018-03-10 08:35:21 -08:00
// --
2018-03-13 08:42:29 -07:00
res, body = request(t, port, "GET", "/blocks/1000000000", nil)
require.Equal(t, http.StatusNotFound, res.StatusCode, body)
}
func TestValidators(t *testing.T) {
2018-03-14 03:43:31 -07:00
2018-03-13 08:42:29 -07:00
var resultVals ctypes.ResultValidators
res, body := request(t, port, "GET", "/validatorsets/latest", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-04-07 10:56:49 -07:00
err := cdc.UnmarshalJSON([]byte(body), &resultVals)
2018-03-13 08:42:29 -07:00
require.Nil(t, err, "Couldn't parse validatorset")
2018-03-13 08:42:29 -07:00
assert.NotEqual(t, ctypes.ResultValidators{}, resultVals)
2018-03-10 08:35:21 -08:00
// --
2018-03-13 08:42:29 -07:00
res, body = request(t, port, "GET", "/validatorsets/1", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-04-07 10:56:49 -07:00
err = cdc.UnmarshalJSON([]byte(body), &resultVals)
2018-03-10 11:14:13 -08:00
require.Nil(t, err, "Couldn't parse validatorset")
assert.NotEqual(t, ctypes.ResultValidators{}, resultVals)
2018-03-10 08:35:21 -08:00
// --
2018-03-13 08:42:29 -07:00
res, body = request(t, port, "GET", "/validatorsets/1000000000", nil)
require.Equal(t, http.StatusNotFound, res.StatusCode)
2018-03-08 06:50:12 -08:00
}
2018-03-10 09:33:05 -08:00
func TestCoinSend(t *testing.T) {
2018-03-10 10:54:14 -08:00
2018-03-10 09:33:05 -08:00
// query empty
//res, body := request(t, port, "GET", "/accounts/8FA6AB57AD6870F6B5B2E57735F38F2F30E73CB6", nil)
2018-03-14 03:43:31 -07:00
res, body := request(t, port, "GET", "/accounts/8FA6AB57AD6870F6B5B2E57735F38F2F30E73CB6", nil)
require.Equal(t, http.StatusNoContent, res.StatusCode, body)
2018-03-10 09:33:05 -08:00
2018-04-07 10:56:49 -07:00
acc := getAccount(t, sendAddr)
initialBalance := acc.GetCoins()
2018-03-14 05:01:55 -07:00
// create TX
2018-03-17 17:42:18 -07:00
receiveAddr, resultTx := doSend(t, port, seed)
tests.WaitForHeight(resultTx.Height+1, port)
2018-03-14 08:18:47 -07:00
// check if tx was commited
assert.Equal(t, uint32(0), resultTx.CheckTx.Code)
assert.Equal(t, uint32(0), resultTx.DeliverTx.Code)
2018-03-14 03:43:31 -07:00
2018-03-14 05:01:55 -07:00
// query sender
2018-04-07 10:56:49 -07:00
acc = getAccount(t, sendAddr)
coins := acc.GetCoins()
2018-03-14 08:18:47 -07:00
mycoins := coins[0]
2018-03-17 17:42:18 -07:00
assert.Equal(t, coinDenom, mycoins.Denom)
2018-04-07 10:56:49 -07:00
assert.Equal(t, initialBalance[0].Amount-1, mycoins.Amount)
2018-03-10 09:33:05 -08:00
2018-03-14 05:01:55 -07:00
// query receiver
2018-04-07 10:56:49 -07:00
acc = getAccount(t, receiveAddr)
coins = acc.GetCoins()
2018-03-14 08:18:47 -07:00
mycoins = coins[0]
2018-03-17 17:42:18 -07:00
assert.Equal(t, coinDenom, mycoins.Denom)
2018-03-14 08:18:47 -07:00
assert.Equal(t, int64(1), mycoins.Amount)
2018-03-10 09:33:05 -08:00
}
2018-03-19 10:13:47 -07:00
func TestIBCTransfer(t *testing.T) {
2018-04-07 10:56:49 -07:00
acc := getAccount(t, sendAddr)
initialBalance := acc.GetCoins()
2018-03-19 10:13:47 -07:00
// create TX
2018-03-20 03:53:02 -07:00
resultTx := doIBCTransfer(t, port, seed)
2018-03-19 10:13:47 -07:00
tests.WaitForHeight(resultTx.Height+1, port)
2018-03-19 10:13:47 -07:00
// check if tx was commited
assert.Equal(t, uint32(0), resultTx.CheckTx.Code)
assert.Equal(t, uint32(0), resultTx.DeliverTx.Code)
// query sender
2018-04-07 10:56:49 -07:00
acc = getAccount(t, sendAddr)
coins := acc.GetCoins()
2018-03-19 10:13:47 -07:00
mycoins := coins[0]
assert.Equal(t, coinDenom, mycoins.Denom)
2018-04-07 10:56:49 -07:00
assert.Equal(t, initialBalance[0].Amount-1, mycoins.Amount)
2018-03-19 10:13:47 -07:00
2018-03-20 03:53:02 -07:00
// TODO: query ibc egress packet state
2018-03-19 10:13:47 -07:00
}
2018-03-14 05:01:55 -07:00
func TestTxs(t *testing.T) {
2018-03-14 10:28:00 -07:00
// TODO: re-enable once we can get txs by tag
2018-03-14 05:01:55 -07:00
// query wrong
2018-03-14 10:28:00 -07:00
// res, body := request(t, port, "GET", "/txs", nil)
// require.Equal(t, http.StatusBadRequest, res.StatusCode, body)
2018-03-14 05:01:55 -07:00
// query empty
2018-03-14 10:28:00 -07:00
// res, body = request(t, port, "GET", fmt.Sprintf("/txs?tag=coin.sender='%s'", "8FA6AB57AD6870F6B5B2E57735F38F2F30E73CB6"), nil)
// require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-14 05:01:55 -07:00
2018-03-14 10:28:00 -07:00
// assert.Equal(t, "[]", body)
2018-03-14 05:01:55 -07:00
// create TX
2018-03-17 17:42:18 -07:00
_, resultTx := doSend(t, port, seed)
2018-03-14 05:01:55 -07:00
tests.WaitForHeight(resultTx.Height+1, port)
2018-03-14 08:18:47 -07:00
// check if tx is findable
2018-03-14 10:28:00 -07:00
res, body := request(t, port, "GET", fmt.Sprintf("/txs/%s", resultTx.Hash), nil)
2018-03-14 05:01:55 -07:00
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-14 08:18:47 -07:00
// // query sender
// res, body = request(t, port, "GET", fmt.Sprintf("/txs?tag=coin.sender='%s'", addr), nil)
// require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-14 05:01:55 -07:00
2018-03-14 08:18:47 -07:00
// assert.NotEqual(t, "[]", body)
2018-03-14 05:01:55 -07:00
2018-03-14 08:18:47 -07:00
// // query receiver
// res, body = request(t, port, "GET", fmt.Sprintf("/txs?tag=coin.receiver='%s'", receiveAddr), nil)
// require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-03-14 05:01:55 -07:00
2018-03-14 08:18:47 -07:00
// assert.NotEqual(t, "[]", body)
2018-03-14 05:01:55 -07:00
}
2018-05-28 23:07:03 -07:00
func TestValidatorsQuery(t *testing.T) {
validators := getValidators(t)
assert.Equal(t, len(validators), 2)
// make sure all the validators were found (order unknown because sorted by owner addr)
foundVal1, foundVal2 := false, false
res1, res2 := hex.EncodeToString(validators[0].Owner), hex.EncodeToString(validators[1].Owner)
if res1 == validatorAddr1 || res2 == validatorAddr1 {
foundVal1 = true
}
if res1 == validatorAddr2 || res2 == validatorAddr2 {
foundVal2 = true
}
assert.True(t, foundVal1, "validatorAddr1 %v, res1 %v, res2 %v", validatorAddr1, res1, res2)
assert.True(t, foundVal2, "validatorAddr2 %v, res1 %v, res2 %v", validatorAddr2, res1, res2)
2018-05-28 23:07:03 -07:00
}
2018-05-14 17:43:46 -07:00
func TestBond(t *testing.T) {
// create bond TX
resultTx := doBond(t, port, seed)
tests.WaitForHeight(resultTx.Height+1, port)
// check if tx was commited
assert.Equal(t, uint32(0), resultTx.CheckTx.Code)
assert.Equal(t, uint32(0), resultTx.DeliverTx.Code)
// query sender
2018-05-20 18:46:09 -07:00
acc := getAccount(t, sendAddr)
2018-05-14 17:43:46 -07:00
coins := acc.GetCoins()
2018-05-26 01:37:39 -07:00
assert.Equal(t, int64(87), coins.AmountOf(coinDenom))
2018-05-14 17:43:46 -07:00
// query candidate
bond := getDelegation(t, sendAddr, validatorAddr1)
2018-05-26 01:37:39 -07:00
assert.Equal(t, "10/1", bond.Shares.String())
2018-05-14 17:43:46 -07:00
}
func TestUnbond(t *testing.T) {
// create unbond TX
resultTx := doUnbond(t, port, seed)
tests.WaitForHeight(resultTx.Height+1, port)
// check if tx was commited
assert.Equal(t, uint32(0), resultTx.CheckTx.Code)
assert.Equal(t, uint32(0), resultTx.DeliverTx.Code)
// query sender
2018-05-20 18:46:09 -07:00
acc := getAccount(t, sendAddr)
2018-05-14 17:43:46 -07:00
coins := acc.GetCoins()
2018-05-26 01:37:39 -07:00
assert.Equal(t, int64(98), coins.AmountOf(coinDenom))
2018-05-14 17:43:46 -07:00
// query candidate
bond := getDelegation(t, sendAddr, validatorAddr1)
2018-05-26 01:37:39 -07:00
assert.Equal(t, "9/1", bond.Shares.String())
2018-05-14 17:43:46 -07:00
}
2018-03-08 07:06:40 -08:00
//__________________________________________________________
// helpers
2018-03-17 17:42:18 -07:00
// strt TM and the LCD in process, listening on their respective sockets
func startTMAndLCD() (*nm.Node, net.Listener, error) {
2018-03-08 06:50:12 -08:00
dir, err := ioutil.TempDir("", "lcd_test")
if err != nil {
return nil, nil, err
}
viper.Set(cli.HomeFlag, dir)
2018-03-17 17:42:18 -07:00
kb, err := keys.GetKeyBase() // dbm.NewMemDB()) // :(
if err != nil {
return nil, nil, err
}
config := GetConfig()
config.Consensus.TimeoutCommit = 1000
config.Consensus.SkipTimeoutCommit = false
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger = log.NewFilter(logger, log.AllowError())
2018-03-17 17:42:18 -07:00
privValidatorFile := config.PrivValidatorFile()
2018-04-06 17:25:08 -07:00
privVal := pvm.LoadOrGenFilePV(privValidatorFile)
db := dbm.NewMemDB()
2018-05-20 18:46:09 -07:00
app := gapp.NewGaiaApp(logger, db)
cdc = gapp.MakeCodec() // XXX
2018-03-17 17:42:18 -07:00
genesisFile := config.GenesisFile()
genDoc, err := tmtypes.GenesisDocFromFile(genesisFile)
if err != nil {
return nil, nil, err
}
genDoc.Validators = append(genDoc.Validators,
tmtypes.GenesisValidator{
PubKey: crypto.GenPrivKeyEd25519().PubKey(),
Power: 1,
2018-05-20 18:46:09 -07:00
Name: "val",
},
)
pk1 := genDoc.Validators[0].PubKey
pk2 := genDoc.Validators[1].PubKey
validatorAddr1 = hex.EncodeToString(pk1.Address())
validatorAddr2 = hex.EncodeToString(pk2.Address())
// NOTE it's bad practice to reuse pk address for the owner address but doing in the
// test for simplicity
var appGenTxs [2]json.RawMessage
appGenTxs[0], _, _, err = gapp.GaiaAppGenTxNF(cdc, pk1, pk1.Address(), "test_val1", true)
if err != nil {
return nil, nil, err
2018-05-20 18:46:09 -07:00
}
appGenTxs[1], _, _, err = gapp.GaiaAppGenTxNF(cdc, pk2, pk2.Address(), "test_val2", true)
if err != nil {
return nil, nil, err
}
genesisState, err := gapp.GaiaAppGenState(cdc, appGenTxs[:])
if err != nil {
return nil, nil, err
}
// add the sendAddr to genesis
var info cryptoKeys.Info
info, seed, err = kb.Create(name, password, cryptoKeys.AlgoEd25519) // XXX global seed
if err != nil {
return nil, nil, err
2018-03-17 17:42:18 -07:00
}
sendAddr = info.PubKey.Address().String() // XXX global
accAuth := auth.NewBaseAccountWithAddress(info.PubKey.Address())
accAuth.Coins = sdk.Coins{{"steak", 100}}
acc := gapp.NewGenesisAccount(&accAuth)
genesisState.Accounts = append(genesisState.Accounts, acc)
appState, err := wire.MarshalJSONIndent(cdc, genesisState)
if err != nil {
return nil, nil, err
}
genDoc.AppStateJSON = appState
2018-03-17 17:42:18 -07:00
// LCD listen address
port = fmt.Sprintf("%d", 17377) // XXX
listenAddr := fmt.Sprintf("tcp://localhost:%s", port) // XXX
2018-03-17 17:42:18 -07:00
// XXX: need to set this so LCD knows the tendermint node address!
viper.Set(client.FlagNode, config.RPC.ListenAddress)
viper.Set(client.FlagChainID, genDoc.ChainID)
2018-03-17 17:42:18 -07:00
node, err := startTM(config, logger, genDoc, privVal, app)
if err != nil {
return nil, nil, err
}
2018-05-30 07:42:59 -07:00
lcd, err := startLCD(logger, listenAddr, cdc)
2018-03-17 17:42:18 -07:00
if err != nil {
return nil, nil, err
}
2018-03-17 17:42:18 -07:00
tests.WaitForStart(port)
2018-03-17 17:42:18 -07:00
return node, lcd, nil
2018-03-05 08:41:50 -08:00
}
// Create & start in-process tendermint node with memdb
// and in-process abci application.
// TODO: need to clean up the WAL dir or enable it to be not persistent
2018-03-17 17:42:18 -07:00
func startTM(cfg *tmcfg.Config, logger log.Logger, genDoc *tmtypes.GenesisDoc, privVal tmtypes.PrivValidator, app abci.Application) (*nm.Node, error) {
genDocProvider := func() (*tmtypes.GenesisDoc, error) { return genDoc, nil }
dbProvider := func(*nm.DBContext) (dbm.DB, error) { return dbm.NewMemDB(), nil }
n, err := nm.NewNode(cfg,
privVal,
proxy.NewLocalClientCreator(app),
genDocProvider,
dbProvider,
logger.With("module", "node"))
if err != nil {
return nil, err
}
err = n.Start()
if err != nil {
return nil, err
}
2018-03-17 17:42:18 -07:00
// wait for rpc
tests.WaitForRPC(GetConfig().RPC.ListenAddress)
2018-03-17 17:42:18 -07:00
logger.Info("Tendermint running!")
return n, err
}
// start the LCD. note this blocks!
2018-05-30 07:42:59 -07:00
func startLCD(logger log.Logger, listenAddr string, cdc *wire.Codec) (net.Listener, error) {
handler := createHandler(cdc)
2018-03-17 17:42:18 -07:00
return tmrpc.StartHTTPServer(listenAddr, handler, logger)
}
2018-03-12 08:31:27 -07:00
func request(t *testing.T, port, method, path string, payload []byte) (*http.Response, string) {
var res *http.Response
var err error
2018-03-12 08:31:27 -07:00
url := fmt.Sprintf("http://localhost:%v%v", port, path)
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
require.Nil(t, err)
res, err = http.DefaultClient.Do(req)
// res, err = http.Post(url, "application/json", bytes.NewBuffer(payload))
2018-03-12 08:31:27 -07:00
require.Nil(t, err)
2018-03-10 08:15:56 -08:00
output, err := ioutil.ReadAll(res.Body)
res.Body.Close()
2018-03-12 08:31:27 -07:00
require.Nil(t, err)
2018-03-10 09:33:05 -08:00
return res, string(output)
2018-03-10 08:35:21 -08:00
}
2018-03-14 05:01:55 -07:00
2018-05-23 22:09:01 -07:00
func getAccount(t *testing.T, sendAddr string) auth.Account {
2018-04-07 10:56:49 -07:00
// get the account to get the sequence
res, body := request(t, port, "GET", "/accounts/"+sendAddr, nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-05-23 22:09:01 -07:00
var acc auth.Account
2018-04-07 10:56:49 -07:00
err := cdc.UnmarshalJSON([]byte(body), &acc)
require.Nil(t, err)
return acc
}
2018-03-17 17:42:18 -07:00
func doSend(t *testing.T, port, seed string) (receiveAddr string, resultTx ctypes.ResultBroadcastTxCommit) {
2018-03-14 05:01:55 -07:00
// create receive address
kb := client.MockKeyBase()
receiveInfo, _, err := kb.Create("receive_address", "1234567890", cryptoKeys.CryptoAlgo("ed25519"))
require.Nil(t, err)
receiveAddr = receiveInfo.PubKey.Address().String()
2018-04-07 10:56:49 -07:00
acc := getAccount(t, sendAddr)
sequence := acc.GetSequence()
2018-03-17 17:42:18 -07:00
2018-03-14 05:01:55 -07:00
// send
2018-03-17 17:42:18 -07:00
jsonStr := []byte(fmt.Sprintf(`{ "name":"%s", "password":"%s", "sequence":%d, "amount":[{ "denom": "%s", "amount": 1 }] }`, name, password, sequence, coinDenom))
2018-04-07 10:56:49 -07:00
res, body := request(t, port, "POST", "/accounts/"+receiveAddr+"/send", jsonStr)
2018-03-14 05:01:55 -07:00
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-04-07 10:56:49 -07:00
err = cdc.UnmarshalJSON([]byte(body), &resultTx)
2018-03-14 08:18:47 -07:00
require.Nil(t, err)
2018-03-17 17:42:18 -07:00
return receiveAddr, resultTx
2018-03-14 05:01:55 -07:00
}
2018-03-19 10:13:47 -07:00
2018-03-20 03:53:02 -07:00
func doIBCTransfer(t *testing.T, port, seed string) (resultTx ctypes.ResultBroadcastTxCommit) {
2018-03-19 10:13:47 -07:00
// create receive address
kb := client.MockKeyBase()
receiveInfo, _, err := kb.Create("receive_address", "1234567890", cryptoKeys.CryptoAlgo("ed25519"))
require.Nil(t, err)
2018-03-20 03:53:02 -07:00
receiveAddr := receiveInfo.PubKey.Address().String()
2018-03-19 10:13:47 -07:00
// get the account to get the sequence
2018-04-07 10:56:49 -07:00
acc := getAccount(t, sendAddr)
sequence := acc.GetSequence()
2018-03-19 10:13:47 -07:00
// send
jsonStr := []byte(fmt.Sprintf(`{ "name":"%s", "password":"%s", "sequence":%d, "amount":[{ "denom": "%s", "amount": 1 }] }`, name, password, sequence, coinDenom))
2018-04-07 10:56:49 -07:00
res, body := request(t, port, "POST", "/ibc/testchain/"+receiveAddr+"/send", jsonStr)
2018-03-19 10:13:47 -07:00
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-04-07 10:56:49 -07:00
err = cdc.UnmarshalJSON([]byte(body), &resultTx)
2018-03-19 10:13:47 -07:00
require.Nil(t, err)
2018-03-20 03:53:02 -07:00
return resultTx
2018-03-19 10:13:47 -07:00
}
2018-05-14 17:43:46 -07:00
func getDelegation(t *testing.T, delegatorAddr, candidateAddr string) stake.Delegation {
2018-05-14 17:43:46 -07:00
// get the account to get the sequence
2018-05-20 18:46:09 -07:00
res, body := request(t, port, "GET", "/stake/"+delegatorAddr+"/bonding_status/"+candidateAddr, nil)
2018-05-14 17:43:46 -07:00
require.Equal(t, http.StatusOK, res.StatusCode, body)
var bond stake.Delegation
2018-05-14 17:43:46 -07:00
err := cdc.UnmarshalJSON([]byte(body), &bond)
require.Nil(t, err)
return bond
}
func doBond(t *testing.T, port, seed string) (resultTx ctypes.ResultBroadcastTxCommit) {
// get the account to get the sequence
acc := getAccount(t, sendAddr)
sequence := acc.GetSequence()
// send
jsonStr := []byte(fmt.Sprintf(`{
"name": "%s",
"password": "%s",
"sequence": %d,
2018-05-26 01:37:39 -07:00
"delegate": [
2018-05-14 17:43:46 -07:00
{
2018-05-26 01:37:39 -07:00
"delegator_addr": "%x",
"validator_addr": "%s",
"bond": { "denom": "%s", "amount": 10 }
2018-05-14 17:43:46 -07:00
}
],
"unbond": []
2018-05-26 01:37:39 -07:00
}`, name, password, sequence, acc.GetAddress(), validatorAddr1, coinDenom))
res, body := request(t, port, "POST", "/stake/delegations", jsonStr)
2018-05-14 17:43:46 -07:00
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-05-20 18:46:09 -07:00
var results []ctypes.ResultBroadcastTxCommit
err := cdc.UnmarshalJSON([]byte(body), &results)
2018-05-14 17:43:46 -07:00
require.Nil(t, err)
2018-05-20 18:46:09 -07:00
return results[0]
2018-05-14 17:43:46 -07:00
}
func doUnbond(t *testing.T, port, seed string) (resultTx ctypes.ResultBroadcastTxCommit) {
// get the account to get the sequence
acc := getAccount(t, sendAddr)
sequence := acc.GetSequence()
// send
jsonStr := []byte(fmt.Sprintf(`{
"name": "%s",
"password": "%s",
"sequence": %d,
"bond": [],
"unbond": [
{
2018-05-26 01:37:39 -07:00
"delegator_addr": "%x",
"validator_addr": "%s",
2018-05-14 17:43:46 -07:00
"shares": "1"
}
]
2018-05-26 01:37:39 -07:00
}`, name, password, sequence, acc.GetAddress(), validatorAddr1))
res, body := request(t, port, "POST", "/stake/delegations", jsonStr)
2018-05-14 17:43:46 -07:00
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-05-20 18:46:09 -07:00
var results []ctypes.ResultBroadcastTxCommit
err := cdc.UnmarshalJSON([]byte(body), &results)
2018-05-14 17:43:46 -07:00
require.Nil(t, err)
2018-05-20 18:46:09 -07:00
return results[0]
2018-05-14 17:43:46 -07:00
}
2018-05-28 23:07:03 -07:00
func getValidators(t *testing.T) []stake.Validator {
2018-05-14 17:43:46 -07:00
// get the account to get the sequence
2018-05-28 23:07:03 -07:00
res, body := request(t, port, "GET", "/stake/validators", nil)
2018-05-14 17:43:46 -07:00
require.Equal(t, http.StatusOK, res.StatusCode, body)
2018-05-28 23:07:03 -07:00
var validators stake.Validators
err := cdc.UnmarshalJSON([]byte(body), &validators)
2018-05-14 17:43:46 -07:00
require.Nil(t, err)
2018-05-28 23:07:03 -07:00
return validators
2018-05-14 17:43:46 -07:00
}