From 249c6157522631bf73da328707ca3f71994ed45c Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 9 Jan 2016 22:13:13 +0100 Subject: [PATCH] Minor refactoring, OpenBlock::close closer to working. --- src/basic_types.rs | 7 +++++++ src/block.rs | 40 ++++++++++++++++------------------------ src/common.rs | 2 ++ src/header.rs | 40 ++++++++++++++-------------------------- src/lib.rs | 3 ++- src/receipt.rs | 2 ++ 6 files changed, 43 insertions(+), 51 deletions(-) create mode 100644 src/basic_types.rs diff --git a/src/basic_types.rs b/src/basic_types.rs new file mode 100644 index 000000000..2c18c59d2 --- /dev/null +++ b/src/basic_types.rs @@ -0,0 +1,7 @@ +use util::*; + +/// Type for a 2048-bit log-bloom, as used by our blocks. +pub type LogBloom = H2048; + +/// Constant 2048-bit datum for 0. Often used as a default. +pub static ZERO_LOGBLOOM: LogBloom = H2048([0x00; 256]); diff --git a/src/block.rs b/src/block.rs index 15546f665..79bcd5505 100644 --- a/src/block.rs +++ b/src/block.rs @@ -1,9 +1,5 @@ -use util::*; -use transaction::*; -use receipt::*; +use common::*; use engine::*; -use header::*; -use env_info::*; use state::*; /// A transaction/receipt execution entry. @@ -124,23 +120,19 @@ impl<'engine> OpenBlock<'engine> { } /// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure out the uncles. - pub fn close(self, uncles: Vec
, author: Address, extra_data: Bytes) -> ClosedBlock<'engine> { - // TODO: populate rest of header. - self.engine.on_close_block(...); - self.header.author = author; - //self.header.transactions_root = ...; - let s = RlpStream::new_list(uncles.len()); - for u in uncles.iter() { - s.append(u.rlp()) - } - let uncle_bytes = u.out(); - self.header.uncles_hash = uncle_bytes.sha3(); - self.header.extra_data = extra_data; - self.header.state_root = self.state.root().clone(); - //self.header.receipts_root = ...; - //self.header.log_bloom = ...; // will need to amalgamate. - self.header.gas_used = self.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0)); - self.header.note_dirty(); + pub fn close(mut self, uncles: Vec
, author: Address, extra_data: Bytes) -> ClosedBlock<'engine> { + // populate rest of header. +// self.engine.on_close_block(...); + self.block.header.author = author; +// self.header.transactions_root = ...; + let uncle_bytes = uncles.iter().fold(RlpStream::new_list(uncles.len()), |mut s, u| {s.append(&u.rlp(Seal::With)); s} ).out(); + self.block.header.uncles_hash = uncle_bytes.sha3(); + self.block.header.extra_data = extra_data; + self.block.header.state_root = self.block.state.root().clone(); +// self.header.receipts_root = ...; + self.block.header.log_bloom = self.block.archive.iter().fold(LogBloom::zero(), |mut b, e| {b |= &e.receipt.log_bloom; b}); + self.block.header.gas_used = self.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0)); + self.block.header.note_dirty(); ClosedBlock::new(self, uncle_bytes) } @@ -151,8 +143,8 @@ impl<'engine> IsBlock for OpenBlock<'engine> { } impl<'engine> ClosedBlock<'engine> { - fn new(open_block: OpenBlock, uncles: Bytes) -> Self { - Self { + fn new<'a>(open_block: OpenBlock<'a>, uncles: Bytes) -> ClosedBlock<'a> { + ClosedBlock { open_block: open_block, uncles: uncles, } diff --git a/src/common.rs b/src/common.rs index ec4eb14cd..106a7a3b9 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,4 +1,5 @@ pub use util::*; +pub use basic_types::*; pub use env_info::*; pub use evm_schedule::*; pub use views::*; @@ -6,3 +7,4 @@ pub use builtin::*; pub use header::*; pub use account::*; pub use transaction::*; +pub use receipt::*; diff --git a/src/header.rs b/src/header.rs index 85655d6e0..56f03460e 100644 --- a/src/header.rs +++ b/src/header.rs @@ -1,14 +1,5 @@ use util::*; - -/// Type for a 2048-bit log-bloom, as used by our blocks. -pub type LogBloom = H2048; - -/// Constant address for point 0. Often used as a default. -pub static ZERO_ADDRESS: Address = Address([0x00; 20]); -/// Constant 256-bit datum for 0. Often used as a default. -pub static ZERO_H256: H256 = H256([0x00; 32]); -/// Constant 2048-bit datum for 0. Often used as a default. -pub static ZERO_LOGBLOOM: LogBloom = H2048([0x00; 256]); +use basic_types::*; /// A block header. /// @@ -39,13 +30,11 @@ pub struct Header { pub hash: RefCell>, //TODO: make this private } -enum SealInclusion { - WithSeal, - WithoutSeal, +pub enum Seal { + With, + Without, } -pub use SealInclusion::*; - impl Header { /// Create a new, default-valued, header. pub fn new() -> Header { @@ -77,8 +66,8 @@ impl Header { match &mut *hash { &mut Some(ref h) => h.clone(), hash @ &mut None => { - *hash = Some(self.hash(WithSeal)); - hash.unwrap().clone() + *hash = Some(self.rlp_sha3(Seal::With)); + hash.as_ref().unwrap().clone() } } } @@ -91,8 +80,8 @@ impl Header { // TODO: get hash without seal. // TODO: make these functions traity - pub fn stream_rlp(&self, s: &mut RlpStream, with_seal: SealInclusion) { - s.append_list(13 + if with_seal == WithSeal {self.seal.len()} else {0}) + pub fn stream_rlp(&self, s: &mut RlpStream, with_seal: Seal) { + s.append_list(13 + match with_seal { Seal::With => self.seal.len(), _ => 0 }); s.append(&self.parent_hash); s.append(&self.uncles_hash); s.append(&self.author); @@ -106,20 +95,19 @@ impl Header { s.append(&self.gas_used); s.append(&self.timestamp); s.append(&self.extra_data); - if with_seal == WithSeal { - for b in self.seal.iter() { - e.append_raw(&b); - } + match with_seal { + Seal::With => for b in self.seal.iter() { s.append_raw(&b, 1); }, + _ => {} } } - pub rlp(&self, with_seal: SealInclusion) -> Bytes { - let s = RlpStream::new(); + pub fn rlp(&self, with_seal: Seal) -> Bytes { + let mut s = RlpStream::new(); self.stream_rlp(&mut s, with_seal); s.out() } - pub hash(&self, with_seal: SealInclusion) -> H256 { self.rlp().sha3() } + pub fn rlp_sha3(&self, with_seal: Seal) -> H256 { self.rlp(with_seal).sha3() } } impl Decodable for Header { diff --git a/src/lib.rs b/src/lib.rs index 698801d8e..f4621feeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #![feature(cell_extras)] - +#![feature(augmented_assignments)] //! Ethcore's ethereum implementation //! //! ### Rust version @@ -85,6 +85,7 @@ extern crate evmjit; extern crate ethcore_util as util; pub mod common; +pub mod basic_types; pub mod env_info; pub mod engine; pub mod state; diff --git a/src/receipt.rs b/src/receipt.rs index fb72e2e18..6f91c14dc 100644 --- a/src/receipt.rs +++ b/src/receipt.rs @@ -1,8 +1,10 @@ use util::*; +use basic_types::LogBloom; /// Information describing execution of a transaction. pub struct Receipt { // TODO pub state_root: H256, pub gas_used: U256, + pub log_bloom: LogBloom, }