openethereum/src/blockchain.rs

125 lines
3.1 KiB
Rust
Raw Normal View History

2015-12-12 15:52:37 +01:00
use std::collections::HashMap;
2015-12-13 17:33:11 +01:00
use std::sync::{Arc, Mutex};
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
// TODO: consider wrapping `genesis_info` into Arc<GenesisInfo>
// 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>,
// extras
// TODO: is arc really needed here, since blockchain itself will be wrapped
// into `Arc`?
block_details: Arc<Mutex<HashMap<H256, BlockDetails>>>
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;
/// use std::str::FromStr;
/// use ethcore::genesis::*;
/// use ethcore::blockchain::*;
/// use util::hash::*;
///
/// fn main() {
/// let genesis = Genesis::new_frontier();
/// let bc = BlockChain::new(genesis);
/// let genesis_hash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3";
/// assert_eq!(bc.genesis_hash(), &H256::from_str(genesis_hash).unwrap());
/// }
/// ```
pub fn new(genesis: Genesis) -> BlockChain {
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 17:33:11 +01:00
// TODO: also insert into backing db
let mut block_details = HashMap::new();
block_details.insert(genesis_hash.clone(), genesis_details);
let bc = 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,
block_details: Arc::new(Mutex::new(block_details))
};
bc
}
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());
2015-12-11 14:41:18 +01:00
block.mutable_state().insert_accounts(&self.genesis_state);
block.mutable_state().commit_db();
// 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!();
}
/// 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 17:33:11 +01:00
return false;
//unimplemented!()
// TODO: check is hash exist in hashes
2015-12-09 19:03:25 +01:00
}
}