Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
8fd243ee
Commit
8fd243ee
authored
Mar 17, 2015
by
Taylor Gerring
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add JSON RPC batch support
http://www.jsonrpc.org/specification#batch
parent
fe819f3b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
52 deletions
+53
-52
http.go
rpc/http.go
+52
-33
util.go
rpc/util.go
+1
-19
No files found.
rpc/http.go
View file @
8fd243ee
package
rpc
import
(
"encoding/json"
"io/ioutil"
"net/http"
"github.com/ethereum/go-ethereum/logger"
...
...
@@ -16,54 +18,71 @@ const (
// JSONRPC returns a handler that implements the Ethereum JSON-RPC API.
func
JSONRPC
(
pipe
*
xeth
.
XEth
,
dataDir
string
)
http
.
Handler
{
var
js
on
JsonWrapper
var
js
w
JsonWrapper
api
:=
NewEthereumApi
(
pipe
,
dataDir
)
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
// TODO this needs to be configurable
w
.
Header
()
.
Set
(
"Access-Control-Allow-Origin"
,
"*"
)
rpchttplogger
.
DebugDetailln
(
"Handling request"
)
// Limit request size to resist DoS
if
req
.
ContentLength
>
maxSizeReqLength
{
jsonerr
:=
&
RpcErrorObject
{
-
32700
,
"Request too large"
}
js
on
.
Send
(
w
,
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
nil
,
Error
:
jsonerr
})
js
w
.
Send
(
w
,
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
nil
,
Error
:
jsonerr
})
return
}
reqParsed
,
reqerr
:=
json
.
ParseRequestBody
(
req
)
switch
reqerr
.
(
type
)
{
case
nil
:
break
case
*
DecodeParamError
,
*
InsufficientParamsError
,
*
ValidationError
:
jsonerr
:=
&
RpcErrorObject
{
-
32602
,
reqerr
.
Error
()}
json
.
Send
(
w
,
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
nil
,
Error
:
jsonerr
})
return
default
:
jsonerr
:=
&
RpcErrorObject
{
-
32700
,
"Could not parse request"
}
json
.
Send
(
w
,
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
nil
,
Error
:
jsonerr
})
return
defer
req
.
Body
.
Close
()
body
,
err
:=
ioutil
.
ReadAll
(
req
.
Body
)
if
err
!=
nil
{
jsonerr
:=
&
RpcErrorObject
{
-
32700
,
"Could not read request body"
}
jsw
.
Send
(
w
,
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
nil
,
Error
:
jsonerr
})
}
var
response
interface
{}
reserr
:=
api
.
GetRequestReply
(
&
reqParsed
,
&
response
)
switch
reserr
.
(
type
)
{
case
nil
:
break
case
*
NotImplementedError
:
jsonerr
:=
&
RpcErrorObject
{
-
32601
,
reserr
.
Error
()}
json
.
Send
(
w
,
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
reqParsed
.
ID
,
Error
:
jsonerr
})
// Try to parse the request as a single
var
reqSingle
RpcRequest
if
err
:=
json
.
Unmarshal
(
body
,
&
reqSingle
);
err
==
nil
{
response
:=
RpcResponse
(
api
,
&
reqSingle
)
jsw
.
Send
(
w
,
&
response
)
return
case
*
DecodeParamError
,
*
InsufficientParamsError
,
*
ValidationError
:
jsonerr
:=
&
RpcErrorObject
{
-
32602
,
reserr
.
Error
()}
json
.
Send
(
w
,
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
reqParsed
.
ID
,
Error
:
jsonerr
})
return
default
:
jsonerr
:=
&
RpcErrorObject
{
-
32603
,
reserr
.
Error
()}
json
.
Send
(
w
,
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
reqParsed
.
ID
,
Error
:
jsonerr
})
}
// Try to parse the request to batch
var
reqBatch
[]
RpcRequest
if
err
:=
json
.
Unmarshal
(
body
,
&
reqBatch
);
err
==
nil
{
// Build response batch
resBatch
:=
make
([]
*
interface
{},
len
(
reqBatch
))
for
i
,
request
:=
range
reqBatch
{
response
:=
RpcResponse
(
api
,
&
request
)
resBatch
[
i
]
=
response
}
jsw
.
Send
(
w
,
resBatch
)
return
}
rpchttplogger
.
DebugDetailf
(
"Generated response: %T %s"
,
response
,
response
)
json
.
Send
(
w
,
&
RpcSuccessResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
reqParsed
.
ID
,
Result
:
response
})
// Not a batch or single request, error
jsonerr
:=
&
RpcErrorObject
{
-
32600
,
"Could not decode request"
}
jsw
.
Send
(
w
,
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
nil
,
Error
:
jsonerr
})
})
}
func
RpcResponse
(
api
*
EthereumApi
,
request
*
RpcRequest
)
*
interface
{}
{
var
reply
,
response
interface
{}
reserr
:=
api
.
GetRequestReply
(
request
,
&
reply
)
switch
reserr
.
(
type
)
{
case
nil
:
response
=
&
RpcSuccessResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
request
.
ID
,
Result
:
reply
}
case
*
NotImplementedError
:
jsonerr
:=
&
RpcErrorObject
{
-
32601
,
reserr
.
Error
()}
response
=
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
request
.
ID
,
Error
:
jsonerr
}
case
*
DecodeParamError
,
*
InsufficientParamsError
,
*
ValidationError
:
jsonerr
:=
&
RpcErrorObject
{
-
32602
,
reserr
.
Error
()}
response
=
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
request
.
ID
,
Error
:
jsonerr
}
default
:
jsonerr
:=
&
RpcErrorObject
{
-
32603
,
reserr
.
Error
()}
response
=
&
RpcErrorResponse
{
JsonRpc
:
jsonrpcver
,
ID
:
request
.
ID
,
Error
:
jsonerr
}
}
rpchttplogger
.
DebugDetailf
(
"Generated response: %T %s"
,
response
,
response
)
return
&
response
}
rpc/util.go
View file @
8fd243ee
...
...
@@ -21,7 +21,7 @@ import (
"fmt"
"io"
"math/big"
"net/http"
//
"net/http"
"reflect"
"time"
...
...
@@ -106,24 +106,6 @@ func (self JsonWrapper) Send(writer io.Writer, v interface{}) (n int, err error)
return
writer
.
Write
(
payload
)
}
func
(
self
JsonWrapper
)
ParseRequestBody
(
req
*
http
.
Request
)
(
RpcRequest
,
error
)
{
var
reqParsed
RpcRequest
// Convert JSON to native types
d
:=
json
.
NewDecoder
(
req
.
Body
)
defer
req
.
Body
.
Close
()
err
:=
d
.
Decode
(
&
reqParsed
)
if
err
!=
nil
{
rpclogger
.
Errorln
(
"Error decoding JSON: "
,
err
)
return
reqParsed
,
err
}
rpclogger
.
DebugDetailf
(
"Parsed request: %s"
,
reqParsed
)
return
reqParsed
,
nil
}
func
toHex
(
b
[]
byte
)
string
{
hex
:=
common
.
Bytes2Hex
(
b
)
// Prefer output of "0x0" instead of "0x"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment