blockchain.rs 43.5 KiB
Newer Older
// 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 <http://www.gnu.org/licenses/>.

//! Blockchain database.
use header::*;
Marek Kotewicz's avatar
Marek Kotewicz committed
use extras::*;
use transaction::*;
Marek Kotewicz's avatar
Marek Kotewicz committed
use views::*;
use chainfilter::{ChainFilter, BloomIndex, FilterDataSource};
use blockchain::block_info::{BlockInfo, BlockLocation};
use blockchain::best_block::BestBlock;
use blockchain::bloom_indexer::BloomIndexer;
use blockchain::tree_route::TreeRoute;
use blockchain::update::ExtrasUpdate;

const BLOOM_INDEX_SIZE: usize = 16;
const BLOOM_LEVELS: u8 = 3;
Marek Kotewicz's avatar
Marek Kotewicz committed

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
/// Blockchain configuration.
#[derive(Debug)]
pub struct BlockChainConfig {
	/// Preferred cache size in bytes.
	pub pref_cache_size: usize,
	/// Maximum cache size in bytes.
	pub max_cache_size: usize,
}

impl Default for BlockChainConfig {
	fn default() -> Self {
		BlockChainConfig {
			pref_cache_size: 1 << 14,
			max_cache_size: 1 << 20,
		}
	}
}

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
/// Interface for querying blocks by hash and by number.
pub trait BlockProvider {
	/// Returns true if the given block is known
	/// (though not necessarily a part of the canon chain).
	fn is_known(&self, hash: &H256) -> bool;

	/// Get raw block data
	fn block(&self, hash: &H256) -> Option<Bytes>;

	/// Get the familial details concerning a block.
	fn block_details(&self, hash: &H256) -> Option<BlockDetails>;

	/// Get the hash of given block's number.
	fn block_hash(&self, index: BlockNumber) -> Option<H256>;

	/// Get the address of transaction with given hash.
	fn transaction_address(&self, hash: &H256) -> Option<TransactionAddress>;

	/// Get receipts of block with given hash.
	fn block_receipts(&self, hash: &H256) -> Option<BlockReceipts>;

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	/// Get the partial-header of a block.
	fn block_header(&self, hash: &H256) -> Option<Header> {
		self.block(hash).map(|bytes| BlockView::new(&bytes).header())
	}

	/// Get a list of uncles for a given block.
Gav Wood's avatar
Gav Wood committed
	/// Returns None if block does not exist.
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	fn uncles(&self, hash: &H256) -> Option<Vec<Header>> {
		self.block(hash).map(|bytes| BlockView::new(&bytes).uncles())
	}

	/// Get a list of uncle hashes for a given block.
	/// Returns None if block does not exist.
	fn uncle_hashes(&self, hash: &H256) -> Option<Vec<H256>> {
		self.block(hash).map(|bytes| BlockView::new(&bytes).uncle_hashes())
	}

	/// Get the number of given block's hash.
	fn block_number(&self, hash: &H256) -> Option<BlockNumber> {
		self.block(hash).map(|bytes| BlockView::new(&bytes).header_view().number())
	}

