mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-15 19:56:11 +00:00
Fix linter errors thrown by lll (#3970)
* Fix long line errors in abci, crypto, and libs packages * Fix long lines in p2p and rpc packages * Fix long lines in abci, state, and tools packages * Fix long lines in behaviour and blockchain packages * Fix long lines in cmd and config packages * Begin fixing long lines in consensus package * Finish fixing long lines in consensus package * Add lll exclusion for lines containing URLs * Fix long lines in crypto package * Fix long lines in evidence package * Fix long lines in mempool and node packages * Fix long lines in libs package * Fix long lines in lite package * Fix new long line in node package * Fix long lines in p2p package * Ignore gocritic warning * Fix long lines in privval package * Fix long lines in rpc package * Fix long lines in scripts package * Fix long lines in state package * Fix long lines in tools package * Fix long lines in types package * Enable lll linter
This commit is contained in:
@@ -170,7 +170,10 @@ func (c *baseRPCClient) ABCIQuery(path string, data cmn.HexBytes) (*ctypes.Resul
|
||||
return c.ABCIQueryWithOptions(path, data, DefaultABCIQueryOptions)
|
||||
}
|
||||
|
||||
func (c *baseRPCClient) ABCIQueryWithOptions(path string, data cmn.HexBytes, opts ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
func (c *baseRPCClient) ABCIQueryWithOptions(
|
||||
path string,
|
||||
data cmn.HexBytes,
|
||||
opts ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
result := new(ctypes.ResultABCIQuery)
|
||||
_, err := c.caller.Call("abci_query",
|
||||
map[string]interface{}{"path": path, "data": data, "height": opts.Height, "prove": opts.Prove},
|
||||
|
||||
@@ -77,7 +77,10 @@ func (c *Local) ABCIQuery(path string, data cmn.HexBytes) (*ctypes.ResultABCIQue
|
||||
return c.ABCIQueryWithOptions(path, data, DefaultABCIQueryOptions)
|
||||
}
|
||||
|
||||
func (c *Local) ABCIQueryWithOptions(path string, data cmn.HexBytes, opts ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
func (c *Local) ABCIQueryWithOptions(
|
||||
path string,
|
||||
data cmn.HexBytes,
|
||||
opts ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
return core.ABCIQuery(c.ctx, path, data, opts.Height, opts.Prove)
|
||||
}
|
||||
|
||||
@@ -161,7 +164,11 @@ func (c *Local) BroadcastEvidence(ev types.Evidence) (*ctypes.ResultBroadcastEvi
|
||||
return core.BroadcastEvidence(c.ctx, ev)
|
||||
}
|
||||
|
||||
func (c *Local) Subscribe(ctx context.Context, subscriber, query string, outCapacity ...int) (out <-chan ctypes.ResultEvent, err error) {
|
||||
func (c *Local) Subscribe(
|
||||
ctx context.Context,
|
||||
subscriber,
|
||||
query string,
|
||||
outCapacity ...int) (out <-chan ctypes.ResultEvent, err error) {
|
||||
q, err := tmquery.New(query)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to parse query")
|
||||
@@ -182,7 +189,11 @@ func (c *Local) Subscribe(ctx context.Context, subscriber, query string, outCapa
|
||||
return outc, nil
|
||||
}
|
||||
|
||||
func (c *Local) eventsRoutine(sub types.Subscription, subscriber string, q tmpubsub.Query, outc chan<- ctypes.ResultEvent) {
|
||||
func (c *Local) eventsRoutine(
|
||||
sub types.Subscription,
|
||||
subscriber string,
|
||||
q tmpubsub.Query,
|
||||
outc chan<- ctypes.ResultEvent) {
|
||||
for {
|
||||
select {
|
||||
case msg := <-sub.Out():
|
||||
|
||||
+12
-3
@@ -30,7 +30,10 @@ func (a ABCIApp) ABCIQuery(path string, data cmn.HexBytes) (*ctypes.ResultABCIQu
|
||||
return a.ABCIQueryWithOptions(path, data, client.DefaultABCIQueryOptions)
|
||||
}
|
||||
|
||||
func (a ABCIApp) ABCIQueryWithOptions(path string, data cmn.HexBytes, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
func (a ABCIApp) ABCIQueryWithOptions(
|
||||
path string,
|
||||
data cmn.HexBytes,
|
||||
opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
q := a.App.Query(abci.RequestQuery{
|
||||
Data: data,
|
||||
Path: path,
|
||||
@@ -94,7 +97,10 @@ func (m ABCIMock) ABCIQuery(path string, data cmn.HexBytes) (*ctypes.ResultABCIQ
|
||||
return m.ABCIQueryWithOptions(path, data, client.DefaultABCIQueryOptions)
|
||||
}
|
||||
|
||||
func (m ABCIMock) ABCIQueryWithOptions(path string, data cmn.HexBytes, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
func (m ABCIMock) ABCIQueryWithOptions(
|
||||
path string,
|
||||
data cmn.HexBytes,
|
||||
opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
res, err := m.Query.GetResponse(QueryArgs{path, data, opts.Height, opts.Prove})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -166,7 +172,10 @@ func (r *ABCIRecorder) ABCIQuery(path string, data cmn.HexBytes) (*ctypes.Result
|
||||
return r.ABCIQueryWithOptions(path, data, client.DefaultABCIQueryOptions)
|
||||
}
|
||||
|
||||
func (r *ABCIRecorder) ABCIQueryWithOptions(path string, data cmn.HexBytes, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
func (r *ABCIRecorder) ABCIQueryWithOptions(
|
||||
path string,
|
||||
data cmn.HexBytes,
|
||||
opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
res, err := r.Client.ABCIQueryWithOptions(path, data, opts)
|
||||
r.addCall(Call{
|
||||
Name: "abci_query",
|
||||
|
||||
@@ -90,7 +90,10 @@ func (c Client) ABCIQuery(path string, data cmn.HexBytes) (*ctypes.ResultABCIQue
|
||||
return c.ABCIQueryWithOptions(path, data, client.DefaultABCIQueryOptions)
|
||||
}
|
||||
|
||||
func (c Client) ABCIQueryWithOptions(path string, data cmn.HexBytes, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
func (c Client) ABCIQueryWithOptions(
|
||||
path string,
|
||||
data cmn.HexBytes,
|
||||
opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
|
||||
return core.ABCIQuery(&rpctypes.Context{}, path, data, opts.Height, opts.Prove)
|
||||
}
|
||||
|
||||
|
||||
+12
-2
@@ -488,7 +488,13 @@ func deepcpVote(vote *types.Vote) (res *types.Vote) {
|
||||
return
|
||||
}
|
||||
|
||||
func newEvidence(t *testing.T, val *privval.FilePV, vote *types.Vote, vote2 *types.Vote, chainID string) types.DuplicateVoteEvidence {
|
||||
func newEvidence(
|
||||
t *testing.T,
|
||||
val *privval.FilePV,
|
||||
vote *types.Vote,
|
||||
vote2 *types.Vote,
|
||||
chainID string,
|
||||
) types.DuplicateVoteEvidence {
|
||||
var err error
|
||||
vote2_ := deepcpVote(vote2)
|
||||
vote2_.Signature, err = val.Key.PrivKey.Sign(vote2_.SignBytes(chainID))
|
||||
@@ -501,7 +507,11 @@ func newEvidence(t *testing.T, val *privval.FilePV, vote *types.Vote, vote2 *typ
|
||||
}
|
||||
}
|
||||
|
||||
func makeEvidences(t *testing.T, val *privval.FilePV, chainID string) (ev types.DuplicateVoteEvidence, fakes []types.DuplicateVoteEvidence) {
|
||||
func makeEvidences(
|
||||
t *testing.T,
|
||||
val *privval.FilePV,
|
||||
chainID string,
|
||||
) (ev types.DuplicateVoteEvidence, fakes []types.DuplicateVoteEvidence) {
|
||||
vote := &types.Vote{
|
||||
ValidatorAddress: val.Key.Address,
|
||||
ValidatorIndex: 0,
|
||||
|
||||
+11
-2
@@ -33,7 +33,10 @@ import (
|
||||
// "response": {
|
||||
// "log": "exists",
|
||||
// "height": "0",
|
||||
// "proof": "010114FED0DAD959F36091AD761C922ABA3CBF1D8349990101020103011406AA2262E2F448242DF2C2607C3CDC705313EE3B0001149D16177BC71E445476174622EA559715C293740C",
|
||||
// "proof": "010114FED0DAD959F36091AD761C922ABA3CBF1" +
|
||||
// "D8349990101020103011406AA2262E2F448242D" +
|
||||
// "F2C2607C3CDC705313EE3B0001149D16177BC71" +
|
||||
// "E445476174622EA559715C293740C",
|
||||
// "value": "61626364",
|
||||
// "key": "61626364",
|
||||
// "index": "-1",
|
||||
@@ -53,7 +56,13 @@ import (
|
||||
// | data | []byte | false | true | Data |
|
||||
// | height | int64 | 0 | false | Height (0 means latest) |
|
||||
// | prove | bool | false | false | Includes proof if true |
|
||||
func ABCIQuery(ctx *rpctypes.Context, path string, data cmn.HexBytes, height int64, prove bool) (*ctypes.ResultABCIQuery, error) {
|
||||
func ABCIQuery(
|
||||
ctx *rpctypes.Context,
|
||||
path string,
|
||||
data cmn.HexBytes,
|
||||
height int64,
|
||||
prove bool,
|
||||
) (*ctypes.ResultABCIQuery, error) {
|
||||
resQuery, err := proxyAppQuery.QuerySync(abci.RequestQuery{
|
||||
Path: path,
|
||||
Data: data,
|
||||
|
||||
+6
-2
@@ -150,7 +150,9 @@ func filterMinMax(height, min, max, limit int64) (int64, int64, error) {
|
||||
// "precommits": [
|
||||
// {
|
||||
// "signature": {
|
||||
// "data": "12C0D8893B8A38224488DC1DE6270DF76BB1A5E9DB1C68577706A6A97C6EC34FFD12339183D5CA8BC2F46148773823DE905B7F6F5862FD564038BB7AE03BF50D",
|
||||
// "data": "12C0D8893B8A38224488DC1DE6270DF76BB1A5E9DB" +
|
||||
// "1C68577706A6A97C6EC34FFD12339183D5CA8BC2F4" +
|
||||
// "6148773823DE905B7F6F5862FD564038BB7AE03BF50D",
|
||||
// "type": "ed25519"
|
||||
// },
|
||||
// "block_id": {
|
||||
@@ -267,7 +269,9 @@ func Block(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlock, error)
|
||||
// "precommits": [
|
||||
// {
|
||||
// "signature": {
|
||||
// "data": "00970429FEC652E9E21D106A90AE8C5413759A7488775CEF4A3F44DC46C7F9D941070E4FBE9ED54DF247FA3983359A0C3A238D61DE55C75C9116D72ABC9CF50F",
|
||||
// "data": "00970429FEC652E9E21D106A90AE8C5413759A74887" +
|
||||
// "75CEF4A3F44DC46C7F9D941070E4FBE9ED54DF247FA" +
|
||||
// "3983359A0C3A238D61DE55C75C9116D72ABC9CF50F",
|
||||
// "type": "ed25519"
|
||||
// },
|
||||
// "block_id": {
|
||||
|
||||
+11
-5
@@ -12,14 +12,18 @@ See it here: https://github.com/tendermint/tendermint/tree/master/rpc/lib
|
||||
|
||||
## Configuration
|
||||
|
||||
RPC can be configured by tuning parameters under `[rpc]` table in the `$TMHOME/config/config.toml` file or by using the `--rpc.X` command-line flags.
|
||||
RPC can be configured by tuning parameters under `[rpc]` table in the `$TMHOME/config/config.toml` file
|
||||
or by using the `--rpc.X` command-line flags.
|
||||
|
||||
Default rpc listen address is `tcp://0.0.0.0:26657`. To set another address, set the `laddr` config parameter to desired value.
|
||||
CORS (Cross-Origin Resource Sharing) can be enabled by setting `cors_allowed_origins`, `cors_allowed_methods`, `cors_allowed_headers` config parameters.
|
||||
Default rpc listen address is `tcp://0.0.0.0:26657`.
|
||||
To set another address, set the `laddr` config parameter to desired value.
|
||||
CORS (Cross-Origin Resource Sharing) can be enabled by setting
|
||||
`cors_allowed_origins`, `cors_allowed_methods`, `cors_allowed_headers` config parameters.
|
||||
|
||||
## Arguments
|
||||
|
||||
Arguments which expect strings or byte arrays may be passed as quoted strings, like `"abc"` or as `0x`-prefixed strings, like `0x616263`.
|
||||
Arguments which expect strings or byte arrays may be passed as quoted strings,
|
||||
like `"abc"` or as `0x`-prefixed strings, like `0x616263`.
|
||||
|
||||
## URI/HTTP
|
||||
|
||||
@@ -58,7 +62,9 @@ JSONRPC requests can be POST'd to the root RPC endpoint via HTTP (e.g. `http://l
|
||||
|
||||
## JSONRPC/websockets
|
||||
|
||||
JSONRPC requests can be made via websocket. The websocket endpoint is at `/websocket`, e.g. `localhost:26657/websocket`. Asynchronous RPC functions like event `subscribe` and `unsubscribe` are only available via websockets.
|
||||
JSONRPC requests can be made via websocket.
|
||||
The websocket endpoint is at `/websocket`, e.g. `localhost:26657/websocket`.
|
||||
Asynchronous RPC functions like event `subscribe` and `unsubscribe` are only available via websockets.
|
||||
|
||||
|
||||
## More Examples
|
||||
|
||||
@@ -19,7 +19,9 @@ import (
|
||||
// // handle error
|
||||
// }
|
||||
// defer client.Stop()
|
||||
// res, err := client.BroadcastEvidence(&types.DuplicateVoteEvidence{PubKey: ev.PubKey, VoteA: ev.VoteA, VoteB: ev.VoteB})
|
||||
// res, err := client.BroadcastEvidence(
|
||||
// &types.DuplicateVoteEvidence{PubKey: ev.PubKey, VoteA: ev.VoteA, VoteB: ev.VoteB},
|
||||
// )
|
||||
// ```
|
||||
//
|
||||
// > The above command returns JSON structured like this:
|
||||
|
||||
@@ -25,7 +25,10 @@ func TestMain(m *testing.M) {
|
||||
}
|
||||
|
||||
func TestBroadcastTx(t *testing.T) {
|
||||
res, err := rpctest.GetGRPCClient().BroadcastTx(context.Background(), &core_grpc.RequestBroadcastTx{Tx: []byte("this is a tx")})
|
||||
res, err := rpctest.GetGRPCClient().BroadcastTx(
|
||||
context.Background(),
|
||||
&core_grpc.RequestBroadcastTx{Tx: []byte("this is a tx")},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 0, res.CheckTx.Code)
|
||||
require.EqualValues(t, 0, res.DeliverTx.Code)
|
||||
|
||||
@@ -291,7 +291,11 @@ func (b *JSONRPCRequestBatch) Send() ([]interface{}, error) {
|
||||
|
||||
// Call enqueues a request to call the given RPC method with the specified
|
||||
// parameters, in the same way that the `JSONRPCClient.Call` function would.
|
||||
func (b *JSONRPCRequestBatch) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) {
|
||||
func (b *JSONRPCRequestBatch) Call(
|
||||
method string,
|
||||
params map[string]interface{},
|
||||
result interface{},
|
||||
) (interface{}, error) {
|
||||
request, err := types.MapToRequest(b.client.cdc, b.client.id, method, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -351,7 +355,12 @@ func (c *URIClient) SetCodec(cdc *amino.Codec) {
|
||||
|
||||
//------------------------------------------------
|
||||
|
||||
func unmarshalResponseBytes(cdc *amino.Codec, responseBytes []byte, expectedID types.JSONRPCStringID, result interface{}) (interface{}, error) {
|
||||
func unmarshalResponseBytes(
|
||||
cdc *amino.Codec,
|
||||
responseBytes []byte,
|
||||
expectedID types.JSONRPCStringID,
|
||||
result interface{},
|
||||
) (interface{}, error) {
|
||||
// Read response. If rpc/core/types is imported, the result will unmarshal
|
||||
// into the correct type.
|
||||
// log.Notice("response", "response", string(responseBytes))
|
||||
@@ -377,7 +386,12 @@ func unmarshalResponseBytes(cdc *amino.Codec, responseBytes []byte, expectedID t
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func unmarshalResponseBytesArray(cdc *amino.Codec, responseBytes []byte, expectedID types.JSONRPCStringID, results []interface{}) ([]interface{}, error) {
|
||||
func unmarshalResponseBytesArray(
|
||||
cdc *amino.Codec,
|
||||
responseBytes []byte,
|
||||
expectedID types.JSONRPCStringID,
|
||||
results []interface{},
|
||||
) ([]interface{}, error) {
|
||||
var (
|
||||
err error
|
||||
responses []types.RPCResponse
|
||||
@@ -390,7 +404,11 @@ func unmarshalResponseBytesArray(cdc *amino.Codec, responseBytes []byte, expecte
|
||||
// and unsuccessful responses.
|
||||
|
||||
if len(results) != len(responses) {
|
||||
return nil, errors.Errorf("expected %d result objects into which to inject responses, but got %d", len(responses), len(results))
|
||||
return nil, errors.Errorf(
|
||||
"expected %d result objects into which to inject responses, but got %d",
|
||||
len(responses),
|
||||
len(results),
|
||||
)
|
||||
}
|
||||
|
||||
for i, response := range responses {
|
||||
|
||||
@@ -414,7 +414,10 @@ func (c *WSClient) writeRoutine() {
|
||||
case <-c.readRoutineQuit:
|
||||
return
|
||||
case <-c.Quit():
|
||||
if err := c.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")); err != nil {
|
||||
if err := c.conn.WriteMessage(
|
||||
websocket.CloseMessage,
|
||||
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
|
||||
); err != nil {
|
||||
c.Logger.Error("failed to write message", "err", err)
|
||||
}
|
||||
return
|
||||
|
||||
+71
-16
@@ -23,8 +23,10 @@ import (
|
||||
types "github.com/tendermint/tendermint/rpc/lib/types"
|
||||
)
|
||||
|
||||
// RegisterRPCFuncs adds a route for each function in the funcMap, as well as general jsonrpc and websocket handlers for all functions.
|
||||
// "result" is the interface on which the result objects are registered, and is popualted with every RPCResponse
|
||||
// RegisterRPCFuncs adds a route for each function in the funcMap,
|
||||
// as well as general jsonrpc and websocket handlers for all functions.
|
||||
// "result" is the interface on which the result objects are registered,
|
||||
// and is popualted with every RPCResponse
|
||||
func RegisterRPCFuncs(mux *http.ServeMux, funcMap map[string]*RPCFunc, cdc *amino.Codec, logger log.Logger) {
|
||||
// HTTP endpoints
|
||||
for funcName, rpcFunc := range funcMap {
|
||||
@@ -103,7 +105,13 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, cdc *amino.Codec, logger lo
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
WriteRPCResponseHTTP(w, types.RPCInvalidRequestError(types.JSONRPCStringID(""), errors.Wrap(err, "error reading request body")))
|
||||
WriteRPCResponseHTTP(
|
||||
w,
|
||||
types.RPCInvalidRequestError(
|
||||
types.JSONRPCStringID(""),
|
||||
errors.Wrap(err, "error reading request body"),
|
||||
),
|
||||
)
|
||||
return
|
||||
}
|
||||
// if its an empty request (like from a browser),
|
||||
@@ -122,7 +130,13 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, cdc *amino.Codec, logger lo
|
||||
// next, try to unmarshal as a single request
|
||||
var request types.RPCRequest
|
||||
if err := json.Unmarshal(b, &request); err != nil {
|
||||
WriteRPCResponseHTTP(w, types.RPCParseError(types.JSONRPCStringID(""), errors.Wrap(err, "error unmarshalling request")))
|
||||
WriteRPCResponseHTTP(
|
||||
w,
|
||||
types.RPCParseError(
|
||||
types.JSONRPCStringID(""),
|
||||
errors.Wrap(err, "error unmarshalling request"),
|
||||
),
|
||||
)
|
||||
return
|
||||
}
|
||||
requests = []types.RPCRequest{request}
|
||||
@@ -133,11 +147,16 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, cdc *amino.Codec, logger lo
|
||||
// A Notification is a Request object without an "id" member.
|
||||
// The Server MUST NOT reply to a Notification, including those that are within a batch request.
|
||||
if request.ID == types.JSONRPCStringID("") {
|
||||
logger.Debug("HTTPJSONRPC received a notification, skipping... (please send a non-empty ID if you want to call a method)")
|
||||
logger.Debug(
|
||||
"HTTPJSONRPC received a notification, skipping... (please send a non-empty ID if you want to call a method)",
|
||||
)
|
||||
continue
|
||||
}
|
||||
if len(r.URL.Path) > 1 {
|
||||
responses = append(responses, types.RPCInvalidRequestError(request.ID, errors.Errorf("path %s is invalid", r.URL.Path)))
|
||||
responses = append(
|
||||
responses,
|
||||
types.RPCInvalidRequestError(request.ID, errors.Errorf("path %s is invalid", r.URL.Path)),
|
||||
)
|
||||
continue
|
||||
}
|
||||
rpcFunc, ok := funcMap[request.Method]
|
||||
@@ -150,7 +169,10 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, cdc *amino.Codec, logger lo
|
||||
if len(request.Params) > 0 {
|
||||
fnArgs, err := jsonParamsToArgs(rpcFunc, cdc, request.Params)
|
||||
if err != nil {
|
||||
responses = append(responses, types.RPCInvalidParamsError(request.ID, errors.Wrap(err, "error converting json params to arguments")))
|
||||
responses = append(
|
||||
responses,
|
||||
types.RPCInvalidParamsError(request.ID, errors.Wrap(err, "error converting json params to arguments")),
|
||||
)
|
||||
continue
|
||||
}
|
||||
args = append(args, fnArgs...)
|
||||
@@ -172,8 +194,8 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, cdc *amino.Codec, logger lo
|
||||
|
||||
func handleInvalidJSONRPCPaths(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Since the pattern "/" matches all paths not matched by other registered patterns we check whether the path is indeed
|
||||
// "/", otherwise return a 404 error
|
||||
// Since the pattern "/" matches all paths not matched by other registered patterns,
|
||||
// we check whether the path is indeed "/", otherwise return a 404 error
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
@@ -183,7 +205,12 @@ func handleInvalidJSONRPCPaths(next http.HandlerFunc) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func mapParamsToArgs(rpcFunc *RPCFunc, cdc *amino.Codec, params map[string]json.RawMessage, argsOffset int) ([]reflect.Value, error) {
|
||||
func mapParamsToArgs(
|
||||
rpcFunc *RPCFunc,
|
||||
cdc *amino.Codec,
|
||||
params map[string]json.RawMessage,
|
||||
argsOffset int,
|
||||
) ([]reflect.Value, error) {
|
||||
values := make([]reflect.Value, len(rpcFunc.argNames))
|
||||
for i, argName := range rpcFunc.argNames {
|
||||
argType := rpcFunc.args[i+argsOffset]
|
||||
@@ -203,7 +230,12 @@ func mapParamsToArgs(rpcFunc *RPCFunc, cdc *amino.Codec, params map[string]json.
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func arrayParamsToArgs(rpcFunc *RPCFunc, cdc *amino.Codec, params []json.RawMessage, argsOffset int) ([]reflect.Value, error) {
|
||||
func arrayParamsToArgs(
|
||||
rpcFunc *RPCFunc,
|
||||
cdc *amino.Codec,
|
||||
params []json.RawMessage,
|
||||
argsOffset int,
|
||||
) ([]reflect.Value, error) {
|
||||
if len(rpcFunc.argNames) != len(params) {
|
||||
return nil, errors.Errorf("expected %v parameters (%v), got %v (%v)",
|
||||
len(rpcFunc.argNames), rpcFunc.argNames, len(params), params)
|
||||
@@ -272,7 +304,13 @@ func makeHTTPHandler(rpcFunc *RPCFunc, cdc *amino.Codec, logger log.Logger) func
|
||||
|
||||
fnArgs, err := httpParamsToArgs(rpcFunc, cdc, r)
|
||||
if err != nil {
|
||||
WriteRPCResponseHTTP(w, types.RPCInvalidParamsError(types.JSONRPCStringID(""), errors.Wrap(err, "error converting http params to arguments")))
|
||||
WriteRPCResponseHTTP(
|
||||
w,
|
||||
types.RPCInvalidParamsError(
|
||||
types.JSONRPCStringID(""),
|
||||
errors.Wrap(err, "error converting http params to arguments"),
|
||||
),
|
||||
)
|
||||
return
|
||||
}
|
||||
args = append(args, fnArgs...)
|
||||
@@ -363,7 +401,16 @@ func _nonJSONStringToArg(cdc *amino.Codec, rt reflect.Type, arg string) (reflect
|
||||
|
||||
var expectingString, expectingByteSlice, expectingInt bool
|
||||
switch rt.Kind() {
|
||||
case reflect.Int, reflect.Uint, reflect.Int8, reflect.Uint8, reflect.Int16, reflect.Uint16, reflect.Int32, reflect.Uint32, reflect.Int64, reflect.Uint64:
|
||||
case reflect.Int,
|
||||
reflect.Uint,
|
||||
reflect.Int8,
|
||||
reflect.Uint8,
|
||||
reflect.Int16,
|
||||
reflect.Uint16,
|
||||
reflect.Int32,
|
||||
reflect.Uint32,
|
||||
reflect.Int64,
|
||||
reflect.Uint64:
|
||||
expectingInt = true
|
||||
case reflect.String:
|
||||
expectingString = true
|
||||
@@ -661,7 +708,9 @@ func (wsc *wsConnection) readRoutine() {
|
||||
// A Notification is a Request object without an "id" member.
|
||||
// The Server MUST NOT reply to a Notification, including those that are within a batch request.
|
||||
if request.ID == types.JSONRPCStringID("") {
|
||||
wsc.Logger.Debug("WSJSONRPC received a notification, skipping... (please send a non-empty ID if you want to call a method)")
|
||||
wsc.Logger.Debug(
|
||||
"WSJSONRPC received a notification, skipping... (please send a non-empty ID if you want to call a method)",
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -677,7 +726,9 @@ func (wsc *wsConnection) readRoutine() {
|
||||
if len(request.Params) > 0 {
|
||||
fnArgs, err := jsonParamsToArgs(rpcFunc, wsc.cdc, request.Params)
|
||||
if err != nil {
|
||||
wsc.WriteRPCResponse(types.RPCInternalError(request.ID, errors.Wrap(err, "error converting json params to arguments")))
|
||||
wsc.WriteRPCResponse(
|
||||
types.RPCInternalError(request.ID, errors.Wrap(err, "error converting json params to arguments")),
|
||||
)
|
||||
continue
|
||||
}
|
||||
args = append(args, fnArgs...)
|
||||
@@ -773,7 +824,11 @@ type WebsocketManager struct {
|
||||
|
||||
// NewWebsocketManager returns a new WebsocketManager that passes a map of
|
||||
// functions, connection options and logger to new WS connections.
|
||||
func NewWebsocketManager(funcMap map[string]*RPCFunc, cdc *amino.Codec, wsConnOptions ...func(*wsConnection)) *WebsocketManager {
|
||||
func NewWebsocketManager(
|
||||
funcMap map[string]*RPCFunc,
|
||||
cdc *amino.Codec,
|
||||
wsConnOptions ...func(*wsConnection),
|
||||
) *WebsocketManager {
|
||||
return &WebsocketManager{
|
||||
funcMap: funcMap,
|
||||
cdc: cdc,
|
||||
|
||||
@@ -54,7 +54,8 @@ func TestRPCParams(t *testing.T) {
|
||||
// bad
|
||||
{`{"jsonrpc": "2.0", "id": "0"}`, "Method not found", types.JSONRPCStringID("0")},
|
||||
{`{"jsonrpc": "2.0", "method": "y", "id": "0"}`, "Method not found", types.JSONRPCStringID("0")},
|
||||
{`{"method": "c", "id": "0", "params": a}`, "invalid character", types.JSONRPCStringID("")}, // id not captured in JSON parsing failures
|
||||
// id not captured in JSON parsing failures
|
||||
{`{"method": "c", "id": "0", "params": a}`, "invalid character", types.JSONRPCStringID("")},
|
||||
{`{"method": "c", "id": "0", "params": ["a"]}`, "got 1", types.JSONRPCStringID("0")},
|
||||
{`{"method": "c", "id": "0", "params": ["a", "b"]}`, "invalid character", types.JSONRPCStringID("0")},
|
||||
{`{"method": "c", "id": "0", "params": [1, 1]}`, "of type string", types.JSONRPCStringID("0")},
|
||||
@@ -253,7 +254,12 @@ func TestWebsocketManagerHandler(t *testing.T) {
|
||||
}
|
||||
|
||||
// check basic functionality works
|
||||
req, err := types.MapToRequest(amino.NewCodec(), types.JSONRPCStringID("TestWebsocketManager"), "c", map[string]interface{}{"s": "a", "i": 10})
|
||||
req, err := types.MapToRequest(
|
||||
amino.NewCodec(),
|
||||
types.JSONRPCStringID("TestWebsocketManager"),
|
||||
"c",
|
||||
map[string]interface{}{"s": "a", "i": 10},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
err = c.WriteJSON(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -160,7 +160,11 @@ func RecoverAndLogHandler(handler http.Handler, logger log.Logger) http.Handler
|
||||
"Panic in RPC HTTP handler", "err", e, "stack",
|
||||
string(debug.Stack()),
|
||||
)
|
||||
WriteRPCResponseHTTPError(rww, http.StatusInternalServerError, types.RPCInternalError(types.JSONRPCStringID(""), e.(error)))
|
||||
WriteRPCResponseHTTPError(
|
||||
rww,
|
||||
http.StatusInternalServerError,
|
||||
types.RPCInternalError(types.JSONRPCStringID(""), e.(error)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,10 @@ func TestResponses(t *testing.T) {
|
||||
|
||||
d := RPCParseError(jsonid, errors.New("Hello world"))
|
||||
e, _ := json.Marshal(d)
|
||||
f := fmt.Sprintf(`{"jsonrpc":"2.0","id":%v,"error":{"code":-32700,"message":"Parse error. Invalid JSON","data":"Hello world"}}`, tt.expected)
|
||||
f := fmt.Sprintf(
|
||||
`{"jsonrpc":"2.0","id":%v,"error":{"code":-32700,"message":"Parse error. Invalid JSON","data":"Hello world"}}`,
|
||||
tt.expected,
|
||||
)
|
||||
assert.Equal(f, string(e))
|
||||
|
||||
g := RPCMethodNotFoundError(jsonid)
|
||||
@@ -58,7 +61,10 @@ func TestUnmarshallResponses(t *testing.T) {
|
||||
cdc := amino.NewCodec()
|
||||
for _, tt := range responseTests {
|
||||
response := &RPCResponse{}
|
||||
err := json.Unmarshal([]byte(fmt.Sprintf(`{"jsonrpc":"2.0","id":%v,"result":{"Value":"hello"}}`, tt.expected)), response)
|
||||
err := json.Unmarshal(
|
||||
[]byte(fmt.Sprintf(`{"jsonrpc":"2.0","id":%v,"result":{"Value":"hello"}}`, tt.expected)),
|
||||
response,
|
||||
)
|
||||
assert.Nil(err)
|
||||
a := NewRPCSuccessResponse(cdc, tt.id, &SampleResult{"hello"})
|
||||
assert.Equal(*response, a)
|
||||
|
||||
Reference in New Issue
Block a user