blockchain init

This commit is contained in:
debris 2015-12-09 19:03:25 +01:00
parent 74368663db
commit c50c005fd6
5 changed files with 100 additions and 6 deletions

18
src/block.rs Normal file
View File

@ -0,0 +1,18 @@
use util::hash::*;
use util::hashdb::*;
use util::overlaydb::*;
use state::*;
/// Active model of a block within the blockchain
pub struct Block {
state: State
}
impl Block {
/// Basic state object from database
pub fn new(db: OverlayDB) -> Block {
Block {
state: State::new(db)
}
}
}

21
src/blockchain.rs Normal file
View File

@ -0,0 +1,21 @@
use util::hash::*;
use util::rlp::*;
use util::hashdb::*;
use util::overlaydb::*;
use blockheader::*;
use block::*;
pub struct BlockChain {
genesis_hash: H256,
genesis_block: Vec<u8>
}
impl BlockChain {
pub fn genesis_block(&self, db: &OverlayDB) -> Block {
let root = BlockView::new(&self.genesis_block).state_root();
if db.exists(&root) {
return Block::new(db.clone())
}
unimplemented!()
}
}

View File

@ -2,10 +2,39 @@ use util::hash::*;
use util::uint::*;
use util::rlp::*;
/// view onto block header rlp
pub struct BlockView<'a> {
rlp: Rlp<'a>
}
impl<'a> BlockView<'a> {
pub fn new(bytes: &'a [u8]) -> BlockView<'a> {
BlockView {
rlp: Rlp::new(bytes)
}
}
pub fn parent_hash(&self) -> H256 { self.rlp.val_at(0) }
pub fn uncles_hash(&self) -> H256 { self.rlp.val_at(1) }
pub fn coinbase(&self) -> Address { self.rlp.val_at(2) }
pub fn state_root(&self) -> H256 { self.rlp.val_at(3) }
pub fn transactions_root(&self) -> H256 { self.rlp.val_at(4) }
pub fn receipts_root(&self) -> H256 { self.rlp.val_at(5) }
pub fn log_bloom(&self) -> H2048 { self.rlp.val_at(6) }
pub fn difficulty(&self) -> U256 { self.rlp.val_at(7) }
pub fn number(&self) -> U256 { self.rlp.val_at(8) }
pub fn gas_limit(&self) -> U256 { self.rlp.val_at(9) }
pub fn gas_usd(&self) -> U256 { self.rlp.val_at(10) }
pub fn timestamp(&self) -> U256 { self.rlp.val_at(11) }
pub fn mix_hash(&self) -> H256 { self.rlp.val_at(12) }
pub fn nonce(&self) -> H64 { self.rlp.val_at(13) }
}
/// Data structure represening block header
pub struct BlockHeader {
parent_hash: H256,
ommers_hash: H256,
beneficiary: Address,
uncles_hash: H256,
coinbase: Address,
state_root: H256,
transactions_root: H256,
receipts_root: H256,
@ -22,10 +51,11 @@ pub struct BlockHeader {
impl Decodable for BlockHeader {
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
decoder.read_list(| d | {
// return an error if d != 14
let blockheader = BlockHeader {
parent_hash: try!(Decodable::decode(&d[0])),
ommers_hash: try!(Decodable::decode(&d[1])),
beneficiary: try!(Decodable::decode(&d[2])),
uncles_hash: try!(Decodable::decode(&d[1])),
coinbase: try!(Decodable::decode(&d[2])),
state_root: try!(Decodable::decode(&d[3])),
transactions_root: try!(Decodable::decode(&d[4])),
receipts_root: try!(Decodable::decode(&d[5])),
@ -47,8 +77,8 @@ impl Encodable for BlockHeader {
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
encoder.emit_list(| e | {
self.parent_hash.encode(e);
self.ommers_hash.encode(e);
self.beneficiary.encode(e);
self.uncles_hash.encode(e);
self.coinbase.encode(e);
self.state_root.encode(e);
self.transactions_root.encode(e);
self.receipts_root.encode(e);

View File

@ -76,11 +76,16 @@ extern crate ethcore_util as util;
#[cfg(feature = "jit" )]
extern crate evmjit;
pub mod block;
pub mod blockchain;
pub mod blockheader;
pub mod transaction;
pub mod networkparams;
pub mod denominations;
pub mod state;
#[test]
fn it_works() {
}

20
src/state.rs Normal file
View File

@ -0,0 +1,20 @@
use util::hash::*;
use util::hashdb::*;
use util::overlaydb::*;
use util::trie::*;
pub struct State {
trie: TrieDB
}
impl State {
pub fn new(db: OverlayDB) -> State {
State {
trie: TrieDB::new(db)
}
}
pub fn new_temp() -> State {
Self::new(OverlayDB::new_temp())
}
}