Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
parity-test-sync
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
parity-test-sync
Commits
a8e49125
Commit
a8e49125
authored
9 years ago
by
Marek Kotewicz
Browse files
Options
Downloads
Patches
Plain Diff
cleanup ethrpc
parent
47e28672
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/bin/client/ethrpc.rs
+55
-20
55 additions, 20 deletions
src/bin/client/ethrpc.rs
src/bin/client/main.rs
+2
-1
2 additions, 1 deletion
src/bin/client/main.rs
with
57 additions
and
21 deletions
src/bin/client/ethrpc.rs
+
55
−
20
View file @
a8e49125
...
...
@@ -5,17 +5,56 @@ use std::sync::{Arc, RwLock};
use
self
::
jsonrpc_core
::{
IoHandler
,
IoDelegate
,
Params
,
Value
,
Error
,
ErrorCode
};
use
ethcore
::
client
::
*
;
struct
Eth
{
client
:
Arc
<
RwLock
<
Client
>>
macro_rules!
rpcerr
{
()
=>
(
Err
(
Error
::
new
(
ErrorCode
::
InternalError
)))
}
impl
Eth
{
fn
new
(
client
:
Arc
<
RwLock
<
Client
>>
)
->
Self
{
Eth
{
/// This could be a part of `jsonrpc_core`. Unfortunately,
/// "only traits defined in the current crate can be implemented for a type parameter".
pub
trait
IntoDelegate
<
T
>
where
T
:
Send
+
Sync
+
'static
{
/// This function should be called to translate custom type into IoDelegate
fn
into_delegate
(
self
)
->
IoDelegate
<
T
>
;
}
/// eth rpc interface
pub
trait
Eth
{
/// returns protocol version
fn
protocol_version
(
&
self
,
_
:
Params
)
->
Result
<
Value
,
Error
>
{
rpcerr!
()
}
/// returns block author
fn
author
(
&
self
,
_
:
Params
)
->
Result
<
Value
,
Error
>
{
rpcerr!
()
}
/// returns current gas_price
fn
gas_price
(
&
self
,
_
:
Params
)
->
Result
<
Value
,
Error
>
{
rpcerr!
()
}
/// returns highest block number
fn
block_number
(
&
self
,
_
:
Params
)
->
Result
<
Value
,
Error
>
{
rpcerr!
()
}
}
impl
<
D
>
IntoDelegate
<
D
>
for
D
where
D
:
Eth
+
Send
+
Sync
+
'static
{
fn
into_delegate
(
self
)
->
IoDelegate
<
D
>
{
let
mut
delegate
=
IoDelegate
::
new
(
Arc
::
new
(
self
));
delegate
.add_method
(
"eth_protocolVersion"
,
D
::
protocol_version
);
delegate
.add_method
(
"eth_coinbase"
,
D
::
author
);
delegate
.add_method
(
"eth_gasPrice"
,
D
::
gas_price
);
delegate
.add_method
(
"eth_blockNumber"
,
D
::
block_number
);
delegate
}
}
pub
struct
EthClient
{
client
:
Arc
<
RwLock
<
Client
>>
,
}
impl
EthClient
{
pub
fn
new
(
client
:
Arc
<
RwLock
<
Client
>>
)
->
Self
{
EthClient
{
client
:
client
}
}
}
impl
Eth
for
EthClient
{
fn
block_number
(
&
self
,
params
:
Params
)
->
Result
<
Value
,
Error
>
{
match
params
{
Params
::
None
=>
Ok
(
Value
::
U64
(
self
.client
.read
()
.unwrap
()
.chain_info
()
.best_block_number
)),
...
...
@@ -24,30 +63,26 @@ impl Eth {
}
}
struct
EthRpc
;
impl
EthRpc
{
fn
build_handler
(
client
:
Arc
<
RwLock
<
Client
>>
)
->
IoHandler
{
let
mut
handler
=
IoHandler
::
new
();
let
mut
eth
=
IoDelegate
::
new
(
Arc
::
new
(
Eth
::
new
(
client
)));
eth
.add_method
(
"eth_blockNumber"
,
Eth
::
block_number
);
handler
.add_delegate
(
eth
);
handler
}
}
pub
struct
HttpServer
{
server
:
jsonrpc_http_server
::
Server
handler
:
IoHandler
,
threads
:
usize
}
impl
HttpServer
{
pub
fn
new
(
client
:
Arc
<
RwLock
<
Client
>>
,
threads
:
usize
)
->
HttpServer
{
pub
fn
new
(
threads
:
usize
)
->
HttpServer
{
HttpServer
{
server
:
jsonrpc_http_server
::
Server
::
new
(
EthRpc
::
build_handler
(
client
),
threads
)
handler
:
IoHandler
::
new
(),
threads
:
threads
}
}
pub
fn
add_delegate
<
I
,
D
>
(
&
mut
self
,
delegate
:
I
)
where
D
:
Send
+
Sync
+
'static
,
I
:
IntoDelegate
<
D
>
{
self
.handler
.add_delegate
(
delegate
.into_delegate
());
}
pub
fn
start_async
(
self
,
addr
:
&
str
)
{
self
.server
.start_async
(
addr
)
let
server
=
jsonrpc_http_server
::
Server
::
new
(
self
.handler
,
self
.threads
);
server
.start_async
(
addr
)
}
}
This diff is collapsed.
Click to expand it.
src/bin/client/main.rs
+
2
−
1
View file @
a8e49125
...
...
@@ -32,7 +32,8 @@ fn setup_log() {
#[cfg(feature
=
"rpc"
)]
fn
setup_rpc_server
(
client
:
Arc
<
RwLock
<
Client
>>
)
{
let
server
=
ethrpc
::
HttpServer
::
new
(
client
,
1
);
let
mut
server
=
ethrpc
::
HttpServer
::
new
(
1
);
server
.add_delegate
(
ethrpc
::
EthClient
::
new
(
client
));
server
.start_async
(
"127.0.0.1:3030"
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment