openethereum/src/blockchain.rs

532 lines
18 KiB
Rust
Raw Normal View History

2015-12-17 17:20:10 +01:00
//! Fast access to blockchain data.
2015-12-12 15:52:37 +01:00
use std::collections::HashMap;
2015-12-16 17:39:15 +01:00
use std::cell::{Cell, RefCell};
2015-12-13 22:39:01 +01:00
use std::path::Path;
use std::hash::Hash;
use rocksdb::{DB, WriteBatch, Writable};
2015-12-16 17:39:15 +01:00
use heapsize::HeapSizeOf;
2015-12-09 19:03:25 +01:00
use util::hash::*;
2015-12-13 17:33:11 +01:00
use util::uint::*;
2015-12-09 19:03:25 +01:00
use util::rlp::*;
use util::hashdb::*;
use util::sha3::*;
2015-12-14 14:15:27 +01:00
use util::bytes::*;
2015-12-16 17:39:15 +01:00
use util::squeeze::*;
2015-12-09 19:03:25 +01:00
use blockheader::*;
2015-12-13 17:33:11 +01:00
use extras::*;
2015-12-14 17:12:47 +01:00
use transaction::*;
2015-12-17 02:13:14 +01:00
use views::*;
2015-12-09 19:03:25 +01:00
2015-12-17 17:20:10 +01:00
/// Represents a tree route between `from` block and `to` block:
///
/// - `blocks` - a vector of hashes of all blocks, ordered from `from` to `to`.
///
/// - `ancestor` - best common ancestor of these blocks.
///
/// - `index` - an index where best common ancestor would be.
2015-12-17 15:11:42 +01:00
pub struct TreeRoute {
pub blocks: Vec<H256>,
pub ancestor: H256,
pub index: usize
}
2015-12-17 17:20:10 +01:00
/// Represents blockchain's in-memory cache size in bytes.
2015-12-16 17:39:15 +01:00
#[derive(Debug)]
pub struct CacheSize {
pub blocks: usize,
pub block_details: usize,
pub transaction_addresses: usize,
pub block_logs: usize,
pub blocks_blooms: usize
}
2015-12-17 17:20:10 +01:00
/// Structure providing fast access to blockchain data.
///
/// **Does not do input data verifycation.**
2015-12-09 19:03:25 +01:00
pub struct BlockChain {
2015-12-17 20:37:04 +01:00
best_block_hash: RefCell<H256>,
2015-12-17 15:11:42 +01:00
best_block_number: Cell<U256>,
best_block_total_difficulty: Cell<U256>,
2015-12-13 22:39:01 +01:00
2015-12-14 14:15:27 +01:00
// block cache
2015-12-16 17:39:15 +01:00
blocks: RefCell<HashMap<H256, Bytes>>,
2015-12-14 14:15:27 +01:00
// extra caches
2015-12-17 01:54:24 +01:00
block_details: RefCell<HashMap<H256, BlockDetails>>,
block_hashes: RefCell<HashMap<U256, H256>>,
transaction_addresses: RefCell<HashMap<H256, TransactionAddress>>,
block_logs: RefCell<HashMap<H256, BlockLogBlooms>>,
blocks_blooms: RefCell<HashMap<H256, BlocksBlooms>>,
2015-12-14 14:15:27 +01:00
extras_db: DB,
blocks_db: DB
2015-12-09 19:03:25 +01:00
}
impl BlockChain {
2015-12-12 15:52:37 +01:00
/// Create new instance of blockchain from given Genesis
///
/// ```rust
/// extern crate ethcore_util as util;
/// extern crate ethcore;
2015-12-13 22:39:01 +01:00
/// use std::env;
2015-12-12 15:52:37 +01:00
/// use std::str::FromStr;
/// use ethcore::genesis::*;
/// use ethcore::blockchain::*;
/// use util::hash::*;
2015-12-13 22:39:01 +01:00
/// use util::uint::*;
2015-12-12 15:52:37 +01:00
///
/// fn main() {
2015-12-17 15:11:42 +01:00
/// let genesis = Genesis::new_frontier();
///
2015-12-13 22:39:01 +01:00
/// let mut dir = env::temp_dir();
/// dir.push(H32::random().hex());
///
2015-12-17 17:20:10 +01:00
/// let bc = BlockChain::new(genesis.block(), &dir);
2015-12-17 15:11:42 +01:00
///
2015-12-12 15:52:37 +01:00
/// let genesis_hash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3";
2015-12-17 01:54:24 +01:00
/// assert_eq!(bc.genesis_hash(), H256::from_str(genesis_hash).unwrap());
/// assert!(bc.is_known(&bc.genesis_hash()));
/// assert_eq!(bc.genesis_hash(), bc.block_hash(&U256::from(0u8)).unwrap());
2015-12-12 15:52:37 +01:00
/// }
/// ```
2015-12-17 17:20:10 +01:00
pub fn new(genesis: &[u8], path: &Path) -> BlockChain {
2015-12-17 15:11:42 +01:00
// open extras db
2015-12-14 14:15:27 +01:00
let mut extras_path = path.to_path_buf();
extras_path.push("extras");
let extras_db = DB::open_default(extras_path.to_str().unwrap()).unwrap();
2015-12-17 15:11:42 +01:00
// open blocks db
2015-12-14 14:15:27 +01:00
let mut blocks_path = path.to_path_buf();
blocks_path.push("blocks");
let blocks_db = DB::open_default(blocks_path.to_str().unwrap()).unwrap();
2015-12-13 22:39:01 +01:00
2015-12-17 01:54:24 +01:00
let bc = BlockChain {
2015-12-17 20:37:04 +01:00
best_block_hash: RefCell::new(H256::new()),
2015-12-17 15:11:42 +01:00
best_block_number: Cell::new(U256::from(0u8)),
best_block_total_difficulty: Cell::new(U256::from(0u8)),
2015-12-16 17:39:15 +01:00
blocks: RefCell::new(HashMap::new()),
2015-12-17 01:54:24 +01:00
block_details: RefCell::new(HashMap::new()),
block_hashes: RefCell::new(HashMap::new()),
transaction_addresses: RefCell::new(HashMap::new()),
block_logs: RefCell::new(HashMap::new()),
blocks_blooms: RefCell::new(HashMap::new()),
2015-12-14 14:15:27 +01:00
extras_db: extras_db,
blocks_db: blocks_db
2015-12-17 01:54:24 +01:00
};
2015-12-17 15:11:42 +01:00
// 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
2015-12-17 17:20:10 +01:00
let block = BlockView::new(genesis);
2015-12-17 15:11:42 +01:00
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![]
};
2015-12-17 17:20:10 +01:00
bc.blocks_db.put(&hash, genesis).unwrap();
2015-12-17 15:11:42 +01:00
let batch = WriteBatch::new();
batch.put_extras(&hash, &details);
batch.put_extras(&header.number(), &hash);
batch.put(b"best", &hash).unwrap();
bc.extras_db.write(batch).unwrap();
hash
}
};
bc.best_block_number.set(bc.block_number(&best_block_hash).unwrap());
bc.best_block_total_difficulty.set(bc.block_details(&best_block_hash).unwrap().total_difficulty);
2015-12-17 20:37:04 +01:00
*bc.best_block_hash.borrow_mut() = best_block_hash;
2015-12-17 15:11:42 +01:00
2015-12-17 01:54:24 +01:00
bc
}
2015-12-17 15:11:42 +01:00
/// Returns a tree route between `from` and `to`, which is a tuple of:
2015-12-17 17:20:10 +01:00
///
2015-12-17 15:11:42 +01:00
/// - a vector of hashes of all blocks, ordered from `from` to `to`.
2015-12-17 17:20:10 +01:00
///
2015-12-17 15:11:42 +01:00
/// - common ancestor of these blocks.
2015-12-17 17:20:10 +01:00
///
2015-12-17 15:11:42 +01:00
/// - an index where best common ancestor would be
///
2015-12-17 17:20:10 +01:00
/// 1.) from newer to older
///
/// - bc: `A1 -> A2 -> A3 -> A4 -> A5`
/// - from: A5, to: A4
/// - route:
///
/// ```json
/// { blocks: [A5], ancestor: A4, index: 1 }
/// ```
2015-12-17 15:11:42 +01:00
///
2015-12-17 17:20:10 +01:00
/// 2.) from older to newer
2015-12-17 15:11:42 +01:00
///
2015-12-17 17:20:10 +01:00
/// - bc: `A1 -> A2 -> A3 -> A4 -> A5`
/// - from: A3, to: A4
/// - route:
2015-12-17 15:11:42 +01:00
///
2015-12-17 17:20:10 +01:00
/// ```json
/// { blocks: [A4], ancestor: A3, index: 0 }
/// ```
2015-12-17 15:11:42 +01:00
///
/// 3.) fork:
///
2015-12-17 17:20:10 +01:00
/// - bc:
///
/// ```text
/// A1 -> A2 -> A3 -> A4
2015-12-17 15:11:42 +01:00
/// -> B3 -> B4
2015-12-17 17:20:10 +01:00
/// ```
/// - from: B4, to: A4
/// - route:
///
/// ```json
/// { blocks: [B4, B3, A3, A4], ancestor: A2, index: 2 }
/// ```
2015-12-17 15:11:42 +01:00
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("from hash is invalid!");
let mut to_details = self.block_details(to).expect("to hash is invalid!");
let mut current_from = from.clone();
let mut current_to = to.clone();
// 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).unwrap();
}
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).unwrap();
}
assert_eq!(from_details.number, to_details.number);
// move to shared parent
while from_details.parent != to_details.parent {
from_branch.push(current_from);
current_from = from_details.parent.clone();
from_details = self.block_details(&from_details.parent).unwrap();
to_branch.push(current_to);
current_to = to_details.parent.clone();
to_details = self.block_details(&to_details.parent).unwrap();
}
let index = from_branch.len();
2015-12-17 20:37:04 +01:00
from_branch.extend(to_branch.into_iter().rev());
2015-12-17 15:11:42 +01:00
TreeRoute {
blocks: from_branch,
ancestor: from_details.parent,
index: index
}
}
2015-12-17 01:54:24 +01:00
/// 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]) {
2015-12-17 15:11:42 +01:00
// create views onto rlp
2015-12-17 01:54:24 +01:00
let block = BlockView::new(bytes);
let header = block.header_view();
if self.is_known(&header.sha3()) {
return;
}
2015-12-17 15:11:42 +01:00
// prepare variables
2015-12-17 01:54:24 +01:00
let hash = block.sha3();
2015-12-17 15:11:42 +01:00
let mut parent_details = self.block_details(&header.parent_hash()).expect("Invalid parent hash.");
let total_difficulty = parent_details.total_difficulty + header.difficulty();
2015-12-17 17:20:10 +01:00
let parent_hash = header.parent_hash();
2015-12-17 15:11:42 +01:00
// create current block details
let details = BlockDetails {
number: header.number(),
total_difficulty: total_difficulty,
2015-12-17 17:20:10 +01:00
parent: parent_hash.clone(),
2015-12-17 15:11:42 +01:00
children: vec![]
};
2015-12-17 01:54:24 +01:00
2015-12-17 15:11:42 +01:00
// store block in db
2015-12-17 01:54:24 +01:00
self.blocks_db.put(&hash, &bytes).unwrap();
2015-12-17 15:11:42 +01:00
// update extra details
{
// insert new block details
let batch = WriteBatch::new();
batch.put_extras(&hash, &details);
// update parent details
parent_details.children.push(hash.clone());
2015-12-17 17:20:10 +01:00
batch.put_extras(&parent_hash, &parent_details);
2015-12-17 15:11:42 +01:00
self.extras_db.write(batch).unwrap();
2015-12-17 17:20:10 +01:00
// also in cache if it's there...
let mut write = self.block_details.borrow_mut();
match write.get_mut(&parent_hash) {
Some(parent_details) => parent_details.children.push(hash.clone()),
None => ()
}
2015-12-17 15:11:42 +01:00
}
// check if we have new best block.
// if yes, it means that we need to move it and its ancestors
// to "canon chain"
if total_difficulty > self.best_block_total_difficulty() {
// find the route between old best block and the new one
let route = self.tree_route(&self.best_block_hash(), &hash);
2015-12-17 17:20:10 +01:00
let extras_batch = match route.blocks.len() {
// its our parent
1 => {
let extras = WriteBatch::new();
extras.put_extras(&header.number(), &hash);
extras
},
// it is a fork
i if i > 1 => {
let ancestor_number = self.block_number(&route.ancestor).unwrap();
let start_number = ancestor_number + U256::from(1u8);
route.blocks.iter()
.skip(route.index)
.enumerate()
.fold(WriteBatch::new(), | acc, (index, hash) | {
acc.put_extras(&(start_number + U256::from(index as u64)), hash);
acc
})
},
// route.len() could be 0 only if inserted block is best block,
// and this is not possible at this stage
_ => { unreachable!(); }
};
2015-12-17 15:11:42 +01:00
// update extras database
extras_batch.put(b"best", &hash).unwrap();
self.extras_db.write(extras_batch).unwrap();
// update local caches
2015-12-17 20:37:04 +01:00
*self.best_block_hash.borrow_mut() = hash;
2015-12-17 15:11:42 +01:00
self.best_block_number.set(header.number());
self.best_block_total_difficulty.set(total_difficulty);
}
}
/// Returns true if the given block is known
/// (though not necessarily a part of the canon chain).
pub fn is_known(&self, hash: &H256) -> bool {
2015-12-15 16:21:19 +01:00
self.query_extras_exist(hash, &self.block_details)
2015-12-13 22:39:01 +01:00
}
2015-12-14 17:12:47 +01:00
/// Returns true if transaction is known.
pub fn is_known_transaction(&self, hash: &H256) -> bool {
self.query_extras_exist(hash, &self.transaction_addresses)
}
2015-12-17 17:20:10 +01:00
/// Returns reference to genesis hash.
2015-12-17 01:54:24 +01:00
pub fn genesis_hash(&self) -> H256 {
self.block_hash(&U256::from(0u8)).expect("Genesis hash should always exist")
}
2015-12-17 17:20:10 +01:00
/// Get the partial-header of a block.
pub fn block_header(&self, hash: &H256) -> Option<Header> {
2015-12-14 17:12:47 +01:00
self.block(hash).map(|bytes| BlockView::new(&bytes).header())
}
/// Get a list of transactions for a given block.
/// Returns None is block deos not exist.
pub fn transactions(&self, hash: &H256) -> Option<Vec<Transaction>> {
self.block(hash).map(|bytes| BlockView::new(&bytes).transactions())
}
/// Get a list of transaction hashes for a given block.
/// Returns None if block does not exist.
pub fn transaction_hashes(&self, hash: &H256) -> Option<Vec<H256>> {
2015-12-14 17:12:47 +01:00
self.block(hash).map(|bytes| BlockView::new(&bytes).transaction_hashes())
}
/// Get a list of uncles for a given block.
/// Returns None is block deos not exist.
pub 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.
pub fn uncle_hashes(&self, hash: &H256) -> Option<Vec<H256>> {
2015-12-14 17:12:47 +01:00
self.block(hash).map(|bytes| BlockView::new(&bytes).uncle_hashes())
}
/// Get the familial details concerning a block.
pub fn block_details(&self, hash: &H256) -> Option<BlockDetails> {
self.query_extras(hash, &self.block_details)
}
2015-12-17 17:20:10 +01:00
/// Get the hash of given block's number.
2015-12-16 17:39:15 +01:00
pub fn block_hash(&self, hash: &U256) -> Option<H256> {
2015-12-14 17:12:47 +01:00
self.query_extras(hash, &self.block_hashes)
}
2015-12-17 17:20:10 +01:00
/// Get best block hash.
2015-12-17 15:11:42 +01:00
pub fn best_block_hash(&self) -> H256 {
2015-12-17 20:37:04 +01:00
self.best_block_hash.borrow().clone()
2015-12-17 15:11:42 +01:00
}
2015-12-17 17:20:10 +01:00
/// Get best block number.
2015-12-17 15:11:42 +01:00
pub fn best_block_number(&self) -> U256 {
self.best_block_number.get()
}
2015-12-17 17:20:10 +01:00
/// Get best block total difficulty.
2015-12-17 15:11:42 +01:00
pub fn best_block_total_difficulty(&self) -> U256 {
self.best_block_total_difficulty.get()
2015-12-16 17:39:15 +01:00
}
2015-12-17 17:20:10 +01:00
/// Get the number of given block's hash.
2015-12-16 17:39:15 +01:00
pub fn block_number(&self, hash: &H256) -> Option<U256> {
self.block(hash).map(|bytes| BlockView::new(&bytes).header_view().number())
}
2015-12-17 17:20:10 +01:00
/// Get the transactions' log blooms of a block.
pub fn log_blooms(&self, hash: &H256) -> Option<BlockLogBlooms> {
self.query_extras(hash, &self.block_logs)
}
fn block(&self, hash: &H256) -> Option<Bytes> {
2015-12-14 14:15:27 +01:00
{
2015-12-16 17:39:15 +01:00
let read = self.blocks.borrow();
2015-12-14 14:15:27 +01:00
match read.get(hash) {
Some(v) => return Some(v.clone()),
None => ()
}
}
let opt = self.blocks_db.get(hash)
.expect("Low level database error. Some issue with disk?");
match opt {
Some(b) => {
let bytes: Bytes = b.to_vec();
2015-12-16 17:39:15 +01:00
let mut write = self.blocks.borrow_mut();
2015-12-14 14:15:27 +01:00
write.insert(hash.clone(), bytes.clone());
Some(bytes)
},
None => None
}
}
2015-12-17 01:54:24 +01:00
fn query_extras<K, T>(&self, hash: &K, cache: &RefCell<HashMap<K, T>>) -> Option<T> where
T: Clone + Decodable + ExtrasIndexable,
2015-12-13 22:39:01 +01:00
K: ExtrasSliceConvertable + Eq + Hash + Clone {
{
2015-12-16 17:39:15 +01:00
let read = cache.borrow();
2015-12-13 22:39:01 +01:00
match read.get(hash) {
Some(v) => return Some(v.clone()),
None => ()
}
}
2015-12-17 01:54:24 +01:00
self.extras_db.get_extras(hash).map(| t: T | {
let mut write = cache.borrow_mut();
write.insert(hash.clone(), t.clone());
t
})
2015-12-13 22:39:01 +01:00
}
2015-12-17 01:54:24 +01:00
fn query_extras_exist<K, T>(&self, hash: &K, cache: &RefCell<HashMap<K, T>>) -> bool where
K: ExtrasSliceConvertable + Eq + Hash + Clone,
T: ExtrasIndexable {
2015-12-13 22:39:01 +01:00
{
2015-12-16 17:39:15 +01:00
let read = cache.borrow();
2015-12-13 22:39:01 +01:00
match read.get(hash) {
Some(_) => return true,
None => ()
}
}
2015-12-17 01:54:24 +01:00
self.extras_db.extras_exists::<_, T>(hash)
2015-12-09 19:03:25 +01:00
}
2015-12-16 17:39:15 +01:00
2015-12-17 17:20:10 +01:00
/// Get current cache size.
2015-12-16 17:39:15 +01:00
pub fn cache_size(&self) -> CacheSize {
CacheSize {
blocks: self.blocks.heap_size_of_children(),
block_details: self.block_details.heap_size_of_children(),
transaction_addresses: self.transaction_addresses.heap_size_of_children(),
block_logs: self.block_logs.heap_size_of_children(),
blocks_blooms: self.blocks_blooms.heap_size_of_children()
}
}
2015-12-17 17:20:10 +01:00
/// Tries to squeeze the cache if its too big.
2015-12-16 17:39:15 +01:00
pub fn squeeze_to_fit(&self, size: CacheSize) {
self.blocks.borrow_mut().squeeze(size.blocks);
self.block_details.borrow_mut().squeeze(size.block_details);
self.transaction_addresses.borrow_mut().squeeze(size.transaction_addresses);
self.block_logs.borrow_mut().squeeze(size.block_logs);
self.blocks_blooms.borrow_mut().squeeze(size.blocks_blooms);
}
2015-12-09 19:03:25 +01:00
}
2015-12-13 22:39:01 +01:00
2015-12-17 17:20:10 +01:00
#[cfg(test)]
mod tests {
use std::env;
use std::str::FromStr;
use rustc_serialize::hex::FromHex;
use util::hash::*;
use util::uint::*;
use blockchain::*;
#[test]
fn valid_tests_extra32() {
let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0925002c3260b44e44c3edebad1cc442142b03020209df1ab8bb86752edbd2cd7a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a0363659b251bf8b819179874c8cce7b9b983d7f3704cbb58a3b334431f7032871889032d09c281e1236c0c0".from_hex().unwrap();
let mut dir = env::temp_dir();
dir.push(H32::random().hex());
let bc = BlockChain::new(&genesis, &dir);
let genesis_hash = H256::from_str("3caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942").unwrap();
2015-12-17 20:37:04 +01:00
assert_eq!(bc.genesis_hash(), genesis_hash.clone());
2015-12-17 17:20:10 +01:00
assert_eq!(bc.best_block_number(), U256::from(0u8));
2015-12-17 20:37:04 +01:00
assert_eq!(bc.best_block_hash(), genesis_hash.clone());
assert_eq!(bc.block_hash(&U256::from(0u8)), Some(genesis_hash.clone()));
2015-12-17 17:20:10 +01:00
assert_eq!(bc.block_hash(&U256::from(1u8)), None);
let first = "f90285f90219a03caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0bac6177a79e910c98d86ec31a09ae37ac2de15b754fd7bed1ba52362c49416bfa0d45893a296c1490a978e0bd321b5f2635d8280365c1fe9f693d65f233e791344a0c7778a7376099ee2e5c455791c1885b5c361b95713fddcbe32d97fd01334d296b90100000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000400000000000000000000000000000000000000000000000000000008302000001832fefd882560b845627cb99a00102030405060708091011121314151617181920212223242526272829303132a08ccb2837fb2923bd97e8f2d08ea32012d6e34be018c73e49a0f98843e8f47d5d88e53be49fec01012ef866f864800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d8785012a05f200801ba0cb088b8d2ff76a7b2c6616c9d02fb6b7a501afbf8b69d7180b09928a1b80b5e4a06448fe7476c606582039bb72a9f6f4b4fad18507b8dfbd00eebbe151cc573cd2c0".from_hex().unwrap();
bc.insert_block(&first);
let first_hash = H256::from_str("a940e5af7d146b3b917c953a82e1966b906dace3a4e355b5b0a4560190357ea1").unwrap();
2015-12-17 20:37:04 +01:00
assert_eq!(bc.block_hash(&U256::from(0u8)), Some(genesis_hash.clone()));
2015-12-17 17:20:10 +01:00
assert_eq!(bc.best_block_number(), U256::from(1u8));
2015-12-17 20:37:04 +01:00
assert_eq!(bc.best_block_hash(), first_hash.clone());
assert_eq!(bc.block_hash(&U256::from(1u8)), 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()]);
2015-12-17 17:20:10 +01:00
assert_eq!(bc.block_hash(&U256::from(2u8)), None);
}
}