	/// Get transaction with given transaction hash.
	fn transaction(&self, address: &TransactionAddress) -> Option<LocalizedTransaction> {
		self.block(&address.block_hash).and_then(|bytes| BlockView::new(&bytes).localized_transaction_at(address.index))
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	/// Get a list of transactions for a given block.
	/// Returns None if block does not exist.
Marek Kotewicz's avatar
Marek Kotewicz committed
	fn transactions(&self, hash: &H256) -> Option<Vec<LocalizedTransaction>> {
		self.block(hash).map(|bytes| BlockView::new(&bytes).localized_transactions())
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	}

	/// Returns reference to genesis hash.
	fn genesis_hash(&self) -> H256 {
		self.block_hash(0).expect("Genesis hash should always exist")
	}

	/// Returns the header of the genesis block.
	fn genesis_header(&self) -> Header {
		self.block_header(&self.genesis_hash()).unwrap()
	}

	/// Returns numbers of blocks containing given bloom.
	fn blocks_with_bloom(&self, bloom: &H2048, from_block: BlockNumber, to_block: BlockNumber) -> Vec<BlockNumber>;
#[derive(Debug, Hash, Eq, PartialEq, Clone)]
Gav Wood's avatar
Gav Wood committed
enum CacheID {
	Block(H256),
	Extras(ExtrasIndex, H256),
}

struct CacheManager {
	cache_usage: VecDeque<HashSet<CacheID>>,
	in_use: HashSet<CacheID>,
}

/// Structure providing fast access to blockchain data.
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
///
Marek Kotewicz's avatar
Marek Kotewicz committed
/// **Does not do input data verification.**
Marek Kotewicz's avatar
Marek Kotewicz committed
pub struct BlockChain {
Gav Wood's avatar
Gav Wood committed
	pref_cache_size: usize,
	max_cache_size: usize,

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	best_block: RwLock<BestBlock>,
Marek Kotewicz's avatar
Marek Kotewicz committed

Marek Kotewicz's avatar
Marek Kotewicz committed
	// block cache
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	blocks: RwLock<HashMap<H256, Bytes>>,
Marek Kotewicz's avatar
Marek Kotewicz committed
	// extra caches
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	block_details: RwLock<HashMap<H256, BlockDetails>>,
	block_hashes: RwLock<HashMap<BlockNumber, H256>>,
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	transaction_addresses: RwLock<HashMap<H256, TransactionAddress>>,
	block_logs: RwLock<HashMap<H256, BlockLogBlooms>>,
	blocks_blooms: RwLock<HashMap<H256, BlocksBlooms>>,
	block_receipts: RwLock<HashMap<H256, BlockReceipts>>,
Marek Kotewicz's avatar
Marek Kotewicz committed

	extras_db: Database,
	blocks_db: Database,

	cache_man: RwLock<CacheManager>,
Marek Kotewicz's avatar
Marek Kotewicz committed
	bloom_indexer: BloomIndexer,
}

impl FilterDataSource for BlockChain {
	fn bloom_at_index(&self, bloom_index: &BloomIndex) -> Option<H2048> {
		let location = self.bloom_indexer.location(bloom_index);
		self.blocks_blooms(&location.hash).and_then(|blooms| blooms.blooms.into_iter().nth(location.index).cloned())
	}
Marek Kotewicz's avatar
Marek Kotewicz committed
}

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
impl BlockProvider for BlockChain {
	/// Returns true if the given block is known
	/// (though not necessarily a part of the canon chain).
	fn is_known(&self, hash: &H256) -> bool {
		self.query_extras_exist(hash, &self.block_details)
	}

	/// Get raw block data
	fn block(&self, hash: &H256) -> Option<Bytes> {
		{
			let read = self.blocks.read().unwrap();
			if let Some(v) = read.get(hash) {
				return Some(v.clone());
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
			}
		}

		let opt = self.blocks_db.get(hash)
			.expect("Low level database error. Some issue with disk?");

Gav Wood's avatar
Gav Wood committed
		self.note_used(CacheID::Block(hash.clone()));

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
		match opt {
			Some(b) => {
				let bytes: Bytes = b.to_vec();
				let mut write = self.blocks.write().unwrap();
				write.insert(hash.clone(), bytes.clone());
				Some(bytes)
			},
			None => None
		}
	}

	/// Get the familial details concerning a block.
	fn block_details(&self, hash: &H256) -> Option<BlockDetails> {
		self.query_extras(hash, &self.block_details)
	}

	/// Get the hash of given block's number.
	fn block_hash(&self, index: BlockNumber) -> Option<H256> {
		self.query_extras(&index, &self.block_hashes)
	}

	/// Get the address of transaction with given hash.
	fn transaction_address(&self, hash: &H256) -> Option<TransactionAddress> {
		self.query_extras(hash, &self.transaction_addresses)
	}
	/// Get receipts of block with given hash.
	fn block_receipts(&self, hash: &H256) -> Option<BlockReceipts> {
		self.query_extras(hash, &self.block_receipts)
	}

	/// Returns numbers of blocks containing given bloom.
	fn blocks_with_bloom(&self, bloom: &H2048, from_block: BlockNumber, to_block: BlockNumber) -> Vec<BlockNumber> {
		let filter = ChainFilter::new(self, self.bloom_indexer.index_size(), self.bloom_indexer.levels());
		filter.blocks_with_bloom(bloom, from_block as usize, to_block as usize).into_iter().map(|b| b as BlockNumber).collect()
	}
Gav Wood's avatar
Gav Wood committed
const COLLECTION_QUEUE_SIZE: usize = 8;
Gav Wood's avatar
Gav Wood committed
pub struct AncestryIter<'a> {
	current: H256,
	chain: &'a BlockChain,
}
Gav Wood's avatar
Gav Wood committed

Gav Wood's avatar
Gav Wood committed
impl<'a> Iterator for AncestryIter<'a> {
	type Item = H256;
	fn next(&mut self) -> Option<H256> {
		if self.current.is_zero() {
			Option::None
		} else {
Gav Wood's avatar
Gav Wood committed
			let mut n = self.chain.block_details(&self.current).unwrap().parent;
			mem::swap(&mut self.current, &mut n);
			Some(n)
Marek Kotewicz's avatar
Marek Kotewicz committed
impl BlockChain {
	/// Create new instance of blockchain from given Genesis
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	pub fn new(config: BlockChainConfig, genesis: &[u8], path: &Path) -> BlockChain {
		// open extras db
Marek Kotewicz's avatar
Marek Kotewicz committed
		let mut extras_path = path.to_path_buf();
		extras_path.push("extras");
		let extras_db = Database::open_default(extras_path.to_str().unwrap()).unwrap();
Marek Kotewicz's avatar
Marek Kotewicz committed

		// open blocks db
Marek Kotewicz's avatar
Marek Kotewicz committed
		let mut blocks_path = path.to_path_buf();
		blocks_path.push("blocks");
		let blocks_db = Database::open_default(blocks_path.to_str().unwrap()).unwrap();
Marek Kotewicz's avatar
Marek Kotewicz committed

Gav Wood's avatar
Gav Wood committed
		let mut cache_man = CacheManager{cache_usage: VecDeque::new(), in_use: HashSet::new()};
		(0..COLLECTION_QUEUE_SIZE).foreach(|_| cache_man.cache_usage.push_back(HashSet::new()));

Marek Kotewicz's avatar
Marek Kotewicz committed
		let bc = BlockChain {
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
			pref_cache_size: config.pref_cache_size,
			max_cache_size: config.max_cache_size,
			best_block: RwLock::new(BestBlock::default()),
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
			blocks: RwLock::new(HashMap::new()),
			block_details: RwLock::new(HashMap::new()),
			block_hashes: RwLock::new(HashMap::new()),
			transaction_addresses: RwLock::new(HashMap::new()),
			block_logs: RwLock::new(HashMap::new()),
			blocks_blooms: RwLock::new(HashMap::new()),
			block_receipts: RwLock::new(HashMap::new()),
Marek Kotewicz's avatar
Marek Kotewicz committed
			extras_db: extras_db,
			blocks_db: blocks_db,
Gav Wood's avatar
Gav Wood committed
			cache_man: RwLock::new(cache_man),
			bloom_indexer: BloomIndexer::new(BLOOM_INDEX_SIZE, BLOOM_LEVELS)
		// load best block
		let best_block_hash = match bc.extras_db.get(b"best").unwrap() {
			Some(best) => H256::from_slice(&best),
			None => {
				// best block does not exist
				// we need to insert genesis into the cache
				let block = BlockView::new(genesis);
				let header = block.header_view();
				let hash = block.sha3();

				let details = BlockDetails {
					number: header.number(),
					total_difficulty: header.difficulty(),
					parent: header.parent_hash(),
					children: vec![]
				};

				bc.blocks_db.put(&hash, genesis).unwrap();
				let batch = DBTransaction::new();
				batch.put_extras(&hash, &details);
				batch.put_extras(&header.number(), &hash);
				batch.put(b"best", &hash).unwrap();
				bc.extras_db.write(batch).unwrap();
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
			let mut best_block = bc.best_block.write().unwrap();
			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;
		}
Marek Kotewicz's avatar
Marek Kotewicz committed
		bc
Gav Wood's avatar
Gav Wood committed
	/// Set the cache configuration.
	pub fn configure_cache(&mut self, pref_cache_size: usize, max_cache_size: usize) {
		self.pref_cache_size = pref_cache_size;
		self.max_cache_size = max_cache_size;
	}

	/// Returns a tree route between `from` and `to`, which is a tuple of:
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	///
	/// - a vector of hashes of all blocks, ordered from `from` to `to`.
	/// - common ancestor of these blocks.
	/// - an index where best common ancestor would be
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	///
	/// 1.) from newer to older
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	///
	/// - bc: `A1 -> A2 -> A3 -> A4 -> A5`
	/// - from: A5, to: A4
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	/// - route:
	///
	///   ```json
	///   { blocks: [A5], ancestor: A4, index: 1 }
	///   ```
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	///
	/// 2.) from older to newer
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	///
	/// - bc: `A1 -> A2 -> A3 -> A4 -> A5`
	/// - from: A3, to: A4
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	/// - route:
	///
	///   ```json
	///   { blocks: [A4], ancestor: A3, index: 0 }
	///   ```
	///
	/// 3.) fork:
	///
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	/// - bc:
	///
	///   ```text
	///   A1 -> A2 -> A3 -> A4
	///              -> B3 -> B4
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	///   ```
	/// - from: B4, to: A4
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	/// - route:
	///
	///   ```json
	///   { blocks: [B4, B3, A3, A4], ancestor: A2, index: 2 }
	///   ```
	pub fn tree_route(&self, from: H256, to: H256) -> TreeRoute {
		let mut from_branch = vec![];
		let mut to_branch = vec![];

		let mut from_details = self.block_details(&from).expect(&format!("0. Expected to find details for block {:?}", from));
		let mut to_details = self.block_details(&to).expect(&format!("1. Expected to find details for block {:?}", to));
		let mut current_from = from;
		let mut current_to = to;

		// reset from && to to the same level
		while from_details.number > to_details.number {
			from_branch.push(current_from);
			current_from = from_details.parent.clone();
			from_details = self.block_details(&from_details.parent).expect(&format!("2. Expected to find details for block {:?}", from_details.parent));
		}

		while to_details.number > from_details.number {
			to_branch.push(current_to);
			current_to = to_details.parent.clone();
			to_details = self.block_details(&to_details.parent).expect(&format!("3. Expected to find details for block {:?}", to_details.parent));
		}

		assert_eq!(from_details.number, to_details.number);

		// move to shared parent
		while current_from != current_to {
			from_branch.push(current_from);
			current_from = from_details.parent.clone();
			from_details = self.block_details(&from_details.parent).expect(&format!("4. Expected to find details for block {:?}", from_details.parent));

			to_branch.push(current_to);
			current_to = to_details.parent.clone();
			to_details = self.block_details(&to_details.parent).expect(&format!("5. Expected to find details for block {:?}", from_details.parent));
		}

		let index = from_branch.len();

		from_branch.extend(to_branch.into_iter().rev());

		TreeRoute {
			blocks: from_branch,
Marek Kotewicz's avatar
Marek Kotewicz committed
			ancestor: current_from,
Marek Kotewicz's avatar
Marek Kotewicz committed
	/// Inserts the block into backing cache database.
	/// Expects the block to be valid and already verified.
	/// If the block is already known, does nothing.
	pub fn insert_block(&self, bytes: &[u8], receipts: Vec<Receipt>) {
		// create views onto rlp
Marek Kotewicz's avatar
Marek Kotewicz committed
		let block = BlockView::new(bytes);
		let header = block.header_view();
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
		let hash = header.sha3();
Marek Kotewicz's avatar
Marek Kotewicz committed

Marek Kotewicz's avatar
Marek Kotewicz committed
		if self.is_known(&hash) {
Marek Kotewicz's avatar
Marek Kotewicz committed
			return;
		}

Marek Kotewicz's avatar
Marek Kotewicz committed
		// store block in db
		self.blocks_db.put(&hash, &bytes).unwrap();

		let info = self.block_info(bytes);

		self.apply_update(ExtrasUpdate {
			block_hashes: self.prepare_block_hashes_update(bytes, &info),
			block_details: self.prepare_block_details_update(bytes, &info),
			block_receipts: self.prepare_block_receipts_update(receipts, &info),
			transactions_addresses: self.prepare_transaction_addresses_update(bytes, &info),
			blocks_blooms: self.prepare_block_blooms_update(bytes, &info),
Marek Kotewicz's avatar
Marek Kotewicz committed

	/// Applies extras update.
	fn apply_update(&self, update: ExtrasUpdate) {
		let batch = DBTransaction::new();
		batch.put(b"best", &update.info.hash).unwrap();

Marek Kotewicz's avatar
Marek Kotewicz committed
		// update best block
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
		let mut best_block = self.best_block.write().unwrap();
		match update.info.location {
			BlockLocation::Branch => (),
			_ => {
				*best_block = BestBlock {
					hash: update.info.hash,
					number: update.info.number,
					total_difficulty: update.info.total_difficulty
				};
			}
		}

		let mut write_hashes = self.block_hashes.write().unwrap();
		for (number, hash) in &update.block_hashes {
			batch.put_extras(number, hash);
		let mut write_details = self.block_details.write().unwrap();
		for (hash, details) in update.block_details.into_iter() {
Marek Kotewicz's avatar
Marek Kotewicz committed
			batch.put_extras(&hash, &details);
Gav Wood's avatar
Gav Wood committed
			write_details.insert(hash.clone(), details);
			self.note_used(CacheID::Extras(ExtrasIndex::BlockDetails, hash));
		}

		let mut write_receipts = self.block_receipts.write().unwrap();
		for (hash, receipt) in &update.block_receipts {
			batch.put_extras(hash, receipt);
			write_receipts.remove(hash);
		}

		let mut write_txs = self.transaction_addresses.write().unwrap();
		for (hash, tx_address) in &update.transactions_addresses {
			batch.put_extras(hash, tx_address);

		let mut write_blocks_blooms = self.blocks_blooms.write().unwrap();
		for (bloom_hash, blocks_bloom) in &update.blocks_blooms {
			batch.put_extras(bloom_hash, blocks_bloom);
			write_blocks_blooms.remove(bloom_hash);
		}
Marek Kotewicz's avatar
Marek Kotewicz committed

		// update extras database
		self.extras_db.write(batch).unwrap();
Gav Wood's avatar
Gav Wood committed
	/// Iterator that lists `first` and then all of `first`'s ancestors, by hash.
Gav Wood's avatar
Gav Wood committed
	pub fn ancestry_iter(&self, first: H256) -> Option<AncestryIter> {
Gav Wood's avatar
Gav Wood committed
		if self.is_known(&first) {
			Some(AncestryIter {
				current: first,
				chain: &self,
			})
		} else {
			None
	/// Given a block's `parent`, find every block header which represents a valid possible uncle.
	pub fn find_uncle_headers(&self, parent: &H256, uncle_generations: usize) -> Option<Vec<Header>> {
Gav Wood's avatar
Gav Wood committed
		if !self.is_known(parent) { return None; }

		let mut excluded = HashSet::new();
Gav Wood's avatar
Gav Wood committed
		for a in self.ancestry_iter(parent.clone()).unwrap().take(uncle_generations) {
			excluded.extend(self.uncle_hashes(&a).unwrap().into_iter());
			excluded.insert(a);
		}

		let mut ret = Vec::new();
		for a in self.ancestry_iter(parent.clone()).unwrap().skip(1).take(uncle_generations) {
			ret.extend(self.block_details(&a).unwrap().children.iter()
				.filter_map(|h| if excluded.contains(h) { None } else { self.block_header(h) })
			);
Gav Wood's avatar
Gav Wood committed
		}
Gav Wood's avatar
Gav Wood committed
	}

	/// Get inserted block info which is critical to preapre extras updates.
	fn block_info(&self, block_bytes: &[u8]) -> BlockInfo {
		let block = BlockView::new(block_bytes);
Marek Kotewicz's avatar
Marek Kotewicz committed
		let header = block.header_view();
Marek Kotewicz's avatar
Marek Kotewicz committed
		let hash = block.sha3();
		let number = header.number();
		let parent_hash = header.parent_hash();
		let parent_details = self.block_details(&parent_hash).expect(format!("Invalid parent hash: {:?}", parent_hash).as_ref());
		let total_difficulty = parent_details.total_difficulty + header.difficulty();
		let is_new_best = total_difficulty > self.best_block_total_difficulty();

		BlockInfo {
			hash: hash,
			number: number,
			total_difficulty: total_difficulty,
			location: if is_new_best {
				// on new best block we need to make sure that all ancestors
				// are moved to "canon chain"
				// find the route between old best block and the new one
				let best_hash = self.best_block_hash();
				let route = self.tree_route(best_hash, parent_hash);

				assert_eq!(number, parent_details.number + 1);

				match route.blocks.len() {
					0 => BlockLocation::CanonChain,
					_ => BlockLocation::BranchBecomingCanonChain {
						ancestor: route.ancestor,
						route: route.blocks.into_iter().skip(route.index).collect()
			} else {
				BlockLocation::Branch
	/// This function returns modified block hashes.
	fn prepare_block_hashes_update(&self, block_bytes: &[u8], info: &BlockInfo) -> HashMap<BlockNumber, H256> {
		let mut block_hashes = HashMap::new();
		let block = BlockView::new(block_bytes);
		let header = block.header_view();
		let number = header.number();

		match info.location {
				block_hashes.insert(number, info.hash.clone());
			},
			BlockLocation::BranchBecomingCanonChain { ref ancestor, ref route } => {
				let ancestor_number = self.block_number(ancestor).unwrap();
				let start_number = ancestor_number + 1;

				for (index, hash) in route.iter().cloned().enumerate() {
					block_hashes.insert(start_number + index as BlockNumber, hash);
				block_hashes.insert(number, info.hash.clone());
	/// This function returns modified block details.
	fn prepare_block_details_update(&self, block_bytes: &[u8], info: &BlockInfo) -> HashMap<H256, BlockDetails> {
		let block = BlockView::new(block_bytes);
		let header = block.header_view();
		let parent_hash = header.parent_hash();
		// update parent
		let mut parent_details = self.block_details(&parent_hash).expect(format!("Invalid parent hash: {:?}", parent_hash).as_ref());
		parent_details.children.push(info.hash.clone());

		// create current block details
		let details = BlockDetails {
			number: header.number(),
			total_difficulty: info.total_difficulty,
			parent: parent_hash.clone(),
			children: vec![]
		};
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed

		let mut block_details = HashMap::new();
		block_details.insert(parent_hash, parent_details);
		block_details.insert(info.hash.clone(), details);
		block_details
Marek Kotewicz's avatar
Marek Kotewicz committed

	/// This function returns modified block receipts.
	fn prepare_block_receipts_update(&self, receipts: Vec<Receipt>, info: &BlockInfo) -> HashMap<H256, BlockReceipts> {
		let mut block_receipts = HashMap::new();
		block_receipts.insert(info.hash.clone(), BlockReceipts::new(receipts));
		block_receipts
Marek Kotewicz's avatar
Marek Kotewicz committed

	/// This function returns modified transaction addresses.
	fn prepare_transaction_addresses_update(&self, block_bytes: &[u8], info: &BlockInfo) -> HashMap<H256, TransactionAddress> {
		let block = BlockView::new(block_bytes);
Marek Kotewicz's avatar
Marek Kotewicz committed
		let transaction_hashes = block.transaction_hashes();
		transaction_hashes.into_iter()
			.enumerate()
			.fold(HashMap::new(), |mut acc, (i ,tx_hash)| {
				acc.insert(tx_hash, TransactionAddress {
					block_hash: info.hash.clone(),
					index: i
				});
				acc
			})
	/// This functions returns modified blocks blooms.
Marek Kotewicz's avatar
Marek Kotewicz committed
	/// To accelerate blooms lookups, blomms are stored in multiple
	/// layers (BLOOM_LEVELS, currently 3).
	/// ChainFilter is responsible for building and rebuilding these layers.
	/// It returns them in HashMap, where values are Blooms and
	/// keys are BloomIndexes. BloomIndex represents bloom location on one
	/// of these layers.
Marek Kotewicz's avatar
Marek Kotewicz committed
	///
	/// To reduce number of queries to databse, block blooms are stored
Marek Kotewicz's avatar
Marek Kotewicz committed
	/// in BlocksBlooms structure which contains info about several
	/// (BLOOM_INDEX_SIZE, currently 16) consecutive blocks blooms.
Marek Kotewicz's avatar
Marek Kotewicz committed
	///
	/// Later, BloomIndexer is used to map bloom location on filter layer (BloomIndex)
	/// to bloom location in database (BlocksBloomLocation).
Marek Kotewicz's avatar
Marek Kotewicz committed
	///
	fn prepare_block_blooms_update(&self, block_bytes: &[u8], info: &BlockInfo) -> HashMap<H256, BlocksBlooms> {
		let block = BlockView::new(block_bytes);
		let header = block.header_view();
		let modified_blooms = match info.location {
			BlockLocation::Branch => HashMap::new(),
			BlockLocation::CanonChain => {
				ChainFilter::new(self, self.bloom_indexer.index_size(), self.bloom_indexer.levels())
					.add_bloom(&header.log_bloom(), header.number() as usize)
			BlockLocation::BranchBecomingCanonChain { ref ancestor, ref route } => {
				let ancestor_number = self.block_number(ancestor).unwrap();
				let start_number = ancestor_number + 1;
				let mut blooms: Vec<H2048> = route.iter()
					.map(|hash| self.block(hash).unwrap())
					.map(|bytes| BlockView::new(&bytes).header_view().log_bloom())
					.collect();

				ChainFilter::new(self, self.bloom_indexer.index_size(), self.bloom_indexer.levels())
					.reset_chain_head(&blooms, start_number as usize, self.best_block_number() as usize)
			}
		};
			.fold(HashMap::new(), | mut acc, (bloom_index, bloom) | {
			{
				let location = self.bloom_indexer.location(&bloom_index);
				let mut blocks_blooms = acc
					.entry(location.hash.clone())
					.or_insert_with(|| self.blocks_blooms(&location.hash).unwrap_or_else(BlocksBlooms::new));
				assert_eq!(self.bloom_indexer.index_size(), blocks_blooms.blooms.len());
				blocks_blooms.blooms[location.index] = bloom;
			}
			acc
	/// Get best block hash.
	pub fn best_block_hash(&self) -> H256 {
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
		self.best_block.read().unwrap().hash.clone()
	/// Get best block number.
	pub fn best_block_number(&self) -> BlockNumber {
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
		self.best_block.read().unwrap().number
	/// Get best block total difficulty.
	pub fn best_block_total_difficulty(&self) -> U256 {
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
		self.best_block.read().unwrap().total_difficulty
	/// Get block blooms.
	fn blocks_blooms(&self, hash: &H256) -> Option<BlocksBlooms> {
		self.query_extras(hash, &self.blocks_blooms)
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	fn query_extras<K, T>(&self, hash: &K, cache: &RwLock<HashMap<K, T>>) -> Option<T> where
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
		T: Clone + Decodable + ExtrasIndexable,
Marek Kotewicz's avatar
Marek Kotewicz committed
		K: ExtrasSliceConvertable + Eq + Hash + Clone {
		{
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
			let read = cache.read().unwrap();
Tomusdrw's avatar
Tomusdrw committed
			if let Some(v) = read.get(hash) {
				return Some(v.clone());
Gav Wood's avatar
Gav Wood committed
		if let Some(h) = hash.as_h256() {
			self.note_used(CacheID::Extras(T::extras_index(), h.clone()));
		}

Marek Kotewicz's avatar
Marek Kotewicz committed
		self.extras_db.get_extras(hash).map(| t: T | {
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
			let mut write = cache.write().unwrap();
Marek Kotewicz's avatar
Marek Kotewicz committed
			write.insert(hash.clone(), t.clone());
			t
		})
Marek Kotewicz's avatar
Marek Kotewicz committed
	}

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	fn query_extras_exist<K, T>(&self, hash: &K, cache: &RwLock<HashMap<K, T>>) -> bool where
Marek Kotewicz's avatar
Marek Kotewicz committed
		K: ExtrasSliceConvertable + Eq + Hash + Clone,
		T: ExtrasIndexable {
Marek Kotewicz's avatar
Marek Kotewicz committed
		{
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
			let read = cache.read().unwrap();
			if let Some(_) = read.get(hash) {
				return true;
Marek Kotewicz's avatar
Marek Kotewicz committed
		self.extras_db.extras_exists::<_, T>(hash)
Marek Kotewicz's avatar
Marek Kotewicz committed
	}
	/// Get current cache size.
	pub fn cache_size(&self) -> CacheSize {
		CacheSize {
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
			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(),
			block_logs: self.block_logs.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()
Gav Wood's avatar
Gav Wood committed
	/// 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();
Gav Wood's avatar
Gav Wood committed
		if !cache_man.cache_usage[0].contains(&id) {
			cache_man.cache_usage[0].insert(id.clone());
			if cache_man.in_use.contains(&id) {
				if let Some(c) = cache_man.cache_usage.iter_mut().skip(1).find(|e|e.contains(&id)) {
					c.remove(&id);
				}
			} else {
				cache_man.in_use.insert(id);
			}
		}
	}

	/// Ticks our cache system and throws out any old data.
Gav Wood's avatar
Gav Wood committed
	pub fn collect_garbage(&self) {
		if self.cache_size().total() < self.pref_cache_size { return; }

		for _ in 0..COLLECTION_QUEUE_SIZE {
			{
				let mut cache_man = self.cache_man.write().unwrap();
				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 block_logs = self.block_logs.write().unwrap();
				let mut blocks_blooms = self.blocks_blooms.write().unwrap();
				let mut block_receipts = self.block_receipts.write().unwrap();
Gav Wood's avatar
Gav Wood committed

				for id in cache_man.cache_usage.pop_back().unwrap().into_iter() {
					cache_man.in_use.remove(&id);
					match id {
						CacheID::Block(h) => { blocks.remove(&h); },
						CacheID::Extras(ExtrasIndex::BlockDetails, h) => { block_details.remove(&h); },
						CacheID::Extras(ExtrasIndex::TransactionAddress, h) => { transaction_addresses.remove(&h); },
						CacheID::Extras(ExtrasIndex::BlockLogBlooms, h) => { block_logs.remove(&h); },
						CacheID::Extras(ExtrasIndex::BlocksBlooms, h) => { blocks_blooms.remove(&h); },
						CacheID::Extras(ExtrasIndex::BlockReceipts, h) => { block_receipts.remove(&h); },
Gav Wood's avatar
Gav Wood committed
						_ => panic!(),
					}
				}
				cache_man.cache_usage.push_front(HashSet::new());
Gav Wood's avatar
Gav Wood committed

Gav Wood's avatar
Gav Wood committed
				// TODO: handle block_hashes properly.
				block_hashes.clear();

				blocks.shrink_to_fit();
				block_details.shrink_to_fit();
 				block_hashes.shrink_to_fit();
 				transaction_addresses.shrink_to_fit();
 				block_logs.shrink_to_fit();
 				blocks_blooms.shrink_to_fit();
 				block_receipts.shrink_to_fit();
Gav Wood's avatar
Gav Wood committed
			}
Gav Wood's avatar
Gav Wood committed
			if self.cache_size().total() < self.max_cache_size { break; }
Gav Wood's avatar
Gav Wood committed
		}
Gav Wood's avatar
Gav Wood committed
		// TODO: m_lastCollection = chrono::system_clock::now();
Marek Kotewicz's avatar
Marek Kotewicz committed
}
Marek Kotewicz's avatar
Marek Kotewicz committed

#[cfg(test)]
mod tests {
	use std::str::FromStr;
	use rustc_serialize::hex::FromHex;
	use util::hash::*;
Marek Kotewicz's avatar
Marek Kotewicz committed
	use util::sha3::Hashable;
	use blockchain::{BlockProvider, BlockChain, BlockChainConfig};
	use tests::helpers::*;
	use devtools::*;
	use blockchain::generator::{ChainGenerator, ChainIterator, BlockFinalizer};
Marek Kotewicz's avatar
Marek Kotewicz committed
	use views::BlockView;
Marek Kotewicz's avatar
Marek Kotewicz committed
	fn basic_blockchain_insert() {
		let mut canon_chain = ChainGenerator::default();
		let mut finalizer = BlockFinalizer::default();
		let genesis = canon_chain.generate(&mut finalizer).unwrap();
		let first = canon_chain.generate(&mut finalizer).unwrap();
Marek Kotewicz's avatar
Marek Kotewicz committed
		let genesis_hash = BlockView::new(&genesis).header_view().sha3();
		let first_hash = BlockView::new(&first).header_view().sha3();
		let temp = RandomTempPath::new();
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
		let bc = BlockChain::new(BlockChainConfig::default(), &genesis, temp.as_path());
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed

		assert_eq!(bc.genesis_hash(), genesis_hash.clone());
		assert_eq!(bc.best_block_number(), 0);
		assert_eq!(bc.best_block_hash(), genesis_hash.clone());
		assert_eq!(bc.block_hash(0), Some(genesis_hash.clone()));
		assert_eq!(bc.block_hash(1), None);
		assert_eq!(bc.block_details(&genesis_hash).unwrap().children, vec![]);
		bc.insert_block(&first, vec![]);
		assert_eq!(bc.block_hash(0), Some(genesis_hash.clone()));
		assert_eq!(bc.best_block_number(), 1);
		assert_eq!(bc.best_block_hash(), first_hash.clone());
		assert_eq!(bc.block_hash(1), Some(first_hash.clone()));
		assert_eq!(bc.block_details(&first_hash).unwrap().parent, genesis_hash.clone());
		assert_eq!(bc.block_details(&genesis_hash).unwrap().children, vec![first_hash.clone()]);
		assert_eq!(bc.block_hash(2), None);
Marek Kotewicz's avatar
Marek Kotewicz committed

Gav Wood's avatar
Gav Wood committed
	#[test]
	fn check_ancestry_iter() {
		let mut canon_chain = ChainGenerator::default();
		let mut finalizer = BlockFinalizer::default();
		let genesis = canon_chain.generate(&mut finalizer).unwrap();
		let genesis_hash = BlockView::new(&genesis).header_view().sha3();

		let temp = RandomTempPath::new();
		let bc = BlockChain::new(BlockChainConfig::default(), &genesis, temp.as_path());

		let mut block_hashes = vec![genesis_hash.clone()];
		for _ in 0..10 {
			let block = canon_chain.generate(&mut finalizer).unwrap();
			block_hashes.push(BlockView::new(&block).header_view().sha3());
			bc.insert_block(&block, vec![]);
		}

		block_hashes.reverse();

Gav Wood's avatar
Gav Wood committed
		assert_eq!(bc.ancestry_iter(block_hashes[0].clone()).unwrap().collect::<Vec<_>>(), block_hashes)
Gav Wood's avatar
Gav Wood committed
	}

Gav Wood's avatar
Gav Wood committed
	#[test]
	#[cfg_attr(feature="dev", allow(cyclomatic_complexity))]
	fn test_find_uncles() {
		let mut canon_chain = ChainGenerator::default();
		let mut finalizer = BlockFinalizer::default();
		let genesis = canon_chain.generate(&mut finalizer).unwrap();
		let b1b = canon_chain.fork(1).generate(&mut finalizer.fork()).unwrap();
		let b1a = canon_chain.generate(&mut finalizer).unwrap();
		let b2b = canon_chain.fork(1).generate(&mut finalizer.fork()).unwrap();
		let b2a = canon_chain.generate(&mut finalizer).unwrap();
		let b3b = canon_chain.fork(1).generate(&mut finalizer.fork()).unwrap();
		let b3a = canon_chain.generate(&mut finalizer).unwrap();
		let b4b = canon_chain.fork(1).generate(&mut finalizer.fork()).unwrap();
		let b4a = canon_chain.generate(&mut finalizer).unwrap();
		let b5b = canon_chain.fork(1).generate(&mut finalizer.fork()).unwrap();
		let b5a = canon_chain.generate(&mut finalizer).unwrap();

		let temp = RandomTempPath::new();
		let bc = BlockChain::new(BlockChainConfig::default(), &genesis, temp.as_path());
		bc.insert_block(&b1a, vec![]);
		bc.insert_block(&b1b, vec![]);
		bc.insert_block(&b2a, vec![]);
		bc.insert_block(&b2b, vec![]);
		bc.insert_block(&b3a, vec![]);
		bc.insert_block(&b3b, vec![]);
		bc.insert_block(&b4a, vec![]);
		bc.insert_block(&b4b, vec![]);
		bc.insert_block(&b5a, vec![]);
		bc.insert_block(&b5b, vec![]);

		assert_eq!(
			[&b4b, &b3b, &b2b].iter().map(|b| BlockView::new(b).header()).collect::<Vec<_>>(),
			bc.find_uncle_headers(&BlockView::new(&b4a).header_view().sha3(), 3).unwrap()
		);

		// TODO: insert block that already includes one of them as an uncle to check it's not allowed.
	}

Marek Kotewicz's avatar
Marek Kotewicz committed
	#[test]
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
	#[cfg_attr(feature="dev", allow(cyclomatic_complexity))]
Marek Kotewicz's avatar
Marek Kotewicz committed
	fn test_small_fork() {
Marek Kotewicz's avatar
Marek Kotewicz committed
		let mut canon_chain = ChainGenerator::default();
		let mut finalizer = BlockFinalizer::default();
		let genesis = canon_chain.generate(&mut finalizer).unwrap();
		let b1 = canon_chain.generate(&mut finalizer).unwrap();
		let b2 = canon_chain.generate(&mut finalizer).unwrap();
		let b3b = canon_chain.fork(1).generate(&mut finalizer.fork()).unwrap();
		let b3a = canon_chain.generate(&mut finalizer).unwrap();
Marek Kotewicz's avatar
Marek Kotewicz committed

		let genesis_hash = BlockView::new(&genesis).header_view().sha3();
		let b1_hash= BlockView::new(&b1).header_view().sha3();
		let b2_hash= BlockView::new(&b2).header_view().sha3();
		let b3a_hash= BlockView::new(&b3a).header_view().sha3();
		let b3b_hash= BlockView::new(&b3b).header_view().sha3();
Marek Kotewicz's avatar
Marek Kotewicz committed

		// b3a is a part of canon chain, whereas b3b is part of sidechain
Marek Kotewicz's avatar
Marek Kotewicz committed
		let best_block_hash = b3a_hash.clone();
Marek Kotewicz's avatar
Marek Kotewicz committed

		let temp = RandomTempPath::new();
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
		let bc = BlockChain::new(BlockChainConfig::default(), &genesis, temp.as_path());
		bc.insert_block(&b1, vec![]);
		bc.insert_block(&b2, vec![]);
		bc.insert_block(&b3a, vec![]);
		bc.insert_block(&b3b, vec![]);
Marek Kotewicz's avatar
Marek Kotewicz committed

		assert_eq!(bc.best_block_hash(), best_block_hash);
		assert_eq!(bc.block_number(&genesis_hash).unwrap(), 0);
		assert_eq!(bc.block_number(&b1_hash).unwrap(), 1);
		assert_eq!(bc.block_number(&b2_hash).unwrap(), 2);
		assert_eq!(bc.block_number(&b3a_hash).unwrap(), 3);
		assert_eq!(bc.block_number(&b3b_hash).unwrap(), 3);

		assert_eq!(bc.block_hash(0).unwrap(), genesis_hash);
		assert_eq!(bc.block_hash(1).unwrap(), b1_hash);
		assert_eq!(bc.block_hash(2).unwrap(), b2_hash);
		assert_eq!(bc.block_hash(3).unwrap(), b3a_hash);
Marek Kotewicz's avatar
Marek Kotewicz committed

		// test trie route
		let r0_1 = bc.tree_route(genesis_hash.clone(), b1_hash.clone());
Marek Kotewicz's avatar
Marek Kotewicz committed
		assert_eq!(r0_1.ancestor, genesis_hash);
		assert_eq!(r0_1.blocks, [b1_hash.clone()]);
		assert_eq!(r0_1.index, 0);

		let r0_2 = bc.tree_route(genesis_hash.clone(), b2_hash.clone());
Marek Kotewicz's avatar
Marek Kotewicz committed
		assert_eq!(r0_2.ancestor, genesis_hash);
		assert_eq!(r0_2.blocks, [b1_hash.clone(), b2_hash.clone()]);
		assert_eq!(r0_2.index, 0);

		let r1_3a = bc.tree_route(b1_hash.clone(), b3a_hash.clone());
Marek Kotewicz's avatar
Marek Kotewicz committed
		assert_eq!(r1_3a.ancestor, b1_hash);
		assert_eq!(r1_3a.blocks, [b2_hash.clone(), b3a_hash.clone()]);
		assert_eq!(r1_3a.index, 0);

		let r1_3b = bc.tree_route(b1_hash.clone(), b3b_hash.clone());
Marek Kotewicz's avatar
Marek Kotewicz committed
		assert_eq!(r1_3b.ancestor, b1_hash);
		assert_eq!(r1_3b.blocks, [b2_hash.clone(), b3b_hash.clone()]);
		assert_eq!(r1_3b.index, 0);

		let r3a_3b = bc.tree_route(b3a_hash.clone(), b3b_hash.clone());
Marek Kotewicz's avatar
Marek Kotewicz committed
		assert_eq!(r3a_3b.ancestor, b2_hash);
		assert_eq!(r3a_3b.blocks, [b3a_hash.clone(), b3b_hash.clone()]);
		assert_eq!(r3a_3b.index, 1);

		let r1_0 = bc.tree_route(b1_hash.clone(), genesis_hash.clone());
Marek Kotewicz's avatar
Marek Kotewicz committed
		assert_eq!(r1_0.ancestor, genesis_hash);
		assert_eq!(r1_0.blocks, [b1_hash.clone()]);
		assert_eq!(r1_0.index, 1);

		let r2_0 = bc.tree_route(b2_hash.clone(), genesis_hash.clone());
Marek Kotewicz's avatar
Marek Kotewicz committed
		assert_eq!(r2_0.ancestor, genesis_hash);
		assert_eq!(r2_0.blocks, [b2_hash.clone(), b1_hash.clone()]);
		assert_eq!(r2_0.index, 2);
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed

		let r3a_1 = bc.tree_route(b3a_hash.clone(), b1_hash.clone());
Marek Kotewicz's avatar
Marek Kotewicz committed
		assert_eq!(r3a_1.ancestor, b1_hash);