Fix deadlock in blockchain. (#8977)

This commit is contained in:
Tomasz Drwięga 2018-06-26 20:49:06 +02:00 committed by 5chdn
parent f27912a07e
commit d921e2c9b5
No known key found for this signature in database
GPG Key ID: 1A40871B597F5F80

View File

@ -1425,18 +1425,24 @@ impl BlockChain {
/// Returns general blockchain information /// Returns general blockchain information
pub fn chain_info(&self) -> BlockChainInfo { pub fn chain_info(&self) -> BlockChainInfo {
// Make sure to call internal methods first to avoid
// recursive locking of `best_block`.
let first_block_hash = self.first_block();
let first_block_number = self.first_block_number().into();
let genesis_hash = self.genesis_hash();
// ensure data consistencly by locking everything first // ensure data consistencly by locking everything first
let best_block = self.best_block.read(); let best_block = self.best_block.read();
let best_ancient_block = self.best_ancient_block.read(); let best_ancient_block = self.best_ancient_block.read();
BlockChainInfo { BlockChainInfo {
total_difficulty: best_block.total_difficulty.clone(), total_difficulty: best_block.total_difficulty.clone(),
pending_total_difficulty: best_block.total_difficulty.clone(), pending_total_difficulty: best_block.total_difficulty.clone(),
genesis_hash: self.genesis_hash(), genesis_hash,
best_block_hash: best_block.hash, best_block_hash: best_block.hash,
best_block_number: best_block.number, best_block_number: best_block.number,
best_block_timestamp: best_block.timestamp, best_block_timestamp: best_block.timestamp,
first_block_hash: self.first_block(), first_block_hash,
first_block_number: From::from(self.first_block_number()), first_block_number,
ancient_block_hash: best_ancient_block.as_ref().map(|b| b.hash), ancient_block_hash: best_ancient_block.as_ref().map(|b| b.hash),
ancient_block_number: best_ancient_block.as_ref().map(|b| b.number), ancient_block_number: best_ancient_block.as_ref().map(|b| b.number),
} }