From 456ad9e21b8e57fa4e39e982d0fde570679fe25f Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 6 Jul 2016 19:52:34 +0200 Subject: [PATCH 01/16] Remove .lock().unwrap() idiom into locked(). --- dapps/src/lib.rs | 4 +- ethcore/src/block_queue.rs | 56 +++++++-------- ethcore/src/client/client.rs | 22 +++--- ethcore/src/miner/miner.rs | 82 +++++++++++----------- ethcore/src/miner/mod.rs | 2 +- ethcore/src/miner/work_notify.rs | 4 +- parity/main.rs | 4 +- rpc/rpctest/src/main.rs | 3 +- rpc/src/v1/helpers/signing_queue.rs | 16 ++--- rpc/src/v1/impls/eth.rs | 3 +- rpc/src/v1/impls/eth_filter.rs | 13 ++-- rpc/src/v1/tests/helpers/miner_service.rs | 20 +++--- rpc/src/v1/tests/mocked/eth.rs | 3 +- rpc/src/v1/tests/mocked/personal_signer.rs | 5 +- util/src/io/worker.rs | 3 +- util/src/misc.rs | 10 +++ util/src/network/host.rs | 66 ++++++++--------- util/src/network/tests.rs | 4 +- util/src/panics.rs | 5 +- 19 files changed, 172 insertions(+), 153 deletions(-) diff --git a/dapps/src/lib.rs b/dapps/src/lib.rs index 1c550fb07..6952d509c 100644 --- a/dapps/src/lib.rs +++ b/dapps/src/lib.rs @@ -53,6 +53,7 @@ extern crate jsonrpc_core; extern crate jsonrpc_http_server; extern crate parity_dapps; extern crate ethcore_rpc; +extern crate ethcore_util; extern crate mime_guess; mod endpoint; @@ -67,6 +68,7 @@ mod proxypac; use std::sync::{Arc, Mutex}; use std::net::SocketAddr; use std::collections::HashMap; +use ethcore_util::misc::Lockable; use jsonrpc_core::{IoHandler, IoDelegate}; use router::auth::{Authorization, NoAuth, HttpBasicAuth}; use ethcore_rpc::Extendable; @@ -149,7 +151,7 @@ impl Server { /// Set callback for panics. pub fn set_panic_handler(&self, handler: F) where F : Fn() -> () + Send + 'static { - *self.panic_handler.lock().unwrap() = Some(Box::new(handler)); + *self.panic_handler.locked() = Some(Box::new(handler)); } } diff --git a/ethcore/src/block_queue.rs b/ethcore/src/block_queue.rs index ce99dcccd..3ad329af5 100644 --- a/ethcore/src/block_queue.rs +++ b/ethcore/src/block_queue.rs @@ -207,9 +207,9 @@ impl BlockQueue { fn verify(verification: Arc, engine: Arc>, wait: Arc, ready: Arc, deleting: Arc, empty: Arc) { while !deleting.load(AtomicOrdering::Acquire) { { - let mut unverified = verification.unverified.lock().unwrap(); + let mut unverified = verification.unverified.locked(); - if unverified.is_empty() && verification.verifying.lock().unwrap().is_empty() { + if unverified.is_empty() && verification.verifying.locked().is_empty() { empty.notify_all(); } @@ -223,11 +223,11 @@ impl BlockQueue { } let block = { - let mut unverified = verification.unverified.lock().unwrap(); + let mut unverified = verification.unverified.locked(); if unverified.is_empty() { continue; } - let mut verifying = verification.verifying.lock().unwrap(); + let mut verifying = verification.verifying.locked(); let block = unverified.pop_front().unwrap(); verifying.push_back(VerifyingBlock{ hash: block.header.hash(), block: None }); block @@ -236,7 +236,7 @@ impl BlockQueue { let block_hash = block.header.hash(); match verify_block_unordered(block.header, block.bytes, engine.deref().deref()) { Ok(verified) => { - let mut verifying = verification.verifying.lock().unwrap(); + let mut verifying = verification.verifying.locked(); for e in verifying.iter_mut() { if e.hash == block_hash { e.block = Some(verified); @@ -245,16 +245,16 @@ impl BlockQueue { } if !verifying.is_empty() && verifying.front().unwrap().hash == block_hash { // we're next! - let mut verified = verification.verified.lock().unwrap(); - let mut bad = verification.bad.lock().unwrap(); + let mut verified = verification.verified.locked(); + let mut bad = verification.bad.locked(); BlockQueue::drain_verifying(&mut verifying, &mut verified, &mut bad); ready.set(); } }, Err(err) => { - let mut verifying = verification.verifying.lock().unwrap(); - let mut verified = verification.verified.lock().unwrap(); - let mut bad = verification.bad.lock().unwrap(); + let mut verifying = verification.verifying.locked(); + let mut verified = verification.verified.locked(); + let mut bad = verification.bad.locked(); warn!(target: "client", "Stage 2 block verification failed for {}\nError: {:?}", block_hash, err); bad.insert(block_hash.clone()); verifying.retain(|e| e.hash != block_hash); @@ -279,9 +279,9 @@ impl BlockQueue { /// Clear the queue and stop verification activity. pub fn clear(&self) { - let mut unverified = self.verification.unverified.lock().unwrap(); - let mut verifying = self.verification.verifying.lock().unwrap(); - let mut verified = self.verification.verified.lock().unwrap(); + let mut unverified = self.verification.unverified.locked(); + let mut verifying = self.verification.verifying.locked(); + let mut verified = self.verification.verified.locked(); unverified.clear(); verifying.clear(); verified.clear(); @@ -290,8 +290,8 @@ impl BlockQueue { /// Wait for unverified queue to be empty pub fn flush(&self) { - let mut unverified = self.verification.unverified.lock().unwrap(); - while !unverified.is_empty() || !self.verification.verifying.lock().unwrap().is_empty() { + let mut unverified = self.verification.unverified.locked(); + while !unverified.is_empty() || !self.verification.verifying.locked().is_empty() { unverified = self.empty.wait(unverified).unwrap(); } } @@ -301,7 +301,7 @@ impl BlockQueue { if self.processing.read().unwrap().contains(&hash) { return BlockStatus::Queued; } - if self.verification.bad.lock().unwrap().contains(&hash) { + if self.verification.bad.locked().contains(&hash) { return BlockStatus::Bad; } BlockStatus::Unknown @@ -316,7 +316,7 @@ impl BlockQueue { return Err(ImportError::AlreadyQueued.into()); } - let mut bad = self.verification.bad.lock().unwrap(); + let mut bad = self.verification.bad.locked(); if bad.contains(&h) { return Err(ImportError::KnownBad.into()); } @@ -330,13 +330,13 @@ impl BlockQueue { match verify_block_basic(&header, &bytes, self.engine.deref().deref()) { Ok(()) => { self.processing.write().unwrap().insert(h.clone()); - self.verification.unverified.lock().unwrap().push_back(UnverifiedBlock { header: header, bytes: bytes }); + self.verification.unverified.locked().push_back(UnverifiedBlock { header: header, bytes: bytes }); self.more_to_verify.notify_all(); Ok(h) }, Err(err) => { warn!(target: "client", "Stage 1 block verification failed for {}\nError: {:?}", BlockView::new(&bytes).header_view().sha3(), err); - self.verification.bad.lock().unwrap().insert(h.clone()); + self.verification.bad.locked().insert(h.clone()); Err(err) } } @@ -347,9 +347,9 @@ impl BlockQueue { if block_hashes.is_empty() { return; } - let mut verified_lock = self.verification.verified.lock().unwrap(); + let mut verified_lock = self.verification.verified.locked(); let mut verified = verified_lock.deref_mut(); - let mut bad = self.verification.bad.lock().unwrap(); + let mut bad = self.verification.bad.locked(); let mut processing = self.processing.write().unwrap(); bad.reserve(block_hashes.len()); for hash in block_hashes { @@ -382,7 +382,7 @@ impl BlockQueue { /// Removes up to `max` verified blocks from the queue pub fn drain(&self, max: usize) -> Vec { - let mut verified = self.verification.verified.lock().unwrap(); + let mut verified = self.verification.verified.locked(); let count = min(max, verified.len()); let mut result = Vec::with_capacity(count); for _ in 0..count { @@ -399,15 +399,15 @@ impl BlockQueue { /// Get queue status. pub fn queue_info(&self) -> BlockQueueInfo { let (unverified_len, unverified_bytes) = { - let v = self.verification.unverified.lock().unwrap(); + let v = self.verification.unverified.locked(); (v.len(), v.heap_size_of_children()) }; let (verifying_len, verifying_bytes) = { - let v = self.verification.verifying.lock().unwrap(); + let v = self.verification.verifying.locked(); (v.len(), v.heap_size_of_children()) }; let (verified_len, verified_bytes) = { - let v = self.verification.verified.lock().unwrap(); + let v = self.verification.verified.locked(); (v.len(), v.heap_size_of_children()) }; BlockQueueInfo { @@ -428,9 +428,9 @@ impl BlockQueue { /// Optimise memory footprint of the heap fields. pub fn collect_garbage(&self) { { - self.verification.unverified.lock().unwrap().shrink_to_fit(); - self.verification.verifying.lock().unwrap().shrink_to_fit(); - self.verification.verified.lock().unwrap().shrink_to_fit(); + self.verification.unverified.locked().shrink_to_fit(); + self.verification.verifying.locked().shrink_to_fit(); + self.verification.verified.locked().shrink_to_fit(); } self.processing.write().unwrap().shrink_to_fit(); } diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index fffef2bb2..a2796a09e 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -258,7 +258,7 @@ impl Client { // Enact Verified Block let parent = chain_has_parent.unwrap(); let last_hashes = self.build_last_hashes(header.parent_hash.clone()); - let db = self.state_db.lock().unwrap().boxed_clone(); + let db = self.state_db.locked().boxed_clone(); let enact_result = enact_verified(&block, engine, self.tracedb.tracing_enabled(), db, &parent, last_hashes, &self.vm_factory, self.trie_factory.clone()); if let Err(e) = enact_result { @@ -432,7 +432,7 @@ impl Client { }; self.block_header(id).and_then(|header| { - let db = self.state_db.lock().unwrap().boxed_clone(); + let db = self.state_db.locked().boxed_clone(); // early exit for pruned blocks if db.is_pruned() && self.chain.best_block_number() >= block_number + HISTORY { @@ -448,7 +448,7 @@ impl Client { /// Get a copy of the best block's state. pub fn state(&self) -> State { State::from_existing( - self.state_db.lock().unwrap().boxed_clone(), + self.state_db.locked().boxed_clone(), HeaderView::new(&self.best_block_header()).state_root(), self.engine.account_start_nonce(), self.trie_factory.clone()) @@ -463,7 +463,7 @@ impl Client { /// Get the report. pub fn report(&self) -> ClientReport { let mut report = self.report.read().unwrap().clone(); - report.state_db_mem = self.state_db.lock().unwrap().mem_used(); + report.state_db_mem = self.state_db.locked().mem_used(); report } @@ -475,7 +475,7 @@ impl Client { match self.mode { Mode::Dark(timeout) => { - let mut ss = self.sleep_state.lock().unwrap(); + let mut ss = self.sleep_state.locked(); if let Some(t) = ss.last_activity { if Instant::now() > t + timeout { self.sleep(); @@ -484,7 +484,7 @@ impl Client { } } Mode::Passive(timeout, wakeup_after) => { - let mut ss = self.sleep_state.lock().unwrap(); + let mut ss = self.sleep_state.locked(); let now = Instant::now(); if let Some(t) = ss.last_activity { if now > t + timeout { @@ -557,14 +557,14 @@ impl Client { } else { trace!(target: "mode", "sleep: Cannot sleep - syncing ongoing."); // TODO: Consider uncommenting. - //*self.last_activity.lock().unwrap() = Some(Instant::now()); + //*self.last_activity.locked() = Some(Instant::now()); } } } /// Notify us that the network has been started. pub fn network_started(&self, url: &String) { - let mut previous_enode = self.previous_enode.lock().unwrap(); + let mut previous_enode = self.previous_enode.locked(); if let Some(ref u) = *previous_enode { if u == url { return; @@ -616,7 +616,7 @@ impl BlockChainClient for Client { fn keep_alive(&self) { if self.mode != Mode::Active { self.wake_up(); - (*self.sleep_state.lock().unwrap()).last_activity = Some(Instant::now()); + (*self.sleep_state.locked()).last_activity = Some(Instant::now()); } } @@ -740,7 +740,7 @@ impl BlockChainClient for Client { } fn state_data(&self, hash: &H256) -> Option { - self.state_db.lock().unwrap().state(hash) + self.state_db.locked().state(hash) } fn block_receipts(&self, hash: &H256) -> Option { @@ -902,7 +902,7 @@ impl MiningBlockChainClient for Client { &self.vm_factory, self.trie_factory.clone(), false, // TODO: this will need to be parameterised once we want to do immediate mining insertion. - self.state_db.lock().unwrap().boxed_clone(), + self.state_db.locked().boxed_clone(), &self.chain.block_header(&h).expect("h is best block hash: so it's header must exist: qed"), self.build_last_hashes(h.clone()), author, diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 0ee56dccb..c9a20e7f1 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -161,8 +161,8 @@ impl Miner { trace!(target: "miner", "prepare_sealing: entering"); let (transactions, mut open_block, original_work_hash) = { - let transactions = {self.transaction_queue.lock().unwrap().top_transactions()}; - let mut sealing_work = self.sealing_work.lock().unwrap(); + let transactions = {self.transaction_queue.locked().top_transactions()}; + let mut sealing_work = self.sealing_work.locked(); let last_work_hash = sealing_work.peek_last_ref().map(|pb| pb.block().fields().header.hash()); let best_hash = chain.best_block_header().sha3(); /* @@ -232,7 +232,7 @@ impl Miner { }; { - let mut queue = self.transaction_queue.lock().unwrap(); + let mut queue = self.transaction_queue.locked(); for hash in invalid_transactions.into_iter() { queue.remove_invalid(&hash, &fetch_account); } @@ -263,7 +263,7 @@ impl Miner { } let (work, is_new) = { - let mut sealing_work = self.sealing_work.lock().unwrap(); + let mut sealing_work = self.sealing_work.locked(); let last_work_hash = sealing_work.peek_last_ref().map(|pb| pb.block().fields().header.hash()); trace!(target: "miner", "Checking whether we need to reseal: orig={:?} last={:?}, this={:?}", original_work_hash, last_work_hash, block.block().fields().header.hash()); let (work, is_new) = if last_work_hash.map_or(true, |h| h != block.block().fields().header.hash()) { @@ -291,20 +291,20 @@ impl Miner { fn update_gas_limit(&self, chain: &MiningBlockChainClient) { let gas_limit = HeaderView::new(&chain.best_block_header()).gas_limit(); - let mut queue = self.transaction_queue.lock().unwrap(); + let mut queue = self.transaction_queue.locked(); queue.set_gas_limit(gas_limit); } /// Returns true if we had to prepare new pending block fn enable_and_prepare_sealing(&self, chain: &MiningBlockChainClient) -> bool { trace!(target: "miner", "enable_and_prepare_sealing: entering"); - let have_work = self.sealing_work.lock().unwrap().peek_last_ref().is_some(); + let have_work = self.sealing_work.locked().peek_last_ref().is_some(); trace!(target: "miner", "enable_and_prepare_sealing: have_work={}", have_work); if !have_work { self.sealing_enabled.store(true, atomic::Ordering::Relaxed); self.prepare_sealing(chain); } - let mut sealing_block_last_request = self.sealing_block_last_request.lock().unwrap(); + let mut sealing_block_last_request = self.sealing_block_last_request.locked(); let best_number = chain.chain_info().best_block_number; if *sealing_block_last_request != best_number { trace!(target: "miner", "enable_and_prepare_sealing: Miner received request (was {}, now {}) - waking up.", *sealing_block_last_request, best_number); @@ -329,7 +329,7 @@ impl Miner { } /// Are we allowed to do a non-mandatory reseal? - fn tx_reseal_allowed(&self) -> bool { Instant::now() > *self.next_allowed_reseal.lock().unwrap() } + fn tx_reseal_allowed(&self) -> bool { Instant::now() > *self.next_allowed_reseal.locked() } } const SEALING_TIMEOUT_IN_BLOCKS : u64 = 5; @@ -337,13 +337,13 @@ const SEALING_TIMEOUT_IN_BLOCKS : u64 = 5; impl MinerService for Miner { fn clear_and_reset(&self, chain: &MiningBlockChainClient) { - self.transaction_queue.lock().unwrap().clear(); + self.transaction_queue.locked().clear(); self.update_sealing(chain); } fn status(&self) -> MinerStatus { - let status = self.transaction_queue.lock().unwrap().status(); - let sealing_work = self.sealing_work.lock().unwrap(); + let status = self.transaction_queue.locked().status(); + let sealing_work = self.sealing_work.locked(); MinerStatus { transactions_in_pending_queue: status.pending, transactions_in_future_queue: status.future, @@ -352,7 +352,7 @@ impl MinerService for Miner { } fn call(&self, chain: &MiningBlockChainClient, t: &SignedTransaction, analytics: CallAnalytics) -> Result { - let sealing_work = self.sealing_work.lock().unwrap(); + let sealing_work = self.sealing_work.locked(); match sealing_work.peek_last_ref() { Some(work) => { let block = work.block(); @@ -399,7 +399,7 @@ impl MinerService for Miner { } fn balance(&self, chain: &MiningBlockChainClient, address: &Address) -> U256 { - let sealing_work = self.sealing_work.lock().unwrap(); + let sealing_work = self.sealing_work.locked(); sealing_work.peek_last_ref().map_or_else( || chain.latest_balance(address), |b| b.block().fields().state.balance(address) @@ -407,7 +407,7 @@ impl MinerService for Miner { } fn storage_at(&self, chain: &MiningBlockChainClient, address: &Address, position: &H256) -> H256 { - let sealing_work = self.sealing_work.lock().unwrap(); + let sealing_work = self.sealing_work.locked(); sealing_work.peek_last_ref().map_or_else( || chain.latest_storage_at(address, position), |b| b.block().fields().state.storage_at(address, position) @@ -415,12 +415,12 @@ impl MinerService for Miner { } fn nonce(&self, chain: &MiningBlockChainClient, address: &Address) -> U256 { - let sealing_work = self.sealing_work.lock().unwrap(); + let sealing_work = self.sealing_work.locked(); sealing_work.peek_last_ref().map_or_else(|| chain.latest_nonce(address), |b| b.block().fields().state.nonce(address)) } fn code(&self, chain: &MiningBlockChainClient, address: &Address) -> Option { - let sealing_work = self.sealing_work.lock().unwrap(); + let sealing_work = self.sealing_work.locked(); sealing_work.peek_last_ref().map_or_else(|| chain.code(address), |b| b.block().fields().state.code(address)) } @@ -442,16 +442,16 @@ impl MinerService for Miner { } fn set_minimal_gas_price(&self, min_gas_price: U256) { - self.transaction_queue.lock().unwrap().set_minimal_gas_price(min_gas_price); + self.transaction_queue.locked().set_minimal_gas_price(min_gas_price); } fn minimal_gas_price(&self) -> U256 { - *self.transaction_queue.lock().unwrap().minimal_gas_price() + *self.transaction_queue.locked().minimal_gas_price() } fn sensible_gas_price(&self) -> U256 { // 10% above our minimum. - *self.transaction_queue.lock().unwrap().minimal_gas_price() * 110.into() / 100.into() + *self.transaction_queue.locked().minimal_gas_price() * 110.into() / 100.into() } fn sensible_gas_limit(&self) -> U256 { @@ -459,15 +459,15 @@ impl MinerService for Miner { } fn transactions_limit(&self) -> usize { - self.transaction_queue.lock().unwrap().limit() + self.transaction_queue.locked().limit() } fn set_transactions_limit(&self, limit: usize) { - self.transaction_queue.lock().unwrap().set_limit(limit) + self.transaction_queue.locked().set_limit(limit) } fn set_tx_gas_limit(&self, limit: U256) { - self.transaction_queue.lock().unwrap().set_tx_gas_limit(limit) + self.transaction_queue.locked().set_tx_gas_limit(limit) } /// Get the author that we will seal blocks as. @@ -493,7 +493,7 @@ impl MinerService for Miner { fn import_external_transactions(&self, chain: &MiningBlockChainClient, transactions: Vec) -> Vec> { - let mut transaction_queue = self.transaction_queue.lock().unwrap(); + let mut transaction_queue = self.transaction_queue.locked(); let results = self.add_transactions_to_queue(chain, transactions, TransactionOrigin::External, &mut transaction_queue); @@ -514,7 +514,7 @@ impl MinerService for Miner { let imported = { // Be sure to release the lock before we call enable_and_prepare_sealing - let mut transaction_queue = self.transaction_queue.lock().unwrap(); + let mut transaction_queue = self.transaction_queue.locked(); let import = self.add_transactions_to_queue(chain, vec![transaction], TransactionOrigin::Local, &mut transaction_queue).pop().unwrap(); match import { @@ -546,13 +546,13 @@ impl MinerService for Miner { } fn all_transactions(&self) -> Vec { - let queue = self.transaction_queue.lock().unwrap(); + let queue = self.transaction_queue.locked(); queue.top_transactions() } fn pending_transactions(&self) -> Vec { - let queue = self.transaction_queue.lock().unwrap(); - let sw = self.sealing_work.lock().unwrap(); + let queue = self.transaction_queue.locked(); + let sw = self.sealing_work.locked(); // TODO: should only use the sealing_work when it's current (it could be an old block) let sealing_set = match self.sealing_enabled.load(atomic::Ordering::Relaxed) { true => sw.peek_last_ref(), @@ -565,8 +565,8 @@ impl MinerService for Miner { } fn pending_transactions_hashes(&self) -> Vec { - let queue = self.transaction_queue.lock().unwrap(); - let sw = self.sealing_work.lock().unwrap(); + let queue = self.transaction_queue.locked(); + let sw = self.sealing_work.locked(); let sealing_set = match self.sealing_enabled.load(atomic::Ordering::Relaxed) { true => sw.peek_last_ref(), false => None, @@ -578,8 +578,8 @@ impl MinerService for Miner { } fn transaction(&self, hash: &H256) -> Option { - let queue = self.transaction_queue.lock().unwrap(); - let sw = self.sealing_work.lock().unwrap(); + let queue = self.transaction_queue.locked(); + let sw = self.sealing_work.locked(); let sealing_set = match self.sealing_enabled.load(atomic::Ordering::Relaxed) { true => sw.peek_last_ref(), false => None, @@ -591,7 +591,7 @@ impl MinerService for Miner { } fn pending_receipts(&self) -> BTreeMap { - match (self.sealing_enabled.load(atomic::Ordering::Relaxed), self.sealing_work.lock().unwrap().peek_last_ref()) { + match (self.sealing_enabled.load(atomic::Ordering::Relaxed), self.sealing_work.locked().peek_last_ref()) { (true, Some(pending)) => { let hashes = pending.transactions() .iter() @@ -606,14 +606,14 @@ impl MinerService for Miner { } fn last_nonce(&self, address: &Address) -> Option { - self.transaction_queue.lock().unwrap().last_nonce(address) + self.transaction_queue.locked().last_nonce(address) } fn update_sealing(&self, chain: &MiningBlockChainClient) { if self.sealing_enabled.load(atomic::Ordering::Relaxed) { let current_no = chain.chain_info().best_block_number; - let has_local_transactions = self.transaction_queue.lock().unwrap().has_local_pending_transactions(); - let last_request = *self.sealing_block_last_request.lock().unwrap(); + let has_local_transactions = self.transaction_queue.locked().has_local_pending_transactions(); + let last_request = *self.sealing_block_last_request.locked(); let should_disable_sealing = !self.forced_sealing() && !has_local_transactions && current_no > last_request @@ -622,9 +622,9 @@ impl MinerService for Miner { if should_disable_sealing { trace!(target: "miner", "Miner sleeping (current {}, last {})", current_no, last_request); self.sealing_enabled.store(false, atomic::Ordering::Relaxed); - self.sealing_work.lock().unwrap().reset(); + self.sealing_work.locked().reset(); } else { - *self.next_allowed_reseal.lock().unwrap() = Instant::now() + self.options.reseal_min_period; + *self.next_allowed_reseal.locked() = Instant::now() + self.options.reseal_min_period; self.prepare_sealing(chain); } } @@ -634,14 +634,14 @@ impl MinerService for Miner { trace!(target: "miner", "map_sealing_work: entering"); self.enable_and_prepare_sealing(chain); trace!(target: "miner", "map_sealing_work: sealing prepared"); - let mut sealing_work = self.sealing_work.lock().unwrap(); + let mut sealing_work = self.sealing_work.locked(); let ret = sealing_work.use_last_ref(); trace!(target: "miner", "map_sealing_work: leaving use_last_ref={:?}", ret.as_ref().map(|b| b.block().fields().header.hash())); ret.map(f) } fn submit_seal(&self, chain: &MiningBlockChainClient, pow_hash: H256, seal: Vec) -> Result<(), Error> { - let result = if let Some(b) = self.sealing_work.lock().unwrap().get_used_if(if self.options.enable_resubmission { GetAction::Clone } else { GetAction::Take }, |b| &b.hash() == &pow_hash) { + let result = if let Some(b) = self.sealing_work.locked().get_used_if(if self.options.enable_resubmission { GetAction::Clone } else { GetAction::Take }, |b| &b.hash() == &pow_hash) { b.lock().try_seal(self.engine(), seal).or_else(|_| { warn!(target: "miner", "Mined solution rejected: Invalid."); Err(Error::PowInvalid) @@ -688,7 +688,7 @@ impl MinerService for Miner { .par_iter() .map(|h| fetch_transactions(chain, h)); out_of_chain.for_each(|txs| { - let mut transaction_queue = self.transaction_queue.lock().unwrap(); + let mut transaction_queue = self.transaction_queue.locked(); let _ = self.add_transactions_to_queue( chain, txs, TransactionOrigin::External, &mut transaction_queue ); @@ -702,7 +702,7 @@ impl MinerService for Miner { .map(|h: &H256| fetch_transactions(chain, h)); in_chain.for_each(|mut txs| { - let mut transaction_queue = self.transaction_queue.lock().unwrap(); + let mut transaction_queue = self.transaction_queue.locked(); let to_remove = txs.drain(..) .map(|tx| { diff --git a/ethcore/src/miner/mod.rs b/ethcore/src/miner/mod.rs index 59acaebd9..06a2ccf2e 100644 --- a/ethcore/src/miner/mod.rs +++ b/ethcore/src/miner/mod.rs @@ -38,7 +38,7 @@ //! assert_eq!(miner.status().transactions_in_pending_queue, 0); //! //! // Check block for sealing -//! //assert!(miner.sealing_block(client.deref()).lock().unwrap().is_some()); +//! //assert!(miner.sealing_block(client.deref()).locked().is_some()); //! } //! ``` diff --git a/ethcore/src/miner/work_notify.rs b/ethcore/src/miner/work_notify.rs index ea2f6140e..a1b6f0a8b 100644 --- a/ethcore/src/miner/work_notify.rs +++ b/ethcore/src/miner/work_notify.rs @@ -61,13 +61,13 @@ impl WorkPoster { pub fn notify(&self, pow_hash: H256, difficulty: U256, number: u64) { // TODO: move this to engine let target = Ethash::difficulty_to_boundary(&difficulty); - let seed_hash = &self.seed_compute.lock().unwrap().get_seedhash(number); + let seed_hash = &self.seed_compute.locked().get_seedhash(number); let seed_hash = H256::from_slice(&seed_hash[..]); let body = format!( r#"{{ "result": ["0x{}","0x{}","0x{}","0x{:x}"] }}"#, pow_hash.hex(), seed_hash.hex(), target.hex(), number ); - let mut client = self.client.lock().unwrap(); + let mut client = self.client.locked(); for u in &self.urls { if let Err(e) = client.request(u.clone(), PostHandler { body: body.clone() }) { warn!("Error sending HTTP notification to {} : {}, retrying", u, e); diff --git a/parity/main.rs b/parity/main.rs index f54c39ab5..495d2a3ad 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -80,7 +80,7 @@ use std::thread::sleep; use std::time::Duration; use rustc_serialize::hex::FromHex; use ctrlc::CtrlC; -use util::{H256, ToPretty, NetworkConfiguration, PayloadInfo, Bytes, UtilError, Colour, Applyable, version, journaldb}; +use util::{Lockable, H256, ToPretty, NetworkConfiguration, PayloadInfo, Bytes, UtilError, Colour, Applyable, version, journaldb}; use util::panics::{MayPanic, ForwardPanic, PanicHandler}; use ethcore::client::{Mode, BlockID, BlockChainClient, ClientConfig, get_db_path, BlockImportError}; use ethcore::error::{ImportError}; @@ -614,7 +614,7 @@ fn wait_for_exit( // Wait for signal let mutex = Mutex::new(()); - let _ = exit.wait(mutex.lock().unwrap()).unwrap(); + let _ = exit.wait(mutex.locked()).unwrap(); info!("Finishing work, please wait..."); } diff --git a/rpc/rpctest/src/main.rs b/rpc/rpctest/src/main.rs index 139514cb1..ed812cd79 100644 --- a/rpc/rpctest/src/main.rs +++ b/rpc/rpctest/src/main.rs @@ -41,6 +41,7 @@ use rpc::v1::tests::helpers::{TestSyncProvider, Config as SyncConfig, TestMinerS use rpc::v1::{Eth, EthClient, EthFilter, EthFilterClient}; use util::panics::MayPanic; use util::hash::Address; +use util::Lockable; const USAGE: &'static str = r#" Parity rpctest client. @@ -137,7 +138,7 @@ impl Configuration { panic_handler.on_panic(move |_reason| { e.notify_all(); }); let mutex = Mutex::new(()); - let _ = exit.wait(mutex.lock().unwrap()).unwrap(); + let _ = exit.wait(mutex.locked()).unwrap(); } } diff --git a/rpc/src/v1/helpers/signing_queue.rs b/rpc/src/v1/helpers/signing_queue.rs index 756718000..c21a056d8 100644 --- a/rpc/src/v1/helpers/signing_queue.rs +++ b/rpc/src/v1/helpers/signing_queue.rs @@ -19,7 +19,7 @@ use std::time::{Instant, Duration}; use std::sync::{mpsc, Mutex, RwLock, Arc}; use std::collections::HashMap; use jsonrpc_core; -use util::U256; +use util::{U256, Lockable}; use v1::helpers::{TransactionRequest, TransactionConfirmation}; /// Result that can be returned from JSON RPC. @@ -110,7 +110,7 @@ pub struct ConfirmationPromise { impl ConfirmationToken { /// Submit solution to all listeners fn resolve(&self, result: Option) { - let mut res = self.result.lock().unwrap(); + let mut res = self.result.locked(); *res = result.map_or(ConfirmationResult::Rejected, |h| ConfirmationResult::Confirmed(h)); // Notify listener self.handle.unpark(); @@ -142,7 +142,7 @@ impl ConfirmationPromise { // Park thread (may wake up spuriously) thread::park_timeout(deadline - now); // Take confirmation result - let res = self.result.lock().unwrap(); + let res = self.result.locked(); // Check the result match *res { ConfirmationResult::Rejected => return None, @@ -183,7 +183,7 @@ impl ConfirmationsQueue { /// This method can be used only once (only single consumer of events can exist). pub fn start_listening(&self, listener: F) -> Result<(), QueueError> where F: Fn(QueueEvent) -> () { - let recv = self.receiver.lock().unwrap().take(); + let recv = self.receiver.locked().take(); if let None = recv { return Err(QueueError::AlreadyUsed); } @@ -208,7 +208,7 @@ impl ConfirmationsQueue { /// Notifies receiver about the event happening in this queue. fn notify(&self, message: QueueEvent) { // We don't really care about the result - let _ = self.sender.lock().unwrap().send(message); + let _ = self.sender.locked().send(message); } /// Removes transaction from this queue and notifies `ConfirmationPromise` holders about the result. @@ -241,7 +241,7 @@ impl SigningQueue for ConfirmationsQueue { fn add_request(&self, transaction: TransactionRequest) -> ConfirmationPromise { // Increment id let id = { - let mut last_id = self.id.lock().unwrap(); + let mut last_id = self.id.locked(); *last_id = *last_id + U256::from(1); *last_id }; @@ -354,7 +354,7 @@ mod test { let r = received.clone(); let handle = thread::spawn(move || { q.start_listening(move |notification| { - let mut v = r.lock().unwrap(); + let mut v = r.locked(); *v = Some(notification); }).expect("Should be closed nicely.") }); @@ -363,7 +363,7 @@ mod test { // then handle.join().expect("Thread should finish nicely"); - let r = received.lock().unwrap().take(); + let r = received.locked().take(); assert_eq!(r, Some(QueueEvent::NewRequest(U256::from(1)))); } diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 13f54feea..180d93769 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -28,6 +28,7 @@ use jsonrpc_core::*; use util::numbers::*; use util::sha3::*; use util::rlp::{encode, decode, UntrustedRlp, View}; +use util::Lockable; use ethcore::account_provider::AccountProvider; use ethcore::client::{MiningBlockChainClient, BlockID, TransactionID, UncleID}; use ethcore::block::IsBlock; @@ -561,7 +562,7 @@ impl Eth for EthClient where miner.map_sealing_work(client.deref(), |b| { let pow_hash = b.hash(); let target = Ethash::difficulty_to_boundary(b.block().header().difficulty()); - let seed_hash = self.seed_compute.lock().unwrap().get_seedhash(b.block().header().number()); + let seed_hash = self.seed_compute.locked().get_seedhash(b.block().header().number()); let block_number = RpcU256::from(b.block().header().number()); to_value(&(RpcH256::from(pow_hash), RpcH256::from(seed_hash), RpcH256::from(target), block_number)) }).unwrap_or(Err(Error::internal_error())) // no work found. diff --git a/rpc/src/v1/impls/eth_filter.rs b/rpc/src/v1/impls/eth_filter.rs index cf2006788..017e0ad32 100644 --- a/rpc/src/v1/impls/eth_filter.rs +++ b/rpc/src/v1/impls/eth_filter.rs @@ -20,6 +20,7 @@ use std::ops::Deref; use std::sync::{Arc, Weak, Mutex}; use std::collections::HashSet; use jsonrpc_core::*; +use util::Lockable; use util::numbers::*; use ethcore::miner::MinerService; use ethcore::filter::Filter as EthcoreFilter; @@ -68,7 +69,7 @@ impl EthFilter for EthFilterClient where try!(self.active()); from_params::<(Filter,)>(params) .and_then(|(filter,)| { - let mut polls = self.polls.lock().unwrap(); + let mut polls = self.polls.locked(); let block_number = take_weak!(self.client).chain_info().best_block_number; let id = polls.create_poll(PollFilter::Logs(block_number, Default::default(), filter)); to_value(&RpcU256::from(id)) @@ -79,7 +80,7 @@ impl EthFilter for EthFilterClient where try!(self.active()); match params { Params::None => { - let mut polls = self.polls.lock().unwrap(); + let mut polls = self.polls.locked(); let id = polls.create_poll(PollFilter::Block(take_weak!(self.client).chain_info().best_block_number)); to_value(&RpcU256::from(id)) }, @@ -91,7 +92,7 @@ impl EthFilter for EthFilterClient where try!(self.active()); match params { Params::None => { - let mut polls = self.polls.lock().unwrap(); + let mut polls = self.polls.locked(); let pending_transactions = take_weak!(self.miner).pending_transactions_hashes(); let id = polls.create_poll(PollFilter::PendingTransaction(pending_transactions)); @@ -106,7 +107,7 @@ impl EthFilter for EthFilterClient where let client = take_weak!(self.client); from_params::<(Index,)>(params) .and_then(|(index,)| { - let mut polls = self.polls.lock().unwrap(); + let mut polls = self.polls.locked(); match polls.poll_mut(&index.value()) { None => Ok(Value::Array(vec![] as Vec)), Some(filter) => match *filter { @@ -196,7 +197,7 @@ impl EthFilter for EthFilterClient where try!(self.active()); from_params::<(Index,)>(params) .and_then(|(index,)| { - let mut polls = self.polls.lock().unwrap(); + let mut polls = self.polls.locked(); match polls.poll(&index.value()) { Some(&PollFilter::Logs(ref _block_number, ref _previous_log, ref filter)) => { let include_pending = filter.to_block == Some(BlockNumber::Pending); @@ -222,7 +223,7 @@ impl EthFilter for EthFilterClient where try!(self.active()); from_params::<(Index,)>(params) .and_then(|(index,)| { - self.polls.lock().unwrap().remove_poll(&index.value()); + self.polls.locked().remove_poll(&index.value()); to_value(&true) }) } diff --git a/rpc/src/v1/tests/helpers/miner_service.rs b/rpc/src/v1/tests/helpers/miner_service.rs index deb12d14d..8c050b118 100644 --- a/rpc/src/v1/tests/helpers/miner_service.rs +++ b/rpc/src/v1/tests/helpers/miner_service.rs @@ -16,7 +16,7 @@ //! Test implementation of miner service. -use util::{Address, H256, Bytes, U256, FixedHash, Uint}; +use util::{Address, H256, Bytes, U256, FixedHash, Uint, Lockable}; use util::standard::*; use ethcore::error::{Error, ExecutionError}; use ethcore::client::{MiningBlockChainClient, Executed, CallAnalytics}; @@ -133,7 +133,7 @@ impl MinerService for TestMinerService { fn import_external_transactions(&self, _chain: &MiningBlockChainClient, transactions: Vec) -> Vec> { // lets assume that all txs are valid - self.imported_transactions.lock().unwrap().extend_from_slice(&transactions); + self.imported_transactions.locked().extend_from_slice(&transactions); for sender in transactions.iter().filter_map(|t| t.sender().ok()) { let nonce = self.last_nonce(&sender).expect("last_nonce must be populated in tests"); @@ -156,7 +156,7 @@ impl MinerService for TestMinerService { } // lets assume that all txs are valid - self.imported_transactions.lock().unwrap().push(transaction); + self.imported_transactions.locked().push(transaction); Ok(TransactionImportResult::Current) } @@ -186,19 +186,19 @@ impl MinerService for TestMinerService { } fn transaction(&self, hash: &H256) -> Option { - self.pending_transactions.lock().unwrap().get(hash).cloned() + self.pending_transactions.locked().get(hash).cloned() } fn all_transactions(&self) -> Vec { - self.pending_transactions.lock().unwrap().values().cloned().collect() + self.pending_transactions.locked().values().cloned().collect() } fn pending_transactions(&self) -> Vec { - self.pending_transactions.lock().unwrap().values().cloned().collect() + self.pending_transactions.locked().values().cloned().collect() } fn pending_receipts(&self) -> BTreeMap { - self.pending_receipts.lock().unwrap().clone() + self.pending_receipts.locked().clone() } fn last_nonce(&self, address: &Address) -> Option { @@ -212,7 +212,7 @@ impl MinerService for TestMinerService { } fn balance(&self, _chain: &MiningBlockChainClient, address: &Address) -> U256 { - self.latest_closed_block.lock().unwrap().as_ref().map_or_else(U256::zero, |b| b.block().fields().state.balance(address).clone()) + self.latest_closed_block.locked().as_ref().map_or_else(U256::zero, |b| b.block().fields().state.balance(address).clone()) } fn call(&self, _chain: &MiningBlockChainClient, _t: &SignedTransaction, _analytics: CallAnalytics) -> Result { @@ -220,7 +220,7 @@ impl MinerService for TestMinerService { } fn storage_at(&self, _chain: &MiningBlockChainClient, address: &Address, position: &H256) -> H256 { - self.latest_closed_block.lock().unwrap().as_ref().map_or_else(H256::default, |b| b.block().fields().state.storage_at(address, position).clone()) + self.latest_closed_block.locked().as_ref().map_or_else(H256::default, |b| b.block().fields().state.storage_at(address, position).clone()) } fn nonce(&self, _chain: &MiningBlockChainClient, address: &Address) -> U256 { @@ -230,7 +230,7 @@ impl MinerService for TestMinerService { } fn code(&self, _chain: &MiningBlockChainClient, address: &Address) -> Option { - self.latest_closed_block.lock().unwrap().as_ref().map_or(None, |b| b.block().fields().state.code(address).clone()) + self.latest_closed_block.locked().as_ref().map_or(None, |b| b.block().fields().state.code(address).clone()) } } diff --git a/rpc/src/v1/tests/mocked/eth.rs b/rpc/src/v1/tests/mocked/eth.rs index 29c10a92b..d68e4b8b4 100644 --- a/rpc/src/v1/tests/mocked/eth.rs +++ b/rpc/src/v1/tests/mocked/eth.rs @@ -18,6 +18,7 @@ use std::str::FromStr; use std::collections::HashMap; use std::sync::{Arc, RwLock}; use jsonrpc_core::IoHandler; +use util::Lockable; use util::hash::{Address, H256, FixedHash}; use util::numbers::{Uint, U256}; use ethcore::account_provider::AccountProvider; @@ -363,7 +364,7 @@ fn rpc_eth_pending_transaction_by_hash() { let tester = EthTester::default(); { let tx: SignedTransaction = decode(&FromHex::from_hex("f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804").unwrap()); - tester.miner.pending_transactions.lock().unwrap().insert(H256::zero(), tx); + tester.miner.pending_transactions.locked().insert(H256::zero(), tx); } let response = r#"{"jsonrpc":"2.0","result":{"blockHash":null,"blockNumber":null,"creates":null,"from":"0x0f65fe9276bc9a24ae7083ae28e2660ef72df99e","gas":"0x5208","gasPrice":"0x01","hash":"0x41df922fd0d4766fcc02e161f8295ec28522f329ae487f14d811e4b64c8d6e31","input":"0x","nonce":"0x00","to":"0x095e7baea6a6c7c4c2dfeb977efac326af552d87","transactionIndex":null,"value":"0x0a"},"id":1}"#; diff --git a/rpc/src/v1/tests/mocked/personal_signer.rs b/rpc/src/v1/tests/mocked/personal_signer.rs index b2ef694a1..374c5dfa0 100644 --- a/rpc/src/v1/tests/mocked/personal_signer.rs +++ b/rpc/src/v1/tests/mocked/personal_signer.rs @@ -18,6 +18,7 @@ use std::sync::Arc; use std::str::FromStr; use jsonrpc_core::IoHandler; use util::numbers::*; +use util::Lockable; use ethcore::account_provider::AccountProvider; use ethcore::client::TestBlockChainClient; use ethcore::transaction::{Transaction, Action}; @@ -112,7 +113,7 @@ fn should_reject_transaction_from_queue_without_dispatching() { // then assert_eq!(tester.io.handle_request(&request), Some(response.to_owned())); assert_eq!(tester.queue.requests().len(), 0); - assert_eq!(tester.miner.imported_transactions.lock().unwrap().len(), 0); + assert_eq!(tester.miner.imported_transactions.locked().len(), 0); } #[test] @@ -181,6 +182,6 @@ fn should_confirm_transaction_and_dispatch() { // then assert_eq!(tester.io.handle_request(&request), Some(response.to_owned())); assert_eq!(tester.queue.requests().len(), 0); - assert_eq!(tester.miner.imported_transactions.lock().unwrap().len(), 1); + assert_eq!(tester.miner.imported_transactions.locked().len(), 1); } diff --git a/util/src/io/worker.rs b/util/src/io/worker.rs index 917b1ad79..333acb6af 100644 --- a/util/src/io/worker.rs +++ b/util/src/io/worker.rs @@ -22,6 +22,7 @@ use crossbeam::sync::chase_lev; use io::service::{HandlerId, IoChannel, IoContext}; use io::{IoHandler}; use panics::*; +use misc::Lockable; pub enum WorkType { Readable, @@ -81,7 +82,7 @@ impl Worker { where Message: Send + Sync + Clone + 'static { loop { { - let lock = wait_mutex.lock().unwrap(); + let lock = wait_mutex.locked(); if deleting.load(AtomicOrdering::Acquire) { return; } diff --git a/util/src/misc.rs b/util/src/misc.rs index 8d22e04d7..3d4df8b94 100644 --- a/util/src/misc.rs +++ b/util/src/misc.rs @@ -65,3 +65,13 @@ pub fn version_data() -> Bytes { s.append(&&Target::os()[0..2]); s.out() } + +/// Object can be locked directly into a `MutexGuard`. +pub trait Lockable { + /// Lock object directly into a `MutexGuard`. + fn locked(&self) -> MutexGuard; +} + +impl Lockable for Mutex { + fn locked(&self) -> MutexGuard { self.lock().unwrap() } +} diff --git a/util/src/network/host.rs b/util/src/network/host.rs index a09295083..c85b4cc57 100644 --- a/util/src/network/host.rs +++ b/util/src/network/host.rs @@ -28,7 +28,7 @@ use std::fs; use mio::*; use mio::tcp::*; use hash::*; -use misc::version; +use misc::*; use crypto::*; use sha3::Hashable; use rlp::*; @@ -202,7 +202,7 @@ impl<'s, Message> NetworkContext<'s, Message> where Message: Send + Sync + Clone protocol: ProtocolId, session: Option, sessions: Arc>>, reserved_peers: &'s HashSet) -> NetworkContext<'s, Message> { - let id = session.as_ref().map(|s| s.lock().unwrap().token()); + let id = session.as_ref().map(|s| s.locked().token()); NetworkContext { io: io, protocol: protocol, @@ -224,7 +224,7 @@ impl<'s, Message> NetworkContext<'s, Message> where Message: Send + Sync + Clone pub fn send(&self, peer: PeerId, packet_id: PacketId, data: Vec) -> Result<(), UtilError> { let session = self.resolve_session(peer); if let Some(session) = session { - try!(session.lock().unwrap().send_packet(self.io, self.protocol, packet_id as u8, &data)); + try!(session.locked().send_packet(self.io, self.protocol, packet_id as u8, &data)); } else { trace!(target: "network", "Send: Peer no longer exist") } @@ -262,7 +262,7 @@ impl<'s, Message> NetworkContext<'s, Message> where Message: Send + Sync + Clone /// Check if the session is still active. pub fn is_expired(&self) -> bool { - self.session.as_ref().map_or(false, |s| s.lock().unwrap().expired()) + self.session.as_ref().map_or(false, |s| s.locked().expired()) } /// Register a new IO timer. 'IoHandler::timeout' will be called with the token. @@ -279,7 +279,7 @@ impl<'s, Message> NetworkContext<'s, Message> where Message: Send + Sync + Clone pub fn peer_info(&self, peer: PeerId) -> String { let session = self.resolve_session(peer); if let Some(session) = session { - return session.lock().unwrap().info.client_version.clone() + return session.locked().info.client_version.clone() } "unknown".to_owned() } @@ -423,7 +423,7 @@ impl Host where Message: Send + Sync + Clone { let entry = NodeEntry { endpoint: n.endpoint.clone(), id: n.id.clone() }; self.nodes.write().unwrap().add_node(n); - if let Some(ref mut discovery) = *self.discovery.lock().unwrap() { + if let Some(ref mut discovery) = *self.discovery.locked() { discovery.add_node(entry); } } @@ -436,7 +436,7 @@ impl Host where Message: Send + Sync + Clone { let entry = NodeEntry { endpoint: n.endpoint.clone(), id: n.id.clone() }; self.reserved_nodes.write().unwrap().insert(n.id.clone()); - if let Some(ref mut discovery) = *self.discovery.lock().unwrap() { + if let Some(ref mut discovery) = *self.discovery.locked() { discovery.add_node(entry); } @@ -454,7 +454,7 @@ impl Host where Message: Send + Sync + Clone { let reserved: HashSet = self.reserved_nodes.read().unwrap().clone(); let mut to_kill = Vec::new(); for e in self.sessions.write().unwrap().iter_mut() { - let mut s = e.lock().unwrap(); + let mut s = e.locked(); { let id = s.id(); if id.is_some() && reserved.contains(id.unwrap()) { @@ -498,7 +498,7 @@ impl Host where Message: Send + Sync + Clone { self.stopping.store(true, AtomicOrdering::Release); let mut to_kill = Vec::new(); for e in self.sessions.write().unwrap().iter_mut() { - let mut s = e.lock().unwrap(); + let mut s = e.locked(); s.disconnect(io, DisconnectReason::ClientQuit); to_kill.push(s.token()); } @@ -555,7 +555,7 @@ impl Host where Message: Send + Sync + Clone { for n in self.nodes.read().unwrap().unordered_entries() { discovery.add_node(n.clone()); } - *self.discovery.lock().unwrap() = Some(discovery); + *self.discovery.locked() = Some(discovery); io.register_stream(DISCOVERY).expect("Error registering UDP listener"); io.register_timer(DISCOVERY_REFRESH, 7200).expect("Error registering discovery timer"); io.register_timer(DISCOVERY_ROUND, 300).expect("Error registering discovery timer"); @@ -571,7 +571,7 @@ impl Host where Message: Send + Sync + Clone { } fn have_session(&self, id: &NodeId) -> bool { - self.sessions.read().unwrap().iter().any(|e| e.lock().unwrap().info.id == Some(id.clone())) + self.sessions.read().unwrap().iter().any(|e| e.locked().info.id == Some(id.clone())) } fn session_count(&self) -> usize { @@ -579,7 +579,7 @@ impl Host where Message: Send + Sync + Clone { } fn connecting_to(&self, id: &NodeId) -> bool { - self.sessions.read().unwrap().iter().any(|e| e.lock().unwrap().id() == Some(id)) + self.sessions.read().unwrap().iter().any(|e| e.locked().id() == Some(id)) } fn handshake_count(&self) -> usize { @@ -589,7 +589,7 @@ impl Host where Message: Send + Sync + Clone { fn keep_alive(&self, io: &IoContext>) { let mut to_kill = Vec::new(); for e in self.sessions.write().unwrap().iter_mut() { - let mut s = e.lock().unwrap(); + let mut s = e.locked(); if !s.keep_alive(io) { s.disconnect(io, DisconnectReason::PingTimeout); to_kill.push(s.token()); @@ -711,7 +711,7 @@ impl Host where Message: Send + Sync + Clone { fn accept(&self, io: &IoContext>) { trace!(target: "network", "Accepting incoming connection"); loop { - let socket = match self.tcp_listener.lock().unwrap().accept() { + let socket = match self.tcp_listener.locked().accept() { Ok(None) => break, Ok(Some((sock, _addr))) => sock, Err(e) => { @@ -728,7 +728,7 @@ impl Host where Message: Send + Sync + Clone { fn session_writable(&self, token: StreamToken, io: &IoContext>) { let session = { self.sessions.read().unwrap().get(token).cloned() }; if let Some(session) = session { - let mut s = session.lock().unwrap(); + let mut s = session.locked(); if let Err(e) = s.writable(io, &self.info.read().unwrap()) { trace!(target: "network", "Session write error: {}: {:?}", token, e); } @@ -750,7 +750,7 @@ impl Host where Message: Send + Sync + Clone { let mut kill = false; let session = { self.sessions.read().unwrap().get(token).cloned() }; if let Some(session) = session.clone() { - let mut s = session.lock().unwrap(); + let mut s = session.locked(); loop { match s.readable(io, &self.info.read().unwrap()) { Err(e) => { @@ -785,7 +785,7 @@ impl Host where Message: Send + Sync + Clone { if let Ok(address) = s.remote_addr() { let entry = NodeEntry { id: s.id().unwrap().clone(), endpoint: NodeEndpoint { address: address, udp_port: address.port() } }; self.nodes.write().unwrap().add_node(Node::new(entry.id.clone(), entry.endpoint.clone())); - let mut discovery = self.discovery.lock().unwrap(); + let mut discovery = self.discovery.locked(); if let Some(ref mut discovery) = *discovery.deref_mut() { discovery.add_node(entry); } @@ -843,7 +843,7 @@ impl Host where Message: Send + Sync + Clone { let sessions = self.sessions.write().unwrap(); if let Some(session) = sessions.get(token).cloned() { expired_session = Some(session.clone()); - let mut s = session.lock().unwrap(); + let mut s = session.locked(); if !s.expired() { if s.is_ready() { self.num_sessions.fetch_sub(1, AtomicOrdering::SeqCst); @@ -879,7 +879,7 @@ impl Host where Message: Send + Sync + Clone { { let sessions = self.sessions.write().unwrap(); for c in sessions.iter() { - let s = c.lock().unwrap(); + let s = c.locked(); if let Some(id) = s.id() { if node_changes.removed.contains(id) { to_remove.push(s.token()); @@ -918,7 +918,7 @@ impl IoHandler> for Host where Messa match stream { FIRST_SESSION ... LAST_SESSION => self.session_readable(stream, io), DISCOVERY => { - let node_changes = { self.discovery.lock().unwrap().as_mut().unwrap().readable(io) }; + let node_changes = { self.discovery.locked().as_mut().unwrap().readable(io) }; if let Some(node_changes) = node_changes { self.update_nodes(io, node_changes); } @@ -935,7 +935,7 @@ impl IoHandler> for Host where Messa match stream { FIRST_SESSION ... LAST_SESSION => self.session_writable(stream, io), DISCOVERY => { - self.discovery.lock().unwrap().as_mut().unwrap().writable(io); + self.discovery.locked().as_mut().unwrap().writable(io); } _ => panic!("Received unknown writable token"), } @@ -951,11 +951,11 @@ impl IoHandler> for Host where Messa warn!("Error initializing public interface: {:?}", e)), FIRST_SESSION ... LAST_SESSION => self.connection_timeout(token, io), DISCOVERY_REFRESH => { - self.discovery.lock().unwrap().as_mut().unwrap().refresh(); + self.discovery.locked().as_mut().unwrap().refresh(); io.update_registration(DISCOVERY).unwrap_or_else(|e| debug!("Error updating discovery registration: {:?}", e)); }, DISCOVERY_ROUND => { - let node_changes = { self.discovery.lock().unwrap().as_mut().unwrap().round() }; + let node_changes = { self.discovery.locked().as_mut().unwrap().round() }; if let Some(node_changes) = node_changes { self.update_nodes(io, node_changes); } @@ -1015,7 +1015,7 @@ impl IoHandler> for Host where Messa NetworkIoMessage::Disconnect(ref peer) => { let session = { self.sessions.read().unwrap().get(*peer).cloned() }; if let Some(session) = session { - session.lock().unwrap().disconnect(io, DisconnectReason::DisconnectRequested); + session.locked().disconnect(io, DisconnectReason::DisconnectRequested); } trace!(target: "network", "Disconnect requested {}", peer); self.kill_connection(*peer, io, false); @@ -1023,8 +1023,8 @@ impl IoHandler> for Host where Messa NetworkIoMessage::DisablePeer(ref peer) => { let session = { self.sessions.read().unwrap().get(*peer).cloned() }; if let Some(session) = session { - session.lock().unwrap().disconnect(io, DisconnectReason::DisconnectRequested); - if let Some(id) = session.lock().unwrap().id() { + session.locked().disconnect(io, DisconnectReason::DisconnectRequested); + if let Some(id) = session.locked().id() { self.nodes.write().unwrap().mark_as_useless(id) } } @@ -1046,11 +1046,11 @@ impl IoHandler> for Host where Messa FIRST_SESSION ... LAST_SESSION => { let session = { self.sessions.read().unwrap().get(stream).cloned() }; if let Some(session) = session { - session.lock().unwrap().register_socket(reg, event_loop).expect("Error registering socket"); + session.locked().register_socket(reg, event_loop).expect("Error registering socket"); } } - DISCOVERY => self.discovery.lock().unwrap().as_ref().unwrap().register_socket(event_loop).expect("Error registering discovery socket"), - TCP_ACCEPT => event_loop.register(&*self.tcp_listener.lock().unwrap(), Token(TCP_ACCEPT), EventSet::all(), PollOpt::edge()).expect("Error registering stream"), + DISCOVERY => self.discovery.locked().as_ref().unwrap().register_socket(event_loop).expect("Error registering discovery socket"), + TCP_ACCEPT => event_loop.register(&*self.tcp_listener.locked(), Token(TCP_ACCEPT), EventSet::all(), PollOpt::edge()).expect("Error registering stream"), _ => warn!("Unexpected stream registration") } } @@ -1060,7 +1060,7 @@ impl IoHandler> for Host where Messa FIRST_SESSION ... LAST_SESSION => { let mut connections = self.sessions.write().unwrap(); if let Some(connection) = connections.get(stream).cloned() { - connection.lock().unwrap().deregister_socket(event_loop).expect("Error deregistering socket"); + connection.locked().deregister_socket(event_loop).expect("Error deregistering socket"); connections.remove(stream); } } @@ -1074,11 +1074,11 @@ impl IoHandler> for Host where Messa FIRST_SESSION ... LAST_SESSION => { let connection = { self.sessions.read().unwrap().get(stream).cloned() }; if let Some(connection) = connection { - connection.lock().unwrap().update_socket(reg, event_loop).expect("Error updating socket"); + connection.locked().update_socket(reg, event_loop).expect("Error updating socket"); } } - DISCOVERY => self.discovery.lock().unwrap().as_ref().unwrap().update_registration(event_loop).expect("Error reregistering discovery socket"), - TCP_ACCEPT => event_loop.reregister(&*self.tcp_listener.lock().unwrap(), Token(TCP_ACCEPT), EventSet::all(), PollOpt::edge()).expect("Error reregistering stream"), + DISCOVERY => self.discovery.locked().as_ref().unwrap().update_registration(event_loop).expect("Error reregistering discovery socket"), + TCP_ACCEPT => event_loop.reregister(&*self.tcp_listener.locked(), Token(TCP_ACCEPT), EventSet::all(), PollOpt::edge()).expect("Error reregistering stream"), _ => warn!("Unexpected stream update") } } diff --git a/util/src/network/tests.rs b/util/src/network/tests.rs index cd3f48d9a..5b6c863cb 100644 --- a/util/src/network/tests.rs +++ b/util/src/network/tests.rs @@ -51,7 +51,7 @@ impl TestProtocol { } pub fn got_packet(&self) -> bool { - self.packet.lock().unwrap().deref()[..] == b"hello"[..] + self.packet.locked().deref()[..] == b"hello"[..] } pub fn got_timeout(&self) -> bool { @@ -70,7 +70,7 @@ impl NetworkProtocolHandler for TestProtocol { fn read(&self, _io: &NetworkContext, _peer: &PeerId, packet_id: u8, data: &[u8]) { assert_eq!(packet_id, 33); - self.packet.lock().unwrap().extend(data); + self.packet.locked().extend(data); } fn connected(&self, io: &NetworkContext, peer: &PeerId) { diff --git a/util/src/panics.rs b/util/src/panics.rs index 854e495a3..368e6a9b4 100644 --- a/util/src/panics.rs +++ b/util/src/panics.rs @@ -20,6 +20,7 @@ use std::thread; use std::ops::DerefMut; use std::sync::{Arc, Mutex}; use std::default::Default; +use misc::Lockable; /// Thread-safe closure for handling possible panics pub trait OnPanicListener: Send + Sync + 'static { @@ -88,7 +89,7 @@ impl PanicHandler { /// Notifies all listeners in case there is a panic. /// You should use `catch_panic` instead of calling this method explicitly. pub fn notify_all(&self, r: String) { - let mut listeners = self.listeners.lock().unwrap(); + let mut listeners = self.listeners.locked(); for listener in listeners.deref_mut() { listener.call(&r); } @@ -97,7 +98,7 @@ impl PanicHandler { impl MayPanic for PanicHandler { fn on_panic(&self, closure: F) where F: OnPanicListener { - self.listeners.lock().unwrap().push(Box::new(closure)); + self.listeners.locked().push(Box::new(closure)); } } From 3b662c285fc4338e5dfcaee5aba57dea64b8767e Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 7 Jul 2016 09:37:31 +0200 Subject: [PATCH 02/16] Switch out .X().unwrap() for .unwrapped_X --- db/src/database.rs | 38 +++---- ethcore/src/account_provider.rs | 12 +- ethcore/src/block_queue.rs | 16 +-- ethcore/src/blockchain/blockchain.rs | 50 ++++---- ethcore/src/client/client.rs | 4 +- ethcore/src/client/test_client.rs | 90 +++++++-------- ethcore/src/db.rs | 8 +- ethcore/src/miner/external.rs | 9 +- ethcore/src/miner/miner.rs | 18 +-- ethcore/src/spec/spec.rs | 10 +- ethcore/src/trace/db.rs | 9 +- parity/hypervisor/mod.rs | 11 +- parity/informant.rs | 16 +-- rpc/src/v1/helpers/signing_queue.rs | 16 +-- rpc/src/v1/tests/helpers/miner_service.rs | 34 +++--- rpc/src/v1/tests/helpers/sync_provider.rs | 4 +- rpc/src/v1/tests/mocked/eth.rs | 22 ++-- rpc/src/v1/tests/mocked/personal.rs | 3 +- sync/src/chain.rs | 2 +- sync/src/lib.rs | 24 ++-- sync/src/tests/chain.rs | 22 ++-- sync/src/tests/helpers.rs | 10 +- util/src/io/service.rs | 11 +- util/src/journaldb/earlymergedb.rs | 7 +- util/src/journaldb/overlayrecentdb.rs | 11 +- util/src/log.rs | 5 +- util/src/misc.rs | 15 +++ util/src/network/host.rs | 132 +++++++++++----------- util/src/network/service.rs | 15 +-- util/src/network/tests.rs | 1 + util/src/panics.rs | 21 ++-- 31 files changed, 335 insertions(+), 311 deletions(-) diff --git a/db/src/database.rs b/db/src/database.rs index d535f1f56..0ad02b783 100644 --- a/db/src/database.rs +++ b/db/src/database.rs @@ -17,8 +17,8 @@ //! Ethcore rocksdb ipc service use traits::*; -use rocksdb::{DB, Writable, WriteBatch, IteratorMode, DBIterator, -IndexType, Options, DBCompactionStyle, BlockBasedOptions, Direction}; +use misc::RwLockable; +use rocksdb::{DB, Writable, WriteBatch, IteratorMode, DBIterator, IndexType, Options, DBCompactionStyle, BlockBasedOptions, Direction}; use std::sync::{RwLock, Arc}; use std::convert::From; use ipc::IpcConfig; @@ -137,8 +137,8 @@ impl Database { } pub fn flush(&self) -> Result<(), Error> { - let mut cache_lock = self.write_cache.write().unwrap(); - let db_lock = self.db.read().unwrap(); + let mut cache_lock = self.write_cache.unwrapped_write(); + let db_lock = self.db.unwrapped_read(); if db_lock.is_none() { return Ok(()); } let db = db_lock.as_ref().unwrap(); @@ -147,8 +147,8 @@ impl Database { } pub fn flush_all(&self) -> Result<(), Error> { - let mut cache_lock = self.write_cache.write().unwrap(); - let db_lock = self.db.read().unwrap(); + let mut cache_lock = self.write_cache.unwrapped_write(); + let db_lock = self.db.unwrapped_read(); if db_lock.is_none() { return Ok(()); } let db = db_lock.as_ref().expect("we should have exited with Ok(()) on the previous step"); @@ -167,7 +167,7 @@ impl Drop for Database { #[derive(Ipc)] impl DatabaseService for Database { fn open(&self, config: DatabaseConfig, path: String) -> Result<(), Error> { - let mut db = self.db.write().unwrap(); + let mut db = self.db.unwrapped_write(); if db.is_some() { return Err(Error::AlreadyOpen); } let mut opts = Options::new(); @@ -194,7 +194,7 @@ impl DatabaseService for Database { fn close(&self) -> Result<(), Error> { try!(self.flush_all()); - let mut db = self.db.write().unwrap(); + let mut db = self.db.unwrapped_write(); if db.is_none() { return Err(Error::IsClosed); } *db = None; @@ -202,19 +202,19 @@ impl DatabaseService for Database { } fn put(&self, key: &[u8], value: &[u8]) -> Result<(), Error> { - let mut cache_lock = self.write_cache.write().unwrap(); + let mut cache_lock = self.write_cache.unwrapped_write(); cache_lock.write(key.to_vec(), value.to_vec()); Ok(()) } fn delete(&self, key: &[u8]) -> Result<(), Error> { - let mut cache_lock = self.write_cache.write().unwrap(); + let mut cache_lock = self.write_cache.unwrapped_write(); cache_lock.remove(key.to_vec()); Ok(()) } fn write(&self, transaction: DBTransaction) -> Result<(), Error> { - let mut cache_lock = self.write_cache.write().unwrap(); + let mut cache_lock = self.write_cache.unwrapped_write(); let mut writes = transaction.writes.borrow_mut(); for kv in writes.drain(..) { @@ -231,13 +231,13 @@ impl DatabaseService for Database { fn get(&self, key: &[u8]) -> Result>, Error> { { let key_vec = key.to_vec(); - let cache_hit = self.write_cache.read().unwrap().get(&key_vec); + let cache_hit = self.write_cache.unwrapped_read().get(&key_vec); if cache_hit.is_some() { return Ok(Some(cache_hit.expect("cache_hit.is_some() = true, still there is none somehow here"))) } } - let db_lock = self.db.read().unwrap(); + let db_lock = self.db.unwrapped_read(); let db = try!(db_lock.as_ref().ok_or(Error::IsClosed)); match try!(db.get(key)) { @@ -249,7 +249,7 @@ impl DatabaseService for Database { } fn get_by_prefix(&self, prefix: &[u8]) -> Result>, Error> { - let db_lock = self.db.read().unwrap(); + let db_lock = self.db.unwrapped_read(); let db = try!(db_lock.as_ref().ok_or(Error::IsClosed)); let mut iter = db.iterator(IteratorMode::From(prefix, Direction::Forward)); @@ -261,17 +261,17 @@ impl DatabaseService for Database { } fn is_empty(&self) -> Result { - let db_lock = self.db.read().unwrap(); + let db_lock = self.db.unwrapped_read(); let db = try!(db_lock.as_ref().ok_or(Error::IsClosed)); Ok(db.iterator(IteratorMode::Start).next().is_none()) } fn iter(&self) -> Result { - let db_lock = self.db.read().unwrap(); + let db_lock = self.db.unwrapped_read(); let db = try!(db_lock.as_ref().ok_or(Error::IsClosed)); - let mut iterators = self.iterators.write().unwrap(); + let mut iterators = self.iterators.unwrapped_write(); let next_iterator = iterators.keys().last().unwrap_or(&0) + 1; iterators.insert(next_iterator, db.iterator(IteratorMode::Start)); Ok(next_iterator) @@ -279,7 +279,7 @@ impl DatabaseService for Database { fn iter_next(&self, handle: IteratorHandle) -> Option { - let mut iterators = self.iterators.write().unwrap(); + let mut iterators = self.iterators.unwrapped_write(); let mut iterator = match iterators.get_mut(&handle) { Some(some_iterator) => some_iterator, None => { return None; }, @@ -294,7 +294,7 @@ impl DatabaseService for Database { } fn dispose_iter(&self, handle: IteratorHandle) -> Result<(), Error> { - let mut iterators = self.iterators.write().unwrap(); + let mut iterators = self.iterators.unwrapped_write(); iterators.remove(&handle); Ok(()) } diff --git a/ethcore/src/account_provider.rs b/ethcore/src/account_provider.rs index a71251727..90d44bfba 100644 --- a/ethcore/src/account_provider.rs +++ b/ethcore/src/account_provider.rs @@ -19,7 +19,7 @@ use std::fmt; use std::sync::RwLock; use std::collections::HashMap; -use util::{Address as H160, H256, H520}; +use util::{Address as H160, H256, H520, RwLockable}; use ethstore::{SecretStore, Error as SSError, SafeAccount, EthStore}; use ethstore::dir::{KeyDirectory}; use ethstore::ethkey::{Address as SSAddress, Message as SSMessage, Secret as SSSecret, Random, Generator}; @@ -177,7 +177,7 @@ impl AccountProvider { // check if account is already unlocked pernamently, if it is, do nothing { - let unlocked = self.unlocked.read().unwrap(); + let unlocked = self.unlocked.unwrapped_read(); if let Some(data) = unlocked.get(&account) { if let Unlock::Perm = data.unlock { return Ok(()) @@ -190,7 +190,7 @@ impl AccountProvider { password: password, }; - let mut unlocked = self.unlocked.write().unwrap(); + let mut unlocked = self.unlocked.unwrapped_write(); unlocked.insert(account, data); Ok(()) } @@ -208,7 +208,7 @@ impl AccountProvider { /// Checks if given account is unlocked pub fn is_unlocked(&self, account: A) -> bool where Address: From { let account = Address::from(account).into(); - let unlocked = self.unlocked.read().unwrap(); + let unlocked = self.unlocked.unwrapped_read(); unlocked.get(&account).is_some() } @@ -218,12 +218,12 @@ impl AccountProvider { let message = Message::from(message).into(); let data = { - let unlocked = self.unlocked.read().unwrap(); + let unlocked = self.unlocked.unwrapped_read(); try!(unlocked.get(&account).ok_or(Error::NotUnlocked)).clone() }; if let Unlock::Temp = data.unlock { - let mut unlocked = self.unlocked.write().unwrap(); + let mut unlocked = self.unlocked.unwrapped_write(); unlocked.remove(&account).expect("data exists: so key must exist: qed"); } diff --git a/ethcore/src/block_queue.rs b/ethcore/src/block_queue.rs index 3ad329af5..c0b712d62 100644 --- a/ethcore/src/block_queue.rs +++ b/ethcore/src/block_queue.rs @@ -285,7 +285,7 @@ impl BlockQueue { unverified.clear(); verifying.clear(); verified.clear(); - self.processing.write().unwrap().clear(); + self.processing.unwrapped_write().clear(); } /// Wait for unverified queue to be empty @@ -298,7 +298,7 @@ impl BlockQueue { /// Check if the block is currently in the queue pub fn block_status(&self, hash: &H256) -> BlockStatus { - if self.processing.read().unwrap().contains(&hash) { + if self.processing.unwrapped_read().contains(&hash) { return BlockStatus::Queued; } if self.verification.bad.locked().contains(&hash) { @@ -312,7 +312,7 @@ impl BlockQueue { let header = BlockView::new(&bytes).header(); let h = header.hash(); { - if self.processing.read().unwrap().contains(&h) { + if self.processing.unwrapped_read().contains(&h) { return Err(ImportError::AlreadyQueued.into()); } @@ -329,7 +329,7 @@ impl BlockQueue { match verify_block_basic(&header, &bytes, self.engine.deref().deref()) { Ok(()) => { - self.processing.write().unwrap().insert(h.clone()); + self.processing.unwrapped_write().insert(h.clone()); self.verification.unverified.locked().push_back(UnverifiedBlock { header: header, bytes: bytes }); self.more_to_verify.notify_all(); Ok(h) @@ -350,7 +350,7 @@ impl BlockQueue { let mut verified_lock = self.verification.verified.locked(); let mut verified = verified_lock.deref_mut(); let mut bad = self.verification.bad.locked(); - let mut processing = self.processing.write().unwrap(); + let mut processing = self.processing.unwrapped_write(); bad.reserve(block_hashes.len()); for hash in block_hashes { bad.insert(hash.clone()); @@ -374,7 +374,7 @@ impl BlockQueue { if block_hashes.is_empty() { return; } - let mut processing = self.processing.write().unwrap(); + let mut processing = self.processing.unwrapped_write(); for hash in block_hashes { processing.remove(&hash); } @@ -421,7 +421,7 @@ impl BlockQueue { + verifying_bytes + verified_bytes // TODO: https://github.com/servo/heapsize/pull/50 - //+ self.processing.read().unwrap().heap_size_of_children(), + //+ self.processing.unwrapped_read().heap_size_of_children(), } } @@ -432,7 +432,7 @@ impl BlockQueue { self.verification.verifying.locked().shrink_to_fit(); self.verification.verified.locked().shrink_to_fit(); } - self.processing.write().unwrap().shrink_to_fit(); + self.processing.unwrapped_write().shrink_to_fit(); } } diff --git a/ethcore/src/blockchain/blockchain.rs b/ethcore/src/blockchain/blockchain.rs index 252c17b34..3ff030ca0 100644 --- a/ethcore/src/blockchain/blockchain.rs +++ b/ethcore/src/blockchain/blockchain.rs @@ -170,7 +170,7 @@ impl BlockProvider for BlockChain { /// Get raw block data fn block(&self, hash: &H256) -> Option { { - let read = self.blocks.read().unwrap(); + let read = self.blocks.unwrapped_read(); if let Some(v) = read.get(hash) { return Some(v.clone()); } @@ -184,7 +184,7 @@ impl BlockProvider for BlockChain { match opt { Some(b) => { let bytes: Bytes = b.to_vec(); - let mut write = self.blocks.write().unwrap(); + let mut write = self.blocks.unwrapped_write(); write.insert(hash.clone(), bytes.clone()); Some(bytes) }, @@ -338,7 +338,7 @@ impl BlockChain { }; { - let mut best_block = bc.best_block.write().unwrap(); + let mut best_block = bc.best_block.unwrapped_write(); best_block.number = bc.block_number(&best_block_hash).unwrap(); best_block.total_difficulty = bc.block_details(&best_block_hash).unwrap().total_difficulty; best_block.hash = best_block_hash; @@ -483,25 +483,25 @@ impl BlockChain { self.note_used(CacheID::BlockDetails(hash)); } - let mut write_details = self.block_details.write().unwrap(); + let mut write_details = self.block_details.unwrapped_write(); batch.extend_with_cache(write_details.deref_mut(), update.block_details, CacheUpdatePolicy::Overwrite); } { - let mut write_receipts = self.block_receipts.write().unwrap(); + let mut write_receipts = self.block_receipts.unwrapped_write(); batch.extend_with_cache(write_receipts.deref_mut(), update.block_receipts, CacheUpdatePolicy::Remove); } { - let mut write_blocks_blooms = self.blocks_blooms.write().unwrap(); + let mut write_blocks_blooms = self.blocks_blooms.unwrapped_write(); batch.extend_with_cache(write_blocks_blooms.deref_mut(), update.blocks_blooms, CacheUpdatePolicy::Remove); } // These cached values must be updated last and togeterh { - let mut best_block = self.best_block.write().unwrap(); - let mut write_hashes = self.block_hashes.write().unwrap(); - let mut write_txs = self.transaction_addresses.write().unwrap(); + let mut best_block = self.best_block.unwrapped_write(); + let mut write_hashes = self.block_hashes.unwrapped_write(); + let mut write_txs = self.transaction_addresses.unwrapped_write(); // update best block match update.info.location { @@ -728,33 +728,33 @@ impl BlockChain { /// Get best block hash. pub fn best_block_hash(&self) -> H256 { - self.best_block.read().unwrap().hash.clone() + self.best_block.unwrapped_read().hash.clone() } /// Get best block number. pub fn best_block_number(&self) -> BlockNumber { - self.best_block.read().unwrap().number + self.best_block.unwrapped_read().number } /// Get best block total difficulty. pub fn best_block_total_difficulty(&self) -> U256 { - self.best_block.read().unwrap().total_difficulty + self.best_block.unwrapped_read().total_difficulty } /// Get current cache size. pub fn cache_size(&self) -> CacheSize { CacheSize { - blocks: self.blocks.read().unwrap().heap_size_of_children(), - block_details: self.block_details.read().unwrap().heap_size_of_children(), - transaction_addresses: self.transaction_addresses.read().unwrap().heap_size_of_children(), - blocks_blooms: self.blocks_blooms.read().unwrap().heap_size_of_children(), - block_receipts: self.block_receipts.read().unwrap().heap_size_of_children(), + blocks: self.blocks.unwrapped_read().heap_size_of_children(), + block_details: self.block_details.unwrapped_read().heap_size_of_children(), + transaction_addresses: self.transaction_addresses.unwrapped_read().heap_size_of_children(), + blocks_blooms: self.blocks_blooms.unwrapped_read().heap_size_of_children(), + block_receipts: self.block_receipts.unwrapped_read().heap_size_of_children(), } } /// Let the cache system know that a cacheable item has been used. fn note_used(&self, id: CacheID) { - let mut cache_man = self.cache_man.write().unwrap(); + let mut cache_man = self.cache_man.unwrapped_write(); if !cache_man.cache_usage[0].contains(&id) { cache_man.cache_usage[0].insert(id.clone()); if cache_man.in_use.contains(&id) { @@ -773,13 +773,13 @@ impl BlockChain { for _ in 0..COLLECTION_QUEUE_SIZE { { - let mut blocks = self.blocks.write().unwrap(); - let mut block_details = self.block_details.write().unwrap(); - let mut block_hashes = self.block_hashes.write().unwrap(); - let mut transaction_addresses = self.transaction_addresses.write().unwrap(); - let mut blocks_blooms = self.blocks_blooms.write().unwrap(); - let mut block_receipts = self.block_receipts.write().unwrap(); - let mut cache_man = self.cache_man.write().unwrap(); + let mut blocks = self.blocks.unwrapped_write(); + let mut block_details = self.block_details.unwrapped_write(); + let mut block_hashes = self.block_hashes.unwrapped_write(); + let mut transaction_addresses = self.transaction_addresses.unwrapped_write(); + let mut blocks_blooms = self.blocks_blooms.unwrapped_write(); + let mut block_receipts = self.block_receipts.unwrapped_write(); + let mut cache_man = self.cache_man.unwrapped_write(); for id in cache_man.cache_usage.pop_back().unwrap().into_iter() { cache_man.in_use.remove(&id); diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index a2796a09e..34f820f3b 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -334,7 +334,7 @@ impl Client { let route = self.commit_block(closed_block, &header.hash(), &block.bytes); import_results.push(route); - self.report.write().unwrap().accrue_block(&block); + self.report.unwrapped_write().accrue_block(&block); trace!(target: "client", "Imported #{} ({})", header.number(), header.hash()); } @@ -462,7 +462,7 @@ impl Client { /// Get the report. pub fn report(&self) -> ClientReport { - let mut report = self.report.read().unwrap().clone(); + let mut report = self.report.unwrapped_read().clone(); report.state_db_mem = self.state_db.locked().mem_used(); report } diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index 16adfe7c3..5ae94a1cd 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -108,38 +108,38 @@ impl TestBlockChainClient { miner: Arc::new(Miner::with_spec(Spec::new_test())), }; client.add_blocks(1, EachBlockWith::Nothing); // add genesis block - client.genesis_hash = client.last_hash.read().unwrap().clone(); + client.genesis_hash = client.last_hash.unwrapped_read().clone(); client } /// Set the transaction receipt result pub fn set_transaction_receipt(&self, id: TransactionID, receipt: LocalizedReceipt) { - self.receipts.write().unwrap().insert(id, receipt); + self.receipts.unwrapped_write().insert(id, receipt); } /// Set the execution result. pub fn set_execution_result(&self, result: Executed) { - *self.execution_result.write().unwrap() = Some(result); + *self.execution_result.unwrapped_write() = Some(result); } /// Set the balance of account `address` to `balance`. pub fn set_balance(&self, address: Address, balance: U256) { - self.balances.write().unwrap().insert(address, balance); + self.balances.unwrapped_write().insert(address, balance); } /// Set nonce of account `address` to `nonce`. pub fn set_nonce(&self, address: Address, nonce: U256) { - self.nonces.write().unwrap().insert(address, nonce); + self.nonces.unwrapped_write().insert(address, nonce); } /// Set `code` at `address`. pub fn set_code(&self, address: Address, code: Bytes) { - self.code.write().unwrap().insert(address, code); + self.code.unwrapped_write().insert(address, code); } /// Set storage `position` to `value` for account `address`. pub fn set_storage(&self, address: Address, position: H256, value: H256) { - self.storage.write().unwrap().insert((address, position), value); + self.storage.unwrapped_write().insert((address, position), value); } /// Set block queue size for testing @@ -149,11 +149,11 @@ impl TestBlockChainClient { /// Add blocks to test client. pub fn add_blocks(&self, count: usize, with: EachBlockWith) { - let len = self.numbers.read().unwrap().len(); + let len = self.numbers.unwrapped_read().len(); for n in len..(len + count) { let mut header = BlockHeader::new(); header.difficulty = From::from(n); - header.parent_hash = self.last_hash.read().unwrap().clone(); + header.parent_hash = self.last_hash.unwrapped_read().clone(); header.number = n as BlockNumber; header.gas_limit = U256::from(1_000_000); let uncles = match with { @@ -161,7 +161,7 @@ impl TestBlockChainClient { let mut uncles = RlpStream::new_list(1); let mut uncle_header = BlockHeader::new(); uncle_header.difficulty = From::from(n); - uncle_header.parent_hash = self.last_hash.read().unwrap().clone(); + uncle_header.parent_hash = self.last_hash.unwrapped_read().clone(); uncle_header.number = n as BlockNumber; uncles.append(&uncle_header); header.uncles_hash = uncles.as_raw().sha3(); @@ -174,7 +174,7 @@ impl TestBlockChainClient { let mut txs = RlpStream::new_list(1); let keypair = KeyPair::create().unwrap(); // Update nonces value - self.nonces.write().unwrap().insert(keypair.address(), U256::one()); + self.nonces.unwrapped_write().insert(keypair.address(), U256::one()); let tx = Transaction { action: Action::Create, value: U256::from(100), @@ -207,7 +207,7 @@ impl TestBlockChainClient { rlp.append(&header); rlp.append_raw(&rlp::NULL_RLP, 1); rlp.append_raw(&rlp::NULL_RLP, 1); - self.blocks.write().unwrap().insert(hash, rlp.out()); + self.blocks.unwrapped_write().insert(hash, rlp.out()); } /// Make a bad block by setting invalid parent hash. @@ -219,12 +219,12 @@ impl TestBlockChainClient { rlp.append(&header); rlp.append_raw(&rlp::NULL_RLP, 1); rlp.append_raw(&rlp::NULL_RLP, 1); - self.blocks.write().unwrap().insert(hash, rlp.out()); + self.blocks.unwrapped_write().insert(hash, rlp.out()); } /// TODO: pub fn block_hash_delta_minus(&mut self, delta: usize) -> H256 { - let blocks_read = self.numbers.read().unwrap(); + let blocks_read = self.numbers.unwrapped_read(); let index = blocks_read.len() - delta; blocks_read[&index].clone() } @@ -232,9 +232,9 @@ impl TestBlockChainClient { fn block_hash(&self, id: BlockID) -> Option { match id { BlockID::Hash(hash) => Some(hash), - BlockID::Number(n) => self.numbers.read().unwrap().get(&(n as usize)).cloned(), - BlockID::Earliest => self.numbers.read().unwrap().get(&0).cloned(), - BlockID::Latest => self.numbers.read().unwrap().get(&(self.numbers.read().unwrap().len() - 1)).cloned() + BlockID::Number(n) => self.numbers.unwrapped_read().get(&(n as usize)).cloned(), + BlockID::Earliest => self.numbers.unwrapped_read().get(&0).cloned(), + BlockID::Latest => self.numbers.unwrapped_read().get(&(self.numbers.unwrapped_read().len() - 1)).cloned() } } } @@ -255,7 +255,7 @@ impl MiningBlockChainClient for TestBlockChainClient { impl BlockChainClient for TestBlockChainClient { fn call(&self, _t: &SignedTransaction, _analytics: CallAnalytics) -> Result { - Ok(self.execution_result.read().unwrap().clone().unwrap()) + Ok(self.execution_result.unwrapped_read().clone().unwrap()) } fn block_total_difficulty(&self, _id: BlockID) -> Option { @@ -268,7 +268,7 @@ impl BlockChainClient for TestBlockChainClient { fn nonce(&self, address: &Address, id: BlockID) -> Option { match id { - BlockID::Latest => Some(self.nonces.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)), + BlockID::Latest => Some(self.nonces.unwrapped_read().get(address).cloned().unwrap_or_else(U256::zero)), _ => None, } } @@ -278,12 +278,12 @@ impl BlockChainClient for TestBlockChainClient { } fn code(&self, address: &Address) -> Option { - self.code.read().unwrap().get(address).cloned() + self.code.unwrapped_read().get(address).cloned() } fn balance(&self, address: &Address, id: BlockID) -> Option { if let BlockID::Latest = id { - Some(self.balances.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)) + Some(self.balances.unwrapped_read().get(address).cloned().unwrap_or_else(U256::zero)) } else { None } @@ -295,7 +295,7 @@ impl BlockChainClient for TestBlockChainClient { fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option { if let BlockID::Latest = id { - Some(self.storage.read().unwrap().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new)) + Some(self.storage.unwrapped_read().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new)) } else { None } @@ -310,7 +310,7 @@ impl BlockChainClient for TestBlockChainClient { } fn transaction_receipt(&self, id: TransactionID) -> Option { - self.receipts.read().unwrap().get(&id).cloned() + self.receipts.unwrapped_read().get(&id).cloned() } fn blocks_with_bloom(&self, _bloom: &H2048, _from_block: BlockID, _to_block: BlockID) -> Option> { @@ -326,11 +326,11 @@ impl BlockChainClient for TestBlockChainClient { } fn block_header(&self, id: BlockID) -> Option { - self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).map(|r| Rlp::new(r).at(0).as_raw().to_vec())) + self.block_hash(id).and_then(|hash| self.blocks.unwrapped_read().get(&hash).map(|r| Rlp::new(r).at(0).as_raw().to_vec())) } fn block_body(&self, id: BlockID) -> Option { - self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).map(|r| { + self.block_hash(id).and_then(|hash| self.blocks.unwrapped_read().get(&hash).map(|r| { let mut stream = RlpStream::new_list(2); stream.append_raw(Rlp::new(&r).at(1).as_raw(), 1); stream.append_raw(Rlp::new(&r).at(2).as_raw(), 1); @@ -339,13 +339,13 @@ impl BlockChainClient for TestBlockChainClient { } fn block(&self, id: BlockID) -> Option { - self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).cloned()) + self.block_hash(id).and_then(|hash| self.blocks.unwrapped_read().get(&hash).cloned()) } fn block_status(&self, id: BlockID) -> BlockStatus { match id { - BlockID::Number(number) if (number as usize) < self.blocks.read().unwrap().len() => BlockStatus::InChain, - BlockID::Hash(ref hash) if self.blocks.read().unwrap().get(hash).is_some() => BlockStatus::InChain, + BlockID::Number(number) if (number as usize) < self.blocks.unwrapped_read().len() => BlockStatus::InChain, + BlockID::Hash(ref hash) if self.blocks.unwrapped_read().get(hash).is_some() => BlockStatus::InChain, _ => BlockStatus::Unknown } } @@ -356,7 +356,7 @@ impl BlockChainClient for TestBlockChainClient { ancestor: H256::new(), index: 0, blocks: { - let numbers_read = self.numbers.read().unwrap(); + let numbers_read = self.numbers.unwrapped_read(); let mut adding = false; let mut blocks = Vec::new(); @@ -413,11 +413,11 @@ impl BlockChainClient for TestBlockChainClient { let header = Rlp::new(&b).val_at::(0); let h = header.hash(); let number: usize = header.number as usize; - if number > self.blocks.read().unwrap().len() { - panic!("Unexpected block number. Expected {}, got {}", self.blocks.read().unwrap().len(), number); + if number > self.blocks.unwrapped_read().len() { + panic!("Unexpected block number. Expected {}, got {}", self.blocks.unwrapped_read().len(), number); } if number > 0 { - match self.blocks.read().unwrap().get(&header.parent_hash) { + match self.blocks.unwrapped_read().get(&header.parent_hash) { Some(parent) => { let parent = Rlp::new(parent).val_at::(0); if parent.number != (header.number - 1) { @@ -429,27 +429,27 @@ impl BlockChainClient for TestBlockChainClient { } } } - let len = self.numbers.read().unwrap().len(); + let len = self.numbers.unwrapped_read().len(); if number == len { { - let mut difficulty = self.difficulty.write().unwrap(); + let mut difficulty = self.difficulty.unwrapped_write(); *difficulty.deref_mut() = *difficulty.deref() + header.difficulty; } - mem::replace(self.last_hash.write().unwrap().deref_mut(), h.clone()); - self.blocks.write().unwrap().insert(h.clone(), b); - self.numbers.write().unwrap().insert(number, h.clone()); + mem::replace(self.last_hash.unwrapped_write().deref_mut(), h.clone()); + self.blocks.unwrapped_write().insert(h.clone(), b); + self.numbers.unwrapped_write().insert(number, h.clone()); let mut parent_hash = header.parent_hash; if number > 0 { let mut n = number - 1; - while n > 0 && self.numbers.read().unwrap()[&n] != parent_hash { - *self.numbers.write().unwrap().get_mut(&n).unwrap() = parent_hash.clone(); + while n > 0 && self.numbers.unwrapped_read()[&n] != parent_hash { + *self.numbers.unwrapped_write().get_mut(&n).unwrap() = parent_hash.clone(); n -= 1; - parent_hash = Rlp::new(&self.blocks.read().unwrap()[&parent_hash]).val_at::(0).parent_hash; + parent_hash = Rlp::new(&self.blocks.unwrapped_read()[&parent_hash]).val_at::(0).parent_hash; } } } else { - self.blocks.write().unwrap().insert(h.clone(), b.to_vec()); + self.blocks.unwrapped_write().insert(h.clone(), b.to_vec()); } Ok(h) } @@ -470,11 +470,11 @@ impl BlockChainClient for TestBlockChainClient { fn chain_info(&self) -> BlockChainInfo { BlockChainInfo { - total_difficulty: *self.difficulty.read().unwrap(), - pending_total_difficulty: *self.difficulty.read().unwrap(), + total_difficulty: *self.difficulty.unwrapped_read(), + pending_total_difficulty: *self.difficulty.unwrapped_read(), genesis_hash: self.genesis_hash.clone(), - best_block_hash: self.last_hash.read().unwrap().clone(), - best_block_number: self.blocks.read().unwrap().len() as BlockNumber - 1, + best_block_hash: self.last_hash.unwrapped_read().clone(), + best_block_number: self.blocks.unwrapped_read().len() as BlockNumber - 1, } } diff --git a/ethcore/src/db.rs b/ethcore/src/db.rs index d38ce3041..576aaef5b 100644 --- a/ethcore/src/db.rs +++ b/ethcore/src/db.rs @@ -20,7 +20,7 @@ use std::ops::Deref; use std::hash::Hash; use std::sync::RwLock; use std::collections::HashMap; -use util::{DBTransaction, Database}; +use util::{DBTransaction, Database, RwLockable}; use util::rlp::{encode, Encodable, decode, Decodable}; #[derive(Clone, Copy)] @@ -115,14 +115,14 @@ pub trait Readable { T: Clone + Decodable, C: Cache { { - let read = cache.read().unwrap(); + let read = cache.unwrapped_read(); if let Some(v) = read.get(key) { return Some(v.clone()); } } self.read(key).map(|value: T|{ - let mut write = cache.write().unwrap(); + let mut write = cache.unwrapped_write(); write.insert(key.clone(), value.clone()); value }) @@ -137,7 +137,7 @@ pub trait Readable { R: Deref, C: Cache { { - let read = cache.read().unwrap(); + let read = cache.unwrapped_read(); if read.get(key).is_some() { return true; } diff --git a/ethcore/src/miner/external.rs b/ethcore/src/miner/external.rs index ece91877d..650df228e 100644 --- a/ethcore/src/miner/external.rs +++ b/ethcore/src/miner/external.rs @@ -16,8 +16,7 @@ use std::collections::HashMap; use std::sync::{Arc, RwLock}; -use util::numbers::U256; -use util::hash::H256; +use util::{RwLockable, U256, H256}; /// External miner interface. pub trait ExternalMinerService: Send + Sync { @@ -55,15 +54,15 @@ impl ExternalMiner { impl ExternalMinerService for ExternalMiner { fn submit_hashrate(&self, hashrate: U256, id: H256) { - self.hashrates.write().unwrap().insert(id, hashrate); + self.hashrates.unwrapped_write().insert(id, hashrate); } fn hashrate(&self) -> U256 { - self.hashrates.read().unwrap().iter().fold(U256::from(0), |sum, (_, v)| sum + *v) + self.hashrates.unwrapped_read().iter().fold(U256::from(0), |sum, (_, v)| sum + *v) } fn is_mining(&self) -> bool { - !self.hashrates.read().unwrap().is_empty() + !self.hashrates.unwrapped_read().is_empty() } } diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index c9a20e7f1..becd872e5 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -425,20 +425,20 @@ impl MinerService for Miner { } fn set_author(&self, author: Address) { - *self.author.write().unwrap() = author; + *self.author.unwrapped_write() = author; } fn set_extra_data(&self, extra_data: Bytes) { - *self.extra_data.write().unwrap() = extra_data; + *self.extra_data.unwrapped_write() = extra_data; } /// Set the gas limit we wish to target when sealing a new block. fn set_gas_floor_target(&self, target: U256) { - self.gas_range_target.write().unwrap().0 = target; + self.gas_range_target.unwrapped_write().0 = target; } fn set_gas_ceil_target(&self, target: U256) { - self.gas_range_target.write().unwrap().1 = target; + self.gas_range_target.unwrapped_write().1 = target; } fn set_minimal_gas_price(&self, min_gas_price: U256) { @@ -455,7 +455,7 @@ impl MinerService for Miner { } fn sensible_gas_limit(&self) -> U256 { - self.gas_range_target.read().unwrap().0 / 5.into() + self.gas_range_target.unwrapped_read().0 / 5.into() } fn transactions_limit(&self) -> usize { @@ -472,22 +472,22 @@ impl MinerService for Miner { /// Get the author that we will seal blocks as. fn author(&self) -> Address { - *self.author.read().unwrap() + *self.author.unwrapped_read() } /// Get the extra_data that we will seal blocks with. fn extra_data(&self) -> Bytes { - self.extra_data.read().unwrap().clone() + self.extra_data.unwrapped_read().clone() } /// Get the gas limit we wish to target when sealing a new block. fn gas_floor_target(&self) -> U256 { - self.gas_range_target.read().unwrap().0 + self.gas_range_target.unwrapped_read().0 } /// Get the gas limit we wish to target when sealing a new block. fn gas_ceil_target(&self) -> U256 { - self.gas_range_target.read().unwrap().1 + self.gas_range_target.unwrapped_read().1 } fn import_external_transactions(&self, chain: &MiningBlockChainClient, transactions: Vec) -> diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index 02b311b24..5c2a7d8d7 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -136,10 +136,10 @@ impl Spec { /// Return the state root for the genesis state, memoising accordingly. pub fn state_root(&self) -> H256 { - if self.state_root_memo.read().unwrap().is_none() { - *self.state_root_memo.write().unwrap() = Some(self.genesis_state.root()); + if self.state_root_memo.unwrapped_read().is_none() { + *self.state_root_memo.unwrapped_write() = Some(self.genesis_state.root()); } - self.state_root_memo.read().unwrap().as_ref().unwrap().clone() + self.state_root_memo.unwrapped_read().as_ref().unwrap().clone() } /// Get the known knodes of the network in enode format. @@ -209,12 +209,12 @@ impl Spec { /// Alter the value of the genesis state. pub fn set_genesis_state(&mut self, s: PodState) { self.genesis_state = s; - *self.state_root_memo.write().unwrap() = None; + *self.state_root_memo.unwrapped_write() = None; } /// Returns `false` if the memoized state root is invalid. `true` otherwise. pub fn is_state_root_valid(&self) -> bool { - self.state_root_memo.read().unwrap().clone().map_or(true, |sr| sr == self.genesis_state.root()) + self.state_root_memo.unwrapped_read().clone().map_or(true, |sr| sr == self.genesis_state.root()) } /// Ensure that the given state DB has the trie nodes in for the genesis state. diff --git a/ethcore/src/trace/db.rs b/ethcore/src/trace/db.rs index 07f5f9c27..a49d28924 100644 --- a/ethcore/src/trace/db.rs +++ b/ethcore/src/trace/db.rs @@ -22,10 +22,9 @@ use std::sync::{RwLock, Arc}; use std::path::Path; use bloomchain::{Number, Config as BloomConfig}; use bloomchain::group::{BloomGroupDatabase, BloomGroupChain, GroupPosition, BloomGroup}; -use util::{H256, H264, Database, DatabaseConfig, DBTransaction}; +use util::{H256, H264, Database, DatabaseConfig, DBTransaction, RwLockable}; use header::BlockNumber; -use trace::{BlockTraces, LocalizedTrace, Config, Switch, Filter, Database as TraceDatabase, ImportRequest, -DatabaseExtras, Error}; +use trace::{BlockTraces, LocalizedTrace, Config, Switch, Filter, Database as TraceDatabase, ImportRequest, DatabaseExtras, Error}; use db::{Key, Writable, Readable, CacheUpdatePolicy}; use blooms; use super::flat::{FlatTrace, FlatBlockTraces, FlatTransactionTraces}; @@ -232,7 +231,7 @@ impl TraceDatabase for TraceDB where T: DatabaseExtras { // at first, let's insert new block traces { - let mut traces = self.traces.write().unwrap(); + let mut traces = self.traces.unwrapped_write(); // it's important to use overwrite here, // cause this value might be queried by hash later batch.write_with_cache(traces.deref_mut(), request.block_hash, request.traces, CacheUpdatePolicy::Overwrite); @@ -260,7 +259,7 @@ impl TraceDatabase for TraceDB where T: DatabaseExtras { .map(|p| (From::from(p.0), From::from(p.1))) .collect::>(); - let mut blooms = self.blooms.write().unwrap(); + let mut blooms = self.blooms.unwrapped_write(); batch.extend_with_cache(blooms.deref_mut(), blooms_to_insert, CacheUpdatePolicy::Remove); } diff --git a/parity/hypervisor/mod.rs b/parity/hypervisor/mod.rs index fbd807d94..501594d1f 100644 --- a/parity/hypervisor/mod.rs +++ b/parity/hypervisor/mod.rs @@ -20,19 +20,18 @@ #![allow(dead_code)] #![cfg_attr(feature="dev", allow(used_underscore_binding))] -pub mod service; - -/// Default value for hypervisor ipc listener -pub const HYPERVISOR_IPC_URL: &'static str = "ipc:///tmp/parity-internal-hyper-status.ipc"; - use nanoipc; use std::sync::{Arc,RwLock}; use hypervisor::service::*; use std::process::{Command,Child}; use std::collections::HashMap; -type BinaryId = &'static str; +pub mod service; +/// Default value for hypervisor ipc listener +pub const HYPERVISOR_IPC_URL: &'static str = "ipc:///tmp/parity-internal-hyper-status.ipc"; + +type BinaryId = &'static str; const BLOCKCHAIN_DB_BINARY: BinaryId = "blockchain"; pub struct Hypervisor { diff --git a/parity/informant.rs b/parity/informant.rs index 0eaebe10f..f0709458f 100644 --- a/parity/informant.rs +++ b/parity/informant.rs @@ -22,7 +22,7 @@ use std::time::{Instant, Duration}; use std::sync::RwLock; use std::ops::{Deref, DerefMut}; use ethsync::{EthSync, SyncProvider}; -use util::Uint; +use util::{Uint, RwLockable}; use ethcore::client::*; use number_prefix::{binary_prefix, Standalone, Prefixed}; @@ -77,18 +77,18 @@ impl Informant { #[cfg_attr(feature="dev", allow(match_bool))] pub fn tick(&self, client: &Client, maybe_sync: Option<&EthSync>) { - let elapsed = self.last_tick.read().unwrap().elapsed(); + let elapsed = self.last_tick.unwrapped_read().elapsed(); if elapsed < Duration::from_secs(5) { return; } - *self.last_tick.write().unwrap() = Instant::now(); + *self.last_tick.unwrapped_write() = Instant::now(); let chain_info = client.chain_info(); let queue_info = client.queue_info(); let cache_info = client.blockchain_cache_info(); - let mut write_report = self.report.write().unwrap(); + let mut write_report = self.report.unwrapped_write(); let report = client.report(); let paint = |c: Style, t: String| match self.with_color { @@ -97,8 +97,8 @@ impl Informant { }; if let (_, _, &Some(ref last_report)) = ( - self.chain_info.read().unwrap().deref(), - self.cache_info.read().unwrap().deref(), + self.chain_info.unwrapped_read().deref(), + self.cache_info.unwrapped_read().deref(), write_report.deref() ) { println!("{} {} {} blk/s {} tx/s {} Mgas/s {}{}+{} Qed {} db {} chain {} queue{}", @@ -137,8 +137,8 @@ impl Informant { ); } - *self.chain_info.write().unwrap().deref_mut() = Some(chain_info); - *self.cache_info.write().unwrap().deref_mut() = Some(cache_info); + *self.chain_info.unwrapped_write().deref_mut() = Some(chain_info); + *self.cache_info.unwrapped_write().deref_mut() = Some(cache_info); *write_report.deref_mut() = Some(report); } } diff --git a/rpc/src/v1/helpers/signing_queue.rs b/rpc/src/v1/helpers/signing_queue.rs index c21a056d8..d41114f96 100644 --- a/rpc/src/v1/helpers/signing_queue.rs +++ b/rpc/src/v1/helpers/signing_queue.rs @@ -19,7 +19,7 @@ use std::time::{Instant, Duration}; use std::sync::{mpsc, Mutex, RwLock, Arc}; use std::collections::HashMap; use jsonrpc_core; -use util::{U256, Lockable}; +use util::{U256, Lockable, RwLockable}; use v1::helpers::{TransactionRequest, TransactionConfirmation}; /// Result that can be returned from JSON RPC. @@ -214,7 +214,7 @@ impl ConfirmationsQueue { /// Removes transaction from this queue and notifies `ConfirmationPromise` holders about the result. /// Notifies also a receiver about that event. fn remove(&self, id: U256, result: Option) -> Option { - let token = self.queue.write().unwrap().remove(&id); + let token = self.queue.unwrapped_write().remove(&id); if let Some(token) = token { // notify receiver about the event @@ -247,7 +247,7 @@ impl SigningQueue for ConfirmationsQueue { }; // Add request to queue let res = { - let mut queue = self.queue.write().unwrap(); + let mut queue = self.queue.unwrapped_write(); queue.insert(id, ConfirmationToken { result: Arc::new(Mutex::new(ConfirmationResult::Waiting)), handle: thread::current(), @@ -266,7 +266,7 @@ impl SigningQueue for ConfirmationsQueue { } fn peek(&self, id: &U256) -> Option { - self.queue.read().unwrap().get(id).map(|token| token.request.clone()) + self.queue.unwrapped_read().get(id).map(|token| token.request.clone()) } fn request_rejected(&self, id: U256) -> Option { @@ -280,17 +280,17 @@ impl SigningQueue for ConfirmationsQueue { } fn requests(&self) -> Vec { - let queue = self.queue.read().unwrap(); + let queue = self.queue.unwrapped_read(); queue.values().map(|token| token.request.clone()).collect() } fn len(&self) -> usize { - let queue = self.queue.read().unwrap(); + let queue = self.queue.unwrapped_read(); queue.len() } fn is_empty(&self) -> bool { - let queue = self.queue.read().unwrap(); + let queue = self.queue.unwrapped_read(); queue.is_empty() } } @@ -301,7 +301,7 @@ mod test { use std::time::Duration; use std::thread; use std::sync::{Arc, Mutex}; - use util::{Address, U256, H256}; + use util::{Address, U256, H256, Lockable}; use v1::helpers::{SigningQueue, ConfirmationsQueue, QueueEvent, TransactionRequest}; use v1::types::H256 as NH256; use jsonrpc_core::to_value; diff --git a/rpc/src/v1/tests/helpers/miner_service.rs b/rpc/src/v1/tests/helpers/miner_service.rs index 8c050b118..b0a4f1115 100644 --- a/rpc/src/v1/tests/helpers/miner_service.rs +++ b/rpc/src/v1/tests/helpers/miner_service.rs @@ -16,7 +16,7 @@ //! Test implementation of miner service. -use util::{Address, H256, Bytes, U256, FixedHash, Uint, Lockable}; +use util::{Address, H256, Bytes, U256, FixedHash, Uint, Lockable, RwLockable}; use util::standard::*; use ethcore::error::{Error, ExecutionError}; use ethcore::client::{MiningBlockChainClient, Executed, CallAnalytics}; @@ -76,57 +76,57 @@ impl MinerService for TestMinerService { } fn set_author(&self, author: Address) { - *self.author.write().unwrap() = author; + *self.author.unwrapped_write() = author; } fn set_extra_data(&self, extra_data: Bytes) { - *self.extra_data.write().unwrap() = extra_data; + *self.extra_data.unwrapped_write() = extra_data; } /// Set the lower gas limit we wish to target when sealing a new block. fn set_gas_floor_target(&self, target: U256) { - self.gas_range_target.write().unwrap().0 = target; + self.gas_range_target.unwrapped_write().0 = target; } /// Set the upper gas limit we wish to target when sealing a new block. fn set_gas_ceil_target(&self, target: U256) { - self.gas_range_target.write().unwrap().1 = target; + self.gas_range_target.unwrapped_write().1 = target; } fn set_minimal_gas_price(&self, min_gas_price: U256) { - *self.min_gas_price.write().unwrap() = min_gas_price; + *self.min_gas_price.unwrapped_write() = min_gas_price; } fn set_transactions_limit(&self, limit: usize) { - *self.limit.write().unwrap() = limit; + *self.limit.unwrapped_write() = limit; } fn set_tx_gas_limit(&self, limit: U256) { - *self.tx_gas_limit.write().unwrap() = limit; + *self.tx_gas_limit.unwrapped_write() = limit; } fn transactions_limit(&self) -> usize { - *self.limit.read().unwrap() + *self.limit.unwrapped_read() } fn author(&self) -> Address { - *self.author.read().unwrap() + *self.author.unwrapped_read() } fn minimal_gas_price(&self) -> U256 { - *self.min_gas_price.read().unwrap() + *self.min_gas_price.unwrapped_read() } fn extra_data(&self) -> Bytes { - self.extra_data.read().unwrap().clone() + self.extra_data.unwrapped_read().clone() } fn gas_floor_target(&self) -> U256 { - self.gas_range_target.read().unwrap().0 + self.gas_range_target.unwrapped_read().0 } fn gas_ceil_target(&self) -> U256 { - self.gas_range_target.read().unwrap().1 + self.gas_range_target.unwrapped_read().1 } /// Imports transactions to transaction queue. @@ -137,7 +137,7 @@ impl MinerService for TestMinerService { for sender in transactions.iter().filter_map(|t| t.sender().ok()) { let nonce = self.last_nonce(&sender).expect("last_nonce must be populated in tests"); - self.last_nonces.write().unwrap().insert(sender, nonce + U256::from(1)); + self.last_nonces.unwrapped_write().insert(sender, nonce + U256::from(1)); } transactions .iter() @@ -152,7 +152,7 @@ impl MinerService for TestMinerService { // keep the pending nonces up to date if let Ok(ref sender) = transaction.sender() { let nonce = self.last_nonce(sender).unwrap_or(chain.latest_nonce(sender)); - self.last_nonces.write().unwrap().insert(sender.clone(), nonce + U256::from(1)); + self.last_nonces.unwrapped_write().insert(sender.clone(), nonce + U256::from(1)); } // lets assume that all txs are valid @@ -202,7 +202,7 @@ impl MinerService for TestMinerService { } fn last_nonce(&self, address: &Address) -> Option { - self.last_nonces.read().unwrap().get(address).cloned() + self.last_nonces.unwrapped_read().get(address).cloned() } /// Submit `seal` as a valid solution for the header of `pow_hash`. diff --git a/rpc/src/v1/tests/helpers/sync_provider.rs b/rpc/src/v1/tests/helpers/sync_provider.rs index 11fa1cb9e..d91c2a98c 100644 --- a/rpc/src/v1/tests/helpers/sync_provider.rs +++ b/rpc/src/v1/tests/helpers/sync_provider.rs @@ -16,7 +16,7 @@ //! Test implementation of SyncProvider. -use util::U256; +use util::{U256, RwLockable}; use ethsync::{SyncProvider, SyncStatus, SyncState}; use std::sync::RwLock; @@ -57,7 +57,7 @@ impl TestSyncProvider { impl SyncProvider for TestSyncProvider { fn status(&self) -> SyncStatus { - self.status.read().unwrap().clone() + self.status.unwrapped_read().clone() } fn start_network(&self) { diff --git a/rpc/src/v1/tests/mocked/eth.rs b/rpc/src/v1/tests/mocked/eth.rs index d68e4b8b4..9e0067c8c 100644 --- a/rpc/src/v1/tests/mocked/eth.rs +++ b/rpc/src/v1/tests/mocked/eth.rs @@ -18,7 +18,7 @@ use std::str::FromStr; use std::collections::HashMap; use std::sync::{Arc, RwLock}; use jsonrpc_core::IoHandler; -use util::Lockable; +use util::{Lockable, RwLockable}; use util::hash::{Address, H256, FixedHash}; use util::numbers::{Uint, U256}; use ethcore::account_provider::AccountProvider; @@ -104,13 +104,13 @@ fn rpc_eth_syncing() { assert_eq!(tester.io.handle_request(request), Some(false_res.to_owned())); { - let mut status = tester.sync.status.write().unwrap(); + let mut status = tester.sync.status.unwrapped_write(); status.state = SyncState::Blocks; status.highest_block_number = Some(2500); // "sync" to 1000 blocks. // causes TestBlockChainClient to return 1000 for its best block number. - let mut blocks = tester.client.blocks.write().unwrap(); + let mut blocks = tester.client.blocks.unwrapped_write(); for i in 0..1000 { blocks.insert(H256::from(i), Vec::new()); } @@ -121,7 +121,7 @@ fn rpc_eth_syncing() { { // finish "syncing" - let mut blocks = tester.client.blocks.write().unwrap(); + let mut blocks = tester.client.blocks.unwrapped_write(); for i in 0..1500 { blocks.insert(H256::from(i + 1000), Vec::new()); } @@ -133,9 +133,9 @@ fn rpc_eth_syncing() { #[test] fn rpc_eth_hashrate() { let tester = EthTester::default(); - tester.hashrates.write().unwrap().insert(H256::from(0), U256::from(0xfffa)); - tester.hashrates.write().unwrap().insert(H256::from(0), U256::from(0xfffb)); - tester.hashrates.write().unwrap().insert(H256::from(1), U256::from(0x1)); + tester.hashrates.unwrapped_write().insert(H256::from(0), U256::from(0xfffa)); + tester.hashrates.unwrapped_write().insert(H256::from(0), U256::from(0xfffb)); + tester.hashrates.unwrapped_write().insert(H256::from(1), U256::from(0x1)); let request = r#"{"jsonrpc": "2.0", "method": "eth_hashrate", "params": [], "id": 1}"#; let response = r#"{"jsonrpc":"2.0","result":"0xfffc","id":1}"#; @@ -158,7 +158,7 @@ fn rpc_eth_submit_hashrate() { let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#; assert_eq!(tester.io.handle_request(request), Some(response.to_owned())); - assert_eq!(tester.hashrates.read().unwrap().get(&H256::from("0x59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c")).cloned(), + assert_eq!(tester.hashrates.unwrapped_read().get(&H256::from("0x59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c")).cloned(), Some(U256::from(0x500_000))); } @@ -215,7 +215,7 @@ fn rpc_eth_mining() { let response = r#"{"jsonrpc":"2.0","result":false,"id":1}"#; assert_eq!(tester.io.handle_request(request), Some(response.to_owned())); - tester.hashrates.write().unwrap().insert(H256::from(1), U256::from(0x1)); + tester.hashrates.unwrapped_write().insert(H256::from(1), U256::from(0x1)); let request = r#"{"jsonrpc": "2.0", "method": "eth_mining", "params": [], "id": 1}"#; let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#; @@ -591,7 +591,7 @@ fn rpc_eth_send_transaction() { assert_eq!(tester.io.handle_request(&request), Some(response)); - tester.miner.last_nonces.write().unwrap().insert(address.clone(), U256::zero()); + tester.miner.last_nonces.unwrapped_write().insert(address.clone(), U256::zero()); let t = Transaction { nonce: U256::one(), @@ -749,7 +749,7 @@ fn returns_error_if_can_mine_and_no_closed_block() { use ethsync::{SyncState}; let eth_tester = EthTester::default(); - eth_tester.sync.status.write().unwrap().state = SyncState::Idle; + eth_tester.sync.status.unwrapped_write().state = SyncState::Idle; let request = r#"{"jsonrpc": "2.0", "method": "eth_getWork", "params": [], "id": 1}"#; let response = r#"{"jsonrpc":"2.0","error":{"code":-32603,"message":"Internal error","data":null},"id":1}"#; diff --git a/rpc/src/v1/tests/mocked/personal.rs b/rpc/src/v1/tests/mocked/personal.rs index cca58c1d7..6cd3ae583 100644 --- a/rpc/src/v1/tests/mocked/personal.rs +++ b/rpc/src/v1/tests/mocked/personal.rs @@ -18,6 +18,7 @@ use std::sync::Arc; use std::str::FromStr; use jsonrpc_core::IoHandler; use util::numbers::*; +use util::RwLockable; use ethcore::account_provider::AccountProvider; use v1::{PersonalClient, Personal}; use v1::tests::helpers::TestMinerService; @@ -174,7 +175,7 @@ fn sign_and_send_transaction() { assert_eq!(tester.io.handle_request(request.as_ref()), Some(response)); - tester.miner.last_nonces.write().unwrap().insert(address.clone(), U256::zero()); + tester.miner.last_nonces.unwrapped_write().insert(address.clone(), U256::zero()); let t = Transaction { nonce: U256::one(), diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 6845c32e3..c96ba8b34 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -1142,7 +1142,7 @@ impl ChainSync { |e| format!("Error sending nodes: {:?}", e)), _ => { - sync.write().unwrap().on_packet(io, peer, packet_id, data); + sync.unwrapped_write().on_packet(io, peer, packet_id, data); Ok(()) } }; diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 93e4f4382..dfb11ee0d 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -71,7 +71,7 @@ extern crate heapsize; use std::ops::*; use std::sync::*; use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId}; -use util::{TimerToken, U256}; +use util::{TimerToken, U256, RwLockable}; use ethcore::client::Client; use ethcore::service::{SyncMessage, NetSyncMessage}; use io::NetSyncIo; @@ -143,28 +143,28 @@ impl EthSync { /// Stop sync pub fn stop(&mut self, io: &mut NetworkContext) { - self.sync.write().unwrap().abort(&mut NetSyncIo::new(io, self.chain.deref())); + self.sync.unwrapped_write().abort(&mut NetSyncIo::new(io, self.chain.deref())); } /// Restart sync pub fn restart(&mut self, io: &mut NetworkContext) { - self.sync.write().unwrap().restart(&mut NetSyncIo::new(io, self.chain.deref())); + self.sync.unwrapped_write().restart(&mut NetSyncIo::new(io, self.chain.deref())); } } impl SyncProvider for EthSync { /// Get sync status fn status(&self) -> SyncStatus { - self.sync.read().unwrap().status() + self.sync.unwrapped_read().status() } fn start_network(&self) { - self.io_channel.read().unwrap().send(NetworkIoMessage::User(SyncMessage::StartNetwork)) + self.io_channel.unwrapped_read().send(NetworkIoMessage::User(SyncMessage::StartNetwork)) .unwrap_or_else(|e| warn!("Error sending IO notification: {:?}", e)); } fn stop_network(&self) { - self.io_channel.read().unwrap().send(NetworkIoMessage::User(SyncMessage::StopNetwork)) + self.io_channel.unwrapped_read().send(NetworkIoMessage::User(SyncMessage::StopNetwork)) .unwrap_or_else(|e| warn!("Error sending IO notification: {:?}", e)); } } @@ -172,7 +172,7 @@ impl SyncProvider for EthSync { impl NetworkProtocolHandler for EthSync { fn initialize(&self, io: &NetworkContext) { io.register_timer(0, 1000).expect("Error registering sync timer"); - *self.io_channel.write().unwrap() = io.io_channel(); + *self.io_channel.unwrapped_write() = io.io_channel(); } fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) { @@ -180,16 +180,16 @@ impl NetworkProtocolHandler for EthSync { } fn connected(&self, io: &NetworkContext, peer: &PeerId) { - self.sync.write().unwrap().on_peer_connected(&mut NetSyncIo::new(io, self.chain.deref()), *peer); + self.sync.unwrapped_write().on_peer_connected(&mut NetSyncIo::new(io, self.chain.deref()), *peer); } fn disconnected(&self, io: &NetworkContext, peer: &PeerId) { - self.sync.write().unwrap().on_peer_aborting(&mut NetSyncIo::new(io, self.chain.deref()), *peer); + self.sync.unwrapped_write().on_peer_aborting(&mut NetSyncIo::new(io, self.chain.deref()), *peer); } fn timeout(&self, io: &NetworkContext, _timer: TimerToken) { - self.sync.write().unwrap().maintain_peers(&mut NetSyncIo::new(io, self.chain.deref())); - self.sync.write().unwrap().maintain_sync(&mut NetSyncIo::new(io, self.chain.deref())); + self.sync.unwrapped_write().maintain_peers(&mut NetSyncIo::new(io, self.chain.deref())); + self.sync.unwrapped_write().maintain_sync(&mut NetSyncIo::new(io, self.chain.deref())); } #[cfg_attr(feature="dev", allow(single_match))] @@ -197,7 +197,7 @@ impl NetworkProtocolHandler for EthSync { match *message { SyncMessage::NewChainBlocks { ref imported, ref invalid, ref enacted, ref retracted, ref sealed } => { let mut sync_io = NetSyncIo::new(io, self.chain.deref()); - self.sync.write().unwrap().chain_new_blocks(&mut sync_io, imported, invalid, enacted, retracted, sealed); + self.sync.unwrapped_write().chain_new_blocks(&mut sync_io, imported, invalid, enacted, retracted, sealed); }, _ => {/* Ignore other messages */}, } diff --git a/sync/src/tests/chain.rs b/sync/src/tests/chain.rs index 2f2bde171..e5a8b3fb9 100644 --- a/sync/src/tests/chain.rs +++ b/sync/src/tests/chain.rs @@ -27,7 +27,7 @@ fn two_peers() { net.peer_mut(2).chain.add_blocks(1000, EachBlockWith::Uncle); net.sync(); assert!(net.peer(0).chain.block(BlockID::Number(1000)).is_some()); - assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref()); + assert_eq!(net.peer(0).chain.blocks.unwrapped_read().deref(), net.peer(1).chain.blocks.unwrapped_read().deref()); } #[test] @@ -37,7 +37,7 @@ fn long_chain() { net.peer_mut(1).chain.add_blocks(50000, EachBlockWith::Nothing); net.sync(); assert!(net.peer(0).chain.block(BlockID::Number(50000)).is_some()); - assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref()); + assert_eq!(net.peer(0).chain.blocks.unwrapped_read().deref(), net.peer(1).chain.blocks.unwrapped_read().deref()); } #[test] @@ -47,7 +47,7 @@ fn status_after_sync() { net.peer_mut(1).chain.add_blocks(1000, EachBlockWith::Uncle); net.peer_mut(2).chain.add_blocks(1000, EachBlockWith::Uncle); net.sync(); - let status = net.peer(0).sync.read().unwrap().status(); + let status = net.peer(0).sync.unwrapped_read().status(); assert_eq!(status.state, SyncState::Idle); } @@ -71,7 +71,7 @@ fn empty_blocks() { } net.sync(); assert!(net.peer(0).chain.block(BlockID::Number(1000)).is_some()); - assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref()); + assert_eq!(net.peer(0).chain.blocks.unwrapped_read().deref(), net.peer(1).chain.blocks.unwrapped_read().deref()); } #[test] @@ -87,12 +87,12 @@ fn forked() { net.peer_mut(1).chain.add_blocks(100, EachBlockWith::Uncle); //fork between 1 and 2 net.peer_mut(2).chain.add_blocks(10, EachBlockWith::Nothing); // peer 1 has the best chain of 601 blocks - let peer1_chain = net.peer(1).chain.numbers.read().unwrap().clone(); + let peer1_chain = net.peer(1).chain.numbers.unwrapped_read().clone(); net.sync(); - assert_eq!(net.peer(0).chain.difficulty.read().unwrap().deref(), net.peer(1).chain.difficulty.read().unwrap().deref()); - assert_eq!(net.peer(0).chain.numbers.read().unwrap().deref(), &peer1_chain); - assert_eq!(net.peer(1).chain.numbers.read().unwrap().deref(), &peer1_chain); - assert_eq!(net.peer(2).chain.numbers.read().unwrap().deref(), &peer1_chain); + assert_eq!(net.peer(0).chain.difficulty.unwrapped_read().deref(), net.peer(1).chain.difficulty.unwrapped_read().deref()); + assert_eq!(net.peer(0).chain.numbers.unwrapped_read().deref(), &peer1_chain); + assert_eq!(net.peer(1).chain.numbers.unwrapped_read().deref(), &peer1_chain); + assert_eq!(net.peer(2).chain.numbers.unwrapped_read().deref(), &peer1_chain); } #[test] @@ -107,14 +107,14 @@ fn restart() { assert!(net.peer(0).chain.chain_info().best_block_number > 100); net.restart_peer(0); - let status = net.peer(0).sync.read().unwrap().status(); + let status = net.peer(0).sync.unwrapped_read().status(); assert_eq!(status.state, SyncState::ChainHead); } #[test] fn status_empty() { let net = TestNet::new(2); - assert_eq!(net.peer(0).sync.read().unwrap().status().state, SyncState::Idle); + assert_eq!(net.peer(0).sync.unwrapped_read().status().state, SyncState::Idle); } #[test] diff --git a/sync/src/tests/helpers.rs b/sync/src/tests/helpers.rs index 9a9afca49..f3a5c6c4c 100644 --- a/sync/src/tests/helpers.rs +++ b/sync/src/tests/helpers.rs @@ -118,7 +118,7 @@ impl TestNet { for client in 0..self.peers.len() { if peer != client { let mut p = self.peers.get_mut(peer).unwrap(); - p.sync.write().unwrap().on_peer_connected(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(client as PeerId)), client as PeerId); + p.sync.unwrapped_write().on_peer_connected(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(client as PeerId)), client as PeerId); } } } @@ -133,18 +133,18 @@ impl TestNet { trace!("----------------"); } let mut p = self.peers.get_mut(peer).unwrap(); - p.sync.write().unwrap().maintain_sync(&mut TestIo::new(&mut p.chain, &mut p.queue, None)); + p.sync.unwrapped_write().maintain_sync(&mut TestIo::new(&mut p.chain, &mut p.queue, None)); } } pub fn sync_step_peer(&mut self, peer_num: usize) { let mut peer = self.peer_mut(peer_num); - peer.sync.write().unwrap().maintain_sync(&mut TestIo::new(&mut peer.chain, &mut peer.queue, None)); + peer.sync.unwrapped_write().maintain_sync(&mut TestIo::new(&mut peer.chain, &mut peer.queue, None)); } pub fn restart_peer(&mut self, i: usize) { let peer = self.peer_mut(i); - peer.sync.write().unwrap().restart(&mut TestIo::new(&mut peer.chain, &mut peer.queue, None)); + peer.sync.unwrapped_write().restart(&mut TestIo::new(&mut peer.chain, &mut peer.queue, None)); } pub fn sync(&mut self) -> u32 { @@ -173,6 +173,6 @@ impl TestNet { pub fn trigger_chain_new_blocks(&mut self, peer_id: usize) { let mut peer = self.peer_mut(peer_id); - peer.sync.write().unwrap().chain_new_blocks(&mut TestIo::new(&mut peer.chain, &mut peer.queue, None), &[], &[], &[], &[], &[]); + peer.sync.unwrapped_write().chain_new_blocks(&mut TestIo::new(&mut peer.chain, &mut peer.queue, None), &[], &[], &[], &[], &[]); } } diff --git a/util/src/io/service.rs b/util/src/io/service.rs index 1e2169808..7f80d50d7 100644 --- a/util/src/io/service.rs +++ b/util/src/io/service.rs @@ -21,6 +21,7 @@ use mio::*; use crossbeam::sync::chase_lev; use slab::Slab; use error::*; +use misc::*; use io::{IoError, IoHandler}; use io::worker::{Worker, Work, WorkType}; use panics::*; @@ -227,7 +228,7 @@ impl Handler for IoManager where Message: Send + Clone + Sync let handler_index = token.as_usize() / TOKENS_PER_HANDLER; let token_id = token.as_usize() % TOKENS_PER_HANDLER; if let Some(handler) = self.handlers.get(handler_index) { - if let Some(timer) = self.timers.read().unwrap().get(&token.as_usize()) { + if let Some(timer) = self.timers.unwrapped_read().get(&token.as_usize()) { event_loop.timeout_ms(token, timer.delay).expect("Error re-registering user timer"); self.worker_channel.push(Work { work_type: WorkType::Timeout, token: token_id, handler: handler.clone(), handler_id: handler_index }); self.work_ready.notify_all(); @@ -249,7 +250,7 @@ impl Handler for IoManager where Message: Send + Clone + Sync // TODO: flush event loop self.handlers.remove(handler_id); // unregister timers - let mut timers = self.timers.write().unwrap(); + let mut timers = self.timers.unwrapped_write(); let to_remove: Vec<_> = timers.keys().cloned().filter(|timer_id| timer_id / TOKENS_PER_HANDLER == handler_id).collect(); for timer_id in to_remove { let timer = timers.remove(&timer_id).expect("to_remove only contains keys from timers; qed"); @@ -259,11 +260,11 @@ impl Handler for IoManager where Message: Send + Clone + Sync IoMessage::AddTimer { handler_id, token, delay } => { let timer_id = token + handler_id * TOKENS_PER_HANDLER; let timeout = event_loop.timeout_ms(Token(timer_id), delay).expect("Error registering user timer"); - self.timers.write().unwrap().insert(timer_id, UserTimer { delay: delay, timeout: timeout }); + self.timers.unwrapped_write().insert(timer_id, UserTimer { delay: delay, timeout: timeout }); }, IoMessage::RemoveTimer { handler_id, token } => { let timer_id = token + handler_id * TOKENS_PER_HANDLER; - if let Some(timer) = self.timers.write().unwrap().remove(&timer_id) { + if let Some(timer) = self.timers.unwrapped_write().remove(&timer_id) { event_loop.clear_timeout(timer.timeout); } }, @@ -277,7 +278,7 @@ impl Handler for IoManager where Message: Send + Clone + Sync handler.deregister_stream(token, event_loop); // unregister a timer associated with the token (if any) let timer_id = token + handler_id * TOKENS_PER_HANDLER; - if let Some(timer) = self.timers.write().unwrap().remove(&timer_id) { + if let Some(timer) = self.timers.unwrapped_write().remove(&timer_id) { event_loop.clear_timeout(timer.timeout); } } diff --git a/util/src/journaldb/earlymergedb.rs b/util/src/journaldb/earlymergedb.rs index b860ae598..15f6a28b5 100644 --- a/util/src/journaldb/earlymergedb.rs +++ b/util/src/journaldb/earlymergedb.rs @@ -20,6 +20,7 @@ use common::*; use rlp::*; use hashdb::*; use memorydb::*; +use misc::RwLockable; use super::{DB_PREFIX_LEN, LATEST_ERA_KEY, VERSION_KEY}; use super::traits::JournalDB; use kvdb::{Database, DBTransaction, DatabaseConfig}; @@ -225,7 +226,7 @@ impl EarlyMergeDB { #[cfg(test)] fn can_reconstruct_refs(&self) -> bool { let (latest_era, reconstructed) = Self::read_refs(&self.backing); - let refs = self.refs.as_ref().unwrap().write().unwrap(); + let refs = self.refs.as_ref().unwrap().unwrapped_write(); if *refs != reconstructed || latest_era != self.latest_era { let clean_refs = refs.iter().filter_map(|(k, v)| if reconstructed.get(k) == Some(v) {None} else {Some((k.clone(), v.clone()))}).collect::>(); let clean_recon = reconstructed.into_iter().filter_map(|(k, v)| if refs.get(&k) == Some(&v) {None} else {Some((k.clone(), v.clone()))}).collect::>(); @@ -333,7 +334,7 @@ impl JournalDB for EarlyMergeDB { fn mem_used(&self) -> usize { self.overlay.mem_used() + match self.refs { - Some(ref c) => c.read().unwrap().heap_size_of_children(), + Some(ref c) => c.unwrapped_read().heap_size_of_children(), None => 0 } } @@ -385,7 +386,7 @@ impl JournalDB for EarlyMergeDB { // // record new commit's details. - let mut refs = self.refs.as_ref().unwrap().write().unwrap(); + let mut refs = self.refs.as_ref().unwrap().unwrapped_write(); let batch = DBTransaction::new(); let trace = false; { diff --git a/util/src/journaldb/overlayrecentdb.rs b/util/src/journaldb/overlayrecentdb.rs index 7afe7e5ac..5c60e22b5 100644 --- a/util/src/journaldb/overlayrecentdb.rs +++ b/util/src/journaldb/overlayrecentdb.rs @@ -20,6 +20,7 @@ use common::*; use rlp::*; use hashdb::*; use memorydb::*; +use misc::RwLockable; use super::{DB_PREFIX_LEN, LATEST_ERA_KEY, VERSION_KEY}; use kvdb::{Database, DBTransaction, DatabaseConfig}; #[cfg(test)] @@ -136,7 +137,7 @@ impl OverlayRecentDB { #[cfg(test)] fn can_reconstruct_refs(&self) -> bool { let reconstructed = Self::read_overlay(&self.backing); - let journal_overlay = self.journal_overlay.read().unwrap(); + let journal_overlay = self.journal_overlay.unwrapped_read(); *journal_overlay == reconstructed } @@ -199,7 +200,7 @@ impl JournalDB for OverlayRecentDB { fn mem_used(&self) -> usize { let mut mem = self.transaction_overlay.mem_used(); - let overlay = self.journal_overlay.read().unwrap(); + let overlay = self.journal_overlay.unwrapped_read(); mem += overlay.backing_overlay.mem_used(); mem += overlay.journal.heap_size_of_children(); mem @@ -209,12 +210,12 @@ impl JournalDB for OverlayRecentDB { self.backing.get(&LATEST_ERA_KEY).expect("Low level database error").is_none() } - fn latest_era(&self) -> Option { self.journal_overlay.read().unwrap().latest_era } + fn latest_era(&self) -> Option { self.journal_overlay.unwrapped_read().latest_era } fn commit(&mut self, now: u64, id: &H256, end: Option<(u64, H256)>) -> Result { // record new commit's details. trace!("commit: #{} ({}), end era: {:?}", now, id, end); - let mut journal_overlay = self.journal_overlay.write().unwrap(); + let mut journal_overlay = self.journal_overlay.unwrapped_write(); let batch = DBTransaction::new(); { let mut r = RlpStream::new_list(3); @@ -321,7 +322,7 @@ impl HashDB for OverlayRecentDB { match k { Some(&(ref d, rc)) if rc > 0 => Some(d), _ => { - let v = self.journal_overlay.read().unwrap().backing_overlay.get(key).map(|v| v.to_vec()); + let v = self.journal_overlay.unwrapped_read().backing_overlay.get(key).map(|v| v.to_vec()); match v { Some(x) => { Some(&self.transaction_overlay.denote(key, x).0) diff --git a/util/src/log.rs b/util/src/log.rs index ed3a5376d..be441384e 100644 --- a/util/src/log.rs +++ b/util/src/log.rs @@ -23,6 +23,7 @@ use env_logger::LogBuilder; use std::sync::{RwLock, RwLockReadGuard}; use std::sync::atomic::{Ordering, AtomicBool}; use arrayvec::ArrayVec; +use misc::RwLockable; pub use ansi_term::{Colour, Style}; lazy_static! { @@ -90,7 +91,7 @@ impl RotatingLogger { /// Append new log entry pub fn append(&self, log: String) { - self.logs.write().unwrap().insert(0, log); + self.logs.unwrapped_write().insert(0, log); } /// Return levels @@ -100,7 +101,7 @@ impl RotatingLogger { /// Return logs pub fn logs(&self) -> RwLockReadGuard> { - self.logs.read().unwrap() + self.logs.unwrapped_read() } } diff --git a/util/src/misc.rs b/util/src/misc.rs index 3d4df8b94..c56bcb85a 100644 --- a/util/src/misc.rs +++ b/util/src/misc.rs @@ -75,3 +75,18 @@ pub trait Lockable { impl Lockable for Mutex { fn locked(&self) -> MutexGuard { self.lock().unwrap() } } + +/// Object can be read or write locked directly into a guard. +pub trait RwLockable { + /// Read-lock object directly into a `ReadGuard`. + fn unwrapped_read(&self) -> RwLockReadGuard; + + /// Write-lock object directly into a `WriteGuard`. + fn unwrapped_write(&self) -> RwLockWriteGuard; +} + +impl RwLockable for RwLock { + fn unwrapped_read(&self) -> RwLockReadGuard { self.read().unwrap() } + fn unwrapped_write(&self) -> RwLockWriteGuard { self.write().unwrap() } +} + diff --git a/util/src/network/host.rs b/util/src/network/host.rs index c85b4cc57..0caaef74c 100644 --- a/util/src/network/host.rs +++ b/util/src/network/host.rs @@ -216,7 +216,7 @@ impl<'s, Message> NetworkContext<'s, Message> where Message: Send + Sync + Clone fn resolve_session(&self, peer: PeerId) -> Option { match self.session_id { Some(id) if id == peer => self.session.clone(), - _ => self.sessions.read().unwrap().get(peer).cloned(), + _ => self.sessions.unwrapped_read().get(peer).cloned(), } } @@ -422,7 +422,7 @@ impl Host where Message: Send + Sync + Clone { Ok(n) => { let entry = NodeEntry { endpoint: n.endpoint.clone(), id: n.id.clone() }; - self.nodes.write().unwrap().add_node(n); + self.nodes.unwrapped_write().add_node(n); if let Some(ref mut discovery) = *self.discovery.locked() { discovery.add_node(entry); } @@ -434,7 +434,7 @@ impl Host where Message: Send + Sync + Clone { let n = try!(Node::from_str(id)); let entry = NodeEntry { endpoint: n.endpoint.clone(), id: n.id.clone() }; - self.reserved_nodes.write().unwrap().insert(n.id.clone()); + self.reserved_nodes.unwrapped_write().insert(n.id.clone()); if let Some(ref mut discovery) = *self.discovery.locked() { discovery.add_node(entry); @@ -444,16 +444,16 @@ impl Host where Message: Send + Sync + Clone { } pub fn set_non_reserved_mode(&self, mode: NonReservedPeerMode, io: &IoContext>) { - let mut info = self.info.write().unwrap(); + let mut info = self.info.unwrapped_write(); if info.config.non_reserved_mode != mode { info.config.non_reserved_mode = mode.clone(); drop(info); if let NonReservedPeerMode::Deny = mode { // disconnect all non-reserved peers here. - let reserved: HashSet = self.reserved_nodes.read().unwrap().clone(); + let reserved: HashSet = self.reserved_nodes.unwrapped_read().clone(); let mut to_kill = Vec::new(); - for e in self.sessions.write().unwrap().iter_mut() { + for e in self.sessions.unwrapped_write().iter_mut() { let mut s = e.locked(); { let id = s.id(); @@ -475,7 +475,7 @@ impl Host where Message: Send + Sync + Clone { pub fn remove_reserved_node(&self, id: &str) -> Result<(), UtilError> { let n = try!(Node::from_str(id)); - self.reserved_nodes.write().unwrap().remove(&n.id); + self.reserved_nodes.unwrapped_write().remove(&n.id); Ok(()) } @@ -485,11 +485,11 @@ impl Host where Message: Send + Sync + Clone { } pub fn external_url(&self) -> Option { - self.info.read().unwrap().public_endpoint.as_ref().map(|e| format!("{}", Node::new(self.info.read().unwrap().id().clone(), e.clone()))) + self.info.unwrapped_read().public_endpoint.as_ref().map(|e| format!("{}", Node::new(self.info.unwrapped_read().id().clone(), e.clone()))) } pub fn local_url(&self) -> String { - let r = format!("{}", Node::new(self.info.read().unwrap().id().clone(), self.info.read().unwrap().local_endpoint.clone())); + let r = format!("{}", Node::new(self.info.unwrapped_read().id().clone(), self.info.unwrapped_read().local_endpoint.clone())); println!("{}", r); r } @@ -497,7 +497,7 @@ impl Host where Message: Send + Sync + Clone { pub fn stop(&self, io: &IoContext>) -> Result<(), UtilError> { self.stopping.store(true, AtomicOrdering::Release); let mut to_kill = Vec::new(); - for e in self.sessions.write().unwrap().iter_mut() { + for e in self.sessions.unwrapped_write().iter_mut() { let mut s = e.locked(); s.disconnect(io, DisconnectReason::ClientQuit); to_kill.push(s.token()); @@ -512,16 +512,16 @@ impl Host where Message: Send + Sync + Clone { fn init_public_interface(&self, io: &IoContext>) -> Result<(), UtilError> { io.clear_timer(INIT_PUBLIC).unwrap(); - if self.info.read().unwrap().public_endpoint.is_some() { + if self.info.unwrapped_read().public_endpoint.is_some() { return Ok(()); } - let local_endpoint = self.info.read().unwrap().local_endpoint.clone(); - let public_address = self.info.read().unwrap().config.public_address.clone(); + let local_endpoint = self.info.unwrapped_read().local_endpoint.clone(); + let public_address = self.info.unwrapped_read().config.public_address.clone(); let public_endpoint = match public_address { None => { let public_address = select_public_address(local_endpoint.address.port()); let public_endpoint = NodeEndpoint { address: public_address, udp_port: local_endpoint.udp_port }; - if self.info.read().unwrap().config.nat_enabled { + if self.info.unwrapped_read().config.nat_enabled { match map_external_address(&local_endpoint) { Some(endpoint) => { info!("NAT mapped to external address {}", endpoint.address); @@ -536,7 +536,7 @@ impl Host where Message: Send + Sync + Clone { Some(addr) => NodeEndpoint { address: addr, udp_port: local_endpoint.udp_port } }; - self.info.write().unwrap().public_endpoint = Some(public_endpoint.clone()); + self.info.unwrapped_write().public_endpoint = Some(public_endpoint.clone()); if let Some(url) = self.external_url() { io.message(NetworkIoMessage::NetworkStarted(url)).unwrap_or_else(|e| warn!("Error sending IO notification: {:?}", e)); @@ -544,15 +544,15 @@ impl Host where Message: Send + Sync + Clone { // Initialize discovery. let discovery = { - let info = self.info.read().unwrap(); + let info = self.info.unwrapped_read(); if info.config.discovery_enabled && info.config.non_reserved_mode == NonReservedPeerMode::Accept { Some(Discovery::new(&info.keys, public_endpoint.address.clone(), public_endpoint, DISCOVERY)) } else { None } }; if let Some(mut discovery) = discovery { - discovery.init_node_list(self.nodes.read().unwrap().unordered_entries()); - for n in self.nodes.read().unwrap().unordered_entries() { + discovery.init_node_list(self.nodes.unwrapped_read().unordered_entries()); + for n in self.nodes.unwrapped_read().unordered_entries() { discovery.add_node(n.clone()); } *self.discovery.locked() = Some(discovery); @@ -571,7 +571,7 @@ impl Host where Message: Send + Sync + Clone { } fn have_session(&self, id: &NodeId) -> bool { - self.sessions.read().unwrap().iter().any(|e| e.locked().info.id == Some(id.clone())) + self.sessions.unwrapped_read().iter().any(|e| e.locked().info.id == Some(id.clone())) } fn session_count(&self) -> usize { @@ -579,16 +579,16 @@ impl Host where Message: Send + Sync + Clone { } fn connecting_to(&self, id: &NodeId) -> bool { - self.sessions.read().unwrap().iter().any(|e| e.locked().id() == Some(id)) + self.sessions.unwrapped_read().iter().any(|e| e.locked().id() == Some(id)) } fn handshake_count(&self) -> usize { - self.sessions.read().unwrap().count() - self.session_count() + self.sessions.unwrapped_read().count() - self.session_count() } fn keep_alive(&self, io: &IoContext>) { let mut to_kill = Vec::new(); - for e in self.sessions.write().unwrap().iter_mut() { + for e in self.sessions.unwrapped_write().iter_mut() { let mut s = e.locked(); if !s.keep_alive(io) { s.disconnect(io, DisconnectReason::PingTimeout); @@ -603,7 +603,7 @@ impl Host where Message: Send + Sync + Clone { fn connect_peers(&self, io: &IoContext>) { let (ideal_peers, mut pin) = { - let info = self.info.read().unwrap(); + let info = self.info.unwrapped_read(); if info.capabilities.is_empty() { return; } @@ -613,7 +613,7 @@ impl Host where Message: Send + Sync + Clone { }; let session_count = self.session_count(); - let reserved_nodes = self.reserved_nodes.read().unwrap(); + let reserved_nodes = self.reserved_nodes.unwrapped_read(); if session_count >= ideal_peers as usize + reserved_nodes.len() { // check if all pinned nodes are connected. if reserved_nodes.iter().all(|n| self.have_session(n) && self.connecting_to(n)) { @@ -634,7 +634,7 @@ impl Host where Message: Send + Sync + Clone { // iterate over all nodes, reserved ones coming first. // if we are pinned to only reserved nodes, ignore all others. let nodes = reserved_nodes.iter().cloned().chain(if !pin { - self.nodes.read().unwrap().nodes() + self.nodes.unwrapped_read().nodes() } else { Vec::new() }); @@ -662,7 +662,7 @@ impl Host where Message: Send + Sync + Clone { let socket = { let address = { - let mut nodes = self.nodes.write().unwrap(); + let mut nodes = self.nodes.unwrapped_write(); if let Some(node) = nodes.get_mut(id) { node.last_attempted = Some(::time::now()); node.endpoint.address @@ -687,10 +687,10 @@ impl Host where Message: Send + Sync + Clone { #[cfg_attr(feature="dev", allow(block_in_if_condition_stmt))] fn create_connection(&self, socket: TcpStream, id: Option<&NodeId>, io: &IoContext>) -> Result<(), UtilError> { - let nonce = self.info.write().unwrap().next_nonce(); - let mut sessions = self.sessions.write().unwrap(); + let nonce = self.info.unwrapped_write().next_nonce(); + let mut sessions = self.sessions.unwrapped_write(); let token = sessions.insert_with_opt(|token| { - match Session::new(io, socket, token, id, &nonce, self.stats.clone(), &self.info.read().unwrap()) { + match Session::new(io, socket, token, id, &nonce, self.stats.clone(), &self.info.unwrapped_read()) { Ok(s) => Some(Arc::new(Mutex::new(s))), Err(e) => { debug!(target: "network", "Session create error: {:?}", e); @@ -726,10 +726,10 @@ impl Host where Message: Send + Sync + Clone { } fn session_writable(&self, token: StreamToken, io: &IoContext>) { - let session = { self.sessions.read().unwrap().get(token).cloned() }; + let session = { self.sessions.unwrapped_read().get(token).cloned() }; if let Some(session) = session { let mut s = session.locked(); - if let Err(e) = s.writable(io, &self.info.read().unwrap()) { + if let Err(e) = s.writable(io, &self.info.unwrapped_read()) { trace!(target: "network", "Session write error: {}: {:?}", token, e); } if s.done() { @@ -748,16 +748,16 @@ impl Host where Message: Send + Sync + Clone { let mut ready_data: Vec = Vec::new(); let mut packet_data: Vec<(ProtocolId, PacketId, Vec)> = Vec::new(); let mut kill = false; - let session = { self.sessions.read().unwrap().get(token).cloned() }; + let session = { self.sessions.unwrapped_read().get(token).cloned() }; if let Some(session) = session.clone() { let mut s = session.locked(); loop { - match s.readable(io, &self.info.read().unwrap()) { + match s.readable(io, &self.info.unwrapped_read()) { Err(e) => { trace!(target: "network", "Session read error: {}:{:?} ({:?}) {:?}", token, s.id(), s.remote_addr(), e); if let UtilError::Network(NetworkError::Disconnect(DisconnectReason::IncompatibleProtocol)) = e { if let Some(id) = s.id() { - self.nodes.write().unwrap().mark_as_useless(id); + self.nodes.unwrapped_write().mark_as_useless(id); } } kill = true; @@ -767,9 +767,9 @@ impl Host where Message: Send + Sync + Clone { self.num_sessions.fetch_add(1, AtomicOrdering::SeqCst); if !s.info.originated { let session_count = self.session_count(); - let reserved_nodes = self.reserved_nodes.read().unwrap(); + let reserved_nodes = self.reserved_nodes.unwrapped_read(); let (ideal_peers, reserved_only) = { - let info = self.info.read().unwrap(); + let info = self.info.unwrapped_read(); (info.config.ideal_peers, info.config.non_reserved_mode == NonReservedPeerMode::Deny) }; @@ -784,14 +784,14 @@ impl Host where Message: Send + Sync + Clone { // Add it no node table if let Ok(address) = s.remote_addr() { let entry = NodeEntry { id: s.id().unwrap().clone(), endpoint: NodeEndpoint { address: address, udp_port: address.port() } }; - self.nodes.write().unwrap().add_node(Node::new(entry.id.clone(), entry.endpoint.clone())); + self.nodes.unwrapped_write().add_node(Node::new(entry.id.clone(), entry.endpoint.clone())); let mut discovery = self.discovery.locked(); if let Some(ref mut discovery) = *discovery.deref_mut() { discovery.add_node(entry); } } } - for (p, _) in self.handlers.read().unwrap().iter() { + for (p, _) in self.handlers.unwrapped_read().iter() { if s.have_capability(p) { ready_data.push(p); } @@ -802,7 +802,7 @@ impl Host where Message: Send + Sync + Clone { protocol, packet_id, }) => { - match self.handlers.read().unwrap().get(protocol) { + match self.handlers.unwrapped_read().get(protocol) { None => { warn!(target: "network", "No handler found for protocol: {:?}", protocol) }, Some(_) => packet_data.push((protocol, packet_id, data)), } @@ -815,16 +815,16 @@ impl Host where Message: Send + Sync + Clone { if kill { self.kill_connection(token, io, true); } - let handlers = self.handlers.read().unwrap(); + let handlers = self.handlers.unwrapped_read(); for p in ready_data { let h = handlers.get(p).unwrap().clone(); self.stats.inc_sessions(); - let reserved = self.reserved_nodes.read().unwrap(); + let reserved = self.reserved_nodes.unwrapped_read(); h.connected(&NetworkContext::new(io, p, session.clone(), self.sessions.clone(), &reserved), &token); } for (p, packet_id, data) in packet_data { let h = handlers.get(p).unwrap().clone(); - let reserved = self.reserved_nodes.read().unwrap(); + let reserved = self.reserved_nodes.unwrapped_read(); h.read(&NetworkContext::new(io, p, session.clone(), self.sessions.clone(), &reserved), &token, packet_id, &data[1..]); } } @@ -840,14 +840,14 @@ impl Host where Message: Send + Sync + Clone { let mut deregister = false; let mut expired_session = None; if let FIRST_SESSION ... LAST_SESSION = token { - let sessions = self.sessions.write().unwrap(); + let sessions = self.sessions.unwrapped_write(); if let Some(session) = sessions.get(token).cloned() { expired_session = Some(session.clone()); let mut s = session.locked(); if !s.expired() { if s.is_ready() { self.num_sessions.fetch_sub(1, AtomicOrdering::SeqCst); - for (p, _) in self.handlers.read().unwrap().iter() { + for (p, _) in self.handlers.unwrapped_read().iter() { if s.have_capability(p) { to_disconnect.push(p); } @@ -861,12 +861,12 @@ impl Host where Message: Send + Sync + Clone { } if let Some(id) = failure_id { if remote { - self.nodes.write().unwrap().note_failure(&id); + self.nodes.unwrapped_write().note_failure(&id); } } for p in to_disconnect { - let h = self.handlers.read().unwrap().get(p).unwrap().clone(); - let reserved = self.reserved_nodes.read().unwrap(); + let h = self.handlers.unwrapped_read().get(p).unwrap().clone(); + let reserved = self.reserved_nodes.unwrapped_read(); h.disconnected(&NetworkContext::new(io, p, expired_session.clone(), self.sessions.clone(), &reserved), &token); } if deregister { @@ -877,7 +877,7 @@ impl Host where Message: Send + Sync + Clone { fn update_nodes(&self, io: &IoContext>, node_changes: TableUpdates) { let mut to_remove: Vec = Vec::new(); { - let sessions = self.sessions.write().unwrap(); + let sessions = self.sessions.unwrapped_write(); for c in sessions.iter() { let s = c.locked(); if let Some(id) = s.id() { @@ -891,7 +891,7 @@ impl Host where Message: Send + Sync + Clone { trace!(target: "network", "Removed from node table: {}", i); self.kill_connection(i, io, false); } - self.nodes.write().unwrap().update(node_changes); + self.nodes.unwrapped_write().update(node_changes); } } @@ -963,13 +963,13 @@ impl IoHandler> for Host where Messa }, NODE_TABLE => { trace!(target: "network", "Refreshing node table"); - self.nodes.write().unwrap().clear_useless(); + self.nodes.unwrapped_write().clear_useless(); }, - _ => match self.timers.read().unwrap().get(&token).cloned() { - Some(timer) => match self.handlers.read().unwrap().get(timer.protocol).cloned() { + _ => match self.timers.unwrapped_read().get(&token).cloned() { + Some(timer) => match self.handlers.unwrapped_read().get(timer.protocol).cloned() { None => { warn!(target: "network", "No handler found for protocol: {:?}", timer.protocol) }, Some(h) => { - let reserved = self.reserved_nodes.read().unwrap(); + let reserved = self.reserved_nodes.unwrapped_read(); h.timeout(&NetworkContext::new(io, timer.protocol, None, self.sessions.clone(), &reserved), timer.token); } }, @@ -989,10 +989,10 @@ impl IoHandler> for Host where Messa ref versions } => { let h = handler.clone(); - let reserved = self.reserved_nodes.read().unwrap(); + let reserved = self.reserved_nodes.unwrapped_read(); h.initialize(&NetworkContext::new(io, protocol, None, self.sessions.clone(), &reserved)); - self.handlers.write().unwrap().insert(protocol, h); - let mut info = self.info.write().unwrap(); + self.handlers.unwrapped_write().insert(protocol, h); + let mut info = self.info.unwrapped_write(); for v in versions { info.capabilities.push(CapabilityInfo { protocol: protocol, version: *v, packet_count:0 }); } @@ -1003,17 +1003,17 @@ impl IoHandler> for Host where Messa ref token, } => { let handler_token = { - let mut timer_counter = self.timer_counter.write().unwrap(); + let mut timer_counter = self.timer_counter.unwrapped_write(); let counter = &mut *timer_counter; let handler_token = *counter; *counter += 1; handler_token }; - self.timers.write().unwrap().insert(handler_token, ProtocolTimer { protocol: protocol, token: *token }); + self.timers.unwrapped_write().insert(handler_token, ProtocolTimer { protocol: protocol, token: *token }); io.register_timer(handler_token, *delay).unwrap_or_else(|e| debug!("Error registering timer {}: {:?}", token, e)); }, NetworkIoMessage::Disconnect(ref peer) => { - let session = { self.sessions.read().unwrap().get(*peer).cloned() }; + let session = { self.sessions.unwrapped_read().get(*peer).cloned() }; if let Some(session) = session { session.locked().disconnect(io, DisconnectReason::DisconnectRequested); } @@ -1021,19 +1021,19 @@ impl IoHandler> for Host where Messa self.kill_connection(*peer, io, false); }, NetworkIoMessage::DisablePeer(ref peer) => { - let session = { self.sessions.read().unwrap().get(*peer).cloned() }; + let session = { self.sessions.unwrapped_read().get(*peer).cloned() }; if let Some(session) = session { session.locked().disconnect(io, DisconnectReason::DisconnectRequested); if let Some(id) = session.locked().id() { - self.nodes.write().unwrap().mark_as_useless(id) + self.nodes.unwrapped_write().mark_as_useless(id) } } trace!(target: "network", "Disabling peer {}", peer); self.kill_connection(*peer, io, false); }, NetworkIoMessage::User(ref message) => { - let reserved = self.reserved_nodes.read().unwrap(); - for (p, h) in self.handlers.read().unwrap().iter() { + let reserved = self.reserved_nodes.unwrapped_read(); + for (p, h) in self.handlers.unwrapped_read().iter() { h.message(&NetworkContext::new(io, p, None, self.sessions.clone(), &reserved), &message); } } @@ -1044,7 +1044,7 @@ impl IoHandler> for Host where Messa fn register_stream(&self, stream: StreamToken, reg: Token, event_loop: &mut EventLoop>>) { match stream { FIRST_SESSION ... LAST_SESSION => { - let session = { self.sessions.read().unwrap().get(stream).cloned() }; + let session = { self.sessions.unwrapped_read().get(stream).cloned() }; if let Some(session) = session { session.locked().register_socket(reg, event_loop).expect("Error registering socket"); } @@ -1058,7 +1058,7 @@ impl IoHandler> for Host where Messa fn deregister_stream(&self, stream: StreamToken, event_loop: &mut EventLoop>>) { match stream { FIRST_SESSION ... LAST_SESSION => { - let mut connections = self.sessions.write().unwrap(); + let mut connections = self.sessions.unwrapped_write(); if let Some(connection) = connections.get(stream).cloned() { connection.locked().deregister_socket(event_loop).expect("Error deregistering socket"); connections.remove(stream); @@ -1072,7 +1072,7 @@ impl IoHandler> for Host where Messa fn update_stream(&self, stream: StreamToken, reg: Token, event_loop: &mut EventLoop>>) { match stream { FIRST_SESSION ... LAST_SESSION => { - let connection = { self.sessions.read().unwrap().get(stream).cloned() }; + let connection = { self.sessions.unwrapped_read().get(stream).cloned() }; if let Some(connection) = connection { connection.locked().update_socket(reg, event_loop).expect("Error updating socket"); } diff --git a/util/src/network/service.rs b/util/src/network/service.rs index 353a24bbe..711a8d860 100644 --- a/util/src/network/service.rs +++ b/util/src/network/service.rs @@ -17,6 +17,7 @@ use std::sync::*; use error::*; use panics::*; +use misc::RwLockable; use network::{NetworkProtocolHandler, NetworkConfiguration}; use network::error::NetworkError; use network::host::{Host, NetworkIoMessage, ProtocolId}; @@ -80,19 +81,19 @@ impl NetworkService where Message: Send + Sync + Clone + 'stat /// Returns external url if available. pub fn external_url(&self) -> Option { - let host = self.host.read().unwrap(); + let host = self.host.unwrapped_read(); host.as_ref().and_then(|h| h.external_url()) } /// Returns external url if available. pub fn local_url(&self) -> Option { - let host = self.host.read().unwrap(); + let host = self.host.unwrapped_read(); host.as_ref().map(|h| h.local_url()) } /// Start network IO pub fn start(&self) -> Result<(), UtilError> { - let mut host = self.host.write().unwrap(); + let mut host = self.host.unwrapped_write(); if host.is_none() { let h = Arc::new(try!(Host::new(self.config.clone(), self.stats.clone()))); try!(self.io_service.register_handler(h.clone())); @@ -103,7 +104,7 @@ impl NetworkService where Message: Send + Sync + Clone + 'stat /// Stop network IO pub fn stop(&self) -> Result<(), UtilError> { - let mut host = self.host.write().unwrap(); + let mut host = self.host.unwrapped_write(); if let Some(ref host) = *host { let io = IoContext::new(self.io_service.channel(), 0); //TODO: take token id from host try!(host.stop(&io)); @@ -114,7 +115,7 @@ impl NetworkService where Message: Send + Sync + Clone + 'stat /// Try to add a reserved peer. pub fn add_reserved_peer(&self, peer: &str) -> Result<(), UtilError> { - let host = self.host.read().unwrap(); + let host = self.host.unwrapped_read(); if let Some(ref host) = *host { host.add_reserved_node(peer) } else { @@ -124,7 +125,7 @@ impl NetworkService where Message: Send + Sync + Clone + 'stat /// Try to remove a reserved peer. pub fn remove_reserved_peer(&self, peer: &str) -> Result<(), UtilError> { - let host = self.host.read().unwrap(); + let host = self.host.unwrapped_read(); if let Some(ref host) = *host { host.remove_reserved_node(peer) } else { @@ -134,7 +135,7 @@ impl NetworkService where Message: Send + Sync + Clone + 'stat /// Set the non-reserved peer mode. pub fn set_non_reserved_mode(&self, mode: ::network::NonReservedPeerMode) { - let host = self.host.read().unwrap(); + let host = self.host.unwrapped_read(); if let Some(ref host) = *host { let io_ctxt = IoContext::new(self.io_service.channel(), 0); host.set_non_reserved_mode(mode, &io_ctxt); diff --git a/util/src/network/tests.rs b/util/src/network/tests.rs index 5b6c863cb..85591b462 100644 --- a/util/src/network/tests.rs +++ b/util/src/network/tests.rs @@ -18,6 +18,7 @@ use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::thread; use std::time::*; use common::*; +use misc::*; use network::*; use io::TimerToken; use crypto::KeyPair; diff --git a/util/src/panics.rs b/util/src/panics.rs index 368e6a9b4..7b6c92639 100644 --- a/util/src/panics.rs +++ b/util/src/panics.rs @@ -120,46 +120,49 @@ impl OnPanicListener for F #[ignore] // panic forwarding doesnt work on the same thread in beta fn should_notify_listeners_about_panic () { use std::sync::RwLock; + use misc::RwLockable; // given let invocations = Arc::new(RwLock::new(vec![])); let i = invocations.clone(); let p = PanicHandler::new(); - p.on_panic(move |t| i.write().unwrap().push(t)); + p.on_panic(move |t| i.unwrapped_write().push(t)); // when p.catch_panic(|| panic!("Panic!")).unwrap_err(); // then - assert!(invocations.read().unwrap()[0] == "Panic!"); + assert!(invocations.unwrapped_read()[0] == "Panic!"); } #[test] #[ignore] // panic forwarding doesnt work on the same thread in beta fn should_notify_listeners_about_panic_when_string_is_dynamic () { use std::sync::RwLock; + use misc::RwLockable; // given let invocations = Arc::new(RwLock::new(vec![])); let i = invocations.clone(); let p = PanicHandler::new(); - p.on_panic(move |t| i.write().unwrap().push(t)); + p.on_panic(move |t| i.unwrapped_write().push(t)); // when p.catch_panic(|| panic!("Panic: {}", 1)).unwrap_err(); // then - assert!(invocations.read().unwrap()[0] == "Panic: 1"); + assert!(invocations.unwrapped_read()[0] == "Panic: 1"); } #[test] fn should_notify_listeners_about_panic_in_other_thread () { use std::thread; use std::sync::RwLock; + use misc::RwLockable; // given let invocations = Arc::new(RwLock::new(vec![])); let i = invocations.clone(); let p = PanicHandler::new(); - p.on_panic(move |t| i.write().unwrap().push(t)); + p.on_panic(move |t| i.unwrapped_write().push(t)); // when let t = thread::spawn(move || @@ -168,18 +171,20 @@ fn should_notify_listeners_about_panic_in_other_thread () { t.join().unwrap_err(); // then - assert!(invocations.read().unwrap()[0] == "Panic!"); + assert!(invocations.unwrapped_read()[0] == "Panic!"); } #[test] #[ignore] // panic forwarding doesnt work on the same thread in beta fn should_forward_panics () { use std::sync::RwLock; + use misc::RwLockable; + // given let invocations = Arc::new(RwLock::new(vec![])); let i = invocations.clone(); let p = PanicHandler::new_in_arc(); - p.on_panic(move |t| i.write().unwrap().push(t)); + p.on_panic(move |t| i.unwrapped_write().push(t)); let p2 = PanicHandler::new(); p.forward_from(&p2); @@ -188,5 +193,5 @@ use std::sync::RwLock; p2.catch_panic(|| panic!("Panic!")).unwrap_err(); // then - assert!(invocations.read().unwrap()[0] == "Panic!"); + assert!(invocations.unwrapped_read()[0] == "Panic!"); } From d7077c8ef7688ccc88a8e4bed2e9d960bbf8078a Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 7 Jul 2016 11:28:33 +0200 Subject: [PATCH 03/16] Fix build. --- ethcore/src/client/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index ffdb42fba..043b35410 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -32,7 +32,7 @@ use util::network::*; use util::io::*; use util::rlp; use util::sha3::*; -use util::{Bytes}; +use util::{Bytes, Lockable, RwLockable}; use util::rlp::{RlpStream, Rlp, UntrustedRlp}; use util::journaldb; use util::journaldb::JournalDB; From eef9586c57694142fabf79a50b953185219d42b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Fri, 8 Jul 2016 17:19:14 +0200 Subject: [PATCH 04/16] Fixing deadlock in miner (#1569) * Fixing deadlock in miner * Adding more comments [ci skip] --- ethcore/src/miner/miner.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 0ee56dccb..7325b4a36 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -301,6 +301,10 @@ impl Miner { let have_work = self.sealing_work.lock().unwrap().peek_last_ref().is_some(); trace!(target: "miner", "enable_and_prepare_sealing: have_work={}", have_work); if !have_work { + // -------------------------------------------------------------------------- + // | NOTE Code below requires transaction_queue and sealing_work locks. | + // | Make sure to release the locks before calling that method. | + // -------------------------------------------------------------------------- self.sealing_enabled.store(true, atomic::Ordering::Relaxed); self.prepare_sealing(chain); } @@ -338,6 +342,10 @@ impl MinerService for Miner { fn clear_and_reset(&self, chain: &MiningBlockChainClient) { self.transaction_queue.lock().unwrap().clear(); + // -------------------------------------------------------------------------- + // | NOTE Code below requires transaction_queue and sealing_work locks. | + // | Make sure to release the locks before calling that method. | + // -------------------------------------------------------------------------- self.update_sealing(chain); } @@ -493,11 +501,18 @@ impl MinerService for Miner { fn import_external_transactions(&self, chain: &MiningBlockChainClient, transactions: Vec) -> Vec> { - let mut transaction_queue = self.transaction_queue.lock().unwrap(); - let results = self.add_transactions_to_queue(chain, transactions, TransactionOrigin::External, - &mut transaction_queue); + let results = { + let mut transaction_queue = self.transaction_queue.lock().unwrap(); + self.add_transactions_to_queue( + chain, transactions, TransactionOrigin::External, &mut transaction_queue + ) + }; if !results.is_empty() && self.options.reseal_on_external_tx && self.tx_reseal_allowed() { + // -------------------------------------------------------------------------- + // | NOTE Code below requires transaction_queue and sealing_work locks. | + // | Make sure to release the locks before calling that method. | + // -------------------------------------------------------------------------- self.update_sealing(chain); } results @@ -531,6 +546,10 @@ impl MinerService for Miner { import }; + // -------------------------------------------------------------------------- + // | NOTE Code below requires transaction_queue and sealing_work locks. | + // | Make sure to release the locks before calling that method. | + // -------------------------------------------------------------------------- if imported.is_ok() && self.options.reseal_on_own_tx && self.tx_reseal_allowed() { // Make sure to do it after transaction is imported and lock is droped. // We need to create pending block and enable sealing @@ -625,6 +644,10 @@ impl MinerService for Miner { self.sealing_work.lock().unwrap().reset(); } else { *self.next_allowed_reseal.lock().unwrap() = Instant::now() + self.options.reseal_min_period; + // -------------------------------------------------------------------------- + // | NOTE Code below requires transaction_queue and sealing_work locks. | + // | Make sure to release the locks before calling that method. | + // -------------------------------------------------------------------------- self.prepare_sealing(chain); } } @@ -715,6 +738,10 @@ impl MinerService for Miner { }); } + // -------------------------------------------------------------------------- + // | NOTE Code below requires transaction_queue and sealing_work locks. | + // | Make sure to release the locks before calling that method. | + // -------------------------------------------------------------------------- self.update_sealing(chain); } } From 25e6b2b8278b219980d11205557e7a6aaee34148 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 8 Jul 2016 17:26:06 +0200 Subject: [PATCH 05/16] Initial implementation of dynamic gas pricer. --- ethcore/Cargo.toml | 2 +- ethcore/src/miner/miner.rs | 105 +++++++++++++++++++++++++++++--- ethcore/src/miner/mod.rs | 3 +- ethcore/src/miner/price_info.rs | 63 +++++++++++++++++++ parity/cli.rs | 5 ++ parity/configuration.rs | 64 ++++++++++++------- parity/io_handler.rs | 2 - parity/main.rs | 4 +- parity/price_info.rs | 47 -------------- rpc/src/v1/tests/eth.rs | 3 +- sync/src/lib.rs | 7 ++- util/src/log.rs | 13 +++- 12 files changed, 228 insertions(+), 90 deletions(-) create mode 100644 ethcore/src/miner/price_info.rs delete mode 100644 parity/price_info.rs diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index 810fd6c51..24dad7331 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -28,7 +28,7 @@ lazy_static = "0.2" ethcore-devtools = { path = "../devtools" } ethjson = { path = "../json" } bloomchain = "0.1" -"ethcore-ipc" = { path = "../ipc/rpc" } +ethcore-ipc = { path = "../ipc/rpc" } rayon = "0.3.1" ethstore = { path = "../ethstore" } semver = "0.2" diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 0ee56dccb..31d152aad 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -32,6 +32,7 @@ use engine::Engine; use miner::{MinerService, MinerStatus, TransactionQueue, AccountDetails, TransactionOrigin}; use miner::work_notify::WorkPoster; use client::TransactionImportResult; +use miner::price_info::PriceInfo; /// Different possible definitions for pending transaction set. @@ -88,10 +89,78 @@ impl Default for MinerOptions { } } +/// Options for the dynamic gas price recalibrator. +pub struct GasPriceCalibratorOptions { + /// Base transaction price to match against. + pub usd_per_tx: f32, + /// How frequently we should recalibrate. + pub recalibration_period: Duration, +} + +/// The gas price validator variant for a GasPricer. +pub struct GasPriceCalibrator { + options: GasPriceCalibratorOptions, + + next_calibration: Instant, +} + +impl GasPriceCalibrator { + fn recalibrate(&mut self, set_price: F) { + trace!(target: "miner", "Recalibrating {:?} versus {:?}", Instant::now(), self.next_calibration); + if Instant::now() >= self.next_calibration { + let usd_per_tx = self.options.usd_per_tx; + trace!(target: "miner", "Getting price info"); + if let Ok(_) = PriceInfo::get(move |price: PriceInfo| { + trace!(target: "miner", "Price info arrived: {:?}", price); + let usd_per_eth = price.ethusd; + let wei_per_usd: f32 = 1.0e18 / usd_per_eth; + let gas_per_tx: f32 = 21000.0; + let wei_per_gas: f32 = wei_per_usd * usd_per_tx / gas_per_tx; + info!(target: "miner", "Updated conversion rate to Ξ1 = {} ({} wei/gas)", format!("US${}", usd_per_eth).apply(Colour::White.bold()), format!("{}", wei_per_gas).apply(Colour::Yellow.bold())); + set_price(U256::from_dec_str(&format!("{:.0}", wei_per_gas)).unwrap()); + }) { + self.next_calibration = Instant::now() + self.options.recalibration_period; + } else { + warn!(target: "miner", "Unable to update Ether price."); + } + } + } +} + +/// Struct to look after updating the acceptable gas price of a miner. +pub enum GasPricer { + /// A fixed gas price in terms of Wei - always the argument given. + Fixed(U256), + /// Gas price is calibrated according to a fixed amount of USD. + Calibrated(GasPriceCalibrator), +} + +impl GasPricer { + /// Create a new Calibrated `GasPricer`. + pub fn new_calibrated(options: GasPriceCalibratorOptions) -> GasPricer { + GasPricer::Calibrated(GasPriceCalibrator { + options: options, + next_calibration: Instant::now(), + }) + } + + /// Create a new Fixed `GasPricer`. + pub fn new_fixed(gas_price: U256) -> GasPricer { + GasPricer::Fixed(gas_price) + } + + fn recalibrate(&mut self, set_price: F) { + match *self { + GasPricer::Fixed(ref max) => set_price(max.clone()), + GasPricer::Calibrated(ref mut cal) => cal.recalibrate(set_price), + } + } +} + /// Keeps track of transactions using priority queue and holds currently mined block. pub struct Miner { // NOTE [ToDr] When locking always lock in this order! - transaction_queue: Mutex, + transaction_queue: Arc>, sealing_work: Mutex>, // for sealing... @@ -106,13 +175,14 @@ pub struct Miner { accounts: Option>, work_poster: Option, + gas_pricer: Mutex, } impl Miner { /// Creates new instance of miner without accounts, but with given spec. pub fn with_spec(spec: Spec) -> Miner { Miner { - transaction_queue: Mutex::new(TransactionQueue::new()), + transaction_queue: Arc::new(Mutex::new(TransactionQueue::new())), options: Default::default(), sealing_enabled: AtomicBool::new(false), next_allowed_reseal: Mutex::new(Instant::now()), @@ -124,14 +194,16 @@ impl Miner { accounts: None, spec: spec, work_poster: None, + gas_pricer: Mutex::new(GasPricer::new_fixed(20_000_000_000u64.into())), } } /// Creates new instance of miner - pub fn new(options: MinerOptions, spec: Spec, accounts: Option>) -> Arc { + pub fn new(options: MinerOptions, gas_pricer: GasPricer, spec: Spec, accounts: Option>) -> Arc { let work_poster = if !options.new_work_notify.is_empty() { Some(WorkPoster::new(&options.new_work_notify)) } else { None }; + let txq = Arc::new(Mutex::new(TransactionQueue::with_limits(options.tx_queue_size, options.tx_gas_limit))); Arc::new(Miner { - transaction_queue: Mutex::new(TransactionQueue::with_limits(options.tx_queue_size, options.tx_gas_limit)), + transaction_queue: txq, sealing_enabled: AtomicBool::new(options.force_sealing || !options.new_work_notify.is_empty()), next_allowed_reseal: Mutex::new(Instant::now()), sealing_block_last_request: Mutex::new(0), @@ -143,6 +215,7 @@ impl Miner { accounts: accounts, spec: spec, work_poster: work_poster, + gas_pricer: Mutex::new(gas_pricer), }) } @@ -160,6 +233,16 @@ impl Miner { fn prepare_sealing(&self, chain: &MiningBlockChainClient) { trace!(target: "miner", "prepare_sealing: entering"); + { + trace!(target: "miner", "recalibrating..."); + let txq = self.transaction_queue.clone(); + self.gas_pricer.lock().unwrap().recalibrate(move |price| { + trace!(target: "miner", "Got gas price! {}", price); + txq.lock().unwrap().set_minimal_gas_price(price); + }); + trace!(target: "miner", "done recalibration."); + } + let (transactions, mut open_block, original_work_hash) = { let transactions = {self.transaction_queue.lock().unwrap().top_transactions()}; let mut sealing_work = self.sealing_work.lock().unwrap(); @@ -490,12 +573,16 @@ impl MinerService for Miner { self.gas_range_target.read().unwrap().1 } - fn import_external_transactions(&self, chain: &MiningBlockChainClient, transactions: Vec) -> - Vec> { + fn import_external_transactions( + &self, + chain: &MiningBlockChainClient, + transactions: Vec + ) -> Vec> { - let mut transaction_queue = self.transaction_queue.lock().unwrap(); - let results = self.add_transactions_to_queue(chain, transactions, TransactionOrigin::External, - &mut transaction_queue); + let results = { + let mut transaction_queue = self.transaction_queue.lock().unwrap(); + self.add_transactions_to_queue(chain, transactions, TransactionOrigin::External, &mut transaction_queue) + }; if !results.is_empty() && self.options.reseal_on_external_tx && self.tx_reseal_allowed() { self.update_sealing(chain); diff --git a/ethcore/src/miner/mod.rs b/ethcore/src/miner/mod.rs index 59acaebd9..64ca2e588 100644 --- a/ethcore/src/miner/mod.rs +++ b/ethcore/src/miner/mod.rs @@ -46,9 +46,10 @@ mod miner; mod external; mod transaction_queue; mod work_notify; +mod price_info; pub use self::transaction_queue::{TransactionQueue, AccountDetails, TransactionOrigin}; -pub use self::miner::{Miner, MinerOptions, PendingSet}; +pub use self::miner::{Miner, MinerOptions, PendingSet, GasPricer, GasPriceCalibratorOptions}; pub use self::external::{ExternalMiner, ExternalMinerService}; pub use client::TransactionImportResult; diff --git a/ethcore/src/miner/price_info.rs b/ethcore/src/miner/price_info.rs new file mode 100644 index 000000000..df2a9aae2 --- /dev/null +++ b/ethcore/src/miner/price_info.rs @@ -0,0 +1,63 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +use rustc_serialize::json::Json; +use std::io::Read; +use std::time::Duration; +use hyper::client::{Handler, Request, Response, Client}; +use hyper::{Next, Encoder, Decoder}; +use hyper::net::HttpStream; +use std::str::FromStr; + +#[derive(Debug)] +pub struct PriceInfo { + pub ethusd: f32, +} + +pub struct SetPriceHandler { + set_price: F, +} + +impl Handler for SetPriceHandler { + fn on_request(&mut self, _: &mut Request) -> Next { trace!(target: "miner", "price_info: on_request"); Next::read().timeout(Duration::from_secs(3)) } + fn on_request_writable(&mut self, _: &mut Encoder) -> Next { trace!(target: "miner", "price_info: on_request_writable"); Next::read().timeout(Duration::from_secs(3)) } + fn on_response(&mut self, _: Response) -> Next { trace!(target: "miner", "price_info: on_response"); Next::read().timeout(Duration::from_secs(3)) } + fn on_response_readable(&mut self, r: &mut Decoder) -> Next { + trace!(target: "miner", "price_info: on_response_readable!"); + let mut body = String::new(); + let _ = r.read_to_string(&mut body).ok() + .and_then(|_| Json::from_str(&body).ok()) + .and_then(|json| json.find_path(&["result", "ethusd"]) + .and_then(|obj| match *obj { + Json::String(ref s) => Some((self.set_price)(PriceInfo { + ethusd: FromStr::from_str(s).unwrap() + })), + _ => None, + })); + Next::end() + } + +} + +impl PriceInfo { + pub fn get(set_price: F) -> Result<(), ()> { + // TODO: Handle each error type properly + trace!(target: "miner", "Starting price info request..."); + Client::new().map_err(|_| ()).and_then(|client| { + client.request(FromStr::from_str("http://api.etherscan.io/api?module=stats&action=ethprice").unwrap(), SetPriceHandler { set_price: set_price }).map_err(|_| ()) + }) + } +} diff --git a/parity/cli.rs b/parity/cli.rs index a865c40b9..07bba06d1 100644 --- a/parity/cli.rs +++ b/parity/cli.rs @@ -170,6 +170,10 @@ Sealing/Mining Options: amount in USD, a web service or 'auto' to use each web service in turn and fallback on the last known good value [default: auto]. + --price-update-period T T will be allowed to pass between each gas price + update. T may be daily, hourly, a number of seconds, + or a time string of the form "2 days", "30 minutes" + etc. [default: hourly]. --gas-floor-target GAS Amount of gas per block to target when sealing a new block [default: 4700000]. --gas-cap GAS A cap on how large we will raise the gas limit per @@ -335,6 +339,7 @@ pub struct Args { pub flag_author: Option, pub flag_usd_per_tx: String, pub flag_usd_per_eth: String, + pub flag_price_update_period: String, pub flag_gas_floor_target: String, pub flag_gas_cap: String, pub flag_extra_data: Option, diff --git a/parity/configuration.rs b/parity/configuration.rs index fc4502673..ec5f99608 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -29,11 +29,10 @@ use util::log::Colour::*; use ethcore::account_provider::AccountProvider; use util::network_settings::NetworkSettings; use ethcore::client::{append_path, get_db_path, Mode, ClientConfig, DatabaseCompactionProfile, Switch, VMType}; -use ethcore::miner::{MinerOptions, PendingSet}; +use ethcore::miner::{MinerOptions, PendingSet, GasPricer, GasPriceCalibratorOptions}; use ethcore::ethereum; use ethcore::spec::Spec; use ethsync::SyncConfig; -use price_info::PriceInfo; use rpc::IpcConfiguration; pub struct Configuration { @@ -154,35 +153,54 @@ impl Configuration { }) } - pub fn gas_price(&self) -> U256 { + fn to_duration(s: &str) -> Duration { + let bad = |_| { + die!("{}: Invalid duration given. See parity --help for more information.", s) + }; + Duration::from_secs(match s { + "daily" => 24 * 60 * 60, + "twice-daily" => 12 * 60 * 60, + "hourly" => 60 * 60, + "half-hourly" => 30 * 60, + "1second" | "1 second" | "second" => 1, + "1minute" | "1 minute" | "minute" => 60, + "1hour" | "1 hour" | "hour" => 60 * 60, + "1day" | "1 day" | "day" => 24 * 60 * 60, + x if x.ends_with("seconds") => FromStr::from_str(&x[0..x.len() - 7]).unwrap_or_else(bad), + x if x.ends_with("minutes") => FromStr::from_str(&x[0..x.len() - 7]).unwrap_or_else(bad) * 60, + x if x.ends_with("hours") => FromStr::from_str(&x[0..x.len() - 5]).unwrap_or_else(bad) * 60 * 60, + x if x.ends_with("days") => FromStr::from_str(&x[0..x.len() - 4]).unwrap_or_else(bad) * 24 * 60 * 60, + x => FromStr::from_str(x).unwrap_or_else(bad), + }) + } + + pub fn gas_pricer(&self) -> GasPricer { match self.args.flag_gasprice.as_ref() { Some(d) => { - U256::from_dec_str(d).unwrap_or_else(|_| { + GasPricer::Fixed(U256::from_dec_str(d).unwrap_or_else(|_| { die!("{}: Invalid gas price given. Must be a decimal unsigned 256-bit number.", d) - }) + })) } _ => { let usd_per_tx: f32 = FromStr::from_str(&self.args.flag_usd_per_tx).unwrap_or_else(|_| { die!("{}: Invalid basic transaction price given in USD. Must be a decimal number.", self.args.flag_usd_per_tx) }); - let usd_per_eth = match self.args.flag_usd_per_eth.as_str() { - "auto" => PriceInfo::get().map_or_else(|| { - let last_known_good = 9.69696; - // TODO: use #1083 to read last known good value. - last_known_good - }, |x| x.ethusd), - "etherscan" => PriceInfo::get().map_or_else(|| { - die!("Unable to retrieve USD value of ETH from etherscan. Rerun with a different value for --usd-per-eth.") - }, |x| x.ethusd), - x => FromStr::from_str(x).unwrap_or_else(|_| die!("{}: Invalid ether price given in USD. Must be a decimal number.", x)) - }; - // TODO: use #1083 to write last known good value as use_per_eth. - - let wei_per_usd: f32 = 1.0e18 / usd_per_eth; - let gas_per_tx: f32 = 21000.0; - let wei_per_gas: f32 = wei_per_usd * usd_per_tx / gas_per_tx; - info!("Using a conversion rate of Ξ1 = {} ({} wei/gas)", format!("US${}", usd_per_eth).apply(White.bold()), format!("{}", wei_per_gas).apply(Yellow.bold())); - U256::from_dec_str(&format!("{:.0}", wei_per_gas)).unwrap() + match self.args.flag_usd_per_eth.as_str() { + "auto" => { + GasPricer::new_calibrated(GasPriceCalibratorOptions { + usd_per_tx: usd_per_tx, + recalibration_period: Self::to_duration(self.args.flag_price_update_period.as_str()), + }) + }, + x => { + let usd_per_eth: f32 = FromStr::from_str(x).unwrap_or_else(|_| die!("{}: Invalid ether price given in USD. Must be a decimal number.", x)); + let wei_per_usd: f32 = 1.0e18 / usd_per_eth; + let gas_per_tx: f32 = 21000.0; + let wei_per_gas: f32 = wei_per_usd * usd_per_tx / gas_per_tx; + info!("Using a fixed conversion rate of Ξ1 = {} ({} wei/gas)", format!("US${}", usd_per_eth).apply(White.bold()), format!("{}", wei_per_gas).apply(Yellow.bold())); + GasPricer::Fixed(U256::from_dec_str(&format!("{:.0}", wei_per_gas)).unwrap()) + } + } } } } diff --git a/parity/io_handler.rs b/parity/io_handler.rs index 4af630d44..497d3e374 100644 --- a/parity/io_handler.rs +++ b/parity/io_handler.rs @@ -49,14 +49,12 @@ impl IoHandler for ClientIoHandler { fn message(&self, _io: &IoContext, message: &NetSyncMessage) { match *message { NetworkIoMessage::User(SyncMessage::StartNetwork) => { - info!("Starting network"); if let Some(network) = self.network.upgrade() { network.start().unwrap_or_else(|e| warn!("Error starting network: {:?}", e)); EthSync::register(&*network, self.sync.clone()).unwrap_or_else(|e| warn!("Error registering eth protocol handler: {}", e)); } }, NetworkIoMessage::User(SyncMessage::StopNetwork) => { - info!("Stopping network"); if let Some(network) = self.network.upgrade() { network.stop().unwrap_or_else(|e| warn!("Error stopping network: {:?}", e)); } diff --git a/parity/main.rs b/parity/main.rs index 5fe75a134..fd90146a1 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -55,7 +55,6 @@ extern crate ethcore_signer; #[macro_use] mod die; -mod price_info; mod upgrade; mod hypervisor; mod setup_log; @@ -222,12 +221,11 @@ fn execute_client(conf: Configuration, spec: Spec, client_config: ClientConfig) let account_service = Arc::new(conf.account_service()); // Miner - let miner = Miner::new(conf.miner_options(), conf.spec(), Some(account_service.clone())); + let miner = Miner::new(conf.miner_options(), conf.gas_pricer(), conf.spec(), Some(account_service.clone())); miner.set_author(conf.author().unwrap_or_default()); miner.set_gas_floor_target(conf.gas_floor_target()); miner.set_gas_ceil_target(conf.gas_ceil_target()); miner.set_extra_data(conf.extra_data()); - miner.set_minimal_gas_price(conf.gas_price()); miner.set_transactions_limit(conf.args.flag_tx_queue_size); // Build client diff --git a/parity/price_info.rs b/parity/price_info.rs deleted file mode 100644 index 0c04b3a77..000000000 --- a/parity/price_info.rs +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2015, 2016 Ethcore (UK) Ltd. -// This file is part of Parity. - -// Parity is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Parity is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Parity. If not, see . - -use rustc_serialize::json::Json; -use std::io::Read; -use hyper::Client; -use hyper::header::Connection; -use std::str::FromStr; - -pub struct PriceInfo { - pub ethusd: f32, -} - -impl PriceInfo { - pub fn get() -> Option { - let mut body = String::new(); - // TODO: Handle each error type properly - let mut client = Client::new(); - client.set_read_timeout(Some(::std::time::Duration::from_secs(3))); - client.get("http://api.etherscan.io/api?module=stats&action=ethprice") - .header(Connection::close()) - .send() - .ok() - .and_then(|mut s| s.read_to_string(&mut body).ok()) - .and_then(|_| Json::from_str(&body).ok()) - .and_then(|json| json.find_path(&["result", "ethusd"]) - .and_then(|obj| match *obj { - Json::String(ref s) => Some(PriceInfo { - ethusd: FromStr::from_str(s).unwrap() - }), - _ => None - })) - } -} diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs index 7b2ffb2d4..4e62bebfe 100644 --- a/rpc/src/v1/tests/eth.rs +++ b/rpc/src/v1/tests/eth.rs @@ -25,7 +25,7 @@ use ethcore::spec::{Genesis, Spec}; use ethcore::block::Block; use ethcore::views::BlockView; use ethcore::ethereum; -use ethcore::miner::{MinerOptions, MinerService, ExternalMiner, Miner, PendingSet}; +use ethcore::miner::{MinerOptions, GasPricer, MinerService, ExternalMiner, Miner, PendingSet}; use ethcore::account_provider::AccountProvider; use devtools::RandomTempPath; use util::Hashable; @@ -64,6 +64,7 @@ fn miner_service(spec: Spec, accounts: Arc) -> Arc { work_queue_size: 50, enable_resubmission: true, }, + GasPricer::new_fixed(20000000000), spec, Some(accounts) ) diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 93e4f4382..bad683dd5 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -44,7 +44,12 @@ //! let mut service = NetworkService::new(NetworkConfiguration::new()).unwrap(); //! service.start().unwrap(); //! let dir = env::temp_dir(); -//! let miner = Miner::new(Default::default(), ethereum::new_frontier(), None); +//! let miner = Miner::new( +//! Default::default(), +//! GasPricer::new_fixed(20_000_000_000.into()), +//! ethereum::new_frontier(), +//! None +//! ); //! let client = Client::new( //! ClientConfig::default(), //! ethereum::new_frontier(), diff --git a/util/src/log.rs b/util/src/log.rs index ed3a5376d..6df6613a2 100644 --- a/util/src/log.rs +++ b/util/src/log.rs @@ -18,7 +18,7 @@ use std::env; use std::borrow::Cow; -use rlog::{LogLevelFilter}; +use rlog::{LogLevelFilter, LogRecord}; use env_logger::LogBuilder; use std::sync::{RwLock, RwLockReadGuard}; use std::sync::atomic::{Ordering, AtomicBool}; @@ -45,6 +45,12 @@ impl> Applyable for T { } } +fn markup(r: &LogRecord) -> String { + let s = format!("{}", r.args()); + println!("{}", s); + s +} + lazy_static! { static ref LOG_DUMMY: bool = { let mut builder = LogBuilder::new(); @@ -54,7 +60,10 @@ lazy_static! { builder.parse(&log); } - if let Ok(_) = builder.init() { + println!("HERE!"); + builder.format(markup); + + if builder.init().is_ok() { println!("logger initialized"); } true From c648171f9197f6c46ec8022d2ce36bee819096b4 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 8 Jul 2016 20:01:20 +0200 Subject: [PATCH 06/16] Fix the HTTP client for getting price. --- ethcore/src/miner/price_info.rs | 41 +++++++++++++++++++++++++++++---- util/src/log.rs | 3 +-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/ethcore/src/miner/price_info.rs b/ethcore/src/miner/price_info.rs index df2a9aae2..9861d19f8 100644 --- a/ethcore/src/miner/price_info.rs +++ b/ethcore/src/miner/price_info.rs @@ -15,12 +15,14 @@ // along with Parity. If not, see . use rustc_serialize::json::Json; +use std::thread; use std::io::Read; use std::time::Duration; +use std::str::FromStr; +use std::sync::mpsc; use hyper::client::{Handler, Request, Response, Client}; use hyper::{Next, Encoder, Decoder}; use hyper::net::HttpStream; -use std::str::FromStr; #[derive(Debug)] pub struct PriceInfo { @@ -29,6 +31,13 @@ pub struct PriceInfo { pub struct SetPriceHandler { set_price: F, + channel: mpsc::Sender<()>, +} + +impl Drop for SetPriceHandler { + fn drop(&mut self) { + let _ = self.channel.send(()); + } } impl Handler for SetPriceHandler { @@ -55,9 +64,33 @@ impl Handler for SetPriceH impl PriceInfo { pub fn get(set_price: F) -> Result<(), ()> { // TODO: Handle each error type properly + let client = try!(Client::new().map_err(|_| ())); trace!(target: "miner", "Starting price info request..."); - Client::new().map_err(|_| ()).and_then(|client| { - client.request(FromStr::from_str("http://api.etherscan.io/api?module=stats&action=ethprice").unwrap(), SetPriceHandler { set_price: set_price }).map_err(|_| ()) - }) + thread::spawn(move || { + trace!(target: "miner", "Inside thread..."); + let (tx, rx) = mpsc::channel(); + let _ = client.request(FromStr::from_str("http://api.etherscan.io/api?module=stats&action=ethprice").unwrap(), SetPriceHandler { + set_price: set_price, + channel: tx, + }).ok().and_then(|_| rx.recv().ok()); + client.close(); + }); + Ok(()) } } + +//#[ignore] +#[test] +fn should_get_price_info() { + use std::sync::{Condvar, Mutex, Arc}; + use std::time::Duration; + use util::log::init_log; + init_log(); + let done = Arc::new((Mutex::new(PriceInfo { ethusd: 0f32 }), Condvar::new())); + let rdone = done.clone(); + trace!(target: "miner", "price_info: getting price_info"); + PriceInfo::get(move |price| { let mut p = rdone.0.lock().unwrap(); *p = price; rdone.1.notify_one(); }).unwrap(); + let p = done.1.wait_timeout(done.0.lock().unwrap(), Duration::from_millis(10000)).unwrap(); + assert!(!p.1.timed_out()); + assert!(p.0.ethusd != 0f32); +} \ No newline at end of file diff --git a/util/src/log.rs b/util/src/log.rs index 6df6613a2..e2e111aa7 100644 --- a/util/src/log.rs +++ b/util/src/log.rs @@ -60,8 +60,7 @@ lazy_static! { builder.parse(&log); } - println!("HERE!"); - builder.format(markup); +// builder.format(markup); if builder.init().is_ok() { println!("logger initialized"); From b304ce58383139286ee367811c8a3b9257d1f9c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Sat, 9 Jul 2016 11:23:06 +0200 Subject: [PATCH 07/16] Uncommenting simple Miner tests (#1571) --- devtools/src/lib.rs | 6 ++--- devtools/src/random_path.rs | 19 ++++++++++++++ ethcore/src/client/test_client.rs | 41 ++++++++++++++++++++++++++++--- ethcore/src/externalities.rs | 1 + ethcore/src/lib.rs | 3 ++- ethcore/src/miner/miner.rs | 3 --- ethcore/src/tests/helpers.rs | 20 --------------- 7 files changed, 62 insertions(+), 31 deletions(-) diff --git a/devtools/src/lib.rs b/devtools/src/lib.rs index e37d5c528..831e4315e 100644 --- a/devtools/src/lib.rs +++ b/devtools/src/lib.rs @@ -19,9 +19,9 @@ extern crate rand; -pub mod random_path; -pub mod test_socket; -pub mod stop_guard; +mod random_path; +mod test_socket; +mod stop_guard; pub use random_path::*; pub use test_socket::*; diff --git a/devtools/src/random_path.rs b/devtools/src/random_path.rs index 09bee7e45..7c1fd19ea 100644 --- a/devtools/src/random_path.rs +++ b/devtools/src/random_path.rs @@ -74,6 +74,25 @@ impl Drop for RandomTempPath { } } +pub struct GuardedTempResult { + pub result: Option, + pub _temp: RandomTempPath +} + +impl GuardedTempResult { + pub fn reference(&self) -> &T { + self.result.as_ref().unwrap() + } + + pub fn reference_mut(&mut self) -> &mut T { + self.result.as_mut().unwrap() + } + + pub fn take(&mut self) -> T { + self.result.take().unwrap() + } +} + #[test] fn creates_dir() { let temp = RandomTempPath::create_dir(); diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index 309934a98..f7387e6ab 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -18,6 +18,7 @@ use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrder}; use util::*; +use devtools::*; use transaction::{Transaction, LocalizedTransaction, SignedTransaction, Action}; use blockchain::TreeRoute; use client::{BlockChainClient, MiningBlockChainClient, BlockChainInfo, BlockStatus, BlockID, @@ -29,7 +30,7 @@ use log_entry::LocalizedLogEntry; use receipt::{Receipt, LocalizedReceipt}; use blockchain::extras::BlockReceipts; use error::{ImportResult}; -use evm::Factory as EvmFactory; +use evm::{Factory as EvmFactory, VMType}; use miner::{Miner, MinerService}; use spec::Spec; @@ -67,6 +68,10 @@ pub struct TestBlockChainClient { pub queue_size: AtomicUsize, /// Miner pub miner: Arc, + /// Spec + pub spec: Spec, + /// VM Factory + pub vm_factory: EvmFactory, } #[derive(Clone)] @@ -106,6 +111,8 @@ impl TestBlockChainClient { receipts: RwLock::new(HashMap::new()), queue_size: AtomicUsize::new(0), miner: Arc::new(Miner::with_spec(Spec::new_test())), + spec: Spec::new_test(), + vm_factory: EvmFactory::new(VMType::Interpreter), }; client.add_blocks(1, EachBlockWith::Nothing); // add genesis block client.genesis_hash = client.last_hash.unwrapped_read().clone(); @@ -239,17 +246,43 @@ impl TestBlockChainClient { } } +pub fn get_temp_journal_db() -> GuardedTempResult> { + let temp = RandomTempPath::new(); + let journal_db = journaldb::new(temp.as_str(), journaldb::Algorithm::EarlyMerge, DatabaseConfig::default()); + GuardedTempResult { + _temp: temp, + result: Some(journal_db) + } +} + impl MiningBlockChainClient for TestBlockChainClient { fn prepare_open_block(&self, _author: Address, _gas_range_target: (U256, U256), _extra_data: Bytes) -> OpenBlock { - unimplemented!(); + let engine = &self.spec.engine; + let genesis_header = self.spec.genesis_header(); + let mut db_result = get_temp_journal_db(); + let mut db = db_result.take(); + self.spec.ensure_db_good(db.as_hashdb_mut()); + let last_hashes = vec![genesis_header.hash()]; + OpenBlock::new( + engine.deref(), + self.vm_factory(), + Default::default(), + false, + db, + &genesis_header, + last_hashes, + Address::zero(), + (3141562.into(), 31415620.into()), + vec![] + ).expect("Opening block for tests will not fail.") } fn vm_factory(&self) -> &EvmFactory { - unimplemented!(); + &self.vm_factory } fn import_sealed_block(&self, _block: SealedBlock) -> ImportResult { - unimplemented!(); + Ok(H256::default()) } } diff --git a/ethcore/src/externalities.rs b/ethcore/src/externalities.rs index 5eccc717b..8a44363fa 100644 --- a/ethcore/src/externalities.rs +++ b/ethcore/src/externalities.rs @@ -298,6 +298,7 @@ mod tests { use evm::{Ext}; use substate::*; use tests::helpers::*; + use devtools::GuardedTempResult; use super::*; use trace::{NoopTracer, NoopVMTracer}; diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index f2269501c..a2df34ca5 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -98,7 +98,8 @@ pub extern crate ethstore; extern crate semver; extern crate ethcore_ipc_nano as nanoipc; -#[cfg(test)] extern crate ethcore_devtools as devtools; +extern crate ethcore_devtools as devtools; + #[cfg(feature = "jit" )] extern crate evmjit; pub mod account_provider; diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index a423592b3..a2935c4ea 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -756,8 +756,6 @@ mod tests { use block::*; use spec::Spec; - // TODO [ToDr] To uncomment` when TestBlockChainClient can actually return a ClosedBlock. - #[ignore] #[test] fn should_prepare_block_to_seal() { // given @@ -769,7 +767,6 @@ mod tests { assert!(sealing_work.is_some(), "Expected closed block"); } - #[ignore] #[test] fn should_still_work_after_a_couple_of_blocks() { // given diff --git a/ethcore/src/tests/helpers.rs b/ethcore/src/tests/helpers.rs index b2e7deb51..883e0aa14 100644 --- a/ethcore/src/tests/helpers.rs +++ b/ethcore/src/tests/helpers.rs @@ -32,26 +32,6 @@ pub enum ChainEra { Homestead, } -#[cfg(test)] -pub struct GuardedTempResult { - result: Option, - _temp: RandomTempPath -} - -impl GuardedTempResult { - pub fn reference(&self) -> &T { - self.result.as_ref().unwrap() - } - - pub fn reference_mut(&mut self) -> &mut T { - self.result.as_mut().unwrap() - } - - pub fn take(&mut self) -> T { - self.result.take().unwrap() - } -} - pub struct TestEngine { engine: Box, max_depth: usize From f48d030f0bfe9276bc445de7e8aee365441f2870 Mon Sep 17 00:00:00 2001 From: arkpar Date: Sat, 9 Jul 2016 11:21:54 +0200 Subject: [PATCH 08/16] Init public interface with IO message --- util/src/network/host.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/util/src/network/host.rs b/util/src/network/host.rs index 0caaef74c..e96cea288 100644 --- a/util/src/network/host.rs +++ b/util/src/network/host.rs @@ -123,8 +123,7 @@ const IDLE: usize = SYS_TIMER + 2; const DISCOVERY: usize = SYS_TIMER + 3; const DISCOVERY_REFRESH: usize = SYS_TIMER + 4; const DISCOVERY_ROUND: usize = SYS_TIMER + 5; -const INIT_PUBLIC: usize = SYS_TIMER + 6; -const NODE_TABLE: usize = SYS_TIMER + 7; +const NODE_TABLE: usize = SYS_TIMER + 6; const FIRST_SESSION: usize = 0; const LAST_SESSION: usize = FIRST_SESSION + MAX_SESSIONS - 1; const USER_TIMER: usize = LAST_SESSION + 256; @@ -156,6 +155,8 @@ pub enum NetworkIoMessage where Message: Send + Sync + Clone { /// Timer delay in milliseconds. delay: u64, }, + /// Initliaze public interface. + InitPublicInterface, /// Disconnect a peer. Disconnect(PeerId), /// Disconnect and temporary disable peer. @@ -511,7 +512,6 @@ impl Host where Message: Send + Sync + Clone { } fn init_public_interface(&self, io: &IoContext>) -> Result<(), UtilError> { - io.clear_timer(INIT_PUBLIC).unwrap(); if self.info.unwrapped_read().public_endpoint.is_some() { return Ok(()); } @@ -546,7 +546,9 @@ impl Host where Message: Send + Sync + Clone { let discovery = { let info = self.info.unwrapped_read(); if info.config.discovery_enabled && info.config.non_reserved_mode == NonReservedPeerMode::Accept { - Some(Discovery::new(&info.keys, public_endpoint.address.clone(), public_endpoint, DISCOVERY)) + let mut udp_addr = local_endpoint.address.clone(); + udp_addr.set_port(local_endpoint.udp_port); + Some(Discovery::new(&info.keys, udp_addr, public_endpoint, DISCOVERY)) } else { None } }; @@ -899,7 +901,7 @@ impl IoHandler> for Host where Messa /// Initialize networking fn initialize(&self, io: &IoContext>) { io.register_timer(IDLE, MAINTENANCE_TIMEOUT).expect("Error registering Network idle timer"); - io.register_timer(INIT_PUBLIC, 0).expect("Error registering initialization timer"); + io.message(NetworkIoMessage::InitPublicInterface).unwrap_or_else(|e| warn!("Error sending IO notification: {:?}", e)); self.maintain_network(io) } @@ -947,8 +949,6 @@ impl IoHandler> for Host where Messa } match token { IDLE => self.maintain_network(io), - INIT_PUBLIC => self.init_public_interface(io).unwrap_or_else(|e| - warn!("Error initializing public interface: {:?}", e)), FIRST_SESSION ... LAST_SESSION => self.connection_timeout(token, io), DISCOVERY_REFRESH => { self.discovery.locked().as_mut().unwrap().refresh(); @@ -1036,7 +1036,9 @@ impl IoHandler> for Host where Messa for (p, h) in self.handlers.unwrapped_read().iter() { h.message(&NetworkContext::new(io, p, None, self.sessions.clone(), &reserved), &message); } - } + }, + NetworkIoMessage::InitPublicInterface => + self.init_public_interface(io).unwrap_or_else(|e| warn!("Error initializing public interface: {:?}", e)), _ => {} // ignore others. } } From 172553c81e047d210f662d20f8f8cb3f6db68325 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 9 Jul 2016 16:39:59 +0200 Subject: [PATCH 09/16] Fix test. --- rpc/src/v1/tests/eth.rs | 2 +- util/src/log.rs | 10 +--------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs index 4e62bebfe..e3e07148e 100644 --- a/rpc/src/v1/tests/eth.rs +++ b/rpc/src/v1/tests/eth.rs @@ -64,7 +64,7 @@ fn miner_service(spec: Spec, accounts: Arc) -> Arc { work_queue_size: 50, enable_resubmission: true, }, - GasPricer::new_fixed(20000000000), + GasPricer::new_fixed(20_000_000_000u64.into()), spec, Some(accounts) ) diff --git a/util/src/log.rs b/util/src/log.rs index ff2ecd163..785737591 100644 --- a/util/src/log.rs +++ b/util/src/log.rs @@ -18,7 +18,7 @@ use std::env; use std::borrow::Cow; -use rlog::{LogLevelFilter, LogRecord}; +use rlog::{LogLevelFilter}; use env_logger::LogBuilder; use std::sync::{RwLock, RwLockReadGuard}; use std::sync::atomic::{Ordering, AtomicBool}; @@ -46,12 +46,6 @@ impl> Applyable for T { } } -fn markup(r: &LogRecord) -> String { - let s = format!("{}", r.args()); - println!("{}", s); - s -} - lazy_static! { static ref LOG_DUMMY: bool = { let mut builder = LogBuilder::new(); @@ -61,8 +55,6 @@ lazy_static! { builder.parse(&log); } -// builder.format(markup); - if builder.init().is_ok() { println!("logger initialized"); } From 716de2187352625289357826604884489f43f8de Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 9 Jul 2016 16:59:15 +0200 Subject: [PATCH 10/16] Address review grumble. --- ethcore/src/miner/price_info.rs | 11 ++++------- sync/src/lib.rs | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/ethcore/src/miner/price_info.rs b/ethcore/src/miner/price_info.rs index 9861d19f8..ac2f81cd0 100644 --- a/ethcore/src/miner/price_info.rs +++ b/ethcore/src/miner/price_info.rs @@ -41,11 +41,11 @@ impl Drop for SetPriceHandler { } impl Handler for SetPriceHandler { - fn on_request(&mut self, _: &mut Request) -> Next { trace!(target: "miner", "price_info: on_request"); Next::read().timeout(Duration::from_secs(3)) } - fn on_request_writable(&mut self, _: &mut Encoder) -> Next { trace!(target: "miner", "price_info: on_request_writable"); Next::read().timeout(Duration::from_secs(3)) } - fn on_response(&mut self, _: Response) -> Next { trace!(target: "miner", "price_info: on_response"); Next::read().timeout(Duration::from_secs(3)) } + fn on_request(&mut self, _: &mut Request) -> Next { Next::read().timeout(Duration::from_secs(3)) } + fn on_request_writable(&mut self, _: &mut Encoder) -> Next { Next::read().timeout(Duration::from_secs(3)) } + fn on_response(&mut self, _: Response) -> Next { Next::read().timeout(Duration::from_secs(3)) } + fn on_response_readable(&mut self, r: &mut Decoder) -> Next { - trace!(target: "miner", "price_info: on_response_readable!"); let mut body = String::new(); let _ = r.read_to_string(&mut body).ok() .and_then(|_| Json::from_str(&body).ok()) @@ -65,9 +65,7 @@ impl PriceInfo { pub fn get(set_price: F) -> Result<(), ()> { // TODO: Handle each error type properly let client = try!(Client::new().map_err(|_| ())); - trace!(target: "miner", "Starting price info request..."); thread::spawn(move || { - trace!(target: "miner", "Inside thread..."); let (tx, rx) = mpsc::channel(); let _ = client.request(FromStr::from_str("http://api.etherscan.io/api?module=stats&action=ethprice").unwrap(), SetPriceHandler { set_price: set_price, @@ -88,7 +86,6 @@ fn should_get_price_info() { init_log(); let done = Arc::new((Mutex::new(PriceInfo { ethusd: 0f32 }), Condvar::new())); let rdone = done.clone(); - trace!(target: "miner", "price_info: getting price_info"); PriceInfo::get(move |price| { let mut p = rdone.0.lock().unwrap(); *p = price; rdone.1.notify_one(); }).unwrap(); let p = done.1.wait_timeout(done.0.lock().unwrap(), Duration::from_millis(10000)).unwrap(); assert!(!p.1.timed_out()); diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 2d624b47b..97c0540de 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -46,7 +46,7 @@ //! let dir = env::temp_dir(); //! let miner = Miner::new( //! Default::default(), -//! GasPricer::new_fixed(20_000_000_000.into()), +//! GasPricer::new_fixed(20_000_000_000u64.into()), //! ethereum::new_frontier(), //! None //! ); From 32a4a060d69a37bc5b8310e0e9eb17772806769f Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Sat, 9 Jul 2016 17:18:34 +0200 Subject: [PATCH 11/16] bringing hypervisor as a crate in ipc dir (#1565) * resurrecting hypervisor in ipc namespace * get rid of the quotes * target: hypervisor --- Cargo.lock | 14 ++++ Cargo.toml | 1 + build.rs | 24 ------- ipc/hypervisor/Cargo.toml | 19 +++++ ipc/hypervisor/build.rs | 43 +++++++++++ .../mod.rs => ipc/hypervisor/src/lib.rs | 72 ++++++++++++------- .../hypervisor/src}/service.rs | 0 .../hypervisor/src}/service.rs.in | 4 +- parity/main.rs | 5 +- 9 files changed, 129 insertions(+), 53 deletions(-) create mode 100644 ipc/hypervisor/Cargo.toml create mode 100644 ipc/hypervisor/build.rs rename parity/hypervisor/mod.rs => ipc/hypervisor/src/lib.rs (69%) rename {parity/hypervisor => ipc/hypervisor/src}/service.rs (100%) rename {parity/hypervisor => ipc/hypervisor/src}/service.rs.in (95%) diff --git a/Cargo.lock b/Cargo.lock index eae4823f6..ffb4fc12b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,7 @@ dependencies = [ "ethcore-devtools 1.3.0", "ethcore-ipc 1.3.0", "ethcore-ipc-codegen 1.3.0", + "ethcore-ipc-hypervisor 1.2.0", "ethcore-ipc-nano 1.3.0", "ethcore-rpc 1.3.0", "ethcore-signer 1.3.0", @@ -315,6 +316,19 @@ dependencies = [ "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ethcore-ipc-hypervisor" +version = "1.2.0" +dependencies = [ + "ethcore-ipc 1.3.0", + "ethcore-ipc-codegen 1.3.0", + "ethcore-ipc-nano 1.3.0", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nanomsg 0.5.1 (git+https://github.com/ethcore/nanomsg.rs.git)", + "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ethcore-ipc-nano" version = "1.3.0" diff --git a/Cargo.toml b/Cargo.toml index e49c3efc5..02b7070f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ ethcore-dapps = { path = "dapps", optional = true } semver = "0.2" ethcore-ipc-nano = { path = "ipc/nano" } ethcore-ipc = { path = "ipc/rpc" } +ethcore-ipc-hypervisor = { path = "ipc/hypervisor" } json-ipc-server = { git = "https://github.com/ethcore/json-ipc-server.git" } ansi_term = "0.7" diff --git a/build.rs b/build.rs index 25764f4be..41b9a1b3e 100644 --- a/build.rs +++ b/build.rs @@ -15,35 +15,11 @@ // along with Parity. If not, see . extern crate rustc_version; -extern crate syntex; -extern crate ethcore_ipc_codegen as codegen; -use std::env; -use std::path::Path; use rustc_version::{version_meta, Channel}; fn main() { if let Channel::Nightly = version_meta().channel { println!("cargo:rustc-cfg=nightly"); } - - let out_dir = env::var_os("OUT_DIR").unwrap(); - - // ipc pass - { - let src = Path::new("parity/hypervisor/service.rs.in"); - let dst = Path::new(&out_dir).join("hypervisor_service_ipc.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &src, &dst).unwrap(); - } - - // serialization pass - { - let src = Path::new(&out_dir).join("hypervisor_service_ipc.rs"); - let dst = Path::new(&out_dir).join("hypervisor_service_cg.rs"); - let mut registry = syntex::Registry::new(); - codegen::register(&mut registry); - registry.expand("", &src, &dst).unwrap(); - } } diff --git a/ipc/hypervisor/Cargo.toml b/ipc/hypervisor/Cargo.toml new file mode 100644 index 000000000..54edac7ec --- /dev/null +++ b/ipc/hypervisor/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "ethcore-ipc-hypervisor" +version = "1.2.0" +authors = ["Nikolay Volf "] +license = "GPL-3.0" +build = "build.rs" + +[features] + +[dependencies] +ethcore-ipc = { path = "../rpc" } +nanomsg = { git = "https://github.com/ethcore/nanomsg.rs.git" } +ethcore-ipc-nano = { path = "../nano" } +semver = "0.2" +log = "0.3" + +[build-dependencies] +syntex = "*" +ethcore-ipc-codegen = { path = "../codegen" } diff --git a/ipc/hypervisor/build.rs b/ipc/hypervisor/build.rs new file mode 100644 index 000000000..75ccd2e8c --- /dev/null +++ b/ipc/hypervisor/build.rs @@ -0,0 +1,43 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +extern crate syntex; +extern crate ethcore_ipc_codegen as codegen; + +use std::env; +use std::path::Path; + +fn main() { + let out_dir = env::var_os("OUT_DIR").unwrap(); + + // ipc pass + { + let src = Path::new("src/service.rs.in"); + let dst = Path::new(&out_dir).join("hypervisor_service_ipc.rs"); + let mut registry = syntex::Registry::new(); + codegen::register(&mut registry); + registry.expand("", &src, &dst).unwrap(); + } + + // serialization pass + { + let src = Path::new(&out_dir).join("hypervisor_service_ipc.rs"); + let dst = Path::new(&out_dir).join("hypervisor_service_cg.rs"); + let mut registry = syntex::Registry::new(); + codegen::register(&mut registry); + registry.expand("", &src, &dst).unwrap(); + } +} diff --git a/parity/hypervisor/mod.rs b/ipc/hypervisor/src/lib.rs similarity index 69% rename from parity/hypervisor/mod.rs rename to ipc/hypervisor/src/lib.rs index 501594d1f..69bbbffae 100644 --- a/parity/hypervisor/mod.rs +++ b/ipc/hypervisor/src/lib.rs @@ -16,57 +16,58 @@ //! Parity interprocess hypervisor module -// while not included in binary -#![allow(dead_code)] #![cfg_attr(feature="dev", allow(used_underscore_binding))] -use nanoipc; -use std::sync::{Arc,RwLock}; -use hypervisor::service::*; -use std::process::{Command,Child}; -use std::collections::HashMap; +extern crate ethcore_ipc as ipc; +extern crate ethcore_ipc_nano as nanoipc; +extern crate semver; +#[macro_use] extern crate log; pub mod service; /// Default value for hypervisor ipc listener pub const HYPERVISOR_IPC_URL: &'static str = "ipc:///tmp/parity-internal-hyper-status.ipc"; +use std::sync::{Arc,RwLock}; +use service::{HypervisorService, IpcModuleId}; +use std::process::{Command,Child}; +use std::collections::HashMap; + +pub use service::{HypervisorServiceClient, CLIENT_MODULE_ID}; + type BinaryId = &'static str; -const BLOCKCHAIN_DB_BINARY: BinaryId = "blockchain"; + +const CLIENT_BINARY: BinaryId = "client"; pub struct Hypervisor { ipc_addr: String, service: Arc, ipc_worker: RwLock>, processes: RwLock>, -} - -impl Default for Hypervisor { - fn default() -> Self { - Hypervisor::new() - } + db_path: String, } impl Hypervisor { /// initializes the Hypervisor service with the open ipc socket for incoming clients - pub fn new() -> Hypervisor { - Hypervisor::with_url(HYPERVISOR_IPC_URL) + pub fn new(db_path: &str) -> Hypervisor { + Hypervisor::with_url(db_path, HYPERVISOR_IPC_URL) } /// Starts on the specified address for ipc listener - fn with_url(addr: &str) -> Hypervisor{ - Hypervisor::with_url_and_service(addr, HypervisorService::new()) + fn with_url(db_path: &str, addr: &str) -> Hypervisor{ + Hypervisor::with_url_and_service(db_path, addr, HypervisorService::new()) } /// Starts with the specified address for the ipc listener and /// the specified list of modules in form of created service - fn with_url_and_service(addr: &str, service: Arc) -> Hypervisor { + fn with_url_and_service(db_path: &str, addr: &str, service: Arc) -> Hypervisor { let worker = nanoipc::Worker::new(&service); Hypervisor{ ipc_addr: addr.to_owned(), service: service, ipc_worker: RwLock::new(worker), processes: RwLock::new(HashMap::new()), + db_path: db_path.to_owned(), } } @@ -74,14 +75,14 @@ impl Hypervisor { /// we match binaries fn match_module(module_id: &IpcModuleId) -> Option { match *module_id { - BLOCKCHAIN_MODULE_ID => Some(BLOCKCHAIN_DB_BINARY), + CLIENT_MODULE_ID => Some(CLIENT_BINARY), // none means the module is inside the main binary _ => None } } /// Creates IPC listener and starts all binaries - fn start(&self) { + pub fn start(&self) { let mut worker = self.ipc_worker.write().unwrap(); worker.add_reqrep(&self.ipc_addr).unwrap_or_else(|e| panic!("Hypervisor ipc worker can not start - critical! ({:?})", e)); @@ -102,8 +103,13 @@ impl Hypervisor { return; } } - let child = Command::new(binary_id).spawn().unwrap_or_else( - |e| panic!("Hypervisor cannot start binary: {}", e)); + + let mut executable_path = std::env::current_exe().unwrap(); + executable_path.pop(); + executable_path.push(binary_id); + + let child = Command::new(&executable_path.to_str().unwrap()).arg(&self.db_path).spawn().unwrap_or_else( + |e| panic!("Hypervisor cannot start binary ({:?}): {}", executable_path, e)); processes.insert(binary_id, child); }); } @@ -120,6 +126,22 @@ impl Hypervisor { worker.poll() } } + + pub fn shutdown(&self, wait_time: Option) { + if wait_time.is_some() { std::thread::sleep(wait_time.unwrap()) } + + let mut childs = self.processes.write().unwrap(); + for (ref mut binary, ref mut child) in childs.iter_mut() { + trace!(target: "hypervisor", "HYPERVISOR: Stopping process module: {}", binary); + child.kill().unwrap(); + } + } +} + +impl Drop for Hypervisor { + fn drop(&mut self) { + self.shutdown(Some(std::time::Duration::new(1, 0))); + } } #[cfg(test)] @@ -135,7 +157,7 @@ mod tests { let url = "ipc:///tmp/test-parity-hypervisor-10.ipc"; let test_module_id = 8080u64; - let hypervisor = Hypervisor::with_url_and_service(url, HypervisorService::with_modules(vec![test_module_id])); + let hypervisor = Hypervisor::with_url_and_service("", url, HypervisorService::with_modules(vec![test_module_id])); assert_eq!(false, hypervisor.modules_ready()); } @@ -155,7 +177,7 @@ mod tests { client.module_ready(test_module_id); }); - let hypervisor = Hypervisor::with_url_and_service(url, HypervisorService::with_modules(vec![test_module_id])); + let hypervisor = Hypervisor::with_url_and_service("", url, HypervisorService::with_modules(vec![test_module_id])); hypervisor.start(); hypervisor_ready_local.store(true, Ordering::Relaxed); hypervisor.wait_for_startup(); diff --git a/parity/hypervisor/service.rs b/ipc/hypervisor/src/service.rs similarity index 100% rename from parity/hypervisor/service.rs rename to ipc/hypervisor/src/service.rs diff --git a/parity/hypervisor/service.rs.in b/ipc/hypervisor/src/service.rs.in similarity index 95% rename from parity/hypervisor/service.rs.in rename to ipc/hypervisor/src/service.rs.in index 134194603..2b626d769 100644 --- a/parity/hypervisor/service.rs.in +++ b/ipc/hypervisor/src/service.rs.in @@ -24,7 +24,7 @@ use std::collections::VecDeque; pub type IpcModuleId = u64; /// Blockhain database module id -pub const BLOCKCHAIN_MODULE_ID: IpcModuleId = 2000; +pub const CLIENT_MODULE_ID: IpcModuleId = 2000; /// IPC service that handles module management pub struct HypervisorService { @@ -43,7 +43,7 @@ impl HypervisorService { impl HypervisorService { /// New service with the default list of modules pub fn new() -> Arc { - HypervisorService::with_modules(vec![]) + HypervisorService::with_modules(vec![CLIENT_MODULE_ID]) } /// New service with list of modules that will report for being ready diff --git a/parity/main.rs b/parity/main.rs index a4530c830..a4f62e81c 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -44,6 +44,8 @@ extern crate ethcore_ipc_nano as nanoipc; extern crate hyper; // for price_info.rs extern crate json_ipc_server as jsonipc; +extern crate ethcore_ipc_hypervisor as hypervisor; + #[cfg(feature = "rpc")] extern crate ethcore_rpc; @@ -57,7 +59,6 @@ extern crate ethcore_signer; mod die; mod price_info; mod upgrade; -mod hypervisor; mod setup_log; mod rpc; mod dapps; @@ -212,7 +213,7 @@ fn execute_client(conf: Configuration, spec: Spec, client_config: ClientConfig) // Check fork settings. if conf.policy() != Policy::None { - warn!("Value given for --policy, yet no proposed forks exist. Ignoring."); + warn!("Value given for --policy, yet no proposed forks exist. Ignoring."); } let net_settings = conf.net_settings(&spec); From ee253c7a3f8f4a2502e91b08fe0e839f9a6a2d35 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 9 Jul 2016 17:22:20 +0200 Subject: [PATCH 12/16] Fix tests again. --- sync/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 97c0540de..97e3d29ee 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -38,7 +38,7 @@ //! use ethcore::client::{Client, ClientConfig}; //! use ethsync::{EthSync, SyncConfig}; //! use ethcore::ethereum; -//! use ethcore::miner::Miner; +//! use ethcore::miner::{GasPricer, Miner}; //! //! fn main() { //! let mut service = NetworkService::new(NetworkConfiguration::new()).unwrap(); From d7caae22416d26a20bdd83671ef787b490ad71c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Sun, 10 Jul 2016 13:18:33 +0200 Subject: [PATCH 13/16] Fixing clippy warnings (#1568) * Fixing clippy warnings * Fixing more warnings --- ethcore/src/client/client.rs | 4 ++-- ethcore/src/miner/miner.rs | 4 ++-- parity/configuration.rs | 8 +++---- parity/informant.rs | 1 - parity/main.rs | 1 + rpc/src/v1/impls/eth_filter.rs | 1 - rpc/src/v1/tests/mocked/eth.rs | 2 +- util/src/migration/mod.rs | 6 ++--- util/src/nibblevec.rs | 42 +++++++++++++++++++++++++--------- 9 files changed, 43 insertions(+), 26 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 043b35410..0c6a2c005 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -587,14 +587,14 @@ impl Client { } /// Notify us that the network has been started. - pub fn network_started(&self, url: &String) { + pub fn network_started(&self, url: &str) { let mut previous_enode = self.previous_enode.locked(); if let Some(ref u) = *previous_enode { if u == url { return; } } - *previous_enode = Some(url.clone()); + *previous_enode = Some(url.into()); info!(target: "mode", "Public node URL: {}", url.apply(Colour::White.bold())); } } diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 297f8227b..c5e4b4a5c 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -93,11 +93,11 @@ impl Default for MinerOptions { pub struct GasPriceCalibratorOptions { /// Base transaction price to match against. pub usd_per_tx: f32, - /// How frequently we should recalibrate. + /// How frequently we should recalibrate. pub recalibration_period: Duration, } -/// The gas price validator variant for a GasPricer. +/// The gas price validator variant for a `GasPricer`. pub struct GasPriceCalibrator { options: GasPriceCalibratorOptions, diff --git a/parity/configuration.rs b/parity/configuration.rs index ec5f99608..5bf632e41 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -158,21 +158,19 @@ impl Configuration { die!("{}: Invalid duration given. See parity --help for more information.", s) }; Duration::from_secs(match s { - "daily" => 24 * 60 * 60, "twice-daily" => 12 * 60 * 60, - "hourly" => 60 * 60, "half-hourly" => 30 * 60, "1second" | "1 second" | "second" => 1, "1minute" | "1 minute" | "minute" => 60, - "1hour" | "1 hour" | "hour" => 60 * 60, - "1day" | "1 day" | "day" => 24 * 60 * 60, + "hourly" | "1hour" | "1 hour" | "hour" => 60 * 60, + "daily" | "1day" | "1 day" | "day" => 24 * 60 * 60, x if x.ends_with("seconds") => FromStr::from_str(&x[0..x.len() - 7]).unwrap_or_else(bad), x if x.ends_with("minutes") => FromStr::from_str(&x[0..x.len() - 7]).unwrap_or_else(bad) * 60, x if x.ends_with("hours") => FromStr::from_str(&x[0..x.len() - 5]).unwrap_or_else(bad) * 60 * 60, x if x.ends_with("days") => FromStr::from_str(&x[0..x.len() - 4]).unwrap_or_else(bad) * 24 * 60 * 60, x => FromStr::from_str(x).unwrap_or_else(bad), }) - } + } pub fn gas_pricer(&self) -> GasPricer { match self.args.flag_gasprice.as_ref() { diff --git a/parity/informant.rs b/parity/informant.rs index 25ec5ca8b..5f2818f31 100644 --- a/parity/informant.rs +++ b/parity/informant.rs @@ -75,7 +75,6 @@ impl Informant { } } - #[cfg_attr(feature="dev", allow(match_bool))] pub fn tick(&self, client: &Client, maybe_sync: Option<(&EthSync, &NetworkService)>) where Message: Send + Sync + Clone + 'static { let elapsed = self.last_tick.unwrapped_read().elapsed(); if elapsed < Duration::from_secs(5) { diff --git a/parity/main.rs b/parity/main.rs index c36345034..0cf7dd67c 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -20,6 +20,7 @@ #![cfg_attr(feature="dev", feature(plugin))] #![cfg_attr(feature="dev", plugin(clippy))] #![cfg_attr(feature="dev", allow(useless_format))] +#![cfg_attr(feature="dev", allow(match_bool))] extern crate docopt; extern crate num_cpus; diff --git a/rpc/src/v1/impls/eth_filter.rs b/rpc/src/v1/impls/eth_filter.rs index 017e0ad32..c12783c1b 100644 --- a/rpc/src/v1/impls/eth_filter.rs +++ b/rpc/src/v1/impls/eth_filter.rs @@ -21,7 +21,6 @@ use std::sync::{Arc, Weak, Mutex}; use std::collections::HashSet; use jsonrpc_core::*; use util::Lockable; -use util::numbers::*; use ethcore::miner::MinerService; use ethcore::filter::Filter as EthcoreFilter; use ethcore::client::{BlockChainClient, BlockID}; diff --git a/rpc/src/v1/tests/mocked/eth.rs b/rpc/src/v1/tests/mocked/eth.rs index 9e0067c8c..112624baf 100644 --- a/rpc/src/v1/tests/mocked/eth.rs +++ b/rpc/src/v1/tests/mocked/eth.rs @@ -18,7 +18,7 @@ use std::str::FromStr; use std::collections::HashMap; use std::sync::{Arc, RwLock}; use jsonrpc_core::IoHandler; -use util::{Lockable, RwLockable}; +use util::RwLockable; use util::hash::{Address, H256, FixedHash}; use util::numbers::{Uint, U256}; use ethcore::account_provider::AccountProvider; diff --git a/util/src/migration/mod.rs b/util/src/migration/mod.rs index 4a51893c0..fddb51fca 100644 --- a/util/src/migration/mod.rs +++ b/util/src/migration/mod.rs @@ -92,7 +92,7 @@ impl Migration for T { } } - if batch.len() != 0 { + if !batch.is_empty() { try!(commit_batch(dest, &batch)); } @@ -189,12 +189,12 @@ impl Manager { // start with the old db. let old_path_str = try!(old_path.to_str().ok_or(Error::MigrationImpossible)); - let mut cur_db = try!(Database::open(&db_config, old_path_str).map_err(|s| Error::Custom(s))); + let mut cur_db = try!(Database::open(&db_config, old_path_str).map_err(Error::Custom)); for migration in migrations { // open the target temporary database. temp_path = temp_idx.path(&db_root); let temp_path_str = try!(temp_path.to_str().ok_or(Error::MigrationImpossible)); - let mut new_db = try!(Database::open(&db_config, temp_path_str).map_err(|s| Error::Custom(s))); + let mut new_db = try!(Database::open(&db_config, temp_path_str).map_err(Error::Custom)); // perform the migration from cur_db to new_db. try!(migration.migrate(&cur_db, &self.config, &mut new_db)); diff --git a/util/src/nibblevec.rs b/util/src/nibblevec.rs index dac022929..15bd60cb0 100644 --- a/util/src/nibblevec.rs +++ b/util/src/nibblevec.rs @@ -1,16 +1,33 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + + //! An owning, nibble-oriented byte vector. use ::NibbleSlice; -#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] -/// Owning, nibble-oriented byte vector. Counterpart to NibbleSlice. +#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Debug)] +/// Owning, nibble-oriented byte vector. Counterpart to `NibbleSlice`. pub struct NibbleVec { inner: Vec, len: usize, } impl NibbleVec { - /// Make a new NibbleVec + /// Make a new `NibbleVec` pub fn new() -> Self { NibbleVec { inner: Vec::new(), @@ -18,7 +35,7 @@ impl NibbleVec { } } - /// Make a NibbleVec with capacity for `n` nibbles. + /// Make a `NibbleVec` with capacity for `n` nibbles. pub fn with_capacity(n: usize) -> Self { NibbleVec { inner: Vec::with_capacity((n / 2) + (n % 2)), @@ -26,10 +43,13 @@ impl NibbleVec { } } - /// Length of the NibbleVec + /// Length of the `NibbleVec` pub fn len(&self) -> usize { self.len } - /// Capacity of the NibbleVec. + /// Retrurns true if `NibbleVec` has zero length + pub fn is_empty(&self) -> bool { self.len == 0 } + + /// Capacity of the `NibbleVec`. pub fn capacity(&self) -> usize { self.inner.capacity() * 2 } /// Try to get the nibble at the given offset. @@ -41,7 +61,7 @@ impl NibbleVec { } } - /// Push a nibble onto the NibbleVec. Ignores the high 4 bits. + /// Push a nibble onto the `NibbleVec`. Ignores the high 4 bits. pub fn push(&mut self, nibble: u8) { let nibble = nibble & 0x0F; @@ -54,9 +74,9 @@ impl NibbleVec { self.len += 1; } - /// Try to pop a nibble off the NibbleVec. Fails if len == 0. + /// Try to pop a nibble off the `NibbleVec`. Fails if len == 0. pub fn pop(&mut self) -> Option { - if self.len == 0 { + if self.is_empty() { return None; } @@ -72,7 +92,7 @@ impl NibbleVec { Some(nibble) } - /// Try to treat this NibbleVec as a NibbleSlice. Works only if len is even. + /// Try to treat this `NibbleVec` as a `NibbleSlice`. Works only if len is even. pub fn as_nibbleslice(&self) -> Option { if self.len % 2 == 0 { Some(NibbleSlice::new(self.inner())) @@ -127,4 +147,4 @@ mod tests { let v2: NibbleVec = v.as_nibbleslice().unwrap().into(); assert_eq!(v, v2); } -} \ No newline at end of file +} From e15f631ec702f976f2b75078bce4450a72d23776 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Sun, 10 Jul 2016 20:18:23 +0200 Subject: [PATCH 14/16] Some obvious evm & uint optimizations (#1576) * fix name and tests for endians * using renamed func * zero and sign opt --- ethcore/src/evm/interpreter/memory.rs | 8 +--- ethcore/src/evm/interpreter/mod.rs | 11 +++--- rpc/src/v1/types/uint.rs | 2 +- util/bigint/src/uint.rs | 57 ++++++++++++++++++++++++--- util/src/hash.rs | 4 +- 5 files changed, 62 insertions(+), 20 deletions(-) diff --git a/ethcore/src/evm/interpreter/memory.rs b/ethcore/src/evm/interpreter/memory.rs index a77a6d494..8195a0da3 100644 --- a/ethcore/src/evm/interpreter/memory.rs +++ b/ethcore/src/evm/interpreter/memory.rs @@ -94,13 +94,7 @@ impl Memory for Vec { fn write(&mut self, offset: U256, value: U256) { let off = offset.low_u64() as usize; - let mut val = value; - - let end = off + 32; - for pos in 0..32 { - self[end - pos - 1] = val.low_u64() as u8; - val = val >> 8; - } + value.to_big_endian(&mut self[off..off+32]); } fn write_byte(&mut self, offset: U256, value: U256) { diff --git a/ethcore/src/evm/interpreter/mod.rs b/ethcore/src/evm/interpreter/mod.rs index 2e03f44b8..e0021dc6b 100644 --- a/ethcore/src/evm/interpreter/mod.rs +++ b/ethcore/src/evm/interpreter/mod.rs @@ -303,9 +303,9 @@ impl Interpreter { let out_size = stack.pop_back(); // Add stipend (only CALL|CALLCODE when value > 0) - let call_gas = call_gas + value.map_or_else(|| Cost::from(0), |val| match val > U256::zero() { - true => Cost::from(ext.schedule().call_stipend), - false => Cost::from(0) + let call_gas = call_gas + value.map_or_else(|| Cost::from(0), |val| match val.is_zero() { + false => Cost::from(ext.schedule().call_stipend), + true => Cost::from(0) }); // Get sender & receive addresses, check if we have balance @@ -550,7 +550,7 @@ impl Interpreter { } fn is_zero(&self, val: &U256) -> bool { - &U256::zero() == val + val.is_zero() } fn bool_to_u256(&self, val: bool) -> U256 { @@ -782,7 +782,8 @@ impl Interpreter { } fn get_and_reset_sign(value: U256) -> (U256, bool) { - let sign = (value >> 255).low_u64() == 1; + let U256(arr) = value; + let sign = arr[3].leading_zeros() == 0; (set_sign(value, sign), sign) } diff --git a/rpc/src/v1/types/uint.rs b/rpc/src/v1/types/uint.rs index 5111f3a42..b1cb0521f 100644 --- a/rpc/src/v1/types/uint.rs +++ b/rpc/src/v1/types/uint.rs @@ -50,7 +50,7 @@ macro_rules! impl_uint { fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: serde::Serializer { let mut hex = "0x".to_owned(); let mut bytes = [0u8; 8 * $size]; - self.0.to_raw_bytes(&mut bytes); + self.0.to_big_endian(&mut bytes); let len = cmp::max((self.0.bits() + 7) / 8, 1); hex.push_str(&bytes[bytes.len() - len..].to_hex()); serializer.serialize_str(&hex) diff --git a/util/bigint/src/uint.rs b/util/bigint/src/uint.rs index 2f58061d4..8e08e590a 100644 --- a/util/bigint/src/uint.rs +++ b/util/bigint/src/uint.rs @@ -524,9 +524,8 @@ pub trait Uint: Sized + Default + FromStr + From + fmt::Debug + fmt::Displa fn bit(&self, index: usize) -> bool; /// Return single byte fn byte(&self, index: usize) -> u8; - /// Get this Uint as slice of bytes - fn to_raw_bytes(&self, bytes: &mut[u8]); - + /// Convert U256 to the sequence of bytes with a big endian + fn to_big_endian(&self, bytes: &mut[u8]); /// Create `Uint(10**n)` fn exp10(n: usize) -> Self; /// Return eponentation `self**other`. Panic on overflow. @@ -551,6 +550,9 @@ pub trait Uint: Sized + Default + FromStr + From + fmt::Debug + fmt::Displa /// Returns negation of this `Uint` and overflow (always true) fn overflowing_neg(self) -> (Self, bool); + + /// Returns + fn is_zero(&self) -> bool; } macro_rules! construct_uint { @@ -616,6 +618,13 @@ macro_rules! construct_uint { arr[0] } + #[inline] + fn is_zero(&self) -> bool { + let &$name(ref arr) = self; + for i in 0..$n_words { if arr[i] != 0 { return false; } } + return true; + } + /// Return the least number of bits needed to represent the number #[inline] fn bits(&self) -> usize { @@ -638,7 +647,7 @@ macro_rules! construct_uint { (arr[index / 8] >> (((index % 8)) * 8)) as u8 } - fn to_raw_bytes(&self, bytes: &mut[u8]) { + fn to_big_endian(&self, bytes: &mut[u8]) { assert!($n_words * 8 == bytes.len()); let &$name(ref arr) = self; for i in 0..bytes.len() { @@ -1454,7 +1463,7 @@ mod tests { let hex = "8090a0b0c0d0e0f00910203040506077583a2cf8264910e1436bda32571012f0"; let uint = U256::from_str(hex).unwrap(); let mut bytes = [0u8; 32]; - uint.to_raw_bytes(&mut bytes); + uint.to_big_endian(&mut bytes); let uint2 = U256::from(&bytes[..]); assert_eq!(uint, uint2); } @@ -2021,6 +2030,44 @@ mod tests { assert!(overflow); } + #[test] + fn big_endian() { + let source = U256([1, 0, 0, 0]); + let mut target = vec![0u8; 32]; + + assert_eq!(source, U256::from(1)); + + source.to_big_endian(&mut target); + assert_eq!( + vec![0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, + 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8], + target); + + let source = U256([512, 0, 0, 0]); + let mut target = vec![0u8; 32]; + + source.to_big_endian(&mut target); + assert_eq!( + vec![0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, + 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 2u8, 0u8], + target); + + let source = U256([0, 512, 0, 0]); + let mut target = vec![0u8; 32]; + + source.to_big_endian(&mut target); + assert_eq!( + vec![0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, + 0u8, 0u8, 0u8, 0u8, 0u8, 2u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8], + target); + + let source = U256::from_str("0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20").unwrap(); + source.to_big_endian(&mut target); + assert_eq!( + vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, + 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20], + target); + } #[test] #[cfg_attr(feature="dev", allow(cyclomatic_complexity))] diff --git a/util/src/hash.rs b/util/src/hash.rs index 95e8f8c8c..1b868b87b 100644 --- a/util/src/hash.rs +++ b/util/src/hash.rs @@ -445,7 +445,7 @@ macro_rules! impl_hash { impl From for H256 { fn from(value: U256) -> H256 { let mut ret = H256::new(); - value.to_raw_bytes(&mut ret); + value.to_big_endian(&mut ret); ret } } @@ -453,7 +453,7 @@ impl From for H256 { impl<'a> From<&'a U256> for H256 { fn from(value: &'a U256) -> H256 { let mut ret: H256 = H256::new(); - value.to_raw_bytes(&mut ret); + value.to_big_endian(&mut ret); ret } } From 2ed09de38eecba7731476cd25cf3fe5b4b6aed0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Mon, 11 Jul 2016 09:42:41 +0200 Subject: [PATCH 15/16] Very basic EVM binary. (#1574) * EVM binary - initial version * Adding missing documentation * Fixing warnings * Basic evmbin options * EVMbin crate. --- ethcore/src/evm/evm.rs | 6 + ethcore/src/evm/interpreter/mod.rs | 3 +- ethcore/src/lib.rs | 4 +- evmbin/Cargo.lock | 1063 ++++++++++++++++++++++++++++ evmbin/Cargo.toml | 11 + evmbin/src/ext.rs | 107 +++ evmbin/src/main.rs | 108 +++ 7 files changed, 1299 insertions(+), 3 deletions(-) create mode 100644 evmbin/Cargo.lock create mode 100644 evmbin/Cargo.toml create mode 100644 evmbin/src/ext.rs create mode 100644 evmbin/src/main.rs diff --git a/ethcore/src/evm/evm.rs b/ethcore/src/evm/evm.rs index 43919034e..3ec943f18 100644 --- a/ethcore/src/evm/evm.rs +++ b/ethcore/src/evm/evm.rs @@ -95,11 +95,17 @@ impl<'a> Finalize for Result> { } } +/// Cost calculation type. For low-gas usage we calculate costs using usize instead of U256 pub trait CostType: ops::Mul + ops::Div + ops::Add + ops::Sub + ops::Shr + ops::Shl + cmp::Ord + Sized + From + Copy { + /// Converts this cost into `U256` fn as_u256(&self) -> U256; + /// Tries to fit `U256` into this `Cost` type fn from_u256(val: U256) -> Result; + /// Convert to usize (may panic) fn as_usize(&self) -> usize; + /// Add with overflow fn overflow_add(self, other: Self) -> (Self, bool); + /// Multiple with overflow fn overflow_mul(self, other: Self) -> (Self, bool); } diff --git a/ethcore/src/evm/interpreter/mod.rs b/ethcore/src/evm/interpreter/mod.rs index e0021dc6b..f79d18b36 100644 --- a/ethcore/src/evm/interpreter/mod.rs +++ b/ethcore/src/evm/interpreter/mod.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -///! Rust VM implementation +//! Rust VM implementation #[cfg(not(feature = "evm-debug"))] macro_rules! evm_debug { @@ -182,6 +182,7 @@ impl Interpreter { instruction: instruction }); } + if info.tier == instructions::GasPriceTier::Invalid { return Err(evm::Error::BadInstruction { instruction: instruction diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index a2df34ca5..4f644db03 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -118,18 +118,18 @@ pub mod pod_state; pub mod engine; pub mod migrations; pub mod miner; +#[macro_use] pub mod evm; +pub mod action_params; mod blooms; mod db; mod common; mod basic_types; -#[macro_use] mod evm; mod env_info; mod pod_account; mod state; mod account; mod account_db; -mod action_params; mod null_engine; mod builtin; mod substate; diff --git a/evmbin/Cargo.lock b/evmbin/Cargo.lock new file mode 100644 index 000000000..14c6d9bcb --- /dev/null +++ b/evmbin/Cargo.lock @@ -0,0 +1,1063 @@ +[root] +name = "evm" +version = "0.1.0" +dependencies = [ + "docopt 0.6.81 (registry+https://github.com/rust-lang/crates.io-index)", + "ethcore 1.3.0", + "ethcore-util 1.3.0", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aho-corasick" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ansi_term" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "arrayvec" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nodrop 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "odds 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aster" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bigint" +version = "0.1.0" +dependencies = [ + "heapsize 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bitflags" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "blastfig" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bloomchain" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bytes" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cfg-if" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "chrono" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cookie" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "crossbeam" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "deque" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "docopt" +version = "0.6.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "regex 0.1.68 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "elastic-array" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "env_logger" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.68 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "eth-secp256k1" +version = "0.5.4" +source = "git+https://github.com/ethcore/rust-secp256k1#a9a0b1be1f39560ca86e8fc8e55e205a753ff25c" +dependencies = [ + "arrayvec 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.28 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ethash" +version = "1.3.0" +dependencies = [ + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "primal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "sha3 0.1.0", +] + +[[package]] +name = "ethcore" +version = "1.3.0" +dependencies = [ + "bloomchain 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "ethash 1.3.0", + "ethcore-devtools 1.3.0", + "ethcore-ipc 1.3.0", + "ethcore-ipc-codegen 1.3.0", + "ethcore-ipc-nano 1.3.0", + "ethcore-util 1.3.0", + "ethjson 0.1.0", + "ethstore 0.1.0", + "heapsize 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "hyper 0.9.4 (git+https://github.com/ethcore/hyper)", + "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ethcore-devtools" +version = "1.3.0" +dependencies = [ + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ethcore-ipc" +version = "1.3.0" +dependencies = [ + "ethcore-devtools 1.3.0", + "ethcore-util 1.3.0", + "nanomsg 0.5.1 (git+https://github.com/ethcore/nanomsg.rs.git)", + "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ethcore-ipc-codegen" +version = "1.3.0" +dependencies = [ + "aster 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", + "quasi 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "quasi_codegen 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ethcore-ipc-nano" +version = "1.3.0" +dependencies = [ + "ethcore-ipc 1.3.0", + "jsonrpc-core 2.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nanomsg 0.5.1 (git+https://github.com/ethcore/nanomsg.rs.git)", +] + +[[package]] +name = "ethcore-util" +version = "1.3.0" +dependencies = [ + "ansi_term 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "arrayvec 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", + "bigint 0.1.0", + "chrono 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", + "elastic-array 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "eth-secp256k1 0.5.4 (git+https://github.com/ethcore/rust-secp256k1)", + "ethcore-devtools 1.3.0", + "heapsize 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "igd 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.5.1 (git+https://github.com/ethcore/mio?branch=v0.5.x)", + "nix 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rocksdb 0.4.5 (git+https://github.com/ethcore/rust-rocksdb)", + "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "sha3 0.1.0", + "slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "table 0.1.0", + "target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "tiny-keccak 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "using_queue 0.1.0", + "vergen 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ethjson" +version = "0.1.0" +dependencies = [ + "ethcore-util 1.3.0", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_codegen 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ethkey" +version = "0.2.0" +dependencies = [ + "eth-secp256k1 0.5.4 (git+https://github.com/ethcore/rust-secp256k1)", + "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "tiny-keccak 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ethstore" +version = "0.1.0" +dependencies = [ + "ethkey 0.2.0", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_codegen 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tiny-keccak 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gcc" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "hamming" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "heapsize" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "hpack" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "httparse" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "hyper" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cookie 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "solicit 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "traitobject 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "url 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "hyper" +version = "0.9.4" +source = "git+https://github.com/ethcore/hyper#9e346c1d4bc30cd4142dea9d8a0b117d30858ca4" +dependencies = [ + "cookie 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "httparse 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rotor 0.6.3 (git+https://github.com/ethcore/rotor)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "spmc 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "traitobject 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "vecio 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "idna" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-bidi 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "igd" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "hyper 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.68 (registry+https://github.com/rust-lang/crates.io-index)", + "xml-rs 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)", + "xmltree 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "itertools" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "jsonrpc-core" +version = "2.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_codegen 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "language-tags" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "lazy_static" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "log" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "matches" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "memchr" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "mime" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "mio" +version = "0.5.1" +source = "git+https://github.com/ethcore/mio?branch=v0.5.x#3842d3b250ffd7bd9b16f9586b875ddcbac2b0dd" +dependencies = [ + "bytes 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "miow 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "net2 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "miow" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "net2 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "nanomsg" +version = "0.5.1" +source = "git+https://github.com/ethcore/nanomsg.rs.git#c40fe442c9afaea5b38009a3d992ca044dcceb00" +dependencies = [ + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "nanomsg-sys 0.5.0 (git+https://github.com/ethcore/nanomsg.rs.git)", +] + +[[package]] +name = "nanomsg-sys" +version = "0.5.0" +source = "git+https://github.com/ethcore/nanomsg.rs.git#c40fe442c9afaea5b38009a3d992ca044dcceb00" +dependencies = [ + "gcc 0.3.28 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "net2" +version = "0.2.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "nix" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "nodrop" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "odds 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "nom" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "num" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-bigint 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-complex 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-bigint" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-complex" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-integer" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-iter" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-rational" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-bigint 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "num_cpus" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "odds" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "primal" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "primal-check 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "primal-estimate 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "primal-sieve 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "primal-bit" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "hamming 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "primal-check" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "primal-estimate" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "primal-sieve" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "hamming 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "primal-bit 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "primal-estimate 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quasi" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quasi_codegen" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aster 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quick-error" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rayon" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "deque 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "0.1.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rocksdb" +version = "0.4.5" +source = "git+https://github.com/ethcore/rust-rocksdb#9be41e05923616dfa28741c58b22776d479751e6" +dependencies = [ + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "rocksdb-sys 0.3.0 (git+https://github.com/ethcore/rust-rocksdb)", +] + +[[package]] +name = "rocksdb-sys" +version = "0.3.0" +source = "git+https://github.com/ethcore/rust-rocksdb#9be41e05923616dfa28741c58b22776d479751e6" +dependencies = [ + "gcc 0.3.28 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rotor" +version = "0.6.3" +source = "git+https://github.com/ethcore/rotor#e63d45137b2eb66d1e085a7c6321a5db8b187576" +dependencies = [ + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.5.1 (git+https://github.com/ethcore/mio?branch=v0.5.x)", + "quick-error 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rust-crypto" +version = "0.2.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.28 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rustc_version" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "semver" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nom 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde_codegen" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aster 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", + "quasi 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "quasi_codegen 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sha3" +version = "0.1.0" +dependencies = [ + "gcc 0.3.28 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "slab" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "slab" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "solicit" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "hpack 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "spmc" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "strsim" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "syntex" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syntex_syntax 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syntex_syntax" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "table" +version = "0.1.0" + +[[package]] +name = "target_info" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "term" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread-id" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "time" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tiny-keccak" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "traitobject" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "typeable" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicase" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-bidi" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-xid" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "url" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-bidi 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "uuid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "url" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "idna 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "using_queue" +version = "0.1.0" + +[[package]] +name = "utf8-ranges" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "uuid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "vecio" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "vergen" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "blastfig 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "ws2_32-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "xml-rs" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "xml-rs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "xmltree" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "xml-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + diff --git a/evmbin/Cargo.toml b/evmbin/Cargo.toml new file mode 100644 index 000000000..3e531f5d3 --- /dev/null +++ b/evmbin/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "evm" +description = "Parity's EVM implementation" +version = "0.1.0" +authors = ["Ethcore "] + +[dependencies] +rustc-serialize = "0.3" +docopt = { version = "0.6" } +ethcore = { path = "../ethcore" } +ethcore-util = { path = "../util" } diff --git a/evmbin/src/ext.rs b/evmbin/src/ext.rs new file mode 100644 index 000000000..0ea932279 --- /dev/null +++ b/evmbin/src/ext.rs @@ -0,0 +1,107 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! Externalities implementation. + +use std::collections::HashMap; +use util::{U256, H256, Address, Bytes, FixedHash}; +use ethcore::client::EnvInfo; +use ethcore::evm::{self, Ext, ContractCreateResult, MessageCallResult, Schedule}; + +pub struct FakeExt { + schedule: Schedule, + store: HashMap, +} + +impl Default for FakeExt { + fn default() -> Self { + FakeExt { + schedule: Schedule::new_homestead(), + store: HashMap::new(), + } + } +} + +impl Ext for FakeExt { + fn storage_at(&self, key: &H256) -> H256 { + self.store.get(key).unwrap_or(&H256::new()).clone() + } + + fn set_storage(&mut self, key: H256, value: H256) { + self.store.insert(key, value); + } + + fn exists(&self, _address: &Address) -> bool { + unimplemented!(); + } + + fn balance(&self, _address: &Address) -> U256 { + unimplemented!(); + } + + fn blockhash(&self, _number: &U256) -> H256 { + unimplemented!(); + } + + fn create(&mut self, _gas: &U256, _value: &U256, _code: &[u8]) -> ContractCreateResult { + unimplemented!(); + } + + fn call(&mut self, + _gas: &U256, + _sender_address: &Address, + _receive_address: &Address, + _value: Option, + _data: &[u8], + _code_address: &Address, + _output: &mut [u8]) -> MessageCallResult { + unimplemented!(); + } + + fn extcode(&self, _address: &Address) -> Bytes { + unimplemented!(); + } + + fn log(&mut self, _topics: Vec, _data: &[u8]) { + unimplemented!(); + } + + fn ret(self, gas: &U256, _data: &[u8]) -> evm::Result { + Ok(*gas) + } + + fn suicide(&mut self, _refund_address: &Address) { + unimplemented!(); + } + + fn schedule(&self) -> &Schedule { + &self.schedule + } + + fn env_info(&self) -> &EnvInfo { + unimplemented!() + } + + fn depth(&self) -> usize { + unimplemented!(); + // self.depth + } + + fn inc_sstore_clears(&mut self) { + unimplemented!(); + // self.sstore_clears += 1; + } +} diff --git a/evmbin/src/main.rs b/evmbin/src/main.rs new file mode 100644 index 000000000..3fa06d004 --- /dev/null +++ b/evmbin/src/main.rs @@ -0,0 +1,108 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! Parity EVM interpreter binary. + +#![warn(missing_docs)] +extern crate ethcore; +extern crate rustc_serialize; +extern crate docopt; +#[macro_use] +extern crate ethcore_util as util; + +mod ext; + +use std::time::Instant; +use std::str::FromStr; +use docopt::Docopt; +use util::{U256, FromHex, Uint, Bytes}; +use ethcore::evm::{Factory, VMType, Finalize}; +use ethcore::action_params::ActionParams; + +const USAGE: &'static str = r#" +EVM implementation for Parity. + Copyright 2016 Ethcore (UK) Limited + +Usage: + evmbin stats [options] + evmbin [-h | --help] + +Transaction options: + --code CODE Contract code. + --input DATA Input data. + --gas GAS Supplied gas. + +General options: + -h, --help Display this message and exit. +"#; + + +fn main() { + let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit()); + + let mut params = ActionParams::default(); + params.gas = args.gas(); + params.code = Some(args.code()); + params.data = args.data(); + + let factory = Factory::new(VMType::Interpreter); + let mut vm = factory.create(params.gas); + let mut ext = ext::FakeExt::default(); + + let start = Instant::now(); + let gas_left = vm.exec(params, &mut ext).finalize(ext).expect("OK"); + let duration = start.elapsed(); + + println!("Gas used: {:?}", args.gas() - gas_left); + println!("Output: {:?}", ""); + println!("Time: {}.{:.9}s", duration.as_secs(), duration.subsec_nanos()); +} + +#[derive(Debug, RustcDecodable)] +struct Args { + cmd_stats: bool, + flag_code: Option, + flag_gas: Option, + flag_input: Option, +} + +impl Args { + pub fn gas(&self) -> U256 { + self.flag_gas + .clone() + .and_then(|g| U256::from_str(&g).ok()) + .unwrap_or_else(|| !U256::zero()) + } + + pub fn code(&self) -> Bytes { + self.flag_code + .clone() + .and_then(|c| c.from_hex().ok()) + .unwrap_or_else(|| die("Code is required.")) + } + + pub fn data(&self) -> Option { + self.flag_input + .clone() + .and_then(|d| d.from_hex().ok()) + } +} + + +fn die(msg: &'static str) -> ! { + println!("{}", msg); + ::std::process::exit(-1) +} From bdf44461737b9bb5e58ee8dc4856d372f866634b Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Mon, 11 Jul 2016 09:46:33 +0200 Subject: [PATCH 16/16] have AccountDB use address hash for uniqueness (#1533) * partially done alternate migration scheme * finish altering migration framework * migrate tests to new migration framework * address comments * remove superfluous newline [ci skip] * TempIdx -> TempIndex [ci skip] * modify account_db to work on address hash, not address * add a database migration for new accountdb * preserve first 96 bits of keys when combining * handle metadata keys in migration and preserve first 96 bits * fix comments and hash address instead of hash * different migrations based on pruning * migrations mutably borrow self * batch abstraction for migration * added missing licence headers * overlay recent v7 migration * better error handling, migrate version key as well * fix migration tests * commit final batch and migrate journaled insertions * two passes on journal to migrate all possible deleted keys --- ethcore/src/account_db.rs | 77 ++++++--- ethcore/src/migrations/extras/mod.rs | 18 +- ethcore/src/migrations/extras/v6.rs | 18 +- ethcore/src/migrations/mod.rs | 1 + ethcore/src/migrations/state/mod.rs | 21 +++ ethcore/src/migrations/state/v7.rs | 247 +++++++++++++++++++++++++++ parity/configuration.rs | 22 ++- parity/die.rs | 2 +- parity/main.rs | 2 +- parity/migration.rs | 56 ++++-- parity/rpc_apis.rs | 1 - util/src/migration/mod.rs | 82 +++++---- util/src/migration/tests.rs | 4 +- 13 files changed, 468 insertions(+), 83 deletions(-) create mode 100644 ethcore/src/migrations/state/mod.rs create mode 100644 ethcore/src/migrations/state/v7.rs diff --git a/ethcore/src/account_db.rs b/ethcore/src/account_db.rs index 7337940da..cee2b4d48 100644 --- a/ethcore/src/account_db.rs +++ b/ethcore/src/account_db.rs @@ -1,26 +1,59 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + //! DB backend wrapper for Account trie use util::*; static NULL_RLP_STATIC: [u8; 1] = [0x80; 1]; +// combines a key with an address hash to ensure uniqueness. +// leaves the first 96 bits untouched in order to support partial key lookup. +#[inline] +fn combine_key<'a>(address_hash: &'a H256, key: &'a H256) -> H256 { + let mut dst = key.clone(); + { + let last_src: &[u8] = &*address_hash; + let last_dst: &mut [u8] = &mut *dst; + for (k, a) in last_dst[12..].iter_mut().zip(&last_src[12..]) { + *k ^= *a + } + } + + dst +} + // TODO: introduce HashDBMut? /// DB backend wrapper for Account trie /// Transforms trie node keys for the database pub struct AccountDB<'db> { db: &'db HashDB, - address: H256, -} - -#[inline] -fn combine_key<'a>(address: &'a H256, key: &'a H256) -> H256 { - address ^ key + address_hash: H256, } impl<'db> AccountDB<'db> { - pub fn new(db: &'db HashDB, address: &Address) -> AccountDB<'db> { + /// Create a new AccountDB from an address. + pub fn new(db: &'db HashDB, address: &Address) -> Self { + Self::from_hash(db, address.sha3()) + } + + /// Create a new AcountDB from an address' hash. + pub fn from_hash(db: &'db HashDB, address_hash: H256) -> Self { AccountDB { db: db, - address: address.into(), + address_hash: address_hash, } } } @@ -34,14 +67,14 @@ impl<'db> HashDB for AccountDB<'db>{ if key == &SHA3_NULL_RLP { return Some(&NULL_RLP_STATIC); } - self.db.get(&combine_key(&self.address, key)) + self.db.get(&combine_key(&self.address_hash, key)) } fn contains(&self, key: &H256) -> bool { if key == &SHA3_NULL_RLP { return true; } - self.db.contains(&combine_key(&self.address, key)) + self.db.contains(&combine_key(&self.address_hash, key)) } fn insert(&mut self, _value: &[u8]) -> H256 { @@ -60,20 +93,26 @@ impl<'db> HashDB for AccountDB<'db>{ /// DB backend wrapper for Account trie pub struct AccountDBMut<'db> { db: &'db mut HashDB, - address: H256, + address_hash: H256, } impl<'db> AccountDBMut<'db> { - pub fn new(db: &'db mut HashDB, address: &Address) -> AccountDBMut<'db> { + /// Create a new AccountDB from an address. + pub fn new(db: &'db mut HashDB, address: &Address) -> Self { + Self::from_hash(db, address.sha3()) + } + + /// Create a new AcountDB from an address' hash. + pub fn from_hash(db: &'db mut HashDB, address_hash: H256) -> Self { AccountDBMut { db: db, - address: address.into(), + address_hash: address_hash, } } #[allow(dead_code)] pub fn immutable(&'db self) -> AccountDB<'db> { - AccountDB { db: self.db, address: self.address.clone() } + AccountDB { db: self.db, address_hash: self.address_hash.clone() } } } @@ -86,14 +125,14 @@ impl<'db> HashDB for AccountDBMut<'db>{ if key == &SHA3_NULL_RLP { return Some(&NULL_RLP_STATIC); } - self.db.get(&combine_key(&self.address, key)) + self.db.get(&combine_key(&self.address_hash, key)) } fn contains(&self, key: &H256) -> bool { if key == &SHA3_NULL_RLP { return true; } - self.db.contains(&combine_key(&self.address, key)) + self.db.contains(&combine_key(&self.address_hash, key)) } fn insert(&mut self, value: &[u8]) -> H256 { @@ -101,7 +140,7 @@ impl<'db> HashDB for AccountDBMut<'db>{ return SHA3_NULL_RLP.clone(); } let k = value.sha3(); - let ak = combine_key(&self.address, &k); + let ak = combine_key(&self.address_hash, &k); self.db.emplace(ak, value.to_vec()); k } @@ -110,7 +149,7 @@ impl<'db> HashDB for AccountDBMut<'db>{ if key == SHA3_NULL_RLP { return; } - let key = combine_key(&self.address, &key); + let key = combine_key(&self.address_hash, &key); self.db.emplace(key, value.to_vec()) } @@ -118,7 +157,7 @@ impl<'db> HashDB for AccountDBMut<'db>{ if key == &SHA3_NULL_RLP { return; } - let key = combine_key(&self.address, key); + let key = combine_key(&self.address_hash, key); self.db.remove(&key) } } diff --git a/ethcore/src/migrations/extras/mod.rs b/ethcore/src/migrations/extras/mod.rs index c4d4790dc..0635596ea 100644 --- a/ethcore/src/migrations/extras/mod.rs +++ b/ethcore/src/migrations/extras/mod.rs @@ -1,5 +1,21 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + //! Extras database migrations. mod v6; -pub use self::v6::ToV6; +pub use self::v6::ToV6; \ No newline at end of file diff --git a/ethcore/src/migrations/extras/v6.rs b/ethcore/src/migrations/extras/v6.rs index 3b8160e67..af2d0389b 100644 --- a/ethcore/src/migrations/extras/v6.rs +++ b/ethcore/src/migrations/extras/v6.rs @@ -1,3 +1,19 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + use util::migration::SimpleMigration; /// This migration reduces the sizes of keys and moves `ExtrasIndex` byte from back to the front. @@ -22,7 +38,7 @@ impl SimpleMigration for ToV6 { 6 } - fn simple_migrate(&self, key: Vec, value: Vec) -> Option<(Vec, Vec)> { + fn simple_migrate(&mut self, key: Vec, value: Vec) -> Option<(Vec, Vec)> { //// at this version all extras keys are 33 bytes long. if key.len() == 33 { diff --git a/ethcore/src/migrations/mod.rs b/ethcore/src/migrations/mod.rs index 1473ced9c..6d86a122f 100644 --- a/ethcore/src/migrations/mod.rs +++ b/ethcore/src/migrations/mod.rs @@ -1,3 +1,4 @@ //! Database migrations. pub mod extras; +pub mod state; diff --git a/ethcore/src/migrations/state/mod.rs b/ethcore/src/migrations/state/mod.rs new file mode 100644 index 000000000..9a6b9e086 --- /dev/null +++ b/ethcore/src/migrations/state/mod.rs @@ -0,0 +1,21 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! State database migrations. + +mod v7; + +pub use self::v7::{ArchiveV7, OverlayRecentV7}; \ No newline at end of file diff --git a/ethcore/src/migrations/state/v7.rs b/ethcore/src/migrations/state/v7.rs new file mode 100644 index 000000000..45a5669b2 --- /dev/null +++ b/ethcore/src/migrations/state/v7.rs @@ -0,0 +1,247 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! This migration migrates the state db to use an accountdb which ensures uniqueness +//! using an address' hash as opposed to the address itself. + +use std::collections::HashMap; + +use util::Bytes; +use util::hash::{Address, FixedHash, H256}; +use util::kvdb::Database; +use util::migration::{Batch, Config, Error, Migration, SimpleMigration}; +use util::rlp::{decode, Rlp, RlpStream, Stream, View}; +use util::sha3::Hashable; + +// attempt to migrate a key, value pair. None if migration not possible. +fn attempt_migrate(mut key_h: H256, val: &[u8]) -> Option { + let val_hash = val.sha3(); + + if key_h != val_hash { + // this is a key which has been xor'd with an address. + // recover the address. + let address = key_h ^ val_hash; + + // check that the address is actually a 20-byte value. + // the leftmost 12 bytes should be zero. + if &address[0..12] != &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] { + return None; + } + + let address_hash = Address::from(address).sha3(); + + // create the xor'd key in place. + key_h.copy_from_slice(&*val_hash); + assert_eq!(key_h, val_hash); + + { + let last_src: &[u8] = &*address_hash; + let last_dst: &mut [u8] = &mut *key_h; + for (k, a) in last_dst[12..].iter_mut().zip(&last_src[12..]) { + *k ^= *a; + } + } + + Some(key_h) + } else { + None + } +} + +/// Version for ArchiveDB. +pub struct ArchiveV7; + +impl SimpleMigration for ArchiveV7 { + fn version(&self) -> u32 { + 7 + } + + fn simple_migrate(&mut self, key: Vec, value: Vec) -> Option<(Vec, Vec)> { + if key.len() != 32 { + // metadata key, ignore. + return Some((key, value)); + } + + let key_h = H256::from_slice(&key[..]); + if let Some(new_key) = attempt_migrate(key_h, &value[..]) { + Some((new_key[..].to_owned(), value)) + } else { + Some((key, value)) + } + } +} + +// magic numbers and constants for overlay-recent at v6. +// re-written here because it may change in the journaldb module. +const V7_LATEST_ERA_KEY: &'static [u8] = &[ b'l', b'a', b's', b't', 0, 0, 0, 0, 0, 0, 0, 0 ]; +const V7_VERSION_KEY: &'static [u8] = &[ b'j', b'v', b'e', b'r', 0, 0, 0, 0, 0, 0, 0, 0 ]; +const DB_VERSION: u32 = 0x203; +const PADDING : [u8; 10] = [0u8; 10]; + +/// Version for OverlayRecent database. +/// more involved than the archive version because of journaling. +#[derive(Default)] +pub struct OverlayRecentV7 { + migrated_keys: HashMap, +} + +impl OverlayRecentV7 { + // walk all journal entries in the database backwards. + // find migrations for any possible inserted keys. + fn walk_journal(&mut self, source: &Database) -> Result<(), Error> { + if let Some(val) = try!(source.get(V7_LATEST_ERA_KEY).map_err(Error::Custom)) { + let mut era = decode::(&val); + loop { + let mut index: usize = 0; + loop { + let entry_key = { + let mut r = RlpStream::new_list(3); + r.append(&era).append(&index).append(&&PADDING[..]); + r.out() + }; + + if let Some(journal_raw) = try!(source.get(&entry_key).map_err(Error::Custom)) { + let rlp = Rlp::new(&journal_raw); + + // migrate all inserted keys. + for r in rlp.at(1).iter() { + let key: H256 = r.val_at(0); + let v: Bytes = r.val_at(1); + + if self.migrated_keys.get(&key).is_none() { + if let Some(new_key) = attempt_migrate(key, &v) { + self.migrated_keys.insert(key, new_key); + } + } + } + index += 1; + } else { + break; + } + } + + if index == 0 || era == 0 { + break; + } + era -= 1; + } + } + Ok(()) + } + + // walk all journal entries in the database backwards. + // replace all possible inserted/deleted keys with their migrated counterparts + // and commit the altered entries. + fn migrate_journal(&self, source: &Database, mut batch: Batch, dest: &mut Database) -> Result<(), Error> { + if let Some(val) = try!(source.get(V7_LATEST_ERA_KEY).map_err(Error::Custom)) { + try!(batch.insert(V7_LATEST_ERA_KEY.into(), val.to_owned(), dest)); + + let mut era = decode::(&val); + loop { + let mut index: usize = 0; + loop { + let entry_key = { + let mut r = RlpStream::new_list(3); + r.append(&era).append(&index).append(&&PADDING[..]); + r.out() + }; + + if let Some(journal_raw) = try!(source.get(&entry_key).map_err(Error::Custom)) { + let rlp = Rlp::new(&journal_raw); + let id: H256 = rlp.val_at(0); + let mut inserted_keys: Vec<(H256, Bytes)> = Vec::new(); + + // migrate all inserted keys. + for r in rlp.at(1).iter() { + let mut key: H256 = r.val_at(0); + let v: Bytes = r.val_at(1); + + if let Some(new_key) = self.migrated_keys.get(&key) { + key = *new_key; + } + + inserted_keys.push((key, v)); + } + + // migrate all deleted keys. + let mut deleted_keys: Vec = rlp.val_at(2); + for old_key in &mut deleted_keys { + if let Some(new) = self.migrated_keys.get(&*old_key) { + *old_key = new.clone(); + } + } + + // rebuild the journal entry rlp. + let mut stream = RlpStream::new_list(3); + stream.append(&id); + stream.begin_list(inserted_keys.len()); + for (k, v) in inserted_keys { + stream.begin_list(2).append(&k).append(&v); + } + + stream.append(&deleted_keys); + + // and insert it into the new database. + try!(batch.insert(entry_key, stream.out(), dest)); + + index += 1; + } else { + break; + } + } + + if index == 0 || era == 0 { + break; + } + era -= 1; + } + } + batch.commit(dest) + } +} + +impl Migration for OverlayRecentV7 { + fn version(&self) -> u32 { 7 } + + // walk all records in the database, attempting to migrate any possible and + // keeping records of those that we do. then migrate the journal using + // this information. + fn migrate(&mut self, source: &Database, config: &Config, dest: &mut Database) -> Result<(), Error> { + let mut batch = Batch::new(config); + + // check version metadata. + match try!(source.get(V7_VERSION_KEY).map_err(Error::Custom)) { + Some(ref version) if decode::(&*version) == DB_VERSION => {} + _ => return Err(Error::MigrationImpossible), // missing or wrong version + } + + for (key, value) in source.iter() { + let mut key = key.into_vec(); + if key.len() == 32 { + let key_h = H256::from_slice(&key[..]); + if let Some(new_key) = attempt_migrate(key_h.clone(), &value) { + self.migrated_keys.insert(key_h, new_key); + key.copy_from_slice(&new_key[..]); + } + } + + try!(batch.insert(key, value.into_vec(), dest)); + } + + try!(self.walk_journal(source)); + self.migrate_journal(source, batch, dest) + } +} \ No newline at end of file diff --git a/parity/configuration.rs b/parity/configuration.rs index 5bf632e41..3f4955d50 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -295,7 +295,7 @@ impl Configuration { ret } - pub fn find_best_db(&self, spec: &Spec) -> Option { + fn find_best_db(&self, spec: &Spec) -> Option { let mut ret = None; let mut latest_era = None; let jdb_types = [journaldb::Algorithm::Archive, journaldb::Algorithm::EarlyMerge, journaldb::Algorithm::OverlayRecent, journaldb::Algorithm::RefCounted]; @@ -314,6 +314,17 @@ impl Configuration { ret } + pub fn pruning_algorithm(&self, spec: &Spec) -> journaldb::Algorithm { + match self.args.flag_pruning.as_str() { + "archive" => journaldb::Algorithm::Archive, + "light" => journaldb::Algorithm::EarlyMerge, + "fast" => journaldb::Algorithm::OverlayRecent, + "basic" => journaldb::Algorithm::RefCounted, + "auto" => self.find_best_db(spec).unwrap_or(journaldb::Algorithm::OverlayRecent), + _ => { die!("Invalid pruning method given."); } + } + } + pub fn client_config(&self, spec: &Spec) -> ClientConfig { let mut client_config = ClientConfig::default(); @@ -341,14 +352,7 @@ impl Configuration { // forced trace db cache size if provided client_config.tracing.db_cache_size = self.args.flag_db_cache_size.and_then(|cs| Some(cs / 4)); - client_config.pruning = match self.args.flag_pruning.as_str() { - "archive" => journaldb::Algorithm::Archive, - "light" => journaldb::Algorithm::EarlyMerge, - "fast" => journaldb::Algorithm::OverlayRecent, - "basic" => journaldb::Algorithm::RefCounted, - "auto" => self.find_best_db(spec).unwrap_or(journaldb::Algorithm::OverlayRecent), - _ => { die!("Invalid pruning method given."); } - }; + client_config.pruning = self.pruning_algorithm(spec); if self.args.flag_fat_db { if let journaldb::Algorithm::Archive = client_config.pruning { diff --git a/parity/die.rs b/parity/die.rs index c38b041f3..80b31f619 100644 --- a/parity/die.rs +++ b/parity/die.rs @@ -22,7 +22,7 @@ use std::process::exit; #[macro_export] macro_rules! die { - ($($arg:tt)*) => (die_with_message(&format!("{}", format_args!($($arg)*)))); + ($($arg:tt)*) => (::die::die_with_message(&format!("{}", format_args!($($arg)*)))); } pub fn die_with_error(module: &'static str, e: ethcore::error::Error) -> ! { diff --git a/parity/main.rs b/parity/main.rs index 0cf7dd67c..45c60234d 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -174,7 +174,7 @@ fn execute_upgrades(conf: &Configuration, spec: &Spec, client_config: &ClientCon } let db_path = get_db_path(Path::new(&conf.path()), client_config.pruning, spec.genesis_header().hash()); - let result = migrate(&db_path); + let result = migrate(&db_path, client_config.pruning); if let Err(err) = result { die_with_message(&format!("{}", err)); } diff --git a/parity/migration.rs b/parity/migration.rs index 6b5c7de99..853cb6e35 100644 --- a/parity/migration.rs +++ b/parity/migration.rs @@ -17,15 +17,16 @@ use std::fs; use std::fs::File; use std::io::{Read, Write, Error as IoError, ErrorKind}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::fmt::{Display, Formatter, Error as FmtError}; +use util::journaldb::Algorithm; use util::migration::{Manager as MigrationManager, Config as MigrationConfig, Error as MigrationError}; use ethcore::migrations; /// Database is assumed to be at default version, when no version file is found. const DEFAULT_VERSION: u32 = 5; /// Current version of database models. -const CURRENT_VERSION: u32 = 6; +const CURRENT_VERSION: u32 = 7; /// Defines how many items are migrated to the new version of database at once. const BATCH_SIZE: usize = 1024; /// Version file name. @@ -74,15 +75,15 @@ impl From for Error { } /// Returns the version file path. -fn version_file_path(path: &PathBuf) -> PathBuf { - let mut file_path = path.clone(); +fn version_file_path(path: &Path) -> PathBuf { + let mut file_path = path.to_owned(); file_path.push(VERSION_FILE_NAME); file_path } /// Reads current database version from the file at given path. /// If the file does not exist returns `DEFAULT_VERSION`. -fn current_version(path: &PathBuf) -> Result { +fn current_version(path: &Path) -> Result { match File::open(version_file_path(path)) { Err(ref err) if err.kind() == ErrorKind::NotFound => Ok(DEFAULT_VERSION), Err(_) => Err(Error::UnknownDatabaseVersion), @@ -96,7 +97,7 @@ fn current_version(path: &PathBuf) -> Result { /// Writes current database version to the file. /// Creates a new file if the version file does not exist yet. -fn update_version(path: &PathBuf) -> Result<(), Error> { +fn update_version(path: &Path) -> Result<(), Error> { try!(fs::create_dir_all(path)); let mut file = try!(File::create(version_file_path(path))); try!(file.write_all(format!("{}", CURRENT_VERSION).as_bytes())); @@ -104,22 +105,29 @@ fn update_version(path: &PathBuf) -> Result<(), Error> { } /// Blocks database path. -fn blocks_database_path(path: &PathBuf) -> PathBuf { - let mut blocks_path = path.clone(); +fn blocks_database_path(path: &Path) -> PathBuf { + let mut blocks_path = path.to_owned(); blocks_path.push("blocks"); blocks_path } /// Extras database path. -fn extras_database_path(path: &PathBuf) -> PathBuf { - let mut extras_path = path.clone(); +fn extras_database_path(path: &Path) -> PathBuf { + let mut extras_path = path.to_owned(); extras_path.push("extras"); extras_path } +/// State database path. +fn state_database_path(path: &Path) -> PathBuf { + let mut state_path = path.to_owned(); + state_path.push("state"); + state_path +} + /// Database backup -fn backup_database_path(path: &PathBuf) -> PathBuf { - let mut backup_path = path.clone(); +fn backup_database_path(path: &Path) -> PathBuf { + let mut backup_path = path.to_owned(); backup_path.pop(); backup_path.push("temp_backup"); backup_path @@ -132,21 +140,34 @@ fn default_migration_settings() -> MigrationConfig { } } -/// Migrations on blocks database. +/// Migrations on the blocks database. fn blocks_database_migrations() -> Result { let manager = MigrationManager::new(default_migration_settings()); Ok(manager) } -/// Migrations on extras database. +/// Migrations on the extras database. fn extras_database_migrations() -> Result { let mut manager = MigrationManager::new(default_migration_settings()); try!(manager.add_migration(migrations::extras::ToV6).map_err(|_| Error::MigrationImpossible)); Ok(manager) } +/// Migrations on the state database. +fn state_database_migrations(pruning: Algorithm) -> Result { + let mut manager = MigrationManager::new(default_migration_settings()); + let res = match pruning { + Algorithm::Archive => manager.add_migration(migrations::state::ArchiveV7), + Algorithm::OverlayRecent => manager.add_migration(migrations::state::OverlayRecentV7::default()), + _ => die!("Unsupported pruning method for migration. Delete DB and resync"), + }; + + try!(res.map_err(|_| Error::MigrationImpossible)); + Ok(manager) +} + /// Migrates database at given position with given migration rules. -fn migrate_database(version: u32, db_path: PathBuf, migrations: MigrationManager) -> Result<(), Error> { +fn migrate_database(version: u32, db_path: PathBuf, mut migrations: MigrationManager) -> Result<(), Error> { // check if migration is needed if !migrations.is_needed(version) { return Ok(()) @@ -175,12 +196,12 @@ fn migrate_database(version: u32, db_path: PathBuf, migrations: MigrationManager Ok(()) } -fn exists(path: &PathBuf) -> bool { +fn exists(path: &Path) -> bool { fs::metadata(path).is_ok() } /// Migrates the database. -pub fn migrate(path: &PathBuf) -> Result<(), Error> { +pub fn migrate(path: &Path, pruning: Algorithm) -> Result<(), Error> { // read version file. let version = try!(current_version(path)); @@ -190,6 +211,7 @@ pub fn migrate(path: &PathBuf) -> Result<(), Error> { println!("Migrating database from version {} to {}", version, CURRENT_VERSION); try!(migrate_database(version, blocks_database_path(path), try!(blocks_database_migrations()))); try!(migrate_database(version, extras_database_path(path), try!(extras_database_migrations()))); + try!(migrate_database(version, state_database_path(path), try!(state_database_migrations(pruning)))); println!("Migration finished"); } diff --git a/parity/rpc_apis.rs b/parity/rpc_apis.rs index 0187f4058..4b971c167 100644 --- a/parity/rpc_apis.rs +++ b/parity/rpc_apis.rs @@ -18,7 +18,6 @@ use std::collections::BTreeMap; use std::str::FromStr; use std::sync::Arc; -use die::*; use ethsync::EthSync; use ethcore::miner::{Miner, ExternalMiner}; use ethcore::client::Client; diff --git a/util/src/migration/mod.rs b/util/src/migration/mod.rs index fddb51fca..048441d7d 100644 --- a/util/src/migration/mod.rs +++ b/util/src/migration/mod.rs @@ -25,6 +25,7 @@ use std::path::{Path, PathBuf}; use ::kvdb::{CompactionProfile, Database, DatabaseConfig, DBTransaction}; /// Migration config. +#[derive(Clone)] pub struct Config { /// Defines how many elements should be migrated at once. pub batch_size: usize, @@ -38,6 +39,45 @@ impl Default for Config { } } +/// A batch of key-value pairs to be written into the database. +pub struct Batch { + inner: BTreeMap, Vec>, + batch_size: usize, +} + +impl Batch { + /// Make a new batch with the given config. + pub fn new(config: &Config) -> Self { + Batch { + inner: BTreeMap::new(), + batch_size: config.batch_size, + } + } + + /// Insert a value into the batch, committing if necessary. + pub fn insert(&mut self, key: Vec, value: Vec, dest: &mut Database) -> Result<(), Error> { + self.inner.insert(key, value); + if self.inner.len() == self.batch_size { + try!(self.commit(dest)); + } + Ok(()) + } + + /// Commit all the items in the batch to the given database. + pub fn commit(&mut self, dest: &mut Database) -> Result<(), Error> { + if self.inner.is_empty() { return Ok(()) } + + let transaction = DBTransaction::new(); + + for keypair in &self.inner { + try!(transaction.put(&keypair.0, &keypair.1).map_err(Error::Custom)); + } + + self.inner.clear(); + dest.write(transaction).map_err(Error::Custom) + } +} + /// Migration error. #[derive(Debug)] pub enum Error { @@ -62,7 +102,7 @@ pub trait Migration: 'static { /// Version of the database after the migration. fn version(&self) -> u32; /// Migrate a source to a destination. - fn migrate(&self, source: &Database, config: &Config, destination: &mut Database) -> Result<(), Error>; + fn migrate(&mut self, source: &Database, config: &Config, destination: &mut Database) -> Result<(), Error>; } /// A simple migration over key-value pairs. @@ -71,46 +111,25 @@ pub trait SimpleMigration: 'static { fn version(&self) -> u32; /// Should migrate existing object to new database. /// Returns `None` if the object does not exist in new version of database. - fn simple_migrate(&self, key: Vec, value: Vec) -> Option<(Vec, Vec)>; + fn simple_migrate(&mut self, key: Vec, value: Vec) -> Option<(Vec, Vec)>; } impl Migration for T { fn version(&self) -> u32 { SimpleMigration::version(self) } - fn migrate(&self, source: &Database, config: &Config, dest: &mut Database) -> Result<(), Error> { - let mut batch: BTreeMap, Vec> = BTreeMap::new(); + fn migrate(&mut self, source: &Database, config: &Config, dest: &mut Database) -> Result<(), Error> { + let mut batch = Batch::new(config); for (key, value) in source.iter() { - if let Some((key, value)) = self.simple_migrate(key.to_vec(), value.to_vec()) { - batch.insert(key, value); - } - - if batch.len() == config.batch_size { - try!(commit_batch(dest, &batch)); - batch.clear(); + try!(batch.insert(key, value, dest)); } } - if !batch.is_empty() { - try!(commit_batch(dest, &batch)); - } - - Ok(()) + batch.commit(dest) } } -/// Commit a batch of writes to a database. -pub fn commit_batch(db: &mut Database, batch: &BTreeMap, Vec>) -> Result<(), Error> { - let transaction = DBTransaction::new(); - - for keypair in batch { - try!(transaction.put(&keypair.0, &keypair.1).map_err(Error::Custom)); - } - - db.write(transaction).map_err(Error::Custom) -} - /// Get the path where all databases reside. fn database_path(path: &Path) -> PathBuf { let mut temp_path = path.to_owned(); @@ -174,7 +193,8 @@ impl Manager { /// Performs migration in order, starting with a source path, migrating between two temporary databases, /// and producing a path where the final migration lives. - pub fn execute(&self, old_path: &Path, version: u32) -> Result { + pub fn execute(&mut self, old_path: &Path, version: u32) -> Result { + let config = self.config.clone(); let migrations = try!(self.migrations_from(version).ok_or(Error::MigrationImpossible)); let db_config = DatabaseConfig { prefix_size: None, @@ -197,7 +217,7 @@ impl Manager { let mut new_db = try!(Database::open(&db_config, temp_path_str).map_err(Error::Custom)); // perform the migration from cur_db to new_db. - try!(migration.migrate(&cur_db, &self.config, &mut new_db)); + try!(migration.migrate(&cur_db, &config, &mut new_db)); // next iteration, we will migrate from this db into the other temp. cur_db = new_db; temp_idx.swap(); @@ -216,10 +236,10 @@ impl Manager { } } - fn migrations_from(&self, version: u32) -> Option<&[Box]> { + fn migrations_from(&mut self, version: u32) -> Option<&mut [Box]> { // index of the first required migration let position = self.migrations.iter().position(|m| m.version() == version + 1); - position.map(|p| &self.migrations[p..]) + position.map(move |p| &mut self.migrations[p..]) } } diff --git a/util/src/migration/tests.rs b/util/src/migration/tests.rs index f441b31e4..58d2c9008 100644 --- a/util/src/migration/tests.rs +++ b/util/src/migration/tests.rs @@ -62,7 +62,7 @@ impl SimpleMigration for Migration0 { 1 } - fn simple_migrate(&self, key: Vec, value: Vec) -> Option<(Vec, Vec)> { + fn simple_migrate(&mut self, key: Vec, value: Vec) -> Option<(Vec, Vec)> { let mut key = key; key.push(0x11); let mut value = value; @@ -78,7 +78,7 @@ impl SimpleMigration for Migration1 { 2 } - fn simple_migrate(&self, key: Vec, _value: Vec) -> Option<(Vec, Vec)> { + fn simple_migrate(&mut self, key: Vec, _value: Vec) -> Option<(Vec, Vec)> { Some((key, vec![])) } }