From 90abc61c567f30185cf1ca9fc7e89dd7cd455b12 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 22 Mar 2017 20:13:18 +0100 Subject: [PATCH] Improve go-data json support in rpc --- rpc/client/httpclient.go | 3 ++- rpc/client/interface.go | 3 ++- rpc/client/localclient.go | 3 ++- rpc/client/mock/abci.go | 9 +++++---- rpc/client/mock/abci_test.go | 9 +++++---- rpc/client/mock/client.go | 3 ++- rpc/client/mock/status_test.go | 5 +++-- rpc/core/abci.go | 3 ++- rpc/core/mempool.go | 3 ++- rpc/core/routes.go | 10 ++++++---- rpc/core/status.go | 5 +++-- rpc/core/types/responses.go | 7 ++++--- rpc/test/client_test.go | 7 +++---- 13 files changed, 41 insertions(+), 29 deletions(-) diff --git a/rpc/client/httpclient.go b/rpc/client/httpclient.go index 04595e766..88a592348 100644 --- a/rpc/client/httpclient.go +++ b/rpc/client/httpclient.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/pkg/errors" + data "github.com/tendermint/go-data" events "github.com/tendermint/go-events" "github.com/tendermint/go-rpc/client" wire "github.com/tendermint/go-wire" @@ -67,7 +68,7 @@ func (c *HTTP) ABCIInfo() (*ctypes.ResultABCIInfo, error) { return (*tmResult).(*ctypes.ResultABCIInfo), nil } -func (c *HTTP) ABCIQuery(path string, data []byte, prove bool) (*ctypes.ResultABCIQuery, error) { +func (c *HTTP) ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) { tmResult := new(ctypes.TMResult) _, err := c.rpc.Call("abci_query", map[string]interface{}{"path": path, "data": data, "prove": prove}, diff --git a/rpc/client/interface.go b/rpc/client/interface.go index 2ba890798..eb79db8b1 100644 --- a/rpc/client/interface.go +++ b/rpc/client/interface.go @@ -20,6 +20,7 @@ implementation. package client import ( + data "github.com/tendermint/go-data" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" ) @@ -30,7 +31,7 @@ import ( type ABCIClient interface { // reading from abci app ABCIInfo() (*ctypes.ResultABCIInfo, error) - ABCIQuery(path string, data []byte, prove bool) (*ctypes.ResultABCIQuery, error) + ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) // writing to abci app BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) diff --git a/rpc/client/localclient.go b/rpc/client/localclient.go index d0f0d11b1..74051d8c9 100644 --- a/rpc/client/localclient.go +++ b/rpc/client/localclient.go @@ -1,6 +1,7 @@ package client import ( + data "github.com/tendermint/go-data" nm "github.com/tendermint/tendermint/node" "github.com/tendermint/tendermint/rpc/core" ctypes "github.com/tendermint/tendermint/rpc/core/types" @@ -56,7 +57,7 @@ func (c Local) ABCIInfo() (*ctypes.ResultABCIInfo, error) { return core.ABCIInfo() } -func (c Local) ABCIQuery(path string, data []byte, prove bool) (*ctypes.ResultABCIQuery, error) { +func (c Local) ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) { return core.ABCIQuery(path, data, prove) } diff --git a/rpc/client/mock/abci.go b/rpc/client/mock/abci.go index 6f6fa1d47..6a9fe6bdf 100644 --- a/rpc/client/mock/abci.go +++ b/rpc/client/mock/abci.go @@ -2,6 +2,7 @@ package mock import ( abci "github.com/tendermint/abci/types" + data "github.com/tendermint/go-data" "github.com/tendermint/tendermint/rpc/client" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" @@ -22,7 +23,7 @@ func (a ABCIApp) ABCIInfo() (*ctypes.ResultABCIInfo, error) { return &ctypes.ResultABCIInfo{a.App.Info()}, nil } -func (a ABCIApp) ABCIQuery(path string, data []byte, prove bool) (*ctypes.ResultABCIQuery, error) { +func (a ABCIApp) ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) { q := a.App.Query(abci.RequestQuery{data, path, 0, prove}) return &ctypes.ResultABCIQuery{q}, nil } @@ -79,7 +80,7 @@ func (m ABCIMock) ABCIInfo() (*ctypes.ResultABCIInfo, error) { return &ctypes.ResultABCIInfo{res.(abci.ResponseInfo)}, nil } -func (m ABCIMock) ABCIQuery(path string, data []byte, prove bool) (*ctypes.ResultABCIQuery, error) { +func (m ABCIMock) ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) { res, err := m.Query.GetResponse(QueryArgs{path, data, prove}) if err != nil { return nil, err @@ -131,7 +132,7 @@ func (r *ABCIRecorder) _assertABCIClient() client.ABCIClient { type QueryArgs struct { Path string - Data []byte + Data data.Bytes Prove bool } @@ -149,7 +150,7 @@ func (r *ABCIRecorder) ABCIInfo() (*ctypes.ResultABCIInfo, error) { return res, err } -func (r *ABCIRecorder) ABCIQuery(path string, data []byte, prove bool) (*ctypes.ResultABCIQuery, error) { +func (r *ABCIRecorder) ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) { res, err := r.Client.ABCIQuery(path, data, prove) r.addCall(Call{ Name: "abci_query", diff --git a/rpc/client/mock/abci_test.go b/rpc/client/mock/abci_test.go index 823752caf..bb3a734a7 100644 --- a/rpc/client/mock/abci_test.go +++ b/rpc/client/mock/abci_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/require" "github.com/tendermint/abci/example/dummy" abci "github.com/tendermint/abci/types" + data "github.com/tendermint/go-data" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" @@ -35,8 +36,8 @@ func TestABCIMock(t *testing.T) { BroadcastCommit: mock.Call{ Args: goodTx, Response: &ctypes.ResultBroadcastTxCommit{ - CheckTx: &abci.ResponseCheckTx{Data: []byte("stand")}, - DeliverTx: &abci.ResponseDeliverTx{Data: []byte("deliver")}, + CheckTx: &abci.ResponseCheckTx{Data: data.Bytes("stand")}, + DeliverTx: &abci.ResponseDeliverTx{Data: data.Bytes("deliver")}, }, Error: errors.New("bad tx"), }, @@ -91,7 +92,7 @@ func TestABCIRecorder(t *testing.T) { require.Equal(0, len(r.Calls)) r.ABCIInfo() - r.ABCIQuery("path", []byte("data"), true) + r.ABCIQuery("path", data.Bytes("data"), true) require.Equal(2, len(r.Calls)) info := r.Calls[0] @@ -163,7 +164,7 @@ func TestABCIApp(t *testing.T) { assert.True(res.DeliverTx.Code.IsOK()) // check the key - qres, err := m.ABCIQuery("/key", []byte(key), false) + qres, err := m.ABCIQuery("/key", data.Bytes(key), false) require.Nil(err) assert.EqualValues(value, qres.Response.Value) } diff --git a/rpc/client/mock/client.go b/rpc/client/mock/client.go index a3cecfca1..478862380 100644 --- a/rpc/client/mock/client.go +++ b/rpc/client/mock/client.go @@ -16,6 +16,7 @@ package mock import ( "reflect" + data "github.com/tendermint/go-data" "github.com/tendermint/tendermint/rpc/client" "github.com/tendermint/tendermint/rpc/core" ctypes "github.com/tendermint/tendermint/rpc/core/types" @@ -83,7 +84,7 @@ func (c Client) ABCIInfo() (*ctypes.ResultABCIInfo, error) { return core.ABCIInfo() } -func (c Client) ABCIQuery(path string, data []byte, prove bool) (*ctypes.ResultABCIQuery, error) { +func (c Client) ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) { return core.ABCIQuery(path, data, prove) } diff --git a/rpc/client/mock/status_test.go b/rpc/client/mock/status_test.go index 3e695cd57..12edc8af0 100644 --- a/rpc/client/mock/status_test.go +++ b/rpc/client/mock/status_test.go @@ -5,6 +5,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + data "github.com/tendermint/go-data" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/rpc/client/mock" @@ -16,8 +17,8 @@ func TestStatus(t *testing.T) { m := &mock.StatusMock{ Call: mock.Call{ Response: &ctypes.ResultStatus{ - LatestBlockHash: []byte("block"), - LatestAppHash: []byte("app"), + LatestBlockHash: data.Bytes("block"), + LatestAppHash: data.Bytes("app"), LatestBlockHeight: 10, }}, } diff --git a/rpc/core/abci.go b/rpc/core/abci.go index 957727267..47b599ada 100644 --- a/rpc/core/abci.go +++ b/rpc/core/abci.go @@ -2,12 +2,13 @@ package core import ( abci "github.com/tendermint/abci/types" + data "github.com/tendermint/go-data" ctypes "github.com/tendermint/tendermint/rpc/core/types" ) //----------------------------------------------------------------------------- -func ABCIQuery(path string, data []byte, prove bool) (*ctypes.ResultABCIQuery, error) { +func ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) { resQuery, err := proxyAppQuery.QuerySync(abci.RequestQuery{ Path: path, Data: data, diff --git a/rpc/core/mempool.go b/rpc/core/mempool.go index 4da83a1f6..358671635 100644 --- a/rpc/core/mempool.go +++ b/rpc/core/mempool.go @@ -5,6 +5,7 @@ import ( "time" abci "github.com/tendermint/abci/types" + data "github.com/tendermint/go-data" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" ) @@ -84,7 +85,7 @@ func BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) { Data: deliverTxRes.Data, Log: deliverTxRes.Log, } - log.Notice("DeliverTx passed ", "tx", []byte(tx), "response", deliverTxR) + log.Notice("DeliverTx passed ", "tx", data.Bytes(tx), "response", deliverTxR) return &ctypes.ResultBroadcastTxCommit{ CheckTx: checkTxR, DeliverTx: deliverTxR, diff --git a/rpc/core/routes.go b/rpc/core/routes.go index 38e609601..58b464208 100644 --- a/rpc/core/routes.go +++ b/rpc/core/routes.go @@ -1,9 +1,11 @@ package core import ( + data "github.com/tendermint/go-data" rpc "github.com/tendermint/go-rpc/server" "github.com/tendermint/go-rpc/types" ctypes "github.com/tendermint/tendermint/rpc/core/types" + "github.com/tendermint/tendermint/types" ) // TODO: better system than "unsafe" prefix @@ -104,19 +106,19 @@ func TxResult(hash []byte, prove bool) (ctypes.TMResult, error) { return Tx(hash, prove) } -func BroadcastTxCommitResult(tx []byte) (ctypes.TMResult, error) { +func BroadcastTxCommitResult(tx types.Tx) (ctypes.TMResult, error) { return BroadcastTxCommit(tx) } -func BroadcastTxSyncResult(tx []byte) (ctypes.TMResult, error) { +func BroadcastTxSyncResult(tx types.Tx) (ctypes.TMResult, error) { return BroadcastTxSync(tx) } -func BroadcastTxAsyncResult(tx []byte) (ctypes.TMResult, error) { +func BroadcastTxAsyncResult(tx types.Tx) (ctypes.TMResult, error) { return BroadcastTxAsync(tx) } -func ABCIQueryResult(path string, data []byte, prove bool) (ctypes.TMResult, error) { +func ABCIQueryResult(path string, data data.Bytes, prove bool) (ctypes.TMResult, error) { return ABCIQuery(path, data, prove) } diff --git a/rpc/core/status.go b/rpc/core/status.go index 96ed46ea6..d7d503802 100644 --- a/rpc/core/status.go +++ b/rpc/core/status.go @@ -1,6 +1,7 @@ package core import ( + data "github.com/tendermint/go-data" ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/types" ) @@ -9,8 +10,8 @@ func Status() (*ctypes.ResultStatus, error) { latestHeight := blockStore.Height() var ( latestBlockMeta *types.BlockMeta - latestBlockHash []byte - latestAppHash []byte + latestBlockHash data.Bytes + latestAppHash data.Bytes latestBlockTime int64 ) if latestHeight != 0 { diff --git a/rpc/core/types/responses.go b/rpc/core/types/responses.go index 7cab8535d..59fa09aa0 100644 --- a/rpc/core/types/responses.go +++ b/rpc/core/types/responses.go @@ -5,6 +5,7 @@ import ( abci "github.com/tendermint/abci/types" "github.com/tendermint/go-crypto" + data "github.com/tendermint/go-data" "github.com/tendermint/go-p2p" "github.com/tendermint/go-rpc/types" "github.com/tendermint/go-wire" @@ -34,8 +35,8 @@ type ResultCommit struct { type ResultStatus struct { NodeInfo *p2p.NodeInfo `json:"node_info"` PubKey crypto.PubKey `json:"pub_key"` - LatestBlockHash []byte `json:"latest_block_hash"` - LatestAppHash []byte `json:"latest_app_hash"` + LatestBlockHash data.Bytes `json:"latest_block_hash"` + LatestAppHash data.Bytes `json:"latest_app_hash"` LatestBlockHeight int `json:"latest_block_height"` LatestBlockTime int64 `json:"latest_block_time"` // nano } @@ -81,7 +82,7 @@ type ResultDumpConsensusState struct { type ResultBroadcastTx struct { Code abci.CodeType `json:"code"` - Data []byte `json:"data"` + Data data.Bytes `json:"data"` Log string `json:"log"` Hash []byte `json:"hash"` diff --git a/rpc/test/client_test.go b/rpc/test/client_test.go index 50e326050..a9792b5be 100644 --- a/rpc/test/client_test.go +++ b/rpc/test/client_test.go @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/require" abci "github.com/tendermint/abci/types" . "github.com/tendermint/go-common" + data "github.com/tendermint/go-data" rpc "github.com/tendermint/go-rpc/client" "github.com/tendermint/tendermint/rpc/core" ctypes "github.com/tendermint/tendermint/rpc/core/types" @@ -85,10 +86,10 @@ func testBroadcastTxSync(t *testing.T, client rpc.HTTPClient) { //-------------------------------------------------------------------------------- // query -func testTxKV(t *testing.T) ([]byte, []byte, []byte) { +func testTxKV(t *testing.T) ([]byte, []byte, types.Tx) { k := randBytes(t) v := randBytes(t) - return k, v, []byte(Fmt("%s=%s", k, v)) + return k, v, types.Tx(Fmt("%s=%s", k, v)) } func sendTx(t *testing.T, client rpc.HTTPClient) ([]byte, []byte) { @@ -114,8 +115,6 @@ func testABCIQuery(t *testing.T, client rpc.HTTPClient) { _, err := client.Call("abci_query", map[string]interface{}{"path": "", "data": k, "prove": false}, tmResult) require.Nil(t, err) - - resQuery := (*tmResult).(*ctypes.ResultABCIQuery) require.EqualValues(t, 0, resQuery.Response.Code) // XXX: specific to value returned by the dummy