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-21 02:57:02 +01:00
|
|
|
use std::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::*;
|
2015-12-11 03:51:23 +01:00
|
|
|
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-21 02:34:41 +01:00
|
|
|
use header::*;
|
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:
|
2015-12-26 15:47:07 +01:00
|
|
|
///
|
2015-12-17 17:20:10 +01:00
|
|
|
/// - `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-21 16:53:07 +01:00
|
|
|
/// Information about best block gathered together
|
2015-12-21 02:57:02 +01:00
|
|
|
struct BestBlock {
|
|
|
|
pub hash: H256,
|
|
|
|
pub number: U256,
|
|
|
|
pub total_difficulty: U256
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BestBlock {
|
|
|
|
fn new() -> BestBlock {
|
|
|
|
BestBlock {
|
|
|
|
hash: H256::new(),
|
|
|
|
number: U256::from(0),
|
|
|
|
total_difficulty: U256::from(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 17:20:10 +01:00
|
|
|
/// Structure providing fast access to blockchain data.
|
2015-12-26 15:47:07 +01:00
|
|
|
///
|
2015-12-21 15:25:58 +01:00
|
|
|
/// **Does not do input data verification.**
|
2015-12-09 19:03:25 +01:00
|
|
|
pub struct BlockChain {
|
2015-12-21 02:57:02 +01:00
|
|
|
best_block: RefCell<BestBlock>,
|
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 13:32:22 +01:00
|
|
|
|
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
|
2015-12-26 15:47:07 +01:00
|
|
|
///
|
2015-12-12 15:52:37 +01:00
|
|
|
/// ```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-26 15:47:07 +01:00
|
|
|
///
|
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-21 02:57:02 +01:00
|
|
|
best_block: RefCell::new(BestBlock::new()),
|
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();
|
2015-12-26 15:47:07 +01:00
|
|
|
|
2015-12-17 15:11:42 +01:00
|
|
|
hash
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-21 02:57:02 +01:00
|
|
|
{
|
|
|
|
let mut best_block = bc.best_block.borrow_mut();
|
|
|
|
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;
|
|
|
|
}
|
2015-12-17 15:11:42 +01:00
|
|
|
|
2015-12-17 01:54:24 +01:00
|
|
|
bc
|
2015-12-11 03:51:23 +01:00
|
|
|
}
|
|
|
|
|
2015-12-17 15:11:42 +01:00
|
|
|
/// Returns a tree route between `from` and `to`, which is a tuple of:
|
2015-12-26 15:47:07 +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-26 15:47:07 +01:00
|
|
|
///
|
2015-12-17 17:20:10 +01:00
|
|
|
/// 1.) from newer to older
|
2015-12-26 15:47:07 +01:00
|
|
|
///
|
2015-12-17 17:20:10 +01:00
|
|
|
/// - bc: `A1 -> A2 -> A3 -> A4 -> A5`
|
|
|
|
/// - from: A5, to: A4
|
2015-12-26 15:47:07 +01:00
|
|
|
/// - route:
|
2015-12-17 17:20:10 +01:00
|
|
|
///
|
|
|
|
/// ```json
|
|
|
|
/// { blocks: [A5], ancestor: A4, index: 1 }
|
|
|
|
/// ```
|
2015-12-26 15:47:07 +01:00
|
|
|
///
|
2015-12-17 17:20:10 +01:00
|
|
|
/// 2.) from older to newer
|
2015-12-26 15:47:07 +01:00
|
|
|
///
|
2015-12-17 17:20:10 +01:00
|
|
|
/// - bc: `A1 -> A2 -> A3 -> A4 -> A5`
|
|
|
|
/// - from: A3, to: A4
|
2015-12-26 15:47:07 +01:00
|
|
|
/// - route:
|
|
|
|
///
|
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-26 15:47:07 +01:00
|
|
|
/// - bc:
|
2015-12-17 17:20:10 +01:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// A1 -> A2 -> A3 -> A4
|
2015-12-17 15:11:42 +01:00
|
|
|
/// -> B3 -> B4
|
2015-12-26 15:47:07 +01:00
|
|
|
/// ```
|
2015-12-17 17:20:10 +01:00
|
|
|
/// - from: B4, to: A4
|
2015-12-26 15:47:07 +01:00
|
|
|
/// - route:
|
|
|
|
///
|
2015-12-17 17:20:10 +01:00
|
|
|
/// ```json
|
|
|
|
/// { blocks: [B4, B3, A3, A4], ancestor: A2, index: 2 }
|
|
|
|
/// ```
|
2015-12-21 14:23:10 +01:00
|
|
|
pub fn tree_route(&self, from: H256, to: H256) -> TreeRoute {
|
|
|
|
let from_details = self.block_details(&from).expect("from hash is invalid!");
|
|
|
|
let to_details = self.block_details(&to).expect("to hash is invalid!");
|
|
|
|
|
|
|
|
self._tree_route((from_details, from), (to_details, to))
|
|
|
|
}
|
|
|
|
|
2015-12-21 16:57:28 +01:00
|
|
|
/// Similar to `tree_route` function, but can be used to return a route
|
2015-12-21 16:49:31 +01:00
|
|
|
/// between blocks which may not be in database yet.
|
2015-12-21 14:23:10 +01:00
|
|
|
fn _tree_route(&self, from: (BlockDetails, H256), to: (BlockDetails, H256)) -> TreeRoute {
|
2015-12-17 15:11:42 +01:00
|
|
|
let mut from_branch = vec![];
|
|
|
|
let mut to_branch = vec![];
|
|
|
|
|
2015-12-21 14:23:10 +01:00
|
|
|
let mut from_details = from.0;
|
|
|
|
let mut to_details = to.0;
|
|
|
|
let mut current_from = from.1;
|
|
|
|
let mut current_to = to.1;
|
2015-12-17 15:11:42 +01:00
|
|
|
|
|
|
|
// 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
|
2015-12-18 11:34:55 +01:00
|
|
|
while current_from != current_to {
|
2015-12-17 15:11:42 +01:00
|
|
|
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,
|
2015-12-21 16:31:51 +01:00
|
|
|
ancestor: current_from,
|
2015-12-17 15:11:42 +01:00
|
|
|
index: index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 14:23:10 +01:00
|
|
|
|
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();
|
2015-12-21 15:22:24 +01:00
|
|
|
let hash = block.sha3();
|
2015-12-17 01:54:24 +01:00
|
|
|
|
2015-12-21 15:22:24 +01:00
|
|
|
if self.is_known(&hash) {
|
2015-12-17 01:54:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-21 15:22:24 +01:00
|
|
|
// store block in db
|
|
|
|
self.blocks_db.put(&hash, &bytes).unwrap();
|
|
|
|
let (batch, new_best) = self.block_to_extras_insert_batch(bytes);
|
|
|
|
|
|
|
|
// update best block
|
|
|
|
let mut best_block = self.best_block.borrow_mut();
|
|
|
|
if let Some(b) = new_best {
|
|
|
|
*best_block = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update caches
|
|
|
|
let mut write = self.block_details.borrow_mut();
|
|
|
|
write.remove(&header.parent_hash());
|
|
|
|
|
|
|
|
// update extras database
|
|
|
|
self.extras_db.write(batch).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transforms block into WriteBatch that may be written into database
|
|
|
|
/// Additionally, if it's new best block it returns new best block object.
|
|
|
|
fn block_to_extras_insert_batch(&self, bytes: &[u8]) -> (WriteBatch, Option<BestBlock>) {
|
|
|
|
// create views onto rlp
|
|
|
|
let block = BlockView::new(bytes);
|
|
|
|
let header = block.header_view();
|
2015-12-26 15:47:07 +01:00
|
|
|
|
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-21 14:23:10 +01:00
|
|
|
let is_new_best = total_difficulty > self.best_block_total_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-26 15:47:07 +01:00
|
|
|
|
2015-12-21 16:49:31 +01:00
|
|
|
// prepare the batch
|
2015-12-21 15:22:24 +01:00
|
|
|
let batch = WriteBatch::new();
|
|
|
|
|
|
|
|
// insert new block details
|
|
|
|
batch.put_extras(&hash, &details);
|
|
|
|
|
|
|
|
// update parent details
|
|
|
|
parent_details.children.push(hash.clone());
|
|
|
|
batch.put_extras(&parent_hash, &parent_details);
|
2015-12-17 15:11:42 +01:00
|
|
|
|
2015-12-21 15:22:24 +01:00
|
|
|
// if it's not new best block, just return
|
|
|
|
if !is_new_best {
|
|
|
|
return (batch, None);
|
2015-12-17 15:11:42 +01:00
|
|
|
}
|
|
|
|
|
2015-12-26 15:47:07 +01:00
|
|
|
// if its new best block we need to make sure that all ancestors
|
2015-12-21 14:23:10 +01:00
|
|
|
// are moved to "canon chain"
|
2015-12-21 15:22:24 +01:00
|
|
|
// find the route between old best block and the new one
|
|
|
|
let best_hash = self.best_block_hash();
|
|
|
|
let best_details = self.block_details(&best_hash).expect("best block hash is invalid!");
|
|
|
|
let route = self._tree_route((best_details, best_hash), (details, hash.clone()));
|
|
|
|
|
|
|
|
match route.blocks.len() {
|
|
|
|
// its our parent
|
|
|
|
1 => batch.put_extras(&header.number(), &hash),
|
|
|
|
// 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);
|
|
|
|
for (index, hash) in route.blocks.iter().skip(route.index).enumerate() {
|
|
|
|
batch.put_extras(&(start_number + U256::from(index as u64)), hash);
|
|
|
|
}
|
|
|
|
},
|
2015-12-21 16:53:07 +01:00
|
|
|
// route.blocks.len() could be 0 only if inserted block is best block,
|
2015-12-21 15:22:24 +01:00
|
|
|
// and this is not possible at this stage
|
|
|
|
_ => { unreachable!(); }
|
|
|
|
};
|
2015-12-17 15:11:42 +01:00
|
|
|
|
2015-12-21 16:49:31 +01:00
|
|
|
// this is new best block
|
2015-12-21 15:22:24 +01:00
|
|
|
batch.put(b"best", &hash).unwrap();
|
2015-12-21 14:23:10 +01:00
|
|
|
|
2015-12-21 15:22:24 +01:00
|
|
|
let best_block = BestBlock {
|
|
|
|
hash: hash,
|
|
|
|
number: header.number(),
|
|
|
|
total_difficulty: total_difficulty
|
|
|
|
};
|
2015-12-21 14:23:10 +01:00
|
|
|
|
2015-12-21 15:22:24 +01:00
|
|
|
(batch, Some(best_block))
|
2015-12-11 03:51:23 +01:00
|
|
|
}
|
|
|
|
|
2015-12-26 15:47:07 +01:00
|
|
|
/// Returns true if the given block is known
|
2015-12-11 03:51:23 +01:00
|
|
|
/// (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-14 16:14:50 +01:00
|
|
|
}
|
|
|
|
|
2015-12-17 17:20:10 +01:00
|
|
|
/// Get the partial-header of a block.
|
2015-12-14 16:14:50 +01:00
|
|
|
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.
|
2015-12-21 15:25:58 +01:00
|
|
|
/// Returns None if block deos not exist.
|
2015-12-14 17:12:47 +01:00
|
|
|
pub fn transactions(&self, hash: &H256) -> Option<Vec<Transaction>> {
|
|
|
|
self.block(hash).map(|bytes| BlockView::new(&bytes).transactions())
|
2015-12-14 16:14:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2015-12-21 15:25:58 +01:00
|
|
|
/// Returns None if block deos not exist.
|
2015-12-14 17:12:47 +01:00
|
|
|
pub fn uncles(&self, hash: &H256) -> Option<Vec<Header>> {
|
|
|
|
self.block(hash).map(|bytes| BlockView::new(&bytes).uncles())
|
2015-12-14 16:14:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-14 16:14:50 +01:00
|
|
|
}
|
|
|
|
|
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-21 02:57:02 +01:00
|
|
|
self.best_block.borrow().hash.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 {
|
2015-12-21 02:57:02 +01:00
|
|
|
self.best_block.borrow().number
|
2015-12-17 15:11:42 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2015-12-21 02:57:02 +01:00
|
|
|
self.best_block.borrow().total_difficulty
|
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.
|
2015-12-14 16:14:50 +01:00
|
|
|
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-26 15:47:07 +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-26 15:47:07 +01:00
|
|
|
fn query_extras_exist<K, T>(&self, hash: &K, cache: &RefCell<HashMap<K, T>>) -> bool where
|
2015-12-17 01:54:24 +01:00
|
|
|
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);
|
2015-12-26 15:47:07 +01:00
|
|
|
|
2015-12-17 17:20:10 +01:00
|
|
|
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);
|
2015-12-26 15:47:07 +01:00
|
|
|
|
2015-12-17 17:20:10 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-12-21 16:31:51 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_small_fork() {
|
|
|
|
let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a07dba07d6b448a186e9612e5f737d1c909dce473e53199901a302c00646d523c1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a059262c330941f3fe2a34d16d6e3c7b30d2ceb37c6a0e9a994c494ee1a61d2410885aa4c8bf8e56e264c0c0".from_hex().unwrap();
|
|
|
|
let b1 = "f90261f901f9a05716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a0e78628dd45a1f8dc495594d83b76c588a3ee67463260f8b7d4a42f574aeab29aa0e9244cf7503b79c03d3a099e07a80d2dbc77bb0b502d8a89d51ac0d68dd31313b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884562791e580a051b3ecba4e3f2b49c11d42dd0851ec514b1be3138080f72a2b6e83868275d98f8877671f479c414b47f862f86080018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca09e2709d7ec9bbe6b1bbbf0b2088828d14cd5e8642a1fee22dc74bfa89761a7f9a04bd8813dee4be989accdb708b1c2e325a7e9c695a8024e30e89d6c644e424747c0".from_hex().unwrap();
|
|
|
|
let b2 = "f902ccf901f9a0437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0c70a5dc56146e5ef025e4e5726a6373c6f12fd2f6784093a19ead0a7d17fb292a040645cbce4fd399e7bb9160b4c30c40d7ee616a030d4e18ef0ed3b02bdb65911a086e608555f63628417032a011d107b36427af37d153f0da02ce3f90fdd5e8c08b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302004002832fefd882c0e384562791e880a0e3cc39ff775cc0a32f175995b92e84b729e5c9a3563ff899e3555b908bc21d75887c3cde283f4846a6f8cdf8cb01018304cb2f8080b87e6060604052606e8060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c0406226146037576035565b005b60406004506056565b6040518082815260200191505060405180910390f35b6000600560006000508190555060059050606b565b90561ba05258615c63503c0a600d6994b12ea5750d45b3c69668e2a371b4fbfb9eeff6b8a0a11be762bc90491231274a2945be35a43f23c27775b1ff24dd521702fe15f73ec0".from_hex().unwrap();
|
|
|
|
let b3a = "f90261f901f9a036fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a09dc4b1357c0b7b8108f8a098f4f9a1a274957bc9ebc22a9ae67ae81739e5b19ca007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefd882524d84562791eb80a074861666bd346c025889745c793b91ab9cd1e2ca19b5cf3c50d04d135b0a4d2b8809fe9587ea4cdc04f862f86002018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d01801ba06fd84874d36d5de9e8e48978c03619b53a96b7ae0a4cd1ac118f103098b44801a00572596974dd7df4f9f69bd7456585618c568d8434ef6453391b89281ce12ae1c0".from_hex().unwrap();
|
|
|
|
let b3b = "f90265f901f9a036fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ab87dc338bfd6f662b1cd90bc0c9e40a1b2146a095312393c9e13ce3a5008b09a0e609b7a7d4b8a2403ec1268627ecd98783627246e8f1b26addb3ff504f76a054a0592fabf92476512952db3a69a2481a42912e668a1ee28c4c322e703bb665f8beb90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefd882a1f084562791ee80a0fe7098fa7e4ac5d637eea81fb23f8f78346826dbab430068dd9a249d0afa99818853e1a6b201ae3545f866f86402018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d0284c04062261ca06edc9ce8e7da4cc34067beb325dcad59e5655a164a5100a50bc3eb681b12c716a0abf9053d5de65b1be81fe50d327b84de685efbeecea34e7b747180a6c6023e44c0".from_hex().unwrap();
|
|
|
|
|
|
|
|
let genesis_hash = H256::from_str("5716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2").unwrap();
|
|
|
|
let b1_hash = H256::from_str("437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4f").unwrap();
|
|
|
|
let b2_hash = H256::from_str("36fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781a").unwrap();
|
|
|
|
let b3a_hash = H256::from_str("c208f88c9f5bf7e00840439742c12e5226d9752981f3ec0521bdcb6dd08af277").unwrap();
|
|
|
|
let b3b_hash = H256::from_str("bf72270ae0d95c9ea39a6adab994793fddb8c10fba7391e26279474124605d54").unwrap();
|
|
|
|
|
|
|
|
// b3a is a part of canon chain, whereas b3b is part of sidechain
|
|
|
|
let best_block_hash = H256::from_str("c208f88c9f5bf7e00840439742c12e5226d9752981f3ec0521bdcb6dd08af277").unwrap();
|
|
|
|
|
|
|
|
let mut dir = env::temp_dir();
|
|
|
|
dir.push(H32::random().hex());
|
|
|
|
|
|
|
|
let bc = BlockChain::new(&genesis, &dir);
|
|
|
|
bc.insert_block(&b1);
|
|
|
|
bc.insert_block(&b2);
|
|
|
|
bc.insert_block(&b3a);
|
|
|
|
bc.insert_block(&b3b);
|
|
|
|
|
|
|
|
assert_eq!(bc.best_block_hash(), best_block_hash);
|
|
|
|
assert_eq!(bc.block_number(&genesis_hash).unwrap(), U256::from(0));
|
|
|
|
assert_eq!(bc.block_number(&b1_hash).unwrap(), U256::from(1));
|
|
|
|
assert_eq!(bc.block_number(&b2_hash).unwrap(), U256::from(2));
|
|
|
|
assert_eq!(bc.block_number(&b3a_hash).unwrap(), U256::from(3));
|
|
|
|
assert_eq!(bc.block_number(&b3b_hash).unwrap(), U256::from(3));
|
|
|
|
|
|
|
|
assert_eq!(bc.block_hash(&U256::from(0)).unwrap(), genesis_hash);
|
|
|
|
assert_eq!(bc.block_hash(&U256::from(1)).unwrap(), b1_hash);
|
|
|
|
assert_eq!(bc.block_hash(&U256::from(2)).unwrap(), b2_hash);
|
|
|
|
assert_eq!(bc.block_hash(&U256::from(3)).unwrap(), b3a_hash);
|
|
|
|
|
|
|
|
// test trie route
|
|
|
|
let r0_1 = bc.tree_route(genesis_hash.clone(), b1_hash.clone());
|
|
|
|
assert_eq!(r0_1.ancestor, genesis_hash);
|
|
|
|
assert_eq!(r0_1.blocks, [b1_hash.clone()]);
|
|
|
|
assert_eq!(r0_1.index, 0);
|
|
|
|
|
2015-12-26 15:47:07 +01:00
|
|
|
let r0_2 = bc.tree_route(genesis_hash.clone(), b2_hash.clone());
|
2015-12-21 16:31:51 +01:00
|
|
|
assert_eq!(r0_2.ancestor, genesis_hash);
|
|
|
|
assert_eq!(r0_2.blocks, [b1_hash.clone(), b2_hash.clone()]);
|
|
|
|
assert_eq!(r0_2.index, 0);
|
|
|
|
|
2015-12-26 15:47:07 +01:00
|
|
|
let r1_3a = bc.tree_route(b1_hash.clone(), b3a_hash.clone());
|
2015-12-21 16:31:51 +01:00
|
|
|
assert_eq!(r1_3a.ancestor, b1_hash);
|
|
|
|
assert_eq!(r1_3a.blocks, [b2_hash.clone(), b3a_hash.clone()]);
|
|
|
|
assert_eq!(r1_3a.index, 0);
|
|
|
|
|
2015-12-26 15:47:07 +01:00
|
|
|
let r1_3b = bc.tree_route(b1_hash.clone(), b3b_hash.clone());
|
2015-12-21 16:31:51 +01:00
|
|
|
assert_eq!(r1_3b.ancestor, b1_hash);
|
|
|
|
assert_eq!(r1_3b.blocks, [b2_hash.clone(), b3b_hash.clone()]);
|
|
|
|
assert_eq!(r1_3b.index, 0);
|
|
|
|
|
2015-12-26 15:47:07 +01:00
|
|
|
let r3a_3b = bc.tree_route(b3a_hash.clone(), b3b_hash.clone());
|
2015-12-21 16:31:51 +01:00
|
|
|
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());
|
|
|
|
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());
|
|
|
|
assert_eq!(r2_0.ancestor, genesis_hash);
|
|
|
|
assert_eq!(r2_0.blocks, [b2_hash.clone(), b1_hash.clone()]);
|
|
|
|
assert_eq!(r2_0.index, 2);
|
2015-12-26 15:47:07 +01:00
|
|
|
|
2015-12-21 16:31:51 +01:00
|
|
|
let r3a_1 = bc.tree_route(b3a_hash.clone(), b1_hash.clone());
|
|
|
|
assert_eq!(r3a_1.ancestor, b1_hash);
|
|
|
|
assert_eq!(r3a_1.blocks, [b3a_hash.clone(), b2_hash.clone()]);
|
|
|
|
assert_eq!(r3a_1.index, 2);
|
|
|
|
|
|
|
|
let r3b_1 = bc.tree_route(b3b_hash.clone(), b1_hash.clone());
|
|
|
|
assert_eq!(r3b_1.ancestor, b1_hash);
|
|
|
|
assert_eq!(r3b_1.blocks, [b3b_hash.clone(), b2_hash.clone()]);
|
|
|
|
assert_eq!(r3b_1.index, 2);
|
|
|
|
|
|
|
|
let r3b_3a = bc.tree_route(b3b_hash.clone(), b3a_hash.clone());
|
|
|
|
assert_eq!(r3b_3a.ancestor, b2_hash);
|
|
|
|
assert_eq!(r3b_3a.blocks, [b3b_hash.clone(), b3a_hash.clone()]);
|
|
|
|
assert_eq!(r3b_3a.index, 1);
|
|
|
|
}
|
2015-12-21 16:38:31 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reopen_blockchain_db() {
|
|
|
|
let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a07dba07d6b448a186e9612e5f737d1c909dce473e53199901a302c00646d523c1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a059262c330941f3fe2a34d16d6e3c7b30d2ceb37c6a0e9a994c494ee1a61d2410885aa4c8bf8e56e264c0c0".from_hex().unwrap();
|
|
|
|
let b1 = "f90261f901f9a05716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a0e78628dd45a1f8dc495594d83b76c588a3ee67463260f8b7d4a42f574aeab29aa0e9244cf7503b79c03d3a099e07a80d2dbc77bb0b502d8a89d51ac0d68dd31313b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884562791e580a051b3ecba4e3f2b49c11d42dd0851ec514b1be3138080f72a2b6e83868275d98f8877671f479c414b47f862f86080018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca09e2709d7ec9bbe6b1bbbf0b2088828d14cd5e8642a1fee22dc74bfa89761a7f9a04bd8813dee4be989accdb708b1c2e325a7e9c695a8024e30e89d6c644e424747c0".from_hex().unwrap();
|
|
|
|
let genesis_hash = H256::from_str("5716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2").unwrap();
|
|
|
|
let b1_hash = H256::from_str("437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4f").unwrap();
|
|
|
|
|
|
|
|
let mut dir = env::temp_dir();
|
|
|
|
dir.push(H32::random().hex());
|
|
|
|
|
|
|
|
{
|
|
|
|
let bc = BlockChain::new(&genesis, &dir);
|
|
|
|
assert_eq!(bc.best_block_hash(), genesis_hash);
|
|
|
|
bc.insert_block(&b1);
|
|
|
|
assert_eq!(bc.best_block_hash(), b1_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let bc = BlockChain::new(&genesis, &dir);
|
|
|
|
assert_eq!(bc.best_block_hash(), b1_hash);
|
|
|
|
}
|
|
|
|
}
|
2015-12-17 17:20:10 +01:00
|
|
|
}
|