Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #532 from ethcore/weak_jsonrpc
Browse files Browse the repository at this point in the history
jsonrpc uses weak pointers to client
  • Loading branch information
debris committed Feb 29, 2016
2 parents 90b41a8 + 77bfe5a commit 47c074a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
6 changes: 3 additions & 3 deletions parity/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ fn setup_rpc_server(client: Arc<Client>, sync: Arc<EthSync>, url: &str, cors_dom

let mut server = rpc::HttpServer::new(1);
server.add_delegate(Web3Client::new().to_delegate());
server.add_delegate(EthClient::new(client.clone(), sync.clone()).to_delegate());
server.add_delegate(EthFilterClient::new(client).to_delegate());
server.add_delegate(NetClient::new(sync).to_delegate());
server.add_delegate(EthClient::new(&client, &sync).to_delegate());
server.add_delegate(EthFilterClient::new(&client).to_delegate());
server.add_delegate(NetClient::new(&sync).to_delegate());
server.start_async(url, cors_domain);
}

Expand Down
43 changes: 22 additions & 21 deletions rpc/src/v1/impls/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Eth rpc implementation.
use std::sync::Arc;
use std::sync::{Arc, Weak};
use ethsync::{EthSync, SyncState};
use jsonrpc_core::*;
use util::hash::*;
Expand All @@ -29,21 +29,22 @@ use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncIn

/// Eth rpc implementation.
pub struct EthClient {
client: Arc<Client>,
sync: Arc<EthSync>
client: Weak<Client>,
sync: Weak<EthSync>
}

