replace synchronization primitives with those from parking_lot (#1593)
* parking_lot in cargo.toml * replace all lock invocations with parking_lot ones * use parking_lot synchronization primitives
This commit is contained in:
committed by
Gav Wood
parent
4226c0f631
commit
36d3d0d7d7
@@ -17,13 +17,13 @@
|
||||
//! Account management.
|
||||
|
||||
use std::fmt;
|
||||
use std::sync::RwLock;
|
||||
use std::collections::HashMap;
|
||||
use util::{Address as H160, H256, H520, RwLockable};
|
||||
use util::{Address as H160, H256, H520, RwLock};
|
||||
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};
|
||||
|
||||
|
||||
/// Type of unlock.
|
||||
#[derive(Clone)]
|
||||
enum Unlock {
|
||||
@@ -177,7 +177,7 @@ impl AccountProvider {
|
||||
|
||||
// check if account is already unlocked pernamently, if it is, do nothing
|
||||
{
|
||||
let unlocked = self.unlocked.unwrapped_read();
|
||||
let unlocked = self.unlocked.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.unwrapped_write();
|
||||
let mut unlocked = self.unlocked.write();
|
||||
unlocked.insert(account, data);
|
||||
Ok(())
|
||||
}
|
||||
@@ -208,7 +208,7 @@ impl AccountProvider {
|
||||
/// Checks if given account is unlocked
|
||||
pub fn is_unlocked<A>(&self, account: A) -> bool where Address: From<A> {
|
||||
let account = Address::from(account).into();
|
||||
let unlocked = self.unlocked.unwrapped_read();
|
||||
let unlocked = self.unlocked.read();
|
||||
unlocked.get(&account).is_some()
|
||||
}
|
||||
|
||||
@@ -218,12 +218,12 @@ impl AccountProvider {
|
||||
let message = Message::from(message).into();
|
||||
|
||||
let data = {
|
||||
let unlocked = self.unlocked.unwrapped_read();
|
||||
let unlocked = self.unlocked.read();
|
||||
try!(unlocked.get(&account).ok_or(Error::NotUnlocked)).clone()
|
||||
};
|
||||
|
||||
if let Unlock::Temp = data.unlock {
|
||||
let mut unlocked = self.unlocked.unwrapped_write();
|
||||
let mut unlocked = self.unlocked.write();
|
||||
unlocked.remove(&account).expect("data exists: so key must exist: qed");
|
||||
}
|
||||
|
||||
|
||||
@@ -193,14 +193,14 @@ impl BlockQueue {
|
||||
fn verify(verification: Arc<Verification>, engine: Arc<Box<Engine>>, wait: Arc<Condvar>, ready: Arc<QueueSignal>, deleting: Arc<AtomicBool>, empty: Arc<Condvar>) {
|
||||
while !deleting.load(AtomicOrdering::Acquire) {
|
||||
{
|
||||
let mut unverified = verification.unverified.locked();
|
||||
let mut unverified = verification.unverified.lock();
|
||||
|
||||
if unverified.is_empty() && verification.verifying.locked().is_empty() {
|
||||
if unverified.is_empty() && verification.verifying.lock().is_empty() {
|
||||
empty.notify_all();
|
||||
}
|
||||
|
||||
while unverified.is_empty() && !deleting.load(AtomicOrdering::Acquire) {
|
||||
unverified = wait.wait(unverified).unwrap();
|
||||
wait.wait(&mut unverified);
|
||||
}
|
||||
|
||||
if deleting.load(AtomicOrdering::Acquire) {
|
||||
@@ -209,11 +209,11 @@ impl BlockQueue {
|
||||
}
|
||||
|
||||
let block = {
|
||||
let mut unverified = verification.unverified.locked();
|
||||
let mut unverified = verification.unverified.lock();
|
||||
if unverified.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mut verifying = verification.verifying.locked();
|
||||
let mut verifying = verification.verifying.lock();
|
||||
let block = unverified.pop_front().unwrap();
|
||||
verifying.push_back(VerifyingBlock{ hash: block.header.hash(), block: None });
|
||||
block
|
||||
@@ -222,7 +222,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.locked();
|
||||
let mut verifying = verification.verifying.lock();
|
||||
for e in verifying.iter_mut() {
|
||||
if e.hash == block_hash {
|
||||
e.block = Some(verified);
|
||||
@@ -231,16 +231,16 @@ impl BlockQueue {
|
||||
}
|
||||
if !verifying.is_empty() && verifying.front().unwrap().hash == block_hash {
|
||||
// we're next!
|
||||
let mut verified = verification.verified.locked();
|
||||
let mut bad = verification.bad.locked();
|
||||
let mut verified = verification.verified.lock();
|
||||
let mut bad = verification.bad.lock();
|
||||
BlockQueue::drain_verifying(&mut verifying, &mut verified, &mut bad);
|
||||
ready.set();
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
let mut verifying = verification.verifying.locked();
|
||||
let mut verified = verification.verified.locked();
|
||||
let mut bad = verification.bad.locked();
|
||||
let mut verifying = verification.verifying.lock();
|
||||
let mut verified = verification.verified.lock();
|
||||
let mut bad = verification.bad.lock();
|
||||
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);
|
||||
@@ -265,29 +265,29 @@ impl BlockQueue {
|
||||
|
||||
/// Clear the queue and stop verification activity.
|
||||
pub fn clear(&self) {
|
||||
let mut unverified = self.verification.unverified.locked();
|
||||
let mut verifying = self.verification.verifying.locked();
|
||||
let mut verified = self.verification.verified.locked();
|
||||
let mut unverified = self.verification.unverified.lock();
|
||||
let mut verifying = self.verification.verifying.lock();
|
||||
let mut verified = self.verification.verified.lock();
|
||||
unverified.clear();
|
||||
verifying.clear();
|
||||
verified.clear();
|
||||
self.processing.unwrapped_write().clear();
|
||||
self.processing.write().clear();
|
||||
}
|
||||
|
||||
/// Wait for unverified queue to be empty
|
||||
pub fn flush(&self) {
|
||||
let mut unverified = self.verification.unverified.locked();
|
||||
while !unverified.is_empty() || !self.verification.verifying.locked().is_empty() {
|
||||
unverified = self.empty.wait(unverified).unwrap();
|
||||
let mut unverified = self.verification.unverified.lock();
|
||||
while !unverified.is_empty() || !self.verification.verifying.lock().is_empty() {
|
||||
self.empty.wait(&mut unverified);
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the block is currently in the queue
|
||||
pub fn block_status(&self, hash: &H256) -> BlockStatus {
|
||||
if self.processing.unwrapped_read().contains(&hash) {
|
||||
if self.processing.read().contains(&hash) {
|
||||
return BlockStatus::Queued;
|
||||
}
|
||||
if self.verification.bad.locked().contains(&hash) {
|
||||
if self.verification.bad.lock().contains(&hash) {
|
||||
return BlockStatus::Bad;
|
||||
}
|
||||
BlockStatus::Unknown
|
||||
@@ -298,11 +298,11 @@ impl BlockQueue {
|
||||
let header = BlockView::new(&bytes).header();
|
||||
let h = header.hash();
|
||||
{
|
||||
if self.processing.unwrapped_read().contains(&h) {
|
||||
if self.processing.read().contains(&h) {
|
||||
return Err(ImportError::AlreadyQueued.into());
|
||||
}
|
||||
|
||||
let mut bad = self.verification.bad.locked();
|
||||
let mut bad = self.verification.bad.lock();
|
||||
if bad.contains(&h) {
|
||||
return Err(ImportError::KnownBad.into());
|
||||
}
|
||||
@@ -315,14 +315,14 @@ impl BlockQueue {
|
||||
|
||||
match verify_block_basic(&header, &bytes, self.engine.deref().deref()) {
|
||||
Ok(()) => {
|
||||
self.processing.unwrapped_write().insert(h.clone());
|
||||
self.verification.unverified.locked().push_back(UnverifiedBlock { header: header, bytes: bytes });
|
||||
self.processing.write().insert(h.clone());
|
||||
self.verification.unverified.lock().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.locked().insert(h.clone());
|
||||
self.verification.bad.lock().insert(h.clone());
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
@@ -333,10 +333,10 @@ impl BlockQueue {
|
||||
if block_hashes.is_empty() {
|
||||
return;
|
||||
}
|
||||
let mut verified_lock = self.verification.verified.locked();
|
||||
let mut verified_lock = self.verification.verified.lock();
|
||||
let mut verified = verified_lock.deref_mut();
|
||||
let mut bad = self.verification.bad.locked();
|
||||
let mut processing = self.processing.unwrapped_write();
|
||||
let mut bad = self.verification.bad.lock();
|
||||
let mut processing = self.processing.write();
|
||||
bad.reserve(block_hashes.len());
|
||||
for hash in block_hashes {
|
||||
bad.insert(hash.clone());
|
||||
@@ -360,7 +360,7 @@ impl BlockQueue {
|
||||
if block_hashes.is_empty() {
|
||||
return;
|
||||
}
|
||||
let mut processing = self.processing.unwrapped_write();
|
||||
let mut processing = self.processing.write();
|
||||
for hash in block_hashes {
|
||||
processing.remove(&hash);
|
||||
}
|
||||
@@ -368,7 +368,7 @@ impl BlockQueue {
|
||||
|
||||
/// Removes up to `max` verified blocks from the queue
|
||||
pub fn drain(&self, max: usize) -> Vec<PreverifiedBlock> {
|
||||
let mut verified = self.verification.verified.locked();
|
||||
let mut verified = self.verification.verified.lock();
|
||||
let count = min(max, verified.len());
|
||||
let mut result = Vec::with_capacity(count);
|
||||
for _ in 0..count {
|
||||
@@ -385,15 +385,15 @@ impl BlockQueue {
|
||||
/// Get queue status.
|
||||
pub fn queue_info(&self) -> BlockQueueInfo {
|
||||
let (unverified_len, unverified_bytes) = {
|
||||
let v = self.verification.unverified.locked();
|
||||
let v = self.verification.unverified.lock();
|
||||
(v.len(), v.heap_size_of_children())
|
||||
};
|
||||
let (verifying_len, verifying_bytes) = {
|
||||
let v = self.verification.verifying.locked();
|
||||
let v = self.verification.verifying.lock();
|
||||
(v.len(), v.heap_size_of_children())
|
||||
};
|
||||
let (verified_len, verified_bytes) = {
|
||||
let v = self.verification.verified.locked();
|
||||
let v = self.verification.verified.lock();
|
||||
(v.len(), v.heap_size_of_children())
|
||||
};
|
||||
BlockQueueInfo {
|
||||
@@ -407,18 +407,18 @@ impl BlockQueue {
|
||||
+ verifying_bytes
|
||||
+ verified_bytes
|
||||
// TODO: https://github.com/servo/heapsize/pull/50
|
||||
//+ self.processing.unwrapped_read().heap_size_of_children(),
|
||||
//+ self.processing.read().heap_size_of_children(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Optimise memory footprint of the heap fields.
|
||||
pub fn collect_garbage(&self) {
|
||||
{
|
||||
self.verification.unverified.locked().shrink_to_fit();
|
||||
self.verification.verifying.locked().shrink_to_fit();
|
||||
self.verification.verified.locked().shrink_to_fit();
|
||||
self.verification.unverified.lock().shrink_to_fit();
|
||||
self.verification.verifying.lock().shrink_to_fit();
|
||||
self.verification.verified.lock().shrink_to_fit();
|
||||
}
|
||||
self.processing.unwrapped_write().shrink_to_fit();
|
||||
self.processing.write().shrink_to_fit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -170,7 +170,7 @@ impl BlockProvider for BlockChain {
|
||||
/// Get raw block data
|
||||
fn block(&self, hash: &H256) -> Option<Bytes> {
|
||||
{
|
||||
let read = self.blocks.unwrapped_read();
|
||||
let read = self.blocks.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.unwrapped_write();
|
||||
let mut write = self.blocks.write();
|
||||
write.insert(hash.clone(), bytes.clone());
|
||||
Some(bytes)
|
||||
},
|
||||
@@ -338,7 +338,7 @@ impl BlockChain {
|
||||
};
|
||||
|
||||
{
|
||||
let mut best_block = bc.best_block.unwrapped_write();
|
||||
let mut best_block = bc.best_block.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.unwrapped_write();
|
||||
let mut write_details = self.block_details.write();
|
||||
batch.extend_with_cache(write_details.deref_mut(), update.block_details, CacheUpdatePolicy::Overwrite);
|
||||
}
|
||||
|
||||
{
|
||||
let mut write_receipts = self.block_receipts.unwrapped_write();
|
||||
let mut write_receipts = self.block_receipts.write();
|
||||
batch.extend_with_cache(write_receipts.deref_mut(), update.block_receipts, CacheUpdatePolicy::Remove);
|
||||
}
|
||||
|
||||
{
|
||||
let mut write_blocks_blooms = self.blocks_blooms.unwrapped_write();
|
||||
let mut write_blocks_blooms = self.blocks_blooms.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.unwrapped_write();
|
||||
let mut write_hashes = self.block_hashes.unwrapped_write();
|
||||
let mut write_txs = self.transaction_addresses.unwrapped_write();
|
||||
let mut best_block = self.best_block.write();
|
||||
let mut write_hashes = self.block_hashes.write();
|
||||
let mut write_txs = self.transaction_addresses.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.unwrapped_read().hash.clone()
|
||||
self.best_block.read().hash.clone()
|
||||
}
|
||||
|
||||
/// Get best block number.
|
||||
pub fn best_block_number(&self) -> BlockNumber {
|
||||
self.best_block.unwrapped_read().number
|
||||
self.best_block.read().number
|
||||
}
|
||||
|
||||
/// Get best block total difficulty.
|
||||
pub fn best_block_total_difficulty(&self) -> U256 {
|
||||
self.best_block.unwrapped_read().total_difficulty
|
||||
self.best_block.read().total_difficulty
|
||||
}
|
||||
|
||||
/// Get current cache size.
|
||||
pub fn cache_size(&self) -> CacheSize {
|
||||
CacheSize {
|
||||
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(),
|
||||
blocks: self.blocks.read().heap_size_of_children(),
|
||||
block_details: self.block_details.read().heap_size_of_children(),
|
||||
transaction_addresses: self.transaction_addresses.read().heap_size_of_children(),
|
||||
blocks_blooms: self.blocks_blooms.read().heap_size_of_children(),
|
||||
block_receipts: self.block_receipts.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.unwrapped_write();
|
||||
let mut cache_man = self.cache_man.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.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();
|
||||
let mut blocks = self.blocks.write();
|
||||
let mut block_details = self.block_details.write();
|
||||
let mut block_hashes = self.block_hashes.write();
|
||||
let mut transaction_addresses = self.transaction_addresses.write();
|
||||
let mut blocks_blooms = self.blocks_blooms.write();
|
||||
let mut block_receipts = self.block_receipts.write();
|
||||
let mut cache_man = self.cache_man.write();
|
||||
|
||||
for id in cache_man.cache_usage.pop_back().unwrap().into_iter() {
|
||||
cache_man.in_use.remove(&id);
|
||||
|
||||
@@ -18,7 +18,7 @@ use std::collections::{HashSet, HashMap};
|
||||
use std::ops::Deref;
|
||||
use std::mem;
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::*;
|
||||
use std::sync::{Arc, Weak};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::fmt;
|
||||
use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering as AtomicOrdering};
|
||||
@@ -30,12 +30,13 @@ use util::panics::*;
|
||||
use util::io::*;
|
||||
use util::rlp;
|
||||
use util::sha3::*;
|
||||
use util::{Bytes, Lockable, RwLockable};
|
||||
use util::Bytes;
|
||||
use util::rlp::{RlpStream, Rlp, UntrustedRlp};
|
||||
use util::journaldb;
|
||||
use util::journaldb::JournalDB;
|
||||
use util::kvdb::*;
|
||||
use util::{Applyable, Stream, View, PerfTimer, Itertools, Colour};
|
||||
use util::{Mutex, RwLock};
|
||||
|
||||
// other
|
||||
use views::BlockView;
|
||||
@@ -236,12 +237,12 @@ impl Client {
|
||||
|
||||
/// Sets the actor to be notified on certain events
|
||||
pub fn set_notify(&self, target: &Arc<ChainNotify>) {
|
||||
let mut write_lock = self.notify.unwrapped_write();
|
||||
let mut write_lock = self.notify.write();
|
||||
*write_lock = Some(Arc::downgrade(target));
|
||||
}
|
||||
|
||||
fn notify(&self) -> Option<Arc<ChainNotify>> {
|
||||
let read_lock = self.notify.unwrapped_read();
|
||||
let read_lock = self.notify.read();
|
||||
read_lock.as_ref().and_then(|weak| weak.upgrade())
|
||||
}
|
||||
|
||||
@@ -293,7 +294,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.locked().boxed_clone();
|
||||
let db = self.state_db.lock().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 {
|
||||
@@ -369,7 +370,7 @@ impl Client {
|
||||
let route = self.commit_block(closed_block, &header.hash(), &block.bytes);
|
||||
import_results.push(route);
|
||||
|
||||
self.report.unwrapped_write().accrue_block(&block);
|
||||
self.report.write().accrue_block(&block);
|
||||
trace!(target: "client", "Imported #{} ({})", header.number(), header.hash());
|
||||
}
|
||||
|
||||
@@ -471,7 +472,7 @@ impl Client {
|
||||
};
|
||||
|
||||
self.block_header(id).and_then(|header| {
|
||||
let db = self.state_db.locked().boxed_clone();
|
||||
let db = self.state_db.lock().boxed_clone();
|
||||
|
||||
// early exit for pruned blocks
|
||||
if db.is_pruned() && self.chain.best_block_number() >= block_number + HISTORY {
|
||||
@@ -487,7 +488,7 @@ impl Client {
|
||||
/// Get a copy of the best block's state.
|
||||
pub fn state(&self) -> State {
|
||||
State::from_existing(
|
||||
self.state_db.locked().boxed_clone(),
|
||||
self.state_db.lock().boxed_clone(),
|
||||
HeaderView::new(&self.best_block_header()).state_root(),
|
||||
self.engine.account_start_nonce(),
|
||||
self.trie_factory.clone())
|
||||
@@ -501,8 +502,8 @@ impl Client {
|
||||
|
||||
/// Get the report.
|
||||
pub fn report(&self) -> ClientReport {
|
||||
let mut report = self.report.unwrapped_read().clone();
|
||||
report.state_db_mem = self.state_db.locked().mem_used();
|
||||
let mut report = self.report.read().clone();
|
||||
report.state_db_mem = self.state_db.lock().mem_used();
|
||||
report
|
||||
}
|
||||
|
||||
@@ -514,7 +515,7 @@ impl Client {
|
||||
|
||||
match self.mode {
|
||||
Mode::Dark(timeout) => {
|
||||
let mut ss = self.sleep_state.locked();
|
||||
let mut ss = self.sleep_state.lock();
|
||||
if let Some(t) = ss.last_activity {
|
||||
if Instant::now() > t + timeout {
|
||||
self.sleep();
|
||||
@@ -523,7 +524,7 @@ impl Client {
|
||||
}
|
||||
}
|
||||
Mode::Passive(timeout, wakeup_after) => {
|
||||
let mut ss = self.sleep_state.locked();
|
||||
let mut ss = self.sleep_state.lock();
|
||||
let now = Instant::now();
|
||||
if let Some(t) = ss.last_activity {
|
||||
if now > t + timeout {
|
||||
@@ -600,14 +601,14 @@ impl Client {
|
||||
} else {
|
||||
trace!(target: "mode", "sleep: Cannot sleep - syncing ongoing.");
|
||||
// TODO: Consider uncommenting.
|
||||
//*self.last_activity.locked() = Some(Instant::now());
|
||||
//*self.last_activity.lock() = Some(Instant::now());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Notify us that the network has been started.
|
||||
pub fn network_started(&self, url: &str) {
|
||||
let mut previous_enode = self.previous_enode.locked();
|
||||
let mut previous_enode = self.previous_enode.lock();
|
||||
if let Some(ref u) = *previous_enode {
|
||||
if u == url {
|
||||
return;
|
||||
@@ -661,7 +662,7 @@ impl BlockChainClient for Client {
|
||||
fn keep_alive(&self) {
|
||||
if self.mode != Mode::Active {
|
||||
self.wake_up();
|
||||
(*self.sleep_state.locked()).last_activity = Some(Instant::now());
|
||||
(*self.sleep_state.lock()).last_activity = Some(Instant::now());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -785,7 +786,7 @@ impl BlockChainClient for Client {
|
||||
}
|
||||
|
||||
fn state_data(&self, hash: &H256) -> Option<Bytes> {
|
||||
self.state_db.locked().state(hash)
|
||||
self.state_db.lock().state(hash)
|
||||
}
|
||||
|
||||
fn block_receipts(&self, hash: &H256) -> Option<Bytes> {
|
||||
@@ -946,7 +947,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.locked().boxed_clone(),
|
||||
self.state_db.lock().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,
|
||||
|
||||
@@ -115,38 +115,38 @@ impl TestBlockChainClient {
|
||||
vm_factory: EvmFactory::new(VMType::Interpreter),
|
||||
};
|
||||
client.add_blocks(1, EachBlockWith::Nothing); // add genesis block
|
||||
client.genesis_hash = client.last_hash.unwrapped_read().clone();
|
||||
client.genesis_hash = client.last_hash.read().clone();
|
||||
client
|
||||
}
|
||||
|
||||
/// Set the transaction receipt result
|
||||
pub fn set_transaction_receipt(&self, id: TransactionID, receipt: LocalizedReceipt) {
|
||||
self.receipts.unwrapped_write().insert(id, receipt);
|
||||
self.receipts.write().insert(id, receipt);
|
||||
}
|
||||
|
||||
/// Set the execution result.
|
||||
pub fn set_execution_result(&self, result: Executed) {
|
||||
*self.execution_result.unwrapped_write() = Some(result);
|
||||
*self.execution_result.write() = Some(result);
|
||||
}
|
||||
|
||||
/// Set the balance of account `address` to `balance`.
|
||||
pub fn set_balance(&self, address: Address, balance: U256) {
|
||||
self.balances.unwrapped_write().insert(address, balance);
|
||||
self.balances.write().insert(address, balance);
|
||||
}
|
||||
|
||||
/// Set nonce of account `address` to `nonce`.
|
||||
pub fn set_nonce(&self, address: Address, nonce: U256) {
|
||||
self.nonces.unwrapped_write().insert(address, nonce);
|
||||
self.nonces.write().insert(address, nonce);
|
||||
}
|
||||
|
||||
/// Set `code` at `address`.
|
||||
pub fn set_code(&self, address: Address, code: Bytes) {
|
||||
self.code.unwrapped_write().insert(address, code);
|
||||
self.code.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.unwrapped_write().insert((address, position), value);
|
||||
self.storage.write().insert((address, position), value);
|
||||
}
|
||||
|
||||
/// Set block queue size for testing
|
||||
@@ -156,11 +156,11 @@ impl TestBlockChainClient {
|
||||
|
||||
/// Add blocks to test client.
|
||||
pub fn add_blocks(&self, count: usize, with: EachBlockWith) {
|
||||
let len = self.numbers.unwrapped_read().len();
|
||||
let len = self.numbers.read().len();
|
||||
for n in len..(len + count) {
|
||||
let mut header = BlockHeader::new();
|
||||
header.difficulty = From::from(n);
|
||||
header.parent_hash = self.last_hash.unwrapped_read().clone();
|
||||
header.parent_hash = self.last_hash.read().clone();
|
||||
header.number = n as BlockNumber;
|
||||
header.gas_limit = U256::from(1_000_000);
|
||||
let uncles = match with {
|
||||
@@ -168,7 +168,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.unwrapped_read().clone();
|
||||
uncle_header.parent_hash = self.last_hash.read().clone();
|
||||
uncle_header.number = n as BlockNumber;
|
||||
uncles.append(&uncle_header);
|
||||
header.uncles_hash = uncles.as_raw().sha3();
|
||||
@@ -181,7 +181,7 @@ impl TestBlockChainClient {
|
||||
let mut txs = RlpStream::new_list(1);
|
||||
let keypair = KeyPair::create().unwrap();
|
||||
// Update nonces value
|
||||
self.nonces.unwrapped_write().insert(keypair.address(), U256::one());
|
||||
self.nonces.write().insert(keypair.address(), U256::one());
|
||||
let tx = Transaction {
|
||||
action: Action::Create,
|
||||
value: U256::from(100),
|
||||
@@ -214,7 +214,7 @@ impl TestBlockChainClient {
|
||||
rlp.append(&header);
|
||||
rlp.append_raw(&rlp::NULL_RLP, 1);
|
||||
rlp.append_raw(&rlp::NULL_RLP, 1);
|
||||
self.blocks.unwrapped_write().insert(hash, rlp.out());
|
||||
self.blocks.write().insert(hash, rlp.out());
|
||||
}
|
||||
|
||||
/// Make a bad block by setting invalid parent hash.
|
||||
@@ -226,12 +226,12 @@ impl TestBlockChainClient {
|
||||
rlp.append(&header);
|
||||
rlp.append_raw(&rlp::NULL_RLP, 1);
|
||||
rlp.append_raw(&rlp::NULL_RLP, 1);
|
||||
self.blocks.unwrapped_write().insert(hash, rlp.out());
|
||||
self.blocks.write().insert(hash, rlp.out());
|
||||
}
|
||||
|
||||
/// TODO:
|
||||
pub fn block_hash_delta_minus(&mut self, delta: usize) -> H256 {
|
||||
let blocks_read = self.numbers.unwrapped_read();
|
||||
let blocks_read = self.numbers.read();
|
||||
let index = blocks_read.len() - delta;
|
||||
blocks_read[&index].clone()
|
||||
}
|
||||
@@ -239,9 +239,9 @@ impl TestBlockChainClient {
|
||||
fn block_hash(&self, id: BlockID) -> Option<H256> {
|
||||
match id {
|
||||
BlockID::Hash(hash) => Some(hash),
|
||||
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()
|
||||
BlockID::Number(n) => self.numbers.read().get(&(n as usize)).cloned(),
|
||||
BlockID::Earliest => self.numbers.read().get(&0).cloned(),
|
||||
BlockID::Latest => self.numbers.read().get(&(self.numbers.read().len() - 1)).cloned()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -288,7 +288,7 @@ impl MiningBlockChainClient for TestBlockChainClient {
|
||||
|
||||
impl BlockChainClient for TestBlockChainClient {
|
||||
fn call(&self, _t: &SignedTransaction, _analytics: CallAnalytics) -> Result<Executed, ExecutionError> {
|
||||
Ok(self.execution_result.unwrapped_read().clone().unwrap())
|
||||
Ok(self.execution_result.read().clone().unwrap())
|
||||
}
|
||||
|
||||
fn block_total_difficulty(&self, _id: BlockID) -> Option<U256> {
|
||||
@@ -301,7 +301,7 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
|
||||
fn nonce(&self, address: &Address, id: BlockID) -> Option<U256> {
|
||||
match id {
|
||||
BlockID::Latest => Some(self.nonces.unwrapped_read().get(address).cloned().unwrap_or_else(U256::zero)),
|
||||
BlockID::Latest => Some(self.nonces.read().get(address).cloned().unwrap_or_else(U256::zero)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -311,12 +311,12 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
}
|
||||
|
||||
fn code(&self, address: &Address) -> Option<Bytes> {
|
||||
self.code.unwrapped_read().get(address).cloned()
|
||||
self.code.read().get(address).cloned()
|
||||
}
|
||||
|
||||
fn balance(&self, address: &Address, id: BlockID) -> Option<U256> {
|
||||
if let BlockID::Latest = id {
|
||||
Some(self.balances.unwrapped_read().get(address).cloned().unwrap_or_else(U256::zero))
|
||||
Some(self.balances.read().get(address).cloned().unwrap_or_else(U256::zero))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -328,7 +328,7 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
|
||||
fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option<H256> {
|
||||
if let BlockID::Latest = id {
|
||||
Some(self.storage.unwrapped_read().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new))
|
||||
Some(self.storage.read().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -343,7 +343,7 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
}
|
||||
|
||||
fn transaction_receipt(&self, id: TransactionID) -> Option<LocalizedReceipt> {
|
||||
self.receipts.unwrapped_read().get(&id).cloned()
|
||||
self.receipts.read().get(&id).cloned()
|
||||
}
|
||||
|
||||
fn blocks_with_bloom(&self, _bloom: &H2048, _from_block: BlockID, _to_block: BlockID) -> Option<Vec<BlockNumber>> {
|
||||
@@ -359,11 +359,11 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
}
|
||||
|
||||
fn block_header(&self, id: BlockID) -> Option<Bytes> {
|
||||
self.block_hash(id).and_then(|hash| self.blocks.unwrapped_read().get(&hash).map(|r| Rlp::new(r).at(0).as_raw().to_vec()))
|
||||
self.block_hash(id).and_then(|hash| self.blocks.read().get(&hash).map(|r| Rlp::new(r).at(0).as_raw().to_vec()))
|
||||
}
|
||||
|
||||
fn block_body(&self, id: BlockID) -> Option<Bytes> {
|
||||
self.block_hash(id).and_then(|hash| self.blocks.unwrapped_read().get(&hash).map(|r| {
|
||||
self.block_hash(id).and_then(|hash| self.blocks.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);
|
||||
@@ -372,13 +372,13 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
}
|
||||
|
||||
fn block(&self, id: BlockID) -> Option<Bytes> {
|
||||
self.block_hash(id).and_then(|hash| self.blocks.unwrapped_read().get(&hash).cloned())
|
||||
self.block_hash(id).and_then(|hash| self.blocks.read().get(&hash).cloned())
|
||||
}
|
||||
|
||||
fn block_status(&self, id: BlockID) -> BlockStatus {
|
||||
match id {
|
||||
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,
|
||||
BlockID::Number(number) if (number as usize) < self.blocks.read().len() => BlockStatus::InChain,
|
||||
BlockID::Hash(ref hash) if self.blocks.read().get(hash).is_some() => BlockStatus::InChain,
|
||||
_ => BlockStatus::Unknown
|
||||
}
|
||||
}
|
||||
@@ -389,7 +389,7 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
ancestor: H256::new(),
|
||||
index: 0,
|
||||
blocks: {
|
||||
let numbers_read = self.numbers.unwrapped_read();
|
||||
let numbers_read = self.numbers.read();
|
||||
let mut adding = false;
|
||||
|
||||
let mut blocks = Vec::new();
|
||||
@@ -446,11 +446,11 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
let header = Rlp::new(&b).val_at::<BlockHeader>(0);
|
||||
let h = header.hash();
|
||||
let number: usize = header.number as usize;
|
||||
if number > self.blocks.unwrapped_read().len() {
|
||||
panic!("Unexpected block number. Expected {}, got {}", self.blocks.unwrapped_read().len(), number);
|
||||
if number > self.blocks.read().len() {
|
||||
panic!("Unexpected block number. Expected {}, got {}", self.blocks.read().len(), number);
|
||||
}
|
||||
if number > 0 {
|
||||
match self.blocks.unwrapped_read().get(&header.parent_hash) {
|
||||
match self.blocks.read().get(&header.parent_hash) {
|
||||
Some(parent) => {
|
||||
let parent = Rlp::new(parent).val_at::<BlockHeader>(0);
|
||||
if parent.number != (header.number - 1) {
|
||||
@@ -462,27 +462,27 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
}
|
||||
}
|
||||
}
|
||||
let len = self.numbers.unwrapped_read().len();
|
||||
let len = self.numbers.read().len();
|
||||
if number == len {
|
||||
{
|
||||
let mut difficulty = self.difficulty.unwrapped_write();
|
||||
let mut difficulty = self.difficulty.write();
|
||||
*difficulty.deref_mut() = *difficulty.deref() + header.difficulty;
|
||||
}
|
||||
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());
|
||||
mem::replace(self.last_hash.write().deref_mut(), h.clone());
|
||||
self.blocks.write().insert(h.clone(), b);
|
||||
self.numbers.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.unwrapped_read()[&n] != parent_hash {
|
||||
*self.numbers.unwrapped_write().get_mut(&n).unwrap() = parent_hash.clone();
|
||||
while n > 0 && self.numbers.read()[&n] != parent_hash {
|
||||
*self.numbers.write().get_mut(&n).unwrap() = parent_hash.clone();
|
||||
n -= 1;
|
||||
parent_hash = Rlp::new(&self.blocks.unwrapped_read()[&parent_hash]).val_at::<BlockHeader>(0).parent_hash;
|
||||
parent_hash = Rlp::new(&self.blocks.read()[&parent_hash]).val_at::<BlockHeader>(0).parent_hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
self.blocks.unwrapped_write().insert(h.clone(), b.to_vec());
|
||||
self.blocks.write().insert(h.clone(), b.to_vec());
|
||||
}
|
||||
Ok(h)
|
||||
}
|
||||
@@ -503,11 +503,11 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
|
||||
fn chain_info(&self) -> BlockChainInfo {
|
||||
BlockChainInfo {
|
||||
total_difficulty: *self.difficulty.unwrapped_read(),
|
||||
pending_total_difficulty: *self.difficulty.unwrapped_read(),
|
||||
total_difficulty: *self.difficulty.read(),
|
||||
pending_total_difficulty: *self.difficulty.read(),
|
||||
genesis_hash: self.genesis_hash.clone(),
|
||||
best_block_hash: self.last_hash.unwrapped_read().clone(),
|
||||
best_block_number: self.blocks.unwrapped_read().len() as BlockNumber - 1,
|
||||
best_block_hash: self.last_hash.read().clone(),
|
||||
best_block_number: self.blocks.read().len() as BlockNumber - 1,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,11 +18,11 @@
|
||||
|
||||
use std::ops::Deref;
|
||||
use std::hash::Hash;
|
||||
use std::sync::RwLock;
|
||||
use std::collections::HashMap;
|
||||
use util::{DBTransaction, Database, RwLockable};
|
||||
use util::{DBTransaction, Database, RwLock};
|
||||
use util::rlp::{encode, Encodable, decode, Decodable};
|
||||
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum CacheUpdatePolicy {
|
||||
Overwrite,
|
||||
@@ -115,14 +115,14 @@ pub trait Readable {
|
||||
T: Clone + Decodable,
|
||||
C: Cache<K, T> {
|
||||
{
|
||||
let read = cache.unwrapped_read();
|
||||
let read = cache.read();
|
||||
if let Some(v) = read.get(key) {
|
||||
return Some(v.clone());
|
||||
}
|
||||
}
|
||||
|
||||
self.read(key).map(|value: T|{
|
||||
let mut write = cache.unwrapped_write();
|
||||
let mut write = cache.write();
|
||||
write.insert(key.clone(), value.clone());
|
||||
value
|
||||
})
|
||||
@@ -137,7 +137,7 @@ pub trait Readable {
|
||||
R: Deref<Target = [u8]>,
|
||||
C: Cache<K, T> {
|
||||
{
|
||||
let read = cache.unwrapped_read();
|
||||
let read = cache.read();
|
||||
if read.get(key).is_some() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -97,7 +97,6 @@ extern crate ethash;
|
||||
pub extern crate ethstore;
|
||||
extern crate semver;
|
||||
extern crate ethcore_ipc_nano as nanoipc;
|
||||
|
||||
extern crate ethcore_devtools as devtools;
|
||||
|
||||
#[cfg(feature = "jit" )] extern crate evmjit;
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use util::{RwLockable, U256, H256};
|
||||
use std::sync::Arc;
|
||||
use util::{RwLock, U256, H256};
|
||||
|
||||
/// External miner interface.
|
||||
pub trait ExternalMinerService: Send + Sync {
|
||||
@@ -54,15 +54,15 @@ impl ExternalMiner {
|
||||
|
||||
impl ExternalMinerService for ExternalMiner {
|
||||
fn submit_hashrate(&self, hashrate: U256, id: H256) {
|
||||
self.hashrates.unwrapped_write().insert(id, hashrate);
|
||||
self.hashrates.write().insert(id, hashrate);
|
||||
}
|
||||
|
||||
fn hashrate(&self) -> U256 {
|
||||
self.hashrates.unwrapped_read().iter().fold(U256::from(0), |sum, (_, v)| sum + *v)
|
||||
self.hashrates.read().iter().fold(U256::from(0), |sum, (_, v)| sum + *v)
|
||||
}
|
||||
|
||||
fn is_mining(&self) -> bool {
|
||||
!self.hashrates.unwrapped_read().is_empty()
|
||||
!self.hashrates.read().is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use rayon::prelude::*;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::{self, AtomicBool};
|
||||
use std::time::{Instant, Duration};
|
||||
|
||||
use util::*;
|
||||
@@ -26,7 +26,7 @@ use client::{MiningBlockChainClient, Executive, Executed, EnvInfo, TransactOptio
|
||||
use block::{ClosedBlock, IsBlock};
|
||||
use error::*;
|
||||
use transaction::SignedTransaction;
|
||||
use receipt::{Receipt};
|
||||
use receipt::Receipt;
|
||||
use spec::Spec;
|
||||
use engine::Engine;
|
||||
use miner::{MinerService, MinerStatus, TransactionQueue, AccountDetails, TransactionOrigin};
|
||||
@@ -34,7 +34,6 @@ use miner::work_notify::WorkPoster;
|
||||
use client::TransactionImportResult;
|
||||
use miner::price_info::PriceInfo;
|
||||
|
||||
|
||||
/// Different possible definitions for pending transaction set.
|
||||
#[derive(Debug)]
|
||||
pub enum PendingSet {
|
||||
@@ -236,16 +235,16 @@ impl Miner {
|
||||
{
|
||||
trace!(target: "miner", "recalibrating...");
|
||||
let txq = self.transaction_queue.clone();
|
||||
self.gas_pricer.lock().unwrap().recalibrate(move |price| {
|
||||
self.gas_pricer.lock().recalibrate(move |price| {
|
||||
trace!(target: "miner", "Got gas price! {}", price);
|
||||
txq.lock().unwrap().set_minimal_gas_price(price);
|
||||
txq.lock().set_minimal_gas_price(price);
|
||||
});
|
||||
trace!(target: "miner", "done recalibration.");
|
||||
}
|
||||
|
||||
let (transactions, mut open_block, original_work_hash) = {
|
||||
let transactions = {self.transaction_queue.locked().top_transactions()};
|
||||
let mut sealing_work = self.sealing_work.locked();
|
||||
let transactions = {self.transaction_queue.lock().top_transactions()};
|
||||
let mut sealing_work = self.sealing_work.lock();
|
||||
let last_work_hash = sealing_work.peek_last_ref().map(|pb| pb.block().fields().header.hash());
|
||||
let best_hash = chain.best_block_header().sha3();
|
||||
/*
|
||||
@@ -315,7 +314,7 @@ impl Miner {
|
||||
};
|
||||
|
||||
{
|
||||
let mut queue = self.transaction_queue.locked();
|
||||
let mut queue = self.transaction_queue.lock();
|
||||
for hash in invalid_transactions.into_iter() {
|
||||
queue.remove_invalid(&hash, &fetch_account);
|
||||
}
|
||||
@@ -346,7 +345,7 @@ impl Miner {
|
||||
}
|
||||
|
||||
let (work, is_new) = {
|
||||
let mut sealing_work = self.sealing_work.locked();
|
||||
let mut sealing_work = self.sealing_work.lock();
|
||||
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()) {
|
||||
@@ -374,14 +373,14 @@ 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.locked();
|
||||
let mut queue = self.transaction_queue.lock();
|
||||
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.locked().peek_last_ref().is_some();
|
||||
let have_work = self.sealing_work.lock().peek_last_ref().is_some();
|
||||
trace!(target: "miner", "enable_and_prepare_sealing: have_work={}", have_work);
|
||||
if !have_work {
|
||||
// --------------------------------------------------------------------------
|
||||
@@ -391,7 +390,7 @@ impl Miner {
|
||||
self.sealing_enabled.store(true, atomic::Ordering::Relaxed);
|
||||
self.prepare_sealing(chain);
|
||||
}
|
||||
let mut sealing_block_last_request = self.sealing_block_last_request.locked();
|
||||
let mut sealing_block_last_request = self.sealing_block_last_request.lock();
|
||||
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);
|
||||
@@ -416,7 +415,7 @@ impl Miner {
|
||||
}
|
||||
|
||||
/// Are we allowed to do a non-mandatory reseal?
|
||||
fn tx_reseal_allowed(&self) -> bool { Instant::now() > *self.next_allowed_reseal.locked() }
|
||||
fn tx_reseal_allowed(&self) -> bool { Instant::now() > *self.next_allowed_reseal.lock() }
|
||||
}
|
||||
|
||||
const SEALING_TIMEOUT_IN_BLOCKS : u64 = 5;
|
||||
@@ -424,7 +423,7 @@ const SEALING_TIMEOUT_IN_BLOCKS : u64 = 5;
|
||||
impl MinerService for Miner {
|
||||
|
||||
fn clear_and_reset(&self, chain: &MiningBlockChainClient) {
|
||||
self.transaction_queue.locked().clear();
|
||||
self.transaction_queue.lock().clear();
|
||||
// --------------------------------------------------------------------------
|
||||
// | NOTE Code below requires transaction_queue and sealing_work locks. |
|
||||
// | Make sure to release the locks before calling that method. |
|
||||
@@ -433,8 +432,8 @@ impl MinerService for Miner {
|
||||
}
|
||||
|
||||
fn status(&self) -> MinerStatus {
|
||||
let status = self.transaction_queue.locked().status();
|
||||
let sealing_work = self.sealing_work.locked();
|
||||
let status = self.transaction_queue.lock().status();
|
||||
let sealing_work = self.sealing_work.lock();
|
||||
MinerStatus {
|
||||
transactions_in_pending_queue: status.pending,
|
||||
transactions_in_future_queue: status.future,
|
||||
@@ -443,7 +442,7 @@ impl MinerService for Miner {
|
||||
}
|
||||
|
||||
fn call(&self, chain: &MiningBlockChainClient, t: &SignedTransaction, analytics: CallAnalytics) -> Result<Executed, ExecutionError> {
|
||||
let sealing_work = self.sealing_work.locked();
|
||||
let sealing_work = self.sealing_work.lock();
|
||||
match sealing_work.peek_last_ref() {
|
||||
Some(work) => {
|
||||
let block = work.block();
|
||||
@@ -490,7 +489,7 @@ impl MinerService for Miner {
|
||||
}
|
||||
|
||||
fn balance(&self, chain: &MiningBlockChainClient, address: &Address) -> U256 {
|
||||
let sealing_work = self.sealing_work.locked();
|
||||
let sealing_work = self.sealing_work.lock();
|
||||
sealing_work.peek_last_ref().map_or_else(
|
||||
|| chain.latest_balance(address),
|
||||
|b| b.block().fields().state.balance(address)
|
||||
@@ -498,7 +497,7 @@ impl MinerService for Miner {
|
||||
}
|
||||
|
||||
fn storage_at(&self, chain: &MiningBlockChainClient, address: &Address, position: &H256) -> H256 {
|
||||
let sealing_work = self.sealing_work.locked();
|
||||
let sealing_work = self.sealing_work.lock();
|
||||
sealing_work.peek_last_ref().map_or_else(
|
||||
|| chain.latest_storage_at(address, position),
|
||||
|b| b.block().fields().state.storage_at(address, position)
|
||||
@@ -506,79 +505,79 @@ impl MinerService for Miner {
|
||||
}
|
||||
|
||||
fn nonce(&self, chain: &MiningBlockChainClient, address: &Address) -> U256 {
|
||||
let sealing_work = self.sealing_work.locked();
|
||||
let sealing_work = self.sealing_work.lock();
|
||||
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<Bytes> {
|
||||
let sealing_work = self.sealing_work.locked();
|
||||
let sealing_work = self.sealing_work.lock();
|
||||
sealing_work.peek_last_ref().map_or_else(|| chain.code(address), |b| b.block().fields().state.code(address))
|
||||
}
|
||||
|
||||
fn set_author(&self, author: Address) {
|
||||
*self.author.unwrapped_write() = author;
|
||||
*self.author.write() = author;
|
||||
}
|
||||
|
||||
fn set_extra_data(&self, extra_data: Bytes) {
|
||||
*self.extra_data.unwrapped_write() = extra_data;
|
||||
*self.extra_data.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.unwrapped_write().0 = target;
|
||||
self.gas_range_target.write().0 = target;
|
||||
}
|
||||
|
||||
fn set_gas_ceil_target(&self, target: U256) {
|
||||
self.gas_range_target.unwrapped_write().1 = target;
|
||||
self.gas_range_target.write().1 = target;
|
||||
}
|
||||
|
||||
fn set_minimal_gas_price(&self, min_gas_price: U256) {
|
||||
self.transaction_queue.locked().set_minimal_gas_price(min_gas_price);
|
||||
self.transaction_queue.lock().set_minimal_gas_price(min_gas_price);
|
||||
}
|
||||
|
||||
fn minimal_gas_price(&self) -> U256 {
|
||||
*self.transaction_queue.locked().minimal_gas_price()
|
||||
*self.transaction_queue.lock().minimal_gas_price()
|
||||
}
|
||||
|
||||
fn sensible_gas_price(&self) -> U256 {
|
||||
// 10% above our minimum.
|
||||
*self.transaction_queue.locked().minimal_gas_price() * 110.into() / 100.into()
|
||||
*self.transaction_queue.lock().minimal_gas_price() * 110.into() / 100.into()
|
||||
}
|
||||
|
||||
fn sensible_gas_limit(&self) -> U256 {
|
||||
self.gas_range_target.unwrapped_read().0 / 5.into()
|
||||
self.gas_range_target.read().0 / 5.into()
|
||||
}
|
||||
|
||||
fn transactions_limit(&self) -> usize {
|
||||
self.transaction_queue.locked().limit()
|
||||
self.transaction_queue.lock().limit()
|
||||
}
|
||||
|
||||
fn set_transactions_limit(&self, limit: usize) {
|
||||
self.transaction_queue.locked().set_limit(limit)
|
||||
self.transaction_queue.lock().set_limit(limit)
|
||||
}
|
||||
|
||||
fn set_tx_gas_limit(&self, limit: U256) {
|
||||
self.transaction_queue.locked().set_tx_gas_limit(limit)
|
||||
self.transaction_queue.lock().set_tx_gas_limit(limit)
|
||||
}
|
||||
|
||||
/// Get the author that we will seal blocks as.
|
||||
fn author(&self) -> Address {
|
||||
*self.author.unwrapped_read()
|
||||
*self.author.read()
|
||||
}
|
||||
|
||||
/// Get the extra_data that we will seal blocks with.
|
||||
fn extra_data(&self) -> Bytes {
|
||||
self.extra_data.unwrapped_read().clone()
|
||||
self.extra_data.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.unwrapped_read().0
|
||||
self.gas_range_target.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.unwrapped_read().1
|
||||
self.gas_range_target.read().1
|
||||
}
|
||||
|
||||
fn import_external_transactions(
|
||||
@@ -588,7 +587,7 @@ impl MinerService for Miner {
|
||||
) -> Vec<Result<TransactionImportResult, Error>> {
|
||||
|
||||
let results = {
|
||||
let mut transaction_queue = self.transaction_queue.locked();
|
||||
let mut transaction_queue = self.transaction_queue.lock();
|
||||
self.add_transactions_to_queue(
|
||||
chain, transactions, TransactionOrigin::External, &mut transaction_queue
|
||||
)
|
||||
@@ -615,7 +614,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.locked();
|
||||
let mut transaction_queue = self.transaction_queue.lock();
|
||||
let import = self.add_transactions_to_queue(chain, vec![transaction], TransactionOrigin::Local, &mut transaction_queue).pop().unwrap();
|
||||
|
||||
match import {
|
||||
@@ -651,13 +650,13 @@ impl MinerService for Miner {
|
||||
}
|
||||
|
||||
fn all_transactions(&self) -> Vec<SignedTransaction> {
|
||||
let queue = self.transaction_queue.locked();
|
||||
let queue = self.transaction_queue.lock();
|
||||
queue.top_transactions()
|
||||
}
|
||||
|
||||
fn pending_transactions(&self) -> Vec<SignedTransaction> {
|
||||
let queue = self.transaction_queue.locked();
|
||||
let sw = self.sealing_work.locked();
|
||||
let queue = self.transaction_queue.lock();
|
||||
let sw = self.sealing_work.lock();
|
||||
// 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(),
|
||||
@@ -670,8 +669,8 @@ impl MinerService for Miner {
|
||||
}
|
||||
|
||||
fn pending_transactions_hashes(&self) -> Vec<H256> {
|
||||
let queue = self.transaction_queue.locked();
|
||||
let sw = self.sealing_work.locked();
|
||||
let queue = self.transaction_queue.lock();
|
||||
let sw = self.sealing_work.lock();
|
||||
let sealing_set = match self.sealing_enabled.load(atomic::Ordering::Relaxed) {
|
||||
true => sw.peek_last_ref(),
|
||||
false => None,
|
||||
@@ -683,8 +682,8 @@ impl MinerService for Miner {
|
||||
}
|
||||
|
||||
fn transaction(&self, hash: &H256) -> Option<SignedTransaction> {
|
||||
let queue = self.transaction_queue.locked();
|
||||
let sw = self.sealing_work.locked();
|
||||
let queue = self.transaction_queue.lock();
|
||||
let sw = self.sealing_work.lock();
|
||||
let sealing_set = match self.sealing_enabled.load(atomic::Ordering::Relaxed) {
|
||||
true => sw.peek_last_ref(),
|
||||
false => None,
|
||||
@@ -696,7 +695,7 @@ impl MinerService for Miner {
|
||||
}
|
||||
|
||||
fn pending_receipts(&self) -> BTreeMap<H256, Receipt> {
|
||||
match (self.sealing_enabled.load(atomic::Ordering::Relaxed), self.sealing_work.locked().peek_last_ref()) {
|
||||
match (self.sealing_enabled.load(atomic::Ordering::Relaxed), self.sealing_work.lock().peek_last_ref()) {
|
||||
(true, Some(pending)) => {
|
||||
let hashes = pending.transactions()
|
||||
.iter()
|
||||
@@ -711,14 +710,14 @@ impl MinerService for Miner {
|
||||
}
|
||||
|
||||
fn last_nonce(&self, address: &Address) -> Option<U256> {
|
||||
self.transaction_queue.locked().last_nonce(address)
|
||||
self.transaction_queue.lock().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.locked().has_local_pending_transactions();
|
||||
let last_request = *self.sealing_block_last_request.locked();
|
||||
let has_local_transactions = self.transaction_queue.lock().has_local_pending_transactions();
|
||||
let last_request = *self.sealing_block_last_request.lock();
|
||||
let should_disable_sealing = !self.forced_sealing()
|
||||
&& !has_local_transactions
|
||||
&& current_no > last_request
|
||||
@@ -727,9 +726,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.locked().reset();
|
||||
self.sealing_work.lock().reset();
|
||||
} else {
|
||||
*self.next_allowed_reseal.locked() = Instant::now() + self.options.reseal_min_period;
|
||||
*self.next_allowed_reseal.lock() = 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. |
|
||||
@@ -743,14 +742,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.locked();
|
||||
let mut sealing_work = self.sealing_work.lock();
|
||||
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<Bytes>) -> Result<(), Error> {
|
||||
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) {
|
||||
let result = if let Some(b) = self.sealing_work.lock().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)
|
||||
@@ -797,7 +796,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.locked();
|
||||
let mut transaction_queue = self.transaction_queue.lock();
|
||||
let _ = self.add_transactions_to_queue(
|
||||
chain, txs, TransactionOrigin::External, &mut transaction_queue
|
||||
);
|
||||
@@ -811,7 +810,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.locked();
|
||||
let mut transaction_queue = self.transaction_queue.lock();
|
||||
|
||||
let to_remove = txs.drain(..)
|
||||
.map(|tx| {
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
//! assert_eq!(miner.status().transactions_in_pending_queue, 0);
|
||||
//!
|
||||
//! // Check block for sealing
|
||||
//! //assert!(miner.sealing_block(client.deref()).locked().is_some());
|
||||
//! //assert!(miner.sealing_block(client.deref()).lock().is_some());
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
|
||||
@@ -80,14 +80,17 @@ impl PriceInfo {
|
||||
//#[ignore]
|
||||
#[test]
|
||||
fn should_get_price_info() {
|
||||
use std::sync::{Condvar, Mutex, Arc};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use util::log::init_log;
|
||||
use util::{Condvar, Mutex};
|
||||
|
||||
init_log();
|
||||
let done = Arc::new((Mutex::new(PriceInfo { ethusd: 0f32 }), Condvar::new()));
|
||||
let rdone = done.clone();
|
||||
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);
|
||||
PriceInfo::get(move |price| { let mut p = rdone.0.lock(); *p = price; rdone.1.notify_one(); }).unwrap();
|
||||
let mut p = done.0.lock();
|
||||
let t = done.1.wait_for(&mut p, Duration::from_millis(10000));
|
||||
assert!(!t.timed_out());
|
||||
assert!(p.ethusd != 0f32);
|
||||
}
|
||||
@@ -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.locked().get_seedhash(number);
|
||||
let seed_hash = &self.seed_compute.lock().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.locked();
|
||||
let mut client = self.client.lock();
|
||||
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);
|
||||
|
||||
@@ -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.unwrapped_read().is_none() {
|
||||
*self.state_root_memo.unwrapped_write() = Some(self.genesis_state.root());
|
||||
if self.state_root_memo.read().is_none() {
|
||||
*self.state_root_memo.write() = Some(self.genesis_state.root());
|
||||
}
|
||||
self.state_root_memo.unwrapped_read().as_ref().unwrap().clone()
|
||||
self.state_root_memo.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.unwrapped_write() = None;
|
||||
*self.state_root_memo.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.unwrapped_read().clone().map_or(true, |sr| sr == self.genesis_state.root())
|
||||
self.state_root_memo.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.
|
||||
|
||||
@@ -18,17 +18,18 @@
|
||||
use std::ptr;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{RwLock, Arc};
|
||||
use std::sync::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, RwLockable};
|
||||
use util::{H256, H264, Database, DatabaseConfig, DBTransaction, RwLock};
|
||||
use header::BlockNumber;
|
||||
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};
|
||||
|
||||
|
||||
const TRACE_DB_VER: &'static [u8] = b"1.0";
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
@@ -231,7 +232,7 @@ impl<T> TraceDatabase for TraceDB<T> where T: DatabaseExtras {
|
||||
|
||||
// at first, let's insert new block traces
|
||||
{
|
||||
let mut traces = self.traces.unwrapped_write();
|
||||
let mut traces = self.traces.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);
|
||||
@@ -259,7 +260,7 @@ impl<T> TraceDatabase for TraceDB<T> where T: DatabaseExtras {
|
||||
.map(|p| (From::from(p.0), From::from(p.1)))
|
||||
.collect::<HashMap<TraceGroupPosition, blooms::BloomGroup>>();
|
||||
|
||||
let mut blooms = self.blooms.unwrapped_write();
|
||||
let mut blooms = self.blooms.write();
|
||||
batch.extend_with_cache(blooms.deref_mut(), blooms_to_insert, CacheUpdatePolicy::Remove);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user