From 9159d5812b7a530cd30feaad0fd33f8c4c181282 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 10 Feb 2016 16:28:59 +0100 Subject: [PATCH] eth_syncing, fixed #397 --- parity/main.rs | 2 +- rpc/src/v1/impls/eth.rs | 23 +++++++++++++---- rpc/src/v1/types/mod.rs | 2 +- rpc/src/v1/types/sync.rs | 53 +++++++++++++++++++++++++++++++++++----- sync/src/lib.rs | 4 +-- 5 files changed, 69 insertions(+), 15 deletions(-) diff --git a/parity/main.rs b/parity/main.rs index f5a07208e..586461a54 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -95,7 +95,7 @@ fn setup_rpc_server(client: Arc, sync: Arc, url: &str) { let mut server = rpc::HttpServer::new(1); 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(NetClient::new(sync).to_delegate()); server.start_async(url); diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 5d60b40a6..a87b4b2a5 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -16,6 +16,7 @@ //! Eth rpc implementation. use std::sync::Arc; +use ethsync::{EthSync, SyncState}; use jsonrpc_core::*; use util::hash::*; use util::uint::*; @@ -24,18 +25,20 @@ use ethcore::client::*; use ethcore::views::*; use ethcore::blockchain::{BlockId, TransactionId}; 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. pub struct EthClient { client: Arc, + sync: Arc } impl EthClient { /// Creates new EthClient. - pub fn new(client: Arc) -> Self { + pub fn new(client: Arc, sync: Arc) -> Self { EthClient { - client: client + client: client, + sync: sync } } } @@ -49,10 +52,20 @@ impl Eth for EthClient { } } - // TODO: do no hardcode default sync status fn syncing(&self, params: Params) -> Result { 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(status.last_imported_block_number.unwrap_or(status.start_block_number)), + highest_block: U256::from(status.highest_block_number.unwrap_or(status.start_block_number)) + }) + }; + to_value(&res) + } _ => Err(Error::invalid_params()) } } diff --git a/rpc/src/v1/types/mod.rs b/rpc/src/v1/types/mod.rs index bdbd157ff..c4c6e8295 100644 --- a/rpc/src/v1/types/mod.rs +++ b/rpc/src/v1/types/mod.rs @@ -29,5 +29,5 @@ pub use self::bytes::Bytes; pub use self::filter::Filter; pub use self::index::Index; pub use self::optionals::OptionalValue; -pub use self::sync::SyncStatus; +pub use self::sync::{SyncStatus, SyncInfo}; pub use self::transaction::Transaction; diff --git a/rpc/src/v1/types/sync.rs b/rpc/src/v1/types/sync.rs index 595da6032..b5568acda 100644 --- a/rpc/src/v1/types/sync.rs +++ b/rpc/src/v1/types/sync.rs @@ -14,14 +14,55 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use util::hash::*; +use serde::{Serialize, Serializer}; +use util::uint::*; -#[derive(Default, Debug, Serialize)] -pub struct SyncStatus { +#[derive(Default, Debug, Serialize, PartialEq)] +pub struct SyncInfo { #[serde(rename="startingBlock")] - pub starting_block: H256, + pub starting_block: U256, #[serde(rename="currentBlock")] - pub current_block: H256, + pub current_block: U256, #[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(&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"}"#); + } } diff --git a/sync/src/lib.rs b/sync/src/lib.rs index b2d1fc29f..522062778 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -76,7 +76,7 @@ pub struct EthSync { sync: RwLock } -pub use self::chain::SyncStatus; +pub use self::chain::{SyncStatus, SyncState}; impl EthSync { /// Creates and register protocol with the network service @@ -132,4 +132,4 @@ impl NetworkProtocolHandler for EthSync { self.sync.write().unwrap().chain_blocks_verified(&mut NetSyncIo::new(io, self.chain.deref())); } } -} \ No newline at end of file +}