impl EthClient {
/// Creates new EthClient.
pub fn new(client: Arc<Client>, sync: Arc<EthSync>) -> Self {
pub fn new(client: &Arc<Client>, sync: &Arc<EthSync>) -> Self {
EthClient {
client: client,
sync: sync
client: Arc::downgrade(client),
sync: Arc::downgrade(sync)
}
}

fn block(&self, id: BlockId, include_txs: bool) -> Result<Value, Error> {
match (self.client.block(id.clone()), self.client.block_total_difficulty(id)) {
let client = take_weak!(self.client);
match (client.block(id.clone()), client.block_total_difficulty(id)) {
(Some(bytes), Some(total_difficulty)) => {
let block_view = BlockView::new(&bytes);
let view = block_view.header_view();
Expand Down Expand Up @@ -78,9 +79,9 @@ impl EthClient {
_ => Ok(Value::Null)
}
}

fn transaction(&self, id: TransactionId) -> Result<Value, Error> {
match self.client.transaction(id) {
match take_weak!(self.client).transaction(id) {
Some(t) => to_value(&Transaction::from(t)),
None => Ok(Value::Null)
}
Expand All @@ -90,20 +91,20 @@ impl EthClient {
impl Eth for EthClient {
fn protocol_version(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => to_value(&U256::from(self.sync.status().protocol_version)),
Params::None => to_value(&U256::from(take_weak!(self.sync).status().protocol_version)),
_ => Err(Error::invalid_params())
}
}

fn syncing(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => {
let status = self.sync.status();
let status = take_weak!(self.sync).status();
let res = match status.state {
SyncState::NotSynced | SyncState::Idle => SyncStatus::None,
SyncState::Waiting | SyncState::Blocks | SyncState::NewBlocks => SyncStatus::Info(SyncInfo {
starting_block: U256::from(status.start_block_number),
current_block: U256::from(self.client.chain_info().best_block_number),
current_block: U256::from(take_weak!(self.client).chain_info().best_block_number),
highest_block: U256::from(status.highest_block_number.unwrap_or(status.start_block_number))
})
};
Expand Down Expand Up @@ -146,22 +147,22 @@ impl Eth for EthClient {

fn block_number(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => to_value(&U256::from(self.client.chain_info().best_block_number)),
Params::None => to_value(&U256::from(take_weak!(self.client).chain_info().best_block_number)),
_ => Err(Error::invalid_params())
}
}

fn block_transaction_count(&self, params: Params) -> Result<Value, Error> {
from_params::<(H256,)>(params)
.and_then(|(hash,)| match self.client.block(BlockId::Hash(hash)) {
.and_then(|(hash,)| match take_weak!(self.client).block(BlockId::Hash(hash)) {
Some(bytes) => to_value(&BlockView::new(&bytes).transactions_count()),
None => Ok(Value::Null)
})
}

fn block_uncles_count(&self, params: Params) -> Result<Value, Error> {
from_params::<(H256,)>(params)
.and_then(|(hash,)| match self.client.block(BlockId::Hash(hash)) {
.and_then(|(hash,)| match take_weak!(self.client).block(BlockId::Hash(hash)) {
Some(bytes) => to_value(&BlockView::new(&bytes).uncles_count()),
None => Ok(Value::Null)
})
Expand All @@ -170,7 +171,7 @@ impl Eth for EthClient {
// TODO: do not ignore block number param
fn code_at(&self, params: Params) -> Result<Value, Error> {
from_params::<(Address, BlockNumber)>(params)
.and_then(|(address, _block_number)| to_value(&self.client.code(&address).map_or_else(Bytes::default, Bytes::new)))
.and_then(|(address, _block_number)| to_value(&take_weak!(self.client).code(&address).map_or_else(Bytes::default, Bytes::new)))
}

fn block_by_hash(&self, params: Params) -> Result<Value, Error> {
Expand Down Expand Up @@ -201,7 +202,7 @@ impl Eth for EthClient {
fn logs(&self, params: Params) -> Result<Value, Error> {
from_params::<(Filter,)>(params)
.and_then(|(filter,)| {
let logs = self.client.logs(filter.into())
let logs = take_weak!(self.client).logs(filter.into())
.into_iter()
.map(From::from)
.collect::<Vec<Log>>();
Expand All @@ -212,14 +213,14 @@ impl Eth for EthClient {

/// Eth filter rpc implementation.
pub struct EthFilterClient {
client: Arc<Client>
client: Weak<Client>
}

impl EthFilterClient {
/// Creates new Eth filter client.
pub fn new(client: Arc<Client>) -> Self {
pub fn new(client: &Arc<Client>) -> Self {
EthFilterClient {
client: client
client: Arc::downgrade(client)
}
}
}
Expand All @@ -234,6 +235,6 @@ impl EthFilter for EthFilterClient {
}

fn filter_changes(&self, _: Params) -> Result<Value, Error> {
to_value(&self.client.chain_info().best_block_hash).map(|v| Value::Array(vec![v]))
to_value(&take_weak!(self.client).chain_info().best_block_hash).map(|v| Value::Array(vec![v]))
}
}
10 changes: 10 additions & 0 deletions rpc/src/v1/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Ethereum rpc interface implementation.

macro_rules! take_weak {
($weak: expr) => {
match $weak.upgrade() {
Some(arc) => arc,
None => return Err(Error::internal_error())
}
}
}

mod web3;
mod eth;
mod net;
Expand Down
12 changes: 6 additions & 6 deletions rpc/src/v1/impls/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Net rpc implementation.
use std::sync::Arc;
use std::sync::{Arc, Weak};
use jsonrpc_core::*;
use ethsync::EthSync;
use v1::traits::Net;

/// Net rpc implementation.
pub struct NetClient {
sync: Arc<EthSync>
sync: Weak<EthSync>
}

impl NetClient {
/// Creates new NetClient.
pub fn new(sync: Arc<EthSync>) -> Self {
pub fn new(sync: &Arc<EthSync>) -> Self {
NetClient {
sync: sync
sync: Arc::downgrade(sync)
}
}
}

impl Net for NetClient {
fn version(&self, _: Params) -> Result<Value, Error> {
Ok(Value::U64(self.sync.status().protocol_version as u64))
Ok(Value::U64(take_weak!(self.sync).status().protocol_version as u64))
}

fn peer_count(&self, _params: Params) -> Result<Value, Error> {
Ok(Value::U64(self.sync.status().num_peers as u64))
Ok(Value::U64(take_weak!(self.sync).status().num_peers as u64))
}
}

0 comments on commit 47c074a

Please sign in to comment.