Merge pull request #664 from ethcore/sync-provider

extend sync status interface to sync provider
This commit is contained in:
Nikolay Volf 2016-03-10 21:18:41 +03:00
commit a207b6a17e
4 changed files with 20 additions and 18 deletions

View File

@ -49,7 +49,7 @@ use ethcore::spec::*;
use ethcore::client::*; use ethcore::client::*;
use ethcore::service::{ClientService, NetSyncMessage}; use ethcore::service::{ClientService, NetSyncMessage};
use ethcore::ethereum; use ethcore::ethereum;
use ethsync::{EthSync, SyncConfig, SyncStatusProvider}; use ethsync::{EthSync, SyncConfig, SyncProvider};
use docopt::Docopt; use docopt::Docopt;
use daemonize::Daemonize; use daemonize::Daemonize;
use number_prefix::{binary_prefix, Standalone, Prefixed}; use number_prefix::{binary_prefix, Standalone, Prefixed};

View File

@ -17,7 +17,7 @@
//! Eth rpc implementation. //! Eth rpc implementation.
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, Weak, Mutex, RwLock}; use std::sync::{Arc, Weak, Mutex, RwLock};
use ethsync::{SyncStatusProvider, SyncState}; use ethsync::{SyncProvider, SyncState};
use jsonrpc_core::*; use jsonrpc_core::*;
use util::numbers::*; use util::numbers::*;
use util::sha3::*; use util::sha3::*;
@ -32,13 +32,13 @@ use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncIn
use v1::helpers::{PollFilter, PollManager}; use v1::helpers::{PollFilter, PollManager};
/// Eth rpc implementation. /// Eth rpc implementation.
pub struct EthClient<C, S> where C: BlockChainClient, S: SyncStatusProvider { pub struct EthClient<C, S> where C: BlockChainClient, S: SyncProvider {
client: Weak<C>, client: Weak<C>,
sync: Weak<S>, sync: Weak<S>,
hashrates: RwLock<HashMap<H256, u64>>, hashrates: RwLock<HashMap<H256, u64>>,
} }
impl<C, S> EthClient<C, S> where C: BlockChainClient, S: SyncStatusProvider { impl<C, S> EthClient<C, S> where C: BlockChainClient, S: SyncProvider {
/// Creates new EthClient. /// Creates new EthClient.
pub fn new(client: &Arc<C>, sync: &Arc<S>) -> Self { pub fn new(client: &Arc<C>, sync: &Arc<S>) -> Self {
EthClient { EthClient {
@ -94,7 +94,7 @@ impl<C, S> EthClient<C, S> where C: BlockChainClient, S: SyncStatusProvider {
} }
} }
impl<C, S> Eth for EthClient<C, S> where C: BlockChainClient + 'static, S: SyncStatusProvider + 'static { impl<C, S> Eth for EthClient<C, S> where C: BlockChainClient + 'static, S: SyncProvider + 'static {
fn protocol_version(&self, params: Params) -> Result<Value, Error> { fn protocol_version(&self, params: Params) -> Result<Value, Error> {
match params { match params {
Params::None => to_value(&U256::from(take_weak!(self.sync).status().protocol_version)), Params::None => to_value(&U256::from(take_weak!(self.sync).status().protocol_version)),

View File

@ -17,15 +17,15 @@
//! Net rpc implementation. //! Net rpc implementation.
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use jsonrpc_core::*; use jsonrpc_core::*;
use ethsync::SyncStatusProvider; use ethsync::SyncProvider;
use v1::traits::Net; use v1::traits::Net;
/// Net rpc implementation. /// Net rpc implementation.
pub struct NetClient<S> where S: SyncStatusProvider { pub struct NetClient<S> where S: SyncProvider {
sync: Weak<S> sync: Weak<S>
} }
impl<S> NetClient<S> where S: SyncStatusProvider { impl<S> NetClient<S> where S: SyncProvider {
/// Creates new NetClient. /// Creates new NetClient.
pub fn new(sync: &Arc<S>) -> Self { pub fn new(sync: &Arc<S>) -> Self {
NetClient { NetClient {
@ -34,7 +34,7 @@ impl<S> NetClient<S> where S: SyncStatusProvider {
} }
} }
impl<S> Net for NetClient<S> where S: SyncStatusProvider + 'static { impl<S> Net for NetClient<S> where S: SyncProvider + 'static {
fn version(&self, _: Params) -> Result<Value, Error> { fn version(&self, _: Params) -> Result<Value, Error> {
Ok(Value::U64(take_weak!(self.sync).status().protocol_version as u64)) Ok(Value::U64(take_weak!(self.sync).status().protocol_version as u64))
} }

View File

@ -95,9 +95,11 @@ impl Default for SyncConfig {
} }
/// Current sync status /// Current sync status
pub trait SyncStatusProvider: Send + Sync { pub trait SyncProvider: Send + Sync {
/// Get sync status /// Get sync status
fn status(&self) -> SyncStatus; fn status(&self) -> SyncStatus;
/// Insert transaction in the sync transaction queue
fn insert_transaction(&self, transaction: ethcore::transaction::SignedTransaction);
} }
/// Ethereum network protocol handler /// Ethereum network protocol handler
@ -130,9 +132,16 @@ impl EthSync {
pub fn restart(&mut self, io: &mut NetworkContext<SyncMessage>) { pub fn restart(&mut self, io: &mut NetworkContext<SyncMessage>) {
self.sync.write().unwrap().restart(&mut NetSyncIo::new(io, self.chain.deref())); self.sync.write().unwrap().restart(&mut NetSyncIo::new(io, self.chain.deref()));
} }
}
impl SyncProvider for EthSync {
/// Get sync status
fn status(&self) -> SyncStatus {
self.sync.read().unwrap().status()
}
/// Insert transaction in transaction queue /// Insert transaction in transaction queue
pub fn insert_transaction(&self, transaction: ethcore::transaction::SignedTransaction) { fn insert_transaction(&self, transaction: ethcore::transaction::SignedTransaction) {
use util::numbers::*; use util::numbers::*;
let nonce_fn = |a: &Address| self.chain.state().nonce(a) + U256::one(); let nonce_fn = |a: &Address| self.chain.state().nonce(a) + U256::one();
@ -141,13 +150,6 @@ impl EthSync {
} }
} }
impl SyncStatusProvider for EthSync {
/// Get sync status
fn status(&self) -> SyncStatus {
self.sync.read().unwrap().status()
}
}
impl NetworkProtocolHandler<SyncMessage> for EthSync { impl NetworkProtocolHandler<SyncMessage> for EthSync {
fn initialize(&self, io: &NetworkContext<SyncMessage>) { fn initialize(&self, io: &NetworkContext<SyncMessage>) {
io.register_timer(0, 1000).expect("Error registering sync timer"); io.register_timer(0, 1000).expect("Error registering sync timer");