From 1af930441ce1e4a9563df17f4cce41b767ec150f Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sat, 28 Jan 2017 08:27:13 -0800 Subject: [PATCH] Support new Query message for proofs --- consensus/mempool_test.go | 7 ++++--- glide.lock | 10 +++++----- node/node.go | 30 +++++++++++++++++++----------- proxy/app_conn.go | 8 ++++---- rpc/core/abci.go | 23 +++++++++++++---------- rpc/core/routes.go | 6 +++--- rpc/core/types/responses.go | 11 ++++------- rpc/test/client_test.go | 22 ++++++++-------------- 8 files changed, 60 insertions(+), 57 deletions(-) diff --git a/consensus/mempool_test.go b/consensus/mempool_test.go index d298b5cb4..4dd82a3f1 100644 --- a/consensus/mempool_test.go +++ b/consensus/mempool_test.go @@ -5,9 +5,9 @@ import ( "testing" "time" + abci "github.com/tendermint/abci/types" "github.com/tendermint/tendermint/config/tendermint_test" "github.com/tendermint/tendermint/types" - abci "github.com/tendermint/abci/types" . "github.com/tendermint/go-common" ) @@ -161,6 +161,7 @@ func (app *CounterApplication) Commit() abci.Result { } } -func (app *CounterApplication) Query(query []byte) abci.Result { - return abci.NewResultOK(nil, Fmt("Query is not supported")) +func (app *CounterApplication) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) { + resQuery.Log = "Query is not supported" + return } diff --git a/glide.lock b/glide.lock index 0277c260f..a3cc726e8 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ hash: e283934fbbd221161d53a918db9e49db8c5be2b8929592b05ffe6b72c2ef0ab1 -updated: 2017-01-14T20:59:48.253671736-08:00 +updated: 2017-01-27T22:32:42.896956819-08:00 imports: - name: github.com/btcsuite/btcd version: afec1bd1245a4a19e6dfe1306974b733e7cbb9b8 @@ -32,7 +32,7 @@ imports: - name: github.com/mattn/go-isatty version: 66b8e73f3f5cda9f96b69efd03dd3d7fc4a5cdb8 - name: github.com/spf13/pflag - version: 25f8b5b07aece3207895bf19f7ab517eb3b22a40 + version: 5ccb023bc27df288a957c5e994cd44fd19619465 - name: github.com/syndtr/goleveldb version: 6ae1797c0b42b9323fc27ff7dcf568df88f2f33d subpackages: @@ -49,7 +49,7 @@ imports: - leveldb/table - leveldb/util - name: github.com/tendermint/abci - version: 6526ab2137fadd0f4d2e25002bbfc1784b4f3c27 + version: 8df0bc3a40ccad0d2be10e33c62c404e65c92502 subpackages: - client - example/counter @@ -67,7 +67,7 @@ imports: - name: github.com/tendermint/go-clist version: 3baa390bbaf7634251c42ad69a8682e7e3990552 - name: github.com/tendermint/go-common - version: 70e694ee76f09058ea38c9ba81b4aa621bd54df1 + version: 339e135776142939d82bc8e699db0bf391fd938d subpackages: - test - name: github.com/tendermint/go-config @@ -85,7 +85,7 @@ imports: - name: github.com/tendermint/go-logger version: cefb3a45c0bf3c493a04e9bcd9b1540528be59f2 - name: github.com/tendermint/go-merkle - version: 2979c7eb8aa020fa1cf203654907dbb889703888 + version: 653cb1f631528351ddbc359b994eb0c96f0341cd - name: github.com/tendermint/go-p2p version: 67c9086b7458eb45b1970483decd01cd744c477a subpackages: diff --git a/node/node.go b/node/node.go index 91415ec74..d9eeb1f42 100644 --- a/node/node.go +++ b/node/node.go @@ -2,10 +2,12 @@ package node import ( "bytes" + "errors" "net" "net/http" "strings" + abci "github.com/tendermint/abci/types" cmn "github.com/tendermint/go-common" cfg "github.com/tendermint/go-config" "github.com/tendermint/go-crypto" @@ -23,9 +25,9 @@ import ( sm "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/version" -) -import _ "net/http/pprof" + _ "net/http/pprof" +) type Node struct { cmn.BaseService @@ -120,24 +122,30 @@ func NewNode(config cfg.Config, privValidator *types.PrivValidator, clientCreato sw.AddReactor("PEX", pexReactor) } - // filter peers by addr or pubkey with a abci query. - // if the query return code is OK, add peer - // XXX: query format subject to change + // Filter peers by addr or pubkey with an ABCI query. + // If the query return code is OK, add peer. + // XXX: Query format subject to change if config.GetBool("filter_peers") { // NOTE: addr is ip:port sw.SetAddrFilter(func(addr net.Addr) error { - res := proxyApp.Query().QuerySync([]byte(cmn.Fmt("p2p/filter/addr/%s", addr.String()))) - if res.IsOK() { + resQuery, err := proxyApp.Query().QuerySync(abci.RequestQuery{Path: cmn.Fmt("/p2p/filter/addr/%s", addr.String())}) + if err != nil { + return err + } + if resQuery.Code.IsOK() { return nil } - return res + return errors.New(resQuery.Code.String()) }) sw.SetPubKeyFilter(func(pubkey crypto.PubKeyEd25519) error { - res := proxyApp.Query().QuerySync([]byte(cmn.Fmt("p2p/filter/pubkey/%X", pubkey.Bytes()))) - if res.IsOK() { + resQuery, err := proxyApp.Query().QuerySync(abci.RequestQuery{Path: cmn.Fmt("/p2p/filter/pubkey/%X", pubkey.Bytes())}) + if err != nil { + return err + } + if resQuery.Code.IsOK() { return nil } - return res + return errors.New(resQuery.Code.String()) }) } diff --git a/proxy/app_conn.go b/proxy/app_conn.go index 6abb8c7eb..8b007737c 100644 --- a/proxy/app_conn.go +++ b/proxy/app_conn.go @@ -34,8 +34,8 @@ type AppConnQuery interface { Error() error EchoSync(string) (res types.Result) - InfoSync() (types.ResponseInfo, error) - QuerySync(tx []byte) (res types.Result) + InfoSync() (resInfo types.ResponseInfo, err error) + QuerySync(reqQuery types.RequestQuery) (resQuery types.ResponseQuery, err error) // SetOptionSync(key string, value string) (res types.Result) } @@ -139,6 +139,6 @@ func (app *appConnQuery) InfoSync() (types.ResponseInfo, error) { return app.appConn.InfoSync() } -func (app *appConnQuery) QuerySync(tx []byte) (res types.Result) { - return app.appConn.QuerySync(tx) +func (app *appConnQuery) QuerySync(reqQuery types.RequestQuery) (types.ResponseQuery, error) { + return app.appConn.QuerySync(reqQuery) } diff --git a/rpc/core/abci.go b/rpc/core/abci.go index 032193431..cb748fe02 100644 --- a/rpc/core/abci.go +++ b/rpc/core/abci.go @@ -1,25 +1,28 @@ package core import ( + abci "github.com/tendermint/abci/types" ctypes "github.com/tendermint/tendermint/rpc/core/types" ) //----------------------------------------------------------------------------- -func ABCIQuery(query []byte) (*ctypes.ResultABCIQuery, error) { - res := proxyAppQuery.QuerySync(query) - return &ctypes.ResultABCIQuery{res}, nil +func ABCIQuery(path string, data []byte, prove bool) (*ctypes.ResultABCIQuery, error) { + resQuery, err := proxyAppQuery.QuerySync(abci.RequestQuery{ + Path: path, + Data: data, + Prove: prove, + }) + if err != nil { + return nil, err + } + return &ctypes.ResultABCIQuery{resQuery}, nil } func ABCIInfo() (*ctypes.ResultABCIInfo, error) { - res, err := proxyAppQuery.InfoSync() + resInfo, err := proxyAppQuery.InfoSync() if err != nil { return nil, err } - return &ctypes.ResultABCIInfo{ - Data: res.Data, - Version: res.Version, - LastBlockHeight: res.LastBlockHeight, - LastBlockAppHash: res.LastBlockAppHash, - }, nil + return &ctypes.ResultABCIInfo{resInfo}, nil } diff --git a/rpc/core/routes.go b/rpc/core/routes.go index fa80bf2fd..8396cd23c 100644 --- a/rpc/core/routes.go +++ b/rpc/core/routes.go @@ -29,7 +29,7 @@ var Routes = map[string]*rpc.RPCFunc{ "broadcast_tx_async": rpc.NewRPCFunc(BroadcastTxAsyncResult, "tx"), // abci API - "abci_query": rpc.NewRPCFunc(ABCIQueryResult, "query"), + "abci_query": rpc.NewRPCFunc(ABCIQueryResult, "path,data,prove"), "abci_info": rpc.NewRPCFunc(ABCIInfoResult, ""), // control API @@ -163,8 +163,8 @@ func BroadcastTxAsyncResult(tx []byte) (ctypes.TMResult, error) { } } -func ABCIQueryResult(query []byte) (ctypes.TMResult, error) { - if r, err := ABCIQuery(query); err != nil { +func ABCIQueryResult(path string, data []byte, prove bool) (ctypes.TMResult, error) { + if r, err := ABCIQuery(path, data, prove); err != nil { return nil, err } else { return r, nil diff --git a/rpc/core/types/responses.go b/rpc/core/types/responses.go index 5915c1db6..1a5deaeab 100644 --- a/rpc/core/types/responses.go +++ b/rpc/core/types/responses.go @@ -1,12 +1,12 @@ package core_types import ( + abci "github.com/tendermint/abci/types" "github.com/tendermint/go-crypto" "github.com/tendermint/go-p2p" "github.com/tendermint/go-rpc/types" "github.com/tendermint/go-wire" "github.com/tendermint/tendermint/types" - abci "github.com/tendermint/abci/types" ) type ResultBlockchainInfo struct { @@ -64,7 +64,7 @@ type ResultBroadcastTx struct { } type ResultBroadcastTxCommit struct { - CheckTx *abci.ResponseCheckTx `json:"check_tx"` + CheckTx *abci.ResponseCheckTx `json:"check_tx"` DeliverTx *abci.ResponseDeliverTx `json:"deliver_tx"` } @@ -74,14 +74,11 @@ type ResultUnconfirmedTxs struct { } type ResultABCIInfo struct { - Data string `json:"data"` - Version string `json:"version"` - LastBlockHeight uint64 `json:"last_block_height"` - LastBlockAppHash []byte `json:"last_block_app_hash"` + Response abci.ResponseInfo `json:"response"` } type ResultABCIQuery struct { - Result abci.Result `json:"result"` + Response abci.ResponseQuery `json:"response"` } type ResultUnsafeFlushMempool struct{} diff --git a/rpc/test/client_test.go b/rpc/test/client_test.go index da0b86f36..b17252dfa 100644 --- a/rpc/test/client_test.go +++ b/rpc/test/client_test.go @@ -8,12 +8,10 @@ import ( "testing" "time" + abci "github.com/tendermint/abci/types" . "github.com/tendermint/go-common" - "github.com/tendermint/go-wire" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" - "github.com/tendermint/abci/example/dummy" - abci "github.com/tendermint/abci/types" ) //-------------------------------------------------------------------------------- @@ -134,7 +132,7 @@ func TestURIABCIQuery(t *testing.T) { k, v := sendTx() time.Sleep(time.Second) tmResult := new(ctypes.TMResult) - _, err := clientURI.Call("abci_query", map[string]interface{}{"query": k}, tmResult) + _, err := clientURI.Call("abci_query", map[string]interface{}{"path": "", "data": k, "prove": false}, tmResult) if err != nil { panic(err) } @@ -144,7 +142,7 @@ func TestURIABCIQuery(t *testing.T) { func TestJSONABCIQuery(t *testing.T) { k, v := sendTx() tmResult := new(ctypes.TMResult) - _, err := clientJSON.Call("abci_query", []interface{}{k}, tmResult) + _, err := clientJSON.Call("abci_query", []interface{}{"", k, false}, tmResult) if err != nil { panic(err) } @@ -153,18 +151,14 @@ func TestJSONABCIQuery(t *testing.T) { func testABCIQuery(t *testing.T, statusI interface{}, value []byte) { tmRes := statusI.(*ctypes.TMResult) - query := (*tmRes).(*ctypes.ResultABCIQuery) - if query.Result.IsErr() { - panic(Fmt("Query returned an err: %v", query)) + resQuery := (*tmRes).(*ctypes.ResultABCIQuery) + if !resQuery.Response.Code.IsOK() { + panic(Fmt("Query returned an err: %v", resQuery)) } - qResult := new(dummy.QueryResult) - if err := wire.ReadJSONBytes(query.Result.Data, qResult); err != nil { - t.Fatal(err) - } // XXX: specific to value returned by the dummy - if qResult.Exists != true { - panic(Fmt("Query error. Expected to find 'exists=true'. Got: %v", qResult)) + if len(resQuery.Response.Value) == 0 { + panic(Fmt("Query error. Found no value")) } }