Merge branch 'network' into verification

This commit is contained in:
arkpar 2016-01-11 12:45:35 +01:00
commit 2b522e2c48
23 changed files with 345 additions and 209 deletions

0
<std macros> Normal file
View File

View File

@ -15,6 +15,7 @@ flate2 = "0.2"
rocksdb = "0.2.1" rocksdb = "0.2.1"
heapsize = "0.2.0" heapsize = "0.2.0"
rust-crypto = "0.2.34" rust-crypto = "0.2.34"
time = "0.1"
evmjit = { path = "rust-evmjit", optional = true } evmjit = { path = "rust-evmjit", optional = true }

View File

@ -16,10 +16,10 @@ fn main() {
::env_logger::init().ok(); ::env_logger::init().ok();
let mut service = NetworkService::start().unwrap(); let mut service = NetworkService::start().unwrap();
//TODO: replace with proper genesis and chain params. //TODO: replace with proper genesis and chain params.
let frontier = ethereum::new_frontier(); let spec = ethereum::new_frontier();
let mut dir = env::temp_dir(); let mut dir = env::temp_dir();
dir.push(H32::random().hex()); dir.push(H32::random().hex());
let client = Arc::new(Client::new(&frontier.genesis_block(), &dir)); let client = Arc::new(Client::new(spec, &dir).unwrap());
EthSync::register(&mut service, client); EthSync::register(&mut service, client);
loop { loop {
let mut cmd = String::new(); let mut cmd = String::new();

View File

@ -21,7 +21,16 @@ pub struct Block {
uncles: Vec<Header>, uncles: Vec<Header>,
} }
/// A set of references to `Block` fields that are publicly accessible.
pub struct BlockRefMut<'a> {
pub header: &'a Header,
pub state: &'a mut State,
pub archive: &'a Vec<Entry>,
pub uncles: &'a Vec<Header>,
}
impl Block { impl Block {
/// Create a new block from the given `state`.
fn new(state: State) -> Block { fn new(state: State) -> Block {
Block { Block {
header: Header::new(), header: Header::new(),
@ -32,7 +41,15 @@ impl Block {
} }
} }
pub fn state_mut(&mut self) -> &mut State { &mut self.state } /// Get a structure containing individual references to all public fields.
pub fn fields(&mut self) -> BlockRefMut {
BlockRefMut {
header: &self.header,
state: &mut self.state,
archive: &self.archive,
uncles: &self.uncles,
}
}
} }
/// Trait for a object that is_a `Block`. /// Trait for a object that is_a `Block`.
@ -61,18 +78,18 @@ impl IsBlock for Block {
/// ///
/// It's a bit like a Vec<Transaction>, eccept that whenever a transaction is pushed, we execute it and /// It's a bit like a Vec<Transaction>, eccept that whenever a transaction is pushed, we execute it and
/// maintain the system `state()`. We also archive execution receipts in preparation for later block creation. /// maintain the system `state()`. We also archive execution receipts in preparation for later block creation.
pub struct OpenBlock<'engine> { pub struct OpenBlock<'x, 'y> {
block: Block, block: Block,
engine: &'engine Engine, engine: &'x Engine,
last_hashes: LastHashes, last_hashes: &'y LastHashes,
} }
/// Just like OpenBlock, except that we've applied `Engine::on_close_block`, finished up the non-seal header fields, /// Just like OpenBlock, except that we've applied `Engine::on_close_block`, finished up the non-seal header fields,
/// and collected the uncles. /// and collected the uncles.
/// ///
/// There is no function available to push a transaction. If you want that you'll need to `reopen()` it. /// There is no function available to push a transaction. If you want that you'll need to `reopen()` it.
pub struct ClosedBlock<'engine> { pub struct ClosedBlock<'x, 'y> {
open_block: OpenBlock<'engine>, open_block: OpenBlock<'x, 'y>,
uncle_bytes: Bytes, uncle_bytes: Bytes,
} }
@ -84,9 +101,9 @@ pub struct SealedBlock {
uncle_bytes: Bytes, uncle_bytes: Bytes,
} }
impl<'engine> OpenBlock<'engine> { impl<'x, 'y> OpenBlock<'x, 'y> {
/// Create a new OpenBlock ready for transaction pushing. /// Create a new OpenBlock ready for transaction pushing.
pub fn new<'a>(engine: &'a Engine, db: OverlayDB, parent: &Header, last_hashes: LastHashes, author: Address, extra_data: Bytes) -> OpenBlock<'a> { pub fn new<'a, 'b>(engine: &'a Engine, db: OverlayDB, parent: &Header, last_hashes: &'b LastHashes, author: Address, extra_data: Bytes) -> OpenBlock<'a, 'b> {
let mut r = OpenBlock { let mut r = OpenBlock {
block: Block::new(State::from_existing(db, parent.state_root.clone(), engine.account_start_nonce())), block: Block::new(State::from_existing(db, parent.state_root.clone(), engine.account_start_nonce())),
engine: engine, engine: engine,
@ -95,6 +112,8 @@ impl<'engine> OpenBlock<'engine> {
r.block.header.set_author(author); r.block.header.set_author(author);
r.block.header.set_extra_data(extra_data); r.block.header.set_extra_data(extra_data);
r.block.header.set_timestamp_now();
engine.populate_from_parent(&mut r.block.header, parent); engine.populate_from_parent(&mut r.block.header, parent);
engine.on_new_block(&mut r.block); engine.on_new_block(&mut r.block);
r r
@ -103,6 +122,9 @@ impl<'engine> OpenBlock<'engine> {
/// Alter the author for the block. /// Alter the author for the block.
pub fn set_author(&mut self, author: Address) { self.block.header.set_author(author); } pub fn set_author(&mut self, author: Address) { self.block.header.set_author(author); }
/// Alter the timestamp of the block.
pub fn set_timestamp(&mut self, timestamp: u64) { self.block.header.set_timestamp(timestamp); }
/// Alter the extra_data for the block. /// Alter the extra_data for the block.
pub fn set_extra_data(&mut self, extra_data: Bytes) -> Result<(), BlockError> { pub fn set_extra_data(&mut self, extra_data: Bytes) -> Result<(), BlockError> {
if extra_data.len() > self.engine.maximum_extra_data_size() { if extra_data.len() > self.engine.maximum_extra_data_size() {
@ -131,9 +153,9 @@ impl<'engine> OpenBlock<'engine> {
pub fn env_info(&self) -> EnvInfo { pub fn env_info(&self) -> EnvInfo {
// TODO: memoise. // TODO: memoise.
EnvInfo { EnvInfo {
number: self.block.header.number.clone(), number: self.block.header.number,
author: self.block.header.author.clone(), author: self.block.header.author.clone(),
timestamp: self.block.header.timestamp.clone(), timestamp: self.block.header.timestamp,
difficulty: self.block.header.difficulty.clone(), difficulty: self.block.header.difficulty.clone(),
last_hashes: self.last_hashes.clone(), last_hashes: self.last_hashes.clone(),
gas_used: self.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0)), gas_used: self.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0)),
@ -157,7 +179,7 @@ impl<'engine> OpenBlock<'engine> {
} }
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure out the uncles. /// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure out the uncles.
pub fn close(self) -> ClosedBlock<'engine> { pub fn close(self) -> ClosedBlock<'x, 'y> {
let mut s = self; let mut s = self;
s.engine.on_close_block(&mut s.block); s.engine.on_close_block(&mut s.block);
s.block.header.transactions_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.transaction.rlp_bytes()).collect()); s.block.header.transactions_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.transaction.rlp_bytes()).collect());
@ -173,16 +195,16 @@ impl<'engine> OpenBlock<'engine> {
} }
} }
impl<'engine> IsBlock for OpenBlock<'engine> { impl<'x, 'y> IsBlock for OpenBlock<'x, 'y> {
fn block(&self) -> &Block { &self.block } fn block(&self) -> &Block { &self.block }
} }
impl<'engine> IsBlock for ClosedBlock<'engine> { impl<'x, 'y> IsBlock for ClosedBlock<'x, 'y> {
fn block(&self) -> &Block { &self.open_block.block } fn block(&self) -> &Block { &self.open_block.block }
} }
impl<'engine> ClosedBlock<'engine> { impl<'x, 'y> ClosedBlock<'x, 'y> {
fn new<'a>(open_block: OpenBlock<'a>, uncle_bytes: Bytes) -> ClosedBlock<'a> { fn new<'a, 'b>(open_block: OpenBlock<'a, 'b>, uncle_bytes: Bytes) -> ClosedBlock<'a, 'b> {
ClosedBlock { ClosedBlock {
open_block: open_block, open_block: open_block,
uncle_bytes: uncle_bytes, uncle_bytes: uncle_bytes,
@ -205,7 +227,7 @@ impl<'engine> ClosedBlock<'engine> {
} }
/// Turn this back into an `OpenBlock`. /// Turn this back into an `OpenBlock`.
pub fn reopen(self) -> OpenBlock<'engine> { self.open_block } pub fn reopen(self) -> OpenBlock<'x, 'y> { self.open_block }
} }
impl SealedBlock { impl SealedBlock {
@ -217,13 +239,26 @@ impl SealedBlock {
for e in self.block.archive.iter() { e.transaction.rlp_append(&mut block_rlp); } for e in self.block.archive.iter() { e.transaction.rlp_append(&mut block_rlp); }
block_rlp.append_raw(&self.uncle_bytes, 1); block_rlp.append_raw(&self.uncle_bytes, 1);
block_rlp.out() block_rlp.out()
} }
/// Drop this object and return the underlieing database.
pub fn drain(self) -> OverlayDB { self.block.state.drop().1 }
} }
impl IsBlock for SealedBlock { impl IsBlock for SealedBlock {
fn block(&self) -> &Block { &self.block } fn block(&self) -> &Block { &self.block }
} }
pub fn enact(rlp_bytes: &[u8], engine: &Engine, db: OverlayDB, parent: &Header, last_hashes: &LastHashes) -> Result<SealedBlock, Error> {
let block = BlockView::new(rlp_bytes);
let header = block.header_view();
let mut b = OpenBlock::new(engine, db, parent, last_hashes, header.author(), header.extra_data());
b.set_timestamp(header.timestamp());
for t in block.transactions().into_iter() { try!(b.push_transaction(t, None)); }
for u in block.uncles().into_iter() { try!(b.push_uncle(u)); }
Ok(try!(b.close().seal(header.seal())))
}
#[test] #[test]
fn open_block() { fn open_block() {
use spec::*; use spec::*;
@ -231,7 +266,30 @@ fn open_block() {
let genesis_header = engine.spec().genesis_header(); let genesis_header = engine.spec().genesis_header();
let mut db = OverlayDB::new_temp(); let mut db = OverlayDB::new_temp();
engine.spec().ensure_db_good(&mut db); engine.spec().ensure_db_good(&mut db);
let b = OpenBlock::new(engine.deref(), db, &genesis_header, vec![genesis_header.hash()], Address::zero(), vec![]); let last_hashes = vec![genesis_header.hash()];
let b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]);
let b = b.close(); let b = b.close();
let _ = b.seal(vec![]); let _ = b.seal(vec![]);
} }
#[test]
fn enact_block() {
use spec::*;
let engine = Spec::new_test().to_engine().unwrap();
let genesis_header = engine.spec().genesis_header();
let mut db = OverlayDB::new_temp();
engine.spec().ensure_db_good(&mut db);
let b = OpenBlock::new(engine.deref(), db, &genesis_header, &vec![genesis_header.hash()], Address::zero(), vec![]).close().seal(vec![]).unwrap();
let orig_bytes = b.rlp_bytes();
let orig_db = b.drain();
let mut db = OverlayDB::new_temp();
engine.spec().ensure_db_good(&mut db);
let e = enact(&orig_bytes, engine.deref(), db, &genesis_header, &vec![genesis_header.hash()]).unwrap();
assert_eq!(e.rlp_bytes(), orig_bytes);
let db = e.drain();
assert_eq!(orig_db.keys(), db.keys());
}

