extend sync status interface to sync provider

This commit is contained in:
Nikolay Volf 2016-03-10 20:32:17 +04:00
parent 8dd41bfe0c
commit 25a63611f8
4 changed files with 20 additions and 18 deletions

View File

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

View File

@ -17,7 +17,7 @@
//! Eth rpc implementation.
use std::collections::HashMap;
use std::sync::{Arc, Weak, Mutex, RwLock};
use ethsync::{SyncStatusProvider, SyncState};
use ethsync::{SyncProvider, SyncState};
use jsonrpc_core::*;
use util::numbers::*;
use util::sha3::*;
@ -32,13 +32,13 @@ use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncIn
use v1::helpers::{PollFilter, PollManager};
/// 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>,
sync: Weak<S>,
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.
pub fn new(client: &Arc<C>, sync: &Arc<S>) -> Self {
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> {
match params {
Params::None => to_value(&U256::from(take_weak!(self.sync).status().protocol_version)),

View File

@ -17,15 +17,15 @@
//! Net rpc implementation.
use std::sync::{Arc, Weak};
use jsonrpc_core::*;
use ethsync::SyncStatusProvider;
use ethsync::SyncProvider;
use v1::traits::Net;
/// Net rpc implementation.
pub struct NetClient<S> where S: SyncStatusProvider {
pub struct NetClient<S> where S: SyncProvider {
sync: Weak<S>
}
impl<S> NetClient<S> where S: SyncStatusProvider {
impl<S> NetClient<S> where S: SyncProvider {
/// Creates new NetClient.
pub fn new(sync: &Arc<S>) -> Self {
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> {
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
pub trait SyncStatusProvider: Send + Sync {
pub trait SyncProvider: Send + Sync {
/// Get sync status
fn status(&self) -> SyncStatus;
/// Insert transaction in the sync transaction queue
fn insert_transaction(&self, transaction: ethcore::transaction::SignedTransaction);
}
/// Ethereum network protocol handler
@ -130,9 +132,16 @@ impl EthSync {
pub fn restart(&mut self, io: &mut NetworkContext<SyncMessage>) {
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
pub fn insert_transaction(&self, transaction: ethcore::transaction::SignedTransaction) {
fn insert_transaction(&self, transaction: ethcore::transaction::SignedTransaction) {
use util::numbers::*;
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 {
fn initialize(&self, io: &NetworkContext<SyncMessage>) {
io.register_timer(0, 1000).expect("Error registering sync timer");