cosmos-sdk/client/lcd/lcd_test.go

728 lines
21 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"
bapp "github.com/cosmos/cosmos-sdk/examples/basecoin/app"
btypes "github.com/cosmos/cosmos-sdk/examples/basecoin/types"
tests "github.com/cosmos/cosmos-sdk/tests"
2018-03-17 17:42:18 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
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 = "mycoin"
coinAmount = int64(10000000)
2018-05-14 17:43:46 -07:00
stakeDenom = "steak"
candidateAddr1 = ""
candidateAddr2 = ""
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
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, sendAddr, "Did not serve keys Address correctly")
assert.Equal(t, m[1].Name, newName, "Did not serve keys name correctly")
assert.Equal(t, m[1].Address, addr, "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, addr, 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
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-19 22:59:29 -07:00
func TestCandidates(t *testing.T) {
candidates := getCandidates(t)
assert.Equal(t, len(candidates), 2)
assert.Equal(t, hex.EncodeToString(candidates[0].Address), candidateAddr1)
assert.Equal(t, hex.EncodeToString(candidates[1].Address), candidateAddr2)
}
2018-05-14 17:43:46 -07:00
func TestBond(t *testing.T) {
acc := getAccount(t, sendAddr)
initialBalance := acc.GetCoins()
// 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
acc = getAccount(t, sendAddr)
coins := acc.GetCoins()
mycoins := coins[0]
assert.Equal(t, coinDenom, mycoins.Denom)
assert.Equal(t, initialBalance[0].Amount-1, mycoins.Amount)
// query candidate
bond := getDelegatorBond(t, sendAddr, candidateAddr1)
assert.Equal(t, "foo", bond.Shares.String())
}
func TestUnbond(t *testing.T) {
acc := getAccount(t, sendAddr)
initialBalance := acc.GetCoins()
// 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
acc = getAccount(t, sendAddr)
coins := acc.GetCoins()
mycoins := coins[0]
assert.Equal(t, coinDenom, mycoins.Denom)
assert.Equal(t, initialBalance[0].Amount, mycoins.Amount)
// query candidate
bond := getDelegatorBond(t, sendAddr, candidateAddr1)
assert.Equal(t, "foo", bond.Shares.String())
}
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
}
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-14 03:43:31 -07:00
}
2018-03-17 17:42:18 -07:00
pubKey := info.PubKey
sendAddr = pubKey.Address().String() // XXX global
config := GetConfig()
config.Consensus.TimeoutCommit = 1000
config.Consensus.SkipTimeoutCommit = false
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
2018-05-19 22:59:29 -07:00
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()
app := bapp.NewBasecoinApp(logger, db)
2018-04-07 10:56:49 -07:00
cdc = bapp.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,
Name: "val1",
},
tmtypes.GenesisValidator{
PubKey: crypto.GenPrivKeyEd25519().PubKey(),
Power: 1,
Name: "val2",
},
)
2018-05-19 22:59:29 -07:00
candidateAddr1 = hex.EncodeToString(genDoc.Validators[1].PubKey.Address())
candidateAddr2 = hex.EncodeToString(genDoc.Validators[2].PubKey.Address())
coins := sdk.Coins{{coinDenom, coinAmount}}
appState := map[string]interface{}{
"accounts": []*btypes.GenesisAccount{
2018-03-17 17:42:18 -07:00
{
Name: "tester",
Address: pubKey.Address(),
Coins: coins,
},
},
"stake": stake.GenesisState{
Pool: stake.Pool{
TotalSupply: 1650,
BondedShares: sdk.NewRat(200, 1),
UnbondedShares: sdk.ZeroRat(),
BondedPool: 200,
UnbondedPool: 0,
InflationLastTime: 0,
Inflation: sdk.NewRat(7, 100),
},
Params: stake.Params{
InflationRateChange: sdk.NewRat(13, 100),
InflationMax: sdk.NewRat(1, 5),
InflationMin: sdk.NewRat(7, 100),
GoalBonded: sdk.NewRat(67, 100),
MaxValidators: 100,
BondDenom: stakeDenom,
},
Candidates: []stake.Candidate{
2018-05-14 17:43:46 -07:00
{
Status: 1,
2018-05-19 22:59:29 -07:00
Address: genDoc.Validators[1].PubKey.Address(),
PubKey: genDoc.Validators[1].PubKey,
Assets: sdk.NewRat(100, 1),
Liabilities: sdk.ZeroRat(),
Description: stake.Description{
Moniker: "validator1",
},
ValidatorBondHeight: 0,
ValidatorBondCounter: 0,
},
{
Status: 1,
2018-05-19 22:59:29 -07:00
Address: genDoc.Validators[2].PubKey.Address(),
PubKey: genDoc.Validators[2].PubKey,
Assets: sdk.NewRat(100, 1),
Liabilities: sdk.ZeroRat(),
Description: stake.Description{
Moniker: "validator2",
},
ValidatorBondHeight: 0,
ValidatorBondCounter: 0,
},
},
},
2018-03-17 17:42:18 -07:00
}
stateBytes, err := json.Marshal(appState)
if err != nil {
return nil, nil, err
}
genDoc.AppStateJSON = stateBytes
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-04-07 10:56:49 -07:00
lcd, err := startLCD(logger, listenAddr)
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-04-07 10:56:49 -07:00
func startLCD(logger log.Logger, listenAddr string) (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-04-07 10:56:49 -07:00
func getAccount(t *testing.T, sendAddr string) sdk.Account {
// get the account to get the sequence
res, body := request(t, port, "GET", "/accounts/"+sendAddr, nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var acc sdk.Account
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 getDelegatorBond(t *testing.T, delegatorAddr, candidateAddr string) stake.DelegatorBond {
// get the account to get the sequence
res, body := request(t, port, "GET", "/stake/"+delegatorAddr+"/bonding_info/"+candidateAddr, nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var bond stake.DelegatorBond
err := cdc.UnmarshalJSON([]byte(body), &bond)
require.Nil(t, err)
return bond
}
2018-05-19 22:59:29 -07:00
func getCandidates(t *testing.T) []stake.Candidate {
// get the account to get the sequence
res, body := request(t, port, "GET", "/stake/candidates", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var candidates []stake.Candidate
err := cdc.UnmarshalJSON([]byte(body), &candidates)
require.Nil(t, err)
return candidates
}
2018-05-14 17:43:46 -07:00
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,
"bond": [
{
"candidate": "%s",
"amount": { "denom": "%s", "amount": 100 }
}
],
"unbond": []
}`, name, password, sequence, candidateAddr1, stakeDenom))
res, body := request(t, port, "POST", "/stake/bondunbond", jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err := cdc.UnmarshalJSON([]byte(body), &resultTx)
require.Nil(t, err)
return
}
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": [
{
"candidate": "%s",
"shares": "1"
}
]
}`, name, password, sequence, candidateAddr1))
2018-05-14 17:43:46 -07:00
res, body := request(t, port, "POST", "/stake/bondunbond", jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err := cdc.UnmarshalJSON([]byte(body), &resultTx)
require.Nil(t, err)
return
}
func doMultiBond(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": [
{
"candidate": "%s",
"amount": { "denom": "%s", "amount": 1 }
},
{
"candidate": "%s",
"amount": { "denom": "%s", "amount": 1 }
},
],
"unbond": [
{
"candidate": "%s",
"shares": "1"
}
]
}`, name, password, sequence, candidateAddr1, stakeDenom, candidateAddr2, stakeDenom, candidateAddr1))
res, body := request(t, port, "POST", "/stake/bondunbond", jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err := cdc.UnmarshalJSON([]byte(body), &resultTx)
require.Nil(t, err)
return
}