fixed grumbles

This commit is contained in:
Svyatoslav Nikolsky 2016-11-17 14:14:16 +03:00
parent 294d7ca1b7
commit 26ab437024
5 changed files with 45 additions and 37 deletions

View File

@ -36,7 +36,7 @@ impl OrphanBlocksPool {
}
/// Get unknown blocks in the insertion order
pub fn unknown_blocks<'a>(&'a self) -> &'a LinkedHashMap<H256, f64> {
pub fn unknown_blocks(&self) -> &LinkedHashMap<H256, f64> {
&self.unknown_blocks
}
@ -50,8 +50,10 @@ impl OrphanBlocksPool {
/// Insert unknown block, for which we know nothing about its parent block
pub fn insert_unknown_block(&mut self, hash: H256, block: Block) {
self.insert_orphaned_block(hash.clone(), block);
self.unknown_blocks.insert(hash, time::precise_time_s());
let previous_value = self.unknown_blocks.insert(hash.clone(), time::precise_time_s());
assert_eq!(previous_value, None);
self.insert_orphaned_block(hash, block);
}
/// Remove all blocks, which are not-unknown
@ -74,7 +76,7 @@ impl OrphanBlocksPool {
if let Entry::Occupied(entry) = self.orphaned_blocks.entry(parent_hash) {
let (_, orphaned) = entry.remove_entry();
for orphaned_hash in orphaned.keys() {
self.unknown_blocks.remove(&orphaned_hash);
self.unknown_blocks.remove(orphaned_hash);
}
queue.extend(orphaned.keys().cloned());
removed.extend(orphaned.into_iter());
@ -88,14 +90,16 @@ impl OrphanBlocksPool {
// TODO: excess clone
let mut removed: Vec<(H256, Block)> = Vec::new();
let parent_orphan_keys: Vec<_> = self.orphaned_blocks.keys().cloned().collect();
for parent_orphan_key in parent_orphan_keys.into_iter() {
if let Entry::Occupied(mut orphan_entry) = self.orphaned_blocks.entry(parent_orphan_key.clone()) {
for parent_orphan_key in parent_orphan_keys {
if let Entry::Occupied(mut orphan_entry) = self.orphaned_blocks.entry(parent_orphan_key) {
if {
let mut orphans = orphan_entry.get_mut();
let orphans_keys: HashSet<H256> = orphans.keys().cloned().collect();
for orphan_to_remove in orphans_keys.intersection(&hashes) {
self.unknown_blocks.remove(orphan_to_remove);
removed.push((orphan_to_remove.clone(), orphans.remove(orphan_to_remove).unwrap()));
removed.push((orphan_to_remove.clone(),
orphans.remove(orphan_to_remove).expect("iterating by intersection of orphans keys with hashes; removing from orphans; qed")
));
}
orphans.is_empty()
} {

View File

@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::collections::{HashMap, HashSet, VecDeque};
use std::collections::hash_map::Entry;
use linked_hash_map::LinkedHashMap;
use time;
@ -42,7 +42,7 @@ impl OrphanTransactionsPool {
}
/// Get unknown transactions in the insertion order
pub fn transactions<'a>(&'a self) -> &'a LinkedHashMap<H256, OrphanTransaction> {
pub fn transactions(&self) -> &LinkedHashMap<H256, OrphanTransaction> {
&self.by_hash
}
@ -51,7 +51,7 @@ impl OrphanTransactionsPool {
assert!(!self.by_hash.contains_key(&hash));
assert!(unknown_parents.iter().all(|h| transaction.inputs.iter().any(|i| &i.previous_output.hash == h)));
for unknown_parent in unknown_parents.iter() {
for unknown_parent in &unknown_parents {
self.by_parent.entry(unknown_parent.clone())
.or_insert_with(HashSet::new)
.insert(hash.clone());
@ -63,26 +63,31 @@ impl OrphanTransactionsPool {
pub fn remove_transactions_for_parent(&mut self, hash: &H256) -> Vec<(H256, Transaction)> {
assert!(!self.by_hash.contains_key(hash));
// remove direct children of hash
let mut removed_orphans_hashes: Vec<H256> = Vec::new();
let mut removal_queue: VecDeque<H256> = VecDeque::new();
removal_queue.push_back(hash.clone());
let mut removed_orphans: Vec<(H256, Transaction)> = Vec::new();
if let Entry::Occupied(children_entry) = self.by_parent.entry(hash.clone()) {
for child in children_entry.get() {
if {
let child_entry = self.by_hash.get_mut(child).expect("every entry in by_parent.values() has corresponding entry in by_hash.keys()");
child_entry.remove_known_parent(hash)
} {
removed_orphans_hashes.push(child.clone());
removed_orphans.push((child.clone(), self.by_hash.remove(child).expect("checked couple of lines above").transaction));
};
while let Some(hash) = removal_queue.pop_front() {
// remove direct children of hash
let mut removed_orphans_hashes: Vec<H256> = Vec::new();
if let Entry::Occupied(children_entry) = self.by_parent.entry(hash.clone()) {
for child in children_entry.get() {
let all_parents_are_known = {
let child_entry = self.by_hash.get_mut(child).expect("every entry in by_parent.values() has corresponding entry in by_hash.keys()");
child_entry.remove_known_parent(&hash)
};
if all_parents_are_known {
removed_orphans_hashes.push(child.clone());
removed_orphans.push((child.clone(), self.by_hash.remove(child).expect("checked couple of lines above").transaction));
}
}
children_entry.remove_entry();
}
children_entry.remove_entry();
}
// then also remove grandchildren of hash & so on
for child_hash in removed_orphans_hashes {
removed_orphans.extend(self.remove_transactions_for_parent(&child_hash));
// then also remove grandchildren of hash & so on
removal_queue.extend(removed_orphans_hashes);
}
removed_orphans

View File

@ -378,7 +378,7 @@ impl Chain {
/// Forget in-memory blocks
pub fn forget_blocks(&mut self, hashes: &[H256]) {
for hash in hashes.iter() {
for hash in hashes {
self.forget_block(hash);
}
}
@ -396,7 +396,7 @@ impl Chain {
/// Forget in-memory blocks, but leave their headers in the headers_chain (orphan queue)
pub fn forget_blocks_leave_header(&mut self, hashes: &[H256]) {
for hash in hashes.iter() {
for hash in hashes {
self.forget_block_leave_header(hash);
}
}
@ -517,14 +517,15 @@ impl Chain {
if self.storage.contains_transaction(hash) {
return TransactionState::Stored;
}
return TransactionState::Unknown;
TransactionState::Unknown
}
/// Get transactions hashes with given state
pub fn transactions_hashes_with_state(&self, state: TransactionState) -> Vec<H256> {
match state {
TransactionState::InMemory => self.memory_pool.get_transactions_ids(),
_ => unimplemented!(),
TransactionState::Verifying => self.verifying_transactions.keys().cloned().collect(),
_ => panic!("wrong argument"),
}
}
@ -550,7 +551,7 @@ impl Chain {
for h in all_keys {
if {
if let Some(entry) = self.verifying_transactions.get(&h) {
if entry.inputs.iter().any(|i| &i.previous_output.hash == &hash) {
if entry.inputs.iter().any(|i| i.previous_output.hash == hash) {
queue.push_back(h.clone());
true
} else {

View File

@ -103,7 +103,6 @@ impl TaskExecutor for LocalSynchronizationTaskExecutor {
let mempool = types::MemPool;
if let Some(connection) = self.peers.get_mut(&peer_index) {
let connection = &mut *connection;
trace!(target: "sync", "Querying memory pool contents from peer#{}", peer_index);
connection.send_mempool(&mempool);
}
@ -118,7 +117,6 @@ impl TaskExecutor for LocalSynchronizationTaskExecutor {
};
if let Some(connection) = self.peers.get_mut(&peer_index) {
let connection = &mut *connection;
trace!(target: "sync", "Querying {} unknown transactions from peer#{}", getdata.inventory.len(), peer_index);
connection.send_getdata(&getdata);
}

View File

@ -119,7 +119,7 @@ pub fn manage_unknown_orphaned_blocks(config: &ManageUnknownBlocksConfig, orphan
let mut unknown_to_remove: HashSet<H256> = HashSet::new();
let mut remove_num = if unknown_blocks.len() > config.max_number { unknown_blocks.len() - config.max_number } else { 0 };
let now = precise_time_s();
for (hash, time) in unknown_blocks.iter() {
for (hash, time) in unknown_blocks {
// remove oldest blocks if there are more unknown blocks that we can hold in memory
if remove_num > 0 {
unknown_to_remove.insert(hash.clone());
@ -143,7 +143,7 @@ pub fn manage_unknown_orphaned_blocks(config: &ManageUnknownBlocksConfig, orphan
.map(|t| t.0)
.collect();
if unknown_to_remove.is_empty() { None } else { Some(unknown_to_remove.into_iter().collect()) }
if unknown_to_remove.is_empty() { None } else { Some(unknown_to_remove) }
}
/// Manage orphaned transactions
@ -153,7 +153,7 @@ pub fn manage_orphaned_transactions(config: &ManageOrphanTransactionsConfig, orp
let mut orphans_to_remove: Vec<H256> = Vec::new();
let mut remove_num = if unknown_transactions.len() > config.max_number { unknown_transactions.len() - config.max_number } else { 0 };
let now = precise_time_s();
for (hash, orphan_tx) in unknown_transactions.iter() {
for (hash, orphan_tx) in unknown_transactions {
// remove oldest blocks if there are more unknown blocks that we can hold in memory
if remove_num > 0 {
orphans_to_remove.push(hash.clone());