Merge branch 'master' into evm
This commit is contained in:
commit
65bce7862a
3
.gitignore
vendored
3
.gitignore
vendored
@ -17,3 +17,6 @@ Cargo.lock
|
||||
|
||||
# mac stuff
|
||||
.DS_Store
|
||||
|
||||
# gdb files
|
||||
.gdb_history
|
||||
|
0
<std macros>
Normal file
0
<std macros>
Normal file
@ -15,6 +15,7 @@ flate2 = "0.2"
|
||||
rocksdb = "0.2"
|
||||
heapsize = "0.2.0"
|
||||
rust-crypto = "0.2.34"
|
||||
time = "0.1"
|
||||
|
||||
evmjit = { path = "rust-evmjit", optional = true }
|
||||
|
||||
|
32
src/bin/client.rs
Normal file
32
src/bin/client.rs
Normal file
@ -0,0 +1,32 @@
|
||||
extern crate ethcore_util as util;
|
||||
extern crate ethcore;
|
||||
extern crate rustc_serialize;
|
||||
extern crate env_logger;
|
||||
|
||||
use std::io::*;
|
||||
use std::env;
|
||||
use std::sync::Arc;
|
||||
use util::hash::*;
|
||||
use util::network::{NetworkService};
|
||||
use ethcore::client::Client;
|
||||
use ethcore::sync::EthSync;
|
||||
use ethcore::ethereum;
|
||||
|
||||
fn main() {
|
||||
::env_logger::init().ok();
|
||||
let mut service = NetworkService::start().unwrap();
|
||||
//TODO: replace with proper genesis and chain params.
|
||||
let spec = ethereum::new_frontier();
|
||||
let mut dir = env::temp_dir();
|
||||
dir.push(H32::random().hex());
|
||||
let client = Arc::new(Client::new(spec, &dir).unwrap());
|
||||
EthSync::register(&mut service, client);
|
||||
loop {
|
||||
let mut cmd = String::new();
|
||||
stdin().read_line(&mut cmd).unwrap();
|
||||
if cmd == "quit\n" || cmd == "exit\n" || cmd == "q\n" {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
195
src/block.rs
195
src/block.rs
@ -17,19 +17,39 @@ pub struct Block {
|
||||
|
||||
archive: Vec<Entry>,
|
||||
archive_set: HashSet<H256>,
|
||||
|
||||
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 {
|
||||
/// Create a new block from the given `state`.
|
||||
fn new(state: State) -> Block {
|
||||
Block {
|
||||
header: Header::new(),
|
||||
state: state,
|
||||
archive: Vec::new(),
|
||||
archive_set: HashSet::new(),
|
||||
uncles: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
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`.
|
||||
@ -45,6 +65,9 @@ pub trait IsBlock {
|
||||
|
||||
/// Get all information on transactions in this block.
|
||||
fn archive(&self) -> &Vec<Entry> { &self.block().archive }
|
||||
|
||||
/// Get all uncles in this block.
|
||||
fn uncles(&self) -> &Vec<Header> { &self.block().uncles }
|
||||
}
|
||||
|
||||
impl IsBlock for Block {
|
||||
@ -55,19 +78,19 @@ impl IsBlock for Block {
|
||||
///
|
||||
/// 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.
|
||||
pub struct OpenBlock<'engine> {
|
||||
pub struct OpenBlock<'x, 'y> {
|
||||
block: Block,
|
||||
engine: &'engine Engine,
|
||||
last_hashes: LastHashes,
|
||||
engine: &'x Engine,
|
||||
last_hashes: &'y LastHashes,
|
||||
}
|
||||
|
||||
/// Just like OpenBlock, except that we've applied `Engine::on_close_block`, finished up the non-seal header fields,
|
||||
/// and collected the uncles.
|
||||
///
|
||||
/// There is no function available to push a transaction. If you want that you'll need to `reopen()` it.
|
||||
pub struct ClosedBlock<'engine> {
|
||||
open_block: OpenBlock<'engine>,
|
||||
uncles: Bytes,
|
||||
pub struct ClosedBlock<'x, 'y> {
|
||||
open_block: OpenBlock<'x, 'y>,
|
||||
uncle_bytes: Bytes,
|
||||
}
|
||||
|
||||
/// A block that has a valid seal.
|
||||
@ -75,30 +98,65 @@ pub struct ClosedBlock<'engine> {
|
||||
/// The block's header has valid seal arguments. The block cannot be reversed into a ClosedBlock or OpenBlock.
|
||||
pub struct SealedBlock {
|
||||
block: Block,
|
||||
_bytes: Bytes,
|
||||
uncle_bytes: Bytes,
|
||||
}
|
||||
|
||||
impl<'engine> OpenBlock<'engine> {
|
||||
impl<'x, 'y> OpenBlock<'x, 'y> {
|
||||
/// Create a new OpenBlock ready for transaction pushing.
|
||||
pub fn new<'a>(engine: &'a Engine, db: OverlayDB, parent: &Header, last_hashes: LastHashes) -> 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 {
|
||||
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,
|
||||
last_hashes: last_hashes,
|
||||
};
|
||||
|
||||
r.block.header.set_number(parent.number() + 1);
|
||||
r.block.header.set_author(author);
|
||||
r.block.header.set_extra_data(extra_data);
|
||||
r.block.header.set_timestamp_now();
|
||||
|
||||
engine.populate_from_parent(&mut r.block.header, parent);
|
||||
engine.on_new_block(&mut r.block);
|
||||
r
|
||||
}
|
||||
|
||||
/// Alter the author for the block.
|
||||
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.
|
||||
pub fn set_extra_data(&mut self, extra_data: Bytes) -> Result<(), BlockError> {
|
||||
if extra_data.len() > self.engine.maximum_extra_data_size() {
|
||||
Err(BlockError::ExtraDataOutOfBounds(OutOfBounds{min: 0, max: self.engine.maximum_extra_data_size(), found: extra_data.len()}))
|
||||
} else {
|
||||
self.block.header.set_extra_data(extra_data);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Add an uncle to the block, if possible.
|
||||
///
|
||||
/// NOTE Will check chain constraints and the uncle number but will NOT check
|
||||
/// that the header itself is actually valid.
|
||||
pub fn push_uncle(&mut self, valid_uncle_header: Header) -> Result<(), BlockError> {
|
||||
if self.block.uncles.len() >= self.engine.maximum_uncle_count() {
|
||||
return Err(BlockError::TooManyUncles(OutOfBounds{min: 0, max: self.engine.maximum_uncle_count(), found: self.block.uncles.len()}));
|
||||
}
|
||||
// TODO: check number
|
||||
// TODO: check not a direct ancestor (use last_hashes for that)
|
||||
self.block.uncles.push(valid_uncle_header);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the environment info concerning this block.
|
||||
pub fn env_info(&self) -> EnvInfo {
|
||||
// TODO: memoise.
|
||||
EnvInfo {
|
||||
number: self.block.header.number.clone(),
|
||||
number: self.block.header.number,
|
||||
author: self.block.header.author.clone(),
|
||||
timestamp: self.block.header.timestamp.clone(),
|
||||
timestamp: self.block.header.timestamp,
|
||||
difficulty: self.block.header.difficulty.clone(),
|
||||
last_hashes: self.last_hashes.clone(),
|
||||
gas_used: self.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0)),
|
||||
@ -106,12 +164,14 @@ impl<'engine> OpenBlock<'engine> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Push a transaction into the block. It will be executed, and archived together with the receipt.
|
||||
pub fn push_transaction(&mut self, t: Transaction, h: Option<H256>) -> Result<&Receipt, EthcoreError> {
|
||||
/// Push a transaction into the block.
|
||||
///
|
||||
/// If valid, it will be executed, and archived together with the receipt.
|
||||
pub fn push_transaction(&mut self, t: Transaction, h: Option<H256>) -> Result<&Receipt, Error> {
|
||||
let env_info = self.env_info();
|
||||
match self.block.state.apply(&env_info, self.engine, &t, true) {
|
||||
match self.block.state.apply(&env_info, self.engine, &t) {
|
||||
Ok(x) => {
|
||||
self.block.archive_set.insert(h.unwrap_or_else(||t.sha3()));
|
||||
self.block.archive_set.insert(h.unwrap_or_else(||t.hash()));
|
||||
self.block.archive.push(Entry { transaction: t, receipt: x.receipt });
|
||||
Ok(&self.block.archive.last().unwrap().receipt)
|
||||
}
|
||||
@ -120,17 +180,14 @@ 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<Header>, author: Address, extra_data: Bytes) -> ClosedBlock<'engine> {
|
||||
pub fn close(self) -> ClosedBlock<'x, 'y> {
|
||||
let mut s = self;
|
||||
// populate rest of header.
|
||||
s.engine.on_close_block(&mut s.block);
|
||||
s.block.header.author = author;
|
||||
// s.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();
|
||||
s.block.header.transactions_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.transaction.rlp_bytes()).collect());
|
||||
let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append(&u.rlp(Seal::With)); s} ).out();
|
||||
s.block.header.uncles_hash = uncle_bytes.sha3();
|
||||
s.block.header.extra_data = extra_data;
|
||||
s.block.header.state_root = s.block.state.root().clone();
|
||||
// s.header.receipts_root = ...;
|
||||
s.block.header.receipts_root = ordered_trie_root(s.block.archive.iter().map(|ref e| e.receipt.rlp_bytes()).collect());
|
||||
s.block.header.log_bloom = s.block.archive.iter().fold(LogBloom::zero(), |mut b, e| {b |= &e.receipt.log_bloom; b});
|
||||
s.block.header.gas_used = s.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0));
|
||||
s.block.header.note_dirty();
|
||||
@ -139,48 +196,104 @@ impl<'engine> OpenBlock<'engine> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'engine> IsBlock for OpenBlock<'engine> {
|
||||
impl<'x, 'y> IsBlock for OpenBlock<'x, 'y> {
|
||||
fn block(&self) -> &Block { &self.block }
|
||||
}
|
||||
|
||||
impl<'engine> ClosedBlock<'engine> {
|
||||
fn new<'a>(open_block: OpenBlock<'a>, uncles: Bytes) -> ClosedBlock<'a> {
|
||||
impl<'x, 'y> IsBlock for ClosedBlock<'x, 'y> {
|
||||
fn block(&self) -> &Block { &self.open_block.block }
|
||||
}
|
||||
|
||||
impl<'x, 'y> ClosedBlock<'x, 'y> {
|
||||
fn new<'a, 'b>(open_block: OpenBlock<'a, 'b>, uncle_bytes: Bytes) -> ClosedBlock<'a, 'b> {
|
||||
ClosedBlock {
|
||||
open_block: open_block,
|
||||
uncles: uncles,
|
||||
uncle_bytes: uncle_bytes,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the hash of the header without seal arguments.
|
||||
pub fn preseal_hash(&self) -> H256 { unimplemented!(); }
|
||||
pub fn hash(&self) -> H256 { self.header().rlp_sha3(Seal::Without) }
|
||||
|
||||
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure ou the uncles.
|
||||
pub fn seal(self, _seal_fields: Vec<Bytes>) -> SealedBlock { unimplemented!(); }
|
||||
/// Provide a valid seal in order to turn this into a `SealedBlock`.
|
||||
///
|
||||
/// NOTE: This does not check the validity of `seal` with the engine.
|
||||
pub fn seal(self, seal: Vec<Bytes>) -> Result<SealedBlock, BlockError> {
|
||||
let mut s = self;
|
||||
if seal.len() != s.open_block.engine.seal_fields() {
|
||||
return Err(BlockError::InvalidSealArity(Mismatch{expected: s.open_block.engine.seal_fields(), found: seal.len()}));
|
||||
}
|
||||
s.open_block.block.header.set_seal(seal);
|
||||
Ok(SealedBlock { block: s.open_block.block, uncle_bytes: s.uncle_bytes })
|
||||
}
|
||||
|
||||
/// Turn this back into an `OpenBlock`.
|
||||
pub fn reopen(self) -> OpenBlock<'engine> { unimplemented!(); }
|
||||
}
|
||||
|
||||
impl<'engine> IsBlock for ClosedBlock<'engine> {
|
||||
fn block(&self) -> &Block { &self.open_block.block }
|
||||
pub fn reopen(self) -> OpenBlock<'x, 'y> { self.open_block }
|
||||
}
|
||||
|
||||
impl SealedBlock {
|
||||
/// Get the RLP-encoding of the block.
|
||||
pub fn rlp_bytes(&self) -> Bytes {
|
||||
let mut block_rlp = RlpStream::new_list(3);
|
||||
self.block.header.stream_rlp(&mut block_rlp, Seal::With);
|
||||
block_rlp.append_list(self.block.archive.len());
|
||||
for e in self.block.archive.iter() { e.transaction.rlp_append(&mut block_rlp); }
|
||||
block_rlp.append_raw(&self.uncle_bytes, 1);
|
||||
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 {
|
||||
fn block(&self) -> &Block { &self.block }
|
||||
}
|
||||
|
||||
/// Enact the block given by `block_bytes` using `engine` on the database `db` with given `parent` block header
|
||||
///
|
||||
pub fn enact(block_bytes: &[u8], engine: &Engine, db: OverlayDB, parent: &Header, last_hashes: &LastHashes) -> Result<SealedBlock, Error> {
|
||||
let block = BlockView::new(block_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]
|
||||
fn open_block() {
|
||||
use spec::*;
|
||||
use ethereum::*;
|
||||
let engine = new_morden().to_engine().unwrap();
|
||||
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()]);
|
||||
let b = b.close(vec![], Address::zero(), vec![]);
|
||||
assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244F40000").unwrap());
|
||||
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.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());
|
||||
assert!(orig_db.keys().iter().filter(|k| orig_db.get(k.0) != db.get(k.0)).next() == None);
|
||||
}
|
@ -33,7 +33,7 @@ pub struct CacheSize {
|
||||
/// Information about best block gathered together
|
||||
struct BestBlock {
|
||||
pub hash: H256,
|
||||
pub number: U256,
|
||||
pub number: BlockNumber,
|
||||
pub total_difficulty: U256
|
||||
}
|
||||
|
||||
@ -41,7 +41,7 @@ impl BestBlock {
|
||||
fn new() -> BestBlock {
|
||||
BestBlock {
|
||||
hash: H256::new(),
|
||||
number: U256::from(0),
|
||||
number: 0,
|
||||
total_difficulty: U256::from(0)
|
||||
}
|
||||
}
|
||||
@ -51,17 +51,17 @@ impl BestBlock {
|
||||
///
|
||||
/// **Does not do input data verification.**
|
||||
pub struct BlockChain {
|
||||
best_block: RefCell<BestBlock>,
|
||||
best_block: RwLock<BestBlock>,
|
||||
|
||||
// block cache
|
||||
blocks: RefCell<HashMap<H256, Bytes>>,
|
||||
blocks: RwLock<HashMap<H256, Bytes>>,
|
||||
|
||||
// extra caches
|
||||
block_details: RefCell<HashMap<H256, BlockDetails>>,
|
||||
block_hashes: RefCell<HashMap<U256, H256>>,
|
||||
transaction_addresses: RefCell<HashMap<H256, TransactionAddress>>,
|
||||
block_logs: RefCell<HashMap<H256, BlockLogBlooms>>,
|
||||
blocks_blooms: RefCell<HashMap<H256, BlocksBlooms>>,
|
||||
block_details: RwLock<HashMap<H256, BlockDetails>>,
|
||||
block_hashes: RwLock<HashMap<BlockNumber, H256>>,
|
||||
transaction_addresses: RwLock<HashMap<H256, TransactionAddress>>,
|
||||
block_logs: RwLock<HashMap<H256, BlockLogBlooms>>,
|
||||
blocks_blooms: RwLock<HashMap<H256, BlocksBlooms>>,
|
||||
|
||||
extras_db: DB,
|
||||
blocks_db: DB
|
||||
@ -92,7 +92,7 @@ impl BlockChain {
|
||||
/// let genesis_hash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3";
|
||||
/// assert_eq!(bc.genesis_hash(), H256::from_str(genesis_hash).unwrap());
|
||||
/// 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 {
|
||||
@ -107,13 +107,13 @@ impl BlockChain {
|
||||
let blocks_db = DB::open_default(blocks_path.to_str().unwrap()).unwrap();
|
||||
|
||||
let bc = BlockChain {
|
||||
best_block: RefCell::new(BestBlock::new()),
|
||||
blocks: RefCell::new(HashMap::new()),
|
||||
block_details: RefCell::new(HashMap::new()),
|
||||
block_hashes: RefCell::new(HashMap::new()),
|
||||
transaction_addresses: RefCell::new(HashMap::new()),
|
||||
block_logs: RefCell::new(HashMap::new()),
|
||||
blocks_blooms: RefCell::new(HashMap::new()),
|
||||
best_block: RwLock::new(BestBlock::new()),
|
||||
blocks: RwLock::new(HashMap::new()),
|
||||
block_details: RwLock::new(HashMap::new()),
|
||||
block_hashes: RwLock::new(HashMap::new()),
|
||||
transaction_addresses: RwLock::new(HashMap::new()),
|
||||
block_logs: RwLock::new(HashMap::new()),
|
||||
blocks_blooms: RwLock::new(HashMap::new()),
|
||||
extras_db: extras_db,
|
||||
blocks_db: blocks_db
|
||||
};
|
||||
@ -148,7 +148,7 @@ impl BlockChain {
|
||||
};
|
||||
|
||||
{
|
||||
let mut best_block = bc.best_block.borrow_mut();
|
||||
let mut best_block = bc.best_block.write().unwrap();
|
||||
best_block.number = bc.block_number(&best_block_hash).unwrap();
|
||||
best_block.total_difficulty = bc.block_details(&best_block_hash).unwrap().total_difficulty;
|
||||
best_block.hash = best_block_hash;
|
||||
@ -199,11 +199,16 @@ impl BlockChain {
|
||||
/// ```json
|
||||
/// { blocks: [B4, B3, A3, A4], ancestor: A2, index: 2 }
|
||||
/// ```
|
||||
pub fn tree_route(&self, from: H256, to: H256) -> TreeRoute {
|
||||
let from_details = self.block_details(&from).expect("from hash is invalid!");
|
||||
let to_details = self.block_details(&to).expect("to hash is invalid!");
|
||||
|
||||
self._tree_route((from_details, from), (to_details, to))
|
||||
pub fn tree_route(&self, from: H256, to: H256) -> Option<TreeRoute> {
|
||||
let from_details = match self.block_details(&from) {
|
||||
Some(h) => h,
|
||||
None => return None,
|
||||
};
|
||||
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
|
||||
@ -262,7 +267,7 @@ impl BlockChain {
|
||||
// create views onto rlp
|
||||
let block = BlockView::new(bytes);
|
||||
let header = block.header_view();
|
||||
let hash = block.sha3();
|
||||
let hash = header.sha3();
|
||||
|
||||
if self.is_known(&hash) {
|
||||
return;
|
||||
@ -273,13 +278,13 @@ impl BlockChain {
|
||||
let (batch, new_best) = self.block_to_extras_insert_batch(bytes);
|
||||
|
||||
// update best block
|
||||
let mut best_block = self.best_block.borrow_mut();
|
||||
let mut best_block = self.best_block.write().unwrap();
|
||||
if let Some(b) = new_best {
|
||||
*best_block = b;
|
||||
}
|
||||
|
||||
// update caches
|
||||
let mut write = self.block_details.borrow_mut();
|
||||
let mut write = self.block_details.write().unwrap();
|
||||
write.remove(&header.parent_hash());
|
||||
|
||||
// update extras database
|
||||
@ -336,9 +341,9 @@ impl BlockChain {
|
||||
// it is a fork
|
||||
i if i > 1 => {
|
||||
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() {
|
||||
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,
|
||||
@ -371,7 +376,7 @@ impl BlockChain {
|
||||
|
||||
/// Returns reference to genesis hash.
|
||||
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.
|
||||
@ -409,27 +414,27 @@ impl BlockChain {
|
||||
}
|
||||
|
||||
/// Get the hash of given block's number.
|
||||
pub fn block_hash(&self, hash: &U256) -> Option<H256> {
|
||||
self.query_extras(hash, &self.block_hashes)
|
||||
pub fn block_hash(&self, index: BlockNumber) -> Option<H256> {
|
||||
self.query_extras(&index, &self.block_hashes)
|
||||
}
|
||||
|
||||
/// Get best block hash.
|
||||
pub fn best_block_hash(&self) -> H256 {
|
||||
self.best_block.borrow().hash.clone()
|
||||
self.best_block.read().unwrap().hash.clone()
|
||||
}
|
||||
|
||||
/// Get best block number.
|
||||
pub fn best_block_number(&self) -> U256 {
|
||||
self.best_block.borrow().number
|
||||
pub fn best_block_number(&self) -> BlockNumber {
|
||||
self.best_block.read().unwrap().number
|
||||
}
|
||||
|
||||
/// Get best block total difficulty.
|
||||
pub fn best_block_total_difficulty(&self) -> U256 {
|
||||
self.best_block.borrow().total_difficulty
|
||||
self.best_block.read().unwrap().total_difficulty
|
||||
}
|
||||
|
||||
/// 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())
|
||||
}
|
||||
|
||||
@ -438,9 +443,10 @@ impl BlockChain {
|
||||
self.query_extras(hash, &self.block_logs)
|
||||
}
|
||||
|
||||
fn block(&self, hash: &H256) -> Option<Bytes> {
|
||||
/// Get raw block data
|
||||
pub fn block(&self, hash: &H256) -> Option<Bytes> {
|
||||
{
|
||||
let read = self.blocks.borrow();
|
||||
let read = self.blocks.read().unwrap();
|
||||
match read.get(hash) {
|
||||
Some(v) => return Some(v.clone()),
|
||||
None => ()
|
||||
@ -453,7 +459,7 @@ impl BlockChain {
|
||||
match opt {
|
||||
Some(b) => {
|
||||
let bytes: Bytes = b.to_vec();
|
||||
let mut write = self.blocks.borrow_mut();
|
||||
let mut write = self.blocks.write().unwrap();
|
||||
write.insert(hash.clone(), bytes.clone());
|
||||
Some(bytes)
|
||||
},
|
||||
@ -461,11 +467,11 @@ impl BlockChain {
|
||||
}
|
||||
}
|
||||
|
||||
fn query_extras<K, T>(&self, hash: &K, cache: &RefCell<HashMap<K, T>>) -> Option<T> where
|
||||
fn query_extras<K, T>(&self, hash: &K, cache: &RwLock<HashMap<K, T>>) -> Option<T> where
|
||||
T: Clone + Decodable + ExtrasIndexable,
|
||||
K: ExtrasSliceConvertable + Eq + Hash + Clone {
|
||||
{
|
||||
let read = cache.borrow();
|
||||
let read = cache.read().unwrap();
|
||||
match read.get(hash) {
|
||||
Some(v) => return Some(v.clone()),
|
||||
None => ()
|
||||
@ -473,17 +479,17 @@ impl BlockChain {
|
||||
}
|
||||
|
||||
self.extras_db.get_extras(hash).map(| t: T | {
|
||||
let mut write = cache.borrow_mut();
|
||||
let mut write = cache.write().unwrap();
|
||||
write.insert(hash.clone(), t.clone());
|
||||
t
|
||||
})
|
||||
}
|
||||
|
||||
fn query_extras_exist<K, T>(&self, hash: &K, cache: &RefCell<HashMap<K, T>>) -> bool where
|
||||
fn query_extras_exist<K, T>(&self, hash: &K, cache: &RwLock<HashMap<K, T>>) -> bool where
|
||||
K: ExtrasSliceConvertable + Eq + Hash + Clone,
|
||||
T: ExtrasIndexable {
|
||||
{
|
||||
let read = cache.borrow();
|
||||
let read = cache.read().unwrap();
|
||||
match read.get(hash) {
|
||||
Some(_) => return true,
|
||||
None => ()
|
||||
@ -496,21 +502,21 @@ impl BlockChain {
|
||||
/// Get current cache size.
|
||||
pub fn cache_size(&self) -> CacheSize {
|
||||
CacheSize {
|
||||
blocks: self.blocks.heap_size_of_children(),
|
||||
block_details: self.block_details.heap_size_of_children(),
|
||||
transaction_addresses: self.transaction_addresses.heap_size_of_children(),
|
||||
block_logs: self.block_logs.heap_size_of_children(),
|
||||
blocks_blooms: self.blocks_blooms.heap_size_of_children()
|
||||
blocks: self.blocks.read().unwrap().heap_size_of_children(),
|
||||
block_details: self.block_details.read().unwrap().heap_size_of_children(),
|
||||
transaction_addresses: self.transaction_addresses.read().unwrap().heap_size_of_children(),
|
||||
block_logs: self.block_logs.read().unwrap().heap_size_of_children(),
|
||||
blocks_blooms: self.blocks_blooms.read().unwrap().heap_size_of_children()
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to squeeze the cache if its too big.
|
||||
pub fn squeeze_to_fit(&self, size: CacheSize) {
|
||||
self.blocks.borrow_mut().squeeze(size.blocks);
|
||||
self.block_details.borrow_mut().squeeze(size.block_details);
|
||||
self.transaction_addresses.borrow_mut().squeeze(size.transaction_addresses);
|
||||
self.block_logs.borrow_mut().squeeze(size.block_logs);
|
||||
self.blocks_blooms.borrow_mut().squeeze(size.blocks_blooms);
|
||||
self.blocks.write().unwrap().squeeze(size.blocks);
|
||||
self.block_details.write().unwrap().squeeze(size.block_details);
|
||||
self.transaction_addresses.write().unwrap().squeeze(size.transaction_addresses);
|
||||
self.block_logs.write().unwrap().squeeze(size.block_logs);
|
||||
self.blocks_blooms.write().unwrap().squeeze(size.blocks_blooms);
|
||||
}
|
||||
}
|
||||
|
||||
@ -520,7 +526,6 @@ mod tests {
|
||||
use std::str::FromStr;
|
||||
use rustc_serialize::hex::FromHex;
|
||||
use util::hash::*;
|
||||
use util::uint::*;
|
||||
use blockchain::*;
|
||||
|
||||
#[test]
|
||||
@ -535,11 +540,10 @@ mod tests {
|
||||
let genesis_hash = H256::from_str("3caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942").unwrap();
|
||||
|
||||
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.block_hash(&U256::from(0u8)), Some(genesis_hash.clone()));
|
||||
assert_eq!(bc.block_hash(&U256::from(1u8)), None);
|
||||
|
||||
assert_eq!(bc.block_hash(0), Some(genesis_hash.clone()));
|
||||
assert_eq!(bc.block_hash(1), None);
|
||||
|
||||
let first = "f90285f90219a03caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0bac6177a79e910c98d86ec31a09ae37ac2de15b754fd7bed1ba52362c49416bfa0d45893a296c1490a978e0bd321b5f2635d8280365c1fe9f693d65f233e791344a0c7778a7376099ee2e5c455791c1885b5c361b95713fddcbe32d97fd01334d296b90100000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000400000000000000000000000000000000000000000000000000000008302000001832fefd882560b845627cb99a00102030405060708091011121314151617181920212223242526272829303132a08ccb2837fb2923bd97e8f2d08ea32012d6e34be018c73e49a0f98843e8f47d5d88e53be49fec01012ef866f864800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d8785012a05f200801ba0cb088b8d2ff76a7b2c6616c9d02fb6b7a501afbf8b69d7180b09928a1b80b5e4a06448fe7476c606582039bb72a9f6f4b4fad18507b8dfbd00eebbe151cc573cd2c0".from_hex().unwrap();
|
||||
|
||||
@ -547,13 +551,13 @@ mod tests {
|
||||
|
||||
let first_hash = H256::from_str("a940e5af7d146b3b917c953a82e1966b906dace3a4e355b5b0a4560190357ea1").unwrap();
|
||||
|
||||
assert_eq!(bc.block_hash(&U256::from(0u8)), Some(genesis_hash.clone()));
|
||||
assert_eq!(bc.best_block_number(), U256::from(1u8));
|
||||
assert_eq!(bc.block_hash(0), Some(genesis_hash.clone()));
|
||||
assert_eq!(bc.best_block_number(), 1);
|
||||
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(&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]
|
||||
@ -583,64 +587,64 @@ mod tests {
|
||||
bc.insert_block(&b3b);
|
||||
|
||||
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(&b1_hash).unwrap(), U256::from(1));
|
||||
assert_eq!(bc.block_number(&b2_hash).unwrap(), U256::from(2));
|
||||
assert_eq!(bc.block_number(&b3a_hash).unwrap(), U256::from(3));
|
||||
assert_eq!(bc.block_number(&b3b_hash).unwrap(), U256::from(3));
|
||||
assert_eq!(bc.block_number(&genesis_hash).unwrap(), 0);
|
||||
assert_eq!(bc.block_number(&b1_hash).unwrap(), 1);
|
||||
assert_eq!(bc.block_number(&b2_hash).unwrap(), 2);
|
||||
assert_eq!(bc.block_number(&b3a_hash).unwrap(), 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(&U256::from(1)).unwrap(), b1_hash);
|
||||
assert_eq!(bc.block_hash(&U256::from(2)).unwrap(), b2_hash);
|
||||
assert_eq!(bc.block_hash(&U256::from(3)).unwrap(), b3a_hash);
|
||||
assert_eq!(bc.block_hash(0).unwrap(), genesis_hash);
|
||||
assert_eq!(bc.block_hash(1).unwrap(), b1_hash);
|
||||
assert_eq!(bc.block_hash(2).unwrap(), b2_hash);
|
||||
assert_eq!(bc.block_hash(3).unwrap(), b3a_hash);
|
||||
|
||||
// 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.blocks, [b1_hash.clone()]);
|
||||
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.blocks, [b1_hash.clone(), b2_hash.clone()]);
|
||||
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.blocks, [b2_hash.clone(), b3a_hash.clone()]);
|
||||
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.blocks, [b2_hash.clone(), b3b_hash.clone()]);
|
||||
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.blocks, [b3a_hash.clone(), b3b_hash.clone()]);
|
||||
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.blocks, [b1_hash.clone()]);
|
||||
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.blocks, [b2_hash.clone(), b1_hash.clone()]);
|
||||
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.blocks, [b3a_hash.clone(), b2_hash.clone()]);
|
||||
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.blocks, [b3b_hash.clone(), b2_hash.clone()]);
|
||||
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.blocks, [b3b_hash.clone(), b3a_hash.clone()]);
|
||||
assert_eq!(r3b_3a.index, 1);
|
||||
|
@ -12,6 +12,11 @@ pub struct Builtin {
|
||||
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 {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "<Builtin>")
|
||||
|
190
src/client.rs
Normal file
190
src/client.rs
Normal file
@ -0,0 +1,190 @@
|
||||
use util::*;
|
||||
use blockchain::BlockChain;
|
||||
use views::BlockView;
|
||||
use error::*;
|
||||
use header::BlockNumber;
|
||||
use spec::Spec;
|
||||
use engine::Engine;
|
||||
use queue::BlockQueue;
|
||||
|
||||
/// General block status
|
||||
pub enum BlockStatus {
|
||||
/// Part of the blockchain.
|
||||
InChain,
|
||||
/// Queued for import.
|
||||
Queued,
|
||||
/// Known as bad.
|
||||
Bad,
|
||||
/// Unknown.
|
||||
Unknown,
|
||||
}
|
||||
|
||||
/// Information about the blockchain gthered together.
|
||||
pub struct BlockChainInfo {
|
||||
/// Blockchain difficulty.
|
||||
pub total_difficulty: U256,
|
||||
/// Block queue difficulty.
|
||||
pub pending_total_difficulty: U256,
|
||||
/// Genesis block hash.
|
||||
pub genesis_hash: H256,
|
||||
/// Best blockchain block hash.
|
||||
pub best_block_hash: H256,
|
||||
/// Best blockchain block number.
|
||||
pub best_block_number: BlockNumber
|
||||
}
|
||||
|
||||
/// Block queue status
|
||||
pub struct BlockQueueStatus {
|
||||
pub full: bool,
|
||||
}
|
||||
|
||||
pub type TreeRoute = ::blockchain::TreeRoute;
|
||||
|
||||
/// Blockchain database client. Owns and manages a blockchain and a block queue.
|
||||
pub trait BlockChainClient : Sync {
|
||||
/// Get raw block header data by block header hash.
|
||||
fn block_header(&self, hash: &H256) -> Option<Bytes>;
|
||||
|
||||
/// Get raw block body data by block header hash.
|
||||
/// Block body is an RLP list of two items: uncles and transactions.
|
||||
fn block_body(&self, hash: &H256) -> Option<Bytes>;
|
||||
|
||||
/// Get raw block data by block header hash.
|
||||
fn block(&self, hash: &H256) -> Option<Bytes>;
|
||||
|
||||
/// Get block status by block header hash.
|
||||
fn block_status(&self, hash: &H256) -> BlockStatus;
|
||||
|
||||
/// Get raw block header data by block number.
|
||||
fn block_header_at(&self, n: BlockNumber) -> Option<Bytes>;
|
||||
|
||||
/// Get raw block body data by block number.
|
||||
/// Block body is an RLP list of two items: uncles and transactions.
|
||||
fn block_body_at(&self, n: BlockNumber) -> Option<Bytes>;
|
||||
|
||||
/// Get raw block data by block number.
|
||||
fn block_at(&self, n: BlockNumber) -> Option<Bytes>;
|
||||
|
||||
/// Get block status by block number.
|
||||
fn block_status_at(&self, n: BlockNumber) -> BlockStatus;
|
||||
|
||||
/// Get a tree route between `from` and `to`.
|
||||
/// See `BlockChain::tree_route`.
|
||||
fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute>;
|
||||
|
||||
/// Get latest state node
|
||||
fn state_data(&self, hash: &H256) -> Option<Bytes>;
|
||||
|
||||
/// Get raw block receipts data by block header hash.
|
||||
fn block_receipts(&self, hash: &H256) -> Option<Bytes>;
|
||||
|
||||
/// Import a block into the blockchain.
|
||||
fn import_block(&mut self, byte: &[u8]) -> ImportResult;
|
||||
|
||||
/// Get block queue information.
|
||||
fn queue_status(&self) -> BlockQueueStatus;
|
||||
|
||||
/// Clear block queue and abort all import activity.
|
||||
fn clear_queue(&mut self);
|
||||
|
||||
/// Get blockchain information.
|
||||
fn chain_info(&self) -> BlockChainInfo;
|
||||
}
|
||||
|
||||
/// Blockchain database client backed by a persistent database. Owns and manages a blockchain and a block queue.
|
||||
pub struct Client {
|
||||
chain: Arc<RwLock<BlockChain>>,
|
||||
_engine: Arc<Box<Engine>>,
|
||||
queue: BlockQueue,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(spec: Spec, path: &Path) -> Result<Client, Error> {
|
||||
let chain = Arc::new(RwLock::new(BlockChain::new(&spec.genesis_block(), path)));
|
||||
let engine = Arc::new(try!(spec.to_engine()));
|
||||
Ok(Client {
|
||||
chain: chain.clone(),
|
||||
_engine: engine.clone(),
|
||||
queue: BlockQueue::new(chain.clone(), engine.clone()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockChainClient for Client {
|
||||
fn block_header(&self, hash: &H256) -> Option<Bytes> {
|
||||
self.chain.read().unwrap().block(hash).map(|bytes| BlockView::new(&bytes).rlp().at(0).as_raw().to_vec())
|
||||
}
|
||||
|
||||
fn block_body(&self, hash: &H256) -> Option<Bytes> {
|
||||
self.chain.read().unwrap().block(hash).map(|bytes| {
|
||||
let rlp = Rlp::new(&bytes);
|
||||
let mut body = RlpStream::new();
|
||||
body.append_raw(rlp.at(1).as_raw(), 1);
|
||||
body.append_raw(rlp.at(2).as_raw(), 1);
|
||||
body.out()
|
||||
})
|
||||
}
|
||||
|
||||
fn block(&self, hash: &H256) -> Option<Bytes> {
|
||||
self.chain.read().unwrap().block(hash)
|
||||
}
|
||||
|
||||
fn block_status(&self, hash: &H256) -> BlockStatus {
|
||||
if self.chain.read().unwrap().is_known(&hash) { BlockStatus::InChain } else { BlockStatus::Unknown }
|
||||
}
|
||||
|
||||
fn block_header_at(&self, n: BlockNumber) -> Option<Bytes> {
|
||||
self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_header(&h))
|
||||
}
|
||||
|
||||
fn block_body_at(&self, n: BlockNumber) -> Option<Bytes> {
|
||||
self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_body(&h))
|
||||
}
|
||||
|
||||
fn block_at(&self, n: BlockNumber) -> Option<Bytes> {
|
||||
self.chain.read().unwrap().block_hash(n).and_then(|h| self.block(&h))
|
||||
}
|
||||
|
||||
fn block_status_at(&self, n: BlockNumber) -> BlockStatus {
|
||||
match self.chain.read().unwrap().block_hash(n) {
|
||||
Some(h) => self.block_status(&h),
|
||||
None => BlockStatus::Unknown
|
||||
}
|
||||
}
|
||||
|
||||
fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute> {
|
||||
self.chain.read().unwrap().tree_route(from.clone(), to.clone())
|
||||
}
|
||||
|
||||
fn state_data(&self, _hash: &H256) -> Option<Bytes> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn block_receipts(&self, _hash: &H256) -> Option<Bytes> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn import_block(&mut self, bytes: &[u8]) -> ImportResult {
|
||||
self.queue.import_block(bytes)
|
||||
}
|
||||
|
||||
fn queue_status(&self) -> BlockQueueStatus {
|
||||
BlockQueueStatus {
|
||||
full: false
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_queue(&mut self) {
|
||||
}
|
||||
|
||||
fn chain_info(&self) -> BlockChainInfo {
|
||||
let chain = self.chain.read().unwrap();
|
||||
BlockChainInfo {
|
||||
total_difficulty: chain.best_block_total_difficulty(),
|
||||
pending_total_difficulty: chain.best_block_total_difficulty(),
|
||||
genesis_hash: chain.genesis_hash(),
|
||||
best_block_hash: chain.best_block_hash(),
|
||||
best_block_number: From::from(chain.best_block_number())
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
pub use util::*;
|
||||
pub use basic_types::*;
|
||||
pub use error::*;
|
||||
pub use env_info::*;
|
||||
pub use evm_schedule::*;
|
||||
pub use views::*;
|
||||
@ -7,4 +8,5 @@ pub use builtin::*;
|
||||
pub use header::*;
|
||||
pub use account::*;
|
||||
pub use transaction::*;
|
||||
pub use log_entry::*;
|
||||
pub use receipt::*;
|
@ -4,14 +4,14 @@ use spec::Spec;
|
||||
|
||||
/// 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.
|
||||
pub trait Engine {
|
||||
pub trait Engine : Sync + Send {
|
||||
/// The name of this engine.
|
||||
fn name(&self) -> &str;
|
||||
/// The version of this engine. Should be of the form
|
||||
fn version(&self) -> SemanticVersion { SemanticVersion::new(0, 0, 0) }
|
||||
|
||||
/// The number of additional header fields required for this engine.
|
||||
fn seal_fields(&self) -> u32 { 0 }
|
||||
fn seal_fields(&self) -> usize { 0 }
|
||||
/// Default values of the additional fields RLP-encoded in a raw (non-list) harness.
|
||||
fn seal_rlp(&self) -> Bytes { vec![] }
|
||||
|
||||
@ -25,23 +25,31 @@ pub trait Engine {
|
||||
fn evm_schedule(&self, env_info: &EnvInfo) -> EvmSchedule;
|
||||
|
||||
/// Some intrinsic operation parameters; by default they take their value from the `spec()`'s `engine_params`.
|
||||
fn maximum_extra_data_size(&self, _env_info: &EnvInfo) -> usize { decode(&self.spec().engine_params.get("maximumExtraDataSize").unwrap()) }
|
||||
fn maximum_extra_data_size(&self) -> usize { decode(&self.spec().engine_params.get("maximumExtraDataSize").unwrap()) }
|
||||
fn maximum_uncle_count(&self) -> usize { 2 }
|
||||
fn account_start_nonce(&self) -> U256 { decode(&self.spec().engine_params.get("accountStartNonce").unwrap()) }
|
||||
|
||||
/// Block transformation functions, before and after the transactions.
|
||||
fn on_new_block(&self, _block: &mut Block) {}
|
||||
fn on_close_block(&self, _block: &mut Block) {}
|
||||
|
||||
/// Verify that `header` is valid.
|
||||
/// `parent` (the parent header) and `block` (the header's full block) may be provided for additional
|
||||
/// checks. Returns either a null `Ok` or a general error detailing the problem with import.
|
||||
// TODO: consider including State in the params.
|
||||
fn verify_block(&self, _header: &Header, _parent: Option<&Header>, _block: Option<&[u8]>) -> Result<(), EthcoreError> { Ok(()) }
|
||||
// TODO: consider including State in the params for verification functions.
|
||||
/// Phase 1 quick block verification. Only does checks that are cheap. `block` (the header's full block)
|
||||
/// may be provided for additional checks. Returns either a null `Ok` or a general error detailing the problem with import.
|
||||
fn verify_block_basic(&self, _header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) }
|
||||
|
||||
/// Phase 2 verification. Perform costly checks such as transaction signatures. `block` (the header's full block)
|
||||
/// may be provided for additional checks. Returns either a null `Ok` or a general error detailing the problem with import.
|
||||
fn verify_block_unordered(&self, _header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) }
|
||||
|
||||
/// Phase 3 verification. Check block information against parent and uncles. `block` (the header's full block)
|
||||
/// may be provided for additional checks. Returns either a null `Ok` or a general error detailing the problem with import.
|
||||
fn verify_block_final(&self, _header: &Header, _parent: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) }
|
||||
|
||||
/// Additional verification for transactions in blocks.
|
||||
// TODO: Add flags for which bits of the transaction to check.
|
||||
// TODO: consider including State in the params.
|
||||
fn verify_transaction(&self, _t: &Transaction, _header: &Header) -> Result<(), EthcoreError> { Ok(()) }
|
||||
fn verify_transaction(&self, _t: &Transaction, _header: &Header) -> Result<(), Error> { Ok(()) }
|
||||
|
||||
/// Don't forget to call Super::populateFromParent when subclassing & overriding.
|
||||
// TODO: consider including State in the params.
|
||||
|
@ -1,4 +1,5 @@
|
||||
use util::*;
|
||||
use header::BlockNumber;
|
||||
|
||||
/// 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.
|
||||
@ -7,11 +8,11 @@ pub type LastHashes = Vec<H256>;
|
||||
/// Information concerning the execution environment for a message-call/contract-creation.
|
||||
pub struct EnvInfo {
|
||||
/// The block number.
|
||||
pub number: U256,
|
||||
pub number: BlockNumber,
|
||||
/// The block author.
|
||||
pub author: Address,
|
||||
/// The block timestamp.
|
||||
pub timestamp: U256,
|
||||
pub timestamp: u64,
|
||||
/// The block difficulty.
|
||||
pub difficulty: U256,
|
||||
/// The block gas limit.
|
||||
@ -25,9 +26,9 @@ pub struct EnvInfo {
|
||||
impl EnvInfo {
|
||||
pub fn new() -> EnvInfo {
|
||||
EnvInfo {
|
||||
number: U256::zero(),
|
||||
number: 0,
|
||||
author: Address::new(),
|
||||
timestamp: U256::zero(),
|
||||
timestamp: 0,
|
||||
difficulty: U256::zero(),
|
||||
gas_limit: U256::zero(),
|
||||
last_hashes: vec![],
|
||||
|
88
src/error.rs
Normal file
88
src/error.rs
Normal file
@ -0,0 +1,88 @@
|
||||
//! General error types for use in ethcore.
|
||||
|
||||
use util::*;
|
||||
use header::BlockNumber;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Mismatch<T: fmt::Debug> {
|
||||
pub expected: T,
|
||||
pub found: T,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OutOfBounds<T: fmt::Debug> {
|
||||
pub min: T,
|
||||
pub max: T,
|
||||
pub found: T,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum BlockError {
|
||||
TooManyUncles(OutOfBounds<usize>),
|
||||
UncleWrongGeneration,
|
||||
ExtraDataOutOfBounds(OutOfBounds<usize>),
|
||||
InvalidSealArity(Mismatch<usize>),
|
||||
TooMuchGasUsed(OutOfBounds<U256>),
|
||||
InvalidUnclesHash(Mismatch<H256>),
|
||||
UncleTooOld(OutOfBounds<BlockNumber>),
|
||||
UncleIsBrother(OutOfBounds<BlockNumber>),
|
||||
UncleInChain(H256),
|
||||
UncleParentNotInChain(H256),
|
||||
InvalidStateRoot,
|
||||
InvalidGasUsed,
|
||||
InvalidTransactionsRoot(Mismatch<H256>),
|
||||
InvalidDifficulty(Mismatch<U256>),
|
||||
InvalidGasLimit(OutOfBounds<U256>),
|
||||
InvalidReceiptsStateRoot,
|
||||
InvalidTimestamp(OutOfBounds<u64>),
|
||||
InvalidLogBloom,
|
||||
InvalidBlockNonce,
|
||||
InvalidParentHash(Mismatch<H256>),
|
||||
InvalidNumber(OutOfBounds<BlockNumber>),
|
||||
UnknownParent(H256),
|
||||
UnknownUncleParent(H256),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ImportError {
|
||||
Bad(Error),
|
||||
AlreadyInChain,
|
||||
AlreadyQueued,
|
||||
}
|
||||
|
||||
impl From<Error> for ImportError {
|
||||
fn from(err: Error) -> ImportError {
|
||||
ImportError::Bad(err)
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of import block operation.
|
||||
pub type ImportResult = Result<(), ImportError>;
|
||||
|
||||
#[derive(Debug)]
|
||||
/// General error type which should be capable of representing all errors in ethcore.
|
||||
pub enum Error {
|
||||
Util(UtilError),
|
||||
Block(BlockError),
|
||||
UnknownEngineName(String),
|
||||
}
|
||||
|
||||
impl From<BlockError> for Error {
|
||||
fn from(err: BlockError) -> Error {
|
||||
Error::Block(err)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: uncomment below once https://github.com/rust-lang/rust/issues/27336 sorted.
|
||||
/*#![feature(concat_idents)]
|
||||
macro_rules! assimilate {
|
||||
($name:ident) => (
|
||||
impl From<concat_idents!($name, Error)> for Error {
|
||||
fn from(err: concat_idents!($name, Error)) -> Error {
|
||||
Error:: $name (err)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
assimilate!(FromHex);
|
||||
assimilate!(BaseData);*/
|
@ -17,13 +17,108 @@ impl Ethash {
|
||||
|
||||
impl Engine for 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 evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() }
|
||||
|
||||
/// 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) {
|
||||
let a = block.header().author.clone();
|
||||
block.state_mut().add_balance(&a, &decode(&self.spec().engine_params.get("blockReward").unwrap()));
|
||||
let reward = self.spec().engine_params.get("blockReward").map(|a| decode(&a)).unwrap_or(U256::from(0u64));
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
|
||||
let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap());
|
||||
if header.difficulty < min_difficulty {
|
||||
return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: min_difficulty, found: header.difficulty })))
|
||||
}
|
||||
let min_gas_limit = decode(self.spec().engine_params.get("minGasLimit").unwrap());
|
||||
if header.gas_limit < min_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();
|
||||
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() })));
|
||||
}
|
||||
// TODO: Verify seal (quick)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn verify_block_unordered(&self, _header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
|
||||
// TODO: Verify seal (full)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn verify_block_final(&self, header: &Header, parent: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
|
||||
// Check difficulty is correct given the two timestamps.
|
||||
let expected_difficulty = self.calculate_difficuty(header, parent);
|
||||
if header.difficulty != expected_difficulty {
|
||||
return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: expected_difficulty, found: header.difficulty })))
|
||||
}
|
||||
let gas_limit_divisor = decode(self.spec().engine_params.get("gasLimitBoundDivisor").unwrap());
|
||||
let min_gas = parent.gas_limit - parent.gas_limit / gas_limit_divisor;
|
||||
let max_gas = parent.gas_limit + parent.gas_limit / gas_limit_divisor;
|
||||
if header.gas_limit <= min_gas || header.gas_limit >= max_gas {
|
||||
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { min: min_gas, max: max_gas, found: header.gas_limit })));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn verify_transaction(&self, _t: &Transaction, _header: &Header) -> Result<(), Error> { Ok(()) }
|
||||
}
|
||||
|
||||
impl Ethash {
|
||||
fn calculate_difficuty(&self, header: &Header, parent: &Header) -> U256 {
|
||||
const EXP_DIFF_PERIOD: u64 = 100000;
|
||||
if header.number == 0 {
|
||||
panic!("Can't calculate genesis block difficulty");
|
||||
}
|
||||
|
||||
let min_difficulty = decode(self.spec().engine_params.get("minimumDifficulty").unwrap());
|
||||
let difficulty_bound_divisor = decode(self.spec().engine_params.get("difficultyBoundDivisor").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 mut target = if header.number < frontier_limit {
|
||||
if header.timestamp >= parent.timestamp + duration_limit {
|
||||
parent.difficulty - (parent.difficulty / difficulty_bound_divisor)
|
||||
}
|
||||
else {
|
||||
parent.difficulty + (parent.difficulty / difficulty_bound_divisor)
|
||||
}
|
||||
}
|
||||
else {
|
||||
let diff_inc = (header.timestamp - parent.timestamp) / 10;
|
||||
if diff_inc <= 1 {
|
||||
parent.difficulty + parent.difficulty / From::from(2048) * From::from(1 - diff_inc)
|
||||
}
|
||||
else {
|
||||
parent.difficulty - parent.difficulty / From::from(2048) * From::from(max(diff_inc - 1, 99))
|
||||
}
|
||||
};
|
||||
target = max(min_difficulty, target);
|
||||
let period = ((parent.number + 1) / EXP_DIFF_PERIOD) as usize;
|
||||
if period > 1 {
|
||||
target = max(min_difficulty, target + (U256::from(1) << (period - 2)));
|
||||
}
|
||||
target
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,7 +129,10 @@ fn on_close_block() {
|
||||
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()]);
|
||||
let b = b.close(vec![], Address::zero(), vec![]);
|
||||
assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244F40000").unwrap());
|
||||
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();
|
||||
assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244f40000").unwrap());
|
||||
}
|
||||
|
||||
// TODO: difficulty test
|
||||
|
@ -52,7 +52,7 @@ mod tests {
|
||||
fn 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();
|
||||
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap());
|
||||
|
||||
@ -63,7 +63,7 @@ mod tests {
|
||||
fn 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();
|
||||
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap());
|
||||
|
||||
|
@ -12,7 +12,8 @@ use env_info::*;
|
||||
use evm_schedule::*;
|
||||
use engine::*;
|
||||
use transaction::*;
|
||||
use evm::{VmFactory, Ext, LogEntry, EvmParams, EvmResult, EvmError};
|
||||
use log_entry::*;
|
||||
use evm::{VmFactory, Ext, EvmParams, EvmResult, EvmError};
|
||||
|
||||
/// Returns new address created from address and given nonce.
|
||||
pub fn contract_address(address: &Address, nonce: &U256) -> Address {
|
||||
@ -167,8 +168,8 @@ impl<'a> Executive<'a> {
|
||||
self.state.inc_nonce(&sender);
|
||||
let mut substate = Substate::new();
|
||||
|
||||
let res = match t.kind() {
|
||||
TransactionKind::ContractCreation => {
|
||||
let res = match t.action() {
|
||||
&Action::Create => {
|
||||
let params = EvmParams {
|
||||
address: contract_address(&sender, &nonce),
|
||||
sender: sender.clone(),
|
||||
@ -179,20 +180,20 @@ impl<'a> Executive<'a> {
|
||||
code: t.data.clone(),
|
||||
data: vec![],
|
||||
};
|
||||
self.call(¶ms, &mut substate, &mut [])
|
||||
self.create(¶ms, &mut substate)
|
||||
},
|
||||
TransactionKind::MessageCall => {
|
||||
&Action::Call(ref address) => {
|
||||
let params = EvmParams {
|
||||
address: t.to.clone().unwrap(),
|
||||
address: address.clone(),
|
||||
sender: sender.clone(),
|
||||
origin: sender.clone(),
|
||||
gas: t.gas,
|
||||
gas_price: t.gas_price,
|
||||
value: t.value,
|
||||
code: self.state.code(&t.to.clone().unwrap()).unwrap_or(vec![]),
|
||||
code: self.state.code(address).unwrap_or(vec![]),
|
||||
data: t.data.clone(),
|
||||
};
|
||||
self.create(¶ms, &mut substate)
|
||||
self.call(¶ms, &mut substate, &mut [])
|
||||
}
|
||||
};
|
||||
|
||||
@ -337,10 +338,10 @@ impl<'a> Ext for Externalities<'a> {
|
||||
}
|
||||
|
||||
fn blockhash(&self, number: &U256) -> H256 {
|
||||
match *number < self.info.number {
|
||||
match *number < U256::from(self.info.number) {
|
||||
false => H256::from(&U256::zero()),
|
||||
true => {
|
||||
let index = self.info.number - *number - U256::one();
|
||||
let index = U256::from(self.info.number) - *number - U256::one();
|
||||
self.info.last_hashes[index.low_u32() as usize].clone()
|
||||
}
|
||||
}
|
||||
|
@ -678,7 +678,7 @@ mod tests {
|
||||
|
||||
let mut state = State::new_temp();
|
||||
let mut info = EnvInfo::new();
|
||||
info.number = U256::one();
|
||||
info.number = 1;
|
||||
info.last_hashes.push(H256::from(address.clone()));
|
||||
let engine = TestEngine::new();
|
||||
let mut substate = Substate::new();
|
||||
@ -704,7 +704,7 @@ mod tests {
|
||||
|
||||
let mut state = State::new_temp();
|
||||
let mut info = EnvInfo::new();
|
||||
info.number = U256::one();
|
||||
info.number = 1;
|
||||
info.last_hashes.push(H256::from(address.clone()));
|
||||
let engine = TestEngine::new();
|
||||
let mut substate = Substate::new();
|
||||
|
@ -3,7 +3,7 @@
|
||||
pub mod ext;
|
||||
pub mod evm;
|
||||
pub mod vmfactory;
|
||||
pub mod logentry;
|
||||
//pub mod logentry;
|
||||
pub mod executive;
|
||||
pub mod params;
|
||||
#[cfg(feature = "jit" )]
|
||||
@ -11,7 +11,7 @@ mod jit;
|
||||
|
||||
pub use self::evm::{Evm, EvmError, EvmResult};
|
||||
pub use self::ext::{Ext};
|
||||
pub use self::logentry::LogEntry;
|
||||
//pub use self::logentry::LogEntry;
|
||||
pub use self::vmfactory::VmFactory;
|
||||
// TODO: reduce this to absolutely necessary things
|
||||
pub use self::executive::{Executive, ExecutionResult, Externalities, Substate, OutputPolicy};
|
||||
|
@ -1,4 +1,5 @@
|
||||
use util::*;
|
||||
use header::BlockNumber;
|
||||
use rocksdb::{DB, Writable};
|
||||
|
||||
/// 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
|
||||
pub trait ExtrasIndexable {
|
||||
fn extras_index() -> ExtrasIndex;
|
||||
@ -88,7 +96,7 @@ impl ExtrasIndexable for H256 {
|
||||
/// Familial details concerning a block
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BlockDetails {
|
||||
pub number: U256,
|
||||
pub number: BlockNumber,
|
||||
pub total_difficulty: U256,
|
||||
pub parent: H256,
|
||||
pub children: Vec<H256>
|
||||
|
106
src/header.rs
106
src/header.rs
@ -1,5 +1,8 @@
|
||||
use util::*;
|
||||
use basic_types::*;
|
||||
use time::now_utc;
|
||||
|
||||
pub type BlockNumber = u64;
|
||||
|
||||
/// A block header.
|
||||
///
|
||||
@ -9,9 +12,10 @@ use basic_types::*;
|
||||
/// Doesn't do all that much on its own.
|
||||
#[derive(Debug)]
|
||||
pub struct Header {
|
||||
// TODO: make all private.
|
||||
pub parent_hash: H256,
|
||||
pub timestamp: U256,
|
||||
pub number: U256,
|
||||
pub timestamp: u64,
|
||||
pub number: BlockNumber,
|
||||
pub author: Address,
|
||||
|
||||
pub transactions_root: H256,
|
||||
@ -27,7 +31,7 @@ pub struct Header {
|
||||
pub difficulty: U256,
|
||||
pub seal: Vec<Bytes>,
|
||||
|
||||
pub hash: RefCell<Option<H256>>, //TODO: make this private
|
||||
pub hash: RefCell<Option<H256>>,
|
||||
}
|
||||
|
||||
pub enum Seal {
|
||||
@ -40,26 +44,48 @@ impl Header {
|
||||
pub fn new() -> Header {
|
||||
Header {
|
||||
parent_hash: ZERO_H256.clone(),
|
||||
timestamp: BAD_U256.clone(),
|
||||
number: ZERO_U256.clone(),
|
||||
timestamp: 0,
|
||||
number: 0,
|
||||
author: ZERO_ADDRESS.clone(),
|
||||
|
||||
transactions_root: ZERO_H256.clone(),
|
||||
uncles_hash: ZERO_H256.clone(),
|
||||
transactions_root: SHA3_NULL_RLP,
|
||||
uncles_hash: SHA3_EMPTY_LIST_RLP,
|
||||
extra_data: vec![],
|
||||
|
||||
state_root: ZERO_H256.clone(),
|
||||
receipts_root: ZERO_H256.clone(),
|
||||
state_root: SHA3_NULL_RLP,
|
||||
receipts_root: SHA3_NULL_RLP,
|
||||
log_bloom: ZERO_LOGBLOOM.clone(),
|
||||
gas_used: ZERO_U256.clone(),
|
||||
gas_limit: ZERO_U256.clone(),
|
||||
gas_used: ZERO_U256,
|
||||
gas_limit: ZERO_U256,
|
||||
|
||||
difficulty: ZERO_U256.clone(),
|
||||
difficulty: ZERO_U256,
|
||||
seal: vec![],
|
||||
hash: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn number(&self) -> BlockNumber { self.number }
|
||||
pub fn timestamp(&self) -> u64 { self.timestamp }
|
||||
pub fn author(&self) -> &Address { &self.author }
|
||||
|
||||
pub fn extra_data(&self) -> &Bytes { &self.extra_data }
|
||||
|
||||
pub fn state_root(&self) -> &H256 { &self.state_root }
|
||||
pub fn receipts_root(&self) -> &H256 { &self.receipts_root }
|
||||
|
||||
pub fn seal(&self) -> &Vec<Bytes> { &self.seal }
|
||||
|
||||
// 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_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(); }
|
||||
|
||||
/// Get the hash of this header (sha3 of the RLP).
|
||||
pub fn hash(&self) -> H256 {
|
||||
let mut hash = self.hash.borrow_mut();
|
||||
@ -112,28 +138,28 @@ impl Header {
|
||||
|
||||
impl Decodable for Header {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
let d = try!(decoder.as_list());
|
||||
let r = decoder.as_rlp();
|
||||
|
||||
let mut blockheader = Header {
|
||||
parent_hash: try!(Decodable::decode(&d[0])),
|
||||
uncles_hash: try!(Decodable::decode(&d[1])),
|
||||
author: 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])),
|
||||
log_bloom: try!(Decodable::decode(&d[6])),
|
||||
difficulty: try!(Decodable::decode(&d[7])),
|
||||
number: try!(Decodable::decode(&d[8])),
|
||||
gas_limit: try!(Decodable::decode(&d[9])),
|
||||
gas_used: try!(Decodable::decode(&d[10])),
|
||||
timestamp: try!(Decodable::decode(&d[11])),
|
||||
extra_data: try!(Decodable::decode(&d[12])),
|
||||
parent_hash: try!(r.val_at(0)),
|
||||
uncles_hash: try!(r.val_at(1)),
|
||||
author: try!(r.val_at(2)),
|
||||
state_root: try!(r.val_at(3)),
|
||||
transactions_root: try!(r.val_at(4)),
|
||||
receipts_root: try!(r.val_at(5)),
|
||||
log_bloom: try!(r.val_at(6)),
|
||||
difficulty: try!(r.val_at(7)),
|
||||
number: try!(r.val_at(8)),
|
||||
gas_limit: try!(r.val_at(9)),
|
||||
gas_used: try!(r.val_at(10)),
|
||||
timestamp: try!(r.val_at(11)),
|
||||
extra_data: try!(r.val_at(12)),
|
||||
seal: vec![],
|
||||
hash: RefCell::new(None),
|
||||
hash: RefCell::new(Some(r.as_raw().sha3()))
|
||||
};
|
||||
|
||||
for i in 13..d.len() {
|
||||
blockheader.seal.push(d[i].as_raw().to_vec());
|
||||
for i in 13..r.item_count() {
|
||||
blockheader.seal.push(try!(r.at(i)).as_raw().to_vec())
|
||||
}
|
||||
|
||||
Ok(blockheader)
|
||||
@ -163,28 +189,6 @@ impl Encodable for Header {
|
||||
})
|
||||
}
|
||||
}
|
||||
/*
|
||||
trait RlpStandard {
|
||||
fn append(&self, s: &mut RlpStream);
|
||||
}
|
||||
|
||||
impl RlpStandard for Header {
|
||||
fn append(&self, s: &mut RlpStream) {
|
||||
s.append_list(13);
|
||||
s.append(self.parent_hash);
|
||||
s.append_raw(self.seal[0]);
|
||||
s.append_standard(self.x);
|
||||
}
|
||||
fn populate(&mut self, s: &Rlp) {
|
||||
}
|
||||
}
|
||||
|
||||
impl RlpStream {
|
||||
fn append_standard<O>(&mut self, o: &O) where O: RlpStandard {
|
||||
o.append(self);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
@ -78,6 +78,7 @@ extern crate flate2;
|
||||
extern crate rocksdb;
|
||||
extern crate heapsize;
|
||||
extern crate crypto;
|
||||
extern crate time;
|
||||
|
||||
extern crate env_logger;
|
||||
#[cfg(feature = "jit" )]
|
||||
@ -87,6 +88,8 @@ extern crate ethcore_util as util;
|
||||
|
||||
pub mod common;
|
||||
pub mod basic_types;
|
||||
pub mod error;
|
||||
pub mod log_entry;
|
||||
pub mod env_info;
|
||||
pub mod engine;
|
||||
pub mod state;
|
||||
@ -102,6 +105,10 @@ pub mod views;
|
||||
pub mod blockchain;
|
||||
pub mod extras;
|
||||
pub mod evm;
|
||||
pub mod block;
|
||||
|
||||
pub mod client;
|
||||
pub mod sync;
|
||||
pub mod block;
|
||||
pub mod verification;
|
||||
pub mod queue;
|
||||
pub mod ethereum;
|
||||
|
@ -1,17 +1,28 @@
|
||||
//! Transaction log entry.
|
||||
use util::hash::*;
|
||||
use util::bytes::*;
|
||||
use util::sha3::*;
|
||||
use util::*;
|
||||
use basic_types::LogBloom;
|
||||
|
||||
/// Data sturcture used to represent Evm log entry.
|
||||
/// A single log's entry.
|
||||
pub struct LogEntry {
|
||||
address: Address,
|
||||
topics: Vec<H256>,
|
||||
data: Bytes
|
||||
pub address: Address,
|
||||
pub topics: Vec<H256>,
|
||||
pub data: Bytes,
|
||||
}
|
||||
|
||||
impl RlpStandard for LogEntry {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.append_list(3);
|
||||
s.append(&self.address);
|
||||
s.append(&self.topics);
|
||||
s.append(&self.data);
|
||||
}
|
||||
}
|
||||
|
||||
impl LogEntry {
|
||||
/// This function should be called to create new log entry.
|
||||
pub fn bloom(&self) -> LogBloom {
|
||||
self.topics.iter().fold(LogBloom::from_bloomed(&self.address.sha3()), |b, t| b.with_bloomed(&t.sha3()))
|
||||
}
|
||||
|
||||
/// Create a new log entry.
|
||||
pub fn new(address: Address, topics: Vec<H256>, data: Bytes) -> LogEntry {
|
||||
LogEntry {
|
||||
address: address,
|
||||
@ -26,7 +37,7 @@ impl LogEntry {
|
||||
}
|
||||
|
||||
/// Returns reference to topics.
|
||||
pub fn topics(&self) -> &[H256] {
|
||||
pub fn topics(&self) -> &Vec<H256> {
|
||||
&self.topics
|
||||
}
|
||||
|
||||
@ -34,24 +45,12 @@ impl LogEntry {
|
||||
pub fn data(&self) -> &Bytes {
|
||||
&self.data
|
||||
}
|
||||
|
||||
/// Returns log bloom of given log entry.
|
||||
pub fn bloom(&self) -> H2048 {
|
||||
let mut bloom = H2048::new();
|
||||
bloom.shift_bloom(&self.address.sha3());
|
||||
for topic in self.topics.iter() {
|
||||
bloom.shift_bloom(&topic.sha3());
|
||||
}
|
||||
bloom
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::str::FromStr;
|
||||
use util::hash::*;
|
||||
use util::bytes::*;
|
||||
use evm::LogEntry;
|
||||
use util::*;
|
||||
use super::LogEntry;
|
||||
|
||||
#[test]
|
||||
fn test_empty_log_bloom() {
|
41
src/queue.rs
Normal file
41
src/queue.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use util::*;
|
||||
use blockchain::BlockChain;
|
||||
use views::{BlockView};
|
||||
use verification::*;
|
||||
use error::*;
|
||||
use engine::Engine;
|
||||
|
||||
/// A queue of blocks. Sits between network or other I/O and the BlockChain.
|
||||
/// Sorts them ready for blockchain insertion.
|
||||
pub struct BlockQueue {
|
||||
bc: Arc<RwLock<BlockChain>>,
|
||||
engine: Arc<Box<Engine>>,
|
||||
}
|
||||
|
||||
impl BlockQueue {
|
||||
/// Creates a new queue instance.
|
||||
pub fn new(bc: Arc<RwLock<BlockChain>>, engine: Arc<Box<Engine>>) -> BlockQueue {
|
||||
BlockQueue {
|
||||
bc: bc,
|
||||
engine: engine,
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear the queue and stop verification activity.
|
||||
pub fn clear(&mut self) {
|
||||
}
|
||||
|
||||
/// Add a block to the queue.
|
||||
pub fn import_block(&mut self, bytes: &[u8]) -> ImportResult {
|
||||
let header = BlockView::new(bytes).header();
|
||||
if self.bc.read().unwrap().is_known(&header.hash()) {
|
||||
return Err(ImportError::AlreadyInChain);
|
||||
}
|
||||
try!(verify_block_basic(bytes, self.engine.deref().deref()));
|
||||
try!(verify_block_unordered(bytes, self.engine.deref().deref()));
|
||||
try!(verify_block_final(bytes, self.engine.deref().deref(), self.bc.read().unwrap().deref()));
|
||||
self.bc.write().unwrap().insert_block(bytes);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,26 @@
|
||||
use util::*;
|
||||
use basic_types::LogBloom;
|
||||
use log_entry::LogEntry;
|
||||
|
||||
/// Information describing execution of a transaction.
|
||||
pub struct Receipt {
|
||||
// TODO
|
||||
pub state_root: H256,
|
||||
pub gas_used: U256,
|
||||
pub log_bloom: LogBloom,
|
||||
pub logs: Vec<LogEntry>,
|
||||
}
|
||||
|
||||
impl RlpStandard for Receipt {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.append_list(4);
|
||||
s.append(&self.state_root);
|
||||
s.append(&self.gas_used);
|
||||
s.append(&self.log_bloom);
|
||||
// TODO: make work:
|
||||
//s.append(&self.logs);
|
||||
s.append_list(self.logs.len());
|
||||
for l in self.logs.iter() {
|
||||
l.rlp_append(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
51
src/spec.rs
51
src/spec.rs
@ -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
|
||||
/// chain and those to be interpreted by the active chain engine.
|
||||
#[derive(Debug)]
|
||||
@ -60,40 +81,40 @@ pub struct Spec {
|
||||
pub difficulty: U256,
|
||||
pub gas_limit: U256,
|
||||
pub gas_used: U256,
|
||||
pub timestamp: U256,
|
||||
pub timestamp: u64,
|
||||
pub extra_data: Bytes,
|
||||
pub genesis_state: HashMap<Address, Account>,
|
||||
pub genesis_state: HashMap<Address, GenesisAccount>,
|
||||
pub seal_fields: usize,
|
||||
pub seal_rlp: Bytes,
|
||||
|
||||
// May be prepopulated if we know this in advance.
|
||||
state_root_memo: RefCell<Option<H256>>,
|
||||
state_root_memo: RwLock<Option<H256>>,
|
||||
}
|
||||
|
||||
impl Spec {
|
||||
/// Convert this object into a boxed Engine of the right underlying type.
|
||||
// TODO avoid this hard-coded nastiness - use dynamic-linked plugin framework instead.
|
||||
pub fn to_engine(self) -> Result<Box<Engine>, EthcoreError> {
|
||||
pub fn to_engine(self) -> Result<Box<Engine>, Error> {
|
||||
match self.engine_name.as_ref() {
|
||||
"NullEngine" => Ok(NullEngine::new_boxed(self)),
|
||||
"Ethash" => Ok(super::ethereum::Ethash::new_boxed(self)),
|
||||
_ => Err(EthcoreError::UnknownName)
|
||||
_ => Err(Error::UnknownEngineName(self.engine_name.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the state root for the genesis state, memoising accordingly.
|
||||
pub fn state_root(&self) -> Ref<H256> {
|
||||
if self.state_root_memo.borrow().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()));
|
||||
pub fn state_root(&self) -> H256 {
|
||||
if self.state_root_memo.read().unwrap().is_none() {
|
||||
*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 {
|
||||
Header {
|
||||
parent_hash: self.parent_hash.clone(),
|
||||
timestamp: self.timestamp.clone(),
|
||||
number: U256::from(0u8),
|
||||
timestamp: self.timestamp,
|
||||
number: 0,
|
||||
author: self.author.clone(),
|
||||
transactions_root: SHA3_NULL_RLP.clone(),
|
||||
uncles_hash: RlpStream::new_list(0).out().sha3(),
|
||||
@ -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)};
|
||||
// TODO: handle code & data if they exist.
|
||||
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(),
|
||||
gas_limit: U256::from_str(&genesis["gasLimit"].as_string().unwrap()[2..]).unwrap(),
|
||||
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(),
|
||||
genesis_state: state,
|
||||
seal_fields: seal_fields,
|
||||
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() {
|
||||
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();
|
||||
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap());
|
||||
|
||||
|
10
src/state.rs
10
src/state.rs
@ -1,8 +1,4 @@
|
||||
use util::*;
|
||||
use account::Account;
|
||||
use transaction::Transaction;
|
||||
use receipt::Receipt;
|
||||
use env_info::EnvInfo;
|
||||
use common::*;
|
||||
use engine::Engine;
|
||||
|
||||
/// Information concerning the result of the `State::apply` operation.
|
||||
@ -10,7 +6,7 @@ pub struct ApplyInfo {
|
||||
pub receipt: Receipt,
|
||||
}
|
||||
|
||||
pub type ApplyResult = Result<ApplyInfo, EthcoreError>;
|
||||
pub type ApplyResult = Result<ApplyInfo, Error>;
|
||||
|
||||
/// Representation of the entire state of all accounts in the system.
|
||||
pub struct State {
|
||||
@ -138,7 +134,7 @@ impl State {
|
||||
|
||||
/// Execute a given transaction.
|
||||
/// This will change the state accordingly.
|
||||
pub fn apply(&mut self, _env_info: &EnvInfo, _engine: &Engine, _t: &Transaction, _is_permanent: bool) -> ApplyResult {
|
||||
pub fn apply(&mut self, _env_info: &EnvInfo, _engine: &Engine, _t: &Transaction) -> ApplyResult {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
|
970
src/sync/chain.rs
Normal file
970
src/sync/chain.rs
Normal file
@ -0,0 +1,970 @@
|
||||
///
|
||||
/// BlockChain synchronization strategy.
|
||||
/// Syncs to peers and keeps up to date.
|
||||
/// This implementation uses ethereum protocol v63
|
||||
///
|
||||
/// Syncing strategy.
|
||||
///
|
||||
/// 1. A peer arrives with a total difficulty better than ours
|
||||
/// 2. Find a common best block between our an peer chain.
|
||||
/// Start with out best block and request headers from peer backwards until a common block is found
|
||||
/// 3. Download headers and block bodies from peers in parallel.
|
||||
/// As soon as a set of the blocks is fully downloaded at the head of the queue it is fed to the blockchain
|
||||
/// 4. Maintain sync by handling NewBlocks/NewHashes messages
|
||||
///
|
||||
|
||||
use util::*;
|
||||
use std::mem::{replace};
|
||||
use views::{HeaderView};
|
||||
use header::{BlockNumber, Header as BlockHeader};
|
||||
use client::{BlockChainClient, BlockStatus};
|
||||
use sync::range_collection::{RangeCollection, ToUsize, FromUsize};
|
||||
use error::*;
|
||||
use sync::io::SyncIo;
|
||||
|
||||
impl ToUsize for BlockNumber {
|
||||
fn to_usize(&self) -> usize {
|
||||
*self as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl FromUsize for BlockNumber {
|
||||
fn from_usize(s: usize) -> BlockNumber {
|
||||
s as BlockNumber
|
||||
}
|
||||
}
|
||||
|
||||
type PacketDecodeError = DecoderError;
|
||||
|
||||
const PROTOCOL_VERSION: u8 = 63u8;
|
||||
const MAX_BODIES_TO_SEND: usize = 256;
|
||||
const MAX_HEADERS_TO_SEND: usize = 512;
|
||||
const MAX_NODE_DATA_TO_SEND: usize = 1024;
|
||||
const MAX_RECEIPTS_TO_SEND: usize = 1024;
|
||||
const MAX_HEADERS_TO_REQUEST: usize = 512;
|
||||
const MAX_BODIES_TO_REQUEST: usize = 256;
|
||||
|
||||
const STATUS_PACKET: u8 = 0x00;
|
||||
const NEW_BLOCK_HASHES_PACKET: u8 = 0x01;
|
||||
const TRANSACTIONS_PACKET: u8 = 0x02;
|
||||
const GET_BLOCK_HEADERS_PACKET: u8 = 0x03;
|
||||
const BLOCK_HEADERS_PACKET: u8 = 0x04;
|
||||
const GET_BLOCK_BODIES_PACKET: u8 = 0x05;
|
||||
const BLOCK_BODIES_PACKET: u8 = 0x06;
|
||||
const NEW_BLOCK_PACKET: u8 = 0x07;
|
||||
|
||||
const GET_NODE_DATA_PACKET: u8 = 0x0d;
|
||||
const NODE_DATA_PACKET: u8 = 0x0e;
|
||||
const GET_RECEIPTS_PACKET: u8 = 0x0f;
|
||||
const RECEIPTS_PACKET: u8 = 0x10;
|
||||
|
||||
const NETWORK_ID: U256 = ONE_U256; //TODO: get this from parent
|
||||
|
||||
struct Header {
|
||||
/// Header data
|
||||
data: Bytes,
|
||||
/// Block hash
|
||||
hash: H256,
|
||||
/// Parent hash
|
||||
parent: H256,
|
||||
}
|
||||
|
||||
/// Used to identify header by transactions and uncles hashes
|
||||
#[derive(Eq, PartialEq, Hash)]
|
||||
struct HeaderId {
|
||||
transactions_root: H256,
|
||||
uncles: H256
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||
/// Sync state
|
||||
pub enum SyncState {
|
||||
/// Initial chain sync has not started yet
|
||||
NotSynced,
|
||||
/// Initial chain sync complete. Waiting for new packets
|
||||
Idle,
|
||||
/// Block downloading paused. Waiting for block queue to process blocks and free some space
|
||||
Waiting,
|
||||
/// Downloading blocks
|
||||
Blocks,
|
||||
/// Downloading blocks learned from NewHashes packet
|
||||
NewBlocks,
|
||||
}
|
||||
|
||||
/// Syncing status and statistics
|
||||
pub struct SyncStatus {
|
||||
/// State
|
||||
pub state: SyncState,
|
||||
/// Syncing protocol version. That's the maximum protocol version we connect to.
|
||||
pub protocol_version: u8,
|
||||
/// BlockChain height for the moment the sync started.
|
||||
pub start_block_number: BlockNumber,
|
||||
/// Last fully downloaded and imported block number.
|
||||
pub last_imported_block_number: BlockNumber,
|
||||
/// Highest block number in the download queue.
|
||||
pub highest_block_number: BlockNumber,
|
||||
/// Total number of blocks for the sync process.
|
||||
pub blocks_total: usize,
|
||||
/// Number of blocks downloaded so far.
|
||||
pub blocks_received: usize,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
/// Peer data type requested
|
||||
enum PeerAsking {
|
||||
Nothing,
|
||||
BlockHeaders,
|
||||
BlockBodies,
|
||||
}
|
||||
|
||||
/// Syncing peer information
|
||||
struct PeerInfo {
|
||||
/// eth protocol version
|
||||
protocol_version: u32,
|
||||
/// Peer chain genesis hash
|
||||
genesis: H256,
|
||||
/// Peer network id
|
||||
network_id: U256,
|
||||
/// Peer best block hash
|
||||
latest: H256,
|
||||
/// Peer total difficulty
|
||||
difficulty: U256,
|
||||
/// Type of data currenty being requested from peer.
|
||||
asking: PeerAsking,
|
||||
/// A set of block numbers being requested
|
||||
asking_blocks: Vec<BlockNumber>,
|
||||
}
|
||||
|
||||
/// Blockchain sync handler.
|
||||
/// See module documentation for more details.
|
||||
pub struct ChainSync {
|
||||
/// Sync state
|
||||
state: SyncState,
|
||||
/// Last block number for the start of sync
|
||||
starting_block: BlockNumber,
|
||||
/// Highest block number seen
|
||||
highest_block: BlockNumber,
|
||||
/// Set of block header numbers being downloaded
|
||||
downloading_headers: HashSet<BlockNumber>,
|
||||
/// Set of block body numbers being downloaded
|
||||
downloading_bodies: HashSet<BlockNumber>,
|
||||
/// 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
|
||||
/// Downloaded bodies
|
||||
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
|
||||
peers: HashMap<PeerId, PeerInfo>,
|
||||
/// Used to map body to header
|
||||
header_ids: HashMap<HeaderId, BlockNumber>,
|
||||
/// Last impoted block number
|
||||
last_imported_block: BlockNumber,
|
||||
/// Last impoted block hash
|
||||
last_imported_hash: H256,
|
||||
/// Syncing total difficulty
|
||||
syncing_difficulty: U256,
|
||||
/// True if common block for our and remote chain has been found
|
||||
have_common_block: bool,
|
||||
}
|
||||
|
||||
|
||||
impl ChainSync {
|
||||
/// Create a new instance of syncing strategy.
|
||||
pub fn new() -> ChainSync {
|
||||
ChainSync {
|
||||
state: SyncState::NotSynced,
|
||||
starting_block: 0,
|
||||
highest_block: 0,
|
||||
downloading_headers: HashSet::new(),
|
||||
downloading_bodies: HashSet::new(),
|
||||
headers: Vec::new(),
|
||||
bodies: Vec::new(),
|
||||
peers: HashMap::new(),
|
||||
header_ids: HashMap::new(),
|
||||
last_imported_block: 0,
|
||||
last_imported_hash: H256::new(),
|
||||
syncing_difficulty: U256::from(0u64),
|
||||
have_common_block: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// @returns Synchonization status
|
||||
pub fn status(&self) -> SyncStatus {
|
||||
SyncStatus {
|
||||
state: self.state.clone(),
|
||||
protocol_version: 63,
|
||||
start_block_number: self.starting_block,
|
||||
last_imported_block_number: self.last_imported_block,
|
||||
highest_block_number: self.highest_block,
|
||||
blocks_total: (self.last_imported_block - self.starting_block) as usize,
|
||||
blocks_received: (self.highest_block - self.starting_block) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
/// Abort all sync activity
|
||||
pub fn abort(&mut self, io: &mut SyncIo) {
|
||||
self.restart(io);
|
||||
self.peers.clear();
|
||||
}
|
||||
|
||||
/// Rest sync. Clear all downloaded data but keep the queue
|
||||
fn reset(&mut self) {
|
||||
self.downloading_headers.clear();
|
||||
self.downloading_bodies.clear();
|
||||
self.headers.clear();
|
||||
self.bodies.clear();
|
||||
for (_, ref mut p) in self.peers.iter_mut() {
|
||||
p.asking_blocks.clear();
|
||||
}
|
||||
self.header_ids.clear();
|
||||
self.syncing_difficulty = From::from(0u64);
|
||||
self.state = SyncState::Idle;
|
||||
}
|
||||
|
||||
/// Restart sync
|
||||
pub fn restart(&mut self, io: &mut SyncIo) {
|
||||
self.reset();
|
||||
self.last_imported_block = 0;
|
||||
self.last_imported_hash = H256::new();
|
||||
self.starting_block = 0;
|
||||
self.highest_block = 0;
|
||||
self.have_common_block = false;
|
||||
io.chain().clear_queue();
|
||||
self.starting_block = io.chain().chain_info().best_block_number;
|
||||
self.state = SyncState::NotSynced;
|
||||
}
|
||||
|
||||
/// Called by peer to report status
|
||||
fn on_peer_status(&mut self, io: &mut SyncIo, peer_id: &PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
||||
let peer = PeerInfo {
|
||||
protocol_version: try!(r.val_at(0)),
|
||||
network_id: try!(r.val_at(1)),
|
||||
difficulty: try!(r.val_at(2)),
|
||||
latest: try!(r.val_at(3)),
|
||||
genesis: try!(r.val_at(4)),
|
||||
asking: PeerAsking::Nothing,
|
||||
asking_blocks: Vec::new(),
|
||||
};
|
||||
|
||||
trace!(target: "sync", "New peer {} (protocol: {}, network: {:?}, difficulty: {:?}, latest:{}, genesis:{})", peer_id, peer.protocol_version, peer.network_id, peer.difficulty, peer.latest, peer.genesis);
|
||||
|
||||
let chain_info = io.chain().chain_info();
|
||||
if peer.genesis != chain_info.genesis_hash {
|
||||
io.disable_peer(peer_id);
|
||||
trace!(target: "sync", "Peer {} genesis hash not matched", peer_id);
|
||||
return Ok(());
|
||||
}
|
||||
if peer.network_id != NETWORK_ID {
|
||||
io.disable_peer(peer_id);
|
||||
trace!(target: "sync", "Peer {} network id not matched", peer_id);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let old = self.peers.insert(peer_id.clone(), peer);
|
||||
if old.is_some() {
|
||||
panic!("ChainSync: new peer already exists");
|
||||
}
|
||||
self.sync_peer(io, peer_id, false);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Called by peer once it has new block headers during sync
|
||||
fn on_peer_block_headers(&mut self, io: &mut SyncIo, peer_id: &PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
||||
self.reset_peer_asking(peer_id, PeerAsking::BlockHeaders);
|
||||
let item_count = r.item_count();
|
||||
trace!(target: "sync", "{} -> BlockHeaders ({} entries)", peer_id, item_count);
|
||||
self.clear_peer_download(peer_id);
|
||||
if self.state != SyncState::Blocks && self.state != SyncState::NewBlocks && self.state != SyncState::Waiting {
|
||||
trace!(target: "sync", "Ignored unexpected block headers");
|
||||
return Ok(());
|
||||
}
|
||||
if self.state == SyncState::Waiting {
|
||||
trace!(target: "sync", "Ignored block headers while waiting");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
for i in 0..item_count {
|
||||
let info: BlockHeader = try!(r.val_at(i));
|
||||
let number = BlockNumber::from(info.number);
|
||||
if number <= self.last_imported_block || self.headers.have_item(&number) {
|
||||
trace!(target: "sync", "Skipping existing block header");
|
||||
continue;
|
||||
}
|
||||
if number > self.highest_block {
|
||||
self.highest_block = number;
|
||||
}
|
||||
let hash = info.hash();
|
||||
match io.chain().block_status(&hash) {
|
||||
BlockStatus::InChain => {
|
||||
self.have_common_block = true;
|
||||
self.last_imported_block = number;
|
||||
self.last_imported_hash = hash.clone();
|
||||
trace!(target: "sync", "Found common header {} ({})", number, hash);
|
||||
},
|
||||
_ => {
|
||||
if self.have_common_block {
|
||||
//validate chain
|
||||
if self.have_common_block && number == self.last_imported_block + 1 && info.parent_hash != self.last_imported_hash {
|
||||
// TODO: lower peer rating
|
||||
debug!(target: "sync", "Mismatched block header {} {}", number, hash);
|
||||
continue;
|
||||
}
|
||||
if self.headers.find_item(&(number - 1)).map_or(false, |p| p.hash != info.parent_hash) {
|
||||
// mismatching parent id, delete the previous block and don't add this one
|
||||
// TODO: lower peer rating
|
||||
debug!(target: "sync", "Mismatched block header {} {}", number, hash);
|
||||
self.remove_downloaded_blocks(number - 1);
|
||||
continue;
|
||||
}
|
||||
if self.headers.find_item(&(number + 1)).map_or(false, |p| p.parent != hash) {
|
||||
// mismatching parent id for the next block, clear following headers
|
||||
debug!(target: "sync", "Mismatched block header {}", number + 1);
|
||||
self.remove_downloaded_blocks(number + 1);
|
||||
}
|
||||
}
|
||||
let hdr = Header {
|
||||
data: try!(r.at(i)).as_raw().to_vec(),
|
||||
hash: hash.clone(),
|
||||
parent: info.parent_hash,
|
||||
};
|
||||
self.headers.insert_item(number, hdr);
|
||||
let header_id = HeaderId {
|
||||
transactions_root: info.transactions_root,
|
||||
uncles: info.uncles_hash
|
||||
};
|
||||
trace!(target: "sync", "Got header {} ({})", number, hash);
|
||||
if header_id.transactions_root == rlp::SHA3_NULL_RLP && header_id.uncles == rlp::SHA3_EMPTY_LIST_RLP {
|
||||
//empty body, just mark as downloaded
|
||||
let mut body_stream = RlpStream::new_list(2);
|
||||
body_stream.append_raw(&rlp::NULL_RLP, 1);
|
||||
body_stream.append_raw(&rlp::EMPTY_LIST_RLP, 1);
|
||||
self.bodies.insert_item(number, body_stream.out());
|
||||
}
|
||||
else {
|
||||
self.header_ids.insert(header_id, number);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.collect_blocks(io);
|
||||
self.continue_sync(io);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Called by peer once it has new block bodies
|
||||
fn on_peer_block_bodies(&mut self, io: &mut SyncIo, peer_id: &PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
||||
use util::triehash::ordered_trie_root;
|
||||
self.reset_peer_asking(peer_id, PeerAsking::BlockBodies);
|
||||
let item_count = r.item_count();
|
||||
trace!(target: "sync", "{} -> BlockBodies ({} entries)", peer_id, item_count);
|
||||
self.clear_peer_download(peer_id);
|
||||
if self.state != SyncState::Blocks && self.state != SyncState::NewBlocks && self.state != SyncState::Waiting {
|
||||
trace!(target: "sync", "Ignored unexpected block bodies");
|
||||
return Ok(());
|
||||
}
|
||||
if self.state == SyncState::Waiting {
|
||||
trace!(target: "sync", "Ignored block bodies while waiting");
|
||||
return Ok(());
|
||||
}
|
||||
for i in 0..item_count {
|
||||
let body = try!(r.at(i));
|
||||
let tx = try!(body.at(0));
|
||||
let tx_root = ordered_trie_root(tx.iter().map(|r| r.as_raw().to_vec()).collect()); //TODO: get rid of vectors here
|
||||
let uncles = try!(body.at(1)).as_raw().sha3();
|
||||
let header_id = HeaderId {
|
||||
transactions_root: tx_root,
|
||||
uncles: uncles
|
||||
};
|
||||
match self.header_ids.get(&header_id).map(|n| *n) {
|
||||
Some(n) => {
|
||||
self.header_ids.remove(&header_id);
|
||||
self.bodies.insert_item(n, body.as_raw().to_vec());
|
||||
trace!(target: "sync", "Got body {}", n);
|
||||
}
|
||||
None => {
|
||||
debug!(target: "sync", "Ignored unknown block body");
|
||||
}
|
||||
}
|
||||
}
|
||||
self.collect_blocks(io);
|
||||
self.continue_sync(io);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Called by peer once it has new block bodies
|
||||
fn on_peer_new_block(&mut self, io: &mut SyncIo, peer_id: &PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
||||
let block_rlp = try!(r.at(0));
|
||||
let header_rlp = try!(block_rlp.at(0));
|
||||
let h = header_rlp.as_raw().sha3();
|
||||
|
||||
trace!(target: "sync", "{} -> NewBlock ({})", peer_id, h);
|
||||
let header_view = HeaderView::new(header_rlp.as_raw());
|
||||
// TODO: Decompose block and add to self.headers and self.bodies instead
|
||||
if header_view.number() == From::from(self.last_imported_block + 1) {
|
||||
match io.chain().import_block(block_rlp.as_raw()) {
|
||||
Err(ImportError::AlreadyInChain) => {
|
||||
trace!(target: "sync", "New block already in chain {:?}", h);
|
||||
},
|
||||
Err(ImportError::AlreadyQueued) => {
|
||||
trace!(target: "sync", "New block already queued {:?}", h);
|
||||
},
|
||||
Ok(()) => {
|
||||
trace!(target: "sync", "New block queued {:?}", h);
|
||||
},
|
||||
Err(e) => {
|
||||
debug!(target: "sync", "Bad new block {:?} : {:?}", h, e);
|
||||
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(())
|
||||
}
|
||||
|
||||
/// Handles NewHashes packet. Initiates headers download for any unknown hashes.
|
||||
fn on_peer_new_hashes(&mut self, io: &mut SyncIo, peer_id: &PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
||||
if self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer").asking != PeerAsking::Nothing {
|
||||
trace!(target: "sync", "Ignoring new hashes since we're already downloading.");
|
||||
return Ok(());
|
||||
}
|
||||
trace!(target: "sync", "{} -> NewHashes ({} entries)", peer_id, r.item_count());
|
||||
let hashes = r.iter().map(|item| (item.val_at::<H256>(0), item.val_at::<U256>(1)));
|
||||
let mut max_height: U256 = From::from(0);
|
||||
for (rh, rd) in hashes {
|
||||
let h = try!(rh);
|
||||
let d = try!(rd);
|
||||
match io.chain().block_status(&h) {
|
||||
BlockStatus::InChain => {
|
||||
trace!(target: "sync", "New block hash already in chain {:?}", h);
|
||||
},
|
||||
BlockStatus::Queued => {
|
||||
trace!(target: "sync", "New hash block already queued {:?}", h);
|
||||
},
|
||||
BlockStatus::Unknown => {
|
||||
trace!(target: "sync", "New unknown block hash {:?}", h);
|
||||
if d > max_height {
|
||||
let peer = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer");
|
||||
peer.latest = h.clone();
|
||||
max_height = d;
|
||||
}
|
||||
},
|
||||
BlockStatus::Bad =>{
|
||||
debug!(target: "sync", "Bad new block hash {:?}", h);
|
||||
io.disable_peer(peer_id);
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Called by peer when it is disconnecting
|
||||
pub fn on_peer_aborting(&mut self, io: &mut SyncIo, peer: &PeerId) {
|
||||
trace!(target: "sync", "== Disconnected {}", peer);
|
||||
if self.peers.contains_key(&peer) {
|
||||
self.clear_peer_download(peer);
|
||||
self.continue_sync(io);
|
||||
}
|
||||
}
|
||||
|
||||
/// Called when a new peer is connected
|
||||
pub fn on_peer_connected(&mut self, io: &mut SyncIo, peer: &PeerId) {
|
||||
trace!(target: "sync", "== Connected {}", peer);
|
||||
self.send_status(io, peer);
|
||||
}
|
||||
|
||||
/// Resume downloading
|
||||
fn continue_sync(&mut self, io: &mut SyncIo) {
|
||||
let mut peers: Vec<(PeerId, U256)> = self.peers.iter().map(|(k, p)| (*k, p.difficulty)).collect();
|
||||
peers.sort_by(|&(_, d1), &(_, d2)| d1.cmp(&d2).reverse()); //TODO: sort by rating
|
||||
for (p, _) in peers {
|
||||
self.sync_peer(io, &p, false);
|
||||
}
|
||||
}
|
||||
|
||||
/// Called after all blocks have been donloaded
|
||||
fn complete_sync(&mut self) {
|
||||
trace!(target: "sync", "Sync complete");
|
||||
self.reset();
|
||||
self.state = SyncState::Idle;
|
||||
}
|
||||
|
||||
/// Enter waiting state
|
||||
fn pause_sync(&mut self) {
|
||||
trace!(target: "sync", "Block queue full, pausing sync");
|
||||
self.state = SyncState::Waiting;
|
||||
}
|
||||
|
||||
/// Find something to do for a peer. Called for a new peer or when a peer is done with it's task.
|
||||
fn sync_peer(&mut self, io: &mut SyncIo, peer_id: &PeerId, force: bool) {
|
||||
let (peer_latest, peer_difficulty) = {
|
||||
let peer = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer");
|
||||
if peer.asking != PeerAsking::Nothing {
|
||||
return;
|
||||
}
|
||||
if self.state == SyncState::Waiting {
|
||||
trace!(target: "sync", "Waiting for block queue");
|
||||
return;
|
||||
}
|
||||
(peer.latest.clone(), peer.difficulty.clone())
|
||||
};
|
||||
|
||||
let td = io.chain().chain_info().pending_total_difficulty;
|
||||
let syncing_difficulty = max(self.syncing_difficulty, td);
|
||||
if force || peer_difficulty > syncing_difficulty {
|
||||
// start sync
|
||||
self.syncing_difficulty = peer_difficulty;
|
||||
if self.state == SyncState::Idle || self.state == SyncState::NotSynced {
|
||||
self.state = SyncState::Blocks;
|
||||
}
|
||||
trace!(target: "sync", "Starting sync with better chain");
|
||||
self.request_headers_by_hash(io, peer_id, &peer_latest, 1, 0, false);
|
||||
}
|
||||
else if self.state == SyncState::Blocks {
|
||||
self.request_blocks(io, peer_id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Find some headers or blocks to download for a peer.
|
||||
fn request_blocks(&mut self, io: &mut SyncIo, peer_id: &PeerId) {
|
||||
self.clear_peer_download(peer_id);
|
||||
|
||||
if io.chain().queue_status().full {
|
||||
self.pause_sync();
|
||||
return;
|
||||
}
|
||||
|
||||
// check to see if we need to download any block bodies first
|
||||
let mut needed_bodies: Vec<H256> = Vec::new();
|
||||
let mut needed_numbers: Vec<BlockNumber> = Vec::new();
|
||||
|
||||
if self.have_common_block && !self.headers.is_empty() && self.headers.range_iter().next().unwrap().0 == self.last_imported_block + 1 {
|
||||
for (start, ref items) in self.headers.range_iter() {
|
||||
if needed_bodies.len() > MAX_BODIES_TO_REQUEST {
|
||||
break;
|
||||
}
|
||||
let mut index: BlockNumber = 0;
|
||||
while index != items.len() as BlockNumber && needed_bodies.len() < MAX_BODIES_TO_REQUEST {
|
||||
let block = start + index;
|
||||
if !self.downloading_bodies.contains(&block) && !self.bodies.have_item(&block) {
|
||||
needed_bodies.push(items[index as usize].hash.clone());
|
||||
needed_numbers.push(block);
|
||||
self.downloading_bodies.insert(block);
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !needed_bodies.is_empty() {
|
||||
replace(&mut self.peers.get_mut(peer_id).unwrap().asking_blocks, needed_numbers);
|
||||
self.request_bodies(io, peer_id, needed_bodies);
|
||||
}
|
||||
else {
|
||||
// check if need to download headers
|
||||
let mut start = 0usize;
|
||||
if !self.have_common_block {
|
||||
// download backwards until common block is found 1 header at a time
|
||||
let chain_info = io.chain().chain_info();
|
||||
start = chain_info.best_block_number as usize;
|
||||
if !self.headers.is_empty() {
|
||||
start = min(start, self.headers.range_iter().next().unwrap().0 as usize - 1);
|
||||
}
|
||||
if start == 0 {
|
||||
self.have_common_block = true; //reached genesis
|
||||
self.last_imported_hash = chain_info.genesis_hash;
|
||||
}
|
||||
}
|
||||
if self.have_common_block {
|
||||
let mut headers: Vec<BlockNumber> = Vec::new();
|
||||
let mut prev = self.last_imported_block + 1;
|
||||
for (next, ref items) in self.headers.range_iter() {
|
||||
if !headers.is_empty() {
|
||||
break;
|
||||
}
|
||||
if next <= prev {
|
||||
prev = next + items.len() as BlockNumber;
|
||||
continue;
|
||||
}
|
||||
let mut block = prev;
|
||||
while block < next && headers.len() <= MAX_HEADERS_TO_REQUEST {
|
||||
if !self.downloading_headers.contains(&(block as BlockNumber)) {
|
||||
headers.push(block as BlockNumber);
|
||||
self.downloading_headers.insert(block as BlockNumber);
|
||||
}
|
||||
block += 1;
|
||||
}
|
||||
prev = next + items.len() as BlockNumber;
|
||||
}
|
||||
|
||||
if !headers.is_empty() {
|
||||
start = headers[0] as usize;
|
||||
let count = headers.len();
|
||||
replace(&mut self.peers.get_mut(peer_id).unwrap().asking_blocks, headers);
|
||||
assert!(!self.headers.have_item(&(start as BlockNumber)));
|
||||
self.request_headers_by_number(io, peer_id, start as BlockNumber, count, 0, false);
|
||||
}
|
||||
}
|
||||
else {
|
||||
self.request_headers_by_number(io, peer_id, start as BlockNumber, 1, 0, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear all blocks/headers marked as being downloaded by a peer.
|
||||
fn clear_peer_download(&mut self, peer_id: &PeerId) {
|
||||
let peer = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer");
|
||||
for b in &peer.asking_blocks {
|
||||
self.downloading_headers.remove(&b);
|
||||
self.downloading_bodies.remove(&b);
|
||||
}
|
||||
peer.asking_blocks.clear();
|
||||
}
|
||||
|
||||
/// Checks if there are blocks fully downloaded that can be imported into the blockchain and does the import.
|
||||
fn collect_blocks(&mut self, io: &mut SyncIo) {
|
||||
if !self.have_common_block || self.headers.is_empty() || self.bodies.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut restart = false;
|
||||
// merge headers and bodies
|
||||
{
|
||||
let headers = self.headers.range_iter().next().unwrap();
|
||||
let bodies = self.bodies.range_iter().next().unwrap();
|
||||
if headers.0 != bodies.0 || headers.0 != self.last_imported_block + 1 {
|
||||
return;
|
||||
}
|
||||
|
||||
let count = min(headers.1.len(), bodies.1.len());
|
||||
let mut imported = 0;
|
||||
for i in 0..count {
|
||||
let mut block_rlp = RlpStream::new_list(3);
|
||||
block_rlp.append_raw(&headers.1[i].data, 1);
|
||||
let body = Rlp::new(&bodies.1[i]);
|
||||
block_rlp.append_raw(body.at(0).as_raw(), 1);
|
||||
block_rlp.append_raw(body.at(1).as_raw(), 1);
|
||||
let h = &headers.1[i].hash;
|
||||
match io.chain().import_block(&block_rlp.out()) {
|
||||
Err(ImportError::AlreadyInChain) => {
|
||||
trace!(target: "sync", "Block already in chain {:?}", h);
|
||||
self.last_imported_block = headers.0 + i as BlockNumber;
|
||||
self.last_imported_hash = h.clone();
|
||||
},
|
||||
Err(ImportError::AlreadyQueued) => {
|
||||
trace!(target: "sync", "Block already queued {:?}", h);
|
||||
self.last_imported_block = headers.0 + i as BlockNumber;
|
||||
self.last_imported_hash = h.clone();
|
||||
},
|
||||
Ok(()) => {
|
||||
trace!(target: "sync", "Block queued {:?}", h);
|
||||
self.last_imported_block = headers.0 + i as BlockNumber;
|
||||
self.last_imported_hash = h.clone();
|
||||
imported += 1;
|
||||
},
|
||||
Err(e) => {
|
||||
debug!(target: "sync", "Bad block {:?} : {:?}", h, e);
|
||||
restart = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
trace!(target: "sync", "Imported {} of {}", imported, count);
|
||||
}
|
||||
|
||||
if restart {
|
||||
self.restart(io);
|
||||
return;
|
||||
}
|
||||
|
||||
self.headers.remove_head(&(self.last_imported_block + 1));
|
||||
self.bodies.remove_head(&(self.last_imported_block + 1));
|
||||
|
||||
if self.headers.is_empty() {
|
||||
assert!(self.bodies.is_empty());
|
||||
self.complete_sync();
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove downloaded bocks/headers starting from specified number.
|
||||
/// Used to recover from an error and re-download parts of the chain detected as bad.
|
||||
fn remove_downloaded_blocks(&mut self, start: BlockNumber) {
|
||||
for n in self.headers.get_tail(&start) {
|
||||
match self.headers.find_item(&n) {
|
||||
Some(ref header_data) => {
|
||||
let header_to_delete = HeaderView::new(&header_data.data);
|
||||
let header_id = HeaderId {
|
||||
transactions_root: header_to_delete.transactions_root(),
|
||||
uncles: header_to_delete.uncles_hash()
|
||||
};
|
||||
self.header_ids.remove(&header_id);
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
self.downloading_bodies.remove(&n);
|
||||
self.downloading_headers.remove(&n);
|
||||
}
|
||||
self.headers.remove_tail(&start);
|
||||
self.bodies.remove_tail(&start);
|
||||
}
|
||||
|
||||
/// Request headers from a peer by block hash
|
||||
fn request_headers_by_hash(&mut self, sync: &mut SyncIo, peer_id: &PeerId, h: &H256, count: usize, skip: usize, reverse: bool) {
|
||||
trace!(target: "sync", "{} <- GetBlockHeaders: {} entries starting from {}", peer_id, count, h);
|
||||
let mut rlp = RlpStream::new_list(4);
|
||||
rlp.append(h);
|
||||
rlp.append(&count);
|
||||
rlp.append(&skip);
|
||||
rlp.append(&if reverse {1u32} else {0u32});
|
||||
self.send_request(sync, peer_id, PeerAsking::BlockHeaders, GET_BLOCK_HEADERS_PACKET, rlp.out());
|
||||
}
|
||||
|
||||
/// Request headers from a peer by block number
|
||||
fn request_headers_by_number(&mut self, sync: &mut SyncIo, peer_id: &PeerId, n: BlockNumber, count: usize, skip: usize, reverse: bool) {
|
||||
let mut rlp = RlpStream::new_list(4);
|
||||
trace!(target: "sync", "{} <- GetBlockHeaders: {} entries starting from {}", peer_id, count, n);
|
||||
rlp.append(&n);
|
||||
rlp.append(&count);
|
||||
rlp.append(&skip);
|
||||
rlp.append(&if reverse {1u32} else {0u32});
|
||||
self.send_request(sync, peer_id, PeerAsking::BlockHeaders, GET_BLOCK_HEADERS_PACKET, rlp.out());
|
||||
}
|
||||
|
||||
/// Request block bodies from a peer
|
||||
fn request_bodies(&mut self, sync: &mut SyncIo, peer_id: &PeerId, hashes: Vec<H256>) {
|
||||
let mut rlp = RlpStream::new_list(hashes.len());
|
||||
trace!(target: "sync", "{} <- GetBlockBodies: {} entries", peer_id, hashes.len());
|
||||
for h in hashes {
|
||||
rlp.append(&h);
|
||||
}
|
||||
self.send_request(sync, peer_id, PeerAsking::BlockBodies, GET_BLOCK_BODIES_PACKET, rlp.out());
|
||||
}
|
||||
|
||||
/// Reset peer status after request is complete.
|
||||
fn reset_peer_asking(&mut self, peer_id: &PeerId, asking: PeerAsking) {
|
||||
let peer = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer");
|
||||
if peer.asking != asking {
|
||||
warn!(target:"sync", "Asking {:?} while expected {:?}", peer.asking, asking);
|
||||
}
|
||||
else {
|
||||
peer.asking = PeerAsking::Nothing;
|
||||
}
|
||||
}
|
||||
|
||||
/// Generic request sender
|
||||
fn send_request(&mut self, sync: &mut SyncIo, peer_id: &PeerId, asking: PeerAsking, packet_id: PacketId, packet: Bytes) {
|
||||
{
|
||||
let peer = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer");
|
||||
if peer.asking != PeerAsking::Nothing {
|
||||
warn!(target:"sync", "Asking {:?} while requesting {:?}", asking, peer.asking);
|
||||
}
|
||||
}
|
||||
match sync.send(*peer_id, packet_id, packet) {
|
||||
Err(e) => {
|
||||
warn!(target:"sync", "Error sending request: {:?}", e);
|
||||
sync.disable_peer(peer_id);
|
||||
self.on_peer_aborting(sync, peer_id);
|
||||
}
|
||||
Ok(_) => {
|
||||
let mut peer = self.peers.get_mut(&peer_id).unwrap();
|
||||
peer.asking = asking;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Called when peer sends us new transactions
|
||||
fn on_peer_transactions(&mut self, _io: &mut SyncIo, _peer_id: &PeerId, _r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Send Status message
|
||||
fn send_status(&mut self, io: &mut SyncIo, peer_id: &PeerId) {
|
||||
let mut packet = RlpStream::new_list(5);
|
||||
let chain = io.chain().chain_info();
|
||||
packet.append(&(PROTOCOL_VERSION as u32));
|
||||
packet.append(&NETWORK_ID); //TODO: network id
|
||||
packet.append(&chain.total_difficulty);
|
||||
packet.append(&chain.best_block_hash);
|
||||
packet.append(&chain.genesis_hash);
|
||||
//TODO: handle timeout for status request
|
||||
match io.send(*peer_id, STATUS_PACKET, packet.out()) {
|
||||
Err(e) => {
|
||||
warn!(target:"sync", "Error sending status request: {:?}", e);
|
||||
io.disable_peer(peer_id);
|
||||
}
|
||||
Ok(_) => ()
|
||||
}
|
||||
}
|
||||
|
||||
/// Respond to GetBlockHeaders request
|
||||
fn return_block_headers(&self, io: &mut SyncIo, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
||||
// Packet layout:
|
||||
// [ block: { P , B_32 }, maxHeaders: P, skip: P, reverse: P in { 0 , 1 } ]
|
||||
let max_headers: usize = try!(r.val_at(1));
|
||||
let skip: usize = try!(r.val_at(2));
|
||||
let reverse: bool = try!(r.val_at(3));
|
||||
let last = io.chain().chain_info().best_block_number;
|
||||
let mut number = if try!(r.at(0)).size() == 32 {
|
||||
// id is a hash
|
||||
let hash: H256 = try!(r.val_at(0));
|
||||
trace!(target: "sync", "-> GetBlockHeaders (hash: {}, max: {}, skip: {}, reverse:{})", hash, max_headers, skip, reverse);
|
||||
match io.chain().block_header(&hash) {
|
||||
Some(hdr) => From::from(HeaderView::new(&hdr).number()),
|
||||
None => last
|
||||
}
|
||||
}
|
||||
else {
|
||||
trace!(target: "sync", "-> GetBlockHeaders (number: {}, max: {}, skip: {}, reverse:{})", try!(r.val_at::<BlockNumber>(0)), max_headers, skip, reverse);
|
||||
try!(r.val_at(0))
|
||||
};
|
||||
|
||||
if reverse {
|
||||
number = min(last, number);
|
||||
} else {
|
||||
number = max(1, number);
|
||||
}
|
||||
let max_count = min(MAX_HEADERS_TO_SEND, max_headers);
|
||||
let mut count = 0;
|
||||
let mut data = Bytes::new();
|
||||
let inc = (skip + 1) as BlockNumber;
|
||||
while number <= last && number > 0 && count < max_count {
|
||||
match io.chain().block_header_at(number) {
|
||||
Some(mut hdr) => {
|
||||
data.append(&mut hdr);
|
||||
count += 1;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
if reverse {
|
||||
if number <= inc {
|
||||
break;
|
||||
}
|
||||
number -= inc;
|
||||
}
|
||||
else {
|
||||
number += inc;
|
||||
}
|
||||
}
|
||||
let mut rlp = RlpStream::new_list(count as usize);
|
||||
rlp.append_raw(&data, count as usize);
|
||||
io.respond(BLOCK_HEADERS_PACKET, rlp.out()).unwrap_or_else(|e|
|
||||
debug!(target: "sync", "Error sending headers: {:?}", e));
|
||||
trace!(target: "sync", "-> GetBlockHeaders: returned {} entries", count);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Respond to GetBlockBodies request
|
||||
fn return_block_bodies(&self, io: &mut SyncIo, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
||||
let mut count = r.item_count();
|
||||
if count == 0 {
|
||||
debug!(target: "sync", "Empty GetBlockBodies request, ignoring.");
|
||||
return Ok(());
|
||||
}
|
||||
trace!(target: "sync", "-> GetBlockBodies: {} entries", count);
|
||||
count = min(count, MAX_BODIES_TO_SEND);
|
||||
let mut added = 0usize;
|
||||
let mut data = Bytes::new();
|
||||
for i in 0..count {
|
||||
match io.chain().block_body(&try!(r.val_at::<H256>(i))) {
|
||||
Some(mut hdr) => {
|
||||
data.append(&mut hdr);
|
||||
added += 1;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
let mut rlp = RlpStream::new_list(added);
|
||||
rlp.append_raw(&data, added);
|
||||
io.respond(BLOCK_BODIES_PACKET, rlp.out()).unwrap_or_else(|e|
|
||||
debug!(target: "sync", "Error sending headers: {:?}", e));
|
||||
trace!(target: "sync", "-> GetBlockBodies: returned {} entries", added);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Respond to GetNodeData request
|
||||
fn return_node_data(&self, io: &mut SyncIo, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
||||
let mut count = r.item_count();
|
||||
if count == 0 {
|
||||
debug!(target: "sync", "Empty GetNodeData request, ignoring.");
|
||||
return Ok(());
|
||||
}
|
||||
count = min(count, MAX_NODE_DATA_TO_SEND);
|
||||
let mut added = 0usize;
|
||||
let mut data = Bytes::new();
|
||||
for i in 0..count {
|
||||
match io.chain().state_data(&try!(r.val_at::<H256>(i))) {
|
||||
Some(mut hdr) => {
|
||||
data.append(&mut hdr);
|
||||
added += 1;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
let mut rlp = RlpStream::new_list(added);
|
||||
rlp.append_raw(&data, added);
|
||||
io.respond(NODE_DATA_PACKET, rlp.out()).unwrap_or_else(|e|
|
||||
debug!(target: "sync", "Error sending headers: {:?}", e));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Respond to GetReceipts request
|
||||
fn return_receipts(&self, io: &mut SyncIo, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
||||
let mut count = r.item_count();
|
||||
if count == 0 {
|
||||
debug!(target: "sync", "Empty GetReceipts request, ignoring.");
|
||||
return Ok(());
|
||||
}
|
||||
count = min(count, MAX_RECEIPTS_TO_SEND);
|
||||
let mut added = 0usize;
|
||||
let mut data = Bytes::new();
|
||||
for i in 0..count {
|
||||
match io.chain().block_receipts(&try!(r.val_at::<H256>(i))) {
|
||||
Some(mut hdr) => {
|
||||
data.append(&mut hdr);
|
||||
added += 1;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
let mut rlp = RlpStream::new_list(added);
|
||||
rlp.append_raw(&data, added);
|
||||
io.respond(RECEIPTS_PACKET, rlp.out()).unwrap_or_else(|e|
|
||||
debug!(target: "sync", "Error sending headers: {:?}", e));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Dispatch incoming requests and responses
|
||||
pub fn on_packet(&mut self, io: &mut SyncIo, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
let rlp = UntrustedRlp::new(data);
|
||||
let result = match packet_id {
|
||||
STATUS_PACKET => self.on_peer_status(io, peer, &rlp),
|
||||
TRANSACTIONS_PACKET => self.on_peer_transactions(io, peer, &rlp),
|
||||
GET_BLOCK_HEADERS_PACKET => self.return_block_headers(io, &rlp),
|
||||
BLOCK_HEADERS_PACKET => self.on_peer_block_headers(io, peer, &rlp),
|
||||
GET_BLOCK_BODIES_PACKET => self.return_block_bodies(io, &rlp),
|
||||
BLOCK_BODIES_PACKET => self.on_peer_block_bodies(io, peer, &rlp),
|
||||
NEW_BLOCK_PACKET => self.on_peer_new_block(io, peer, &rlp),
|
||||
NEW_BLOCK_HASHES_PACKET => self.on_peer_new_hashes(io, peer, &rlp),
|
||||
GET_NODE_DATA_PACKET => self.return_node_data(io, &rlp),
|
||||
GET_RECEIPTS_PACKET => self.return_receipts(io, &rlp),
|
||||
_ => {
|
||||
debug!(target: "sync", "Unknown packet {}", packet_id);
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
result.unwrap_or_else(|e| {
|
||||
debug!(target:"sync", "{} -> Malformed packet {} : {}", peer, packet_id, e);
|
||||
})
|
||||
}
|
||||
|
||||
/// Maintain other peers. Send out any new blocks and transactions
|
||||
pub fn maintain_sync(&mut self, _io: &mut SyncIo) {
|
||||
}
|
||||
}
|
||||
|
53
src/sync/io.rs
Normal file
53
src/sync/io.rs
Normal file
@ -0,0 +1,53 @@
|
||||
use client::BlockChainClient;
|
||||
use util::network::{HandlerIo, PeerId, PacketId,};
|
||||
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 {
|
||||
/// Disable a peer
|
||||
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>;
|
||||
/// Send a packet to a peer.
|
||||
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;
|
||||
}
|
||||
|
||||
/// Wraps `HandlerIo` and the blockchain client
|
||||
pub struct NetSyncIo<'s, 'h> where 'h:'s {
|
||||
network: &'s mut HandlerIo<'h>,
|
||||
chain: &'s mut BlockChainClient
|
||||
}
|
||||
|
||||
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> {
|
||||
NetSyncIo {
|
||||
network: network,
|
||||
chain: chain,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> {
|
||||
fn disable_peer(&mut self, peer_id: &PeerId) {
|
||||
self.network.disable_peer(*peer_id);
|
||||
}
|
||||
|
||||
fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError>{
|
||||
self.network.respond(packet_id, data)
|
||||
}
|
||||
|
||||
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError>{
|
||||
self.network.send(peer_id, packet_id, data)
|
||||
}
|
||||
|
||||
fn chain<'a>(&'a mut self) -> &'a mut BlockChainClient {
|
||||
self.chain
|
||||
}
|
||||
}
|
||||
|
||||
|
100
src/sync/mod.rs
Normal file
100
src/sync/mod.rs
Normal file
@ -0,0 +1,100 @@
|
||||
/// Blockchain sync module
|
||||
/// Implements ethereum protocol version 63 as specified here:
|
||||
/// https://github.com/ethereum/wiki/wiki/Ethereum-Wire-Protocol
|
||||
///
|
||||
/// Usage example:
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// extern crate ethcore;
|
||||
/// use std::env;
|
||||
/// use std::sync::Arc;
|
||||
/// use util::network::NetworkService;
|
||||
/// use ethcore::client::Client;
|
||||
/// use ethcore::sync::EthSync;
|
||||
/// use ethcore::ethereum;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let mut service = NetworkService::start().unwrap();
|
||||
/// let dir = env::temp_dir();
|
||||
/// let client = Arc::new(Client::new(ethereum::new_frontier(), &dir).unwrap());
|
||||
/// EthSync::register(&mut service, client);
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
use std::sync::Arc;
|
||||
use client::BlockChainClient;
|
||||
use util::network::{ProtocolHandler, NetworkService, HandlerIo, TimerToken, PeerId, Message};
|
||||
use sync::chain::ChainSync;
|
||||
use sync::io::NetSyncIo;
|
||||
|
||||
mod chain;
|
||||
mod io;
|
||||
mod range_collection;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
/// Ethereum network protocol handler
|
||||
pub struct EthSync {
|
||||
/// Shared blockchain client. TODO: this should evetually become an IPC endpoint
|
||||
chain: Arc<BlockChainClient + Send + Sized>,
|
||||
/// Sync strategy
|
||||
sync: ChainSync
|
||||
}
|
||||
|
||||
pub use self::chain::SyncStatus;
|
||||
|
||||
impl EthSync {
|
||||
/// Creates and register protocol with the network service
|
||||
pub fn register(service: &mut NetworkService, chain: Arc<BlockChainClient + Send + Sized>) {
|
||||
let sync = Box::new(EthSync {
|
||||
chain: chain,
|
||||
sync: ChainSync::new(),
|
||||
});
|
||||
service.register_protocol(sync, "eth", &[62u8, 63u8]).expect("Error registering eth protocol handler");
|
||||
}
|
||||
|
||||
/// Get sync status
|
||||
pub fn status(&self) -> SyncStatus {
|
||||
self.sync.status()
|
||||
}
|
||||
|
||||
/// Stop sync
|
||||
pub fn stop(&mut self, io: &mut HandlerIo) {
|
||||
self.sync.abort(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
||||
}
|
||||
|
||||
/// Restart sync
|
||||
pub fn restart(&mut self, io: &mut HandlerIo) {
|
||||
self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
||||
}
|
||||
}
|
||||
|
||||
impl ProtocolHandler for EthSync {
|
||||
fn initialize(&mut self, io: &mut HandlerIo) {
|
||||
self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
||||
io.register_timer(1000).unwrap();
|
||||
}
|
||||
|
||||
fn read(&mut self, io: &mut HandlerIo, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
self.sync.on_packet(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer, packet_id, data);
|
||||
}
|
||||
|
||||
fn connected(&mut self, io: &mut HandlerIo, peer: &PeerId) {
|
||||
self.sync.on_peer_connected(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer);
|
||||
}
|
||||
|
||||
fn disconnected(&mut self, io: &mut HandlerIo, peer: &PeerId) {
|
||||
self.sync.on_peer_aborting(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer);
|
||||
}
|
||||
|
||||
fn timeout(&mut self, io: &mut HandlerIo, _timer: TimerToken) {
|
||||
self.sync.maintain_sync(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
|
||||
}
|
||||
|
||||
fn message(&mut self, _io: &mut HandlerIo, _message: &Message) {
|
||||
}
|
||||
}
|
||||
|
||||
|
259
src/sync/range_collection.rs
Normal file
259
src/sync/range_collection.rs
Normal file
@ -0,0 +1,259 @@
|
||||
/// This module defines a trait for a collection of ranged values and an implementation
|
||||
/// for this trait over sorted vector.
|
||||
|
||||
use std::ops::{Add, Sub, Range};
|
||||
|
||||
pub trait ToUsize {
|
||||
fn to_usize(&self) -> usize;
|
||||
}
|
||||
|
||||
pub trait FromUsize {
|
||||
fn from_usize(s: usize) -> Self;
|
||||
}
|
||||
|
||||
/// A key-value collection orderd by key with sequential key-value pairs grouped together.
|
||||
/// Such group is called a range.
|
||||
/// E.g. a set of collection of 5 pairs {1, a}, {2, b}, {10, x}, {11, y}, {12, z} will be grouped into two ranges: {1, [a,b]}, {10, [x,y,z]}
|
||||
pub trait RangeCollection<K, V> {
|
||||
/// Check if the given key is present in the collection.
|
||||
fn have_item(&self, key: &K) -> bool;
|
||||
/// Get value by key.
|
||||
fn find_item(&self, key: &K) -> Option<&V>;
|
||||
/// Get a range of keys from `key` till the end of the range that has `key`
|
||||
/// Returns an empty range is key does not exist.
|
||||
fn get_tail(&mut self, key: &K) -> Range<K>;
|
||||
/// Remove all elements < `start` in the range that contains `start` - 1
|
||||
fn remove_head(&mut self, start: &K);
|
||||
/// Remove all elements >= `start` in the range that contains `start`
|
||||
fn remove_tail(&mut self, start: &K);
|
||||
/// Remove all elements >= `tail`
|
||||
fn insert_item(&mut self, key: K, value: V);
|
||||
/// Get an iterator over ranges
|
||||
fn range_iter<'c>(&'c self) -> RangeIterator<'c, K, V>;
|
||||
}
|
||||
|
||||
/// Range iterator. For each range yelds a key for the first element of the range and a vector of values.
|
||||
pub struct RangeIterator<'c, K:'c, V:'c> {
|
||||
range: usize,
|
||||
collection: &'c Vec<(K, Vec<V>)>
|
||||
}
|
||||
|
||||
impl<'c, K:'c, V:'c> Iterator for RangeIterator<'c, K, V> where K: Add<Output = K> + FromUsize + ToUsize + Copy {
|
||||
type Item = (K, &'c [V]);
|
||||
// The 'Iterator' trait only requires the 'next' method to be defined. The
|
||||
// return type is 'Option<T>', 'None' is returned when the 'Iterator' is
|
||||
// over, otherwise the next value is returned wrapped in 'Some'
|
||||
fn next(&mut self) -> Option<(K, &'c [V])> {
|
||||
if self.range > 0 {
|
||||
self.range -= 1;
|
||||
}
|
||||
else {
|
||||
return None;
|
||||
}
|
||||
match self.collection.get(self.range) {
|
||||
Some(&(ref k, ref vec)) => {
|
||||
Some((*k, &vec))
|
||||
},
|
||||
None => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> RangeCollection<K, V> for Vec<(K, Vec<V>)> where K: Ord + PartialEq + Add<Output = K> + Sub<Output = K> + Copy + FromUsize + ToUsize {
|
||||
fn range_iter<'c>(&'c self) -> RangeIterator<'c, K, V> {
|
||||
RangeIterator {
|
||||
range: self.len(),
|
||||
collection: self
|
||||
}
|
||||
}
|
||||
|
||||
fn have_item(&self, key: &K) -> bool {
|
||||
match self.binary_search_by(|&(k, _)| k.cmp(key).reverse()) {
|
||||
Ok(_) => true,
|
||||
Err(index) => match self.get(index) {
|
||||
Some(&(ref k, ref v)) => k <= key && (*k + FromUsize::from_usize(v.len())) > *key,
|
||||
_ => false
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn find_item(&self, key: &K) -> Option<&V> {
|
||||
match self.binary_search_by(|&(k, _)| k.cmp(key).reverse()) {
|
||||
Ok(index) => self.get(index).unwrap().1.get(0),
|
||||
Err(index) => match self.get(index) {
|
||||
Some(&(ref k, ref v)) if k <= key && (*k + FromUsize::from_usize(v.len())) > *key => v.get((*key - *k).to_usize()),
|
||||
_ => None
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn get_tail(&mut self, key: &K) -> Range<K> {
|
||||
let kv = *key;
|
||||
match self.binary_search_by(|&(k, _)| k.cmp(key).reverse()) {
|
||||
Ok(index) => kv..(kv + FromUsize::from_usize(self[index].1.len())),
|
||||
Err(index) => {
|
||||
match self.get_mut(index) {
|
||||
Some(&mut (ref k, ref mut v)) if k <= key && (*k + FromUsize::from_usize(v.len())) > *key => {
|
||||
kv..(*k + FromUsize::from_usize(v.len()))
|
||||
}
|
||||
_ => kv..kv
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
/// Remove element key and following elements in the same range
|
||||
fn remove_tail(&mut self, key: &K) {
|
||||
match self.binary_search_by(|&(k, _)| k.cmp(key).reverse()) {
|
||||
Ok(index) => { self.remove(index); },
|
||||
Err(index) =>{
|
||||
let mut empty = false;
|
||||
match self.get_mut(index) {
|
||||
Some(&mut (ref k, ref mut v)) if k <= key && (*k + FromUsize::from_usize(v.len())) > *key => {
|
||||
v.truncate((*key - *k).to_usize());
|
||||
empty = v.is_empty();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if empty {
|
||||
self.remove(index);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove range elements up to key
|
||||
fn remove_head(&mut self, key: &K) {
|
||||
if *key == FromUsize::from_usize(0) {
|
||||
return
|
||||
}
|
||||
|
||||
let prev = *key - FromUsize::from_usize(1);
|
||||
match self.binary_search_by(|&(k, _)| k.cmp(key).reverse()) {
|
||||
Ok(_) => { }, //start of range, do nothing.
|
||||
Err(index) => {
|
||||
let mut empty = false;
|
||||
match self.get_mut(index) {
|
||||
Some(&mut (ref mut k, ref mut v)) if *k <= prev && (*k + FromUsize::from_usize(v.len())) > prev => {
|
||||
let tail = v.split_off((*key - *k).to_usize());
|
||||
empty = tail.is_empty();
|
||||
let removed = ::std::mem::replace(v, tail);
|
||||
let new_k = *k + FromUsize::from_usize(removed.len());
|
||||
::std::mem::replace(k, new_k);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if empty {
|
||||
self.remove(index);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_item(&mut self, key: K, value: V) {
|
||||
assert!(!self.have_item(&key));
|
||||
|
||||
let lower = match self.binary_search_by(|&(k, _)| k.cmp(&key).reverse()) {
|
||||
Ok(index) => index,
|
||||
Err(index) => index,
|
||||
};
|
||||
|
||||
let mut to_remove: Option<usize> = None;
|
||||
if lower < self.len() && self[lower].0 + FromUsize::from_usize(self[lower].1.len()) == key {
|
||||
// extend into existing chunk
|
||||
self[lower].1.push(value);
|
||||
}
|
||||
else {
|
||||
// insert a new chunk
|
||||
let range: Vec<V> = vec![value];
|
||||
self.insert(lower, (key, range));
|
||||
};
|
||||
if lower > 0 {
|
||||
let next = lower - 1;
|
||||
if next < self.len()
|
||||
{
|
||||
{
|
||||
let (mut next, mut inserted) = self.split_at_mut(lower);
|
||||
let mut next = next.last_mut().unwrap();
|
||||
let mut inserted = inserted.first_mut().unwrap();
|
||||
if next.0 == key + FromUsize::from_usize(1)
|
||||
{
|
||||
inserted.1.append(&mut next.1);
|
||||
to_remove = Some(lower - 1);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(r) = to_remove {
|
||||
self.remove(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_range() {
|
||||
use std::cmp::{Ordering};
|
||||
|
||||
let mut ranges: Vec<(u64, Vec<char>)> = Vec::new();
|
||||
assert_eq!(ranges.range_iter().next(), None);
|
||||
assert_eq!(ranges.find_item(&1), None);
|
||||
assert!(!ranges.have_item(&1));
|
||||
assert_eq!(ranges.get_tail(&0), 0..0);
|
||||
|
||||
ranges.insert_item(17, 'q');
|
||||
assert_eq!(ranges.range_iter().cmp(vec![(17, &['q'][..])]), Ordering::Equal);
|
||||
assert_eq!(ranges.find_item(&17), Some(&'q'));
|
||||
assert!(ranges.have_item(&17));
|
||||
assert_eq!(ranges.get_tail(&17), 17..18);
|
||||
|
||||
ranges.insert_item(18, 'r');
|
||||
assert_eq!(ranges.range_iter().cmp(vec![(17, &['q', 'r'][..])]), Ordering::Equal);
|
||||
assert_eq!(ranges.find_item(&18), Some(&'r'));
|
||||
assert!(ranges.have_item(&18));
|
||||
assert_eq!(ranges.get_tail(&17), 17..19);
|
||||
|
||||
ranges.insert_item(16, 'p');
|
||||
assert_eq!(ranges.range_iter().cmp(vec![(16, &['p', 'q', 'r'][..])]), Ordering::Equal);
|
||||
assert_eq!(ranges.find_item(&16), Some(&'p'));
|
||||
assert_eq!(ranges.find_item(&17), Some(&'q'));
|
||||
assert_eq!(ranges.find_item(&18), Some(&'r'));
|
||||
assert!(ranges.have_item(&16));
|
||||
assert_eq!(ranges.get_tail(&17), 17..19);
|
||||
assert_eq!(ranges.get_tail(&16), 16..19);
|
||||
|
||||
ranges.insert_item(2, 'b');
|
||||
assert_eq!(ranges.range_iter().cmp(vec![(2, &['b'][..]), (16, &['p', 'q', 'r'][..])]), Ordering::Equal);
|
||||
assert_eq!(ranges.find_item(&2), Some(&'b'));
|
||||
|
||||
ranges.insert_item(3, 'c');
|
||||
ranges.insert_item(4, 'd');
|
||||
assert_eq!(ranges.get_tail(&3), 3..5);
|
||||
assert_eq!(ranges.range_iter().cmp(vec![(2, &['b', 'c', 'd'][..]), (16, &['p', 'q', 'r'][..])]), Ordering::Equal);
|
||||
|
||||
let mut r = ranges.clone();
|
||||
r.remove_head(&1);
|
||||
assert_eq!(r.range_iter().cmp(vec![(2, &['b', 'c', 'd'][..]), (16, &['p', 'q', 'r'][..])]), Ordering::Equal);
|
||||
r.remove_head(&2);
|
||||
assert_eq!(r.range_iter().cmp(vec![(2, &['b', 'c', 'd'][..]), (16, &['p', 'q', 'r'][..])]), Ordering::Equal);
|
||||
r.remove_head(&3);
|
||||
assert_eq!(r.range_iter().cmp(vec![(3, &['c', 'd'][..]), (16, &['p', 'q', 'r'][..])]), Ordering::Equal);
|
||||
r.remove_head(&10);
|
||||
assert_eq!(r.range_iter().cmp(vec![(3, &['c', 'd'][..]), (16, &['p', 'q', 'r'][..])]), Ordering::Equal);
|
||||
r.remove_head(&5);
|
||||
assert_eq!(r.range_iter().cmp(vec![(16, &['p', 'q', 'r'][..])]), Ordering::Equal);
|
||||
r.remove_head(&19);
|
||||
assert_eq!(r.range_iter().next(), None);
|
||||
|
||||
let mut r = ranges.clone();
|
||||
r.remove_tail(&20);
|
||||
assert_eq!(r.range_iter().cmp(vec![(2, &['b', 'c', 'd'][..]), (16, &['p', 'q', 'r'][..])]), Ordering::Equal);
|
||||
r.remove_tail(&17);
|
||||
assert_eq!(r.range_iter().cmp(vec![(2, &['b', 'c', 'd'][..]), (16, &['p'][..])]), Ordering::Equal);
|
||||
r.remove_tail(&16);
|
||||
assert_eq!(r.range_iter().cmp(vec![(2, &['b', 'c', 'd'][..])]), Ordering::Equal);
|
||||
r.remove_tail(&3);
|
||||
assert_eq!(r.range_iter().cmp(vec![(2, &['b'][..])]), Ordering::Equal);
|
||||
r.remove_tail(&2);
|
||||
assert_eq!(r.range_iter().next(), None);
|
||||
}
|
||||
|
337
src/sync/tests.rs
Normal file
337
src/sync/tests.rs
Normal file
@ -0,0 +1,337 @@
|
||||
use util::*;
|
||||
use client::{BlockChainClient, BlockStatus, TreeRoute, BlockQueueStatus, BlockChainInfo};
|
||||
use header::{Header as BlockHeader, BlockNumber};
|
||||
use error::*;
|
||||
use sync::io::SyncIo;
|
||||
use sync::chain::ChainSync;
|
||||
|
||||
struct TestBlockChainClient {
|
||||
blocks: HashMap<H256, Bytes>,
|
||||
numbers: HashMap<usize, H256>,
|
||||
genesis_hash: H256,
|
||||
last_hash: H256,
|
||||
difficulty: U256
|
||||
}
|
||||
|
||||
impl TestBlockChainClient {
|
||||
fn new() -> TestBlockChainClient {
|
||||
|
||||
let mut client = TestBlockChainClient {
|
||||
blocks: HashMap::new(),
|
||||
numbers: HashMap::new(),
|
||||
genesis_hash: H256::new(),
|
||||
last_hash: H256::new(),
|
||||
difficulty: From::from(0),
|
||||
};
|
||||
client.add_blocks(1, true); // add genesis block
|
||||
client.genesis_hash = client.last_hash.clone();
|
||||
client
|
||||
}
|
||||
|
||||
pub fn add_blocks(&mut self, count: usize, empty: bool) {
|
||||
for n in self.numbers.len()..(self.numbers.len() + count) {
|
||||
let mut header = BlockHeader::new();
|
||||
header.difficulty = From::from(n);
|
||||
header.parent_hash = self.last_hash.clone();
|
||||
header.number = n as BlockNumber;
|
||||
let mut uncles = RlpStream::new_list(if empty {0} else {1});
|
||||
if !empty {
|
||||
uncles.append(&H256::from(&U256::from(n)));
|
||||
header.uncles_hash = uncles.as_raw().sha3();
|
||||
}
|
||||
let mut rlp = RlpStream::new_list(3);
|
||||
rlp.append(&header);
|
||||
rlp.append_raw(&rlp::NULL_RLP, 1);
|
||||
rlp.append_raw(uncles.as_raw(), 1);
|
||||
self.import_block(rlp.as_raw()).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockChainClient for TestBlockChainClient {
|
||||
fn block_header(&self, h: &H256) -> Option<Bytes> {
|
||||
self.blocks.get(h).map(|r| Rlp::new(r).at(0).as_raw().to_vec())
|
||||
|
||||
}
|
||||
|
||||
fn block_body(&self, h: &H256) -> Option<Bytes> {
|
||||
self.blocks.get(h).map(|r| {
|
||||
let mut stream = RlpStream::new_list(2);
|
||||
stream.append_raw(Rlp::new(&r).at(1).as_raw(), 1);
|
||||
stream.append_raw(Rlp::new(&r).at(2).as_raw(), 1);
|
||||
stream.out()
|
||||
})
|
||||
}
|
||||
|
||||
fn block(&self, h: &H256) -> Option<Bytes> {
|
||||
self.blocks.get(h).map(|b| b.clone())
|
||||
}
|
||||
|
||||
fn block_status(&self, h: &H256) -> BlockStatus {
|
||||
match self.blocks.get(h) {
|
||||
Some(_) => BlockStatus::InChain,
|
||||
None => BlockStatus::Unknown
|
||||
}
|
||||
}
|
||||
|
||||
fn block_header_at(&self, n: BlockNumber) -> Option<Bytes> {
|
||||
self.numbers.get(&(n as usize)).and_then(|h| self.block_header(h))
|
||||
}
|
||||
|
||||
fn block_body_at(&self, n: BlockNumber) -> Option<Bytes> {
|
||||
self.numbers.get(&(n as usize)).and_then(|h| self.block_body(h))
|
||||
}
|
||||
|
||||
fn block_at(&self, n: BlockNumber) -> Option<Bytes> {
|
||||
self.numbers.get(&(n as usize)).map(|h| self.blocks.get(h).unwrap().clone())
|
||||
}
|
||||
|
||||
fn block_status_at(&self, n: BlockNumber) -> BlockStatus {
|
||||
if (n as usize) < self.blocks.len() {
|
||||
BlockStatus::InChain
|
||||
} else {
|
||||
BlockStatus::Unknown
|
||||
}
|
||||
}
|
||||
|
||||
fn tree_route(&self, _from: &H256, _to: &H256) -> Option<TreeRoute> {
|
||||
Some(TreeRoute {
|
||||
blocks: Vec::new(),
|
||||
ancestor: H256::new(),
|
||||
index: 0
|
||||
})
|
||||
}
|
||||
|
||||
fn state_data(&self, _h: &H256) -> Option<Bytes> {
|
||||
None
|
||||
}
|
||||
|
||||
fn block_receipts(&self, _h: &H256) -> Option<Bytes> {
|
||||
None
|
||||
}
|
||||
|
||||
fn import_block(&mut self, b: &[u8]) -> ImportResult {
|
||||
let header = Rlp::new(&b).val_at::<BlockHeader>(0);
|
||||
let number: usize = header.number as usize;
|
||||
if number > self.blocks.len() {
|
||||
panic!("Unexpected block number. Expected {}, got {}", self.blocks.len(), number);
|
||||
}
|
||||
if number > 0 {
|
||||
match self.blocks.get(&header.parent_hash) {
|
||||
Some(parent) => {
|
||||
let parent = Rlp::new(parent).val_at::<BlockHeader>(0);
|
||||
if parent.number != (header.number - 1) {
|
||||
panic!("Unexpected block parent");
|
||||
}
|
||||
},
|
||||
None => {
|
||||
panic!("Unknown block parent {:?} for block {}", header.parent_hash, number);
|
||||
}
|
||||
}
|
||||
}
|
||||
if number == self.numbers.len() {
|
||||
self.difficulty = self.difficulty + header.difficulty;
|
||||
self.last_hash = header.hash();
|
||||
self.blocks.insert(header.hash(), b.to_vec());
|
||||
self.numbers.insert(number, header.hash());
|
||||
let mut parent_hash = header.parent_hash;
|
||||
if number > 0 {
|
||||
let mut n = number - 1;
|
||||
while n > 0 && self.numbers[&n] != parent_hash {
|
||||
*self.numbers.get_mut(&n).unwrap() = parent_hash.clone();
|
||||
n -= 1;
|
||||
parent_hash = Rlp::new(&self.blocks[&parent_hash]).val_at::<BlockHeader>(0).parent_hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
self.blocks.insert(header.hash(), b.to_vec());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn queue_status(&self) -> BlockQueueStatus {
|
||||
BlockQueueStatus {
|
||||
full: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_queue(&mut self) {
|
||||
}
|
||||
|
||||
fn chain_info(&self) -> BlockChainInfo {
|
||||
BlockChainInfo {
|
||||
total_difficulty: self.difficulty,
|
||||
pending_total_difficulty: self.difficulty,
|
||||
genesis_hash: self.genesis_hash.clone(),
|
||||
best_block_hash: self.last_hash.clone(),
|
||||
best_block_number: self.blocks.len() as BlockNumber - 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TestIo<'p> {
|
||||
chain: &'p mut TestBlockChainClient,
|
||||
queue: &'p mut VecDeque<TestPacket>,
|
||||
sender: Option<PeerId>,
|
||||
}
|
||||
|
||||
impl<'p> TestIo<'p> {
|
||||
fn new(chain: &'p mut TestBlockChainClient, queue: &'p mut VecDeque<TestPacket>, sender: Option<PeerId>) -> TestIo<'p> {
|
||||
TestIo {
|
||||
chain: chain,
|
||||
queue: queue,
|
||||
sender: sender
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'p> SyncIo for TestIo<'p> {
|
||||
fn disable_peer(&mut self, _peer_id: &PeerId) {
|
||||
}
|
||||
|
||||
fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> {
|
||||
self.queue.push_back(TestPacket {
|
||||
data: data,
|
||||
packet_id: packet_id,
|
||||
recipient: self.sender.unwrap()
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> {
|
||||
self.queue.push_back(TestPacket {
|
||||
data: data,
|
||||
packet_id: packet_id,
|
||||
recipient: peer_id,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn chain<'a>(&'a mut self) -> &'a mut BlockChainClient {
|
||||
self.chain
|
||||
}
|
||||
}
|
||||
|
||||
struct TestPacket {
|
||||
data: Bytes,
|
||||
packet_id: PacketId,
|
||||
recipient: PeerId,
|
||||
}
|
||||
|
||||
struct TestPeer {
|
||||
chain: TestBlockChainClient,
|
||||
sync: ChainSync,
|
||||
queue: VecDeque<TestPacket>,
|
||||
}
|
||||
|
||||
struct TestNet {
|
||||
peers: Vec<TestPeer>
|
||||
}
|
||||
|
||||
impl TestNet {
|
||||
pub fn new(n: usize) -> TestNet {
|
||||
let mut net = TestNet {
|
||||
peers: Vec::new(),
|
||||
};
|
||||
for _ in 0..n {
|
||||
net.peers.push(TestPeer {
|
||||
chain: TestBlockChainClient::new(),
|
||||
sync: ChainSync::new(),
|
||||
queue: VecDeque::new(),
|
||||
});
|
||||
}
|
||||
net
|
||||
}
|
||||
|
||||
pub fn peer(&self, i: usize) -> &TestPeer {
|
||||
self.peers.get(i).unwrap()
|
||||
}
|
||||
|
||||
pub fn peer_mut(&mut self, i: usize) -> &mut TestPeer {
|
||||
self.peers.get_mut(i).unwrap()
|
||||
}
|
||||
|
||||
pub fn start(&mut self) {
|
||||
for peer in 0..self.peers.len() {
|
||||
for client in 0..self.peers.len() {
|
||||
if peer != client {
|
||||
let mut p = self.peers.get_mut(peer).unwrap();
|
||||
p.sync.on_peer_connected(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(client as PeerId)), &(client as PeerId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sync_step(&mut self) {
|
||||
for peer in 0..self.peers.len() {
|
||||
match self.peers[peer].queue.pop_front() {
|
||||
Some(packet) => {
|
||||
let mut p = self.peers.get_mut(packet.recipient).unwrap();
|
||||
trace!("--- {} -> {} ---", peer, packet.recipient);
|
||||
p.sync.on_packet(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(peer as PeerId)), &(peer as PeerId), packet.packet_id, &packet.data);
|
||||
trace!("----------------");
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
let mut p = self.peers.get_mut(peer).unwrap();
|
||||
p.sync.maintain_sync(&mut TestIo::new(&mut p.chain, &mut p.queue, None));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sync(&mut self) {
|
||||
self.start();
|
||||
while !self.done() {
|
||||
self.sync_step()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn done(&self) -> bool {
|
||||
self.peers.iter().all(|p| p.queue.is_empty())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn full_sync_two_peers() {
|
||||
::env_logger::init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
net.peer_mut(1).chain.add_blocks(1000, false);
|
||||
net.peer_mut(2).chain.add_blocks(1000, false);
|
||||
net.sync();
|
||||
assert!(net.peer(0).chain.block_at(1000).is_some());
|
||||
assert_eq!(net.peer(0).chain.blocks, net.peer(1).chain.blocks);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_sync_empty_blocks() {
|
||||
::env_logger::init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
for n in 0..200 {
|
||||
net.peer_mut(1).chain.add_blocks(5, n % 2 == 0);
|
||||
net.peer_mut(2).chain.add_blocks(5, n % 2 == 0);
|
||||
}
|
||||
net.sync();
|
||||
assert!(net.peer(0).chain.block_at(1000).is_some());
|
||||
assert_eq!(net.peer(0).chain.blocks, net.peer(1).chain.blocks);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forked_sync() {
|
||||
::env_logger::init().ok();
|
||||
let mut net = TestNet::new(3);
|
||||
net.peer_mut(0).chain.add_blocks(300, false);
|
||||
net.peer_mut(1).chain.add_blocks(300, false);
|
||||
net.peer_mut(2).chain.add_blocks(300, false);
|
||||
net.peer_mut(0).chain.add_blocks(100, true); //fork
|
||||
net.peer_mut(1).chain.add_blocks(200, false);
|
||||
net.peer_mut(2).chain.add_blocks(200, false);
|
||||
net.peer_mut(1).chain.add_blocks(100, false); //fork between 1 and 2
|
||||
net.peer_mut(2).chain.add_blocks(10, true);
|
||||
// peer 1 has the best chain of 601 blocks
|
||||
let peer1_chain = net.peer(1).chain.numbers.clone();
|
||||
net.sync();
|
||||
assert_eq!(net.peer(0).chain.numbers, peer1_chain);
|
||||
assert_eq!(net.peer(1).chain.numbers, peer1_chain);
|
||||
assert_eq!(net.peer(2).chain.numbers, peer1_chain);
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
use util::*;
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
pub enum TransactionKind {
|
||||
ContractCreation,
|
||||
MessageCall
|
||||
pub enum Action {
|
||||
Create,
|
||||
Call(Address),
|
||||
}
|
||||
|
||||
/// A set of information describing an externally-originating message call
|
||||
@ -12,63 +11,61 @@ pub struct Transaction {
|
||||
pub nonce: U256,
|
||||
pub gas_price: U256,
|
||||
pub gas: U256,
|
||||
pub to: Option<Address>,
|
||||
pub action: Action,
|
||||
pub value: U256,
|
||||
pub data: Bytes
|
||||
pub data: Bytes,
|
||||
|
||||
hash: RefCell<Option<H256>>, //TODO: make this private
|
||||
}
|
||||
|
||||
impl RlpStandard for Transaction {
|
||||
fn rlp_append(&self, s: &mut RlpStream) {
|
||||
s.append_list(6);
|
||||
s.append(&self.nonce);
|
||||
s.append(&self.gas_price);
|
||||
s.append(&self.gas);
|
||||
match self.action {
|
||||
Action::Create => s.append_empty_data(),
|
||||
Action::Call(ref to) => s.append(to),
|
||||
};
|
||||
s.append(&self.value);
|
||||
s.append(&self.data);
|
||||
}
|
||||
}
|
||||
|
||||
impl Transaction {
|
||||
pub fn new() -> Self {
|
||||
Transaction {
|
||||
nonce: U256::zero(),
|
||||
gas_price: U256::zero(),
|
||||
gas: U256::zero(),
|
||||
to: None,
|
||||
value: U256::zero(),
|
||||
data: vec![]
|
||||
/// Get the hash of this header (sha3 of the RLP).
|
||||
pub fn hash(&self) -> H256 {
|
||||
let mut hash = self.hash.borrow_mut();
|
||||
match &mut *hash {
|
||||
&mut Some(ref h) => h.clone(),
|
||||
hash @ &mut None => {
|
||||
*hash = Some(self.rlp_sha3());
|
||||
hash.as_ref().unwrap().clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns sender of the transaction.
|
||||
/// TODO: implement
|
||||
pub fn sender(&self) -> Address {
|
||||
Address::new()
|
||||
}
|
||||
|
||||
/// Is this transaction meant to create a contract?
|
||||
pub fn is_contract_creation(&self) -> bool {
|
||||
self.kind() == TransactionKind::ContractCreation
|
||||
}
|
||||
|
||||
/// Is this transaction meant to send a message?
|
||||
pub fn is_message_call(&self) -> bool {
|
||||
self.kind() == TransactionKind::MessageCall
|
||||
/// Note that some fields have changed. Resets the memoised hash.
|
||||
pub fn note_dirty(&self) {
|
||||
*self.hash.borrow_mut() = None;
|
||||
}
|
||||
|
||||
/// Returns transaction type.
|
||||
pub fn kind(&self) -> TransactionKind {
|
||||
match self.to.is_some() {
|
||||
true => TransactionKind::MessageCall,
|
||||
false => TransactionKind::ContractCreation
|
||||
}
|
||||
}
|
||||
pub fn action(&self) -> &Action { &self.action }
|
||||
|
||||
/// Get the hash of this transaction.
|
||||
pub fn sha3(&self) -> H256 {
|
||||
unimplemented!();
|
||||
}
|
||||
/// Returns transaction sender.
|
||||
pub fn sender(&self) -> Address { Address::new() }
|
||||
}
|
||||
|
||||
impl Encodable for Transaction {
|
||||
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
||||
encoder.emit_list(| e | {
|
||||
self.nonce.encode(e);
|
||||
self.gas_price.encode(e);
|
||||
self.gas.encode(e);
|
||||
self.to.encode(e);
|
||||
self.value.encode(e);
|
||||
self.data.encode(e);
|
||||
})
|
||||
impl Decodable for Action {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
let rlp = decoder.as_rlp();
|
||||
if rlp.is_empty() {
|
||||
Ok(Action::Create)
|
||||
} else {
|
||||
Ok(Action::Call(try!(rlp.as_val())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,12 +77,12 @@ impl Decodable for Transaction {
|
||||
nonce: try!(Decodable::decode(&d[0])),
|
||||
gas_price: try!(Decodable::decode(&d[1])),
|
||||
gas: try!(Decodable::decode(&d[2])),
|
||||
to: try!(Decodable::decode(&d[3])),
|
||||
action: try!(Decodable::decode(&d[3])),
|
||||
value: try!(Decodable::decode(&d[4])),
|
||||
data: try!(Decodable::decode(&d[5])),
|
||||
hash: RefCell::new(None)
|
||||
};
|
||||
|
||||
Ok(transaction)
|
||||
}
|
||||
}
|
||||
|
||||
|
154
src/verification.rs
Normal file
154
src/verification.rs
Normal file
@ -0,0 +1,154 @@
|
||||
/// Block and transaction verification functions
|
||||
///
|
||||
/// Block verification is done in 3 steps
|
||||
/// 1. Quick verification upon adding to the block queue
|
||||
/// 2. Signatures verification done in the queue.
|
||||
/// 3. Final verification against the blockchain done before enactment.
|
||||
|
||||
use common::*;
|
||||
use engine::Engine;
|
||||
use blockchain::BlockChain;
|
||||
|
||||
/// Phase 1 quick block verification. Only does checks that are cheap. Operates on a single block
|
||||
pub fn verify_block_basic(bytes: &[u8], engine: &Engine) -> Result<(), Error> {
|
||||
let block = BlockView::new(bytes);
|
||||
let header = block.header();
|
||||
try!(verify_header(&header));
|
||||
try!(verify_block_integrity(bytes, &header.transactions_root, &header.uncles_hash));
|
||||
try!(engine.verify_block_basic(&header, Some(bytes)));
|
||||
for u in Rlp::new(bytes).at(2).iter().map(|rlp| rlp.as_val::<Header>()) {
|
||||
try!(verify_header(&u));
|
||||
try!(engine.verify_block_basic(&u, None));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Phase 2 verification. Perform costly checks such as transaction signatures and block nonce for ethash.
|
||||
/// Still operates on a individual block
|
||||
/// TODO: return cached transactions, header hash.
|
||||
pub fn verify_block_unordered(bytes: &[u8], engine: &Engine) -> Result<(), Error> {
|
||||
let block = BlockView::new(bytes);
|
||||
let header = block.header();
|
||||
try!(engine.verify_block_unordered(&header, Some(bytes)));
|
||||
for u in Rlp::new(bytes).at(2).iter().map(|rlp| rlp.as_val::<Header>()) {
|
||||
try!(engine.verify_block_unordered(&u, None));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Phase 3 verification. Check block information against parent and uncles.
|
||||
pub fn verify_block_final(bytes: &[u8], engine: &Engine, bc: &BlockChain) -> Result<(), Error> {
|
||||
let block = BlockView::new(bytes);
|
||||
let header = block.header();
|
||||
let parent = try!(bc.block_header(&header.parent_hash).ok_or::<Error>(From::from(BlockError::UnknownParent(header.parent_hash.clone()))));
|
||||
try!(verify_parent(&header, &parent));
|
||||
try!(engine.verify_block_final(&header, &parent, Some(bytes)));
|
||||
|
||||
let num_uncles = Rlp::new(bytes).at(2).item_count();
|
||||
if num_uncles != 0 {
|
||||
if num_uncles > engine.maximum_uncle_count() {
|
||||
return Err(From::from(BlockError::TooManyUncles(OutOfBounds { min: 0, max: engine.maximum_uncle_count(), found: num_uncles })));
|
||||
}
|
||||
|
||||
let mut excluded = HashSet::new();
|
||||
excluded.insert(header.hash());
|
||||
let mut hash = header.parent_hash.clone();
|
||||
excluded.insert(hash.clone());
|
||||
for _ in 0..6 {
|
||||
match bc.block_details(&hash) {
|
||||
Some(details) => {
|
||||
excluded.insert(details.parent.clone());
|
||||
let b = bc.block(&hash).unwrap();
|
||||
excluded.extend(BlockView::new(&b).uncle_hashes());
|
||||
hash = details.parent;
|
||||
}
|
||||
None => break
|
||||
}
|
||||
}
|
||||
|
||||
for uncle in Rlp::new(bytes).at(2).iter().map(|rlp| rlp.as_val::<Header>()) {
|
||||
let uncle_parent = try!(bc.block_header(&uncle.parent_hash).ok_or::<Error>(From::from(BlockError::UnknownUncleParent(uncle.parent_hash.clone()))));
|
||||
if excluded.contains(&uncle_parent.hash()) {
|
||||
return Err(From::from(BlockError::UncleInChain(uncle_parent.hash())))
|
||||
}
|
||||
|
||||
// m_currentBlock.number() - uncle.number() m_cB.n - uP.n()
|
||||
// 1 2
|
||||
// 2
|
||||
// 3
|
||||
// 4
|
||||
// 5
|
||||
// 6 7
|
||||
// (8 Invalid)
|
||||
|
||||
let depth = if header.number > uncle.number { header.number - uncle.number } else { 0 };
|
||||
if depth > 6 {
|
||||
return Err(From::from(BlockError::UncleTooOld(OutOfBounds { min: header.number - depth, max: header.number - 1, found: uncle.number })));
|
||||
}
|
||||
else if depth < 1 {
|
||||
return Err(From::from(BlockError::UncleIsBrother(OutOfBounds { min: header.number - depth, max: header.number - 1, found: uncle.number })));
|
||||
}
|
||||
|
||||
// cB
|
||||
// cB.p^1 1 depth, valid uncle
|
||||
// cB.p^2 ---/ 2
|
||||
// cB.p^3 -----/ 3
|
||||
// cB.p^4 -------/ 4
|
||||
// cB.p^5 ---------/ 5
|
||||
// cB.p^6 -----------/ 6
|
||||
// cB.p^7 -------------/
|
||||
// cB.p^8
|
||||
let mut expected_uncle_parent = header.parent_hash.clone();
|
||||
for _ in 0..depth {
|
||||
expected_uncle_parent = bc.block_details(&expected_uncle_parent).unwrap().parent;
|
||||
}
|
||||
if expected_uncle_parent != uncle_parent.hash() {
|
||||
return Err(From::from(BlockError::UncleParentNotInChain(uncle_parent.hash())));
|
||||
}
|
||||
|
||||
try!(engine.verify_block_final(&uncle, &uncle_parent, Some(bytes)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check basic header parameters.
|
||||
fn verify_header(header: &Header) -> Result<(), Error> {
|
||||
if header.number > From::from(BlockNumber::max_value()) {
|
||||
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 {
|
||||
return Err(From::from(BlockError::TooMuchGasUsed(OutOfBounds { max: header.gas_limit, min: From::from(0), found: header.gas_used })));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check header parameters agains parent header.
|
||||
fn verify_parent(header: &Header, parent: &Header) -> Result<(), Error> {
|
||||
if !header.parent_hash.is_zero() && parent.hash() != header.parent_hash {
|
||||
return Err(From::from(BlockError::InvalidParentHash(Mismatch { expected: parent.hash(), found: header.parent_hash.clone() })))
|
||||
}
|
||||
if header.timestamp <= parent.timestamp {
|
||||
return Err(From::from(BlockError::InvalidTimestamp(OutOfBounds { max: u64::max_value(), min: parent.timestamp + 1, found: header.timestamp })))
|
||||
}
|
||||
if header.number <= parent.number {
|
||||
return Err(From::from(BlockError::InvalidNumber(OutOfBounds { max: BlockNumber::max_value(), min: parent.number + 1, found: header.number })));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Verify block data against header: transactions root and uncles hash.
|
||||
fn verify_block_integrity(block: &[u8], transactions_root: &H256, uncles_hash: &H256) -> Result<(), Error> {
|
||||
let block = Rlp::new(block);
|
||||
let tx = block.at(1);
|
||||
let expected_root = &ordered_trie_root(tx.iter().map(|r| r.as_raw().to_vec()).collect()); //TODO: get rid of vectors here
|
||||
if expected_root != transactions_root {
|
||||
return Err(From::from(BlockError::InvalidTransactionsRoot(Mismatch { expected: expected_root.clone(), found: transactions_root.clone() })))
|
||||
}
|
||||
let expected_uncles = &block.at(2).as_raw().sha3();
|
||||
if expected_uncles != uncles_hash {
|
||||
return Err(From::from(BlockError::InvalidUnclesHash(Mismatch { expected: expected_uncles.clone(), found: uncles_hash.clone() })))
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ impl<'a> HeaderView<'a> {
|
||||
pub fn difficulty(&self) -> U256 { self.rlp.val_at(7) }
|
||||
|
||||
/// 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.
|
||||
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) }
|
||||
|
||||
/// 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.
|
||||
pub fn extra_data(&self) -> Bytes { self.rlp.val_at(12) }
|
||||
|
Loading…
Reference in New Issue
Block a user