fix network_start regression (#1629)

* fix network_start regression

* network io handler

* move registration to the network start
This commit is contained in:
Nikolay Volf 2016-07-16 11:31:59 +02:00 committed by Gav Wood
parent 78007cf80b
commit 7b5d39e0a1
2 changed files with 29 additions and 18 deletions

View File

@ -35,7 +35,7 @@ use util::rlp::{RlpStream, Rlp, UntrustedRlp};
use util::journaldb;
use util::journaldb::JournalDB;
use util::kvdb::*;
use util::{Stream, View, PerfTimer, Itertools, Colour};
use util::{Stream, View, PerfTimer, Itertools};
use util::{Mutex, RwLock};
// other
@ -608,18 +608,6 @@ impl Client {
}
}
}
/// Notify us that the network has been started.
pub fn network_started(&self, url: &str) {
let mut previous_enode = self.previous_enode.lock();
if let Some(ref u) = *previous_enode {
if u == url {
return;
}
}
*previous_enode = Some(url.into());
info!(target: "mode", "Public node URL: {}", Colour::White.bold().paint(url));
}
}
#[derive(Ipc)]
@ -687,7 +675,7 @@ impl BlockChainClient for Client {
fn block(&self, id: BlockID) -> Option<Bytes> {
if let &BlockID::Pending = &id {
if let Some(block) = self.miner.pending_block() {
if let Some(block) = self.miner.pending_block() {
return Some(block.rlp_bytes(Seal::Without));
}
}
@ -708,7 +696,7 @@ impl BlockChainClient for Client {
if let &BlockID::Pending = &id {
if let Some(block) = self.miner.pending_block() {
return Some(*block.header.difficulty() + self.block_total_difficulty(BlockID::Latest).expect("blocks in chain have details; qed"));
}
}
}
Self::block_hash(&self.chain, id).and_then(|hash| self.chain.block_details(&hash)).map(|d| d.total_difficulty)
}

View File

@ -21,10 +21,25 @@ use network::error::NetworkError;
use network::host::{Host, NetworkContext, NetworkIoMessage, ProtocolId};
use network::stats::NetworkStats;
use io::*;
use std::sync::Arc;
use parking_lot::RwLock;
use std::sync::Arc;
use ansi_term::Colour;
struct HostHandler {
public_url: RwLock<Option<String>>
}
impl IoHandler<NetworkIoMessage> for HostHandler {
fn message(&self, _io: &IoContext<NetworkIoMessage>, message: &NetworkIoMessage) {
if let NetworkIoMessage::NetworkStarted(ref public_url) = *message {
let mut url = self.public_url.write();
if url.as_ref().map(|uref| uref != public_url).unwrap_or(true) {
info!(target: "network", "Public node URL: {}", Colour::White.bold().paint(public_url.as_ref()));
}
*url = Some(public_url.to_owned());
}
}
}
/// IO Service with networking
/// `Message` defines a notification data type.
@ -34,12 +49,14 @@ pub struct NetworkService {
host: RwLock<Option<Arc<Host>>>,
stats: Arc<NetworkStats>,
panic_handler: Arc<PanicHandler>,
host_handler: Arc<HostHandler>,
config: NetworkConfiguration,
}
impl NetworkService {
/// Starts IO event loop
pub fn new(config: NetworkConfiguration) -> Result<NetworkService, UtilError> {
let host_handler = Arc::new(HostHandler { public_url: RwLock::new(None) });
let panic_handler = PanicHandler::new_in_arc();
let io_service = try!(IoService::<NetworkIoMessage>::start());
panic_handler.forward_from(&io_service);
@ -53,6 +70,7 @@ impl NetworkService {
panic_handler: panic_handler,
host: RwLock::new(None),
config: config,
host_handler: host_handler,
})
}
@ -106,6 +124,11 @@ impl NetworkService {
try!(self.io_service.register_handler(h.clone()));
*host = Some(h);
}
if self.host_handler.public_url.read().is_none() {
try!(self.io_service.register_handler(self.host_handler.clone()));
}
Ok(())
}