eth_syncing, fixed #397

This commit is contained in:
debris 2016-02-10 16:28:59 +01:00
parent 5347d4fe43
commit 9159d5812b
5 changed files with 69 additions and 15 deletions

View File

@ -95,7 +95,7 @@ fn setup_rpc_server(client: Arc<Client>, sync: Arc<EthSync>, 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);

View File

@ -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<Client>,
sync: Arc<EthSync>
}
impl EthClient {
/// Creates new EthClient.
pub fn new(client: Arc<Client>) -> Self {
pub fn new(client: Arc<Client>, sync: Arc<EthSync>) -> 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<Value, Error> {
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())
}
}

View File

@ -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;

View File

@ -14,14 +14,55 @@
// You should have received a copy of the GNU General Public License
// 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)]
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<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>
}
pub use self::chain::SyncStatus;
pub use self::chain::{SyncStatus, SyncState};
impl EthSync {
/// Creates and register protocol with the network service