      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244

    use std::collections::{HashMap, HashSet, VecDeque};
    use std::collections::hash_map::Entry;
    use linked_hash_map::LinkedHashMap;
    use time;
    use primitives::hash::H256;
    use chain::IndexedBlock;

    #[derive(Debug)]
    /// Storage for blocks, for which we have no parent yet.
    /// Blocks from this storage are either moved to verification queue, or removed at all.
    pub struct OrphanBlocksPool {
    	/// Blocks from requested_hashes, but received out-of-order.
    	orphaned_blocks: HashMap<H256, HashMap<H256, IndexedBlock>>,
    	/// Blocks that we have received without requesting with receiving time.
    	unknown_blocks: LinkedHashMap<H256, f64>,
    }

    impl OrphanBlocksPool {
    	/// Create new pool
    	pub fn new() -> Self {
    		OrphanBlocksPool {
    			orphaned_blocks: HashMap::new(),
    			unknown_blocks: LinkedHashMap::new(),
    		}
    	}

    	/// Get total number of blocks in pool
    	pub fn len(&self) -> usize {
    		self.orphaned_blocks.len()
    	}

    	/// Check if block with given hash is stored as unknown in this pool
    	pub fn contains_unknown_block(&self, hash: &H256) -> bool {
    		self.unknown_blocks.contains_key(hash)
    	}

    	/// Get unknown blocks in the insertion order
    	pub fn unknown_blocks(&self) -> &LinkedHashMap<H256, f64> {
    		&self.unknown_blocks
    	}

    	/// Insert orphaned block, for which we have already requested its parent block
    	pub fn insert_orphaned_block(&mut self, block: IndexedBlock) {
    		self.orphaned_blocks
    			.entry(block.header.raw.previous_header_hash.clone())
    			.or_insert_with(HashMap::new)
    			.insert(block.header.hash.clone(), block);
    	}

    	/// Insert unknown block, for which we know nothing about its parent block
    	pub fn insert_unknown_block(&mut self, block: IndexedBlock) {
    		let previous_value = self.unknown_blocks.insert(block.header.hash.clone(), time::precise_time_s());
    		assert_eq!(previous_value, None);

    		self.insert_orphaned_block(block);
    	}

    	/// Remove all blocks, which are not-unknown
    	pub fn remove_known_blocks(&mut self) -> Vec<H256> {
    		let orphans_to_remove: HashSet<_> = self.orphaned_blocks.values()
    			.flat_map(|v| v.iter().map(|e| e.0.clone()))
    			.filter(|h| !self.unknown_blocks.contains_key(h))
    			.collect();
    		self.remove_blocks(&orphans_to_remove);
    		orphans_to_remove.into_iter().collect()
    	}

    	/// Remove all blocks, depending on this parent
    	pub fn remove_blocks_for_parent(&mut self, hash: &H256) -> VecDeque<IndexedBlock> {
    		let mut queue: VecDeque<H256> = VecDeque::new();
    		queue.push_back(hash.clone());

    		let mut removed: VecDeque<IndexedBlock> = VecDeque::new();
    		while let Some(parent_hash) = queue.pop_front() {
    			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);
    				}
    				queue.extend(orphaned.keys().cloned());
    				removed.extend(orphaned.into_iter().map(|(_, b)| b));
    			}
    		}
    		removed
    	}

    	/// Remove blocks with given hashes + all dependent blocks
    	pub fn remove_blocks(&mut self, hashes: &HashSet<H256>) -> Vec<IndexedBlock> {
    		// TODO: excess clone
    		let mut removed: Vec<IndexedBlock> = Vec::new();
    		let parent_orphan_keys: Vec<_> = self.orphaned_blocks.keys().cloned().collect();
    		for parent_orphan_key in parent_orphan_keys {
    			if let Entry::Occupied(mut orphan_entry) = self.orphaned_blocks.entry(parent_orphan_key) {
    				let remove_entry = {
    					let 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(
    							orphans.remove(orphan_to_remove)
    								.expect("iterating by intersection of orphans keys with hashes; removing from orphans; qed")
    						);
    					}
    					orphans.is_empty()
    				};

    				if remove_entry {
    					orphan_entry.remove_entry();
    				}
    			}
    		}

    		// also delete all children
    		for hash in hashes.iter() {
    			removed.extend(self.remove_blocks_for_parent(hash));
    		}

    		removed
    	}
    }

    #[cfg(test)]
    mod tests {
    	extern crate test_data;

    	use std::collections::HashSet;
    	use primitives::hash::H256;
    	use super::OrphanBlocksPool;

    	#[test]
    	fn orphan_block_pool_empty_on_start() {
    		let pool = OrphanBlocksPool::new();
    		assert_eq!(pool.len(), 0);
    	}

    	#[test]
    	fn orphan_block_pool_insert_orphan_block() {
    		let mut pool = OrphanBlocksPool::new();
    		let b1 = test_data::block_h1();
    		let b1_hash = b1.hash();

    		pool.insert_orphaned_block(b1.into());

    		assert_eq!(pool.len(), 1);
    		assert!(!pool.contains_unknown_block(&b1_hash));
    		assert_eq!(pool.unknown_blocks().len(), 0);
    	}

    	#[test]
    	fn orphan_block_pool_insert_unknown_block() {
    		let mut pool = OrphanBlocksPool::new();
    		let b1 = test_data::block_h1();
    		let b1_hash = b1.hash();

    		pool.insert_unknown_block(b1.into());

    		assert_eq!(pool.len(), 1);
    		assert!(pool.contains_unknown_block(&b1_hash));
    		assert_eq!(pool.unknown_blocks().len(), 1);
    	}

    	#[test]
    	fn orphan_block_pool_remove_known_blocks() {
    		let mut pool = OrphanBlocksPool::new();
    		let b1 = test_data::block_h1();
    		let b1_hash = b1.hash();
    		let b2 = test_data::block_h169();
    		let b2_hash = b2.hash();

    		pool.insert_orphaned_block(b1.into());
    		pool.insert_unknown_block(b2.into());

    		assert_eq!(pool.len(), 2);
    		assert!(!pool.contains_unknown_block(&b1_hash));
    		assert!(pool.contains_unknown_block(&b2_hash));
    		assert_eq!(pool.unknown_blocks().len(), 1);

    		pool.remove_known_blocks();

    		assert_eq!(pool.len(), 1);
    		assert!(!pool.contains_unknown_block(&b1_hash));
    		assert!(pool.contains_unknown_block(&b2_hash));
    		assert_eq!(pool.unknown_blocks().len(), 1);
    	}

    	#[test]
    	fn orphan_block_pool_remove_blocks_for_parent() {
    		let mut pool = OrphanBlocksPool::new();
    		let b1 = test_data::block_h1();
    		let b1_hash = b1.hash();
    		let b2 = test_data::block_h169();
    		let b2_hash = b2.hash();
    		let b3 = test_data::block_h2();
    		let b3_hash = b3.hash();

    		pool.insert_orphaned_block(b1.into());
    		pool.insert_unknown_block(b2.into());
    		pool.insert_orphaned_block(b3.into());

    		let removed = pool.remove_blocks_for_parent(&test_data::genesis().hash());
    		assert_eq!(removed.len(), 2);
    		assert_eq!(removed[0].hash(), &b1_hash);
    		assert_eq!(removed[1].hash(), &b3_hash);

    		assert_eq!(pool.len(), 1);
    		assert!(!pool.contains_unknown_block(&b1_hash));
    		assert!(pool.contains_unknown_block(&b2_hash));
    		assert!(!pool.contains_unknown_block(&b1_hash));
    		assert_eq!(pool.unknown_blocks().len(), 1);
    	}

    	#[test]
    	fn orphan_block_pool_remove_blocks() {
    		let mut pool = OrphanBlocksPool::new();
    		let b1 = test_data::block_h1();
    		let b1_hash = b1.hash();
    		let b2 = test_data::block_h2();
    		let b2_hash = b2.hash();
    		let b3 = test_data::block_h169();
    		let b3_hash = b3.hash();
    		let b4 = test_data::block_h170();
    		let b4_hash = b4.hash();
    		let b5 = test_data::block_h181();

    		pool.insert_orphaned_block(b1.into());
    		pool.insert_orphaned_block(b2.into());
    		pool.insert_orphaned_block(b3.into());
    		pool.insert_orphaned_block(b4.into());
    		pool.insert_orphaned_block(b5.into());

    		let mut blocks_to_remove: HashSet<H256> = HashSet::new();
    		blocks_to_remove.insert(b1_hash.clone());
    		blocks_to_remove.insert(b3_hash.clone());

    		let removed = pool.remove_blocks(&blocks_to_remove);
    		assert_eq!(removed.len(), 4);
    		assert!(removed.iter().any(|ref b| b.hash() == &b1_hash));
    		assert!(removed.iter().any(|ref b| b.hash() == &b2_hash));
    		assert!(removed.iter().any(|ref b| b.hash() == &b3_hash));
    		assert!(removed.iter().any(|ref b| b.hash() == &b4_hash));

    		assert_eq!(pool.len(), 1);
    	}
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)