View File

@ -1,6 +1,5 @@
//! Fast access to blockchain data. //! Fast access to blockchain data.
use std::sync::*;
use util::*; use util::*;
use rocksdb::{DB, WriteBatch, Writable}; use rocksdb::{DB, WriteBatch, Writable};
use header::*; use header::*;
@ -34,7 +33,7 @@ pub struct CacheSize {
/// Information about best block gathered together /// Information about best block gathered together
struct BestBlock { struct BestBlock {
pub hash: H256, pub hash: H256,
pub number: U256, pub number: BlockNumber,
pub total_difficulty: U256 pub total_difficulty: U256
} }
@ -42,7 +41,7 @@ impl BestBlock {
fn new() -> BestBlock { fn new() -> BestBlock {
BestBlock { BestBlock {
hash: H256::new(), hash: H256::new(),
number: U256::from(0), number: 0,
total_difficulty: U256::from(0) total_difficulty: U256::from(0)
} }
} }
@ -59,7 +58,7 @@ pub struct BlockChain {
// extra caches // extra caches
block_details: RwLock<HashMap<H256, BlockDetails>>, block_details: RwLock<HashMap<H256, BlockDetails>>,
block_hashes: RwLock<HashMap<U256, H256>>, block_hashes: RwLock<HashMap<BlockNumber, H256>>,
transaction_addresses: RwLock<HashMap<H256, TransactionAddress>>, transaction_addresses: RwLock<HashMap<H256, TransactionAddress>>,
block_logs: RwLock<HashMap<H256, BlockLogBlooms>>, block_logs: RwLock<HashMap<H256, BlockLogBlooms>>,
blocks_blooms: RwLock<HashMap<H256, BlocksBlooms>>, blocks_blooms: RwLock<HashMap<H256, BlocksBlooms>>,
@ -93,7 +92,7 @@ impl BlockChain {
/// let genesis_hash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"; /// let genesis_hash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3";
/// assert_eq!(bc.genesis_hash(), H256::from_str(genesis_hash).unwrap()); /// assert_eq!(bc.genesis_hash(), H256::from_str(genesis_hash).unwrap());
/// assert!(bc.is_known(&bc.genesis_hash())); /// assert!(bc.is_known(&bc.genesis_hash()));
/// assert_eq!(bc.genesis_hash(), bc.block_hash(&U256::from(0u8)).unwrap()); /// assert_eq!(bc.genesis_hash(), bc.block_hash(0).unwrap());
/// } /// }
/// ``` /// ```
pub fn new(genesis: &[u8], path: &Path) -> BlockChain { pub fn new(genesis: &[u8], path: &Path) -> BlockChain {
@ -200,11 +199,16 @@ impl BlockChain {
/// ```json /// ```json
/// { blocks: [B4, B3, A3, A4], ancestor: A2, index: 2 } /// { blocks: [B4, B3, A3, A4], ancestor: A2, index: 2 }
/// ``` /// ```
pub fn tree_route(&self, from: H256, to: H256) -> TreeRoute { pub fn tree_route(&self, from: H256, to: H256) -> Option<TreeRoute> {
let from_details = self.block_details(&from).expect("from hash is invalid!"); let from_details = match self.block_details(&from) {
let to_details = self.block_details(&to).expect("to hash is invalid!"); Some(h) => h,
None => return None,
self._tree_route((from_details, from), (to_details, to)) };
let to_details = match self.block_details(&to) {
Some(h) => h,
None => return None,
};
Some(self._tree_route((from_details, from), (to_details, to)))
} }
/// Similar to `tree_route` function, but can be used to return a route /// Similar to `tree_route` function, but can be used to return a route
@ -337,9 +341,9 @@ impl BlockChain {
// it is a fork // it is a fork
i if i > 1 => { i if i > 1 => {
let ancestor_number = self.block_number(&route.ancestor).unwrap(); let ancestor_number = self.block_number(&route.ancestor).unwrap();
let start_number = ancestor_number + U256::from(1u8); let start_number = ancestor_number + 1;
for (index, hash) in route.blocks.iter().skip(route.index).enumerate() { for (index, hash) in route.blocks.iter().skip(route.index).enumerate() {
batch.put_extras(&(start_number + U256::from(index as u64)), hash); batch.put_extras(&(start_number + index as BlockNumber), hash);
} }
}, },
// route.blocks.len() could be 0 only if inserted block is best block, // route.blocks.len() could be 0 only if inserted block is best block,
@ -372,7 +376,7 @@ impl BlockChain {
/// Returns reference to genesis hash. /// Returns reference to genesis hash.
pub fn genesis_hash(&self) -> H256 { pub fn genesis_hash(&self) -> H256 {
self.block_hash(&U256::from(0u8)).expect("Genesis hash should always exist") self.block_hash(0).expect("Genesis hash should always exist")
} }
/// Get the partial-header of a block. /// Get the partial-header of a block.
@ -410,8 +414,8 @@ impl BlockChain {
} }
/// Get the hash of given block's number. /// Get the hash of given block's number.
pub fn block_hash(&self, hash: &U256) -> Option<H256> { pub fn block_hash(&self, index: BlockNumber) -> Option<H256> {
self.query_extras(hash, &self.block_hashes) self.query_extras(&index, &self.block_hashes)
} }
/// Get best block hash. /// Get best block hash.
@ -420,7 +424,7 @@ impl BlockChain {
} }
/// Get best block number. /// Get best block number.
pub fn best_block_number(&self) -> U256 { pub fn best_block_number(&self) -> BlockNumber {
self.best_block.read().unwrap().number self.best_block.read().unwrap().number
} }
@ -430,7 +434,7 @@ impl BlockChain {
} }
/// Get the number of given block's hash. /// Get the number of given block's hash.
pub fn block_number(&self, hash: &H256) -> Option<U256> { pub fn block_number(&self, hash: &H256) -> Option<BlockNumber> {
self.block(hash).map(|bytes| BlockView::new(&bytes).header_view().number()) self.block(hash).map(|bytes| BlockView::new(&bytes).header_view().number())
} }
@ -522,7 +526,6 @@ mod tests {
use std::str::FromStr; use std::str::FromStr;
use rustc_serialize::hex::FromHex; use rustc_serialize::hex::FromHex;
use util::hash::*; use util::hash::*;
use util::uint::*;
use blockchain::*; use blockchain::*;
#[test] #[test]
@ -537,25 +540,24 @@ mod tests {
let genesis_hash = H256::from_str("3caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942").unwrap(); let genesis_hash = H256::from_str("3caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942").unwrap();
assert_eq!(bc.genesis_hash(), genesis_hash.clone()); assert_eq!(bc.genesis_hash(), genesis_hash.clone());
assert_eq!(bc.best_block_number(), U256::from(0u8)); assert_eq!(bc.best_block_number(), 0);
assert_eq!(bc.best_block_hash(), genesis_hash.clone()); assert_eq!(bc.best_block_hash(), genesis_hash.clone());
assert_eq!(bc.block_hash(&U256::from(0u8)), Some(genesis_hash.clone())); assert_eq!(bc.block_hash(0), Some(genesis_hash.clone()));
assert_eq!(bc.block_hash(&U256::from(1u8)), None); assert_eq!(bc.block_hash(1), None);
let first = "f90285f90219a03caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0bac6177a79e910c98d86ec31a09ae37ac2de15b754fd7bed1ba52362c49416bfa0d45893a296c1490a978e0bd321b5f2635d8280365c1fe9f693d65f233e791344a0c7778a7376099ee2e5c455791c1885b5c361b95713fddcbe32d97fd01334d296b90100000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000400000000000000000000000000000000000000000000000000000008302000001832fefd882560b845627cb99a00102030405060708091011121314151617181920212223242526272829303132a08ccb2837fb2923bd97e8f2d08ea32012d6e34be018c73e49a0f98843e8f47d5d88e53be49fec01012ef866f864800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d8785012a05f200801ba0cb088b8d2ff76a7b2c6616c9d02fb6b7a501afbf8b69d7180b09928a1b80b5e4a06448fe7476c606582039bb72a9f6f4b4fad18507b8dfbd00eebbe151cc573cd2c0".from_hex().unwrap(); let first = "f90285f90219a03caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0bac6177a79e910c98d86ec31a09ae37ac2de15b754fd7bed1ba52362c49416bfa0d45893a296c1490a978e0bd321b5f2635d8280365c1fe9f693d65f233e791344a0c7778a7376099ee2e5c455791c1885b5c361b95713fddcbe32d97fd01334d296b90100000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000400000000000000000000000000000000000000000000000000000008302000001832fefd882560b845627cb99a00102030405060708091011121314151617181920212223242526272829303132a08ccb2837fb2923bd97e8f2d08ea32012d6e34be018c73e49a0f98843e8f47d5d88e53be49fec01012ef866f864800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d8785012a05f200801ba0cb088b8d2ff76a7b2c6616c9d02fb6b7a501afbf8b69d7180b09928a1b80b5e4a06448fe7476c606582039bb72a9f6f4b4fad18507b8dfbd00eebbe151cc573cd2c0".from_hex().unwrap();
bc.insert_block(&first); bc.insert_block(&first);
let first_hash = H256::from_str("a940e5af7d146b3b917c953a82e1966b906dace3a4e355b5b0a4560190357ea1").unwrap(); let first_hash = H256::from_str("a940e5af7d146b3b917c953a82e1966b906dace3a4e355b5b0a4560190357ea1").unwrap();
assert_eq!(bc.block_hash(&U256::from(0u8)), Some(genesis_hash.clone())); assert_eq!(bc.block_hash(0), Some(genesis_hash.clone()));
assert_eq!(bc.best_block_number(), U256::from(1u8)); assert_eq!(bc.best_block_number(), 1);
assert_eq!(bc.best_block_hash(), first_hash.clone()); assert_eq!(bc.best_block_hash(), first_hash.clone());
assert_eq!(bc.block_hash(&U256::from(1u8)), Some(first_hash.clone())); assert_eq!(bc.block_hash(1), Some(first_hash.clone()));
assert_eq!(bc.block_details(&first_hash).unwrap().parent, genesis_hash.clone()); assert_eq!(bc.block_details(&first_hash).unwrap().parent, genesis_hash.clone());
assert_eq!(bc.block_details(&genesis_hash).unwrap().children, vec![first_hash.clone()]); assert_eq!(bc.block_details(&genesis_hash).unwrap().children, vec![first_hash.clone()]);
assert_eq!(bc.block_hash(&U256::from(2u8)), None); assert_eq!(bc.block_hash(2), None);
} }
#[test] #[test]
@ -585,64 +587,64 @@ mod tests {
bc.insert_block(&b3b); bc.insert_block(&b3b);
assert_eq!(bc.best_block_hash(), best_block_hash); assert_eq!(bc.best_block_hash(), best_block_hash);
assert_eq!(bc.block_number(&genesis_hash).unwrap(), U256::from(0)); assert_eq!(bc.block_number(&genesis_hash).unwrap(), 0);
assert_eq!(bc.block_number(&b1_hash).unwrap(), U256::from(1)); assert_eq!(bc.block_number(&b1_hash).unwrap(), 1);
assert_eq!(bc.block_number(&b2_hash).unwrap(), U256::from(2)); assert_eq!(bc.block_number(&b2_hash).unwrap(), 2);
assert_eq!(bc.block_number(&b3a_hash).unwrap(), U256::from(3)); assert_eq!(bc.block_number(&b3a_hash).unwrap(), 3);
assert_eq!(bc.block_number(&b3b_hash).unwrap(), U256::from(3)); assert_eq!(bc.block_number(&b3b_hash).unwrap(), 3);
assert_eq!(bc.block_hash(&U256::from(0)).unwrap(), genesis_hash); assert_eq!(bc.block_hash(0).unwrap(), genesis_hash);
assert_eq!(bc.block_hash(&U256::from(1)).unwrap(), b1_hash); assert_eq!(bc.block_hash(1).unwrap(), b1_hash);
assert_eq!(bc.block_hash(&U256::from(2)).unwrap(), b2_hash); assert_eq!(bc.block_hash(2).unwrap(), b2_hash);
assert_eq!(bc.block_hash(&U256::from(3)).unwrap(), b3a_hash); assert_eq!(bc.block_hash(3).unwrap(), b3a_hash);
// test trie route // test trie route
let r0_1 = bc.tree_route(genesis_hash.clone(), b1_hash.clone()); let r0_1 = bc.tree_route(genesis_hash.clone(), b1_hash.clone()).unwrap();
assert_eq!(r0_1.ancestor, genesis_hash); assert_eq!(r0_1.ancestor, genesis_hash);
assert_eq!(r0_1.blocks, [b1_hash.clone()]); assert_eq!(r0_1.blocks, [b1_hash.clone()]);
assert_eq!(r0_1.index, 0); assert_eq!(r0_1.index, 0);
let r0_2 = bc.tree_route(genesis_hash.clone(), b2_hash.clone()); let r0_2 = bc.tree_route(genesis_hash.clone(), b2_hash.clone()).unwrap();
assert_eq!(r0_2.ancestor, genesis_hash); assert_eq!(r0_2.ancestor, genesis_hash);
assert_eq!(r0_2.blocks, [b1_hash.clone(), b2_hash.clone()]); assert_eq!(r0_2.blocks, [b1_hash.clone(), b2_hash.clone()]);
assert_eq!(r0_2.index, 0); assert_eq!(r0_2.index, 0);
let r1_3a = bc.tree_route(b1_hash.clone(), b3a_hash.clone()); let r1_3a = bc.tree_route(b1_hash.clone(), b3a_hash.clone()).unwrap();
assert_eq!(r1_3a.ancestor, b1_hash); assert_eq!(r1_3a.ancestor, b1_hash);
assert_eq!(r1_3a.blocks, [b2_hash.clone(), b3a_hash.clone()]); assert_eq!(r1_3a.blocks, [b2_hash.clone(), b3a_hash.clone()]);
assert_eq!(r1_3a.index, 0); assert_eq!(r1_3a.index, 0);
let r1_3b = bc.tree_route(b1_hash.clone(), b3b_hash.clone()); let r1_3b = bc.tree_route(b1_hash.clone(), b3b_hash.clone()).unwrap();
assert_eq!(r1_3b.ancestor, b1_hash); assert_eq!(r1_3b.ancestor, b1_hash);
assert_eq!(r1_3b.blocks, [b2_hash.clone(), b3b_hash.clone()]); assert_eq!(r1_3b.blocks, [b2_hash.clone(), b3b_hash.clone()]);
assert_eq!(r1_3b.index, 0); assert_eq!(r1_3b.index, 0);
let r3a_3b = bc.tree_route(b3a_hash.clone(), b3b_hash.clone()); let r3a_3b = bc.tree_route(b3a_hash.clone(), b3b_hash.clone()).unwrap();
assert_eq!(r3a_3b.ancestor, b2_hash); assert_eq!(r3a_3b.ancestor, b2_hash);
assert_eq!(r3a_3b.blocks, [b3a_hash.clone(), b3b_hash.clone()]); assert_eq!(r3a_3b.blocks, [b3a_hash.clone(), b3b_hash.clone()]);
assert_eq!(r3a_3b.index, 1); assert_eq!(r3a_3b.index, 1);
let r1_0 = bc.tree_route(b1_hash.clone(), genesis_hash.clone()); let r1_0 = bc.tree_route(b1_hash.clone(), genesis_hash.clone()).unwrap();
assert_eq!(r1_0.ancestor, genesis_hash); assert_eq!(r1_0.ancestor, genesis_hash);
assert_eq!(r1_0.blocks, [b1_hash.clone()]); assert_eq!(r1_0.blocks, [b1_hash.clone()]);
assert_eq!(r1_0.index, 1); assert_eq!(r1_0.index, 1);
let r2_0 = bc.tree_route(b2_hash.clone(), genesis_hash.clone()); let r2_0 = bc.tree_route(b2_hash.clone(), genesis_hash.clone()).unwrap();
assert_eq!(r2_0.ancestor, genesis_hash); assert_eq!(r2_0.ancestor, genesis_hash);
assert_eq!(r2_0.blocks, [b2_hash.clone(), b1_hash.clone()]); assert_eq!(r2_0.blocks, [b2_hash.clone(), b1_hash.clone()]);
assert_eq!(r2_0.index, 2); assert_eq!(r2_0.index, 2);
let r3a_1 = bc.tree_route(b3a_hash.clone(), b1_hash.clone()); let r3a_1 = bc.tree_route(b3a_hash.clone(), b1_hash.clone()).unwrap();
assert_eq!(r3a_1.ancestor, b1_hash); assert_eq!(r3a_1.ancestor, b1_hash);
assert_eq!(r3a_1.blocks, [b3a_hash.clone(), b2_hash.clone()]); assert_eq!(r3a_1.blocks, [b3a_hash.clone(), b2_hash.clone()]);
assert_eq!(r3a_1.index, 2); assert_eq!(r3a_1.index, 2);
let r3b_1 = bc.tree_route(b3b_hash.clone(), b1_hash.clone()); let r3b_1 = bc.tree_route(b3b_hash.clone(), b1_hash.clone()).unwrap();
assert_eq!(r3b_1.ancestor, b1_hash); assert_eq!(r3b_1.ancestor, b1_hash);
assert_eq!(r3b_1.blocks, [b3b_hash.clone(), b2_hash.clone()]); assert_eq!(r3b_1.blocks, [b3b_hash.clone(), b2_hash.clone()]);
assert_eq!(r3b_1.index, 2); assert_eq!(r3b_1.index, 2);
let r3b_3a = bc.tree_route(b3b_hash.clone(), b3a_hash.clone()); let r3b_3a = bc.tree_route(b3b_hash.clone(), b3a_hash.clone()).unwrap();
assert_eq!(r3b_3a.ancestor, b2_hash); assert_eq!(r3b_3a.ancestor, b2_hash);
assert_eq!(r3b_3a.blocks, [b3b_hash.clone(), b3a_hash.clone()]); assert_eq!(r3b_3a.blocks, [b3b_hash.clone(), b3a_hash.clone()]);
assert_eq!(r3b_3a.index, 1); assert_eq!(r3b_3a.index, 1);

View File

@ -12,6 +12,11 @@ pub struct Builtin {
pub execute: Box<Fn(&[u8], &mut [u8])>, pub execute: Box<Fn(&[u8], &mut [u8])>,
} }
// Rust does not mark closurer that do not capture as Sync
// We promise that all builtins are thread safe since they only operate on given input.
unsafe impl Sync for Builtin {}
unsafe impl Send for Builtin {}
impl fmt::Debug for Builtin { impl fmt::Debug for Builtin {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<Builtin>") write!(f, "<Builtin>")

View File

@ -1,22 +1,17 @@
use std::sync::Arc;
use util::*; use util::*;
use blockchain::BlockChain; use blockchain::BlockChain;
use views::BlockView; use views::BlockView;
use error::*;
/// Status for a block in a queue. use header::BlockNumber;
pub enum QueueStatus { use spec::Spec;
/// Part of the known chain. use engine::Engine;
Known,
/// Part of the unknown chain.
Unknown,
}
/// General block status /// General block status
pub enum BlockStatus { pub enum BlockStatus {
/// Part of the blockchain. /// Part of the blockchain.
InChain, InChain,
/// Queued for import. /// Queued for import.
Queued(QueueStatus), Queued,
/// Known as bad. /// Known as bad.
Bad, Bad,
/// Unknown. /// Unknown.
@ -24,16 +19,7 @@ pub enum BlockStatus {
} }
/// Result of import block operation. /// Result of import block operation.
pub enum ImportResult { pub type ImportResult = Result<(), ImportError>;
/// Added to import queue.
Queued(QueueStatus),
/// Already in the chain.
AlreadyInChain,
/// Already queued for import.
AlreadyQueued(QueueStatus),
/// Bad or already known as bad.
Bad,
}
/// Information about the blockchain gthered together. /// Information about the blockchain gthered together.
pub struct BlockChainInfo { pub struct BlockChainInfo {
@ -56,8 +42,6 @@ pub struct BlockQueueStatus {
pub type TreeRoute = ::blockchain::TreeRoute; pub type TreeRoute = ::blockchain::TreeRoute;
pub type BlockNumber = u64;
/// Blockchain database client. Owns and manages a blockchain and a block queue. /// Blockchain database client. Owns and manages a blockchain and a block queue.
pub trait BlockChainClient : Sync { pub trait BlockChainClient : Sync {
/// Get raw block header data by block header hash. /// Get raw block header data by block header hash.
@ -88,7 +72,7 @@ pub trait BlockChainClient : Sync {
/// Get a tree route between `from` and `to`. /// Get a tree route between `from` and `to`.
/// See `BlockChain::tree_route`. /// See `BlockChain::tree_route`.
fn tree_route(&self, from: &H256, to: &H256) -> TreeRoute; fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute>;
/// Get latest state node /// Get latest state node
fn state_data(&self, hash: &H256) -> Option<Bytes>; fn state_data(&self, hash: &H256) -> Option<Bytes>;
@ -112,14 +96,17 @@ pub trait BlockChainClient : Sync {
/// Blockchain database client backed by a persistent database. Owns and manages a blockchain and a block queue. /// Blockchain database client backed by a persistent database. Owns and manages a blockchain and a block queue.
pub struct Client { pub struct Client {
chain: Arc<BlockChain>, chain: Arc<BlockChain>,
_engine: Arc<Box<Engine>>,
} }
impl Client { impl Client {
pub fn new(genesis: &[u8], path: &Path) -> Client { pub fn new(spec: Spec, path: &Path) -> Result<Client, Error> {
let chain = Arc::new(BlockChain::new(genesis, path)); let chain = Arc::new(BlockChain::new(&spec.genesis_block(), path));
Client { let engine = Arc::new(try!(spec.to_engine()));
Ok(Client {
chain: chain.clone(), chain: chain.clone(),
} _engine: engine,
})
} }
} }
@ -147,25 +134,25 @@ impl BlockChainClient for Client {
} }
fn block_header_at(&self, n: BlockNumber) -> Option<Bytes> { fn block_header_at(&self, n: BlockNumber) -> Option<Bytes> {
self.chain.block_hash(&From::from(n)).and_then(|h| self.block_header(&h)) self.chain.block_hash(n).and_then(|h| self.block_header(&h))
} }
fn block_body_at(&self, n: BlockNumber) -> Option<Bytes> { fn block_body_at(&self, n: BlockNumber) -> Option<Bytes> {
self.chain.block_hash(&From::from(n)).and_then(|h| self.block_body(&h)) self.chain.block_hash(n).and_then(|h| self.block_body(&h))
} }
fn block_at(&self, n: BlockNumber) -> Option<Bytes> { fn block_at(&self, n: BlockNumber) -> Option<Bytes> {
self.chain.block_hash(&From::from(n)).and_then(|h| self.block(&h)) self.chain.block_hash(n).and_then(|h| self.block(&h))
} }
fn block_status_at(&self, n: BlockNumber) -> BlockStatus { fn block_status_at(&self, n: BlockNumber) -> BlockStatus {
match self.chain.block_hash(&From::from(n)) { match self.chain.block_hash(n) {
Some(h) => self.block_status(&h), Some(h) => self.block_status(&h),
None => BlockStatus::Unknown None => BlockStatus::Unknown
} }
} }
fn tree_route(&self, from: &H256, to: &H256) -> TreeRoute { fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute> {
self.chain.tree_route(from.clone(), to.clone()) self.chain.tree_route(from.clone(), to.clone())
} }
@ -184,11 +171,11 @@ impl BlockChainClient for Client {
let header = block.header_view(); let header = block.header_view();
let hash = header.sha3(); let hash = header.sha3();
if self.chain.is_known(&hash) { if self.chain.is_known(&hash) {
return ImportResult::Bad; return Err(ImportError::AlreadyInChain);
} }
} }
self.chain.insert_block(bytes); self.chain.insert_block(bytes);
ImportResult::Queued(QueueStatus::Known) Ok(())
} }
fn queue_status(&self) -> BlockQueueStatus { fn queue_status(&self) -> BlockQueueStatus {

View File

@ -4,7 +4,7 @@ use spec::Spec;
/// A consensus mechanism for the chain. Generally either proof-of-work or proof-of-stake-based. /// A consensus mechanism for the chain. Generally either proof-of-work or proof-of-stake-based.
/// Provides hooks into each of the major parts of block import. /// Provides hooks into each of the major parts of block import.
pub trait Engine { pub trait Engine : Sync + Send {
/// The name of this engine. /// The name of this engine.
fn name(&self) -> &str; fn name(&self) -> &str;
/// The version of this engine. Should be of the form /// The version of this engine. Should be of the form

View File

@ -1,4 +1,5 @@
use util::*; use util::*;
use header::BlockNumber;
/// Simple vector of hashes, should be at most 256 items large, can be smaller if being used /// Simple vector of hashes, should be at most 256 items large, can be smaller if being used
/// for a block whose number is less than 257. /// for a block whose number is less than 257.
@ -7,11 +8,11 @@ pub type LastHashes = Vec<H256>;
/// Information concerning the execution environment for a message-call/contract-creation. /// Information concerning the execution environment for a message-call/contract-creation.
pub struct EnvInfo { pub struct EnvInfo {
/// The block number. /// The block number.
pub number: U256, pub number: BlockNumber,
/// The block author. /// The block author.
pub author: Address, pub author: Address,
/// The block timestamp. /// The block timestamp.
pub timestamp: U256, pub timestamp: u64,
/// The block difficulty. /// The block difficulty.
pub difficulty: U256, pub difficulty: U256,
/// The block gas limit. /// The block gas limit.
@ -20,4 +21,4 @@ pub struct EnvInfo {
pub last_hashes: LastHashes, pub last_hashes: LastHashes,
/// The gas used. /// The gas used.
pub gas_used: U256, pub gas_used: U256,
} }

View File

@ -1,6 +1,7 @@
//! General error types for use in ethcore. //! General error types for use in ethcore.
use util::*; use util::*;
use header::BlockNumber;
#[derive(Debug)] #[derive(Debug)]
pub struct Mismatch<T: fmt::Debug> { pub struct Mismatch<T: fmt::Debug> {
@ -23,8 +24,8 @@ pub enum BlockError {
InvalidSealArity(Mismatch<usize>), InvalidSealArity(Mismatch<usize>),
TooMuchGasUsed(OutOfBounds<U256>), TooMuchGasUsed(OutOfBounds<U256>),
InvalidUnclesHash(Mismatch<H256>), InvalidUnclesHash(Mismatch<H256>),
UncleTooOld(OutOfBounds<U256>), UncleTooOld(OutOfBounds<BlockNumber>),
UncleIsBrother(OutOfBounds<U256>), UncleIsBrother(OutOfBounds<BlockNumber>),
UncleInChain(H256), UncleInChain(H256),
UncleParentNotInChain(H256), UncleParentNotInChain(H256),
InvalidStateRoot, InvalidStateRoot,
@ -33,15 +34,22 @@ pub enum BlockError {
InvalidDifficulty(Mismatch<U256>), InvalidDifficulty(Mismatch<U256>),
InvalidGasLimit(OutOfBounds<U256>), InvalidGasLimit(OutOfBounds<U256>),
InvalidReceiptsStateRoot, InvalidReceiptsStateRoot,
InvalidTimestamp(OutOfBounds<U256>), InvalidTimestamp(OutOfBounds<u64>),
InvalidLogBloom, InvalidLogBloom,
InvalidBlockNonce, InvalidBlockNonce,
InvalidParentHash(Mismatch<H256>), InvalidParentHash(Mismatch<H256>),
InvalidNumber(OutOfBounds<U256>), InvalidNumber(OutOfBounds<BlockNumber>),
UnknownParent(H256), UnknownParent(H256),
UnknownUncleParent(H256), UnknownUncleParent(H256),
} }
#[derive(Debug)]
pub enum ImportError {
Bad(BlockError),
AlreadyInChain,
AlreadyQueued,
}
#[derive(Debug)] #[derive(Debug)]
/// General error type which should be capable of representing all errors in ethcore. /// General error type which should be capable of representing all errors in ethcore.
pub enum Error { pub enum Error {

View File

@ -17,13 +17,31 @@ impl Ethash {
impl Engine for Ethash { impl Engine for Ethash {
fn name(&self) -> &str { "Ethash" } fn name(&self) -> &str { "Ethash" }
fn version(&self) -> SemanticVersion { SemanticVersion::new(1, 0, 0) }
// Two fields - mix
fn seal_fields(&self) -> usize { 2 }
// Two empty data items in RLP.
fn seal_rlp(&self) -> Bytes { encode(&H64::new()) }
/// Additional engine-specific information for the user/developer concerning `header`.
fn extra_info(&self, _header: &Header) -> HashMap<String, String> { HashMap::new() }
fn spec(&self) -> &Spec { &self.spec } fn spec(&self) -> &Spec { &self.spec }
fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() } fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() }
/// Apply the block reward on finalisation of the block. /// Apply the block reward on finalisation of the block.
/// This assumes that all uncles are valid uncles (i.e. of at least one generation before the current).
fn on_close_block(&self, block: &mut Block) { fn on_close_block(&self, block: &mut Block) {
let a = block.header().author.clone(); let reward = self.spec().engine_params.get("blockReward").map(|a| decode(&a)).unwrap_or(U256::from(0u64));
block.state_mut().add_balance(&a, &decode(&self.spec().engine_params.get("blockReward").unwrap())); let fields = block.fields();
// Bestow block reward
fields.state.add_balance(&fields.header.author, &(reward + reward / U256::from(32) * U256::from(fields.uncles.len())));
// Bestow uncle rewards
let current_number = fields.header.number();
for u in fields.uncles.iter() {
fields.state.add_balance(u.author(), &(reward * U256::from((8 + u.number() - current_number) / 8)));
}
} }
@ -37,7 +55,7 @@ impl Engine for Ethash {
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { min: min_gas_limit, max: From::from(0), found: header.gas_limit }))); return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { min: min_gas_limit, max: From::from(0), found: header.gas_limit })));
} }
let maximum_extra_data_size = self.maximum_extra_data_size(); let maximum_extra_data_size = self.maximum_extra_data_size();
if header.number != From::from(0) && header.extra_data.len() > maximum_extra_data_size { if header.number != 0 && header.extra_data.len() > maximum_extra_data_size {
return Err(From::from(BlockError::ExtraDataOutOfBounds(OutOfBounds { min: 0, max: maximum_extra_data_size, found: header.extra_data.len() }))); return Err(From::from(BlockError::ExtraDataOutOfBounds(OutOfBounds { min: 0, max: maximum_extra_data_size, found: header.extra_data.len() })));
} }
// TODO: Verify seal (quick) // TODO: Verify seal (quick)
@ -70,13 +88,13 @@ impl Engine for Ethash {
impl Ethash { impl Ethash {
fn calculate_difficuty(&self, header: &Header, parent: &Header) -> U256 { fn calculate_difficuty(&self, header: &Header, parent: &Header) -> U256 {
const EXP_DIFF_PERIOD: u64 = 100000; const EXP_DIFF_PERIOD: u64 = 100000;
if header.number == From::from(0) { if header.number == 0 {
panic!("Can't calculate genesis block difficulty"); panic!("Can't calculate genesis block difficulty");
} }
let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap()); let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap());
let difficulty_bound_divisor = decode(self.spec().engine_params.get("difficultyBoundDivisor").unwrap()); let difficulty_bound_divisor = decode(self.spec().engine_params.get("difficultyBoundDivisor").unwrap());
let duration_limit = decode(self.spec().engine_params.get("durationLimit").unwrap()); let duration_limit: u64 = decode(self.spec().engine_params.get("durationLimit").unwrap());
let frontier_limit = decode(self.spec().engine_params.get("frontierCompatibilityModeLimit").unwrap()); let frontier_limit = decode(self.spec().engine_params.get("frontierCompatibilityModeLimit").unwrap());
let mut target = if header.number < frontier_limit { let mut target = if header.number < frontier_limit {
if header.timestamp >= parent.timestamp + duration_limit { if header.timestamp >= parent.timestamp + duration_limit {
@ -87,16 +105,16 @@ impl Ethash {
} }
} }
else { else {
let diff_inc = (header.timestamp - parent.timestamp) / From::from(10); let diff_inc = (header.timestamp - parent.timestamp) / 10;
if diff_inc <= From::from(1) { if diff_inc <= 1 {
parent.difficulty + parent.difficulty / From::from(2048) * (U256::from(1) - diff_inc) parent.difficulty + parent.difficulty / From::from(2048) * From::from(1 - diff_inc)
} }
else { else {
parent.difficulty - parent.difficulty / From::from(2048) * max(diff_inc - From::from(1), From::from(99)) parent.difficulty - parent.difficulty / From::from(2048) * From::from(max(diff_inc - 1, 99))
} }
}; };
target = max(min_difficulty, target); target = max(min_difficulty, target);
let period = ((parent.number + From::from(1)).as_u64() / EXP_DIFF_PERIOD) as usize; let period = ((parent.number + 1) / EXP_DIFF_PERIOD) as usize;
if period > 1 { if period > 1 {
target = max(min_difficulty, target + (U256::from(1) << (period - 2))); target = max(min_difficulty, target + (U256::from(1) << (period - 2)));
} }
@ -111,9 +129,10 @@ fn on_close_block() {
let genesis_header = engine.spec().genesis_header(); let genesis_header = engine.spec().genesis_header();
let mut db = OverlayDB::new_temp(); let mut db = OverlayDB::new_temp();
engine.spec().ensure_db_good(&mut db); engine.spec().ensure_db_good(&mut db);
let b = OpenBlock::new(engine.deref(), db, &genesis_header, vec![genesis_header.hash()], Address::zero(), vec![]); let last_hashes = vec![genesis_header.hash()];
let b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]);
let b = b.close(); let b = b.close();
assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244F40000").unwrap()); assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244f40000").unwrap());
} }
// TODO: difficulty test // TODO: difficulty test

View File

@ -52,7 +52,7 @@ mod tests {
fn morden() { fn morden() {
let morden = new_morden(); let morden = new_morden();
assert_eq!(*morden.state_root(), H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap()); assert_eq!(morden.state_root(), H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap());
let genesis = morden.genesis_block(); let genesis = morden.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap()); assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap());
@ -63,10 +63,10 @@ mod tests {
fn frontier() { fn frontier() {
let frontier = new_frontier(); let frontier = new_frontier();
assert_eq!(*frontier.state_root(), H256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap()); assert_eq!(frontier.state_root(), H256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap());
let genesis = frontier.genesis_block(); let genesis = frontier.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap()); assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap());
let _ = frontier.to_engine(); let _ = frontier.to_engine();
} }
} }

View File

@ -1,4 +1,5 @@
use util::*; use util::*;
use header::BlockNumber;
use rocksdb::{DB, Writable}; use rocksdb::{DB, Writable};
/// Represents index of extra data in database /// Represents index of extra data in database
@ -74,6 +75,13 @@ impl ExtrasSliceConvertable for U256 {
} }
} }
// NICE: make less horrible.
impl ExtrasSliceConvertable for BlockNumber {
fn to_extras_slice(&self, i: ExtrasIndex) -> H264 {
U256::from(*self).to_extras_slice(i)
}
}
/// Types implementing this trait can be indexed in extras database /// Types implementing this trait can be indexed in extras database
pub trait ExtrasIndexable { pub trait ExtrasIndexable {
fn extras_index() -> ExtrasIndex; fn extras_index() -> ExtrasIndex;
@ -88,7 +96,7 @@ impl ExtrasIndexable for H256 {
/// Familial details concerning a block /// Familial details concerning a block
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct BlockDetails { pub struct BlockDetails {
pub number: U256, pub number: BlockNumber,
pub total_difficulty: U256, pub total_difficulty: U256,
pub parent: H256, pub parent: H256,
pub children: Vec<H256> pub children: Vec<H256>

View File

@ -1,5 +1,8 @@
use util::*; use util::*;
use basic_types::*; use basic_types::*;
use time::now_utc;
pub type BlockNumber = u64;
/// A block header. /// A block header.
/// ///
@ -11,8 +14,8 @@ use basic_types::*;
pub struct Header { pub struct Header {
// TODO: make all private. // TODO: make all private.
pub parent_hash: H256, pub parent_hash: H256,
pub timestamp: U256, pub timestamp: u64,
pub number: U256, pub number: BlockNumber,
pub author: Address, pub author: Address,
pub transactions_root: H256, pub transactions_root: H256,
@ -41,8 +44,8 @@ impl Header {
pub fn new() -> Header { pub fn new() -> Header {
Header { Header {
parent_hash: ZERO_H256.clone(), parent_hash: ZERO_H256.clone(),
timestamp: BAD_U256, timestamp: 0,
number: ZERO_U256, number: 0,
author: ZERO_ADDRESS.clone(), author: ZERO_ADDRESS.clone(),
transactions_root: SHA3_NULL_RLP, transactions_root: SHA3_NULL_RLP,
@ -61,14 +64,23 @@ impl Header {
} }
} }
pub fn number(&self) -> BlockNumber { self.number }
pub fn timestamp(&self) -> u64 { self.timestamp }
pub fn author(&self) -> &Address { &self.author } pub fn author(&self) -> &Address { &self.author }
pub fn extra_data(&self) -> &Bytes { &self.extra_data } pub fn extra_data(&self) -> &Bytes { &self.extra_data }
pub fn seal(&self) -> &Vec<Bytes> { &self.seal } pub fn seal(&self) -> &Vec<Bytes> { &self.seal }
// TODO: seal_at, set_seal_at &c. // TODO: seal_at, set_seal_at &c.
pub fn set_number(&mut self, a: BlockNumber) { self.number = a; self.note_dirty(); }
pub fn set_timestamp(&mut self, a: u64) { self.timestamp = a; self.note_dirty(); }
pub fn set_timestamp_now(&mut self) { self.timestamp = now_utc().to_timespec().sec as u64; self.note_dirty(); }
pub fn set_author(&mut self, a: Address) { if a != self.author { self.author = a; self.note_dirty(); } } pub fn set_author(&mut self, a: Address) { if a != self.author { self.author = a; self.note_dirty(); } }
pub fn set_extra_data(&mut self, a: Bytes) { if a != self.extra_data { self.extra_data = a; self.note_dirty(); } } pub fn set_extra_data(&mut self, a: Bytes) { if a != self.extra_data { self.extra_data = a; self.note_dirty(); } }
pub fn set_seal(&mut self, a: Vec<Bytes>) { self.seal = a; self.note_dirty(); } pub fn set_seal(&mut self, a: Vec<Bytes>) { self.seal = a; self.note_dirty(); }
/// Get the hash of this header (sha3 of the RLP). /// Get the hash of this header (sha3 of the RLP).

View File

@ -78,6 +78,7 @@ extern crate flate2;
extern crate rocksdb; extern crate rocksdb;
extern crate heapsize; extern crate heapsize;
extern crate crypto; extern crate crypto;
extern crate time;
extern crate env_logger; extern crate env_logger;
#[cfg(feature = "jit" )] #[cfg(feature = "jit" )]

View File

@ -1,26 +1,24 @@
use std::sync::Arc; use std::sync::Arc;
//use util::bytes::*; use util::*;
use util::sha3::*;
use blockchain::BlockChain; use blockchain::BlockChain;
use client::{QueueStatus, ImportResult}; use client::{QueueStatus, ImportResult};
use views::{BlockView}; use views::{BlockView};
/// A queue of blocks. Sits between network or other I/O and the BlockChain.
pub struct BlockQueue { /// Sorts them ready for blockchain insertion.
chain: Arc<BlockChain> pub struct BlockQueue;
}
impl BlockQueue { impl BlockQueue {
pub fn new(chain: Arc<BlockChain>) -> BlockQueue { /// Creates a new queue instance.
BlockQueue { pub fn new() -> BlockQueue {
chain: chain
}
} }
/// Clear the queue and stop verification activity.
pub fn clear(&mut self) { pub fn clear(&mut self) {
} }
pub fn import_block(&mut self, bytes: &[u8]) -> ImportResult { /// Add a block to the queue.
pub fn import_block(&mut self, bytes: &[u8], bc: &mut BlockChain) -> ImportResult {
//TODO: verify block //TODO: verify block
{ {
let block = BlockView::new(bytes); let block = BlockView::new(bytes);
@ -30,7 +28,7 @@ impl BlockQueue {
return ImportResult::Bad; return ImportResult::Bad;
} }
} }
self.chain.insert_block(bytes); bc.insert_block(bytes);
ImportResult::Queued(QueueStatus::Known) ImportResult::Queued(QueueStatus::Known)
} }
} }

View File

@ -40,6 +40,27 @@ fn json_to_rlp_map(json: &Json) -> HashMap<String, Bytes> {
}) })
} }
//TODO: add code and data
#[derive(Debug)]
/// Genesis account data. Does no thave a DB overlay cache
pub struct GenesisAccount {
// Balance of the account.
balance: U256,
// Nonce of the account.
nonce: U256,
}
impl GenesisAccount {
pub fn rlp(&self) -> Bytes {
let mut stream = RlpStream::new_list(4);
stream.append(&self.nonce);
stream.append(&self.balance);
stream.append(&SHA3_NULL_RLP);
stream.append(&SHA3_EMPTY);
stream.out()
}
}
/// Parameters for a block chain; includes both those intrinsic to the design of the /// Parameters for a block chain; includes both those intrinsic to the design of the
/// chain and those to be interpreted by the active chain engine. /// chain and those to be interpreted by the active chain engine.
#[derive(Debug)] #[derive(Debug)]
@ -60,14 +81,14 @@ pub struct Spec {
pub difficulty: U256, pub difficulty: U256,
pub gas_limit: U256, pub gas_limit: U256,
pub gas_used: U256, pub gas_used: U256,
pub timestamp: U256, pub timestamp: u64,
pub extra_data: Bytes, pub extra_data: Bytes,
pub genesis_state: HashMap<Address, Account>, pub genesis_state: HashMap<Address, GenesisAccount>,
pub seal_fields: usize, pub seal_fields: usize,
pub seal_rlp: Bytes, pub seal_rlp: Bytes,
// May be prepopulated if we know this in advance. // May be prepopulated if we know this in advance.
state_root_memo: RefCell<Option<H256>>, state_root_memo: RwLock<Option<H256>>,
} }
impl Spec { impl Spec {
@ -82,18 +103,18 @@ impl Spec {
} }
/// Return the state root for the genesis state, memoising accordingly. /// Return the state root for the genesis state, memoising accordingly.
pub fn state_root(&self) -> Ref<H256> { pub fn state_root(&self) -> H256 {
if self.state_root_memo.borrow().is_none() { if self.state_root_memo.read().unwrap().is_none() {
*self.state_root_memo.borrow_mut() = Some(sec_trie_root(self.genesis_state.iter().map(|(k, v)| (k.to_vec(), v.rlp())).collect())); *self.state_root_memo.write().unwrap() = Some(sec_trie_root(self.genesis_state.iter().map(|(k, v)| (k.to_vec(), v.rlp())).collect()));
} }
Ref::map(self.state_root_memo.borrow(), |x|x.as_ref().unwrap()) self.state_root_memo.read().unwrap().as_ref().unwrap().clone()
} }
pub fn genesis_header(&self) -> Header { pub fn genesis_header(&self) -> Header {
Header { Header {
parent_hash: self.parent_hash.clone(), parent_hash: self.parent_hash.clone(),
timestamp: self.timestamp.clone(), timestamp: self.timestamp,
number: U256::from(0u8), number: 0,
author: self.author.clone(), author: self.author.clone(),
transactions_root: SHA3_NULL_RLP.clone(), transactions_root: SHA3_NULL_RLP.clone(),
uncles_hash: RlpStream::new_list(0).out().sha3(), uncles_hash: RlpStream::new_list(0).out().sha3(),
@ -113,7 +134,7 @@ impl Spec {
let r = Rlp::new(&seal); let r = Rlp::new(&seal);
(0..self.seal_fields).map(|i| r.at(i).as_raw().to_vec()).collect() (0..self.seal_fields).map(|i| r.at(i).as_raw().to_vec()).collect()
}, },
hash: RefCell::new(None) hash: RefCell::new(None),
} }
} }
@ -149,7 +170,7 @@ impl Spec {
// let nonce = if let Some(&Json::String(ref n)) = acc.find("nonce") {U256::from_dec_str(n).unwrap_or(U256::from(0))} else {U256::from(0)}; // let nonce = if let Some(&Json::String(ref n)) = acc.find("nonce") {U256::from_dec_str(n).unwrap_or(U256::from(0))} else {U256::from(0)};
// TODO: handle code & data if they exist. // TODO: handle code & data if they exist.
if balance.is_some() || nonce.is_some() { if balance.is_some() || nonce.is_some() {
state.insert(addr, Account::new_basic(balance.unwrap_or(U256::from(0)), nonce.unwrap_or(U256::from(0)))); state.insert(addr, GenesisAccount { balance: balance.unwrap_or(U256::from(0)), nonce: nonce.unwrap_or(U256::from(0)) });
} }
} }
} }
@ -181,12 +202,12 @@ impl Spec {
difficulty: U256::from_str(&genesis["difficulty"].as_string().unwrap()[2..]).unwrap(), difficulty: U256::from_str(&genesis["difficulty"].as_string().unwrap()[2..]).unwrap(),
gas_limit: U256::from_str(&genesis["gasLimit"].as_string().unwrap()[2..]).unwrap(), gas_limit: U256::from_str(&genesis["gasLimit"].as_string().unwrap()[2..]).unwrap(),
gas_used: U256::from(0u8), gas_used: U256::from(0u8),
timestamp: U256::from_str(&genesis["timestamp"].as_string().unwrap()[2..]).unwrap(), timestamp: u64::from_str(&genesis["timestamp"].as_string().unwrap()[2..]).unwrap(),
extra_data: genesis["extraData"].as_string().unwrap()[2..].from_hex().unwrap(), extra_data: genesis["extraData"].as_string().unwrap()[2..].from_hex().unwrap(),
genesis_state: state, genesis_state: state,
seal_fields: seal_fields, seal_fields: seal_fields,
seal_rlp: seal_rlp, seal_rlp: seal_rlp,
state_root_memo: RefCell::new(genesis.find("stateRoot").and_then(|_| genesis["stateRoot"].as_string()).map(|s| H256::from_str(&s[2..]).unwrap())), state_root_memo: RwLock::new(genesis.find("stateRoot").and_then(|_| genesis["stateRoot"].as_string()).map(|s| H256::from_str(&s[2..]).unwrap())),
} }
} }
@ -228,7 +249,7 @@ mod tests {
fn test_chain() { fn test_chain() {
let test_spec = Spec::new_test(); let test_spec = Spec::new_test();
assert_eq!(*test_spec.state_root(), H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap()); assert_eq!(test_spec.state_root(), H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap());
let genesis = test_spec.genesis_block(); let genesis = test_spec.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap()); assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap());

View File

@ -16,9 +16,10 @@
use util::*; use util::*;
use std::mem::{replace}; use std::mem::{replace};
use views::{HeaderView}; use views::{HeaderView};
use header::{Header as BlockHeader}; use header::{BlockNumber, Header as BlockHeader};
use client::{BlockNumber, BlockChainClient, BlockStatus, QueueStatus, ImportResult}; use client::{BlockChainClient, BlockStatus};
use sync::range_collection::{RangeCollection, ToUsize, FromUsize}; use sync::range_collection::{RangeCollection, ToUsize, FromUsize};
use error::*;
use sync::io::SyncIo; use sync::io::SyncIo;
impl ToUsize for BlockNumber { impl ToUsize for BlockNumber {
@ -76,12 +77,13 @@ struct HeaderId {
} }
#[derive(Copy, Clone, Eq, PartialEq, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Debug)]
/// Sync state
pub enum SyncState { pub enum SyncState {
/// Initial chain sync has not started yet /// Initial chain sync has not started yet
NotSynced, NotSynced,
/// Initial chain sync complete. Waiting for new packets /// Initial chain sync complete. Waiting for new packets
Idle, Idle,
/// Block downloading paused. Waiting for block queue to process blocks and free space /// Block downloading paused. Waiting for block queue to process blocks and free some space
Waiting, Waiting,
/// Downloading blocks /// Downloading blocks
Blocks, Blocks,
@ -108,24 +110,33 @@ pub struct SyncStatus {
} }
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
/// Peer data type requested
enum PeerAsking { enum PeerAsking {
Nothing, Nothing,
BlockHeaders, BlockHeaders,
BlockBodies, BlockBodies,
} }
/// Syncing peer information
struct PeerInfo { struct PeerInfo {
/// eth protocol version
protocol_version: u32, protocol_version: u32,
/// Peer chain genesis hash
genesis: H256, genesis: H256,
/// Peer network id
network_id: U256, network_id: U256,
/// Peer best block hash
latest: H256, latest: H256,
/// Peer total difficulty
difficulty: U256, difficulty: U256,
/// Type of data currenty being requested from peer.
asking: PeerAsking, asking: PeerAsking,
/// A set of block numbers being requested
asking_blocks: Vec<BlockNumber>, asking_blocks: Vec<BlockNumber>,
} }
type Body = Bytes; /// Blockchain sync handler.
/// See module documentation for more details.
pub struct ChainSync { pub struct ChainSync {
/// Sync state /// Sync state
state: SyncState, state: SyncState,
@ -140,7 +151,7 @@ pub struct ChainSync {
/// Downloaded headers. /// Downloaded headers.
headers: Vec<(BlockNumber, Vec<Header>)>, //TODO: use BTreeMap once range API is sable. For now it is a vector sorted in descending order headers: Vec<(BlockNumber, Vec<Header>)>, //TODO: use BTreeMap once range API is sable. For now it is a vector sorted in descending order
/// Downloaded bodies /// Downloaded bodies
bodies: Vec<(BlockNumber, Vec<Body>)>, //TODO: use BTreeMap once range API is sable. For now it is a vector sorted in descending order bodies: Vec<(BlockNumber, Vec<Bytes>)>, //TODO: use BTreeMap once range API is sable. For now it is a vector sorted in descending order
/// Peer info /// Peer info
peers: HashMap<PeerId, PeerInfo>, peers: HashMap<PeerId, PeerInfo>,
/// Used to map body to header /// Used to map body to header
@ -390,31 +401,31 @@ impl ChainSync {
// TODO: Decompose block and add to self.headers and self.bodies instead // TODO: Decompose block and add to self.headers and self.bodies instead
if header_view.number() == From::from(self.last_imported_block + 1) { if header_view.number() == From::from(self.last_imported_block + 1) {
match io.chain().import_block(block_rlp.as_raw()) { match io.chain().import_block(block_rlp.as_raw()) {
ImportResult::AlreadyInChain => { Err(ImportError::AlreadyInChain) => {
trace!(target: "sync", "New block already in chain {:?}", h); trace!(target: "sync", "New block already in chain {:?}", h);
}, },
ImportResult::AlreadyQueued(_) => { Err(ImportError::AlreadyQueued) => {
trace!(target: "sync", "New block already queued {:?}", h); trace!(target: "sync", "New block already queued {:?}", h);
}, },
ImportResult::Queued(QueueStatus::Known) => { Ok(()) => {
trace!(target: "sync", "New block queued {:?}", h); trace!(target: "sync", "New block queued {:?}", h);
}, },
ImportResult::Queued(QueueStatus::Unknown) => { Err(e) => {
trace!(target: "sync", "New block unknown {:?}", h); debug!(target: "sync", "Bad new block {:?} : {:?}", h, e);
//TODO: handle too many unknown blocks
let difficulty: U256 = try!(r.val_at(1));
let peer_difficulty = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer").difficulty;
if difficulty > peer_difficulty {
trace!(target: "sync", "Received block {:?} with no known parent. Peer needs syncing...", h);
self.sync_peer(io, peer_id, true);
}
},
ImportResult::Bad =>{
debug!(target: "sync", "Bad new block {:?}", h);
io.disable_peer(peer_id); io.disable_peer(peer_id);
} }
}; };
} }
else {
trace!(target: "sync", "New block unknown {:?}", h);
//TODO: handle too many unknown blocks
let difficulty: U256 = try!(r.val_at(1));
let peer_difficulty = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer").difficulty;
if difficulty > peer_difficulty {
trace!(target: "sync", "Received block {:?} with no known parent. Peer needs syncing...", h);
self.sync_peer(io, peer_id, true);
}
}
Ok(()) Ok(())
} }
@ -434,7 +445,7 @@ impl ChainSync {
BlockStatus::InChain => { BlockStatus::InChain => {
trace!(target: "sync", "New block hash already in chain {:?}", h); trace!(target: "sync", "New block hash already in chain {:?}", h);
}, },
BlockStatus::Queued(_) => { BlockStatus::Queued => {
trace!(target: "sync", "New hash block already queued {:?}", h); trace!(target: "sync", "New hash block already queued {:?}", h);
}, },
BlockStatus::Unknown => { BlockStatus::Unknown => {
@ -642,27 +653,24 @@ impl ChainSync {
block_rlp.append_raw(body.at(1).as_raw(), 1); block_rlp.append_raw(body.at(1).as_raw(), 1);
let h = &headers.1[i].hash; let h = &headers.1[i].hash;
match io.chain().import_block(&block_rlp.out()) { match io.chain().import_block(&block_rlp.out()) {
ImportResult::AlreadyInChain => { Err(ImportError::AlreadyInChain) => {
trace!(target: "sync", "Block already in chain {:?}", h); trace!(target: "sync", "Block already in chain {:?}", h);
self.last_imported_block = headers.0 + i as BlockNumber; self.last_imported_block = headers.0 + i as BlockNumber;
self.last_imported_hash = h.clone(); self.last_imported_hash = h.clone();
}, },
ImportResult::AlreadyQueued(_) => { Err(ImportError::AlreadyQueued) => {
trace!(target: "sync", "Block already queued {:?}", h); trace!(target: "sync", "Block already queued {:?}", h);
self.last_imported_block = headers.0 + i as BlockNumber; self.last_imported_block = headers.0 + i as BlockNumber;
self.last_imported_hash = h.clone(); self.last_imported_hash = h.clone();
}, },
ImportResult::Queued(QueueStatus::Known) => { Ok(()) => {
trace!(target: "sync", "Block queued {:?}", h); trace!(target: "sync", "Block queued {:?}", h);
self.last_imported_block = headers.0 + i as BlockNumber; self.last_imported_block = headers.0 + i as BlockNumber;
self.last_imported_hash = h.clone(); self.last_imported_hash = h.clone();
imported += 1; imported += 1;
}, },
ImportResult::Queued(QueueStatus::Unknown) => { Err(e) => {
panic!("Queued out of order block"); debug!(target: "sync", "Bad block {:?} : {:?}", h, e);
},
ImportResult::Bad =>{
debug!(target: "sync", "Bad block {:?}", h);
restart = true; restart = true;
} }
} }

View File

@ -2,19 +2,28 @@ use client::BlockChainClient;
use util::network::{HandlerIo, PeerId, PacketId,}; use util::network::{HandlerIo, PeerId, PacketId,};
use util::error::UtilError; use util::error::UtilError;
/// IO interface for the syning handler.
/// Provides peer connection management and an interface to the blockchain client.
// TODO: ratings
pub trait SyncIo { pub trait SyncIo {
/// Disable a peer
fn disable_peer(&mut self, peer_id: &PeerId); fn disable_peer(&mut self, peer_id: &PeerId);
/// Respond to current request with a packet. Can be called from an IO handler for incoming packet.
fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError>; fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError>;
/// Send a packet to a peer.
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError>; fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError>;
/// Get the blockchain
fn chain<'s>(&'s mut self) -> &'s mut BlockChainClient; fn chain<'s>(&'s mut self) -> &'s mut BlockChainClient;
} }
/// Wraps `HandlerIo` and the blockchain client
pub struct NetSyncIo<'s, 'h> where 'h:'s { pub struct NetSyncIo<'s, 'h> where 'h:'s {
network: &'s mut HandlerIo<'h>, network: &'s mut HandlerIo<'h>,
chain: &'s mut BlockChainClient chain: &'s mut BlockChainClient
} }
impl<'s, 'h> NetSyncIo<'s, 'h> { impl<'s, 'h> NetSyncIo<'s, 'h> {
/// Creates a new instance from the `HandlerIo` and the blockchain client reference.
pub fn new(network: &'s mut HandlerIo<'h>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h> { pub fn new(network: &'s mut HandlerIo<'h>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h> {
NetSyncIo { NetSyncIo {
network: network, network: network,

View File

@ -16,9 +16,8 @@
/// ///
/// fn main() { /// fn main() {
/// let mut service = NetworkService::start().unwrap(); /// let mut service = NetworkService::start().unwrap();
/// let frontier = ethereum::new_frontier();
/// let dir = env::temp_dir(); /// let dir = env::temp_dir();
/// let client = Arc::new(Client::new(&frontier.genesis_block(), &dir)); /// let client = Arc::new(Client::new(ethereum::new_frontier(), &dir).unwrap());
/// EthSync::register(&mut service, client); /// EthSync::register(&mut service, client);
/// } /// }
/// ``` /// ```

View File

@ -6,8 +6,8 @@ use util::sha3::Hashable;
use util::rlp::{self, Rlp, RlpStream, View, Stream}; use util::rlp::{self, Rlp, RlpStream, View, Stream};
use util::network::{PeerId, PacketId}; use util::network::{PeerId, PacketId};
use util::error::UtilError; use util::error::UtilError;
use client::{BlockChainClient, BlockStatus, BlockNumber, TreeRoute, BlockQueueStatus, BlockChainInfo, ImportResult, QueueStatus}; use client::{BlockChainClient, BlockStatus, TreeRoute, BlockQueueStatus, BlockChainInfo, ImportResult};
use header::Header as BlockHeader; use header::{Header as BlockHeader, BlockNumber};
use sync::io::SyncIo; use sync::io::SyncIo;
use sync::chain::ChainSync; use sync::chain::ChainSync;
@ -39,7 +39,7 @@ impl TestBlockChainClient {
let mut header = BlockHeader::new(); let mut header = BlockHeader::new();
header.difficulty = From::from(n); header.difficulty = From::from(n);
header.parent_hash = self.last_hash.clone(); header.parent_hash = self.last_hash.clone();
header.number = From::from(n); header.number = n as BlockNumber;
let mut uncles = RlpStream::new_list(if empty {0} else {1}); let mut uncles = RlpStream::new_list(if empty {0} else {1});
if !empty { if !empty {
uncles.append(&H256::from(&U256::from(n))); uncles.append(&H256::from(&U256::from(n)));
@ -49,7 +49,7 @@ impl TestBlockChainClient {
rlp.append(&header); rlp.append(&header);
rlp.append_raw(&rlp::NULL_RLP, 1); rlp.append_raw(&rlp::NULL_RLP, 1);
rlp.append_raw(uncles.as_raw(), 1); rlp.append_raw(uncles.as_raw(), 1);
self.import_block(rlp.as_raw()); self.import_block(rlp.as_raw()).unwrap();
} }
} }
} }
@ -100,12 +100,12 @@ impl BlockChainClient for TestBlockChainClient {
} }
} }
fn tree_route(&self, _from: &H256, _to: &H256) -> TreeRoute { fn tree_route(&self, _from: &H256, _to: &H256) -> Option<TreeRoute> {
TreeRoute { Some(TreeRoute {
blocks: Vec::new(), blocks: Vec::new(),
ancestor: H256::new(), ancestor: H256::new(),
index: 0 index: 0
} })
} }
fn state_data(&self, _h: &H256) -> Option<Bytes> { fn state_data(&self, _h: &H256) -> Option<Bytes> {
@ -118,7 +118,7 @@ impl BlockChainClient for TestBlockChainClient {
fn import_block(&mut self, b: &[u8]) -> ImportResult { fn import_block(&mut self, b: &[u8]) -> ImportResult {
let header = Rlp::new(&b).val_at::<BlockHeader>(0); let header = Rlp::new(&b).val_at::<BlockHeader>(0);
let number: usize = header.number.low_u64() as usize; let number: usize = header.number as usize;
if number > self.blocks.len() { if number > self.blocks.len() {
panic!("Unexpected block number. Expected {}, got {}", self.blocks.len(), number); panic!("Unexpected block number. Expected {}, got {}", self.blocks.len(), number);
} }
@ -126,7 +126,7 @@ impl BlockChainClient for TestBlockChainClient {
match self.blocks.get(&header.parent_hash) { match self.blocks.get(&header.parent_hash) {
Some(parent) => { Some(parent) => {
let parent = Rlp::new(parent).val_at::<BlockHeader>(0); let parent = Rlp::new(parent).val_at::<BlockHeader>(0);
if parent.number != (header.number - From::from(1)) { if parent.number != (header.number - 1) {
panic!("Unexpected block parent"); panic!("Unexpected block parent");
} }
}, },
@ -153,7 +153,7 @@ impl BlockChainClient for TestBlockChainClient {
else { else {
self.blocks.insert(header.hash(), b.to_vec()); self.blocks.insert(header.hash(), b.to_vec());
} }
ImportResult::Queued(QueueStatus::Known) Ok(())
} }
fn queue_status(&self) -> BlockQueueStatus { fn queue_status(&self) -> BlockQueueStatus {

View File

@ -6,7 +6,6 @@
/// 3. Final verification against the blockchain done before enactment. /// 3. Final verification against the blockchain done before enactment.
use common::*; use common::*;
use client::BlockNumber;
use engine::Engine; use engine::Engine;
use blockchain::BlockChain; use blockchain::BlockChain;
@ -82,12 +81,12 @@ pub fn verify_block_final(bytes: &[u8], engine: &mut Engine, bc: &BlockChain) ->
// 6 7 // 6 7
// (8 Invalid) // (8 Invalid)
let depth = if header.number > uncle.number { header.number - uncle.number } else { From::from(0) }; let depth = if header.number > uncle.number { header.number - uncle.number } else { 0 };
if depth > From::from(6) { if depth > 6 {
return Err(From::from(BlockError::UncleTooOld(OutOfBounds { min: header.number - depth, max: header.number - From::from(1), found: uncle.number }))); return Err(From::from(BlockError::UncleTooOld(OutOfBounds { min: header.number - depth, max: header.number - 1, found: uncle.number })));
} }
else if depth < From::from(1) { else if depth < 1 {
return Err(From::from(BlockError::UncleIsBrother(OutOfBounds { min: header.number - depth, max: header.number - From::from(1), found: uncle.number }))); return Err(From::from(BlockError::UncleIsBrother(OutOfBounds { min: header.number - depth, max: header.number - 1, found: uncle.number })));
} }
// cB // cB
@ -100,7 +99,7 @@ pub fn verify_block_final(bytes: &[u8], engine: &mut Engine, bc: &BlockChain) ->
// cB.p^7 -------------/ // cB.p^7 -------------/
// cB.p^8 // cB.p^8
let mut expected_uncle_parent = header.parent_hash.clone(); let mut expected_uncle_parent = header.parent_hash.clone();
for _ in 0..depth.as_u32() { for _ in 0..depth {
expected_uncle_parent = bc.block_details(&expected_uncle_parent).unwrap().parent; expected_uncle_parent = bc.block_details(&expected_uncle_parent).unwrap().parent;
} }
if expected_uncle_parent != uncle_parent.hash() { if expected_uncle_parent != uncle_parent.hash() {
@ -116,7 +115,7 @@ pub fn verify_block_final(bytes: &[u8], engine: &mut Engine, bc: &BlockChain) ->
/// Check basic header parameters. /// Check basic header parameters.
fn verify_header(header: &Header) -> Result<(), Error> { fn verify_header(header: &Header) -> Result<(), Error> {
if header.number > From::from(BlockNumber::max_value()) { if header.number > From::from(BlockNumber::max_value()) {
return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: From::from(BlockNumber::max_value()), min: From::from(0), found: header.number }))) return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: From::from(BlockNumber::max_value()), min: 0, found: header.number })))
} }
if header.gas_used > header.gas_limit { if header.gas_used > header.gas_limit {
return Err(From::from(BlockError::TooMuchGasUsed(OutOfBounds { max: header.gas_limit, min: From::from(0), found: header.gas_used }))); return Err(From::from(BlockError::TooMuchGasUsed(OutOfBounds { max: header.gas_limit, min: From::from(0), found: header.gas_used })));
@ -130,10 +129,10 @@ fn verify_parent(header: &Header, parent: &Header) -> Result<(), Error> {
return Err(From::from(BlockError::InvalidParentHash(Mismatch { expected: parent.hash(), found: header.parent_hash.clone() }))) return Err(From::from(BlockError::InvalidParentHash(Mismatch { expected: parent.hash(), found: header.parent_hash.clone() })))
} }
if header.timestamp <= parent.timestamp { if header.timestamp <= parent.timestamp {
return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: BAD_U256, min: parent.timestamp + From::from(1), found: header.timestamp }))) return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: u64::max_value(), min: parent.timestamp + 1, found: header.timestamp })))
} }
if header.number <= parent.number { if header.number <= parent.number {
return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: From::from(BlockNumber::max_value()), min: parent.number + From::from(1), found: header.number }))); return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: BlockNumber::max_value(), min: parent.number + 1, found: header.number })));
} }
Ok(()) Ok(())
} }

View File

@ -113,8 +113,8 @@ impl<'a> HeaderView<'a> {
pub fn difficulty(&self) -> U256 { self.rlp.val_at(7) } pub fn difficulty(&self) -> U256 { self.rlp.val_at(7) }
/// Returns block number. /// Returns block number.
pub fn number(&self) -> U256 { self.rlp.val_at(8) } pub fn number(&self) -> BlockNumber { self.rlp.val_at(8) }
/// Returns block gas limit. /// Returns block gas limit.
pub fn gas_limit(&self) -> U256 { self.rlp.val_at(9) } pub fn gas_limit(&self) -> U256 { self.rlp.val_at(9) }
@ -122,7 +122,7 @@ impl<'a> HeaderView<'a> {
pub fn gas_used(&self) -> U256 { self.rlp.val_at(10) } pub fn gas_used(&self) -> U256 { self.rlp.val_at(10) }
/// Returns timestamp. /// Returns timestamp.
pub fn timestamp(&self) -> U256 { self.rlp.val_at(11) } pub fn timestamp(&self) -> u64 { self.rlp.val_at(11) }
/// Returns block extra data. /// Returns block extra data.
pub fn extra_data(&self) -> Bytes { self.rlp.val_at(12) } pub fn extra_data(&self) -> Bytes { self.rlp.val_at(12) }