blockchain.rs 85.8 KiB
Newer Older
			blocks_blooms: self.prepare_block_blooms_update(bytes, &info),
			transactions_addresses: self.prepare_transaction_addresses_update(bytes, &info),
Marek Kotewicz's avatar
Marek Kotewicz committed
			info: info.clone(),
Tomasz Drwięga's avatar
Tomasz Drwięga committed
			block: bytes,
Marek Kotewicz's avatar
Marek Kotewicz committed

		ImportRoute::from(info)
Gav Wood's avatar
Gav Wood committed
	/// Get inserted block info which is critical to prepare extras updates.
	fn block_info(&self, header: &HeaderView, route: TreeRoute, extras: &ExtrasInsert) -> BlockInfo {
		let hash = header.hash();
Gav Wood's avatar
Gav Wood committed
		let number = header.number();
		let parent_hash = header.parent_hash();
		let parent_details = self.block_details(&parent_hash).unwrap_or_else(|| panic!("Invalid parent hash: {:?}", parent_hash));

		BlockInfo {
			hash: hash,
			number: number,
keorn's avatar
keorn committed
			total_difficulty: parent_details.total_difficulty + header.difficulty(),
			location: match extras.fork_choice {
				ForkChoice::New => {
					// 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
					match route.blocks.len() {
						0 => BlockLocation::CanonChain,
						_ => {
							let retracted = route.blocks.iter().take(route.index).cloned().collect::<Vec<_>>().into_iter().collect::<Vec<_>>();
							let enacted = route.blocks.into_iter().skip(route.index).collect::<Vec<_>>();
							BlockLocation::BranchBecomingCanonChain(BranchBecomingCanonChainData {
								ancestor: route.ancestor,
								enacted: enacted,
								retracted: retracted,
							})
						}
Gav Wood's avatar
Gav Wood committed
					}
				},
				ForkChoice::Old => BlockLocation::Branch,
			},
	/// Mark a block to be considered finalized. Returns `Some(())` if the operation succeeds, and `None` if the block
	/// hash is not found.
	pub fn mark_finalized(&self, batch: &mut DBTransaction, block_hash: H256) -> Option<()> {
		let mut block_details = self.block_details(&block_hash)?;
		block_details.is_finalized = true;

		self.update_block_details(batch, block_hash, block_details);
		Some(())
	}

	/// Prepares extras block detail update.
	fn update_block_details(&self, batch: &mut DBTransaction, block_hash: H256, block_details: BlockDetails) {
		let mut details_map = HashMap::new();
		details_map.insert(block_hash, block_details);

		// We're only updating one existing value. So it shouldn't suffer from cache decoherence problem.
		let mut write_details = self.pending_block_details.write();
		batch.extend_with_cache(db::COL_EXTRA, &mut *write_details, details_map, CacheUpdatePolicy::Overwrite);
	}

	/// Prepares extras update.
	fn prepare_update(&self, batch: &mut DBTransaction, update: ExtrasUpdate, is_best: bool) {
			let mut write_receipts = self.block_receipts.write();
			batch.extend_with_cache(db::COL_EXTRA, &mut *write_receipts, update.block_receipts, CacheUpdatePolicy::Remove);
		if let Some((block, blooms)) = update.blocks_blooms {
			self.db.blooms()
				.insert_blooms(block, blooms.iter())
				.expect("Low level database error. Some issue with disk?");
		// These cached values must be updated last with all four locks taken to avoid
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
		{
			let mut best_block = self.pending_best_block.write();
			if is_best && update.info.location != BlockLocation::Branch {
				batch.put(db::COL_EXTRA, b"best", &update.info.hash);
				let block = encoded::Block::new(update.block.to_vec());
				*best_block = Some(BestBlock {
					total_difficulty: update.info.total_difficulty,
					header: block.decode_header(),
					block,
			let mut write_hashes = self.pending_block_hashes.write();
			let mut write_details = self.pending_block_details.write();
			let mut write_txs = self.pending_transaction_addresses.write();
			batch.extend_with_cache(db::COL_EXTRA, &mut *write_details, update.block_details, CacheUpdatePolicy::Overwrite);
			batch.extend_with_cache(db::COL_EXTRA, &mut *write_hashes, update.block_hashes, CacheUpdatePolicy::Overwrite);
			batch.extend_with_option_cache(db::COL_EXTRA, &mut *write_txs, update.transactions_addresses, CacheUpdatePolicy::Overwrite);
	/// Apply pending insertion updates
	pub fn commit(&self) {
		let mut pending_best_block = self.pending_best_block.write();
		let mut pending_write_hashes = self.pending_block_hashes.write();
		let mut pending_block_details = self.pending_block_details.write();
		let mut pending_write_txs = self.pending_transaction_addresses.write();

		let mut best_block = self.best_block.write();
		let mut write_block_details = self.block_details.write();
		let mut write_hashes = self.block_hashes.write();
		let mut write_txs = self.transaction_addresses.write();
		// update best block
		if let Some(block) = pending_best_block.take() {
			*best_block = block;
		}

		let pending_txs = mem::replace(&mut *pending_write_txs, HashMap::new());
		let (retracted_txs, enacted_txs) = pending_txs.into_iter().partition::<HashMap<_, _>, _>(|&(_, ref value)| value.is_none());

		let pending_hashes_keys: Vec<_> = pending_write_hashes.keys().cloned().collect();
		let enacted_txs_keys: Vec<_> = enacted_txs.keys().cloned().collect();
		let pending_block_hashes: Vec<_> = pending_block_details.keys().cloned().collect();
		write_hashes.extend(mem::replace(&mut *pending_write_hashes, HashMap::new()));
		write_txs.extend(enacted_txs.into_iter().map(|(k, v)| (k, v.expect("Transactions were partitioned; qed"))));
		write_block_details.extend(mem::replace(&mut *pending_block_details, HashMap::new()));

		for hash in retracted_txs.keys() {
			write_txs.remove(hash);
		}
		let mut cache_man = self.cache_man.lock();
		for n in pending_hashes_keys {
			cache_man.note_used(CacheId::BlockHashes(n));
		for hash in enacted_txs_keys {
			cache_man.note_used(CacheId::TransactionAddresses(hash));

		for hash in pending_block_hashes {
			cache_man.note_used(CacheId::BlockDetails(hash));
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,
Gav Wood's avatar
Gav Wood committed
			})
		} else {
			None
	/// Iterator that lists `first` and then all of `first`'s ancestors, by extended header.
	pub fn ancestry_with_metadata_iter<'a>(&'a self, first: H256) -> AncestryWithMetadataIter {
		AncestryWithMetadataIter {
			current: if self.is_known(&first) {
				first
			} else {
				H256::default() // zero hash
			},
			chain: self
		}
	}

	/// 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<encoded::Header>> {
		self.find_uncle_hashes(parent, uncle_generations)
			.map(|v| v.into_iter().filter_map(|h| self.block_header_data(&h)).collect())
	}

	/// Given a block's `parent`, find every block hash which represents a valid possible uncle.
	pub fn find_uncle_hashes(&self, parent: &H256, uncle_generations: usize) -> Option<Vec<H256>> {

		let mut excluded = HashSet::new();
		let ancestry = self.ancestry_iter(parent.clone())?;

		for a in ancestry.clone().take(uncle_generations) {
			if let Some(uncles) = self.uncle_hashes(&a) {
				excluded.extend(uncles);
				excluded.insert(a);
			} else {
				break
			}
		for a in ancestry.skip(1).take(uncle_generations) {
			if let Some(details) = self.block_details(&a) {
				ret.extend(details.children.iter().filter(|h| !excluded.contains(h)))
			} else {
				break
			}
Gav Wood's avatar
Gav Wood committed
		}
	/// 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 = view!(BlockView, block_bytes);
		let header = block.header_view();
		let number = header.number();

		match info.location {
				block_hashes.insert(number, info.hash);
Nikolay Volf's avatar
Nikolay Volf committed
			BlockLocation::BranchBecomingCanonChain(ref data) => {
Tomasz Drwięga's avatar
Tomasz Drwięga committed
				let ancestor_number = self.block_number(&data.ancestor).expect("Block number of ancestor is always in DB");
Nikolay Volf's avatar
Nikolay Volf committed
				for (index, hash) in data.enacted.iter().cloned().enumerate() {
					block_hashes.insert(start_number + index as BlockNumber, hash);
				block_hashes.insert(number, info.hash);
	/// This function returns modified block details.
	/// Uses the given parent details or attempts to load them from the database.
	fn prepare_block_details_update(&self, block_bytes: &[u8], info: &BlockInfo, is_finalized: bool, metadata: Option<Vec<u8>>) -> HashMap<H256, BlockDetails> {
		let block = view!(BlockView, block_bytes);
		let parent_hash = header.parent_hash();
		let mut parent_details = self.block_details(&parent_hash).unwrap_or_else(|| panic!("Invalid parent hash: {:?}", parent_hash));
		parent_details.children.push(info.hash);
		// create current block details.
		let details = BlockDetails {
			number: header.number(),
			total_difficulty: info.total_difficulty,
			parent: parent_hash,
			is_finalized: is_finalized,
			metadata: metadata,
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, details);
	/// 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, BlockReceipts::new(receipts));
	/// This function returns modified transaction addresses.
	fn prepare_transaction_addresses_update(&self, block_bytes: &[u8], info: &BlockInfo) -> HashMap<H256, Option<TransactionAddress>> {
		let block = view!(BlockView, block_bytes);
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
		let transaction_hashes = block.transaction_hashes();
		match info.location {
			BlockLocation::CanonChain => {
				transaction_hashes.into_iter()
					.enumerate()
					.map(|(i ,tx_hash)| {
						(tx_hash, Some(TransactionAddress {
							block_hash: info.hash,
					})
					.collect()
			},
			BlockLocation::BranchBecomingCanonChain(ref data) => {
				let addresses = data.enacted.iter()
Marek Kotewicz's avatar
Marek Kotewicz committed
					.flat_map(|hash| {
						let body = self.block_body(hash).expect("Enacted block must be in database.");
						let hashes = body.transaction_hashes();
						hashes.into_iter()
							.enumerate()
							.map(|(i, tx_hash)| (tx_hash, Some(TransactionAddress {
								block_hash: *hash,
							})))
							.collect::<HashMap<H256, Option<TransactionAddress>>>()
					});

				let current_addresses = transaction_hashes.into_iter()
					.enumerate()
					.map(|(i ,tx_hash)| {
						(tx_hash, Some(TransactionAddress {
							block_hash: info.hash,
				let retracted = data.retracted.iter().flat_map(|hash| {
					let body = self.block_body(hash).expect("Retracted block must be in database.");
					let hashes = body.transaction_hashes();
					hashes.into_iter().map(|hash| (hash, None)).collect::<HashMap<H256, Option<TransactionAddress>>>()
				});

				// The order here is important! Don't remove transaction if it was part of enacted blocks as well.
				retracted.chain(addresses).chain(current_addresses).collect()
			},
			BlockLocation::Branch => HashMap::new(),
		}
	/// This functions returns modified blocks blooms.
	///
	/// 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.
	///
	/// To reduce number of queries to databse, block blooms are stored
	/// in BlocksBlooms structure which contains info about several
	/// (BLOOM_INDEX_SIZE, currently 16) consecutive blocks blooms.
	///
	/// Later, BloomIndexer is used to map bloom location on filter layer (BloomIndex)
	/// to bloom location in database (BlocksBloomLocation).
	///
	fn prepare_block_blooms_update(&self, block_bytes: &[u8], info: &BlockInfo) -> Option<(u64, Vec<Bloom>)> {
		let block = view!(BlockView, block_bytes);
		let header = block.header_view();

		match info.location {
			BlockLocation::Branch => None,
			BlockLocation::CanonChain => {
				let log_bloom = header.log_bloom();
				if log_bloom.is_zero() {
				} else {
					Some((info.number, vec![log_bloom]))
				}
			},
			BlockLocation::BranchBecomingCanonChain(ref data) => {
				let ancestor_number = self.block_number(&data.ancestor).unwrap();
				let start_number = ancestor_number + 1;

				let mut blooms: Vec<Bloom> = data.enacted.iter()
					.map(|hash| self.block_header_data(hash).unwrap())
					.map(|h| h.log_bloom())
					.collect();

				blooms.push(header.log_bloom());
				Some((start_number, blooms))
	/// Get best block hash.
	pub fn best_block_hash(&self) -> H256 {
		self.best_block.read().header.hash()
	/// Get best block number.
	pub fn best_block_number(&self) -> BlockNumber {
		self.best_block.read().header.number()
	/// Get best block timestamp.
	pub fn best_block_timestamp(&self) -> u64 {
		self.best_block.read().header.timestamp()
	/// Get best block total difficulty.
	pub fn best_block_total_difficulty(&self) -> U256 {
		self.best_block.read().total_difficulty
Tomasz Drwięga's avatar
Tomasz Drwięga committed
	/// Get best block header
	pub fn best_block_header(&self) -> Header {
		self.best_block.read().header.clone()
	/// Get current cache size.
	pub fn cache_size(&self) -> CacheSize {
		CacheSize {
Tomasz Drwięga's avatar
Tomasz Drwięga committed
			blocks: self.block_headers.read().heap_size_of_children() + self.block_bodies.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(),
			block_receipts: self.block_receipts.read().heap_size_of_children(),
	/// Ticks our cache system and throws out any old data.
Gav Wood's avatar
Gav Wood committed
	pub fn collect_garbage(&self) {
		let current_size = self.cache_size().total();
		let mut block_headers = self.block_headers.write();
		let mut block_bodies = self.block_bodies.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 block_receipts = self.block_receipts.write();

		let mut cache_man = self.cache_man.lock();
		cache_man.collect_garbage(current_size, | ids | {
			for id in &ids {
				match *id {
					CacheId::BlockHeader(ref h) => { block_headers.remove(h); },
					CacheId::BlockBody(ref h) => { block_bodies.remove(h); },
					CacheId::BlockDetails(ref h) => { block_details.remove(h); }
					CacheId::BlockHashes(ref h) => { block_hashes.remove(h); }
					CacheId::TransactionAddresses(ref h) => { transaction_addresses.remove(h); }
					CacheId::BlockReceipts(ref h) => { block_receipts.remove(h); }
Gav Wood's avatar
Gav Wood committed
			}
			block_headers.shrink_to_fit();
			block_bodies.shrink_to_fit();
			block_details.shrink_to_fit();
			block_hashes.shrink_to_fit();
			transaction_addresses.shrink_to_fit();
			block_receipts.shrink_to_fit();

			block_headers.heap_size_of_children() +
			block_bodies.heap_size_of_children() +
			block_details.heap_size_of_children() +
			block_hashes.heap_size_of_children() +
			transaction_addresses.heap_size_of_children() +
			block_receipts.heap_size_of_children()
Tomasz Drwięga's avatar
Tomasz Drwięga committed

	/// Create a block body from a block.
	pub fn block_to_body(block: &[u8]) -> Bytes {
		let mut body = RlpStream::new_list(2);
		let block_view = view!(BlockView, block);
		body.append_raw(block_view.transactions_rlp().as_raw(), 1);
		body.append_raw(block_view.uncles_rlp().as_raw(), 1);
Tomasz Drwięga's avatar
Tomasz Drwięga committed
		body.out()
	}

	/// Returns general blockchain information
	pub fn chain_info(&self) -> BlockChainInfo {
		// ensure data consistencly by locking everything first
		let best_block = self.best_block.read();
		let best_ancient_block = self.best_ancient_block.read();
		BlockChainInfo {
			total_difficulty: best_block.total_difficulty,
			pending_total_difficulty: best_block.total_difficulty,
			genesis_hash: self.genesis_hash(),
			best_block_hash: best_block.header.hash(),
			best_block_number: best_block.header.number(),
			best_block_timestamp: best_block.header.timestamp(),
			first_block_hash: self.first_block(),
			first_block_number: From::from(self.first_block_number()),
			ancient_block_hash: best_ancient_block.as_ref().map(|b| b.hash),
			ancient_block_number: best_ancient_block.as_ref().map(|b| b.number),
		}
	}
Marek Kotewicz's avatar
Marek Kotewicz committed
}
Marek Kotewicz's avatar
Marek Kotewicz committed

#[cfg(test)]
mod tests {
Tomasz Drwięga's avatar
Tomasz Drwięga committed
	use std::sync::Arc;
	use rustc_hex::FromHex;
	use hash::keccak;
	use kvdb::DBTransaction;
Marek Kotewicz's avatar
Marek Kotewicz committed
	use ethereum_types::*;
	use receipt::{Receipt, TransactionOutcome};
	use blockchain::{BlockProvider, BlockChain, BlockChainDB, Config, ImportRoute};
	use test_helpers::{
		generate_dummy_blockchain, generate_dummy_blockchain_with_extra,
		generate_dummy_empty_blockchain
	};
	use blockchain::generator::{BlockGenerator, BlockBuilder, BlockOptions};
	use blockchain::extras::TransactionAddress;
	use transaction::{Transaction, Action};
	use log_entry::{LogEntry, LocalizedLogEntry};
	use test_helpers::new_db;
	fn new_chain(genesis: &[u8], db: Arc<BlockChainDB>) -> BlockChain {
keorn's avatar
keorn committed
		BlockChain::new(Config::default(), genesis, db)
	fn insert_block(db: &Arc<BlockChainDB>, bc: &BlockChain, bytes: &[u8], receipts: Vec<Receipt>) -> ImportRoute {
		insert_block_commit(db, bc, bytes, receipts, true)
	}

	fn insert_block_commit(db: &Arc<BlockChainDB>, bc: &BlockChain, bytes: &[u8], receipts: Vec<Receipt>, commit: bool) -> ImportRoute {
		let mut batch = db.key_value().transaction();
		let res = insert_block_batch(&mut batch, bc, bytes, receipts);
		db.key_value().write(batch).unwrap();
		if commit {
			bc.commit();
		}
		res
	}

	fn insert_block_batch(batch: &mut DBTransaction, bc: &BlockChain, bytes: &[u8], receipts: Vec<Receipt>) -> ImportRoute {
		use views::BlockView;
		use blockchain::ExtrasInsert;

		let block = view!(BlockView, bytes);
		let header = block.header_view();
		let parent_hash = header.parent_hash();
		let parent_details = bc.block_details(&parent_hash).unwrap_or_else(|| panic!("Invalid parent hash: {:?}", parent_hash));
		let block_total_difficulty = parent_details.total_difficulty + header.difficulty();
		let fork_choice = if block_total_difficulty > bc.best_block_total_difficulty() {
			::engines::ForkChoice::New
		} else {
			::engines::ForkChoice::Old
		};

		bc.insert_block(batch, bytes, receipts, ExtrasInsert {
			fork_choice: fork_choice,
			is_finalized: false,
			metadata: None
		})
	}

Tomasz Drwięga's avatar
Tomasz Drwięga committed
	#[test]
	fn should_cache_best_block() {
		// given
		let genesis = BlockBuilder::genesis();
		let first = genesis.add_block();
		let bc = new_chain(&genesis.last().encoded(), db.clone());
Tomasz Drwięga's avatar
Tomasz Drwięga committed
		assert_eq!(bc.best_block_number(), 0);

		// when
		insert_block_commit(&db, &bc, &first.last().encoded(), vec![], false);
		assert_eq!(bc.best_block_number(), 0);
		bc.commit();
Tomasz Drwięga's avatar
Tomasz Drwięga committed
		// NOTE no db.write here (we want to check if best block is cached)

		// then
		assert_eq!(bc.best_block_number(), 1);
		assert!(bc.block(&bc.best_block_hash()).is_some(), "Best block should be queryable even without DB write.");
	}
Marek Kotewicz's avatar
Marek Kotewicz committed
	fn basic_blockchain_insert() {
		let genesis = BlockBuilder::genesis();
		let first = genesis.add_block();

		let genesis = genesis.last();
		let first = first.last();
		let genesis_hash = genesis.hash();
		let first_hash = first.hash();
		let bc = new_chain(&genesis.encoded(), db.clone());
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed

		assert_eq!(bc.genesis_hash(), genesis_hash);
		assert_eq!(bc.best_block_hash(), genesis_hash);
		assert_eq!(bc.block_hash(0), Some(genesis_hash));
		assert_eq!(bc.block_hash(1), None);
		assert_eq!(bc.block_details(&genesis_hash).unwrap().children, vec![]);
		let mut batch = db.key_value().transaction();
		insert_block_batch(&mut batch, &bc, &first.encoded(), vec![]);
		db.key_value().write(batch).unwrap();
		assert_eq!(bc.block_hash(0), Some(genesis_hash));
		assert_eq!(bc.best_block_number(), 1);
		assert_eq!(bc.best_block_hash(), first_hash);
		assert_eq!(bc.block_hash(1), Some(first_hash));
		assert_eq!(bc.block_details(&first_hash).unwrap().parent, genesis_hash);
		assert_eq!(bc.block_details(&genesis_hash).unwrap().children, vec![first_hash]);
		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 genesis = BlockBuilder::genesis();
		let first_10 = genesis.add_blocks(10);
		let generator = BlockGenerator::new(vec![first_10]);
Gav Wood's avatar
Gav Wood committed

		let bc = new_chain(&genesis.last().encoded(), db.clone());
Gav Wood's avatar
Gav Wood committed

		let mut block_hashes = vec![genesis.last().hash()];
		let mut batch = db.key_value().transaction();
		for block in generator {
			block_hashes.push(block.hash());
			insert_block_batch(&mut batch, &bc, &block.encoded(), vec![]);
Gav Wood's avatar
Gav Wood committed
		}
		db.key_value().write(batch).unwrap();
Gav Wood's avatar
Gav Wood committed

		block_hashes.reverse();

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

Marek Kotewicz's avatar
Marek Kotewicz committed
	#[test]
Gav Wood's avatar
Gav Wood committed
	fn test_find_uncles() {
		let genesis = BlockBuilder::genesis();
		let b1a = genesis.add_block();
		let b2a = b1a.add_block();
		let b3a = b2a.add_block();
		let b4a = b3a.add_block();
		let b5a = b4a.add_block();

		let b1b = genesis.add_block_with_difficulty(9);
		let b2b = b1a.add_block_with_difficulty(9);
		let b3b = b2a.add_block_with_difficulty(9);
		let b4b = b3a.add_block_with_difficulty(9);
		let b5b = b4a.add_block_with_difficulty(9);

		let uncle_headers = vec![
			b4b.last().header().encoded(),
			b3b.last().header().encoded(),
			b2b.last().header().encoded(),
		];
		let b4a_hash = b4a.last().hash();

		let generator = BlockGenerator::new(
			vec![b1a, b1b, b2a, b2b, b3a, b3b, b4a, b4b, b5a, b5b]
		);
		let bc = new_chain(&genesis.last().encoded(), db.clone());
		for b in generator {
			insert_block(&db, &bc, &b.encoded(), vec![]);
Gav Wood's avatar
Gav Wood committed

		assert_eq!(uncle_headers, bc.find_uncle_headers(&b4a_hash, 3).unwrap());
Gav Wood's avatar
Gav Wood committed
		// TODO: insert block that already includes one of them as an uncle to check it's not allowed.
	}

		keccak("").into()
	#[test]
	fn test_fork_transaction_addresses() {
		let t1 = Transaction {
			nonce: 0.into(),
			gas_price: 0.into(),
			gas: 100_000.into(),
			action: Action::Create,
			value: 100.into(),
			data: "601080600c6000396000f3006000355415600957005b60203560003555".from_hex().unwrap(),
		let t1_hash = t1.hash();
		let genesis = BlockBuilder::genesis();
		let b1a = genesis.add_block_with_transactions(iter::once(t1));
		let b1b = genesis.add_block_with_difficulty(9);
		let b2 = b1b.add_block();
		let b1a_hash = b1a.last().hash();
		let b2_hash = b2.last().hash();
		let bc = new_chain(&genesis.last().encoded(), db.clone());
		let mut batch = db.key_value().transaction();
		let _ = insert_block_batch(&mut batch, &bc, &b1a.last().encoded(), vec![]);
		let _ = insert_block_batch(&mut batch, &bc, &b1b.last().encoded(), vec![]);
		db.key_value().write(batch).unwrap();

		assert_eq!(bc.best_block_hash(), b1a_hash);
		assert_eq!(bc.transaction_address(&t1_hash), Some(TransactionAddress {
			block_hash: b1a_hash,
			index: 0,
		}));

		// now let's make forked chain the canon chain
		let mut batch = db.key_value().transaction();
		let _ = insert_block_batch(&mut batch, &bc, &b2.last().encoded(), vec![]);
		db.key_value().write(batch).unwrap();

		// Transaction should be retracted
		assert_eq!(bc.best_block_hash(), b2_hash);
		assert_eq!(bc.transaction_address(&t1_hash), None);
	}

	#[test]
	fn test_overwriting_transaction_addresses() {
		let t1 = Transaction {
			nonce: 0.into(),
			gas_price: 0.into(),
			gas: 100_000.into(),
			action: Action::Create,
			value: 100.into(),
			data: "601080600c6000396000f3006000355415600957005b60203560003555".from_hex().unwrap(),

		let t2 = Transaction {
			nonce: 1.into(),
			gas_price: 0.into(),
			gas: 100_000.into(),
			action: Action::Create,
			value: 100.into(),
			data: "601080600c6000396000f3006000355415600957005b60203560003555".from_hex().unwrap(),

		let t3 = Transaction {
			nonce: 2.into(),
			gas_price: 0.into(),
			gas: 100_000.into(),
			action: Action::Create,
			value: 100.into(),
			data: "601080600c6000396000f3006000355415600957005b60203560003555".from_hex().unwrap(),
		let genesis = BlockBuilder::genesis();
		let b1a = genesis.add_block_with_transactions(vec![t1.clone(), t2.clone()]);
		// insert transactions in different order,
		// the block has lower difficulty, so the hash is also different
		let b1b = genesis.add_block_with(|| BlockOptions {
			difficulty: 9.into(),
			transactions: vec![t2.clone(), t1.clone()],
			..Default::default()
		});
		let b2 = b1b.add_block_with_transactions(iter::once(t3.clone()));
		let b1a_hash = b1a.last().hash();
		let b1b_hash = b1b.last().hash();
		let b2_hash = b2.last().hash();

		let t1_hash = t1.hash();
		let t2_hash = t2.hash();
		let t3_hash = t3.hash();

		let bc = new_chain(&genesis.last().encoded(), db.clone());
		let mut batch = db.key_value().transaction();
		let _ = insert_block_batch(&mut batch, &bc, &b1a.last().encoded(), vec![]);
		let _ = insert_block_batch(&mut batch, &bc, &b1b.last().encoded(), vec![]);
		db.key_value().write(batch).unwrap();

		assert_eq!(bc.best_block_hash(), b1a_hash);
		assert_eq!(bc.transaction_address(&t1_hash), Some(TransactionAddress {
			block_hash: b1a_hash,
		}));
		assert_eq!(bc.transaction_address(&t2_hash), Some(TransactionAddress {
			block_hash: b1a_hash,

		// now let's make forked chain the canon chain
		let mut batch = db.key_value().transaction();
		let _ = insert_block_batch(&mut batch, &bc, &b2.last().encoded(), vec![]);
		db.key_value().write(batch).unwrap();

		assert_eq!(bc.best_block_hash(), b2_hash);
		assert_eq!(bc.transaction_address(&t1_hash), Some(TransactionAddress {
			block_hash: b1b_hash,
		}));
		assert_eq!(bc.transaction_address(&t2_hash), Some(TransactionAddress {
			block_hash: b1b_hash,
		}));
		assert_eq!(bc.transaction_address(&t3_hash), Some(TransactionAddress {
			block_hash: b2_hash,
Marek Kotewicz's avatar
Marek Kotewicz committed
	#[test]
	fn test_small_fork() {
		let genesis = BlockBuilder::genesis();
		let b1 = genesis.add_block();
		let b2 = b1.add_block();
		let b3a = b2.add_block();
		let b3b = b2.add_block_with_difficulty(9);

		let genesis_hash = genesis.last().hash();
		let b1_hash = b1.last().hash();
		let b2_hash = b2.last().hash();
		let b3a_hash = b3a.last().hash();
		let b3b_hash = b3b.last().hash();
Marek Kotewicz's avatar
Marek Kotewicz committed

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

		let bc = new_chain(&genesis.last().encoded(), db.clone());
		let mut batch = db.key_value().transaction();
		let ir1 = insert_block_batch(&mut batch, &bc, &b1.last().encoded(), vec![]);
		let ir2 = insert_block_batch(&mut batch, &bc, &b2.last().encoded(), vec![]);
		let ir3b = insert_block_batch(&mut batch, &bc, &b3b.last().encoded(), vec![]);
		db.key_value().write(batch).unwrap();
		assert_eq!(bc.block_hash(3).unwrap(), b3b_hash);
		let mut batch = db.key_value().transaction();
		let ir3a = insert_block_batch(&mut batch, &bc, &b3a.last().encoded(), vec![]);
		db.key_value().write(batch).unwrap();
Marek Kotewicz's avatar
Marek Kotewicz committed

		assert_eq!(ir1, ImportRoute {
			enacted: vec![b1_hash],
			retracted: vec![],
Gav Wood's avatar
Gav Wood committed
			omitted: vec![],
Marek Kotewicz's avatar
Marek Kotewicz committed
		});

		assert_eq!(ir2, ImportRoute {
			enacted: vec![b2_hash],
			retracted: vec![],
Gav Wood's avatar
Gav Wood committed
			omitted: vec![],
Marek Kotewicz's avatar
Marek Kotewicz committed
		});

		assert_eq!(ir3b, ImportRoute {
			enacted: vec![b3b_hash],
			retracted: vec![],
Gav Wood's avatar
Gav Wood committed
			omitted: vec![],
Marek Kotewicz's avatar
Marek Kotewicz committed
		});

		assert_eq!(ir3a, ImportRoute {
			enacted: vec![b3a_hash],
			retracted: vec![b3b_hash],
Gav Wood's avatar
Gav Wood committed
			omitted: vec![],
Marek Kotewicz's avatar
Marek Kotewicz committed
		});
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, b1_hash).unwrap();
Marek Kotewicz's avatar
Marek Kotewicz committed
		assert_eq!(r0_1.ancestor, genesis_hash);
		assert_eq!(r0_1.blocks, [b1_hash]);
Marek Kotewicz's avatar
Marek Kotewicz committed
		assert_eq!(r0_1.index, 0);

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

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

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

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

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

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

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

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

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

	#[test]
	fn test_reopen_blockchain_db() {
		let genesis = BlockBuilder::genesis();
		let first = genesis.add_block();
		let genesis_hash = genesis.last().hash();
		let first_hash = first.last().hash();

			let bc = new_chain(&genesis.last().encoded(), db.clone());
			assert_eq!(bc.best_block_hash(), genesis_hash);
			let mut batch = db.key_value().transaction();
			insert_block_batch(&mut batch, &bc, &first.last().encoded(), vec![]);
			db.key_value().write(batch).unwrap();
			assert_eq!(bc.best_block_hash(), first_hash);
			let bc = new_chain(&genesis.last().encoded(), db.clone());
			assert_eq!(bc.best_block_hash(), first_hash);
Nikolay Volf's avatar
Nikolay Volf committed

	#[test]
	fn can_contain_arbitrary_block_sequence() {
		let bc = generate_dummy_blockchain(50);
		assert_eq!(bc.best_block_number(), 49);
Nikolay Volf's avatar
Nikolay Volf committed
	}
		let bc = generate_dummy_blockchain(3000);
Nikolay Volf's avatar
Nikolay Volf committed

		assert_eq!(bc.best_block_number(), 2999);
		let best_hash = bc.best_block_hash();
		let mut block_header = bc.block_header_data(&best_hash);

		while !block_header.is_none() {
			block_header = bc.block_header_data(&block_header.unwrap().parent_hash());
		}
		assert!(bc.cache_size().blocks > 1024 * 1024);

		for _ in 0..2 {
Gav Wood's avatar
Gav Wood committed
			bc.collect_garbage();
		assert!(bc.cache_size().blocks < 1024 * 1024);
	}
Nikolay Volf's avatar
Nikolay Volf committed

	#[test]
	fn can_contain_arbitrary_block_sequence_with_extra() {
		let bc = generate_dummy_blockchain_with_extra(25);
Nikolay Volf's avatar
Nikolay Volf committed
		assert_eq!(bc.best_block_number(), 24);
	}

	#[test]
	fn can_contain_only_genesis_block() {
		let bc = generate_dummy_empty_blockchain();
Nikolay Volf's avatar
Nikolay Volf committed
		assert_eq!(bc.best_block_number(), 0);
	}

	#[test]
	fn find_transaction_by_hash() {
		let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0af81e09f8c46ca322193edfda764fa7e88e81923f802f1d325ec0b0308ac2cd0a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830200008083023e38808454c98c8142a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421880102030405060708c0c0".from_hex().unwrap();
		let b1 = "f904a8f901faa0ce1f26f798dd03c8782d63b3e42e79a64eaea5694ea686ac5d7ce3df5171d1aea01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0a65c2364cd0f1542d761823dc0109c6b072f14c20459598c5455c274601438f4a070616ebd7ad2ed6fb7860cf7e9df00163842351c38a87cac2c1cb193895035a2a05c5b4fc43c2d45787f54e1ae7d27afdb4ad16dfc567c5692070d5c4556e0b1d7b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830200000183023ec683021536845685109780a029f07836e4e59229b3a065913afc27702642c683bba689910b2b2fd45db310d3888957e6d004a31802f902a7f85f800a8255f094aaaf5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca0575da4e21b66fa764be5f74da9389e67693d066fb0d1312e19e17e501da00ecda06baf5a5327595f6619dfc2fcb3f2e6fb410b5810af3cb52d0e7508038e91a188f85f010a82520894bbbf5374fce5edbc8e2a8697c15331677e6ebf0b0a801ba04fa966bf34b93abc1bcd665554b7f316b50f928477b50be0f3285ead29d18c5ba017bba0eeec1625ab433746955e125d46d80b7fdc97386c51266f842d8e02192ef85f020a82520894bbbf5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca004377418ae981cc32b1312b4a427a1d69a821b28db8584f5f2bd8c6d42458adaa053a1dba1af177fac92f3b6af0a9fa46a22adf56e686c93794b6a012bf254abf5f85f030a82520894bbbf5374fce5edbc8e2a8697c15331677e6ebf0b0a801ca04fe13febd28a05f4fcb2f451d7ddc2dda56486d9f8c79a62b0ba4da775122615a0651b2382dd402df9ebc27f8cb4b2e0f3cea68dda2dca0ee9603608f0b6f51668f85f040a82520894bbbf5374fce5edbc8e2a8697c15331677e6ebf0b0a801ba078e6a0ba086a08f8450e208a399bb2f2d2a0d984acd2517c7c7df66ccfab567da013254002cd45a97fac049ae00afbc43ed0d9961d0c56a3b2382c80ce41c198ddf85f050a82520894bbbf5374fce5edbc8e2a8697c15331677e6ebf0b0a801ba0a7174d8f43ea71c8e3ca9477691add8d80ac8e0ed89d8d8b572041eef81f4a54a0534ea2e28ec4da3b5b944b18c51ec84a5cf35f5b3343c5fb86521fd2d388f506f85f060a82520894bbbf5374fce5edbc8e2a8697c15331677e6ebf0b0a801ba034bd04065833536a10c77ee2a43a5371bc6d34837088b861dd9d4b7f44074b59a078807715786a13876d3455716a6b9cb2186b7a4887a5c31160fc877454958616c0".from_hex().unwrap();
		let b1_hash: H256 = "f53f268d23a71e85c7d6d83a9504298712b84c1a2ba220441c86eeda0bf0b6e3".into();
keorn's avatar
keorn committed
		let bc = new_chain(&genesis, db.clone());
		let mut batch = db.key_value().transaction();
		insert_block_batch(&mut batch, &bc, &b1, vec![]);
		db.key_value().write(batch).unwrap();
		let transactions = bc.transactions(&b1_hash).unwrap();
		assert_eq!(transactions.len(), 7);
		for t in transactions {
			assert_eq!(bc.transaction(&bc.transaction_address(&t.hash()).unwrap()).unwrap(), t);
	#[test]
	fn test_logs() {
		let t1 = Transaction {
			nonce: 0.into(),
			gas_price: 0.into(),
			gas: 100_000.into(),
			action: Action::Create,
			value: 101.into(),