openethereum/src/blockchain.rs

201 lines
5.4 KiB
Rust
Raw Normal View History

2015-12-12 15:52:37 +01:00
use std::collections::HashMap;
2015-12-13 22:39:01 +01:00
use std::sync::{Arc, RwLock};
use std::path::Path;
use std::hash::Hash;
use rocksdb::{DB, WriteBatch, Writable};
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::overlaydb::*;
use util::sha3::*;
2015-12-09 19:03:25 +01:00
use blockheader::*;
use block::*;
use verifiedblock::*;
use importroute::*;
2015-12-11 14:41:18 +01:00
use account::*;
2015-12-12 15:52:37 +01:00
use genesis::*;
2015-12-13 17:33:11 +01:00
use extras::*;
2015-12-09 19:03:25 +01:00
pub struct BlockChain {
2015-12-13 17:33:11 +01:00
// rlp list of 3
genesis_block: Vec<u8>,
2015-12-13 17:33:11 +01:00
// genesis block header
2015-12-12 15:52:37 +01:00
genesis_header: Vec<u8>,
2015-12-11 14:41:18 +01:00
genesis_hash: H256,
2015-12-13 17:33:11 +01:00
genesis_state: HashMap<Address, Account>,
2015-12-13 22:39:01 +01:00
last_block_number: U256,
2015-12-13 17:33:11 +01:00
// extras
block_details_hash: Extras<H256, BlockDetails>,
block_hashes_hash: Extras<U256, H256>,
transaction_addresses_hash: Extras<H256, TransactionAddress>,
block_logs_hash: Extras<H256, BlockLogBlooms>,
blocks_blooms_hash: Extras<H256, BlocksBlooms>,
2015-12-13 22:39:01 +01:00
extras_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-13 22:39:01 +01:00
/// let mut dir = env::temp_dir();
/// dir.push(H32::random().hex());
///
2015-12-12 15:52:37 +01:00
/// let genesis = Genesis::new_frontier();
2015-12-13 22:39:01 +01:00
/// let bc = BlockChain::new(genesis, &dir);
2015-12-12 15:52:37 +01:00
/// let genesis_hash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3";
/// assert_eq!(bc.genesis_hash(), &H256::from_str(genesis_hash).unwrap());
2015-12-13 22:39:01 +01:00
/// assert!(bc.is_known(bc.genesis_hash()));
/// assert_eq!(bc.genesis_hash(), &bc.number_hash(&U256::from(0u8)).unwrap());
2015-12-12 15:52:37 +01:00
/// }
/// ```
2015-12-13 22:39:01 +01:00
pub fn new(genesis: Genesis, path: &Path) -> BlockChain {
2015-12-12 15:52:37 +01:00
let (genesis_block, genesis_state) = genesis.drain();
2015-12-13 17:33:11 +01:00
let genesis_header = Rlp::new(&genesis_block).at(0).raw().to_vec();
let genesis_hash = BlockView::new(&genesis_header).sha3();
let genesis_details = BlockDetails {
number: U256::from(0u64),
total_difficulty: BlockView::new(&genesis_header).difficulty(),
parent: H256::new(),
children: vec![]
2015-12-12 15:52:37 +01:00
};
2015-12-13 22:39:01 +01:00
let db = DB::open_default(path.to_str().unwrap()).unwrap();
{
let mut batch = WriteBatch::new();
batch.put(&genesis_hash.to_extras_slice(ExtrasIndex::BlockDetails), &encode(&genesis_details));
batch.put(&U256::from(0u8).to_extras_slice(ExtrasIndex::BlockHash), &encode(&genesis_hash));
db.write(batch);
}
2015-12-13 17:33:11 +01:00
2015-12-13 22:39:01 +01:00
BlockChain {
genesis_block: genesis_block,
2015-12-12 15:52:37 +01:00
genesis_header: genesis_header,
2015-12-11 14:41:18 +01:00
genesis_hash: genesis_hash,
2015-12-13 17:33:11 +01:00
genesis_state: genesis_state,
2015-12-13 22:39:01 +01:00
last_block_number: U256::from(0u8),
block_details_hash: Extras::new(ExtrasIndex::BlockDetails),
block_hashes_hash: Extras::new(ExtrasIndex::BlockHash),
transaction_addresses_hash: Extras::new(ExtrasIndex::TransactionAddress),
block_logs_hash: Extras::new(ExtrasIndex::BlockLogBlooms),
blocks_blooms_hash: Extras::new(ExtrasIndex::BlocksBlooms),
2015-12-13 22:39:01 +01:00
extras_db: db
}
}
2015-12-12 15:52:37 +01:00
pub fn genesis_hash(&self) -> &H256 {
&self.genesis_hash
}
2015-12-09 19:03:25 +01:00
pub fn genesis_block(&self, db: &OverlayDB) -> Block {
let root = BlockView::new(&self.genesis_block).state_root();
2015-12-09 19:03:25 +01:00
if db.exists(&root) {
return Block::new_existing(db.clone(), root)
}
let mut block = Block::new(db.clone());
// TODO: commit
//block.mutable_state().insert_accounts(&self.genesis_state);
2015-12-14 11:05:40 +01:00
block.mutable_state().db().commit();
// TODO: set previous block
// TODO: reset current
block
}
pub fn verify_block<'a>(&self, block: &'a [u8]) -> VerifiedBlock<'a> {
//TODO: verify block
VerifiedBlock::new(block)
}
pub fn import_block(&self, block: &[u8], db: &OverlayDB) -> ImportRoute {
let view = BlockView::new(block);
// check if we already know this block
if self.is_known(&view.sha3()) {
2015-12-09 19:03:25 +01:00
}
// check if we already know parent of this block
if !self.is_known(&view.parent_hash()) {
}
unimplemented!();
}
2015-12-13 22:39:01 +01:00
/// Get the hash of given block's number
pub fn number_hash(&self, hash: &U256) -> Option<H256> {
self.query_extras(hash, &self.block_hashes_hash)
2015-12-13 22:39:01 +01:00
}
/// 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-13 22:39:01 +01:00
// TODO: first do lookup in blocks_db for given hash
// TODO: consider taking into account current block
match self.query_extras(hash, &self.block_details_hash) {
2015-12-13 22:39:01 +01:00
None => false,
Some(details) => details.number <= self.last_block_number
}
}
pub fn query_extras<K, T>(&self, hash: &K, cache: &Extras<K, T>) -> Option<T> where
T: Clone + Decodable,
K: ExtrasSliceConvertable + Eq + Hash + Clone {
{
let read = cache.read().unwrap();
match read.get(hash) {
Some(v) => return Some(v.clone()),
None => ()
}
}
let opt = self.extras_db.get(&hash.to_extras_slice(cache.index()))
.expect("Low level database error. Some issue with disk?");
match opt {
Some(b) => {
let t: T = decode(&b);
let mut write = cache.write().unwrap();
write.insert(hash.clone(), t.clone());
Some(t)
},
None => None
}
}
pub fn query_extras_exist<K, T>(&self, hash: &K, cache: &Extras<K, T>) -> bool where
K: ExtrasSliceConvertable + Eq + Hash + Clone {
{
let read = cache.read().unwrap();
match read.get(hash) {
Some(_) => return true,
None => ()
}
}
let opt = self.extras_db.get(&hash.to_extras_slice(cache.index()))
.expect("Low level database error. Some issue with disk?");
opt.is_some()
2015-12-09 19:03:25 +01:00
}
}
2015-12-13 22:39:01 +01:00