2015-12-09 19:03:25 +01:00
|
|
|
use util::hash::*;
|
|
|
|
use util::rlp::*;
|
|
|
|
use util::hashdb::*;
|
|
|
|
use util::overlaydb::*;
|
2015-12-11 03:51:23 +01:00
|
|
|
use util::sha3::*;
|
2015-12-09 19:03:25 +01:00
|
|
|
use blockheader::*;
|
|
|
|
use block::*;
|
2015-12-11 03:51:23 +01:00
|
|
|
use verifiedblock::*;
|
|
|
|
use importroute::*;
|
2015-12-09 19:03:25 +01:00
|
|
|
|
|
|
|
pub struct BlockChain {
|
2015-12-11 03:51:23 +01:00
|
|
|
genesis_block: Vec<u8>,
|
|
|
|
genesis_hash: H256
|
2015-12-09 19:03:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockChain {
|
2015-12-11 03:51:23 +01:00
|
|
|
pub fn new(genesis_block: Vec<u8>) -> BlockChain {
|
|
|
|
// consider creating `GenesisView` for genesis block RLP
|
|
|
|
let genesis_hash = BlockView::new(&genesis_block).parent_hash().sha3();
|
|
|
|
|
|
|
|
BlockChain {
|
|
|
|
genesis_block: genesis_block,
|
|
|
|
genesis_hash: 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-11 03:51:23 +01:00
|
|
|
|
2015-12-09 19:03:25 +01:00
|
|
|
if db.exists(&root) {
|
2015-12-11 03:51:23 +01:00
|
|
|
return Block::new_existing(db.clone(), root)
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut block = Block::new(db.clone());
|
|
|
|
// TODO: commit genesis state (accounts) to block.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
|
|
|
}
|
2015-12-11 03:51:23 +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-09 19:03:25 +01:00
|
|
|
unimplemented!()
|
2015-12-11 03:51:23 +01:00
|
|
|
// TODO: check is hash exist in hashes
|
2015-12-09 19:03:25 +01:00
|
|
|
}
|
|
|
|
}
|