From c50c005fd605efe8078d05d30b87fa6bdc6aa477 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 9 Dec 2015 19:03:25 +0100 Subject: [PATCH] blockchain init --- src/block.rs | 18 ++++++++++++++++++ src/blockchain.rs | 21 +++++++++++++++++++++ src/blockheader.rs | 42 ++++++++++++++++++++++++++++++++++++------ src/lib.rs | 5 +++++ src/state.rs | 20 ++++++++++++++++++++ 5 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 src/block.rs create mode 100644 src/blockchain.rs create mode 100644 src/state.rs diff --git a/src/block.rs b/src/block.rs new file mode 100644 index 000000000..9727d6a37 --- /dev/null +++ b/src/block.rs @@ -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) + } + } +} diff --git a/src/blockchain.rs b/src/blockchain.rs new file mode 100644 index 000000000..895a1dfb6 --- /dev/null +++ b/src/blockchain.rs @@ -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 +} + +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!() + } +} diff --git a/src/blockheader.rs b/src/blockheader.rs index 50a8d9c35..0f5010a37 100644 --- a/src/blockheader.rs +++ b/src/blockheader.rs @@ -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(decoder: &D) -> Result 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(&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); diff --git a/src/lib.rs b/src/lib.rs index ce40bc439..d66351e83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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() { } diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 000000000..693544bcd --- /dev/null +++ b/src/state.rs @@ -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()) + } +}