Merge branch 'master' of github.com:ethcore/parity into jsonrpc2

This commit is contained in:
debris
2016-02-10 21:52:52 +01:00
12 changed files with 401 additions and 69 deletions

View File

@@ -26,6 +26,7 @@ use views::*;
use header::*;
use service::*;
use client::BlockStatus;
use util::panics::*;
/// Block queue status
#[derive(Debug)]
@@ -59,6 +60,7 @@ impl BlockQueueInfo {
/// A queue of blocks. Sits between network or other I/O and the BlockChain.
/// Sorts them ready for blockchain insertion.
pub struct BlockQueue {
panic_handler: Arc<PanicHandler>,
engine: Arc<Box<Engine>>,
more_to_verify: Arc<Condvar>,
verification: Arc<Mutex<Verification>>,
@@ -113,6 +115,7 @@ impl BlockQueue {
let ready_signal = Arc::new(QueueSignal { signalled: AtomicBool::new(false), message_channel: message_channel });
let deleting = Arc::new(AtomicBool::new(false));
let empty = Arc::new(Condvar::new());
let panic_handler = PanicHandler::new_in_arc();
let mut verifiers: Vec<JoinHandle<()>> = Vec::new();
let thread_count = max(::num_cpus::get(), 3) - 2;
@@ -123,11 +126,21 @@ impl BlockQueue {
let ready_signal = ready_signal.clone();
let empty = empty.clone();
let deleting = deleting.clone();
verifiers.push(thread::Builder::new().name(format!("Verifier #{}", i)).spawn(move || BlockQueue::verify(verification, engine, more_to_verify, ready_signal, deleting, empty))
.expect("Error starting block verification thread"));
let panic_handler = panic_handler.clone();
verifiers.push(
thread::Builder::new()
.name(format!("Verifier #{}", i))
.spawn(move || {
panic_handler.catch_panic(move || {
BlockQueue::verify(verification, engine, more_to_verify, ready_signal, deleting, empty)
}).unwrap()
})
.expect("Error starting block verification thread")
);
}
BlockQueue {
engine: engine,
panic_handler: panic_handler,
ready_signal: ready_signal.clone(),
more_to_verify: more_to_verify.clone(),
verification: verification.clone(),
@@ -150,7 +163,7 @@ impl BlockQueue {
while lock.unverified.is_empty() && !deleting.load(AtomicOrdering::Relaxed) {
lock = wait.wait(lock).unwrap();
}
if deleting.load(AtomicOrdering::Relaxed) {
return;
}
@@ -324,6 +337,12 @@ impl BlockQueue {
}
}
impl MayPanic for BlockQueue {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener {
self.panic_handler.on_panic(closure);
}
}
impl Drop for BlockQueue {
fn drop(&mut self) {
self.clear();

View File

@@ -859,7 +859,7 @@ mod tests {
let transactions = bc.transactions(&b1_hash).unwrap();
assert_eq!(transactions.len(), 7);
for t in transactions {
assert_eq!(bc.transaction(TransactionId::Hash(t.hash())).unwrap(), t);
assert_eq!(bc.transaction(&t.hash()).unwrap(), t);
}
}
}

View File

@@ -17,6 +17,7 @@
//! Blockchain database client.
use util::*;
use util::panics::*;
use rocksdb::{Options, DB, DBCompactionStyle};
use blockchain::{BlockChain, BlockProvider, CacheSize};
use views::BlockView;
@@ -167,7 +168,8 @@ pub struct Client {
state_db: Mutex<JournalDB>,
block_queue: RwLock<BlockQueue>,
report: RwLock<ClientReport>,
import_lock: Mutex<()>
import_lock: Mutex<()>,
panic_handler: Arc<PanicHandler>,
}
const HISTORY: u64 = 1000;
@@ -208,19 +210,25 @@ impl Client {
let mut state_path = path.to_path_buf();
state_path.push("state");
let db = Arc::new(DB::open(&opts, state_path.to_str().unwrap()).unwrap());
let engine = Arc::new(try!(spec.to_engine()));
let mut state_db = JournalDB::new_with_arc(db.clone());
if state_db.is_empty() && engine.spec().ensure_db_good(&mut state_db) {
state_db.commit(0, &engine.spec().genesis_header().hash(), None).expect("Error commiting genesis state to state DB");
}
let block_queue = BlockQueue::new(engine.clone(), message_channel);
let panic_handler = PanicHandler::new_in_arc();
panic_handler.forward_from(&block_queue);
Ok(Arc::new(Client {
chain: chain,
engine: engine.clone(),
engine: engine,
state_db: Mutex::new(state_db),
block_queue: RwLock::new(BlockQueue::new(engine, message_channel)),
block_queue: RwLock::new(block_queue),
report: RwLock::new(Default::default()),
import_lock: Mutex::new(()),
panic_handler: panic_handler
}))
}
@@ -435,3 +443,9 @@ impl BlockChainClient for Client {
}
}
}
impl MayPanic for Client {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener {
self.panic_handler.on_panic(closure);
}
}

View File

@@ -17,6 +17,7 @@
//! Creates and registers client and network services.
use util::*;
use util::panics::*;
use spec::Spec;
use error::*;
use std::env;
@@ -27,7 +28,7 @@ use client::Client;
pub enum SyncMessage {
/// New block has been imported into the blockchain
NewChainBlock(Bytes), //TODO: use Cow
/// A block is ready
/// A block is ready
BlockVerified,
}
@@ -38,17 +39,22 @@ pub type NetSyncMessage = NetworkIoMessage<SyncMessage>;
pub struct ClientService {
net_service: NetworkService<SyncMessage>,
client: Arc<Client>,
panic_handler: Arc<PanicHandler>
}
impl ClientService {
/// Start the service in a separate thread.
pub fn start(spec: Spec, net_config: NetworkConfiguration) -> Result<ClientService, Error> {
let panic_handler = PanicHandler::new_in_arc();
let mut net_service = try!(NetworkService::start(net_config));
panic_handler.forward_from(&net_service);
info!("Starting {}", net_service.host_info());
info!("Configured for {} using {} engine", spec.name, spec.engine_name);
let mut dir = env::home_dir().unwrap();
dir.push(".parity");
let client = try!(Client::new(spec, &dir, net_service.io().channel()));
panic_handler.forward_from(client.deref());
let client_io = Arc::new(ClientIoHandler {
client: client.clone()
});
@@ -57,6 +63,7 @@ impl ClientService {
Ok(ClientService {
net_service: net_service,
client: client,
panic_handler: panic_handler,
})
}
@@ -81,6 +88,12 @@ impl ClientService {
}
}
impl MayPanic for ClientService {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener {
self.panic_handler.on_panic(closure);
}
}
/// IO interface for the Client handler
struct ClientIoHandler {
client: Arc<Client>