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
f3aac71f
Commit
f3aac71f
authored
Dec 09, 2015
by
zsfelfoldi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rpc/v2: optionally passing context argument to rpc v2 api methods
parent
fa187a36
Changes
8
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
1122 additions
and
18 deletions
+1122
-18
Godeps.json
Godeps/Godeps.json
+4
-0
context.go
Godeps/_workspace/src/golang.org/x/net/context/context.go
+447
-0
context_test.go
...s/_workspace/src/golang.org/x/net/context/context_test.go
+575
-0
withtimeout_test.go
...orkspace/src/golang.org/x/net/context/withtimeout_test.go
+26
-0
server.go
rpc/v2/server.go
+14
-7
server_test.go
rpc/v2/server_test.go
+38
-2
types.go
rpc/v2/types.go
+1
-1
utils.go
rpc/v2/utils.go
+17
-8
No files found.
Godeps/Godeps.json
View file @
f3aac71f
...
...
@@ -101,6 +101,10 @@
"ImportPath"
:
"golang.org/x/crypto/scrypt"
,
"Rev"
:
"4ed45ec682102c643324fae5dff8dab085b6c300"
},
{
"ImportPath"
:
"golang.org/x/net/context"
,
"Rev"
:
"e0403b4e005737430c05a57aac078479844f919c"
},
{
"ImportPath"
:
"golang.org/x/net/html"
,
"Rev"
:
"e0403b4e005737430c05a57aac078479844f919c"
...
...
Godeps/_workspace/src/golang.org/x/net/context/context.go
0 → 100644
View file @
f3aac71f
This diff is collapsed.
Click to expand it.
Godeps/_workspace/src/golang.org/x/net/context/context_test.go
0 → 100644
View file @
f3aac71f
This diff is collapsed.
Click to expand it.
Godeps/_workspace/src/golang.org/x/net/context/withtimeout_test.go
0 → 100644
View file @
f3aac71f
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
context_test
import
(
"fmt"
"time"
"golang.org/x/net/context"
)
func
ExampleWithTimeout
()
{
// Pass a context with a timeout to tell a blocking function that it
// should abandon its work after the timeout elapses.
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
100
*
time
.
Millisecond
)
select
{
case
<-
time
.
After
(
200
*
time
.
Millisecond
)
:
fmt
.
Println
(
"overslept"
)
case
<-
ctx
.
Done
()
:
fmt
.
Println
(
ctx
.
Err
())
// prints "context deadline exceeded"
}
// Output:
// context deadline exceeded
}
rpc/v2/server.go
View file @
f3aac71f
...
...
@@ -25,6 +25,7 @@ import (
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"golang.org/x/net/context"
)
// NewServer will create a new server instance with no registered handlers.
...
...
@@ -120,6 +121,9 @@ func (s *Server) ServeCodec(codec ServerCodec) {
codec
.
Close
()
}()
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
for
{
reqs
,
batch
,
err
:=
s
.
readRequest
(
codec
)
if
err
!=
nil
{
...
...
@@ -129,9 +133,9 @@ func (s *Server) ServeCodec(codec ServerCodec) {
}
if
batch
{
go
s
.
execBatch
(
codec
,
reqs
)
go
s
.
execBatch
(
c
tx
,
c
odec
,
reqs
)
}
else
{
go
s
.
exec
(
codec
,
reqs
[
0
])
go
s
.
exec
(
c
tx
,
c
odec
,
reqs
[
0
])
}
}
}
...
...
@@ -220,7 +224,7 @@ func (s *Server) unsubscribe(subid string) bool {
}
// handle executes a request and returns the response from the callback.
func
(
s
*
Server
)
handle
(
codec
ServerCodec
,
req
*
serverRequest
)
interface
{}
{
func
(
s
*
Server
)
handle
(
c
tx
context
.
Context
,
c
odec
ServerCodec
,
req
*
serverRequest
)
interface
{}
{
if
req
.
err
!=
nil
{
return
codec
.
CreateErrorResponse
(
&
req
.
id
,
req
.
err
)
}
...
...
@@ -255,6 +259,9 @@ func (s *Server) handle(codec ServerCodec, req *serverRequest) interface{} {
}
arguments
:=
[]
reflect
.
Value
{
req
.
callb
.
rcvr
}
if
req
.
callb
.
hasCtx
{
arguments
=
append
(
arguments
,
reflect
.
ValueOf
(
ctx
))
}
if
len
(
req
.
args
)
>
0
{
arguments
=
append
(
arguments
,
req
.
args
...
)
}
...
...
@@ -277,12 +284,12 @@ func (s *Server) handle(codec ServerCodec, req *serverRequest) interface{} {
}
// exec executes the given request and writes the result back using the codec.
func
(
s
*
Server
)
exec
(
codec
ServerCodec
,
req
*
serverRequest
)
{
func
(
s
*
Server
)
exec
(
c
tx
context
.
Context
,
c
odec
ServerCodec
,
req
*
serverRequest
)
{
var
response
interface
{}
if
req
.
err
!=
nil
{
response
=
codec
.
CreateErrorResponse
(
&
req
.
id
,
req
.
err
)
}
else
{
response
=
s
.
handle
(
codec
,
req
)
response
=
s
.
handle
(
c
tx
,
c
odec
,
req
)
}
if
err
:=
codec
.
Write
(
response
);
err
!=
nil
{
...
...
@@ -293,13 +300,13 @@ func (s *Server) exec(codec ServerCodec, req *serverRequest) {
// execBatch executes the given requests and writes the result back using the codec. It will only write the response
// back when the last request is processed.
func
(
s
*
Server
)
execBatch
(
codec
ServerCodec
,
requests
[]
*
serverRequest
)
{
func
(
s
*
Server
)
execBatch
(
c
tx
context
.
Context
,
c
odec
ServerCodec
,
requests
[]
*
serverRequest
)
{
responses
:=
make
([]
interface
{},
len
(
requests
))
for
i
,
req
:=
range
requests
{
if
req
.
err
!=
nil
{
responses
[
i
]
=
codec
.
CreateErrorResponse
(
&
req
.
id
,
req
.
err
)
}
else
{
responses
[
i
]
=
s
.
handle
(
codec
,
req
)
responses
[
i
]
=
s
.
handle
(
c
tx
,
c
odec
,
req
)
}
}
...
...
rpc/v2/server_test.go
View file @
f3aac71f
...
...
@@ -6,6 +6,8 @@ import (
"reflect"
"testing"
"time"
"golang.org/x/net/context"
)
type
Service
struct
{}
...
...
@@ -27,6 +29,10 @@ func (s *Service) Echo(str string, i int, args *Args) Result {
return
Result
{
str
,
i
,
args
}
}
func
(
s
*
Service
)
EchoWithCtx
(
ctx
context
.
Context
,
str
string
,
i
int
,
args
*
Args
)
Result
{
return
Result
{
str
,
i
,
args
}
}
func
(
s
*
Service
)
Rets
()
(
string
,
error
)
{
return
""
,
nil
}
...
...
@@ -64,8 +70,8 @@ func TestServerRegisterName(t *testing.T) {
t
.
Fatalf
(
"Expected service calc to be registered"
)
}
if
len
(
svc
.
callbacks
)
!=
3
{
t
.
Errorf
(
"Expected
3
callbacks for service 'calc', got %d"
,
len
(
svc
.
callbacks
))
if
len
(
svc
.
callbacks
)
!=
4
{
t
.
Errorf
(
"Expected
4
callbacks for service 'calc', got %d"
,
len
(
svc
.
callbacks
))
}
if
len
(
svc
.
subscriptions
)
!=
1
{
...
...
@@ -217,3 +223,33 @@ func TestServerMethodExecution(t *testing.T) {
t
.
Fatalf
(
"expected %s, got %s
\n
"
,
expected
,
codec
.
output
)
}
}
func
TestServerMethodWithCtx
(
t
*
testing
.
T
)
{
server
:=
NewServer
()
service
:=
new
(
Service
)
if
err
:=
server
.
RegisterName
(
"test"
,
service
);
err
!=
nil
{
t
.
Fatalf
(
"%v"
,
err
)
}
id
:=
int64
(
12345
)
req
:=
jsonRequest
{
Method
:
"echoWithCtx"
,
Version
:
"2.0"
,
Id
:
&
id
,
}
args
:=
[]
interface
{}{
"string arg"
,
1122
,
&
Args
{
"qwerty"
}}
req
.
Payload
,
_
=
json
.
Marshal
(
&
args
)
input
,
_
:=
json
.
Marshal
(
&
req
)
codec
:=
&
ServerTestCodec
{
input
:
input
,
closer
:
make
(
chan
interface
{})}
go
server
.
ServeCodec
(
codec
)
<-
codec
.
closer
expected
:=
`{"jsonrpc":"2.0","id":12345,"result":{"String":"string arg","Int":1122,"Args":{"S":"qwerty"}}}`
if
expected
!=
codec
.
output
{
t
.
Fatalf
(
"expected %s, got %s
\n
"
,
expected
,
codec
.
output
)
}
}
rpc/v2/types.go
View file @
f3aac71f
...
...
@@ -22,7 +22,6 @@ import (
"math/big"
"reflect"
"strings"
"sync"
"github.com/ethereum/go-ethereum/event"
...
...
@@ -41,6 +40,7 @@ type callback struct {
rcvr
reflect
.
Value
// receiver of method
method
reflect
.
Method
// callback
argTypes
[]
reflect
.
Type
// input argument types
hasCtx
bool
// method's first argument is a context (not included in argTypes)
errPos
int
// err return idx, of -1 when method cannot return error
isSubscribe
bool
// indication if the callback is a subscription
}
...
...
rpc/v2/utils.go
View file @
f3aac71f
...
...
@@ -24,6 +24,8 @@ import (
"reflect"
"unicode"
"unicode/utf8"
"golang.org/x/net/context"
)
// Is this an exported - upper case - name?
...
...
@@ -107,6 +109,8 @@ func isBlockNumber(t reflect.Type) bool {
return
t
==
blockNumberType
}
var
contextType
=
reflect
.
TypeOf
(
new
(
context
.
Context
))
.
Elem
()
// suitableCallbacks iterates over the methods of the given type. It will determine if a method satisfies the criteria
// for a RPC callback or a subscription callback and adds it to the collection of callbacks or subscriptions. See server
// documentation for a summary of these criteria.
...
...
@@ -129,12 +133,19 @@ METHODS:
h
.
method
=
method
h
.
errPos
=
-
1
firstArg
:=
1
numIn
:=
mtype
.
NumIn
()
if
numIn
>=
2
&&
mtype
.
In
(
1
)
==
contextType
{
h
.
hasCtx
=
true
firstArg
=
2
}
if
h
.
isSubscribe
{
h
.
argTypes
=
make
([]
reflect
.
Type
,
mtype
.
NumIn
()
-
1
)
// skip rcvr type
for
i
:=
1
;
i
<
mtype
.
NumIn
()
;
i
++
{
h
.
argTypes
=
make
([]
reflect
.
Type
,
numIn
-
firstArg
)
// skip rcvr type
for
i
:=
firstArg
;
i
<
numIn
;
i
++
{
argType
:=
mtype
.
In
(
i
)
if
isExportedOrBuiltinType
(
argType
)
{
h
.
argTypes
[
i
-
1
]
=
argType
h
.
argTypes
[
i
-
firstArg
]
=
argType
}
else
{
continue
METHODS
}
...
...
@@ -144,17 +155,15 @@ METHODS:
continue
METHODS
}
numIn
:=
mtype
.
NumIn
()
// determine method arguments, ignore first arg since it's the receiver type
// Arguments must be exported or builtin types
h
.
argTypes
=
make
([]
reflect
.
Type
,
numIn
-
1
)
for
i
:=
1
;
i
<
numIn
;
i
++
{
h
.
argTypes
=
make
([]
reflect
.
Type
,
numIn
-
firstArg
)
for
i
:=
firstArg
;
i
<
numIn
;
i
++
{
argType
:=
mtype
.
In
(
i
)
if
!
isExportedOrBuiltinType
(
argType
)
{
continue
METHODS
}
h
.
argTypes
[
i
-
1
]
=
argType
h
.
argTypes
[
i
-
firstArg
]
=
argType
}
// check that all returned values are exported or builtin types
...
...
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