expose query and info through rpc

This commit is contained in:
Ethan Buchman 2016-08-22 16:00:48 -04:00
parent bfa690b6f7
commit 41918d619c
6 changed files with 128 additions and 10 deletions

View File

@ -209,6 +209,7 @@ func (n *Node) StartRPC() ([]net.Listener, error) {
rpccore.SetSwitch(n.sw) rpccore.SetSwitch(n.sw)
rpccore.SetPrivValidator(n.privValidator) rpccore.SetPrivValidator(n.privValidator)
rpccore.SetGenesisDoc(n.genesisDoc) rpccore.SetGenesisDoc(n.genesisDoc)
rpccore.SetProxyAppQuery(n.proxyApp.Query())
listenAddrs := strings.Split(n.config.GetString("rpc_laddr"), ",") listenAddrs := strings.Split(n.config.GetString("rpc_laddr"), ",")

View File

@ -8,6 +8,7 @@ import (
bc "github.com/tendermint/tendermint/blockchain" bc "github.com/tendermint/tendermint/blockchain"
"github.com/tendermint/tendermint/consensus" "github.com/tendermint/tendermint/consensus"
mempl "github.com/tendermint/tendermint/mempool" mempl "github.com/tendermint/tendermint/mempool"
"github.com/tendermint/tendermint/proxy"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
) )
@ -19,6 +20,7 @@ var mempoolReactor *mempl.MempoolReactor
var p2pSwitch *p2p.Switch var p2pSwitch *p2p.Switch
var privValidator *types.PrivValidator var privValidator *types.PrivValidator
var genDoc *types.GenesisDoc // cache the genesis structure var genDoc *types.GenesisDoc // cache the genesis structure
var proxyAppQuery proxy.AppConnQuery
var config cfg.Config = nil var config cfg.Config = nil
@ -57,3 +59,7 @@ func SetPrivValidator(pv *types.PrivValidator) {
func SetGenesisDoc(doc *types.GenesisDoc) { func SetGenesisDoc(doc *types.GenesisDoc) {
genDoc = doc genDoc = doc
} }
func SetProxyAppQuery(appConn proxy.AppConnQuery) {
proxyAppQuery = appConn
}

View File

@ -25,6 +25,9 @@ var Routes = map[string]*rpc.RPCFunc{
"unconfirmed_txs": rpc.NewRPCFunc(UnconfirmedTxsResult, ""), "unconfirmed_txs": rpc.NewRPCFunc(UnconfirmedTxsResult, ""),
"num_unconfirmed_txs": rpc.NewRPCFunc(NumUnconfirmedTxsResult, ""), "num_unconfirmed_txs": rpc.NewRPCFunc(NumUnconfirmedTxsResult, ""),
"tmsp_query": rpc.NewRPCFunc(TMSPQueryResult, "query"),
"tmsp_info": rpc.NewRPCFunc(TMSPInfoResult, ""),
"unsafe_flush_mempool": rpc.NewRPCFunc(UnsafeFlushMempool, ""), "unsafe_flush_mempool": rpc.NewRPCFunc(UnsafeFlushMempool, ""),
"unsafe_set_config": rpc.NewRPCFunc(UnsafeSetConfigResult, "type,key,value"), "unsafe_set_config": rpc.NewRPCFunc(UnsafeSetConfigResult, "type,key,value"),
"unsafe_start_cpu_profiler": rpc.NewRPCFunc(UnsafeStartCPUProfilerResult, "filename"), "unsafe_start_cpu_profiler": rpc.NewRPCFunc(UnsafeStartCPUProfilerResult, "filename"),
@ -152,6 +155,22 @@ func BroadcastTxAsyncResult(tx []byte) (ctypes.TMResult, error) {
} }
} }
func TMSPQueryResult(query []byte) (ctypes.TMResult, error) {
if r, err := TMSPQuery(query); err != nil {
return nil, err
} else {
return r, nil
}
}
func TMSPInfoResult() (ctypes.TMResult, error) {
if r, err := TMSPInfo(); err != nil {
return nil, err
} else {
return r, nil
}
}
func UnsafeFlushMempoolResult() (ctypes.TMResult, error) { func UnsafeFlushMempoolResult() (ctypes.TMResult, error) {
if r, err := UnsafeFlushMempool(); err != nil { if r, err := UnsafeFlushMempool(); err != nil {
return nil, err return nil, err

17
rpc/core/tmsp.go Normal file
View File

@ -0,0 +1,17 @@
package core
import (
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)
//-----------------------------------------------------------------------------
func TMSPQuery(query []byte) (*ctypes.ResultTMSPQuery, error) {
res := proxyAppQuery.QuerySync(query)
return &ctypes.ResultTMSPQuery{res}, nil
}
func TMSPInfo() (*ctypes.ResultTMSPInfo, error) {
res := proxyAppQuery.InfoSync()
return &ctypes.ResultTMSPInfo{res}, nil
}

View File

@ -68,6 +68,14 @@ type ResultUnconfirmedTxs struct {
Txs []types.Tx `json:"txs"` Txs []types.Tx `json:"txs"`
} }
type ResultTMSPInfo struct {
Result tmsp.Result `json:"result"`
}
type ResultTMSPQuery struct {
Result tmsp.Result `json:"result"`
}
type ResultUnsafeFlushMempool struct{} type ResultUnsafeFlushMempool struct{}
type ResultUnsafeSetConfig struct{} type ResultUnsafeSetConfig struct{}
@ -107,6 +115,10 @@ const (
ResultTypeBroadcastTx = byte(0x60) ResultTypeBroadcastTx = byte(0x60)
ResultTypeUnconfirmedTxs = byte(0x61) ResultTypeUnconfirmedTxs = byte(0x61)
// 0x7 bytes are for querying the application
ResultTypeTMSPQuery = byte(0x70)
ResultTypeTMSPInfo = byte(0x71)
// 0x8 bytes are for events // 0x8 bytes are for events
ResultTypeSubscribe = byte(0x80) ResultTypeSubscribe = byte(0x80)
ResultTypeUnsubscribe = byte(0x81) ResultTypeUnsubscribe = byte(0x81)
@ -145,4 +157,6 @@ var _ = wire.RegisterInterface(
wire.ConcreteType{&ResultUnsafeProfile{}, ResultTypeUnsafeStopCPUProfiler}, wire.ConcreteType{&ResultUnsafeProfile{}, ResultTypeUnsafeStopCPUProfiler},
wire.ConcreteType{&ResultUnsafeProfile{}, ResultTypeUnsafeWriteHeapProfile}, wire.ConcreteType{&ResultUnsafeProfile{}, ResultTypeUnsafeWriteHeapProfile},
wire.ConcreteType{&ResultUnsafeFlushMempool{}, ResultTypeUnsafeFlushMempool}, wire.ConcreteType{&ResultUnsafeFlushMempool{}, ResultTypeUnsafeFlushMempool},
wire.ConcreteType{&ResultTMSPQuery{}, ResultTypeTMSPQuery},
wire.ConcreteType{&ResultTMSPInfo{}, ResultTypeTMSPInfo},
) )

View File

@ -2,9 +2,12 @@ package rpctest
import ( import (
"bytes" "bytes"
"crypto/rand" crand "crypto/rand"
"fmt" "fmt"
"math/rand"
"strings"
"testing" "testing"
"time"
. "github.com/tendermint/go-common" . "github.com/tendermint/go-common"
ctypes "github.com/tendermint/tendermint/rpc/core/types" ctypes "github.com/tendermint/tendermint/rpc/core/types"
@ -14,6 +17,7 @@ import (
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// Test the HTTP client // Test the HTTP client
// These tests assume the dummy app
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
@ -49,20 +53,22 @@ func testStatus(t *testing.T, statusI interface{}) {
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// broadcast tx sync // broadcast tx sync
func testTx() []byte { // random bytes (excluding byte('='))
buf := make([]byte, 16) func randBytes() []byte {
_, err := rand.Read(buf) n := rand.Intn(10) + 2
buf := make([]byte, n)
_, err := crand.Read(buf)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return buf return bytes.Replace(buf, []byte("="), []byte{100}, -1)
} }
func TestURIBroadcastTxSync(t *testing.T) { func TestURIBroadcastTxSync(t *testing.T) {
config.Set("block_size", 0) config.Set("block_size", 0)
defer config.Set("block_size", -1) defer config.Set("block_size", -1)
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
tx := testTx() tx := randBytes()
_, err := clientURI.Call("broadcast_tx_sync", map[string]interface{}{"tx": tx}, tmResult) _, err := clientURI.Call("broadcast_tx_sync", map[string]interface{}{"tx": tx}, tmResult)
if err != nil { if err != nil {
panic(err) panic(err)
@ -74,7 +80,7 @@ func TestJSONBroadcastTxSync(t *testing.T) {
config.Set("block_size", 0) config.Set("block_size", 0)
defer config.Set("block_size", -1) defer config.Set("block_size", -1)
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
tx := testTx() tx := randBytes()
_, err := clientJSON.Call("broadcast_tx_sync", []interface{}{tx}, tmResult) _, err := clientJSON.Call("broadcast_tx_sync", []interface{}{tx}, tmResult)
if err != nil { if err != nil {
panic(err) panic(err)
@ -95,18 +101,73 @@ func testBroadcastTxSync(t *testing.T, resI interface{}, tx []byte) {
txs := mem.Reap(1) txs := mem.Reap(1)
if !bytes.Equal(txs[0], tx) { if !bytes.Equal(txs[0], tx) {
panic(Fmt("Tx in mempool does not match test tx. Got %X, expected %X", txs[0], testTx)) panic(Fmt("Tx in mempool does not match test tx. Got %X, expected %X", txs[0], tx))
} }
mem.Flush() mem.Flush()
} }
//--------------------------------------------------------------------------------
// 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()
_, err := clientJSON.Call("broadcast_tx_commit", []interface{}{tx}, tmResult)
if err != nil {
panic(err)
}
fmt.Println("SENT TX", tx)
fmt.Printf("SENT TX %X\n", tx)
fmt.Printf("k %X; v %X", k, v)
return k, v
}
func TestURITMSPQuery(t *testing.T) {
k, v := sendTx()
time.Sleep(time.Second)
tmResult := new(ctypes.TMResult)
_, err := clientURI.Call("tmsp_query", map[string]interface{}{"query": Fmt("%X", k)}, tmResult)
if err != nil {
panic(err)
}
testTMSPQuery(t, tmResult, v)
}
func TestJSONTMSPQuery(t *testing.T) {
k, v := sendTx()
tmResult := new(ctypes.TMResult)
_, err := clientJSON.Call("tmsp_query", []interface{}{Fmt("%X", k)}, tmResult)
if err != nil {
panic(err)
}
testTMSPQuery(t, tmResult, v)
}
func testTMSPQuery(t *testing.T, statusI interface{}, value []byte) {
tmRes := statusI.(*ctypes.TMResult)
query := (*tmRes).(*ctypes.ResultTMSPQuery)
if query.Result.IsErr() {
panic(Fmt("Query returned an err: %v", query))
}
// XXX: specific to value returned by the dummy
if !strings.Contains(string(query.Result.Data), "exists=true") {
panic(Fmt("Query error. Expected to find 'exists=true'. Got: %s", query.Result.Data))
}
}
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// broadcast tx commit // broadcast tx commit
func TestURIBroadcastTxCommit(t *testing.T) { func TestURIBroadcastTxCommit(t *testing.T) {
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
tx := testTx() tx := randBytes()
_, err := clientURI.Call("broadcast_tx_commit", map[string]interface{}{"tx": tx}, tmResult) _, err := clientURI.Call("broadcast_tx_commit", map[string]interface{}{"tx": tx}, tmResult)
if err != nil { if err != nil {
panic(err) panic(err)
@ -116,7 +177,7 @@ func TestURIBroadcastTxCommit(t *testing.T) {
func TestJSONBroadcastTxCommit(t *testing.T) { func TestJSONBroadcastTxCommit(t *testing.T) {
tmResult := new(ctypes.TMResult) tmResult := new(ctypes.TMResult)
tx := testTx() tx := randBytes()
_, err := clientJSON.Call("broadcast_tx_commit", []interface{}{tx}, tmResult) _, err := clientJSON.Call("broadcast_tx_commit", []interface{}{tx}, tmResult)
if err != nil { if err != nil {
panic(err) panic(err)