tendermint/rpc/test/client_test.go

382 lines
9.7 KiB
Go
Raw Normal View History

2016-01-10 13:33:52 -08:00
package rpctest
import (
2016-07-05 11:41:50 -07:00
"bytes"
2016-08-22 13:00:48 -07:00
crand "crypto/rand"
2016-01-10 13:33:52 -08:00
"fmt"
2016-08-22 13:00:48 -07:00
"math/rand"
2016-01-10 13:33:52 -08:00
"testing"
2016-08-22 13:00:48 -07:00
"time"
2016-01-10 13:33:52 -08:00
2017-01-28 08:27:13 -08:00
abci "github.com/tendermint/abci/types"
. "github.com/tendermint/go-common"
2016-01-10 13:33:52 -08:00
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/tendermint/tendermint/types"
)
//--------------------------------------------------------------------------------
// Test the HTTP client
2016-08-22 13:00:48 -07:00
// These tests assume the dummy app
2016-03-02 22:18:11 -08:00
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// status
2016-01-10 13:33:52 -08:00
func TestURIStatus(t *testing.T) {
2016-01-13 15:38:55 -08:00
tmResult := new(ctypes.TMResult)
2017-02-21 10:36:18 -08:00
_, err := GetURIClient().Call("status", map[string]interface{}{}, tmResult)
2016-01-10 13:33:52 -08:00
if err != nil {
panic(err)
2016-01-10 13:33:52 -08:00
}
2016-01-13 15:38:55 -08:00
testStatus(t, tmResult)
2016-01-10 13:33:52 -08:00
}
func TestJSONStatus(t *testing.T) {
2016-01-13 15:38:55 -08:00
tmResult := new(ctypes.TMResult)
2017-02-21 10:36:18 -08:00
_, err := GetJSONClient().Call("status", []interface{}{}, tmResult)
2016-01-10 13:33:52 -08:00
if err != nil {
panic(err)
2016-01-10 13:33:52 -08:00
}
2016-01-13 15:38:55 -08:00
testStatus(t, tmResult)
2016-01-10 13:33:52 -08:00
}
2016-01-13 15:38:55 -08:00
func testStatus(t *testing.T, statusI interface{}) {
2017-02-21 10:36:18 -08:00
chainID := GetConfig().GetString("chain_id")
2016-01-13 15:38:55 -08:00
tmRes := statusI.(*ctypes.TMResult)
status := (*tmRes).(*ctypes.ResultStatus)
2016-01-10 13:33:52 -08:00
if status.NodeInfo.Network != chainID {
panic(Fmt("ChainID mismatch: got %s expected %s",
2016-01-10 13:33:52 -08:00
status.NodeInfo.Network, chainID))
}
}
2016-07-05 11:41:50 -07:00
//--------------------------------------------------------------------------------
// broadcast tx sync
2016-08-22 13:00:48 -07:00
// random bytes (excluding byte('='))
func randBytes() []byte {
n := rand.Intn(10) + 2
buf := make([]byte, n)
_, err := crand.Read(buf)
if err != nil {
panic(err)
}
2016-08-22 13:00:48 -07:00
return bytes.Replace(buf, []byte("="), []byte{100}, -1)
}
2016-07-05 11:41:50 -07:00
func TestURIBroadcastTxSync(t *testing.T) {
config.Set("block_size", 0)
defer config.Set("block_size", -1)
tmResult := new(ctypes.TMResult)
2016-08-22 13:00:48 -07:00
tx := randBytes()
2017-02-21 10:36:18 -08:00
_, err := GetURIClient().Call("broadcast_tx_sync", map[string]interface{}{"tx": tx}, tmResult)
2016-07-05 11:41:50 -07:00
if err != nil {
panic(err)
2016-07-05 11:41:50 -07:00
}
testBroadcastTxSync(t, tmResult, tx)
2016-07-05 11:41:50 -07:00
}
func TestJSONBroadcastTxSync(t *testing.T) {
config.Set("block_size", 0)
defer config.Set("block_size", -1)
tmResult := new(ctypes.TMResult)
2016-08-22 13:00:48 -07:00
tx := randBytes()
2017-02-21 10:36:18 -08:00
_, err := GetJSONClient().Call("broadcast_tx_sync", []interface{}{tx}, tmResult)
2016-07-05 11:41:50 -07:00
if err != nil {
panic(err)
2016-01-10 13:33:52 -08:00
}
testBroadcastTxSync(t, tmResult, tx)
2016-07-05 11:41:50 -07:00
}
func testBroadcastTxSync(t *testing.T, resI interface{}, tx []byte) {
2016-07-05 11:41:50 -07:00
tmRes := resI.(*ctypes.TMResult)
res := (*tmRes).(*ctypes.ResultBroadcastTx)
2017-01-12 12:53:32 -08:00
if res.Code != abci.CodeType_OK {
panic(Fmt("BroadcastTxSync got non-zero exit code: %v. %X; %s", res.Code, res.Data, res.Log))
2016-01-10 13:33:52 -08:00
}
2016-07-05 11:41:50 -07:00
mem := node.MempoolReactor().Mempool
if mem.Size() != 1 {
panic(Fmt("Mempool size should have been 1. Got %d", mem.Size()))
2016-01-10 13:33:52 -08:00
}
2016-07-05 11:41:50 -07:00
txs := mem.Reap(1)
if !bytes.Equal(txs[0], tx) {
2016-08-22 13:00:48 -07:00
panic(Fmt("Tx in mempool does not match test tx. Got %X, expected %X", txs[0], tx))
2016-01-10 13:33:52 -08:00
}
2016-07-05 11:41:50 -07:00
mem.Flush()
}
2016-08-22 13:00:48 -07:00
//--------------------------------------------------------------------------------
// query
func testTxKV() ([]byte, []byte, []byte) {
k := randBytes()
v := randBytes()
return k, v, []byte(Fmt("%s=%s", k, v))
}
func sendTx() ([]byte, []byte) {
tmResult := new(ctypes.TMResult)
k, v, tx := testTxKV()
2017-02-21 10:36:18 -08:00
_, err := GetJSONClient().Call("broadcast_tx_commit", []interface{}{tx}, tmResult)
2016-08-22 13:00:48 -07:00
if err != nil {
panic(err)
}
return k, v
}
2017-01-12 12:53:32 -08:00
func TestURIABCIQuery(t *testing.T) {
2016-08-22 13:00:48 -07:00
k, v := sendTx()
time.Sleep(time.Second)
tmResult := new(ctypes.TMResult)
2017-02-21 10:36:18 -08:00
_, err := GetURIClient().Call("abci_query", map[string]interface{}{"path": "", "data": k, "prove": false}, tmResult)
2016-08-22 13:00:48 -07:00
if err != nil {
panic(err)
}
2017-01-12 12:53:32 -08:00
testABCIQuery(t, tmResult, v)
2016-08-22 13:00:48 -07:00
}
2017-01-12 12:53:32 -08:00
func TestJSONABCIQuery(t *testing.T) {
2016-08-22 13:00:48 -07:00
k, v := sendTx()
tmResult := new(ctypes.TMResult)
2017-02-21 10:36:18 -08:00
_, err := GetJSONClient().Call("abci_query", []interface{}{"", k, false}, tmResult)
2016-08-22 13:00:48 -07:00
if err != nil {
panic(err)
}
2017-01-12 12:53:32 -08:00
testABCIQuery(t, tmResult, v)
2016-08-22 13:00:48 -07:00
}
2017-01-12 12:53:32 -08:00
func testABCIQuery(t *testing.T, statusI interface{}, value []byte) {
2016-08-22 13:00:48 -07:00
tmRes := statusI.(*ctypes.TMResult)
2017-01-28 08:27:13 -08:00
resQuery := (*tmRes).(*ctypes.ResultABCIQuery)
if !resQuery.Response.Code.IsOK() {
panic(Fmt("Query returned an err: %v", resQuery))
2016-08-22 13:00:48 -07:00
}
2016-11-22 18:28:57 -08:00
2016-08-22 13:00:48 -07:00
// XXX: specific to value returned by the dummy
2017-01-28 08:27:13 -08:00
if len(resQuery.Response.Value) == 0 {
panic(Fmt("Query error. Found no value"))
2016-08-22 13:00:48 -07:00
}
}
2016-07-05 11:41:50 -07:00
//--------------------------------------------------------------------------------
// broadcast tx commit
func TestURIBroadcastTxCommit(t *testing.T) {
tmResult := new(ctypes.TMResult)
2016-08-22 13:00:48 -07:00
tx := randBytes()
2017-02-21 10:36:18 -08:00
_, err := GetURIClient().Call("broadcast_tx_commit", map[string]interface{}{"tx": tx}, tmResult)
2016-07-05 11:41:50 -07:00
if err != nil {
panic(err)
2016-07-05 11:41:50 -07:00
}
testBroadcastTxCommit(t, tmResult, tx)
2016-07-05 11:41:50 -07:00
}
func TestJSONBroadcastTxCommit(t *testing.T) {
tmResult := new(ctypes.TMResult)
2016-08-22 13:00:48 -07:00
tx := randBytes()
2017-02-21 10:36:18 -08:00
_, err := GetJSONClient().Call("broadcast_tx_commit", []interface{}{tx}, tmResult)
2016-07-05 11:41:50 -07:00
if err != nil {
panic(err)
2016-07-05 11:41:50 -07:00
}
testBroadcastTxCommit(t, tmResult, tx)
2016-07-05 11:41:50 -07:00
}
func testBroadcastTxCommit(t *testing.T, resI interface{}, tx []byte) {
2016-07-05 11:41:50 -07:00
tmRes := resI.(*ctypes.TMResult)
res := (*tmRes).(*ctypes.ResultBroadcastTxCommit)
checkTx := res.CheckTx
2017-01-12 12:53:32 -08:00
if checkTx.Code != abci.CodeType_OK {
panic(Fmt("BroadcastTxCommit got non-zero exit code from CheckTx: %v. %X; %s", checkTx.Code, checkTx.Data, checkTx.Log))
}
2017-01-12 12:55:03 -08:00
deliverTx := res.DeliverTx
if deliverTx.Code != abci.CodeType_OK {
panic(Fmt("BroadcastTxCommit got non-zero exit code from CheckTx: %v. %X; %s", deliverTx.Code, deliverTx.Data, deliverTx.Log))
2016-07-05 11:41:50 -07:00
}
mem := node.MempoolReactor().Mempool
if mem.Size() != 0 {
panic(Fmt("Mempool size should have been 0. Got %d", mem.Size()))
2016-07-05 11:41:50 -07:00
}
// TODO: find tx in block
2016-07-05 11:41:50 -07:00
}
2016-01-10 13:33:52 -08:00
//--------------------------------------------------------------------------------
// Test the websocket service
var wsTyp = "JSONRPC"
// make a simple connection to the server
func TestWSConnect(t *testing.T) {
2017-02-21 10:36:18 -08:00
wsc := GetWSClient()
2016-01-13 18:20:25 -08:00
wsc.Stop()
2016-01-10 13:33:52 -08:00
}
// receive a new block message
func TestWSNewBlock(t *testing.T) {
2017-02-21 10:36:18 -08:00
wsc := GetWSClient()
2016-01-10 13:33:52 -08:00
eid := types.EventStringNewBlock()
2016-01-13 18:20:25 -08:00
subscribe(t, wsc, eid)
2016-01-10 13:33:52 -08:00
defer func() {
2016-01-13 18:20:25 -08:00
unsubscribe(t, wsc, eid)
wsc.Stop()
2016-01-10 13:33:52 -08:00
}()
2016-01-13 18:20:25 -08:00
waitForEvent(t, wsc, eid, true, func() {}, func(eid string, b interface{}) error {
2017-02-21 10:36:18 -08:00
// fmt.Println("Check:", b)
2016-01-10 13:33:52 -08:00
return nil
})
}
// receive a few new block messages in a row, with increasing height
func TestWSBlockchainGrowth(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
2017-02-21 10:36:18 -08:00
wsc := GetWSClient()
2016-01-10 13:33:52 -08:00
eid := types.EventStringNewBlock()
2016-01-13 18:20:25 -08:00
subscribe(t, wsc, eid)
2016-01-10 13:33:52 -08:00
defer func() {
2016-01-13 18:20:25 -08:00
unsubscribe(t, wsc, eid)
wsc.Stop()
2016-01-10 13:33:52 -08:00
}()
2016-01-13 18:20:25 -08:00
2016-01-10 13:33:52 -08:00
// listen for NewBlock, ensure height increases by 1
2016-01-13 18:20:25 -08:00
var initBlockN int
for i := 0; i < 3; i++ {
waitForEvent(t, wsc, eid, true, func() {}, func(eid string, eventData interface{}) error {
block := eventData.(types.EventDataNewBlock).Block
if i == 0 {
initBlockN = block.Header.Height
} else {
if block.Header.Height != initBlockN+i {
return fmt.Errorf("Expected block %d, got block %d", initBlockN+i, block.Header.Height)
}
}
return nil
})
}
2016-01-10 13:33:52 -08:00
}
2016-10-06 12:23:22 -07:00
func TestWSTxEvent(t *testing.T) {
2017-02-21 10:36:18 -08:00
wsc := GetWSClient()
2016-10-06 12:23:22 -07:00
tx := randBytes()
// listen for the tx I am about to submit
eid := types.EventStringTx(types.Tx(tx))
subscribe(t, wsc, eid)
defer func() {
unsubscribe(t, wsc, eid)
wsc.Stop()
}()
// send an tx
tmResult := new(ctypes.TMResult)
2017-02-21 10:36:18 -08:00
_, err := GetJSONClient().Call("broadcast_tx_sync", []interface{}{tx}, tmResult)
2016-10-06 12:23:22 -07:00
if err != nil {
t.Fatal("Error submitting event")
}
waitForEvent(t, wsc, eid, true, func() {}, func(eid string, b interface{}) error {
evt, ok := b.(types.EventDataTx)
if !ok {
t.Fatal("Got wrong event type", b)
}
if bytes.Compare([]byte(evt.Tx), tx) != 0 {
t.Error("Event returned different tx")
}
2017-01-12 12:53:32 -08:00
if evt.Code != abci.CodeType_OK {
2016-10-06 12:23:22 -07:00
t.Error("Event returned tx error code", evt.Code)
}
return nil
})
}
2016-01-10 13:33:52 -08:00
/* TODO: this with dummy app..
func TestWSDoubleFire(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
con := newWSCon(t)
eid := types.EventStringAccInput(user[0].Address)
subscribe(t, con, eid)
defer func() {
unsubscribe(t, con, eid)
con.Close()
}()
amt := int64(100)
toAddr := user[1].Address
// broadcast the transaction, wait to hear about it
waitForEvent(t, con, eid, true, func() {
tx := makeDefaultSendTxSigned(t, wsTyp, toAddr, amt)
broadcastTx(t, wsTyp, tx)
}, func(eid string, b []byte) error {
return nil
})
// but make sure we don't hear about it twice
waitForEvent(t, con, eid, false, func() {
}, func(eid string, b []byte) error {
return nil
})
}*/
2016-05-11 20:33:09 -07:00
//--------------------------------------------------------------------------------
// unsafe_set_config
var stringVal = "my string"
var intVal = 987654321
var boolVal = true
// don't change these
var testCasesUnsafeSetConfig = [][]string{
[]string{"string", "key1", stringVal},
[]string{"int", "key2", fmt.Sprintf("%v", intVal)},
[]string{"bool", "key3", fmt.Sprintf("%v", boolVal)},
}
func TestURIUnsafeSetConfig(t *testing.T) {
for _, testCase := range testCasesUnsafeSetConfig {
tmResult := new(ctypes.TMResult)
2017-02-21 10:36:18 -08:00
_, err := GetURIClient().Call("unsafe_set_config", map[string]interface{}{
2016-05-11 20:33:09 -07:00
"type": testCase[0],
"key": testCase[1],
"value": testCase[2],
}, tmResult)
if err != nil {
panic(err)
2016-05-11 20:33:09 -07:00
}
}
testUnsafeSetConfig(t)
}
func TestJSONUnsafeSetConfig(t *testing.T) {
for _, testCase := range testCasesUnsafeSetConfig {
tmResult := new(ctypes.TMResult)
2017-02-21 10:36:18 -08:00
_, err := GetJSONClient().Call("unsafe_set_config", []interface{}{testCase[0], testCase[1], testCase[2]}, tmResult)
2016-05-11 20:33:09 -07:00
if err != nil {
panic(err)
2016-05-11 20:33:09 -07:00
}
}
testUnsafeSetConfig(t)
}
func testUnsafeSetConfig(t *testing.T) {
s := config.GetString("key1")
if s != stringVal {
panic(Fmt("got %v, expected %v", s, stringVal))
2016-05-11 20:33:09 -07:00
}
i := config.GetInt("key2")
if i != intVal {
panic(Fmt("got %v, expected %v", i, intVal))
2016-05-11 20:33:09 -07:00
}
b := config.GetBool("key3")
if b != boolVal {
panic(Fmt("got %v, expected %v", b, boolVal))
2016-05-11 20:33:09 -07:00
}
}