Merge pull request #398 from ethcore/jsonrpc2

eth_syncing, fixed #397
This commit is contained in:
Arkadiy Paronyan 2016-02-10 19:13:00 +01:00
commit 0f665a6cbc
5 changed files with 74 additions and 21 deletions

View File

@ -96,7 +96,7 @@ fn setup_rpc_server(client: Arc<Client>, sync: Arc<EthSync>, url: &str) {
let mut server = rpc::HttpServer::new(1); let mut server = rpc::HttpServer::new(1);
server.add_delegate(Web3Client::new().to_delegate()); server.add_delegate(Web3Client::new().to_delegate());
server.add_delegate(EthClient::new(client.clone()).to_delegate()); server.add_delegate(EthClient::new(client.clone(), sync.clone()).to_delegate());
server.add_delegate(EthFilterClient::new(client).to_delegate()); server.add_delegate(EthFilterClient::new(client).to_delegate());
server.add_delegate(NetClient::new(sync).to_delegate()); server.add_delegate(NetClient::new(sync).to_delegate());
server.start_async(url); server.start_async(url);

View File

@ -16,6 +16,7 @@
//! Eth rpc implementation. //! Eth rpc implementation.
use std::sync::Arc; use std::sync::Arc;
use ethsync::{EthSync, SyncState};
use jsonrpc_core::*; use jsonrpc_core::*;
use util::hash::*; use util::hash::*;
use util::uint::*; use util::uint::*;
@ -23,36 +24,48 @@ use util::sha3::*;
use ethcore::client::*; use ethcore::client::*;
use ethcore::views::*; use ethcore::views::*;
use ethcore::blockchain::{BlockId, TransactionId}; use ethcore::blockchain::{BlockId, TransactionId};
use ethcore::ethereum::denominations::shannon;
use v1::traits::{Eth, EthFilter}; use v1::traits::{Eth, EthFilter};
use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, Transaction, OptionalValue, Index}; use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, OptionalValue, Index};
/// Eth rpc implementation. /// Eth rpc implementation.
pub struct EthClient { pub struct EthClient {
client: Arc<Client>, client: Arc<Client>,
sync: Arc<EthSync>
} }
impl EthClient { impl EthClient {
/// Creates new EthClient. /// Creates new EthClient.
pub fn new(client: Arc<Client>) -> Self { pub fn new(client: Arc<Client>, sync: Arc<EthSync>) -> Self {
EthClient { EthClient {
client: client client: client,
sync: sync
} }
} }
} }
impl Eth for EthClient { impl Eth for EthClient {
// TODO: do not hardcode protocol version
fn protocol_version(&self, params: Params) -> Result<Value, Error> { fn protocol_version(&self, params: Params) -> Result<Value, Error> {
match params { match params {
Params::None => Ok(Value::U64(63)), Params::None => to_value(&U256::from(self.sync.status().protocol_version)),
_ => Err(Error::invalid_params()) _ => Err(Error::invalid_params())
} }
} }
// TODO: do no hardcode default sync status
fn syncing(&self, params: Params) -> Result<Value, Error> { fn syncing(&self, params: Params) -> Result<Value, Error> {
match params { match params {
Params::None => to_value(&SyncStatus::default()), Params::None => {
let status = 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),
highest_block: U256::from(status.highest_block_number.unwrap_or(status.start_block_number))
})
};
to_value(&res)
}
_ => Err(Error::invalid_params()) _ => Err(Error::invalid_params())
} }
} }
@ -76,22 +89,21 @@ impl Eth for EthClient {
// TODO: return real hashrate once we have mining // TODO: return real hashrate once we have mining
fn hashrate(&self, params: Params) -> Result<Value, Error> { fn hashrate(&self, params: Params) -> Result<Value, Error> {
match params { match params {
Params::None => Ok(Value::U64(0)), Params::None => to_value(&U256::zero()),
_ => Err(Error::invalid_params()) _ => Err(Error::invalid_params())
} }
} }
// TODO: do not hardode gas_price
fn gas_price(&self, params: Params) -> Result<Value, Error> { fn gas_price(&self, params: Params) -> Result<Value, Error> {
match params { match params {
Params::None => Ok(Value::U64(0)), Params::None => to_value(&(shannon() * U256::from(50))),
_ => Err(Error::invalid_params()) _ => Err(Error::invalid_params())
} }
} }
fn block_number(&self, params: Params) -> Result<Value, Error> { fn block_number(&self, params: Params) -> Result<Value, Error> {
match params { match params {
Params::None => Ok(Value::U64(self.client.chain_info().best_block_number)), Params::None => to_value(&U256::from(self.client.chain_info().best_block_number)),
_ => Err(Error::invalid_params()) _ => Err(Error::invalid_params())
} }
} }

View File

@ -29,5 +29,5 @@ pub use self::bytes::Bytes;
pub use self::filter::Filter; pub use self::filter::Filter;
pub use self::index::Index; pub use self::index::Index;
pub use self::optionals::OptionalValue; pub use self::optionals::OptionalValue;
pub use self::sync::SyncStatus; pub use self::sync::{SyncStatus, SyncInfo};
pub use self::transaction::Transaction; pub use self::transaction::Transaction;

View File

@ -14,14 +14,55 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use util::hash::*; use serde::{Serialize, Serializer};
use util::uint::*;
#[derive(Default, Debug, Serialize)] #[derive(Default, Debug, Serialize, PartialEq)]
pub struct SyncStatus { pub struct SyncInfo {
#[serde(rename="startingBlock")] #[serde(rename="startingBlock")]
pub starting_block: H256, pub starting_block: U256,
#[serde(rename="currentBlock")] #[serde(rename="currentBlock")]
pub current_block: H256, pub current_block: U256,
#[serde(rename="highestBlock")] #[serde(rename="highestBlock")]
pub highest_block: H256, pub highest_block: U256,
}
#[derive(Debug, PartialEq)]
pub enum SyncStatus {
Info(SyncInfo),
None
}
impl Serialize for SyncStatus {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer {
match *self {
SyncStatus::Info(ref info) => info.serialize(serializer),
SyncStatus::None => false.serialize(serializer)
}
}
}
#[cfg(test)]
mod tests {
use serde_json;
use super::*;
#[test]
fn test_serialize_sync_info() {
let t = SyncInfo::default();
let serialized = serde_json::to_string(&t).unwrap();
assert_eq!(serialized, r#"{"startingBlock":"0x00","currentBlock":"0x00","highestBlock":"0x00"}"#);
}
#[test]
fn test_serialize_sync_status() {
let t = SyncStatus::None;
let serialized = serde_json::to_string(&t).unwrap();
assert_eq!(serialized, "false");
let t = SyncStatus::Info(SyncInfo::default());
let serialized = serde_json::to_string(&t).unwrap();
assert_eq!(serialized, r#"{"startingBlock":"0x00","currentBlock":"0x00","highestBlock":"0x00"}"#);
}
} }

View File

@ -76,7 +76,7 @@ pub struct EthSync {
sync: RwLock<ChainSync> sync: RwLock<ChainSync>
} }
pub use self::chain::SyncStatus; pub use self::chain::{SyncStatus, SyncState};
impl EthSync { impl EthSync {
/// Creates and register protocol with the network service /// Creates and register protocol with the network service