Merge branch 'master' into delegatecall
This commit is contained in:
commit
fbb57b1c1b
@ -20,10 +20,13 @@ time = "0.1"
|
||||
evmjit = { path = "rust-evmjit", optional = true }
|
||||
ethash = { path = "ethash" }
|
||||
num_cpus = "0.2"
|
||||
ctrlc = "1.0"
|
||||
clippy = "*" # Always newest, since we use nightly
|
||||
|
||||
[features]
|
||||
jit = ["evmjit"]
|
||||
evm_debug = []
|
||||
test-heavy = []
|
||||
evm-debug = []
|
||||
|
||||
[[bin]]
|
||||
name = "client"
|
||||
|
@ -30,11 +30,13 @@ impl EthashManager {
|
||||
/// `nonce` - The nonce to pack into the mix
|
||||
pub fn compute_light(&self, block_number: u64, header_hash: &H256, nonce: u64) -> ProofOfWork {
|
||||
let epoch = block_number / ETHASH_EPOCH_LENGTH;
|
||||
if !self.lights.read().unwrap().contains_key(&epoch) {
|
||||
let mut lights = self.lights.write().unwrap(); // obtain write lock
|
||||
if !lights.contains_key(&epoch) {
|
||||
let light = Light::new(block_number);
|
||||
lights.insert(epoch, light);
|
||||
while !self.lights.read().unwrap().contains_key(&epoch) {
|
||||
if let Ok(mut lights) = self.lights.try_write()
|
||||
{
|
||||
if !lights.contains_key(&epoch) {
|
||||
let light = Light::new(block_number);
|
||||
lights.insert(epoch, light);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.lights.read().unwrap().get(&epoch).unwrap().compute(header_hash, nonce)
|
||||
|
3
hook.sh
Executable file
3
hook.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
echo "#!/bin/sh\ncargo test" >> ./.git/hooks/pre-push
|
||||
chmod +x ./.git/hooks/pre-push
|
@ -103,7 +103,7 @@ impl Account {
|
||||
/// Get (and cache) the contents of the trie's storage at `key`.
|
||||
pub fn storage_at(&self, db: &HashDB, key: &H256) -> H256 {
|
||||
self.storage_overlay.borrow_mut().entry(key.clone()).or_insert_with(||{
|
||||
(Filth::Clean, H256::from(SecTrieDB::new(db, &self.storage_root).get(key.bytes()).map(|v| -> U256 {decode(v)}).unwrap_or(U256::zero())))
|
||||
(Filth::Clean, H256::from(SecTrieDB::new(db, &self.storage_root).get(key.bytes()).map_or(U256::zero(), |v| -> U256 {decode(v)})))
|
||||
}).1.clone()
|
||||
}
|
||||
|
||||
@ -149,7 +149,7 @@ impl Account {
|
||||
/// Provide a database to lookup `code_hash`. Should not be called if it is a contract without code.
|
||||
pub fn cache_code(&mut self, db: &HashDB) -> bool {
|
||||
// TODO: fill out self.code_cache;
|
||||
return self.is_cached() ||
|
||||
self.is_cached() ||
|
||||
match self.code_hash {
|
||||
Some(ref h) => match db.lookup(h) {
|
||||
Some(x) => { self.code_cache = x.to_vec(); true },
|
||||
@ -248,8 +248,8 @@ mod tests {
|
||||
|
||||
let a = Account::from_rlp(&rlp);
|
||||
assert_eq!(a.storage_root().unwrap().hex(), "c57e1afb758b07f8d2c8f13a3b6e44fa5ff94ab266facc5a4fd3f062426e50b2");
|
||||
assert_eq!(a.storage_at(&mut db, &H256::from(&U256::from(0x00u64))), H256::from(&U256::from(0x1234u64)));
|
||||
assert_eq!(a.storage_at(&mut db, &H256::from(&U256::from(0x01u64))), H256::new());
|
||||
assert_eq!(a.storage_at(&db, &H256::from(&U256::from(0x00u64))), H256::from(&U256::from(0x1234u64)));
|
||||
assert_eq!(a.storage_at(&db, &H256::from(&U256::from(0x01u64))), H256::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -15,10 +15,10 @@ pub enum Existance {
|
||||
|
||||
impl fmt::Display for Existance {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
&Existance::Born => try!(write!(f, "+++")),
|
||||
&Existance::Alive => try!(write!(f, "***")),
|
||||
&Existance::Died => try!(write!(f, "XXX")),
|
||||
match *self {
|
||||
Existance::Born => try!(write!(f, "+++")),
|
||||
Existance::Alive => try!(write!(f, "***")),
|
||||
Existance::Died => try!(write!(f, "XXX")),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -72,11 +72,11 @@ impl AccountDiff {
|
||||
code: Diff::new(pre.code.clone(), post.code.clone()),
|
||||
storage: storage.into_iter().map(|k|
|
||||
(k.clone(), Diff::new(
|
||||
pre.storage.get(&k).cloned().unwrap_or(H256::new()),
|
||||
post.storage.get(&k).cloned().unwrap_or(H256::new())
|
||||
pre.storage.get(&k).cloned().unwrap_or_else(H256::new),
|
||||
post.storage.get(&k).cloned().unwrap_or_else(H256::new)
|
||||
))).collect(),
|
||||
};
|
||||
if r.balance.is_same() && r.nonce.is_same() && r.code.is_same() && r.storage.len() == 0 {
|
||||
if r.balance.is_same() && r.nonce.is_same() && r.code.is_same() && r.storage.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(r)
|
||||
@ -112,16 +112,15 @@ impl fmt::Display for AccountDiff {
|
||||
Diff::Changed(ref pre, ref post) => try!(write!(f, "${} ({} {} {})", post, pre, if pre > post {"-"} else {"+"}, *max(pre, post) - *min(pre, post))),
|
||||
_ => {},
|
||||
}
|
||||
match self.code {
|
||||
Diff::Born(ref x) => try!(write!(f, " code {}", x.pretty())),
|
||||
_ => {},
|
||||
if let Diff::Born(ref x) = self.code {
|
||||
try!(write!(f, " code {}", x.pretty()));
|
||||
}
|
||||
try!(write!(f, "\n"));
|
||||
for (k, dv) in self.storage.iter() {
|
||||
match dv {
|
||||
&Diff::Born(ref v) => try!(write!(f, " + {} => {}\n", interpreted_hash(k), interpreted_hash(v))),
|
||||
&Diff::Changed(ref pre, ref post) => try!(write!(f, " * {} => {} (was {})\n", interpreted_hash(k), interpreted_hash(post), interpreted_hash(pre))),
|
||||
&Diff::Died(_) => try!(write!(f, " X {}\n", interpreted_hash(k))),
|
||||
for (k, dv) in &self.storage {
|
||||
match *dv {
|
||||
Diff::Born(ref v) => try!(write!(f, " + {} => {}\n", interpreted_hash(k), interpreted_hash(v))),
|
||||
Diff::Changed(ref pre, ref post) => try!(write!(f, " * {} => {} (was {})\n", interpreted_hash(k), interpreted_hash(post), interpreted_hash(pre))),
|
||||
Diff::Died(_) => try!(write!(f, " X {}\n", interpreted_hash(k))),
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
@ -3,17 +3,18 @@ extern crate ethcore;
|
||||
extern crate rustc_serialize;
|
||||
extern crate log;
|
||||
extern crate env_logger;
|
||||
extern crate ctrlc;
|
||||
|
||||
use std::io::stdin;
|
||||
use std::env;
|
||||
use log::{LogLevelFilter};
|
||||
use env_logger::LogBuilder;
|
||||
use ctrlc::CtrlC;
|
||||
use util::*;
|
||||
use ethcore::client::*;
|
||||
use ethcore::service::ClientService;
|
||||
use ethcore::service::{ClientService, NetSyncMessage};
|
||||
use ethcore::ethereum;
|
||||
use ethcore::blockchain::CacheSize;
|
||||
use ethcore::sync::*;
|
||||
use ethcore::sync::EthSync;
|
||||
|
||||
fn setup_log() {
|
||||
let mut builder = LogBuilder::new();
|
||||
@ -30,41 +31,57 @@ fn main() {
|
||||
setup_log();
|
||||
let spec = ethereum::new_frontier();
|
||||
let mut service = ClientService::start(spec).unwrap();
|
||||
let io_handler = Box::new(ClientIoHandler { client: service.client(), timer: 0, info: Default::default() });
|
||||
let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default(), sync: service.sync() });
|
||||
service.io().register_handler(io_handler).expect("Error registering IO handler");
|
||||
loop {
|
||||
let mut cmd = String::new();
|
||||
stdin().read_line(&mut cmd).unwrap();
|
||||
if cmd == "quit\n" || cmd == "exit\n" || cmd == "q\n" {
|
||||
break;
|
||||
|
||||
let exit = Arc::new(Condvar::new());
|
||||
let e = exit.clone();
|
||||
CtrlC::set_handler(move || { e.notify_all(); });
|
||||
let mutex = Mutex::new(());
|
||||
let _ = exit.wait(mutex.lock().unwrap()).unwrap();
|
||||
}
|
||||
|
||||
struct Informant {
|
||||
chain_info: RwLock<Option<BlockChainInfo>>,
|
||||
cache_info: RwLock<Option<CacheSize>>,
|
||||
report: RwLock<Option<ClientReport>>,
|
||||
}
|
||||
|
||||
impl Default for Informant {
|
||||
fn default() -> Self {
|
||||
Informant {
|
||||
chain_info: RwLock::new(None),
|
||||
cache_info: RwLock::new(None),
|
||||
report: RwLock::new(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
struct Informant {
|
||||
chain_info: Option<BlockChainInfo>,
|
||||
cache_info: Option<CacheSize>,
|
||||
report: Option<ClientReport>,
|
||||
}
|
||||
|
||||
impl Informant {
|
||||
pub fn tick(&mut self, client: &Client) {
|
||||
pub fn tick(&self, client: &Client, sync: &EthSync) {
|
||||
// 5 seconds betwen calls. TODO: calculate this properly.
|
||||
let dur = 5usize;
|
||||
|
||||
let chain_info = client.chain_info();
|
||||
let queue_info = client.queue_info();
|
||||
let cache_info = client.cache_info();
|
||||
let report = client.report();
|
||||
let sync_info = sync.status();
|
||||
|
||||
if let (_, &Some(ref last_cache_info), &Some(ref last_report)) = (&self.chain_info, &self.cache_info, &self.report) {
|
||||
println!("[ {} {} ]---[ {} blk/s | {} tx/s | {} gas/s //···{}···// {} ({}) bl {} ({}) ex ]",
|
||||
if let (_, &Some(ref last_cache_info), &Some(ref last_report)) = (self.chain_info.read().unwrap().deref(), self.cache_info.read().unwrap().deref(), self.report.read().unwrap().deref()) {
|
||||
println!("[ {} {} ]---[ {} blk/s | {} tx/s | {} gas/s //··· {}/{} peers, {} downloaded, {}+{} queued ···// {} ({}) bl {} ({}) ex ]",
|
||||
chain_info.best_block_number,
|
||||
chain_info.best_block_hash,
|
||||
(report.blocks_imported - last_report.blocks_imported) / dur,
|
||||
(report.transactions_applied - last_report.transactions_applied) / dur,
|
||||
(report.gas_processed - last_report.gas_processed) / From::from(dur),
|
||||
0, // TODO: peers
|
||||
|
||||
sync_info.num_active_peers,
|
||||
sync_info.num_peers,
|
||||
sync_info.blocks_received,
|
||||
queue_info.unverified_queue_size,
|
||||
queue_info.verified_queue_size,
|
||||
|
||||
cache_info.blocks,
|
||||
cache_info.blocks as isize - last_cache_info.blocks as isize,
|
||||
cache_info.block_details,
|
||||
@ -72,28 +89,28 @@ impl Informant {
|
||||
);
|
||||
}
|
||||
|
||||
self.chain_info = Some(chain_info);
|
||||
self.cache_info = Some(cache_info);
|
||||
self.report = Some(report);
|
||||
*self.chain_info.write().unwrap().deref_mut() = Some(chain_info);
|
||||
*self.cache_info.write().unwrap().deref_mut() = Some(cache_info);
|
||||
*self.report.write().unwrap().deref_mut() = Some(report);
|
||||
}
|
||||
}
|
||||
|
||||
const INFO_TIMER: TimerToken = 0;
|
||||
|
||||
struct ClientIoHandler {
|
||||
client: Arc<RwLock<Client>>,
|
||||
timer: TimerToken,
|
||||
client: Arc<Client>,
|
||||
sync: Arc<EthSync>,
|
||||
info: Informant,
|
||||
}
|
||||
|
||||
impl IoHandler<NetSyncMessage> for ClientIoHandler {
|
||||
fn initialize<'s>(&'s mut self, io: &mut IoContext<'s, NetSyncMessage>) {
|
||||
self.timer = io.register_timer(5000).expect("Error registering timer");
|
||||
fn initialize(&self, io: &IoContext<NetSyncMessage>) {
|
||||
io.register_timer(INFO_TIMER, 5000).expect("Error registering timer");
|
||||
}
|
||||
|
||||
fn timeout<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>, timer: TimerToken) {
|
||||
if self.timer == timer {
|
||||
let client = self.client.read().unwrap();
|
||||
client.tick();
|
||||
self.info.tick(client.deref());
|
||||
fn timeout(&self, _io: &IoContext<NetSyncMessage>, timer: TimerToken) {
|
||||
if INFO_TIMER == timer {
|
||||
self.info.tick(&self.client, &self.sync);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![allow(ptr_arg)] // Because of &LastHashes -> &Vec<_>
|
||||
|
||||
use common::*;
|
||||
use engine::*;
|
||||
use state::*;
|
||||
@ -173,7 +175,7 @@ impl<'x, 'y> OpenBlock<'x, 'y> {
|
||||
timestamp: self.block.header.timestamp,
|
||||
difficulty: self.block.header.difficulty.clone(),
|
||||
last_hashes: self.last_hashes.clone(), // TODO: should be a reference.
|
||||
gas_used: self.block.archive.last().map(|t| t.receipt.gas_used).unwrap_or(U256::from(0)),
|
||||
gas_used: self.block.archive.last().map_or(U256::zero(), |t| t.receipt.gas_used),
|
||||
gas_limit: self.block.header.gas_limit.clone(),
|
||||
}
|
||||
}
|
||||
@ -204,7 +206,7 @@ impl<'x, 'y> OpenBlock<'x, 'y> {
|
||||
s.block.header.state_root = s.block.state.root().clone();
|
||||
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.gas_used = s.block.archive.last().map_or(U256::zero(), |t| t.receipt.gas_used);
|
||||
s.block.header.note_dirty();
|
||||
|
||||
ClosedBlock::new(s, uncle_bytes)
|
||||
@ -255,7 +257,7 @@ impl SealedBlock {
|
||||
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); }
|
||||
for e in &self.block.archive { e.transaction.rlp_append(&mut block_rlp); }
|
||||
block_rlp.append_raw(&self.uncle_bytes, 1);
|
||||
block_rlp.out()
|
||||
}
|
||||
|
@ -1,12 +1,25 @@
|
||||
//! A queue of blocks. Sits between network or other I/O and the BlockChain.
|
||||
//! Sorts them ready for blockchain insertion.
|
||||
use std::thread::{JoinHandle, self};
|
||||
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
|
||||
use util::*;
|
||||
use verification::*;
|
||||
use error::*;
|
||||
use engine::Engine;
|
||||
use sync::*;
|
||||
use views::*;
|
||||
use header::*;
|
||||
use service::*;
|
||||
|
||||
/// Block queue status
|
||||
#[derive(Debug)]
|
||||
pub struct BlockQueueInfo {
|
||||
/// Indicates that queue is full
|
||||
pub full: bool,
|
||||
/// Number of queued blocks pending verification
|
||||
pub unverified_queue_size: usize,
|
||||
/// Number of verified queued blocks pending import
|
||||
pub verified_queue_size: usize,
|
||||
}
|
||||
|
||||
/// A queue of blocks. Sits between network or other I/O and the BlockChain.
|
||||
/// Sorts them ready for blockchain insertion.
|
||||
@ -63,14 +76,15 @@ impl BlockQueue {
|
||||
let deleting = Arc::new(AtomicBool::new(false));
|
||||
|
||||
let mut verifiers: Vec<JoinHandle<()>> = Vec::new();
|
||||
let thread_count = max(::num_cpus::get(), 2) - 1;
|
||||
for _ in 0..thread_count {
|
||||
let thread_count = max(::num_cpus::get(), 3) - 2;
|
||||
for i in 0..thread_count {
|
||||
let verification = verification.clone();
|
||||
let engine = engine.clone();
|
||||
let more_to_verify = more_to_verify.clone();
|
||||
let ready_signal = ready_signal.clone();
|
||||
let deleting = deleting.clone();
|
||||
verifiers.push(thread::spawn(move || BlockQueue::verify(verification, engine, more_to_verify, ready_signal, deleting)));
|
||||
verifiers.push(thread::Builder::new().name(format!("Verifier #{}", i)).spawn(move || BlockQueue::verify(verification, engine, more_to_verify, ready_signal, deleting))
|
||||
.expect("Error starting block verification thread"));
|
||||
}
|
||||
BlockQueue {
|
||||
engine: engine,
|
||||
@ -204,7 +218,7 @@ impl BlockQueue {
|
||||
verification.verified = new_verified;
|
||||
}
|
||||
|
||||
/// TODO [arkpar] Please document me
|
||||
/// Removes up to `max` verified blocks from the queue
|
||||
pub fn drain(&mut self, max: usize) -> Vec<PreVerifiedBlock> {
|
||||
let mut verification = self.verification.lock().unwrap();
|
||||
let count = min(max, verification.verified.len());
|
||||
@ -215,8 +229,21 @@ impl BlockQueue {
|
||||
result.push(block);
|
||||
}
|
||||
self.ready_signal.reset();
|
||||
if !verification.verified.is_empty() {
|
||||
self.ready_signal.set();
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Get queue status.
|
||||
pub fn queue_info(&self) -> BlockQueueInfo {
|
||||
let verification = self.verification.lock().unwrap();
|
||||
BlockQueueInfo {
|
||||
full: false,
|
||||
verified_queue_size: verification.verified.len(),
|
||||
unverified_queue_size: verification.unverified.len(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BlockQueue {
|
||||
@ -234,7 +261,7 @@ impl Drop for BlockQueue {
|
||||
mod tests {
|
||||
use util::*;
|
||||
use spec::*;
|
||||
use queue::*;
|
||||
use block_queue::*;
|
||||
|
||||
#[test]
|
||||
fn test_block_queue() {
|
@ -153,9 +153,8 @@ impl BlockProvider for BlockChain {
|
||||
fn block(&self, hash: &H256) -> Option<Bytes> {
|
||||
{
|
||||
let read = self.blocks.read().unwrap();
|
||||
match read.get(hash) {
|
||||
Some(v) => return Some(v.clone()),
|
||||
None => ()
|
||||
if let Some(v) = read.get(hash) {
|
||||
return Some(v.clone());
|
||||
}
|
||||
}
|
||||
|
||||
@ -188,7 +187,7 @@ impl BlockProvider for BlockChain {
|
||||
|
||||
const COLLECTION_QUEUE_SIZE: usize = 2;
|
||||
const MIN_CACHE_SIZE: usize = 1;
|
||||
const MAX_CACHE_SIZE: usize = 1024 * 1024 * 1;
|
||||
const MAX_CACHE_SIZE: usize = 1024 * 1024;
|
||||
|
||||
impl BlockChain {
|
||||
/// Create new instance of blockchain from given Genesis
|
||||
@ -342,19 +341,19 @@ impl BlockChain {
|
||||
Some(h) => h,
|
||||
None => return None,
|
||||
};
|
||||
Some(self._tree_route((from_details, from), (to_details, to)))
|
||||
Some(self._tree_route((&from_details, &from), (&to_details, &to)))
|
||||
}
|
||||
|
||||
/// Similar to `tree_route` function, but can be used to return a route
|
||||
/// between blocks which may not be in database yet.
|
||||
fn _tree_route(&self, from: (BlockDetails, H256), to: (BlockDetails, H256)) -> TreeRoute {
|
||||
fn _tree_route(&self, from: (&BlockDetails, &H256), to: (&BlockDetails, &H256)) -> TreeRoute {
|
||||
let mut from_branch = vec![];
|
||||
let mut to_branch = vec![];
|
||||
|
||||
let mut from_details = from.0;
|
||||
let mut to_details = to.0;
|
||||
let mut current_from = from.1;
|
||||
let mut current_to = to.1;
|
||||
let mut from_details = from.0.clone();
|
||||
let mut to_details = to.0.clone();
|
||||
let mut current_from = from.1.clone();
|
||||
let mut current_to = to.1.clone();
|
||||
|
||||
// reset from && to to the same level
|
||||
while from_details.number > to_details.number {
|
||||
@ -409,7 +408,7 @@ impl BlockChain {
|
||||
|
||||
// store block in db
|
||||
self.blocks_db.put(&hash, &bytes).unwrap();
|
||||
let (batch, new_best) = self.block_to_extras_insert_batch(bytes);
|
||||
let (batch, new_best, details) = self.block_to_extras_insert_batch(bytes);
|
||||
|
||||
// update best block
|
||||
let mut best_block = self.best_block.write().unwrap();
|
||||
@ -420,6 +419,8 @@ impl BlockChain {
|
||||
// update caches
|
||||
let mut write = self.block_details.write().unwrap();
|
||||
write.remove(&header.parent_hash());
|
||||
write.insert(hash.clone(), details);
|
||||
self.note_used(CacheID::Block(hash));
|
||||
|
||||
// update extras database
|
||||
self.extras_db.write(batch).unwrap();
|
||||
@ -427,7 +428,7 @@ impl BlockChain {
|
||||
|
||||
/// Transforms block into WriteBatch that may be written into database
|
||||
/// Additionally, if it's new best block it returns new best block object.
|
||||
fn block_to_extras_insert_batch(&self, bytes: &[u8]) -> (WriteBatch, Option<BestBlock>) {
|
||||
fn block_to_extras_insert_batch(&self, bytes: &[u8]) -> (WriteBatch, Option<BestBlock>, BlockDetails) {
|
||||
// create views onto rlp
|
||||
let block = BlockView::new(bytes);
|
||||
let header = block.header_view();
|
||||
@ -459,7 +460,7 @@ impl BlockChain {
|
||||
|
||||
// if it's not new best block, just return
|
||||
if !is_new_best {
|
||||
return (batch, None);
|
||||
return (batch, None, details);
|
||||
}
|
||||
|
||||
// if its new best block we need to make sure that all ancestors
|
||||
@ -467,7 +468,7 @@ impl BlockChain {
|
||||
// find the route between old best block and the new one
|
||||
let best_hash = self.best_block_hash();
|
||||
let best_details = self.block_details(&best_hash).expect("best block hash is invalid!");
|
||||
let route = self._tree_route((best_details, best_hash), (details, hash.clone()));
|
||||
let route = self._tree_route((&best_details, &best_hash), (&details, &hash));
|
||||
|
||||
match route.blocks.len() {
|
||||
// its our parent
|
||||
@ -494,7 +495,7 @@ impl BlockChain {
|
||||
total_difficulty: total_difficulty
|
||||
};
|
||||
|
||||
(batch, Some(best_block))
|
||||
(batch, Some(best_block), details)
|
||||
}
|
||||
|
||||
/// Returns true if transaction is known.
|
||||
@ -527,9 +528,8 @@ impl BlockChain {
|
||||
K: ExtrasSliceConvertable + Eq + Hash + Clone {
|
||||
{
|
||||
let read = cache.read().unwrap();
|
||||
match read.get(hash) {
|
||||
Some(v) => return Some(v.clone()),
|
||||
None => ()
|
||||
if let Some(v) = read.get(hash) {
|
||||
return Some(v.clone());
|
||||
}
|
||||
}
|
||||
|
||||
@ -549,9 +549,8 @@ impl BlockChain {
|
||||
T: ExtrasIndexable {
|
||||
{
|
||||
let read = cache.read().unwrap();
|
||||
match read.get(hash) {
|
||||
Some(_) => return true,
|
||||
None => ()
|
||||
if let Some(_) = read.get(hash) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -670,6 +669,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(cyclomatic_complexity)]
|
||||
fn test_small_fork() {
|
||||
let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a07dba07d6b448a186e9612e5f737d1c909dce473e53199901a302c00646d523c1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a059262c330941f3fe2a34d16d6e3c7b30d2ceb37c6a0e9a994c494ee1a61d2410885aa4c8bf8e56e264c0c0".from_hex().unwrap();
|
||||
let b1 = "f90261f901f9a05716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a0e78628dd45a1f8dc495594d83b76c588a3ee67463260f8b7d4a42f574aeab29aa0e9244cf7503b79c03d3a099e07a80d2dbc77bb0b502d8a89d51ac0d68dd31313b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884562791e580a051b3ecba4e3f2b49c11d42dd0851ec514b1be3138080f72a2b6e83868275d98f8877671f479c414b47f862f86080018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca09e2709d7ec9bbe6b1bbbf0b2088828d14cd5e8642a1fee22dc74bfa89761a7f9a04bd8813dee4be989accdb708b1c2e325a7e9c695a8024e30e89d6c644e424747c0".from_hex().unwrap();
|
||||
|
@ -94,16 +94,13 @@ pub fn new_builtin_exec(name: &str) -> Option<Box<Fn(&[u8], &mut [u8])>> {
|
||||
if it.v == H256::from(&U256::from(27)) || it.v == H256::from(&U256::from(28)) {
|
||||
let s = Signature::from_rsv(&it.r, &it.s, it.v[31] - 27);
|
||||
if ec::is_valid(&s) {
|
||||
match ec::recover(&s, &it.hash) {
|
||||
Ok(p) => {
|
||||
let r = p.as_slice().sha3();
|
||||
// NICE: optimise and separate out into populate-like function
|
||||
for i in 0..min(32, output.len()) {
|
||||
output[i] = if i < 12 {0} else {r[i]};
|
||||
}
|
||||
if let Ok(p) = ec::recover(&s, &it.hash) {
|
||||
let r = p.as_slice().sha3();
|
||||
// NICE: optimise and separate out into populate-like function
|
||||
for i in 0..min(32, output.len()) {
|
||||
output[i] = if i < 12 {0} else {r[i]};
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
})),
|
||||
|
@ -6,8 +6,8 @@ use error::*;
|
||||
use header::BlockNumber;
|
||||
use spec::Spec;
|
||||
use engine::Engine;
|
||||
use queue::BlockQueue;
|
||||
use sync::NetSyncMessage;
|
||||
use block_queue::{BlockQueue, BlockQueueInfo};
|
||||
use service::NetSyncMessage;
|
||||
use env_info::LastHashes;
|
||||
use verification::*;
|
||||
use block::*;
|
||||
@ -46,13 +46,6 @@ impl fmt::Display for BlockChainInfo {
|
||||
}
|
||||
}
|
||||
|
||||
/// Block queue status
|
||||
#[derive(Debug)]
|
||||
pub struct BlockQueueStatus {
|
||||
/// TODO [arkpar] Please document me
|
||||
pub full: bool,
|
||||
}
|
||||
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type TreeRoute = ::blockchain::TreeRoute;
|
||||
|
||||
@ -95,13 +88,13 @@ pub trait BlockChainClient : Sync + Send {
|
||||
fn block_receipts(&self, hash: &H256) -> Option<Bytes>;
|
||||
|
||||
/// Import a block into the blockchain.
|
||||
fn import_block(&mut self, bytes: Bytes) -> ImportResult;
|
||||
fn import_block(&self, bytes: Bytes) -> ImportResult;
|
||||
|
||||
/// Get block queue information.
|
||||
fn queue_status(&self) -> BlockQueueStatus;
|
||||
fn queue_info(&self) -> BlockQueueInfo;
|
||||
|
||||
/// Clear block queue and abort all import activity.
|
||||
fn clear_queue(&mut self);
|
||||
fn clear_queue(&self);
|
||||
|
||||
/// Get blockchain information.
|
||||
fn chain_info(&self) -> BlockChainInfo;
|
||||
@ -132,19 +125,21 @@ pub struct Client {
|
||||
chain: Arc<RwLock<BlockChain>>,
|
||||
engine: Arc<Box<Engine>>,
|
||||
state_db: JournalDB,
|
||||
queue: BlockQueue,
|
||||
report: ClientReport,
|
||||
block_queue: RwLock<BlockQueue>,
|
||||
report: RwLock<ClientReport>,
|
||||
uncommited_states: RwLock<HashMap<H256, JournalDB>>,
|
||||
import_lock: Mutex<()>
|
||||
}
|
||||
|
||||
const HISTORY: u64 = 1000;
|
||||
|
||||
impl Client {
|
||||
/// Create a new client with given spec and DB path.
|
||||
pub fn new(spec: Spec, path: &Path, message_channel: IoChannel<NetSyncMessage> ) -> Result<Client, Error> {
|
||||
pub fn new(spec: Spec, path: &Path, message_channel: IoChannel<NetSyncMessage> ) -> Result<Arc<Client>, Error> {
|
||||
let chain = Arc::new(RwLock::new(BlockChain::new(&spec.genesis_block(), path)));
|
||||
let mut opts = Options::new();
|
||||
opts.create_if_missing(true);
|
||||
opts.set_max_open_files(256);
|
||||
opts.create_if_missing(true);
|
||||
/*opts.set_use_fsync(false);
|
||||
opts.set_bytes_per_sync(8388608);
|
||||
opts.set_disable_data_sync(false);
|
||||
@ -164,37 +159,40 @@ impl Client {
|
||||
|
||||
let mut state_path = path.to_path_buf();
|
||||
state_path.push("state");
|
||||
let db = DB::open(&opts, state_path.to_str().unwrap()).unwrap();
|
||||
let mut state_db = JournalDB::new(db);
|
||||
let db = Arc::new(DB::open(&opts, state_path.to_str().unwrap()).unwrap());
|
||||
|
||||
let engine = Arc::new(try!(spec.to_engine()));
|
||||
if engine.spec().ensure_db_good(&mut state_db) {
|
||||
state_db.commit(0, &engine.spec().genesis_header().hash(), None).expect("Error commiting genesis state to state DB");
|
||||
{
|
||||
let mut state_db = JournalDB::new_with_arc(db.clone());
|
||||
if engine.spec().ensure_db_good(&mut state_db) {
|
||||
state_db.commit(0, &engine.spec().genesis_header().hash(), None).expect("Error commiting genesis state to state DB");
|
||||
}
|
||||
}
|
||||
let state_db = JournalDB::new_with_arc(db);
|
||||
|
||||
// chain.write().unwrap().ensure_good(&state_db);
|
||||
|
||||
Ok(Client {
|
||||
Ok(Arc::new(Client {
|
||||
chain: chain,
|
||||
engine: engine.clone(),
|
||||
state_db: state_db,
|
||||
queue: BlockQueue::new(engine, message_channel),
|
||||
report: Default::default(),
|
||||
})
|
||||
block_queue: RwLock::new(BlockQueue::new(engine, message_channel)),
|
||||
report: RwLock::new(Default::default()),
|
||||
uncommited_states: RwLock::new(HashMap::new()),
|
||||
import_lock: Mutex::new(()),
|
||||
}))
|
||||
}
|
||||
|
||||
/// This is triggered by a message coming from a block queue when the block is ready for insertion
|
||||
pub fn import_verified_blocks(&mut self) {
|
||||
|
||||
pub fn import_verified_blocks(&self, _io: &IoChannel<NetSyncMessage>) {
|
||||
let mut bad = HashSet::new();
|
||||
let blocks = self.queue.drain(128);
|
||||
let _import_lock = self.import_lock.lock();
|
||||
let blocks = self.block_queue.write().unwrap().drain(128);
|
||||
if blocks.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
for block in blocks {
|
||||
if bad.contains(&block.header.parent_hash) {
|
||||
self.queue.mark_as_bad(&block.header.hash());
|
||||
self.block_queue.write().unwrap().mark_as_bad(&block.header.hash());
|
||||
bad.insert(block.header.hash());
|
||||
continue;
|
||||
}
|
||||
@ -202,7 +200,7 @@ impl Client {
|
||||
let header = &block.header;
|
||||
if let Err(e) = verify_block_family(&header, &block.bytes, self.engine.deref().deref(), self.chain.read().unwrap().deref()) {
|
||||
warn!(target: "client", "Stage 3 block verification failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e);
|
||||
self.queue.mark_as_bad(&header.hash());
|
||||
self.block_queue.write().unwrap().mark_as_bad(&header.hash());
|
||||
bad.insert(block.header.hash());
|
||||
return;
|
||||
};
|
||||
@ -210,7 +208,7 @@ impl Client {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
warn!(target: "client", "Block import failed for #{} ({}): Parent not found ({}) ", header.number(), header.hash(), header.parent_hash);
|
||||
self.queue.mark_as_bad(&header.hash());
|
||||
self.block_queue.write().unwrap().mark_as_bad(&header.hash());
|
||||
bad.insert(block.header.hash());
|
||||
return;
|
||||
},
|
||||
@ -228,18 +226,19 @@ impl Client {
|
||||
}
|
||||
}
|
||||
|
||||
let result = match enact_verified(&block, self.engine.deref().deref(), self.state_db.clone(), &parent, &last_hashes) {
|
||||
let db = self.state_db.clone();
|
||||
let result = match enact_verified(&block, self.engine.deref().deref(), db, &parent, &last_hashes) {
|
||||
Ok(b) => b,
|
||||
Err(e) => {
|
||||
warn!(target: "client", "Block import failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e);
|
||||
bad.insert(block.header.hash());
|
||||
self.queue.mark_as_bad(&header.hash());
|
||||
self.block_queue.write().unwrap().mark_as_bad(&header.hash());
|
||||
return;
|
||||
}
|
||||
};
|
||||
if let Err(e) = verify_block_final(&header, result.block().header()) {
|
||||
warn!(target: "client", "Stage 4 block verification failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e);
|
||||
self.queue.mark_as_bad(&header.hash());
|
||||
self.block_queue.write().unwrap().mark_as_bad(&header.hash());
|
||||
return;
|
||||
}
|
||||
|
||||
@ -252,12 +251,16 @@ impl Client {
|
||||
return;
|
||||
}
|
||||
}
|
||||
self.report.accrue_block(&block);
|
||||
|
||||
self.report.write().unwrap().accrue_block(&block);
|
||||
trace!(target: "client", "Imported #{} ({})", header.number(), header.hash());
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear cached state overlay
|
||||
pub fn clear_state(&self, hash: &H256) {
|
||||
self.uncommited_states.write().unwrap().remove(hash);
|
||||
}
|
||||
|
||||
/// Get info on the cache.
|
||||
pub fn cache_info(&self) -> CacheSize {
|
||||
self.chain.read().unwrap().cache_size()
|
||||
@ -265,7 +268,7 @@ impl Client {
|
||||
|
||||
/// Get the report.
|
||||
pub fn report(&self) -> ClientReport {
|
||||
self.report.clone()
|
||||
self.report.read().unwrap().clone()
|
||||
}
|
||||
|
||||
/// Tick the client.
|
||||
@ -328,21 +331,20 @@ impl BlockChainClient for Client {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn import_block(&mut self, bytes: Bytes) -> ImportResult {
|
||||
fn import_block(&self, bytes: Bytes) -> ImportResult {
|
||||
let header = BlockView::new(&bytes).header();
|
||||
if self.chain.read().unwrap().is_known(&header.hash()) {
|
||||
return Err(ImportError::AlreadyInChain);
|
||||
}
|
||||
self.queue.import_block(bytes)
|
||||
self.block_queue.write().unwrap().import_block(bytes)
|
||||
}
|
||||
|
||||
fn queue_status(&self) -> BlockQueueStatus {
|
||||
BlockQueueStatus {
|
||||
full: false
|
||||
}
|
||||
fn queue_info(&self) -> BlockQueueInfo {
|
||||
self.block_queue.read().unwrap().queue_info()
|
||||
}
|
||||
|
||||
fn clear_queue(&mut self) {
|
||||
fn clear_queue(&self) {
|
||||
self.block_queue.write().unwrap().clear();
|
||||
}
|
||||
|
||||
fn chain_info(&self) -> BlockChainInfo {
|
||||
|
@ -25,8 +25,14 @@ pub struct EnvInfo {
|
||||
}
|
||||
|
||||
impl EnvInfo {
|
||||
/// TODO [debris] Please document me
|
||||
/// Create empty env_info initialized with zeros
|
||||
pub fn new() -> EnvInfo {
|
||||
EnvInfo::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for EnvInfo {
|
||||
fn default() -> Self {
|
||||
EnvInfo {
|
||||
number: 0,
|
||||
author: Address::new(),
|
||||
@ -53,11 +59,3 @@ impl FromJson for EnvInfo {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO: it should be the other way around.
|
||||
/// `new` should call `default`.
|
||||
impl Default for EnvInfo {
|
||||
fn default() -> Self {
|
||||
EnvInfo::new()
|
||||
}
|
||||
}
|
||||
|
@ -32,13 +32,13 @@ impl Ethash {
|
||||
}
|
||||
|
||||
fn u64_param(&self, name: &str) -> u64 {
|
||||
*self.u64_params.write().unwrap().entry(name.to_string()).or_insert_with(||
|
||||
self.spec().engine_params.get(name).map(|a| decode(&a)).unwrap_or(0u64))
|
||||
*self.u64_params.write().unwrap().entry(name.to_owned()).or_insert_with(||
|
||||
self.spec().engine_params.get(name).map_or(0u64, |a| decode(&a)))
|
||||
}
|
||||
|
||||
fn u256_param(&self, name: &str) -> U256 {
|
||||
*self.u256_params.write().unwrap().entry(name.to_string()).or_insert_with(||
|
||||
self.spec().engine_params.get(name).map(|a| decode(&a)).unwrap_or(x!(0)))
|
||||
*self.u256_params.write().unwrap().entry(name.to_owned()).or_insert_with(||
|
||||
self.spec().engine_params.get(name).map_or(x!(0), |a| decode(&a)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,7 +84,7 @@ impl Engine for Ethash {
|
||||
/// 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 reward = self.spec().engine_params.get("blockReward").map(|a| decode(&a)).unwrap_or(U256::from(0u64));
|
||||
let reward = self.spec().engine_params.get("blockReward").map_or(U256::from(0u64), |a| decode(&a));
|
||||
let fields = block.fields();
|
||||
|
||||
// Bestow block reward
|
||||
@ -153,6 +153,7 @@ impl Engine for Ethash {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(wrong_self_convention)] // to_ethash should take self
|
||||
impl Ethash {
|
||||
fn calculate_difficuty(&self, header: &Header, parent: &Header) -> U256 {
|
||||
const EXP_DIFF_PERIOD: u64 = 100000;
|
||||
|
@ -68,10 +68,11 @@ impl Factory {
|
||||
fn jit() -> Box<Evm> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
}
|
||||
impl Default for Factory {
|
||||
/// Returns jitvm factory
|
||||
#[cfg(feature = "jit")]
|
||||
pub fn default() -> Factory {
|
||||
fn default() -> Factory {
|
||||
Factory {
|
||||
evm: VMType::Jit
|
||||
}
|
||||
@ -79,7 +80,7 @@ impl Factory {
|
||||
|
||||
/// Returns native rust evm factory
|
||||
#[cfg(not(feature = "jit"))]
|
||||
pub fn default() -> Factory {
|
||||
fn default() -> Factory {
|
||||
Factory {
|
||||
evm: VMType::Interpreter
|
||||
}
|
||||
|
@ -7,19 +7,19 @@ use super::instructions::Instruction;
|
||||
use std::marker::Copy;
|
||||
use evm::{MessageCallResult, ContractCreateResult};
|
||||
|
||||
#[cfg(not(feature = "evm_debug"))]
|
||||
#[cfg(not(feature = "evm-debug"))]
|
||||
macro_rules! evm_debug {
|
||||
($x: expr) => {}
|
||||
}
|
||||
|
||||
#[cfg(feature = "evm_debug")]
|
||||
#[cfg(feature = "evm-debug")]
|
||||
macro_rules! evm_debug {
|
||||
($x: expr) => {
|
||||
$x
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "evm_debug")]
|
||||
#[cfg(feature = "evm-debug")]
|
||||
fn color(instruction: Instruction, name: &'static str) -> String {
|
||||
let c = instruction as usize % 6;
|
||||
let colors = [31, 34, 33, 32, 35, 36];
|
||||
@ -72,7 +72,7 @@ impl<S : Copy> VecStack<S> {
|
||||
|
||||
impl<S : fmt::Display> Stack<S> for VecStack<S> {
|
||||
fn peek(&self, no_from_top: usize) -> &S {
|
||||
return &self.stack[self.stack.len() - no_from_top - 1];
|
||||
&self.stack[self.stack.len() - no_from_top - 1]
|
||||
}
|
||||
|
||||
fn swap_with_top(&mut self, no_from_top: usize) {
|
||||
@ -157,7 +157,7 @@ impl Memory for Vec<u8> {
|
||||
}
|
||||
|
||||
fn size(&self) -> usize {
|
||||
return self.len()
|
||||
self.len()
|
||||
}
|
||||
|
||||
fn read_slice(&self, init_off_u: U256, init_size_u: U256) -> &[u8] {
|
||||
@ -228,6 +228,7 @@ struct CodeReader<'a> {
|
||||
code: &'a Bytes
|
||||
}
|
||||
|
||||
#[allow(len_without_is_empty)]
|
||||
impl<'a> CodeReader<'a> {
|
||||
/// Get `no_of_bytes` from code and convert to U256. Move PC
|
||||
fn read(&mut self, no_of_bytes: usize) -> U256 {
|
||||
@ -330,6 +331,7 @@ impl evm::Evm for Interpreter {
|
||||
}
|
||||
|
||||
impl Interpreter {
|
||||
#[allow(cyclomatic_complexity)]
|
||||
fn get_gas_cost_mem(&self,
|
||||
ext: &evm::Ext,
|
||||
instruction: Instruction,
|
||||
@ -750,7 +752,7 @@ impl Interpreter {
|
||||
let big_id = stack.pop_back();
|
||||
let id = big_id.low_u64() as usize;
|
||||
let max = id.wrapping_add(32);
|
||||
let data = params.data.clone().unwrap_or(vec![]);
|
||||
let data = params.data.clone().unwrap_or_else(|| vec![]);
|
||||
let bound = cmp::min(data.len(), max);
|
||||
if id < bound && big_id < U256::from(data.len()) {
|
||||
let mut v = data[id..bound].to_vec();
|
||||
@ -761,7 +763,7 @@ impl Interpreter {
|
||||
}
|
||||
},
|
||||
instructions::CALLDATASIZE => {
|
||||
stack.push(U256::from(params.data.clone().unwrap_or(vec![]).len()));
|
||||
stack.push(U256::from(params.data.clone().map_or(0, |l| l.len())));
|
||||
},
|
||||
instructions::CODESIZE => {
|
||||
stack.push(U256::from(code.len()));
|
||||
@ -772,10 +774,10 @@ impl Interpreter {
|
||||
stack.push(U256::from(len));
|
||||
},
|
||||
instructions::CALLDATACOPY => {
|
||||
self.copy_data_to_memory(mem, stack, ¶ms.data.clone().unwrap_or(vec![]));
|
||||
self.copy_data_to_memory(mem, stack, ¶ms.data.clone().unwrap_or_else(|| vec![]));
|
||||
},
|
||||
instructions::CODECOPY => {
|
||||
self.copy_data_to_memory(mem, stack, ¶ms.code.clone().unwrap_or(vec![]));
|
||||
self.copy_data_to_memory(mem, stack, ¶ms.code.clone().unwrap_or_else(|| vec![]));
|
||||
},
|
||||
instructions::EXTCODECOPY => {
|
||||
let address = u256_to_address(&stack.pop_back());
|
||||
@ -815,7 +817,7 @@ impl Interpreter {
|
||||
fn copy_data_to_memory(&self,
|
||||
mem: &mut Memory,
|
||||
stack: &mut Stack<U256>,
|
||||
data: &Bytes) {
|
||||
data: &[u8]) {
|
||||
let offset = stack.pop_back();
|
||||
let index = stack.pop_back();
|
||||
let size = stack.pop_back();
|
||||
@ -1085,7 +1087,7 @@ impl Interpreter {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn find_jump_destinations(&self, code: &Bytes) -> HashSet<CodePosition> {
|
||||
fn find_jump_destinations(&self, code: &[u8]) -> HashSet<CodePosition> {
|
||||
let mut jump_dests = HashSet::new();
|
||||
let mut position = 0;
|
||||
|
||||
@ -1100,7 +1102,7 @@ impl Interpreter {
|
||||
position += 1;
|
||||
}
|
||||
|
||||
return jump_dests;
|
||||
jump_dests
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,7 @@ struct FakeExt {
|
||||
logs: Vec<FakeLogEntry>,
|
||||
_suicides: HashSet<Address>,
|
||||
info: EnvInfo,
|
||||
_schedule: Schedule
|
||||
schedule: Schedule
|
||||
}
|
||||
|
||||
impl FakeExt {
|
||||
@ -97,7 +97,7 @@ impl Ext for FakeExt {
|
||||
}
|
||||
|
||||
fn schedule(&self) -> &Schedule {
|
||||
&self._schedule
|
||||
&self.schedule
|
||||
}
|
||||
|
||||
fn env_info(&self) -> &EnvInfo {
|
||||
@ -130,7 +130,7 @@ fn test_stack_underflow() {
|
||||
};
|
||||
|
||||
match err {
|
||||
evm::Error::StackUnderflow {instruction: _, wanted, on_stack} => {
|
||||
evm::Error::StackUnderflow {wanted, on_stack, ..} => {
|
||||
assert_eq!(wanted, 2);
|
||||
assert_eq!(on_stack, 0);
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ impl<'a> Executive<'a> {
|
||||
}
|
||||
|
||||
/// Creates `Externalities` from `Executive`.
|
||||
pub fn to_externalities<'_>(&'_ mut self, origin_info: OriginInfo, substate: &'_ mut Substate, output: OutputPolicy<'_>) -> Externalities {
|
||||
pub fn as_externalities<'_>(&'_ mut self, origin_info: OriginInfo, substate: &'_ mut Substate, output: OutputPolicy<'_>) -> Externalities {
|
||||
Externalities::new(self.state, self.info, self.engine, self.depth, origin_info, substate, output)
|
||||
}
|
||||
|
||||
@ -123,8 +123,8 @@ impl<'a> Executive<'a> {
|
||||
|
||||
let mut substate = Substate::new();
|
||||
|
||||
let res = match t.action() {
|
||||
&Action::Create => {
|
||||
let res = match *t.action() {
|
||||
Action::Create => {
|
||||
let new_address = contract_address(&sender, &nonce);
|
||||
let params = ActionParams {
|
||||
code_address: new_address.clone(),
|
||||
@ -139,7 +139,7 @@ impl<'a> Executive<'a> {
|
||||
};
|
||||
self.create(params, &mut substate)
|
||||
},
|
||||
&Action::Call(ref address) => {
|
||||
Action::Call(ref address) => {
|
||||
let params = ActionParams {
|
||||
code_address: address.clone(),
|
||||
address: address.clone(),
|
||||
@ -180,7 +180,7 @@ impl<'a> Executive<'a> {
|
||||
// if destination is builtin, try to execute it
|
||||
|
||||
let default = [];
|
||||
let data = if let &Some(ref d) = ¶ms.data { d as &[u8] } else { &default as &[u8] };
|
||||
let data = if let Some(ref d) = params.data { d as &[u8] } else { &default as &[u8] };
|
||||
|
||||
let cost = self.engine.cost_of_builtin(¶ms.code_address, data);
|
||||
match cost <= params.gas {
|
||||
@ -201,7 +201,7 @@ impl<'a> Executive<'a> {
|
||||
let mut unconfirmed_substate = Substate::new();
|
||||
|
||||
let res = {
|
||||
let mut ext = self.to_externalities(OriginInfo::from(¶ms), &mut unconfirmed_substate, OutputPolicy::Return(output));
|
||||
let mut ext = self.as_externalities(OriginInfo::from(¶ms), &mut unconfirmed_substate, OutputPolicy::Return(output));
|
||||
self.engine.vm_factory().create().exec(params, &mut ext)
|
||||
};
|
||||
|
||||
@ -235,7 +235,7 @@ impl<'a> Executive<'a> {
|
||||
}
|
||||
|
||||
let res = {
|
||||
let mut ext = self.to_externalities(OriginInfo::from(¶ms), &mut unconfirmed_substate, OutputPolicy::InitContract);
|
||||
let mut ext = self.as_externalities(OriginInfo::from(¶ms), &mut unconfirmed_substate, OutputPolicy::InitContract);
|
||||
self.engine.vm_factory().create().exec(params, &mut ext)
|
||||
};
|
||||
self.enact_result(&res, substate, unconfirmed_substate, backup);
|
||||
@ -253,7 +253,7 @@ impl<'a> Executive<'a> {
|
||||
let refunds_bound = sstore_refunds + suicide_refunds;
|
||||
|
||||
// real ammount to refund
|
||||
let gas_left_prerefund = match &result { &Ok(x) => x, _ => x!(0) };
|
||||
let gas_left_prerefund = match result { Ok(x) => x, _ => x!(0) };
|
||||
let refunded = cmp::min(refunds_bound, (t.gas - gas_left_prerefund) / U256::from(2));
|
||||
let gas_left = gas_left_prerefund + refunded;
|
||||
|
||||
@ -270,7 +270,7 @@ impl<'a> Executive<'a> {
|
||||
self.state.add_balance(&self.info.author, &fees_value);
|
||||
|
||||
// perform suicides
|
||||
for address in substate.suicides.iter() {
|
||||
for address in &substate.suicides {
|
||||
trace!("Killing {}", address);
|
||||
self.state.kill_account(address);
|
||||
}
|
||||
@ -278,11 +278,7 @@ impl<'a> Executive<'a> {
|
||||
match result {
|
||||
Err(evm::Error::Internal) => Err(ExecutionError::Internal),
|
||||
// TODO [ToDr] BadJumpDestination @debris - how to handle that?
|
||||
Err(evm::Error::OutOfGas)
|
||||
| Err(evm::Error::BadJumpDestination { destination: _ })
|
||||
| Err(evm::Error::BadInstruction { instruction: _ })
|
||||
| Err(evm::Error::StackUnderflow {instruction: _, wanted: _, on_stack: _})
|
||||
| Err(evm::Error::OutOfStack {instruction: _, wanted: _, limit: _}) => {
|
||||
Err(_) => {
|
||||
Ok(Executed {
|
||||
gas: t.gas,
|
||||
gas_used: t.gas,
|
||||
@ -307,15 +303,15 @@ impl<'a> Executive<'a> {
|
||||
|
||||
fn enact_result(&mut self, result: &evm::Result, substate: &mut Substate, un_substate: Substate, backup: State) {
|
||||
// TODO: handle other evm::Errors same as OutOfGas once they are implemented
|
||||
match result {
|
||||
&Err(evm::Error::OutOfGas)
|
||||
| &Err(evm::Error::BadJumpDestination { destination: _ })
|
||||
| &Err(evm::Error::BadInstruction { instruction: _ })
|
||||
| &Err(evm::Error::StackUnderflow {instruction: _, wanted: _, on_stack: _})
|
||||
| &Err(evm::Error::OutOfStack {instruction: _, wanted: _, limit: _}) => {
|
||||
match *result {
|
||||
Err(evm::Error::OutOfGas)
|
||||
| Err(evm::Error::BadJumpDestination {..})
|
||||
| Err(evm::Error::BadInstruction {.. })
|
||||
| Err(evm::Error::StackUnderflow {..})
|
||||
| Err(evm::Error::OutOfStack {..}) => {
|
||||
self.state.revert(backup);
|
||||
},
|
||||
&Ok(_) | &Err(evm::Error::Internal) => substate.accrue(un_substate)
|
||||
Ok(_) | Err(evm::Error::Internal) => substate.accrue(un_substate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -191,9 +191,10 @@ impl<'a> Ext for Externalities<'a> {
|
||||
}
|
||||
|
||||
fn extcode(&self, address: &Address) -> Bytes {
|
||||
self.state.code(address).unwrap_or(vec![])
|
||||
self.state.code(address).unwrap_or_else(|| vec![])
|
||||
}
|
||||
|
||||
#[allow(match_ref_pats)]
|
||||
fn ret(&mut self, gas: &U256, data: &[u8]) -> Result<U256, evm::Error> {
|
||||
match &mut self.output {
|
||||
&mut OutputPolicy::Return(BytesRef::Fixed(ref mut slice)) => unsafe {
|
||||
|
@ -2,7 +2,7 @@ use util::*;
|
||||
use basic_types::*;
|
||||
use time::now_utc;
|
||||
|
||||
/// TODO [Gav Wood] Please document me
|
||||
/// Type for Block number
|
||||
pub type BlockNumber = u64;
|
||||
|
||||
/// A block header.
|
||||
@ -171,9 +171,10 @@ impl Header {
|
||||
s.append(&self.gas_used);
|
||||
s.append(&self.timestamp);
|
||||
s.append(&self.extra_data);
|
||||
match with_seal {
|
||||
Seal::With => for b in self.seal.iter() { s.append_raw(&b, 1); },
|
||||
_ => {}
|
||||
if let Seal::With = with_seal {
|
||||
for b in &self.seal {
|
||||
s.append_raw(&b, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -236,7 +237,7 @@ impl Encodable for Header {
|
||||
self.timestamp.encode(e);
|
||||
self.extra_data.encode(e);
|
||||
|
||||
for b in self.seal.iter() {
|
||||
for b in &self.seal {
|
||||
e.emit_raw(&b);
|
||||
}
|
||||
})
|
||||
|
@ -1,8 +1,11 @@
|
||||
#![warn(missing_docs)]
|
||||
#![feature(cell_extras)]
|
||||
#![feature(augmented_assignments)]
|
||||
//#![feature(plugin)]
|
||||
#![feature(plugin)]
|
||||
//#![plugin(interpolate_idents)]
|
||||
#![plugin(clippy)]
|
||||
#![allow(needless_range_loop, match_bool)]
|
||||
|
||||
//! Ethcore's ethereum implementation
|
||||
//!
|
||||
//! ### Rust version
|
||||
@ -73,7 +76,6 @@
|
||||
//! sudo make install
|
||||
//! sudo ldconfig
|
||||
//! ```
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate rustc_serialize;
|
||||
@ -149,6 +151,5 @@ pub mod sync;
|
||||
pub mod block;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub mod verification;
|
||||
/// TODO [debris] Please document me
|
||||
pub mod queue;
|
||||
pub mod block_queue;
|
||||
pub mod ethereum;
|
||||
|
@ -11,7 +11,7 @@ pub struct NullEngine {
|
||||
}
|
||||
|
||||
impl NullEngine {
|
||||
/// TODO [Tomusdrw] Please document me
|
||||
/// Returns new instance of NullEngine with default VM Factory
|
||||
pub fn new_boxed(spec: Spec) -> Box<Engine> {
|
||||
Box::new(NullEngine{
|
||||
spec: spec,
|
||||
|
@ -26,10 +26,10 @@ impl FromJson for PodState {
|
||||
let code = acc.find("code").map(&Bytes::from_json);
|
||||
if balance.is_some() || nonce.is_some() || storage.is_some() || code.is_some() {
|
||||
state.insert(address_from_hex(address), PodAccount{
|
||||
balance: balance.unwrap_or(U256::zero()),
|
||||
nonce: nonce.unwrap_or(U256::zero()),
|
||||
storage: storage.unwrap_or(BTreeMap::new()),
|
||||
code: code.unwrap_or(Vec::new())
|
||||
balance: balance.unwrap_or_else(U256::zero),
|
||||
nonce: nonce.unwrap_or_else(U256::zero),
|
||||
storage: storage.unwrap_or_else(BTreeMap::new),
|
||||
code: code.unwrap_or_else(Vec::new)
|
||||
});
|
||||
}
|
||||
state
|
||||
|
@ -36,7 +36,7 @@ impl RlpStandard for Receipt {
|
||||
// TODO: make work:
|
||||
//s.append(&self.logs);
|
||||
s.append_list(self.logs.len());
|
||||
for l in self.logs.iter() {
|
||||
for l in &self.logs {
|
||||
l.rlp_append(s);
|
||||
}
|
||||
}
|
||||
|
@ -5,10 +5,23 @@ use error::*;
|
||||
use std::env;
|
||||
use client::Client;
|
||||
|
||||
/// Message type for external and internal events
|
||||
#[derive(Clone)]
|
||||
pub enum SyncMessage {
|
||||
/// New block has been imported into the blockchain
|
||||
NewChainBlock(Bytes), //TODO: use Cow
|
||||
/// A block is ready
|
||||
BlockVerified,
|
||||
}
|
||||
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type NetSyncMessage = NetworkIoMessage<SyncMessage>;
|
||||
|
||||
/// Client service setup. Creates and registers client and network services with the IO subsystem.
|
||||
pub struct ClientService {
|
||||
net_service: NetworkService<SyncMessage>,
|
||||
client: Arc<RwLock<Client>>,
|
||||
client: Arc<Client>,
|
||||
sync: Arc<EthSync>,
|
||||
}
|
||||
|
||||
impl ClientService {
|
||||
@ -20,9 +33,9 @@ impl ClientService {
|
||||
let mut dir = env::home_dir().unwrap();
|
||||
dir.push(".parity");
|
||||
dir.push(H64::from(spec.genesis_header().hash()).hex());
|
||||
let client = Arc::new(RwLock::new(try!(Client::new(spec, &dir, net_service.io().channel()))));
|
||||
EthSync::register(&mut net_service, client.clone());
|
||||
let client_io = Box::new(ClientIoHandler {
|
||||
let client = try!(Client::new(spec, &dir, net_service.io().channel()));
|
||||
let sync = EthSync::register(&mut net_service, client.clone());
|
||||
let client_io = Arc::new(ClientIoHandler {
|
||||
client: client.clone()
|
||||
});
|
||||
try!(net_service.io().register_handler(client_io));
|
||||
@ -30,6 +43,7 @@ impl ClientService {
|
||||
Ok(ClientService {
|
||||
net_service: net_service,
|
||||
client: client,
|
||||
sync: sync,
|
||||
})
|
||||
}
|
||||
|
||||
@ -39,34 +53,47 @@ impl ClientService {
|
||||
}
|
||||
|
||||
/// TODO [arkpar] Please document me
|
||||
pub fn client(&self) -> Arc<RwLock<Client>> {
|
||||
pub fn client(&self) -> Arc<Client> {
|
||||
self.client.clone()
|
||||
|
||||
}
|
||||
|
||||
/// Get shared sync handler
|
||||
pub fn sync(&self) -> Arc<EthSync> {
|
||||
self.sync.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// IO interface for the Client handler
|
||||
struct ClientIoHandler {
|
||||
client: Arc<RwLock<Client>>
|
||||
client: Arc<Client>
|
||||
}
|
||||
|
||||
const CLIENT_TICK_TIMER: TimerToken = 0;
|
||||
const CLIENT_TICK_MS: u64 = 5000;
|
||||
|
||||
impl IoHandler<NetSyncMessage> for ClientIoHandler {
|
||||
fn initialize<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>) {
|
||||
fn initialize(&self, io: &IoContext<NetSyncMessage>) {
|
||||
io.register_timer(CLIENT_TICK_TIMER, CLIENT_TICK_MS).expect("Error registering client timer");
|
||||
}
|
||||
|
||||
fn message<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>, net_message: &'s mut NetSyncMessage) {
|
||||
match net_message {
|
||||
&mut UserMessage(ref mut message) => {
|
||||
match message {
|
||||
&mut SyncMessage::BlockVerified => {
|
||||
self.client.write().unwrap().import_verified_blocks();
|
||||
},
|
||||
_ => {}, // ignore other messages
|
||||
}
|
||||
|
||||
}
|
||||
_ => {}, // ignore other messages
|
||||
fn timeout(&self, _io: &IoContext<NetSyncMessage>, timer: TimerToken) {
|
||||
if timer == CLIENT_TICK_TIMER {
|
||||
self.client.tick();
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(match_ref_pats)]
|
||||
#[allow(single_match)]
|
||||
fn message(&self, io: &IoContext<NetSyncMessage>, net_message: &NetSyncMessage) {
|
||||
if let &UserMessage(ref message) = net_message {
|
||||
match message {
|
||||
&SyncMessage::BlockVerified => {
|
||||
self.client.import_verified_blocks(&io.channel());
|
||||
},
|
||||
_ => {}, // ignore other messages
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
27
src/spec.rs
27
src/spec.rs
@ -10,7 +10,7 @@ pub fn gzip64res_to_json(source: &[u8]) -> Json {
|
||||
let data = source.from_base64().expect("Genesis block is malformed!");
|
||||
let data_ref: &[u8] = &data;
|
||||
let mut decoder = GzDecoder::new(data_ref).expect("Gzip is invalid");
|
||||
let mut s: String = "".to_string();
|
||||
let mut s: String = "".to_owned();
|
||||
decoder.read_to_string(&mut s).expect("Gzip is invalid");
|
||||
Json::from_str(&s).expect("Json is invalid")
|
||||
}
|
||||
@ -18,14 +18,14 @@ pub fn gzip64res_to_json(source: &[u8]) -> Json {
|
||||
/// Convert JSON value to equivlaent RLP representation.
|
||||
// TODO: handle container types.
|
||||
fn json_to_rlp(json: &Json) -> Bytes {
|
||||
match json {
|
||||
&Json::Boolean(o) => encode(&(if o {1u64} else {0})),
|
||||
&Json::I64(o) => encode(&(o as u64)),
|
||||
&Json::U64(o) => encode(&o),
|
||||
&Json::String(ref s) if s.len() >= 2 && &s[0..2] == "0x" && U256::from_str(&s[2..]).is_ok() => {
|
||||
match *json {
|
||||
Json::Boolean(o) => encode(&(if o {1u64} else {0})),
|
||||
Json::I64(o) => encode(&(o as u64)),
|
||||
Json::U64(o) => encode(&o),
|
||||
Json::String(ref s) if s.len() >= 2 && &s[0..2] == "0x" && U256::from_str(&s[2..]).is_ok() => {
|
||||
encode(&U256::from_str(&s[2..]).unwrap())
|
||||
},
|
||||
&Json::String(ref s) => {
|
||||
Json::String(ref s) => {
|
||||
encode(s)
|
||||
},
|
||||
_ => panic!()
|
||||
@ -108,6 +108,7 @@ pub struct Spec {
|
||||
state_root_memo: RwLock<Option<H256>>,
|
||||
}
|
||||
|
||||
#[allow(wrong_self_convention)] // because to_engine(self) should be to_engine(&self)
|
||||
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.
|
||||
@ -185,13 +186,13 @@ impl FromJson for Spec {
|
||||
builtins.insert(addr.clone(), builtin);
|
||||
}
|
||||
}
|
||||
let balance = acc.find("balance").and_then(|x| match x { &Json::String(ref b) => U256::from_dec_str(b).ok(), _ => None });
|
||||
let nonce = acc.find("nonce").and_then(|x| match x { &Json::String(ref b) => U256::from_dec_str(b).ok(), _ => None });
|
||||
let balance = acc.find("balance").and_then(|x| match *x { Json::String(ref b) => U256::from_dec_str(b).ok(), _ => None });
|
||||
let nonce = acc.find("nonce").and_then(|x| match *x { Json::String(ref b) => U256::from_dec_str(b).ok(), _ => None });
|
||||
// let balance = if let Some(&Json::String(ref b)) = acc.find("balance") {U256::from_dec_str(b).unwrap_or(U256::from(0))} else {U256::from(0)};
|
||||
// let nonce = if let Some(&Json::String(ref n)) = acc.find("nonce") {U256::from_dec_str(n).unwrap_or(U256::from(0))} else {U256::from(0)};
|
||||
// TODO: handle code & data if they exist.
|
||||
if balance.is_some() || nonce.is_some() {
|
||||
state.insert(addr, GenesisAccount { balance: balance.unwrap_or(U256::from(0)), nonce: nonce.unwrap_or(U256::from(0)) });
|
||||
state.insert(addr, GenesisAccount { balance: balance.unwrap_or_else(U256::zero), nonce: nonce.unwrap_or_else(U256::zero) });
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -215,8 +216,8 @@ impl FromJson for Spec {
|
||||
|
||||
|
||||
Spec {
|
||||
name: json.find("name").map(|j| j.as_string().unwrap()).unwrap_or("unknown").to_string(),
|
||||
engine_name: json["engineName"].as_string().unwrap().to_string(),
|
||||
name: json.find("name").map_or("unknown", |j| j.as_string().unwrap()).to_owned(),
|
||||
engine_name: json["engineName"].as_string().unwrap().to_owned(),
|
||||
engine_params: json_to_rlp_map(&json["params"]),
|
||||
builtins: builtins,
|
||||
parent_hash: H256::from_str(&genesis["parentHash"].as_string().unwrap()[2..]).unwrap(),
|
||||
@ -242,7 +243,7 @@ impl Spec {
|
||||
let mut root = H256::new();
|
||||
{
|
||||
let mut t = SecTrieDBMut::new(db, &mut root);
|
||||
for (address, account) in self.genesis_state.iter() {
|
||||
for (address, account) in &self.genesis_state {
|
||||
t.insert(address.as_slice(), &account.rlp());
|
||||
}
|
||||
}
|
||||
|
19
src/state.rs
19
src/state.rs
@ -88,22 +88,22 @@ impl State {
|
||||
|
||||
/// Get the balance of account `a`.
|
||||
pub fn balance(&self, a: &Address) -> U256 {
|
||||
self.get(a, false).as_ref().map(|account| account.balance().clone()).unwrap_or(U256::from(0u8))
|
||||
self.get(a, false).as_ref().map_or(U256::zero(), |account| account.balance().clone())
|
||||
}
|
||||
|
||||
/// Get the nonce of account `a`.
|
||||
pub fn nonce(&self, a: &Address) -> U256 {
|
||||
self.get(a, false).as_ref().map(|account| account.nonce().clone()).unwrap_or(U256::from(0u8))
|
||||
self.get(a, false).as_ref().map_or(U256::zero(), |account| account.nonce().clone())
|
||||
}
|
||||
|
||||
/// Mutate storage of account `a` so that it is `value` for `key`.
|
||||
pub fn storage_at(&self, a: &Address, key: &H256) -> H256 {
|
||||
self.get(a, false).as_ref().map(|a|a.storage_at(&self.db, key)).unwrap_or(H256::new())
|
||||
self.get(a, false).as_ref().map_or(H256::new(), |a|a.storage_at(&self.db, key))
|
||||
}
|
||||
|
||||
/// Mutate storage of account `a` so that it is `value` for `key`.
|
||||
pub fn code(&self, a: &Address) -> Option<Bytes> {
|
||||
self.get(a, true).as_ref().map(|a|a.code().map(|x|x.to_vec())).unwrap_or(None)
|
||||
self.get(a, true).as_ref().map_or(None, |a|a.code().map(|x|x.to_vec()))
|
||||
}
|
||||
|
||||
/// Add `incr` to the balance of account `a`.
|
||||
@ -170,6 +170,7 @@ impl State {
|
||||
|
||||
/// Commit accounts to SecTrieDBMut. This is similar to cpp-ethereum's dev::eth::commit.
|
||||
/// `accounts` is mutable because we may need to commit the code or storage and record that.
|
||||
#[allow(match_ref_pats)]
|
||||
pub fn commit_into(db: &mut HashDB, root: &mut H256, accounts: &mut HashMap<Address, Option<Account>>) {
|
||||
// first, commit the sub trees.
|
||||
// TODO: is this necessary or can we dispense with the `ref mut a` for just `a`?
|
||||
@ -186,9 +187,9 @@ impl State {
|
||||
{
|
||||
let mut trie = SecTrieDBMut::from_existing(db, root);
|
||||
for (address, ref a) in accounts.iter() {
|
||||
match a {
|
||||
&&Some(ref account) => trie.insert(address, &account.rlp()),
|
||||
&&None => trie.remove(address),
|
||||
match **a {
|
||||
Some(ref account) => trie.insert(address, &account.rlp()),
|
||||
None => trie.remove(address),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -210,7 +211,7 @@ impl State {
|
||||
pub fn to_hashmap_pod(&self) -> HashMap<Address, PodAccount> {
|
||||
// TODO: handle database rather than just the cache.
|
||||
self.cache.borrow().iter().fold(HashMap::new(), |mut m, (add, opt)| {
|
||||
if let &Some(ref acc) = opt {
|
||||
if let Some(ref acc) = *opt {
|
||||
m.insert(add.clone(), PodAccount::from_account(acc));
|
||||
}
|
||||
m
|
||||
@ -221,7 +222,7 @@ impl State {
|
||||
pub fn to_pod(&self) -> PodState {
|
||||
// TODO: handle database rather than just the cache.
|
||||
PodState::new(self.cache.borrow().iter().fold(BTreeMap::new(), |mut m, (add, opt)| {
|
||||
if let &Some(ref acc) = opt {
|
||||
if let Some(ref acc) = *opt {
|
||||
m.insert(add.clone(), PodAccount::from_account(acc));
|
||||
}
|
||||
m
|
||||
|
@ -15,7 +15,7 @@ impl StateDiff {
|
||||
|
||||
impl fmt::Display for StateDiff {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
for (add, acc) in self.0.iter() {
|
||||
for (add, acc) in &self.0 {
|
||||
try!(write!(f, "{} {}: {}", acc.existance(), add, acc));
|
||||
}
|
||||
Ok(())
|
||||
|
@ -107,6 +107,10 @@ pub struct SyncStatus {
|
||||
pub blocks_total: usize,
|
||||
/// Number of blocks downloaded so far.
|
||||
pub blocks_received: usize,
|
||||
/// Total number of connected peers
|
||||
pub num_peers: usize,
|
||||
/// Total number of active peers
|
||||
pub num_active_peers: usize,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
@ -195,8 +199,10 @@ impl ChainSync {
|
||||
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,
|
||||
blocks_received: (self.last_imported_block - self.starting_block) as usize,
|
||||
blocks_total: (self.highest_block - self.starting_block) as usize,
|
||||
num_peers: self.peers.len(),
|
||||
num_active_peers: self.peers.values().filter(|p| p.asking != PeerAsking::Nothing).count(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -212,7 +218,7 @@ impl ChainSync {
|
||||
self.downloading_bodies.clear();
|
||||
self.headers.clear();
|
||||
self.bodies.clear();
|
||||
for (_, ref mut p) in self.peers.iter_mut() {
|
||||
for (_, ref mut p) in &mut self.peers {
|
||||
p.asking_blocks.clear();
|
||||
}
|
||||
self.header_ids.clear();
|
||||
@ -268,6 +274,7 @@ impl ChainSync {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(cyclomatic_complexity)]
|
||||
/// 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);
|
||||
@ -375,7 +382,7 @@ impl ChainSync {
|
||||
transactions_root: tx_root,
|
||||
uncles: uncles
|
||||
};
|
||||
match self.header_ids.get(&header_id).map(|n| *n) {
|
||||
match self.header_ids.get(&header_id).cloned() {
|
||||
Some(n) => {
|
||||
self.header_ids.remove(&header_id);
|
||||
self.bodies.insert_item(n, body.as_raw().to_vec());
|
||||
@ -424,6 +431,10 @@ impl ChainSync {
|
||||
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);
|
||||
{
|
||||
let peer = self.peers.get_mut(&peer_id).expect("ChainSync: unknown peer");
|
||||
peer.latest = header_view.sha3();
|
||||
}
|
||||
self.sync_peer(io, peer_id, true);
|
||||
}
|
||||
}
|
||||
@ -540,7 +551,7 @@ impl ChainSync {
|
||||
fn request_blocks(&mut self, io: &mut SyncIo, peer_id: PeerId) {
|
||||
self.clear_peer_download(peer_id);
|
||||
|
||||
if io.chain().queue_status().full {
|
||||
if io.chain().queue_info().full {
|
||||
self.pause_sync();
|
||||
return;
|
||||
}
|
||||
@ -699,16 +710,13 @@ impl ChainSync {
|
||||
/// 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 => {}
|
||||
if let Some(ref header_data) = self.headers.find_item(&n) {
|
||||
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);
|
||||
}
|
||||
self.downloading_bodies.remove(&n);
|
||||
self.downloading_headers.remove(&n);
|
||||
@ -796,12 +804,9 @@ impl ChainSync {
|
||||
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(_) => ()
|
||||
if let Err(e) = io.send(peer_id, STATUS_PACKET, packet.out()) {
|
||||
warn!(target:"sync", "Error sending status request: {:?}", e);
|
||||
io.disable_peer(peer_id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -837,12 +842,9 @@ impl ChainSync {
|
||||
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 let Some(mut hdr) = io.chain().block_header_at(number) {
|
||||
data.append(&mut hdr);
|
||||
count += 1;
|
||||
}
|
||||
if reverse {
|
||||
if number <= inc {
|
||||
@ -874,12 +876,9 @@ impl ChainSync {
|
||||
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 => {}
|
||||
if let Some(mut hdr) = io.chain().block_body(&try!(r.val_at::<H256>(i))) {
|
||||
data.append(&mut hdr);
|
||||
added += 1;
|
||||
}
|
||||
}
|
||||
let mut rlp = RlpStream::new_list(added);
|
||||
@ -901,12 +900,9 @@ impl ChainSync {
|
||||
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 => {}
|
||||
if let Some(mut hdr) = io.chain().state_data(&try!(r.val_at::<H256>(i))) {
|
||||
data.append(&mut hdr);
|
||||
added += 1;
|
||||
}
|
||||
}
|
||||
let mut rlp = RlpStream::new_list(added);
|
||||
@ -927,12 +923,9 @@ impl ChainSync {
|
||||
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 => {}
|
||||
if let Some(mut hdr) = io.chain().block_receipts(&try!(r.val_at::<H256>(i))) {
|
||||
data.append(&mut hdr);
|
||||
added += 1;
|
||||
}
|
||||
}
|
||||
let mut rlp = RlpStream::new_list(added);
|
||||
@ -967,7 +960,7 @@ impl ChainSync {
|
||||
}
|
||||
|
||||
/// Maintain other peers. Send out any new blocks and transactions
|
||||
pub fn maintain_sync(&mut self, _io: &mut SyncIo) {
|
||||
pub fn _maintain_sync(&mut self, _io: &mut SyncIo) {
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use client::BlockChainClient;
|
||||
use util::{NetworkContext, PeerId, PacketId,};
|
||||
use util::error::UtilError;
|
||||
use sync::SyncMessage;
|
||||
use service::SyncMessage;
|
||||
|
||||
/// IO interface for the syning handler.
|
||||
/// Provides peer connection management and an interface to the blockchain client.
|
||||
@ -14,7 +14,7 @@ pub trait SyncIo {
|
||||
/// 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;
|
||||
fn chain(&self) -> &BlockChainClient;
|
||||
/// Returns peer client identifier string
|
||||
fn peer_info(&self, peer_id: PeerId) -> String {
|
||||
peer_id.to_string()
|
||||
@ -22,14 +22,14 @@ pub trait SyncIo {
|
||||
}
|
||||
|
||||
/// Wraps `NetworkContext` and the blockchain client
|
||||
pub struct NetSyncIo<'s, 'h, 'io> where 'h: 's, 'io: 'h {
|
||||
network: &'s mut NetworkContext<'h, 'io, SyncMessage>,
|
||||
chain: &'s mut BlockChainClient
|
||||
pub struct NetSyncIo<'s, 'h> where 'h: 's {
|
||||
network: &'s NetworkContext<'h, SyncMessage>,
|
||||
chain: &'s BlockChainClient
|
||||
}
|
||||
|
||||
impl<'s, 'h, 'io> NetSyncIo<'s, 'h, 'io> {
|
||||
impl<'s, 'h> NetSyncIo<'s, 'h> {
|
||||
/// Creates a new instance from the `NetworkContext` and the blockchain client reference.
|
||||
pub fn new(network: &'s mut NetworkContext<'h, 'io, SyncMessage>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h,'io> {
|
||||
pub fn new(network: &'s NetworkContext<'h, SyncMessage>, chain: &'s BlockChainClient) -> NetSyncIo<'s, 'h> {
|
||||
NetSyncIo {
|
||||
network: network,
|
||||
chain: chain,
|
||||
@ -37,7 +37,7 @@ impl<'s, 'h, 'io> NetSyncIo<'s, 'h, 'io> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, 'h, 'op> SyncIo for NetSyncIo<'s, 'h, 'op> {
|
||||
impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> {
|
||||
fn disable_peer(&mut self, peer_id: PeerId) {
|
||||
self.network.disable_peer(peer_id);
|
||||
}
|
||||
@ -50,7 +50,7 @@ impl<'s, 'h, 'op> SyncIo for NetSyncIo<'s, 'h, 'op> {
|
||||
self.network.send(peer_id, packet_id, data)
|
||||
}
|
||||
|
||||
fn chain<'a>(&'a mut self) -> &'a mut BlockChainClient {
|
||||
fn chain(&self) -> &BlockChainClient {
|
||||
self.chain
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
/// fn main() {
|
||||
/// let mut service = NetworkService::start().unwrap();
|
||||
/// let dir = env::temp_dir();
|
||||
/// let client = Arc::new(Client::new(ethereum::new_frontier(), &dir).unwrap());
|
||||
/// let client = Client::new(ethereum::new_frontier(), &dir, service.io().channel()).unwrap();
|
||||
/// EthSync::register(&mut service, client);
|
||||
/// }
|
||||
/// ```
|
||||
@ -25,10 +25,9 @@
|
||||
use std::ops::*;
|
||||
use std::sync::*;
|
||||
use client::Client;
|
||||
use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId, NetworkIoMessage};
|
||||
use util::TimerToken;
|
||||
use util::Bytes;
|
||||
use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId};
|
||||
use sync::chain::ChainSync;
|
||||
use service::SyncMessage;
|
||||
use sync::io::NetSyncIo;
|
||||
|
||||
mod chain;
|
||||
@ -38,76 +37,57 @@ mod range_collection;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
/// Message type for external events
|
||||
pub enum SyncMessage {
|
||||
/// New block has been imported into the blockchain
|
||||
NewChainBlock(Bytes),
|
||||
/// A block is ready
|
||||
BlockVerified,
|
||||
}
|
||||
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type NetSyncMessage = NetworkIoMessage<SyncMessage>;
|
||||
|
||||
/// Ethereum network protocol handler
|
||||
pub struct EthSync {
|
||||
/// Shared blockchain client. TODO: this should evetually become an IPC endpoint
|
||||
chain: Arc<RwLock<Client>>,
|
||||
chain: Arc<Client>,
|
||||
/// Sync strategy
|
||||
sync: ChainSync
|
||||
sync: RwLock<ChainSync>
|
||||
}
|
||||
|
||||
pub use self::chain::SyncStatus;
|
||||
|
||||
impl EthSync {
|
||||
/// Creates and register protocol with the network service
|
||||
pub fn register(service: &mut NetworkService<SyncMessage>, chain: Arc<RwLock<Client>>) {
|
||||
let sync = Box::new(EthSync {
|
||||
pub fn register(service: &mut NetworkService<SyncMessage>, chain: Arc<Client>) -> Arc<EthSync> {
|
||||
let sync = Arc::new(EthSync {
|
||||
chain: chain,
|
||||
sync: ChainSync::new(),
|
||||
sync: RwLock::new(ChainSync::new()),
|
||||
});
|
||||
service.register_protocol(sync, "eth", &[62u8, 63u8]).expect("Error registering eth protocol handler");
|
||||
service.register_protocol(sync.clone(), "eth", &[62u8, 63u8]).expect("Error registering eth protocol handler");
|
||||
sync
|
||||
}
|
||||
|
||||
/// Get sync status
|
||||
pub fn status(&self) -> SyncStatus {
|
||||
self.sync.status()
|
||||
self.sync.read().unwrap().status()
|
||||
}
|
||||
|
||||
/// Stop sync
|
||||
pub fn stop(&mut self, io: &mut NetworkContext<SyncMessage>) {
|
||||
self.sync.abort(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()));
|
||||
self.sync.write().unwrap().abort(&mut NetSyncIo::new(io, self.chain.deref()));
|
||||
}
|
||||
|
||||
/// Restart sync
|
||||
pub fn restart(&mut self, io: &mut NetworkContext<SyncMessage>) {
|
||||
self.sync.restart(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()));
|
||||
self.sync.write().unwrap().restart(&mut NetSyncIo::new(io, self.chain.deref()));
|
||||
}
|
||||
}
|
||||
|
||||
impl NetworkProtocolHandler<SyncMessage> for EthSync {
|
||||
fn initialize(&mut self, io: &mut NetworkContext<SyncMessage>) {
|
||||
self.sync.restart(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()));
|
||||
io.register_timer(1000).unwrap();
|
||||
fn initialize(&self, _io: &NetworkContext<SyncMessage>) {
|
||||
}
|
||||
|
||||
fn read(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
self.sync.on_packet(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()) , *peer, packet_id, data);
|
||||
fn read(&self, io: &NetworkContext<SyncMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
self.sync.write().unwrap().on_packet(&mut NetSyncIo::new(io, self.chain.deref()) , *peer, packet_id, data);
|
||||
}
|
||||
|
||||
fn connected(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId) {
|
||||
self.sync.on_peer_connected(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()), *peer);
|
||||
fn connected(&self, io: &NetworkContext<SyncMessage>, peer: &PeerId) {
|
||||
self.sync.write().unwrap().on_peer_connected(&mut NetSyncIo::new(io, self.chain.deref()), *peer);
|
||||
}
|
||||
|
||||
fn disconnected(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId) {
|
||||
self.sync.on_peer_aborting(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()), *peer);
|
||||
}
|
||||
|
||||
fn timeout(&mut self, io: &mut NetworkContext<SyncMessage>, _timer: TimerToken) {
|
||||
self.sync.maintain_sync(&mut NetSyncIo::new(io, self.chain.write().unwrap().deref_mut()));
|
||||
}
|
||||
|
||||
fn message(&mut self, _io: &mut NetworkContext<SyncMessage>, _message: &SyncMessage) {
|
||||
fn disconnected(&self, io: &NetworkContext<SyncMessage>, peer: &PeerId) {
|
||||
self.sync.write().unwrap().on_peer_aborting(&mut NetSyncIo::new(io, self.chain.deref()), *peer);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,7 @@ pub trait RangeCollection<K, V> {
|
||||
/// 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>;
|
||||
fn range_iter(& self) -> RangeIterator<K, V>;
|
||||
}
|
||||
|
||||
/// Range iterator. For each range yelds a key for the first element of the range and a vector of values.
|
||||
@ -60,7 +60,7 @@ impl<'c, K:'c, V:'c> Iterator for RangeIterator<'c, K, V> where K: Add<Output =
|
||||
}
|
||||
|
||||
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> {
|
||||
fn range_iter(&self) -> RangeIterator<K, V> {
|
||||
RangeIterator {
|
||||
range: self.len(),
|
||||
collection: self
|
||||
@ -191,6 +191,7 @@ impl<K, V> RangeCollection<K, V> for Vec<(K, Vec<V>)> where K: Ord + PartialEq +
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(cyclomatic_complexity)]
|
||||
fn test_range() {
|
||||
use std::cmp::{Ordering};
|
||||
|
||||
|
@ -1,38 +1,40 @@
|
||||
use util::*;
|
||||
use client::{BlockChainClient, BlockStatus, TreeRoute, BlockQueueStatus, BlockChainInfo};
|
||||
use client::{BlockChainClient, BlockStatus, TreeRoute, BlockChainInfo};
|
||||
use block_queue::BlockQueueInfo;
|
||||
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>,
|
||||
blocks: RwLock<HashMap<H256, Bytes>>,
|
||||
numbers: RwLock<HashMap<usize, H256>>,
|
||||
genesis_hash: H256,
|
||||
last_hash: H256,
|
||||
difficulty: U256
|
||||
last_hash: RwLock<H256>,
|
||||
difficulty: RwLock<U256>,
|
||||
}
|
||||
|
||||
impl TestBlockChainClient {
|
||||
fn new() -> TestBlockChainClient {
|
||||
|
||||
let mut client = TestBlockChainClient {
|
||||
blocks: HashMap::new(),
|
||||
numbers: HashMap::new(),
|
||||
blocks: RwLock::new(HashMap::new()),
|
||||
numbers: RwLock::new(HashMap::new()),
|
||||
genesis_hash: H256::new(),
|
||||
last_hash: H256::new(),
|
||||
difficulty: From::from(0),
|
||||
last_hash: RwLock::new(H256::new()),
|
||||
difficulty: RwLock::new(From::from(0)),
|
||||
};
|
||||
client.add_blocks(1, true); // add genesis block
|
||||
client.genesis_hash = client.last_hash.clone();
|
||||
client.genesis_hash = client.last_hash.read().unwrap().clone();
|
||||
client
|
||||
}
|
||||
|
||||
pub fn add_blocks(&mut self, count: usize, empty: bool) {
|
||||
for n in self.numbers.len()..(self.numbers.len() + count) {
|
||||
let len = self.numbers.read().unwrap().len();
|
||||
for n in len..(len + count) {
|
||||
let mut header = BlockHeader::new();
|
||||
header.difficulty = From::from(n);
|
||||
header.parent_hash = self.last_hash.clone();
|
||||
header.parent_hash = self.last_hash.read().unwrap().clone();
|
||||
header.number = n as BlockNumber;
|
||||
let mut uncles = RlpStream::new_list(if empty {0} else {1});
|
||||
if !empty {
|
||||
@ -50,12 +52,12 @@ impl TestBlockChainClient {
|
||||
|
||||
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())
|
||||
self.blocks.read().unwrap().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| {
|
||||
self.blocks.read().unwrap().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);
|
||||
@ -64,30 +66,30 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
}
|
||||
|
||||
fn block(&self, h: &H256) -> Option<Bytes> {
|
||||
self.blocks.get(h).map(|b| b.clone())
|
||||
self.blocks.read().unwrap().get(h).cloned()
|
||||
}
|
||||
|
||||
fn block_status(&self, h: &H256) -> BlockStatus {
|
||||
match self.blocks.get(h) {
|
||||
match self.blocks.read().unwrap().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))
|
||||
self.numbers.read().unwrap().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))
|
||||
self.numbers.read().unwrap().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())
|
||||
self.numbers.read().unwrap().get(&(n as usize)).map(|h| self.blocks.read().unwrap().get(h).unwrap().clone())
|
||||
}
|
||||
|
||||
fn block_status_at(&self, n: BlockNumber) -> BlockStatus {
|
||||
if (n as usize) < self.blocks.len() {
|
||||
if (n as usize) < self.blocks.read().unwrap().len() {
|
||||
BlockStatus::InChain
|
||||
} else {
|
||||
BlockStatus::Unknown
|
||||
@ -110,14 +112,14 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
None
|
||||
}
|
||||
|
||||
fn import_block(&mut self, b: Bytes) -> ImportResult {
|
||||
fn import_block(&self, b: Bytes) -> 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 > self.blocks.read().unwrap().len() {
|
||||
panic!("Unexpected block number. Expected {}, got {}", self.blocks.read().unwrap().len(), number);
|
||||
}
|
||||
if number > 0 {
|
||||
match self.blocks.get(&header.parent_hash) {
|
||||
match self.blocks.read().unwrap().get(&header.parent_hash) {
|
||||
Some(parent) => {
|
||||
let parent = Rlp::new(parent).val_at::<BlockHeader>(0);
|
||||
if parent.number != (header.number - 1) {
|
||||
@ -129,43 +131,46 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
}
|
||||
}
|
||||
}
|
||||
if number == self.numbers.len() {
|
||||
self.difficulty = self.difficulty + header.difficulty;
|
||||
self.last_hash = header.hash();
|
||||
self.blocks.insert(header.hash(), b);
|
||||
self.numbers.insert(number, header.hash());
|
||||
let len = self.numbers.read().unwrap().len();
|
||||
if number == len {
|
||||
*self.difficulty.write().unwrap().deref_mut() += header.difficulty;
|
||||
mem::replace(self.last_hash.write().unwrap().deref_mut(), header.hash());
|
||||
self.blocks.write().unwrap().insert(header.hash(), b);
|
||||
self.numbers.write().unwrap().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();
|
||||
while n > 0 && self.numbers.read().unwrap()[&n] != parent_hash {
|
||||
*self.numbers.write().unwrap().get_mut(&n).unwrap() = parent_hash.clone();
|
||||
n -= 1;
|
||||
parent_hash = Rlp::new(&self.blocks[&parent_hash]).val_at::<BlockHeader>(0).parent_hash;
|
||||
parent_hash = Rlp::new(&self.blocks.read().unwrap()[&parent_hash]).val_at::<BlockHeader>(0).parent_hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
self.blocks.insert(header.hash(), b.to_vec());
|
||||
self.blocks.write().unwrap().insert(header.hash(), b.to_vec());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn queue_status(&self) -> BlockQueueStatus {
|
||||
BlockQueueStatus {
|
||||
fn queue_info(&self) -> BlockQueueInfo {
|
||||
BlockQueueInfo {
|
||||
full: false,
|
||||
verified_queue_size: 0,
|
||||
unverified_queue_size: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_queue(&mut self) {
|
||||
fn clear_queue(&self) {
|
||||
}
|
||||
|
||||
fn chain_info(&self) -> BlockChainInfo {
|
||||
BlockChainInfo {
|
||||
total_difficulty: self.difficulty,
|
||||
pending_total_difficulty: self.difficulty,
|
||||
total_difficulty: *self.difficulty.read().unwrap(),
|
||||
pending_total_difficulty: *self.difficulty.read().unwrap(),
|
||||
genesis_hash: self.genesis_hash.clone(),
|
||||
best_block_hash: self.last_hash.clone(),
|
||||
best_block_number: self.blocks.len() as BlockNumber - 1,
|
||||
best_block_hash: self.last_hash.read().unwrap().clone(),
|
||||
best_block_number: self.blocks.read().unwrap().len() as BlockNumber - 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -208,7 +213,7 @@ impl<'p> SyncIo for TestIo<'p> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn chain<'a>(&'a mut self) -> &'a mut BlockChainClient {
|
||||
fn chain(&self) -> &BlockChainClient {
|
||||
self.chain
|
||||
}
|
||||
}
|
||||
@ -265,17 +270,14 @@ impl TestNet {
|
||||
|
||||
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 => {}
|
||||
if let Some(packet) = self.peers[peer].queue.pop_front() {
|
||||
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!("----------------");
|
||||
}
|
||||
let mut p = self.peers.get_mut(peer).unwrap();
|
||||
p.sync.maintain_sync(&mut TestIo::new(&mut p.chain, &mut p.queue, None));
|
||||
p.sync._maintain_sync(&mut TestIo::new(&mut p.chain, &mut p.queue, None));
|
||||
}
|
||||
}
|
||||
|
||||
@ -300,7 +302,7 @@ fn full_sync_two_peers() {
|
||||
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);
|
||||
assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -313,7 +315,7 @@ fn full_sync_empty_blocks() {
|
||||
}
|
||||
net.sync();
|
||||
assert!(net.peer(0).chain.block_at(1000).is_some());
|
||||
assert_eq!(net.peer(0).chain.blocks, net.peer(1).chain.blocks);
|
||||
assert_eq!(net.peer(0).chain.blocks.read().unwrap().deref(), net.peer(1).chain.blocks.read().unwrap().deref());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -329,9 +331,9 @@ fn forked_sync() {
|
||||
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();
|
||||
let peer1_chain = net.peer(1).chain.numbers.read().unwrap().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);
|
||||
assert_eq!(net.peer(0).chain.numbers.read().unwrap().deref(), &peer1_chain);
|
||||
assert_eq!(net.peer(1).chain.numbers.read().unwrap().deref(), &peer1_chain);
|
||||
assert_eq!(net.peer(2).chain.numbers.read().unwrap().deref(), &peer1_chain);
|
||||
}
|
||||
|
@ -183,7 +183,7 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
|
||||
let mut fail = false;
|
||||
//let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.to_string()); fail = true };
|
||||
let mut fail_unless = |cond: bool, s: &str | if !cond && !fail {
|
||||
failed.push(format!("[{}] {}: {}", vm, name.to_string(), s));
|
||||
failed.push(format!("[{}] {}: {}", vm, name, s));
|
||||
fail = true
|
||||
};
|
||||
|
||||
@ -202,15 +202,9 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
|
||||
BTreeMap::from_json(&s["storage"]).into_iter().foreach(|(k, v)| state.set_storage(&address, k, v));
|
||||
});
|
||||
|
||||
let mut info = EnvInfo::new();
|
||||
|
||||
test.find("env").map(|env| {
|
||||
info.author = xjson!(&env["currentCoinbase"]);
|
||||
info.difficulty = xjson!(&env["currentDifficulty"]);
|
||||
info.gas_limit = xjson!(&env["currentGasLimit"]);
|
||||
info.number = xjson!(&env["currentNumber"]);
|
||||
info.timestamp = xjson!(&env["currentTimestamp"]);
|
||||
});
|
||||
let info = test.find("env").map(|env| {
|
||||
EnvInfo::from_json(env)
|
||||
}).unwrap_or_default();
|
||||
|
||||
let engine = TestEngine::new(1, vm.clone());
|
||||
|
||||
@ -260,7 +254,7 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
|
||||
test.find("post").map(|pre| for (addr, s) in pre.as_object().unwrap() {
|
||||
let address = Address::from(addr.as_ref());
|
||||
|
||||
fail_unless(state.code(&address).unwrap_or(vec![]) == Bytes::from_json(&s["code"]), "code is incorrect");
|
||||
fail_unless(state.code(&address).unwrap_or_else(|| vec![]) == Bytes::from_json(&s["code"]), "code is incorrect");
|
||||
fail_unless(state.balance(&address) == xjson!(&s["balance"]), "balance is incorrect");
|
||||
fail_unless(state.nonce(&address) == xjson!(&s["nonce"]), "nonce is incorrect");
|
||||
BTreeMap::from_json(&s["storage"]).iter().foreach(|(k, v)| fail_unless(&state.storage_at(&address, &k) == v, "storage is incorrect"));
|
||||
@ -281,7 +275,7 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
|
||||
}
|
||||
|
||||
|
||||
for f in failed.iter() {
|
||||
for f in &failed {
|
||||
println!("FAILED: {:?}", f);
|
||||
}
|
||||
|
||||
@ -292,11 +286,10 @@ fn do_json_test_for(vm: &VMType, json_data: &[u8]) -> Vec<String> {
|
||||
declare_test!{ExecutiveTests_vmArithmeticTest, "VMTests/vmArithmeticTest"}
|
||||
declare_test!{ExecutiveTests_vmBitwiseLogicOperationTest, "VMTests/vmBitwiseLogicOperationTest"}
|
||||
// this one crashes with some vm internal error. Separately they pass.
|
||||
declare_test_ignore!{ExecutiveTests_vmBlockInfoTest, "VMTests/vmBlockInfoTest"}
|
||||
declare_test!{ignore => ExecutiveTests_vmBlockInfoTest, "VMTests/vmBlockInfoTest"}
|
||||
declare_test!{ExecutiveTests_vmEnvironmentalInfoTest, "VMTests/vmEnvironmentalInfoTest"}
|
||||
declare_test!{ExecutiveTests_vmIOandFlowOperationsTest, "VMTests/vmIOandFlowOperationsTest"}
|
||||
// this one take way too long.
|
||||
declare_test_ignore!{ExecutiveTests_vmInputLimits, "VMTests/vmInputLimits"}
|
||||
declare_test!{heavy => ExecutiveTests_vmInputLimits, "VMTests/vmInputLimits"}
|
||||
declare_test!{ExecutiveTests_vmLogTest, "VMTests/vmLogTest"}
|
||||
declare_test!{ExecutiveTests_vmPerformanceTest, "VMTests/vmPerformanceTest"}
|
||||
declare_test!{ExecutiveTests_vmPushDupSwapTest, "VMTests/vmPushDupSwapTest"}
|
||||
|
@ -15,7 +15,7 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
|
||||
let mut fail = false;
|
||||
{
|
||||
let mut fail_unless = |cond: bool| if !cond && !fail {
|
||||
failed.push(name.to_string());
|
||||
failed.push(name.clone());
|
||||
flush(format!("FAIL\n"));
|
||||
fail = true;
|
||||
true
|
||||
@ -73,20 +73,20 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
|
||||
|
||||
declare_test!{StateTests_stBlockHashTest, "StateTests/stBlockHashTest"}
|
||||
declare_test!{StateTests_stCallCodes, "StateTests/stCallCodes"}
|
||||
declare_test_ignore!{StateTests_stCallCreateCallCodeTest, "StateTests/stCallCreateCallCodeTest"} //<< Out of stack
|
||||
declare_test!{StateTests_stDelegatecallTest, "StateTests/stDelegatecallTest"} //<< FAIL - gas too high
|
||||
declare_test!{ignore => StateTests_stCallCreateCallCodeTest, "StateTests/stCallCreateCallCodeTest"} //<< Out of stack
|
||||
declare_test!{StateTests_stDelegatecallTest, "StateTests/stDelegatecallTest"}
|
||||
declare_test!{StateTests_stExample, "StateTests/stExample"}
|
||||
declare_test!{StateTests_stInitCodeTest, "StateTests/stInitCodeTest"}
|
||||
declare_test!{StateTests_stLogTests, "StateTests/stLogTests"}
|
||||
declare_test!{StateTests_stMemoryStressTest, "StateTests/stMemoryStressTest"}
|
||||
declare_test!{StateTests_stMemoryTest, "StateTests/stMemoryTest"}
|
||||
declare_test!{heavy => StateTests_stMemoryStressTest, "StateTests/stMemoryStressTest"}
|
||||
declare_test!{heavy => StateTests_stMemoryTest, "StateTests/stMemoryTest"}
|
||||
declare_test!{StateTests_stPreCompiledContracts, "StateTests/stPreCompiledContracts"}
|
||||
declare_test_ignore!{StateTests_stQuadraticComplexityTest, "StateTests/stQuadraticComplexityTest"} //<< Too long
|
||||
declare_test_ignore!{StateTests_stRecursiveCreate, "StateTests/stRecursiveCreate"} //<< Out of stack
|
||||
declare_test!{heavy => StateTests_stQuadraticComplexityTest, "StateTests/stQuadraticComplexityTest"} //<< Too long
|
||||
declare_test!{ignore => StateTests_stRecursiveCreate, "StateTests/stRecursiveCreate"} //<< Out of stack
|
||||
declare_test!{StateTests_stRefundTest, "StateTests/stRefundTest"}
|
||||
declare_test!{StateTests_stSolidityTest, "StateTests/stSolidityTest"}
|
||||
declare_test_ignore!{StateTests_stSpecialTest, "StateTests/stSpecialTest"} //<< Signal 11
|
||||
declare_test_ignore!{StateTests_stSystemOperationsTest, "StateTests/stSystemOperationsTest"} //<< Signal 11
|
||||
declare_test!{ignore => StateTests_stSpecialTest, "StateTests/stSpecialTest"} //<< Out of Stack
|
||||
declare_test!{ignore => StateTests_stSystemOperationsTest, "StateTests/stSystemOperationsTest"} //<< Out of stack
|
||||
declare_test!{StateTests_stTransactionTest, "StateTests/stTransactionTest"}
|
||||
declare_test!{StateTests_stTransitionTest, "StateTests/stTransitionTest"}
|
||||
declare_test!{StateTests_stWalletTest, "StateTests/stWalletTest"}
|
||||
|
@ -1,24 +1,34 @@
|
||||
pub use common::*;
|
||||
|
||||
macro_rules! test {
|
||||
($name: expr) => {
|
||||
assert!(do_json_test(include_bytes!(concat!("../../res/ethereum/tests/", $name, ".json"))).is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! declare_test {
|
||||
($id: ident, $name: expr) => {
|
||||
#[test]
|
||||
#[allow(non_snake_case)]
|
||||
fn $id() {
|
||||
assert!(do_json_test(include_bytes!(concat!("../../res/ethereum/tests/", $name, ".json"))).len() == 0);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! declare_test_ignore {
|
||||
($id: ident, $name: expr) => {
|
||||
#[test]
|
||||
(ignore => $id: ident, $name: expr) => {
|
||||
#[ignore]
|
||||
#[test]
|
||||
#[allow(non_snake_case)]
|
||||
fn $id() {
|
||||
assert!(do_json_test(include_bytes!(concat!("../../res/ethereum/tests/", $name, ".json"))).len() == 0);
|
||||
test!($name);
|
||||
}
|
||||
};
|
||||
(heavy => $id: ident, $name: expr) => {
|
||||
#[cfg(feature = "test-heavy")]
|
||||
#[test]
|
||||
#[allow(non_snake_case)]
|
||||
fn $id() {
|
||||
test!($name);
|
||||
}
|
||||
};
|
||||
($id: ident, $name: expr) => {
|
||||
#[test]
|
||||
#[allow(non_snake_case)]
|
||||
fn $id() {
|
||||
test!($name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,13 +9,13 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
|
||||
let ot = RefCell::new(Transaction::new());
|
||||
for (name, test) in json.as_object().unwrap() {
|
||||
let mut fail = false;
|
||||
let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.to_string()); println!("Transaction: {:?}", ot.borrow()); fail = true };
|
||||
let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.clone()); println!("Transaction: {:?}", ot.borrow()); fail = true };
|
||||
let schedule = match test.find("blocknumber")
|
||||
.and_then(|j| j.as_string())
|
||||
.and_then(|s| BlockNumber::from_str(s).ok())
|
||||
.unwrap_or(0) { x if x < 900000 => &old_schedule, _ => &new_schedule };
|
||||
let rlp = Bytes::from_json(&test["rlp"]);
|
||||
let res = UntrustedRlp::new(&rlp).as_val().map_err(|e| From::from(e)).and_then(|t: Transaction| t.validate(schedule, schedule.have_delegate_call));
|
||||
let res = UntrustedRlp::new(&rlp).as_val().map_err(From::from).and_then(|t: Transaction| t.validate(schedule, schedule.have_delegate_call));
|
||||
fail_unless(test.find("transaction").is_none() == res.is_err());
|
||||
if let (Some(&Json::Object(ref tx)), Some(&Json::String(ref expect_sender))) = (test.find("transaction"), test.find("sender")) {
|
||||
let t = res.unwrap();
|
||||
@ -30,11 +30,11 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
|
||||
fail_unless(to == &xjson!(&tx["to"]));
|
||||
} else {
|
||||
*ot.borrow_mut() = t.clone();
|
||||
fail_unless(Bytes::from_json(&tx["to"]).len() == 0);
|
||||
fail_unless(Bytes::from_json(&tx["to"]).is_empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
for f in failed.iter() {
|
||||
for f in &failed {
|
||||
println!("FAILED: {:?}", f);
|
||||
}
|
||||
failed
|
||||
@ -65,14 +65,14 @@ declare_test!{TransactionTests/ttTransactionTest}
|
||||
declare_test!{TransactionTests/tt10mbDataField}
|
||||
declare_test!{TransactionTests/ttWrongRLPTransaction}
|
||||
declare_test!{TransactionTests/Homestead/ttTransactionTest}
|
||||
declare_test!{TransactionTests/Homestead/tt10mbDataField}
|
||||
declare_test!{heavy => TransactionTests/Homestead/tt10mbDataField}
|
||||
declare_test!{TransactionTests/Homestead/ttWrongRLPTransaction}
|
||||
declare_test!{TransactionTests/RandomTests/tr201506052141PYTHON}*/
|
||||
|
||||
declare_test!{TransactionTests_ttTransactionTest, "TransactionTests/ttTransactionTest"}
|
||||
declare_test_ignore!{TransactionTests_tt10mbDataField, "TransactionTests/tt10mbDataField"}
|
||||
declare_test!{heavy => TransactionTests_tt10mbDataField, "TransactionTests/tt10mbDataField"}
|
||||
declare_test!{TransactionTests_ttWrongRLPTransaction, "TransactionTests/ttWrongRLPTransaction"}
|
||||
declare_test!{TransactionTests_Homestead_ttTransactionTest, "TransactionTests/Homestead/ttTransactionTest"}
|
||||
declare_test_ignore!{TransactionTests_Homestead_tt10mbDataField, "TransactionTests/Homestead/tt10mbDataField"}
|
||||
declare_test!{heavy => TransactionTests_Homestead_tt10mbDataField, "TransactionTests/Homestead/tt10mbDataField"}
|
||||
declare_test!{TransactionTests_Homestead_ttWrongRLPTransaction, "TransactionTests/Homestead/ttWrongRLPTransaction"}
|
||||
declare_test!{TransactionTests_RandomTests_tr201506052141PYTHON, "TransactionTests/RandomTests/tr201506052141PYTHON"}
|
||||
|
@ -117,9 +117,8 @@ impl Transaction {
|
||||
};
|
||||
s.append(&self.value);
|
||||
s.append(&self.data);
|
||||
match with_seal {
|
||||
Seal::With => { s.append(&(self.v as u16)).append(&self.r).append(&self.s); },
|
||||
_ => {}
|
||||
if let Seal::With = with_seal {
|
||||
s.append(&(self.v as u16)).append(&self.r).append(&self.s);
|
||||
}
|
||||
}
|
||||
|
||||
@ -138,7 +137,7 @@ impl FromJson for Transaction {
|
||||
gas_price: xjson!(&json["gasPrice"]),
|
||||
gas: xjson!(&json["gasLimit"]),
|
||||
action: match Bytes::from_json(&json["to"]) {
|
||||
ref x if x.len() == 0 => Action::Create,
|
||||
ref x if x.is_empty() => Action::Create,
|
||||
ref x => Action::Call(Address::from_slice(x)),
|
||||
},
|
||||
value: xjson!(&json["value"]),
|
||||
@ -303,4 +302,4 @@ fn signing() {
|
||||
let key = KeyPair::create().unwrap();
|
||||
let t = Transaction::new_create(U256::from(42u64), b"Hello!".to_vec(), U256::from(3000u64), U256::from(50_000u64), U256::from(1u64)).signed(&key.secret());
|
||||
assert_eq!(Address::from(key.public().sha3()), t.sender().unwrap());
|
||||
}
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ pub fn verify_block_unordered(header: Header, bytes: Bytes, engine: &Engine) ->
|
||||
/// Phase 3 verification. Check block information against parent and uncles.
|
||||
pub fn verify_block_family<BC>(header: &Header, bytes: &[u8], engine: &Engine, bc: &BC) -> Result<(), Error> where BC: BlockProvider {
|
||||
// TODO: verify timestamp
|
||||
let parent = try!(bc.block_header(&header.parent_hash).ok_or::<Error>(From::from(BlockError::UnknownParent(header.parent_hash.clone()))));
|
||||
let parent = try!(bc.block_header(&header.parent_hash).ok_or_else(|| Error::from(BlockError::UnknownParent(header.parent_hash.clone()))));
|
||||
try!(verify_parent(&header, &parent));
|
||||
try!(engine.verify_block_family(&header, &parent, Some(bytes)));
|
||||
|
||||
@ -122,7 +122,7 @@ pub fn verify_block_family<BC>(header: &Header, bytes: &[u8], engine: &Engine, b
|
||||
// cB.p^7 -------------/
|
||||
// cB.p^8
|
||||
let mut expected_uncle_parent = header.parent_hash.clone();
|
||||
let uncle_parent = try!(bc.block_header(&uncle.parent_hash).ok_or::<Error>(From::from(BlockError::UnknownUncleParent(uncle.parent_hash.clone()))));
|
||||
let uncle_parent = try!(bc.block_header(&uncle.parent_hash).ok_or_else(|| Error::from(BlockError::UnknownUncleParent(uncle.parent_hash.clone()))));
|
||||
for _ in 0..depth {
|
||||
match bc.block_details(&expected_uncle_parent) {
|
||||
Some(details) => {
|
||||
@ -284,7 +284,7 @@ mod tests {
|
||||
|
||||
/// Get raw block data
|
||||
fn block(&self, hash: &H256) -> Option<Bytes> {
|
||||
self.blocks.get(hash).map(|b| b.clone())
|
||||
self.blocks.get(hash).cloned()
|
||||
}
|
||||
|
||||
/// Get the familial details concerning a block.
|
||||
@ -302,7 +302,7 @@ mod tests {
|
||||
|
||||
/// Get the hash of given block's number.
|
||||
fn block_hash(&self, index: BlockNumber) -> Option<H256> {
|
||||
self.numbers.get(&index).map(|h| h.clone())
|
||||
self.numbers.get(&index).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -141,7 +141,7 @@ impl<'a> BlockView<'a> {
|
||||
|
||||
/// Return List of transactions in given block.
|
||||
pub fn transaction_views(&self) -> Vec<TransactionView> {
|
||||
self.rlp.at(1).iter().map(|rlp| TransactionView::new_from_rlp(rlp)).collect()
|
||||
self.rlp.at(1).iter().map(TransactionView::new_from_rlp).collect()
|
||||
}
|
||||
|
||||
/// Return transaction hashes.
|
||||
@ -156,7 +156,7 @@ impl<'a> BlockView<'a> {
|
||||
|
||||
/// Return List of transactions in given block.
|
||||
pub fn uncle_views(&self) -> Vec<HeaderView> {
|
||||
self.rlp.at(2).iter().map(|rlp| HeaderView::new_from_rlp(rlp)).collect()
|
||||
self.rlp.at(2).iter().map(HeaderView::new_from_rlp).collect()
|
||||
}
|
||||
|
||||
/// Return list of uncle hashes of given block.
|
||||
|
@ -22,8 +22,10 @@ rust-crypto = "0.2.34"
|
||||
elastic-array = "0.4"
|
||||
heapsize = "0.2"
|
||||
itertools = "0.4"
|
||||
crossbeam = "0.2"
|
||||
slab = { git = "https://github.com/arkpar/slab.git" }
|
||||
sha3 = { path = "sha3" }
|
||||
clippy = "*" # Always newest, since we use nightly
|
||||
|
||||
[dev-dependencies]
|
||||
json-tests = { path = "json-tests" }
|
||||
|
@ -106,18 +106,18 @@ impl<'a> Deref for BytesRef<'a> {
|
||||
type Target = [u8];
|
||||
|
||||
fn deref(&self) -> &[u8] {
|
||||
match self {
|
||||
&BytesRef::Flexible(ref bytes) => bytes,
|
||||
&BytesRef::Fixed(ref bytes) => bytes
|
||||
match *self {
|
||||
BytesRef::Flexible(ref bytes) => bytes,
|
||||
BytesRef::Fixed(ref bytes) => bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl <'a> DerefMut for BytesRef<'a> {
|
||||
fn deref_mut(&mut self) -> &mut [u8] {
|
||||
match self {
|
||||
&mut BytesRef::Flexible(ref mut bytes) => bytes,
|
||||
&mut BytesRef::Fixed(ref mut bytes) => bytes
|
||||
match *self {
|
||||
BytesRef::Flexible(ref mut bytes) => bytes,
|
||||
BytesRef::Fixed(ref mut bytes) => bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -299,7 +299,7 @@ pub trait FromBytes: Sized {
|
||||
|
||||
impl FromBytes for String {
|
||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<String> {
|
||||
Ok(::std::str::from_utf8(bytes).unwrap().to_string())
|
||||
Ok(::std::str::from_utf8(bytes).unwrap().to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -323,10 +323,9 @@ impl<'a, D> ChainFilter<'a, D> where D: FilterDataSource
|
||||
let offset = level_size * index;
|
||||
|
||||
// go doooown!
|
||||
match self.blocks(bloom, from_block, to_block, max_level, offset) {
|
||||
Some(blocks) => result.extend(blocks),
|
||||
None => ()
|
||||
};
|
||||
if let Some(blocks) = self.blocks(bloom, from_block, to_block, max_level, offset) {
|
||||
result.extend(blocks);
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
|
@ -207,11 +207,11 @@ macro_rules! impl_hash {
|
||||
|
||||
impl FromJson for $from {
|
||||
fn from_json(json: &Json) -> Self {
|
||||
match json {
|
||||
&Json::String(ref s) => {
|
||||
match *json {
|
||||
Json::String(ref s) => {
|
||||
match s.len() % 2 {
|
||||
0 => FromStr::from_str(clean_0x(s)).unwrap(),
|
||||
_ => FromStr::from_str(&("0".to_string() + &(clean_0x(s).to_string()))[..]).unwrap()
|
||||
_ => FromStr::from_str(&("0".to_owned() + &(clean_0x(s).to_owned()))[..]).unwrap()
|
||||
}
|
||||
},
|
||||
_ => Default::default(),
|
||||
@ -221,7 +221,7 @@ macro_rules! impl_hash {
|
||||
|
||||
impl fmt::Debug for $from {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
for i in self.0.iter() {
|
||||
for i in &self.0[..] {
|
||||
try!(write!(f, "{:02x}", i));
|
||||
}
|
||||
Ok(())
|
||||
@ -229,11 +229,11 @@ macro_rules! impl_hash {
|
||||
}
|
||||
impl fmt::Display for $from {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
for i in self.0[0..2].iter() {
|
||||
for i in &self.0[0..2] {
|
||||
try!(write!(f, "{:02x}", i));
|
||||
}
|
||||
try!(write!(f, "…"));
|
||||
for i in self.0[$size - 4..$size].iter() {
|
||||
for i in &self.0[$size - 4..$size] {
|
||||
try!(write!(f, "{:02x}", i));
|
||||
}
|
||||
Ok(())
|
||||
@ -291,36 +291,36 @@ macro_rules! impl_hash {
|
||||
impl Index<usize> for $from {
|
||||
type Output = u8;
|
||||
|
||||
fn index<'a>(&'a self, index: usize) -> &'a u8 {
|
||||
fn index(&self, index: usize) -> &u8 {
|
||||
&self.0[index]
|
||||
}
|
||||
}
|
||||
impl IndexMut<usize> for $from {
|
||||
fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut u8 {
|
||||
fn index_mut(&mut self, index: usize) -> &mut u8 {
|
||||
&mut self.0[index]
|
||||
}
|
||||
}
|
||||
impl Index<ops::Range<usize>> for $from {
|
||||
type Output = [u8];
|
||||
|
||||
fn index<'a>(&'a self, index: ops::Range<usize>) -> &'a [u8] {
|
||||
fn index(&self, index: ops::Range<usize>) -> &[u8] {
|
||||
&self.0[index]
|
||||
}
|
||||
}
|
||||
impl IndexMut<ops::Range<usize>> for $from {
|
||||
fn index_mut<'a>(&'a mut self, index: ops::Range<usize>) -> &'a mut [u8] {
|
||||
fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [u8] {
|
||||
&mut self.0[index]
|
||||
}
|
||||
}
|
||||
impl Index<ops::RangeFull> for $from {
|
||||
type Output = [u8];
|
||||
|
||||
fn index<'a>(&'a self, _index: ops::RangeFull) -> &'a [u8] {
|
||||
fn index(&self, _index: ops::RangeFull) -> &[u8] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
impl IndexMut<ops::RangeFull> for $from {
|
||||
fn index_mut<'a>(&'a mut self, _index: ops::RangeFull) -> &'a mut [u8] {
|
||||
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut [u8] {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
@ -440,9 +440,9 @@ macro_rules! impl_hash {
|
||||
fn from(s: &'_ str) -> $from {
|
||||
use std::str::FromStr;
|
||||
if s.len() % 2 == 1 {
|
||||
$from::from_str(&("0".to_string() + &(clean_0x(s).to_string()))[..]).unwrap_or($from::new())
|
||||
$from::from_str(&("0".to_owned() + &(clean_0x(s).to_owned()))[..]).unwrap_or_else(|_| $from::new())
|
||||
} else {
|
||||
$from::from_str(clean_0x(s)).unwrap_or($from::new())
|
||||
$from::from_str(clean_0x(s)).unwrap_or_else(|_| $from::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -565,6 +565,7 @@ mod tests {
|
||||
use std::str::FromStr;
|
||||
|
||||
#[test]
|
||||
#[allow(eq_op)]
|
||||
fn hash() {
|
||||
let h = H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]);
|
||||
assert_eq!(H64::from_str("0123456789abcdef").unwrap(), h);
|
||||
|
@ -8,27 +8,28 @@
|
||||
///
|
||||
/// struct MyHandler;
|
||||
///
|
||||
/// #[derive(Clone)]
|
||||
/// struct MyMessage {
|
||||
/// data: u32
|
||||
/// }
|
||||
///
|
||||
/// impl IoHandler<MyMessage> for MyHandler {
|
||||
/// fn initialize(&mut self, io: &mut IoContext<MyMessage>) {
|
||||
/// io.register_timer(1000).unwrap();
|
||||
/// fn initialize(&self, io: &IoContext<MyMessage>) {
|
||||
/// io.register_timer(0, 1000).unwrap();
|
||||
/// }
|
||||
///
|
||||
/// fn timeout(&mut self, _io: &mut IoContext<MyMessage>, timer: TimerToken) {
|
||||
/// fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
|
||||
/// println!("Timeout {}", timer);
|
||||
/// }
|
||||
///
|
||||
/// fn message(&mut self, _io: &mut IoContext<MyMessage>, message: &mut MyMessage) {
|
||||
/// fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
|
||||
/// println!("Message {}", message.data);
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// fn main () {
|
||||
/// let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
|
||||
/// service.register_handler(Box::new(MyHandler)).unwrap();
|
||||
/// service.register_handler(Arc::new(MyHandler)).unwrap();
|
||||
///
|
||||
/// // Wait for quit condition
|
||||
/// // ...
|
||||
@ -36,6 +37,9 @@
|
||||
/// }
|
||||
/// ```
|
||||
mod service;
|
||||
mod worker;
|
||||
|
||||
use mio::{EventLoop, Token};
|
||||
|
||||
#[derive(Debug)]
|
||||
/// TODO [arkpar] Please document me
|
||||
@ -44,7 +48,7 @@ pub enum IoError {
|
||||
Mio(::std::io::Error),
|
||||
}
|
||||
|
||||
impl<Message> From<::mio::NotifyError<service::IoMessage<Message>>> for IoError where Message: Send {
|
||||
impl<Message> From<::mio::NotifyError<service::IoMessage<Message>>> for IoError where Message: Send + Clone {
|
||||
fn from(_err: ::mio::NotifyError<service::IoMessage<Message>>) -> IoError {
|
||||
IoError::Mio(::std::io::Error::new(::std::io::ErrorKind::ConnectionAborted, "Network IO notification error"))
|
||||
}
|
||||
@ -53,54 +57,63 @@ impl<Message> From<::mio::NotifyError<service::IoMessage<Message>>> for IoError
|
||||
/// Generic IO handler.
|
||||
/// All the handler function are called from within IO event loop.
|
||||
/// `Message` type is used as notification data
|
||||
pub trait IoHandler<Message>: Send where Message: Send + 'static {
|
||||
pub trait IoHandler<Message>: Send + Sync where Message: Send + Sync + Clone + 'static {
|
||||
/// Initialize the handler
|
||||
fn initialize<'s>(&'s mut self, _io: &mut IoContext<'s, Message>) {}
|
||||
fn initialize(&self, _io: &IoContext<Message>) {}
|
||||
/// Timer function called after a timeout created with `HandlerIo::timeout`.
|
||||
fn timeout<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _timer: TimerToken) {}
|
||||
fn timeout(&self, _io: &IoContext<Message>, _timer: TimerToken) {}
|
||||
/// Called when a broadcasted message is received. The message can only be sent from a different IO handler.
|
||||
fn message<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _message: &'s mut Message) {} // TODO: make message immutable and provide internal channel for adding network handler
|
||||
fn message(&self, _io: &IoContext<Message>, _message: &Message) {}
|
||||
/// Called when an IO stream gets closed
|
||||
fn stream_hup<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _stream: StreamToken) {}
|
||||
fn stream_hup(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
|
||||
/// Called when an IO stream can be read from
|
||||
fn stream_readable<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _stream: StreamToken) {}
|
||||
fn stream_readable(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
|
||||
/// Called when an IO stream can be written to
|
||||
fn stream_writable<'s>(&'s mut self, _io: &mut IoContext<'s, Message>, _stream: StreamToken) {}
|
||||
fn stream_writable(&self, _io: &IoContext<Message>, _stream: StreamToken) {}
|
||||
/// Register a new stream with the event loop
|
||||
fn register_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop<IoManager<Message>>) {}
|
||||
/// Re-register a stream with the event loop
|
||||
fn update_stream(&self, _stream: StreamToken, _reg: Token, _event_loop: &mut EventLoop<IoManager<Message>>) {}
|
||||
}
|
||||
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type TimerToken = service::TimerToken;
|
||||
pub use io::service::TimerToken;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type StreamToken = service::StreamToken;
|
||||
pub use io::service::StreamToken;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type IoContext<'s, M> = service::IoContext<'s, M>;
|
||||
pub use io::service::IoContext;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type IoService<M> = service::IoService<M>;
|
||||
pub use io::service::IoService;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type IoChannel<M> = service::IoChannel<M>;
|
||||
//pub const USER_TOKEN_START: usize = service::USER_TOKEN; // TODO: ICE in rustc 1.7.0-nightly (49c382779 2016-01-12)
|
||||
pub use io::service::IoChannel;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub use io::service::IoManager;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub use io::service::TOKENS_PER_HANDLER;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use std::sync::Arc;
|
||||
use io::*;
|
||||
|
||||
struct MyHandler;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct MyMessage {
|
||||
data: u32
|
||||
}
|
||||
|
||||
impl IoHandler<MyMessage> for MyHandler {
|
||||
fn initialize(&mut self, io: &mut IoContext<MyMessage>) {
|
||||
io.register_timer(1000).unwrap();
|
||||
fn initialize(&self, io: &IoContext<MyMessage>) {
|
||||
io.register_timer(0, 1000).unwrap();
|
||||
}
|
||||
|
||||
fn timeout(&mut self, _io: &mut IoContext<MyMessage>, timer: TimerToken) {
|
||||
fn timeout(&self, _io: &IoContext<MyMessage>, timer: TimerToken) {
|
||||
println!("Timeout {}", timer);
|
||||
}
|
||||
|
||||
fn message(&mut self, _io: &mut IoContext<MyMessage>, message: &mut MyMessage) {
|
||||
fn message(&self, _io: &IoContext<MyMessage>, message: &MyMessage) {
|
||||
println!("Message {}", message.data);
|
||||
}
|
||||
}
|
||||
@ -108,7 +121,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_service_register_handler () {
|
||||
let mut service = IoService::<MyMessage>::start().expect("Error creating network service");
|
||||
service.register_handler(Box::new(MyHandler)).unwrap();
|
||||
service.register_handler(Arc::new(MyHandler)).unwrap();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,148 +1,229 @@
|
||||
use std::sync::*;
|
||||
use std::thread::{self, JoinHandle};
|
||||
use std::collections::HashMap;
|
||||
use mio::*;
|
||||
use mio::util::{Slab};
|
||||
use hash::*;
|
||||
use rlp::*;
|
||||
use error::*;
|
||||
use io::{IoError, IoHandler};
|
||||
use arrayvec::*;
|
||||
use crossbeam::sync::chase_lev;
|
||||
use io::worker::{Worker, Work, WorkType};
|
||||
|
||||
/// Timer ID
|
||||
pub type TimerToken = usize;
|
||||
/// Timer ID
|
||||
pub type StreamToken = usize;
|
||||
/// IO Hadndler ID
|
||||
pub type HandlerId = usize;
|
||||
|
||||
// Tokens
|
||||
const MAX_USER_TIMERS: usize = 32;
|
||||
const USER_TIMER: usize = 0;
|
||||
const LAST_USER_TIMER: usize = USER_TIMER + MAX_USER_TIMERS - 1;
|
||||
//const USER_TOKEN: usize = LAST_USER_TIMER + 1;
|
||||
/// Maximum number of tokens a handler can use
|
||||
pub const TOKENS_PER_HANDLER: usize = 16384;
|
||||
|
||||
/// Messages used to communicate with the event loop from other threads.
|
||||
pub enum IoMessage<Message> where Message: Send + Sized {
|
||||
#[derive(Clone)]
|
||||
pub enum IoMessage<Message> where Message: Send + Clone + Sized {
|
||||
/// Shutdown the event loop
|
||||
Shutdown,
|
||||
/// Register a new protocol handler.
|
||||
AddHandler {
|
||||
handler: Box<IoHandler<Message>+Send>,
|
||||
handler: Arc<IoHandler<Message>+Send>,
|
||||
},
|
||||
AddTimer {
|
||||
handler_id: HandlerId,
|
||||
token: TimerToken,
|
||||
delay: u64,
|
||||
},
|
||||
RemoveTimer {
|
||||
handler_id: HandlerId,
|
||||
token: TimerToken,
|
||||
},
|
||||
RegisterStream {
|
||||
handler_id: HandlerId,
|
||||
token: StreamToken,
|
||||
},
|
||||
UpdateStreamRegistration {
|
||||
handler_id: HandlerId,
|
||||
token: StreamToken,
|
||||
},
|
||||
/// Broadcast a message across all protocol handlers.
|
||||
UserMessage(Message)
|
||||
}
|
||||
|
||||
/// IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem.
|
||||
pub struct IoContext<'s, Message> where Message: Send + 'static {
|
||||
timers: &'s mut Slab<UserTimer>,
|
||||
/// Low leve MIO Event loop for custom handler registration.
|
||||
pub event_loop: &'s mut EventLoop<IoManager<Message>>,
|
||||
pub struct IoContext<Message> where Message: Send + Clone + 'static {
|
||||
channel: IoChannel<Message>,
|
||||
handler: HandlerId,
|
||||
}
|
||||
|
||||
impl<'s, Message> IoContext<'s, Message> where Message: Send + 'static {
|
||||
impl<Message> IoContext<Message> where Message: Send + Clone + 'static {
|
||||
/// Create a new IO access point. Takes references to all the data that can be updated within the IO handler.
|
||||
fn new(event_loop: &'s mut EventLoop<IoManager<Message>>, timers: &'s mut Slab<UserTimer>) -> IoContext<'s, Message> {
|
||||
pub fn new(channel: IoChannel<Message>, handler: HandlerId) -> IoContext<Message> {
|
||||
IoContext {
|
||||
event_loop: event_loop,
|
||||
timers: timers,
|
||||
handler: handler,
|
||||
channel: channel,
|
||||
}
|
||||
}
|
||||
|
||||
/// Register a new IO timer. Returns a new timer token. 'IoHandler::timeout' will be called with the token.
|
||||
pub fn register_timer(&mut self, ms: u64) -> Result<TimerToken, UtilError> {
|
||||
match self.timers.insert(UserTimer {
|
||||
/// Register a new IO timer. 'IoHandler::timeout' will be called with the token.
|
||||
pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), UtilError> {
|
||||
try!(self.channel.send_io(IoMessage::AddTimer {
|
||||
token: token,
|
||||
delay: ms,
|
||||
}) {
|
||||
Ok(token) => {
|
||||
self.event_loop.timeout_ms(token, ms).expect("Error registering user timer");
|
||||
Ok(token.as_usize())
|
||||
},
|
||||
_ => { panic!("Max timers reached") }
|
||||
}
|
||||
handler_id: self.handler,
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Delete a timer.
|
||||
pub fn clear_timer(&self, token: TimerToken) -> Result<(), UtilError> {
|
||||
try!(self.channel.send_io(IoMessage::RemoveTimer {
|
||||
token: token,
|
||||
handler_id: self.handler,
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
/// Register a new IO stream.
|
||||
pub fn register_stream(&self, token: StreamToken) -> Result<(), UtilError> {
|
||||
try!(self.channel.send_io(IoMessage::RegisterStream {
|
||||
token: token,
|
||||
handler_id: self.handler,
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reregister an IO stream.
|
||||
pub fn update_registration(&self, token: StreamToken) -> Result<(), UtilError> {
|
||||
try!(self.channel.send_io(IoMessage::UpdateStreamRegistration {
|
||||
token: token,
|
||||
handler_id: self.handler,
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Broadcast a message to other IO clients
|
||||
pub fn message(&mut self, message: Message) {
|
||||
match self.event_loop.channel().send(IoMessage::UserMessage(message)) {
|
||||
Ok(_) => {}
|
||||
Err(e) => { panic!("Error sending io message {:?}", e); }
|
||||
}
|
||||
pub fn message(&self, message: Message) {
|
||||
self.channel.send(message).expect("Error seding message");
|
||||
}
|
||||
|
||||
/// Get message channel
|
||||
pub fn channel(&self) -> IoChannel<Message> {
|
||||
self.channel.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct UserTimer {
|
||||
delay: u64,
|
||||
timeout: Timeout,
|
||||
}
|
||||
|
||||
/// Root IO handler. Manages user handlers, messages and IO timers.
|
||||
pub struct IoManager<Message> where Message: Send {
|
||||
timers: Slab<UserTimer>,
|
||||
handlers: Vec<Box<IoHandler<Message>>>,
|
||||
pub struct IoManager<Message> where Message: Send + Sync {
|
||||
timers: Arc<RwLock<HashMap<HandlerId, UserTimer>>>,
|
||||
handlers: Vec<Arc<IoHandler<Message>>>,
|
||||
_workers: Vec<Worker>,
|
||||
worker_channel: chase_lev::Worker<Work<Message>>,
|
||||
work_ready: Arc<Condvar>,
|
||||
}
|
||||
|
||||
impl<Message> IoManager<Message> where Message: Send + 'static {
|
||||
impl<Message> IoManager<Message> where Message: Send + Sync + Clone + 'static {
|
||||
/// Creates a new instance and registers it with the event loop.
|
||||
pub fn start(event_loop: &mut EventLoop<IoManager<Message>>) -> Result<(), UtilError> {
|
||||
let (worker, stealer) = chase_lev::deque();
|
||||
let num_workers = 4;
|
||||
let work_ready_mutex = Arc::new(Mutex::new(()));
|
||||
let work_ready = Arc::new(Condvar::new());
|
||||
let workers = (0..num_workers).map(|i|
|
||||
Worker::new(i, stealer.clone(), IoChannel::new(event_loop.channel()), work_ready.clone(), work_ready_mutex.clone())).collect();
|
||||
|
||||
let mut io = IoManager {
|
||||
timers: Slab::new_starting_at(Token(USER_TIMER), MAX_USER_TIMERS),
|
||||
timers: Arc::new(RwLock::new(HashMap::new())),
|
||||
handlers: Vec::new(),
|
||||
worker_channel: worker,
|
||||
_workers: workers,
|
||||
work_ready: work_ready,
|
||||
};
|
||||
try!(event_loop.run(&mut io));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<Message> Handler for IoManager<Message> where Message: Send + 'static {
|
||||
impl<Message> Handler for IoManager<Message> where Message: Send + Clone + Sync + 'static {
|
||||
type Timeout = Token;
|
||||
type Message = IoMessage<Message>;
|
||||
|
||||
fn ready(&mut self, event_loop: &mut EventLoop<Self>, token: Token, events: EventSet) {
|
||||
fn ready(&mut self, _event_loop: &mut EventLoop<Self>, token: Token, events: EventSet) {
|
||||
let handler_index = token.as_usize() / TOKENS_PER_HANDLER;
|
||||
let token_id = token.as_usize() % TOKENS_PER_HANDLER;
|
||||
if handler_index >= self.handlers.len() {
|
||||
panic!("Unexpected stream token: {}", token.as_usize());
|
||||
}
|
||||
let handler = self.handlers[handler_index].clone();
|
||||
|
||||
if events.is_hup() {
|
||||
for h in self.handlers.iter_mut() {
|
||||
h.stream_hup(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize());
|
||||
}
|
||||
}
|
||||
else if events.is_readable() {
|
||||
for h in self.handlers.iter_mut() {
|
||||
h.stream_readable(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize());
|
||||
}
|
||||
}
|
||||
else if events.is_writable() {
|
||||
for h in self.handlers.iter_mut() {
|
||||
h.stream_writable(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize());
|
||||
self.worker_channel.push(Work { work_type: WorkType::Hup, token: token_id, handler: handler.clone(), handler_id: handler_index });
|
||||
}
|
||||
else {
|
||||
if events.is_readable() {
|
||||
self.worker_channel.push(Work { work_type: WorkType::Readable, token: token_id, handler: handler.clone(), handler_id: handler_index });
|
||||
}
|
||||
if events.is_writable() {
|
||||
self.worker_channel.push(Work { work_type: WorkType::Writable, token: token_id, handler: handler.clone(), handler_id: handler_index });
|
||||
}
|
||||
}
|
||||
self.work_ready.notify_all();
|
||||
}
|
||||
|
||||
fn timeout(&mut self, event_loop: &mut EventLoop<Self>, token: Token) {
|
||||
match token.as_usize() {
|
||||
USER_TIMER ... LAST_USER_TIMER => {
|
||||
let delay = {
|
||||
let timer = self.timers.get_mut(token).expect("Unknown user timer token");
|
||||
timer.delay
|
||||
};
|
||||
for h in self.handlers.iter_mut() {
|
||||
h.timeout(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize());
|
||||
}
|
||||
event_loop.timeout_ms(token, delay).expect("Error re-registering user timer");
|
||||
}
|
||||
_ => { // Just pass the event down. IoHandler is supposed to re-register it if required.
|
||||
for h in self.handlers.iter_mut() {
|
||||
h.timeout(&mut IoContext::new(event_loop, &mut self.timers), token.as_usize());
|
||||
}
|
||||
}
|
||||
let handler_index = token.as_usize() / TOKENS_PER_HANDLER;
|
||||
let token_id = token.as_usize() % TOKENS_PER_HANDLER;
|
||||
if handler_index >= self.handlers.len() {
|
||||
panic!("Unexpected timer token: {}", token.as_usize());
|
||||
}
|
||||
if let Some(timer) = self.timers.read().unwrap().get(&token.as_usize()) {
|
||||
event_loop.timeout_ms(token, timer.delay).expect("Error re-registering user timer");
|
||||
let handler = self.handlers[handler_index].clone();
|
||||
self.worker_channel.push(Work { work_type: WorkType::Timeout, token: token_id, handler: handler, handler_id: handler_index });
|
||||
self.work_ready.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
fn notify(&mut self, event_loop: &mut EventLoop<Self>, msg: Self::Message) {
|
||||
let mut m = msg;
|
||||
match m {
|
||||
match msg {
|
||||
IoMessage::Shutdown => event_loop.shutdown(),
|
||||
IoMessage::AddHandler {
|
||||
handler,
|
||||
} => {
|
||||
self.handlers.push(handler);
|
||||
self.handlers.last_mut().unwrap().initialize(&mut IoContext::new(event_loop, &mut self.timers));
|
||||
IoMessage::AddHandler { handler } => {
|
||||
let handler_id = {
|
||||
self.handlers.push(handler.clone());
|
||||
self.handlers.len() - 1
|
||||
};
|
||||
handler.initialize(&IoContext::new(IoChannel::new(event_loop.channel()), handler_id));
|
||||
},
|
||||
IoMessage::UserMessage(ref mut data) => {
|
||||
for h in self.handlers.iter_mut() {
|
||||
h.message(&mut IoContext::new(event_loop, &mut self.timers), data);
|
||||
IoMessage::AddTimer { handler_id, token, delay } => {
|
||||
let timer_id = token + handler_id * TOKENS_PER_HANDLER;
|
||||
let timeout = event_loop.timeout_ms(Token(timer_id), delay).expect("Error registering user timer");
|
||||
self.timers.write().unwrap().insert(timer_id, UserTimer { delay: delay, timeout: timeout });
|
||||
},
|
||||
IoMessage::RemoveTimer { handler_id, token } => {
|
||||
let timer_id = token + handler_id * TOKENS_PER_HANDLER;
|
||||
if let Some(timer) = self.timers.write().unwrap().remove(&timer_id) {
|
||||
event_loop.clear_timeout(timer.timeout);
|
||||
}
|
||||
},
|
||||
IoMessage::RegisterStream { handler_id, token } => {
|
||||
let handler = self.handlers.get(handler_id).expect("Unknown handler id").clone();
|
||||
handler.register_stream(token, Token(token + handler_id * TOKENS_PER_HANDLER), event_loop);
|
||||
},
|
||||
IoMessage::UpdateStreamRegistration { handler_id, token } => {
|
||||
let handler = self.handlers.get(handler_id).expect("Unknown handler id").clone();
|
||||
handler.update_stream(token, Token(token + handler_id * TOKENS_PER_HANDLER), event_loop);
|
||||
},
|
||||
IoMessage::UserMessage(data) => {
|
||||
for n in 0 .. self.handlers.len() {
|
||||
let handler = self.handlers[n].clone();
|
||||
self.worker_channel.push(Work { work_type: WorkType::Message(data.clone()), token: 0, handler: handler, handler_id: n });
|
||||
}
|
||||
self.work_ready.notify_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -150,11 +231,19 @@ impl<Message> Handler for IoManager<Message> where Message: Send + 'static {
|
||||
|
||||
/// Allows sending messages into the event loop. All the IO handlers will get the message
|
||||
/// in the `message` callback.
|
||||
pub struct IoChannel<Message> where Message: Send {
|
||||
pub struct IoChannel<Message> where Message: Send + Clone{
|
||||
channel: Option<Sender<IoMessage<Message>>>
|
||||
}
|
||||
|
||||
impl<Message> IoChannel<Message> where Message: Send {
|
||||
impl<Message> Clone for IoChannel<Message> where Message: Send + Clone {
|
||||
fn clone(&self) -> IoChannel<Message> {
|
||||
IoChannel {
|
||||
channel: self.channel.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Message> IoChannel<Message> where Message: Send + Clone {
|
||||
/// Send a msessage through the channel
|
||||
pub fn send(&self, message: Message) -> Result<(), IoError> {
|
||||
if let Some(ref channel) = self.channel {
|
||||
@ -163,20 +252,31 @@ impl<Message> IoChannel<Message> where Message: Send {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Send low level io message
|
||||
pub fn send_io(&self, message: IoMessage<Message>) -> Result<(), IoError> {
|
||||
if let Some(ref channel) = self.channel {
|
||||
try!(channel.send(message))
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Create a new channel to connected to event loop.
|
||||
pub fn disconnected() -> IoChannel<Message> {
|
||||
IoChannel { channel: None }
|
||||
}
|
||||
|
||||
fn new(channel: Sender<IoMessage<Message>>) -> IoChannel<Message> {
|
||||
IoChannel { channel: Some(channel) }
|
||||
}
|
||||
}
|
||||
|
||||
/// General IO Service. Starts an event loop and dispatches IO requests.
|
||||
/// 'Message' is a notification message type
|
||||
pub struct IoService<Message> where Message: Send + 'static {
|
||||
pub struct IoService<Message> where Message: Send + Sync + Clone + 'static {
|
||||
thread: Option<JoinHandle<()>>,
|
||||
host_channel: Sender<IoMessage<Message>>
|
||||
host_channel: Sender<IoMessage<Message>>,
|
||||
}
|
||||
|
||||
impl<Message> IoService<Message> where Message: Send + 'static {
|
||||
impl<Message> IoService<Message> where Message: Send + Sync + Clone + 'static {
|
||||
/// Starts IO event loop
|
||||
pub fn start() -> Result<IoService<Message>, UtilError> {
|
||||
let mut event_loop = EventLoop::new().unwrap();
|
||||
@ -191,7 +291,7 @@ impl<Message> IoService<Message> where Message: Send + 'static {
|
||||
}
|
||||
|
||||
/// Regiter a IO hadnler with the event loop.
|
||||
pub fn register_handler(&mut self, handler: Box<IoHandler<Message>+Send>) -> Result<(), IoError> {
|
||||
pub fn register_handler(&mut self, handler: Arc<IoHandler<Message>+Send>) -> Result<(), IoError> {
|
||||
try!(self.host_channel.send(IoMessage::AddHandler {
|
||||
handler: handler,
|
||||
}));
|
||||
@ -210,10 +310,10 @@ impl<Message> IoService<Message> where Message: Send + 'static {
|
||||
}
|
||||
}
|
||||
|
||||
impl<Message> Drop for IoService<Message> where Message: Send {
|
||||
impl<Message> Drop for IoService<Message> where Message: Send + Sync + Clone {
|
||||
fn drop(&mut self) {
|
||||
self.host_channel.send(IoMessage::Shutdown).unwrap();
|
||||
self.thread.take().unwrap().join().unwrap();
|
||||
self.thread.take().unwrap().join().ok();
|
||||
}
|
||||
}
|
||||
|
||||
|
99
util/src/io/worker.rs
Normal file
99
util/src/io/worker.rs
Normal file
@ -0,0 +1,99 @@
|
||||
use std::sync::*;
|
||||
use std::mem;
|
||||
use std::thread::{JoinHandle, self};
|
||||
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
|
||||
use crossbeam::sync::chase_lev;
|
||||
use io::service::{HandlerId, IoChannel, IoContext};
|
||||
use io::{IoHandler};
|
||||
|
||||
pub enum WorkType<Message> {
|
||||
Readable,
|
||||
Writable,
|
||||
Hup,
|
||||
Timeout,
|
||||
Message(Message)
|
||||
}
|
||||
|
||||
pub struct Work<Message> {
|
||||
pub work_type: WorkType<Message>,
|
||||
pub token: usize,
|
||||
pub handler_id: HandlerId,
|
||||
pub handler: Arc<IoHandler<Message>>,
|
||||
}
|
||||
|
||||
/// An IO worker thread
|
||||
/// Sorts them ready for blockchain insertion.
|
||||
pub struct Worker {
|
||||
thread: Option<JoinHandle<()>>,
|
||||
wait: Arc<Condvar>,
|
||||
deleting: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl Worker {
|
||||
/// Creates a new worker instance.
|
||||
pub fn new<Message>(index: usize,
|
||||
stealer: chase_lev::Stealer<Work<Message>>,
|
||||
channel: IoChannel<Message>,
|
||||
wait: Arc<Condvar>,
|
||||
wait_mutex: Arc<Mutex<()>>) -> Worker
|
||||
where Message: Send + Sync + Clone + 'static {
|
||||
let deleting = Arc::new(AtomicBool::new(false));
|
||||
let mut worker = Worker {
|
||||
thread: None,
|
||||
wait: wait.clone(),
|
||||
deleting: deleting.clone(),
|
||||
};
|
||||
worker.thread = Some(thread::Builder::new().name(format!("IO Worker #{}", index)).spawn(
|
||||
move || Worker::work_loop(stealer, channel.clone(), wait, wait_mutex.clone(), deleting))
|
||||
.expect("Error creating worker thread"));
|
||||
worker
|
||||
}
|
||||
|
||||
fn work_loop<Message>(stealer: chase_lev::Stealer<Work<Message>>,
|
||||
channel: IoChannel<Message>, wait: Arc<Condvar>,
|
||||
wait_mutex: Arc<Mutex<()>>,
|
||||
deleting: Arc<AtomicBool>)
|
||||
where Message: Send + Sync + Clone + 'static {
|
||||
while !deleting.load(AtomicOrdering::Relaxed) {
|
||||
{
|
||||
let lock = wait_mutex.lock().unwrap();
|
||||
let _ = wait.wait(lock).unwrap();
|
||||
if deleting.load(AtomicOrdering::Relaxed) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
while let chase_lev::Steal::Data(work) = stealer.steal() {
|
||||
Worker::do_work(work, channel.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn do_work<Message>(work: Work<Message>, channel: IoChannel<Message>) where Message: Send + Sync + Clone + 'static {
|
||||
match work.work_type {
|
||||
WorkType::Readable => {
|
||||
work.handler.stream_readable(&IoContext::new(channel, work.handler_id), work.token);
|
||||
},
|
||||
WorkType::Writable => {
|
||||
work.handler.stream_writable(&IoContext::new(channel, work.handler_id), work.token);
|
||||
}
|
||||
WorkType::Hup => {
|
||||
work.handler.stream_hup(&IoContext::new(channel, work.handler_id), work.token);
|
||||
}
|
||||
WorkType::Timeout => {
|
||||
work.handler.timeout(&IoContext::new(channel, work.handler_id), work.token);
|
||||
}
|
||||
WorkType::Message(message) => {
|
||||
work.handler.message(&IoContext::new(channel, work.handler_id), &message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Worker {
|
||||
fn drop(&mut self) {
|
||||
self.deleting.store(true, AtomicOrdering::Relaxed);
|
||||
self.wait.notify_all();
|
||||
let thread = mem::replace(&mut self.thread, None).unwrap();
|
||||
thread.join().ok();
|
||||
}
|
||||
}
|
@ -34,6 +34,16 @@ impl JournalDB {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new instance given a shared `backing` database.
|
||||
pub fn new_with_arc(backing: Arc<DB>) -> JournalDB {
|
||||
JournalDB {
|
||||
forward: OverlayDB::new_with_arc(backing.clone()),
|
||||
backing: backing,
|
||||
inserts: vec![],
|
||||
removes: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new instance with an anonymous temporary database.
|
||||
pub fn new_temp() -> JournalDB {
|
||||
let mut dir = env::temp_dir();
|
||||
@ -96,7 +106,7 @@ impl JournalDB {
|
||||
})) {
|
||||
let rlp = Rlp::new(&rlp_data);
|
||||
let to_remove: Vec<H256> = rlp.val_at(if canon_id == rlp.val_at(0) {2} else {1});
|
||||
for i in to_remove.iter() {
|
||||
for i in &to_remove {
|
||||
self.forward.remove(i);
|
||||
}
|
||||
try!(self.backing.delete(&last));
|
||||
|
@ -11,18 +11,18 @@ pub fn clean(s: &str) -> &str {
|
||||
|
||||
fn u256_from_str(s: &str) -> U256 {
|
||||
if s.len() >= 2 && &s[0..2] == "0x" {
|
||||
U256::from_str(&s[2..]).unwrap_or(U256::from(0))
|
||||
U256::from_str(&s[2..]).unwrap_or_else(|_| U256::zero())
|
||||
} else {
|
||||
U256::from_dec_str(s).unwrap_or(U256::from(0))
|
||||
U256::from_dec_str(s).unwrap_or_else(|_| U256::zero())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromJson for Bytes {
|
||||
fn from_json(json: &Json) -> Self {
|
||||
match json {
|
||||
&Json::String(ref s) => match s.len() % 2 {
|
||||
0 => FromHex::from_hex(clean(s)).unwrap_or(vec![]),
|
||||
_ => FromHex::from_hex(&("0".to_string() + &(clean(s).to_string()))[..]).unwrap_or(vec![]),
|
||||
match *json {
|
||||
Json::String(ref s) => match s.len() % 2 {
|
||||
0 => FromHex::from_hex(clean(s)).unwrap_or_else(|_| vec![]),
|
||||
_ => FromHex::from_hex(&("0".to_owned() + &(clean(s).to_owned()))[..]).unwrap_or_else(|_| vec![]),
|
||||
},
|
||||
_ => vec![],
|
||||
}
|
||||
@ -31,8 +31,8 @@ impl FromJson for Bytes {
|
||||
|
||||
impl FromJson for BTreeMap<H256, H256> {
|
||||
fn from_json(json: &Json) -> Self {
|
||||
match json {
|
||||
&Json::Object(ref o) => o.iter().map(|(key, value)| (x!(&u256_from_str(key)), x!(&U256::from_json(value)))).collect(),
|
||||
match *json {
|
||||
Json::Object(ref o) => o.iter().map(|(key, value)| (x!(&u256_from_str(key)), x!(&U256::from_json(value)))).collect(),
|
||||
_ => BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
@ -40,8 +40,8 @@ impl FromJson for BTreeMap<H256, H256> {
|
||||
|
||||
impl<T> FromJson for Vec<T> where T: FromJson {
|
||||
fn from_json(json: &Json) -> Self {
|
||||
match json {
|
||||
&Json::Array(ref o) => o.iter().map(|x|T::from_json(x)).collect(),
|
||||
match *json {
|
||||
Json::Array(ref o) => o.iter().map(|x|T::from_json(x)).collect(),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
@ -49,9 +49,9 @@ impl<T> FromJson for Vec<T> where T: FromJson {
|
||||
|
||||
impl<T> FromJson for Option<T> where T: FromJson {
|
||||
fn from_json(json: &Json) -> Self {
|
||||
match json {
|
||||
&Json::String(ref o) if o.is_empty() => None,
|
||||
&Json::Null => None,
|
||||
match *json {
|
||||
Json::String(ref o) if o.is_empty() => None,
|
||||
Json::Null => None,
|
||||
_ => Some(FromJson::from_json(json)),
|
||||
}
|
||||
}
|
||||
@ -135,4 +135,4 @@ fn option_types() {
|
||||
assert_eq!(None, v);
|
||||
let v: Option<u16> = xjson!(&j["empty"]);
|
||||
assert_eq!(None, v);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,9 @@
|
||||
#![feature(op_assign_traits)]
|
||||
#![feature(augmented_assignments)]
|
||||
#![feature(associated_consts)]
|
||||
#![feature(plugin)]
|
||||
#![plugin(clippy)]
|
||||
#![allow(needless_range_loop, match_bool)]
|
||||
//! Ethcore-util library
|
||||
//!
|
||||
//! ### Rust version:
|
||||
@ -51,6 +54,7 @@ extern crate crypto as rcrypto;
|
||||
extern crate secp256k1;
|
||||
extern crate arrayvec;
|
||||
extern crate elastic_array;
|
||||
extern crate crossbeam;
|
||||
|
||||
/// TODO [Gav Wood] Please document me
|
||||
pub mod standard;
|
||||
|
@ -18,13 +18,13 @@ impl<T> Diff<T> where T: Eq {
|
||||
pub fn new(pre: T, post: T) -> Self { if pre == post { Diff::Same } else { Diff::Changed(pre, post) } }
|
||||
|
||||
/// Get the before value, if there is one.
|
||||
pub fn pre(&self) -> Option<&T> { match self { &Diff::Died(ref x) | &Diff::Changed(ref x, _) => Some(x), _ => None } }
|
||||
pub fn pre(&self) -> Option<&T> { match *self { Diff::Died(ref x) | Diff::Changed(ref x, _) => Some(x), _ => None } }
|
||||
|
||||
/// Get the after value, if there is one.
|
||||
pub fn post(&self) -> Option<&T> { match self { &Diff::Born(ref x) | &Diff::Changed(_, ref x) => Some(x), _ => None } }
|
||||
pub fn post(&self) -> Option<&T> { match *self { Diff::Born(ref x) | Diff::Changed(_, ref x) => Some(x), _ => None } }
|
||||
|
||||
/// Determine whether there was a change or not.
|
||||
pub fn is_same(&self) -> bool { match self { &Diff::Same => true, _ => false }}
|
||||
pub fn is_same(&self) -> bool { match *self { Diff::Same => true, _ => false }}
|
||||
}
|
||||
|
||||
#[derive(PartialEq,Eq,Clone,Copy)]
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::collections::VecDeque;
|
||||
use mio::{Handler, Token, EventSet, EventLoop, Timeout, PollOpt, TryRead, TryWrite};
|
||||
use mio::{Handler, Token, EventSet, EventLoop, PollOpt, TryRead, TryWrite};
|
||||
use mio::tcp::*;
|
||||
use hash::*;
|
||||
use sha3::*;
|
||||
@ -7,6 +7,7 @@ use bytes::*;
|
||||
use rlp::*;
|
||||
use std::io::{self, Cursor, Read};
|
||||
use error::*;
|
||||
use io::{IoContext, StreamToken};
|
||||
use network::error::NetworkError;
|
||||
use network::handshake::Handshake;
|
||||
use crypto;
|
||||
@ -17,11 +18,12 @@ use rcrypto::buffer::*;
|
||||
use tiny_keccak::Keccak;
|
||||
|
||||
const ENCRYPTED_HEADER_LEN: usize = 32;
|
||||
const RECIEVE_PAYLOAD_TIMEOUT: u64 = 30000;
|
||||
|
||||
/// Low level tcp connection
|
||||
pub struct Connection {
|
||||
/// Connection id (token)
|
||||
pub token: Token,
|
||||
pub token: StreamToken,
|
||||
/// Network socket
|
||||
pub socket: TcpStream,
|
||||
/// Receive buffer
|
||||
@ -45,14 +47,14 @@ pub enum WriteStatus {
|
||||
|
||||
impl Connection {
|
||||
/// Create a new connection with given id and socket.
|
||||
pub fn new(token: Token, socket: TcpStream) -> Connection {
|
||||
pub fn new(token: StreamToken, socket: TcpStream) -> Connection {
|
||||
Connection {
|
||||
token: token,
|
||||
socket: socket,
|
||||
send_queue: VecDeque::new(),
|
||||
rec_buf: Bytes::new(),
|
||||
rec_size: 0,
|
||||
interest: EventSet::hup(),
|
||||
interest: EventSet::hup() | EventSet::readable(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,7 +88,7 @@ impl Connection {
|
||||
|
||||
/// Add a packet to send queue.
|
||||
pub fn send(&mut self, data: Bytes) {
|
||||
if data.len() != 0 {
|
||||
if !data.is_empty() {
|
||||
self.send_queue.push_back(Cursor::new(data));
|
||||
}
|
||||
if !self.interest.is_writable() {
|
||||
@ -132,20 +134,19 @@ impl Connection {
|
||||
}
|
||||
|
||||
/// Register this connection with the IO event loop.
|
||||
pub fn register<Host: Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> io::Result<()> {
|
||||
trace!(target: "net", "connection register; token={:?}", self.token);
|
||||
self.interest.insert(EventSet::readable());
|
||||
event_loop.register(&self.socket, self.token, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| {
|
||||
error!("Failed to register {:?}, {:?}", self.token, e);
|
||||
pub fn register_socket<Host: Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> io::Result<()> {
|
||||
trace!(target: "net", "connection register; token={:?}", reg);
|
||||
event_loop.register(&self.socket, reg, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| {
|
||||
error!("Failed to register {:?}, {:?}", reg, e);
|
||||
Err(e)
|
||||
})
|
||||
}
|
||||
|
||||
/// Update connection registration. Should be called at the end of the IO handler.
|
||||
pub fn reregister<Host: Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> io::Result<()> {
|
||||
trace!(target: "net", "connection reregister; token={:?}", self.token);
|
||||
event_loop.reregister( &self.socket, self.token, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| {
|
||||
error!("Failed to reregister {:?}, {:?}", self.token, e);
|
||||
pub fn update_socket<Host: Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> io::Result<()> {
|
||||
trace!(target: "net", "connection reregister; token={:?}", reg);
|
||||
event_loop.reregister( &self.socket, reg, self.interest, PollOpt::edge() | PollOpt::oneshot()).or_else(|e| {
|
||||
error!("Failed to reregister {:?}, {:?}", reg, e);
|
||||
Err(e)
|
||||
})
|
||||
}
|
||||
@ -182,8 +183,6 @@ pub struct EncryptedConnection {
|
||||
ingress_mac: Keccak,
|
||||
/// Read state
|
||||
read_state: EncryptedConnectionState,
|
||||
/// Disconnect timeout
|
||||
idle_timeout: Option<Timeout>,
|
||||
/// Protocol id for the last received packet
|
||||
protocol_id: u16,
|
||||
/// Payload expected to be received for the last header.
|
||||
@ -192,7 +191,7 @@ pub struct EncryptedConnection {
|
||||
|
||||
impl EncryptedConnection {
|
||||
/// Create an encrypted connection out of the handshake. Consumes a handshake object.
|
||||
pub fn new(handshake: Handshake) -> Result<EncryptedConnection, UtilError> {
|
||||
pub fn new(mut handshake: Handshake) -> Result<EncryptedConnection, UtilError> {
|
||||
let shared = try!(crypto::ecdh::agree(handshake.ecdhe.secret(), &handshake.remote_public));
|
||||
let mut nonce_material = H512::new();
|
||||
if handshake.originated {
|
||||
@ -227,6 +226,7 @@ impl EncryptedConnection {
|
||||
ingress_mac.update(&mac_material);
|
||||
ingress_mac.update(if handshake.originated { &handshake.ack_cipher } else { &handshake.auth_cipher });
|
||||
|
||||
handshake.connection.expect(ENCRYPTED_HEADER_LEN);
|
||||
Ok(EncryptedConnection {
|
||||
connection: handshake.connection,
|
||||
encoder: encoder,
|
||||
@ -235,7 +235,6 @@ impl EncryptedConnection {
|
||||
egress_mac: egress_mac,
|
||||
ingress_mac: ingress_mac,
|
||||
read_state: EncryptedConnectionState::Header,
|
||||
idle_timeout: None,
|
||||
protocol_id: 0,
|
||||
payload_len: 0
|
||||
})
|
||||
@ -337,16 +336,14 @@ impl EncryptedConnection {
|
||||
}
|
||||
|
||||
/// Readable IO handler. Tracker receive status and returns decoded packet if avaialable.
|
||||
pub fn readable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<Option<Packet>, UtilError> {
|
||||
self.idle_timeout.map(|t| event_loop.clear_timeout(t));
|
||||
pub fn readable<Message>(&mut self, io: &IoContext<Message>) -> Result<Option<Packet>, UtilError> where Message: Send + Clone{
|
||||
io.clear_timer(self.connection.token).unwrap();
|
||||
match self.read_state {
|
||||
EncryptedConnectionState::Header => {
|
||||
match try!(self.connection.readable()) {
|
||||
Some(data) => {
|
||||
try!(self.read_header(&data));
|
||||
},
|
||||
None => {}
|
||||
};
|
||||
if let Some(data) = try!(self.connection.readable()) {
|
||||
try!(self.read_header(&data));
|
||||
try!(io.register_timer(self.connection.token, RECIEVE_PAYLOAD_TIMEOUT));
|
||||
}
|
||||
Ok(None)
|
||||
},
|
||||
EncryptedConnectionState::Payload => {
|
||||
@ -363,24 +360,15 @@ impl EncryptedConnection {
|
||||
}
|
||||
|
||||
/// Writable IO handler. Processes send queeue.
|
||||
pub fn writable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
|
||||
self.idle_timeout.map(|t| event_loop.clear_timeout(t));
|
||||
pub fn writable<Message>(&mut self, io: &IoContext<Message>) -> Result<(), UtilError> where Message: Send + Clone {
|
||||
io.clear_timer(self.connection.token).unwrap();
|
||||
try!(self.connection.writable());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Register this connection with the event handler.
|
||||
pub fn register<Host:Handler<Timeout=Token>>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
|
||||
self.connection.expect(ENCRYPTED_HEADER_LEN);
|
||||
self.idle_timeout.map(|t| event_loop.clear_timeout(t));
|
||||
self.idle_timeout = event_loop.timeout_ms(self.connection.token, 1800).ok();
|
||||
try!(self.connection.reregister(event_loop));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Update connection registration. This should be called at the end of the event loop.
|
||||
pub fn reregister<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
|
||||
try!(self.connection.reregister(event_loop));
|
||||
pub fn update_socket<Host:Handler>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
|
||||
try!(self.connection.update_socket(reg, event_loop));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ impl Discovery {
|
||||
discovery_round: 0,
|
||||
discovery_id: NodeId::new(),
|
||||
discovery_nodes: HashSet::new(),
|
||||
node_buckets: (0..NODE_BINS).map(|x| NodeBucket::new(x)).collect(),
|
||||
node_buckets: (0..NODE_BINS).map(NodeBucket::new).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -122,7 +122,8 @@ impl Discovery {
|
||||
ret
|
||||
}
|
||||
|
||||
fn nearest_node_entries<'b>(source: &NodeId, target: &NodeId, buckets: &'b Vec<NodeBucket>) -> Vec<&'b NodeId>
|
||||
#[allow(cyclomatic_complexity)]
|
||||
fn nearest_node_entries<'b>(source: &NodeId, target: &NodeId, buckets: &'b [NodeBucket]) -> Vec<&'b NodeId>
|
||||
{
|
||||
// send ALPHA FindNode packets to nodes we know, closest to target
|
||||
const LAST_BIN: u32 = NODE_BINS - 1;
|
||||
@ -136,21 +137,21 @@ impl Discovery {
|
||||
if head > 1 && tail != LAST_BIN {
|
||||
while head != tail && head < NODE_BINS && count < BUCKET_SIZE
|
||||
{
|
||||
for n in buckets[head as usize].nodes.iter()
|
||||
for n in &buckets[head as usize].nodes
|
||||
{
|
||||
if count < BUCKET_SIZE {
|
||||
count += 1;
|
||||
found.entry(Discovery::distance(target, &n)).or_insert(Vec::new()).push(n);
|
||||
found.entry(Discovery::distance(target, &n)).or_insert_with(Vec::new).push(n);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if count < BUCKET_SIZE && tail != 0 {
|
||||
for n in buckets[tail as usize].nodes.iter() {
|
||||
for n in &buckets[tail as usize].nodes {
|
||||
if count < BUCKET_SIZE {
|
||||
count += 1;
|
||||
found.entry(Discovery::distance(target, &n)).or_insert(Vec::new()).push(n);
|
||||
found.entry(Discovery::distance(target, &n)).or_insert_with(Vec::new).push(n);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
@ -166,10 +167,10 @@ impl Discovery {
|
||||
}
|
||||
else if head < 2 {
|
||||
while head < NODE_BINS && count < BUCKET_SIZE {
|
||||
for n in buckets[head as usize].nodes.iter() {
|
||||
for n in &buckets[head as usize].nodes {
|
||||
if count < BUCKET_SIZE {
|
||||
count += 1;
|
||||
found.entry(Discovery::distance(target, &n)).or_insert(Vec::new()).push(n);
|
||||
found.entry(Discovery::distance(target, &n)).or_insert_with(Vec::new).push(n);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
@ -180,10 +181,10 @@ impl Discovery {
|
||||
}
|
||||
else {
|
||||
while tail > 0 && count < BUCKET_SIZE {
|
||||
for n in buckets[tail as usize].nodes.iter() {
|
||||
for n in &buckets[tail as usize].nodes {
|
||||
if count < BUCKET_SIZE {
|
||||
count += 1;
|
||||
found.entry(Discovery::distance(target, &n)).or_insert(Vec::new()).push(n);
|
||||
found.entry(Discovery::distance(target, &n)).or_insert_with(Vec::new).push(n);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
|
@ -19,11 +19,17 @@ pub enum DisconnectReason
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// Network error.
|
||||
pub enum NetworkError {
|
||||
/// Authentication error.
|
||||
Auth,
|
||||
/// Unrecognised protocol.
|
||||
BadProtocol,
|
||||
/// Peer not found.
|
||||
PeerNotFound,
|
||||
/// Peer is diconnected.
|
||||
Disconnect(DisconnectReason),
|
||||
/// Socket IO error.
|
||||
Io(IoError),
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ use network::host::{HostInfo};
|
||||
use network::node::NodeId;
|
||||
use error::*;
|
||||
use network::error::NetworkError;
|
||||
use io::{IoContext, StreamToken};
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
enum HandshakeState {
|
||||
@ -33,8 +34,6 @@ pub struct Handshake {
|
||||
state: HandshakeState,
|
||||
/// Outgoing or incoming connection
|
||||
pub originated: bool,
|
||||
/// Disconnect timeout
|
||||
idle_timeout: Option<Timeout>,
|
||||
/// ECDH ephemeral
|
||||
pub ecdhe: KeyPair,
|
||||
/// Connection nonce
|
||||
@ -51,16 +50,16 @@ pub struct Handshake {
|
||||
|
||||
const AUTH_PACKET_SIZE: usize = 307;
|
||||
const ACK_PACKET_SIZE: usize = 210;
|
||||
const HANDSHAKE_TIMEOUT: u64 = 30000;
|
||||
|
||||
impl Handshake {
|
||||
/// Create a new handshake object
|
||||
pub fn new(token: Token, id: &NodeId, socket: TcpStream, nonce: &H256) -> Result<Handshake, UtilError> {
|
||||
pub fn new(token: StreamToken, id: &NodeId, socket: TcpStream, nonce: &H256) -> Result<Handshake, UtilError> {
|
||||
Ok(Handshake {
|
||||
id: id.clone(),
|
||||
connection: Connection::new(token, socket),
|
||||
originated: false,
|
||||
state: HandshakeState::New,
|
||||
idle_timeout: None,
|
||||
ecdhe: try!(KeyPair::create()),
|
||||
nonce: nonce.clone(),
|
||||
remote_public: Public::new(),
|
||||
@ -71,8 +70,9 @@ impl Handshake {
|
||||
}
|
||||
|
||||
/// Start a handhsake
|
||||
pub fn start(&mut self, host: &HostInfo, originated: bool) -> Result<(), UtilError> {
|
||||
pub fn start<Message>(&mut self, io: &IoContext<Message>, host: &HostInfo, originated: bool) -> Result<(), UtilError> where Message: Send + Clone{
|
||||
self.originated = originated;
|
||||
io.register_timer(self.connection.token, HANDSHAKE_TIMEOUT).ok();
|
||||
if originated {
|
||||
try!(self.write_auth(host));
|
||||
}
|
||||
@ -89,50 +89,48 @@ impl Handshake {
|
||||
}
|
||||
|
||||
/// Readable IO handler. Drives the state change.
|
||||
pub fn readable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>, host: &HostInfo) -> Result<(), UtilError> {
|
||||
self.idle_timeout.map(|t| event_loop.clear_timeout(t));
|
||||
pub fn readable<Message>(&mut self, io: &IoContext<Message>, host: &HostInfo) -> Result<(), UtilError> where Message: Send + Clone {
|
||||
io.clear_timer(self.connection.token).unwrap();
|
||||
match self.state {
|
||||
HandshakeState::ReadingAuth => {
|
||||
match try!(self.connection.readable()) {
|
||||
Some(data) => {
|
||||
try!(self.read_auth(host, &data));
|
||||
try!(self.write_ack());
|
||||
},
|
||||
None => {}
|
||||
if let Some(data) = try!(self.connection.readable()) {
|
||||
try!(self.read_auth(host, &data));
|
||||
try!(self.write_ack());
|
||||
};
|
||||
},
|
||||
HandshakeState::ReadingAck => {
|
||||
match try!(self.connection.readable()) {
|
||||
Some(data) => {
|
||||
try!(self.read_ack(host, &data));
|
||||
self.state = HandshakeState::StartSession;
|
||||
},
|
||||
None => {}
|
||||
if let Some(data) = try!(self.connection.readable()) {
|
||||
try!(self.read_ack(host, &data));
|
||||
self.state = HandshakeState::StartSession;
|
||||
};
|
||||
},
|
||||
HandshakeState::StartSession => {},
|
||||
_ => { panic!("Unexpected state"); }
|
||||
}
|
||||
if self.state != HandshakeState::StartSession {
|
||||
try!(self.connection.reregister(event_loop));
|
||||
try!(io.update_registration(self.connection.token));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writabe IO handler.
|
||||
pub fn writable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>, _host: &HostInfo) -> Result<(), UtilError> {
|
||||
self.idle_timeout.map(|t| event_loop.clear_timeout(t));
|
||||
pub fn writable<Message>(&mut self, io: &IoContext<Message>, _host: &HostInfo) -> Result<(), UtilError> where Message: Send + Clone {
|
||||
io.clear_timer(self.connection.token).unwrap();
|
||||
try!(self.connection.writable());
|
||||
if self.state != HandshakeState::StartSession {
|
||||
try!(self.connection.reregister(event_loop));
|
||||
io.update_registration(self.connection.token).unwrap();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Register the IO handler with the event loop
|
||||
pub fn register<Host:Handler<Timeout=Token>>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
|
||||
self.idle_timeout.map(|t| event_loop.clear_timeout(t));
|
||||
self.idle_timeout = event_loop.timeout_ms(self.connection.token, 1800).ok();
|
||||
try!(self.connection.register(event_loop));
|
||||
/// Register the socket with the event loop
|
||||
pub fn register_socket<Host:Handler<Timeout=Token>>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
|
||||
try!(self.connection.register_socket(reg, event_loop));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_socket<Host:Handler<Timeout=Token>>(&self, reg: Token, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
|
||||
try!(self.connection.update_socket(reg, event_loop));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,9 @@
|
||||
use std::mem;
|
||||
use std::net::{SocketAddr};
|
||||
use std::collections::{HashMap};
|
||||
use std::hash::{Hasher};
|
||||
use std::str::{FromStr};
|
||||
use std::sync::*;
|
||||
use std::ops::*;
|
||||
use mio::*;
|
||||
use mio::tcp::*;
|
||||
use mio::udp::*;
|
||||
@ -64,19 +65,25 @@ pub type PacketId = u8;
|
||||
pub type ProtocolId = &'static str;
|
||||
|
||||
/// Messages used to communitate with the event loop from other threads.
|
||||
pub enum NetworkIoMessage<Message> where Message: Send {
|
||||
#[derive(Clone)]
|
||||
pub enum NetworkIoMessage<Message> where Message: Send + Sync + Clone {
|
||||
/// Register a new protocol handler.
|
||||
AddHandler {
|
||||
handler: Option<Box<NetworkProtocolHandler<Message>+Send>>,
|
||||
/// Handler shared instance.
|
||||
handler: Arc<NetworkProtocolHandler<Message> + Sync>,
|
||||
/// Protocol Id.
|
||||
protocol: ProtocolId,
|
||||
/// Supported protocol versions.
|
||||
versions: Vec<u8>,
|
||||
},
|
||||
/// Send data over the network.
|
||||
Send {
|
||||
peer: PeerId,
|
||||
packet_id: PacketId,
|
||||
/// Register a new protocol timer
|
||||
AddTimer {
|
||||
/// Protocol Id.
|
||||
protocol: ProtocolId,
|
||||
data: Vec<u8>,
|
||||
/// Timer token.
|
||||
token: TimerToken,
|
||||
/// Timer delay in milliseconds.
|
||||
delay: u64,
|
||||
},
|
||||
/// User message
|
||||
User(Message),
|
||||
@ -104,46 +111,45 @@ impl Encodable for CapabilityInfo {
|
||||
}
|
||||
|
||||
/// IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem.
|
||||
pub struct NetworkContext<'s, 'io, Message> where Message: Send + 'static, 'io: 's {
|
||||
io: &'s mut IoContext<'io, NetworkIoMessage<Message>>,
|
||||
pub struct NetworkContext<'s, Message> where Message: Send + Sync + Clone + 'static, 's {
|
||||
io: &'s IoContext<NetworkIoMessage<Message>>,
|
||||
protocol: ProtocolId,
|
||||
connections: &'s mut Slab<ConnectionEntry>,
|
||||
timers: &'s mut HashMap<TimerToken, ProtocolId>,
|
||||
connections: Arc<RwLock<Slab<SharedConnectionEntry>>>,
|
||||
session: Option<StreamToken>,
|
||||
}
|
||||
|
||||
impl<'s, 'io, Message> NetworkContext<'s, 'io, Message> where Message: Send + 'static, {
|
||||
impl<'s, Message> NetworkContext<'s, Message> where Message: Send + Sync + Clone + 'static, {
|
||||
/// Create a new network IO access point. Takes references to all the data that can be updated within the IO handler.
|
||||
fn new(io: &'s mut IoContext<'io, NetworkIoMessage<Message>>,
|
||||
fn new(io: &'s IoContext<NetworkIoMessage<Message>>,
|
||||
protocol: ProtocolId,
|
||||
session: Option<StreamToken>, connections: &'s mut Slab<ConnectionEntry>,
|
||||
timers: &'s mut HashMap<TimerToken, ProtocolId>) -> NetworkContext<'s, 'io, Message> {
|
||||
session: Option<StreamToken>, connections: Arc<RwLock<Slab<SharedConnectionEntry>>>) -> NetworkContext<'s, Message> {
|
||||
NetworkContext {
|
||||
io: io,
|
||||
protocol: protocol,
|
||||
session: session,
|
||||
connections: connections,
|
||||
timers: timers,
|
||||
}
|
||||
}
|
||||
|
||||
/// Send a packet over the network to another peer.
|
||||
pub fn send(&mut self, peer: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> {
|
||||
match self.connections.get_mut(peer) {
|
||||
Some(&mut ConnectionEntry::Session(ref mut s)) => {
|
||||
s.send_packet(self.protocol, packet_id as u8, &data).unwrap_or_else(|e| {
|
||||
warn!(target: "net", "Send error: {:?}", e);
|
||||
}); //TODO: don't copy vector data
|
||||
},
|
||||
_ => {
|
||||
warn!(target: "net", "Send: Peer does not exist");
|
||||
pub fn send(&self, peer: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> {
|
||||
if let Some(connection) = self.connections.read().unwrap().get(peer).cloned() {
|
||||
match *connection.lock().unwrap().deref_mut() {
|
||||
ConnectionEntry::Session(ref mut s) => {
|
||||
s.send_packet(self.protocol, packet_id as u8, &data).unwrap_or_else(|e| {
|
||||
warn!(target: "net", "Send error: {:?}", e);
|
||||
}); //TODO: don't copy vector data
|
||||
},
|
||||
_ => warn!(target: "net", "Send: Peer is not connected yet")
|
||||
}
|
||||
} else {
|
||||
warn!(target: "net", "Send: Peer does not exist")
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Respond to a current network message. Panics if no there is no packet in the context.
|
||||
pub fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> {
|
||||
pub fn respond(&self, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> {
|
||||
match self.session {
|
||||
Some(session) => self.send(session, packet_id, data),
|
||||
None => {
|
||||
@ -153,31 +159,28 @@ impl<'s, 'io, Message> NetworkContext<'s, 'io, Message> where Message: Send + 's
|
||||
}
|
||||
|
||||
/// Disable current protocol capability for given peer. If no capabilities left peer gets disconnected.
|
||||
pub fn disable_peer(&mut self, _peer: PeerId) {
|
||||
pub fn disable_peer(&self, _peer: PeerId) {
|
||||
//TODO: remove capability, disconnect if no capabilities left
|
||||
}
|
||||
|
||||
/// Register a new IO timer. Returns a new timer token. 'NetworkProtocolHandler::timeout' will be called with the token.
|
||||
pub fn register_timer(&mut self, ms: u64) -> Result<TimerToken, UtilError>{
|
||||
match self.io.register_timer(ms) {
|
||||
Ok(token) => {
|
||||
self.timers.insert(token, self.protocol);
|
||||
Ok(token)
|
||||
},
|
||||
e => e,
|
||||
}
|
||||
/// Register a new IO timer. 'IoHandler::timeout' will be called with the token.
|
||||
pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), UtilError> {
|
||||
self.io.message(NetworkIoMessage::AddTimer {
|
||||
token: token,
|
||||
delay: ms,
|
||||
protocol: self.protocol,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns peer identification string
|
||||
pub fn peer_info(&self, peer: PeerId) -> String {
|
||||
match self.connections.get(peer) {
|
||||
Some(&ConnectionEntry::Session(ref s)) => {
|
||||
s.info.client_version.clone()
|
||||
},
|
||||
_ => {
|
||||
"unknown".to_string()
|
||||
if let Some(connection) = self.connections.read().unwrap().get(peer).cloned() {
|
||||
if let ConnectionEntry::Session(ref s) = *connection.lock().unwrap().deref() {
|
||||
return s.info.client_version.clone()
|
||||
}
|
||||
}
|
||||
"unknown".to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
@ -213,7 +216,7 @@ impl HostInfo {
|
||||
/// Increments and returns connection nonce.
|
||||
pub fn next_nonce(&mut self) -> H256 {
|
||||
self.nonce = self.nonce.sha3();
|
||||
return self.nonce.clone();
|
||||
self.nonce.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@ -222,66 +225,100 @@ enum ConnectionEntry {
|
||||
Session(Session)
|
||||
}
|
||||
|
||||
/// Root IO handler. Manages protocol handlers, IO timers and network connections.
|
||||
pub struct Host<Message> where Message: Send {
|
||||
pub info: HostInfo,
|
||||
udp_socket: UdpSocket,
|
||||
listener: TcpListener,
|
||||
connections: Slab<ConnectionEntry>,
|
||||
timers: HashMap<TimerToken, ProtocolId>,
|
||||
nodes: HashMap<NodeId, Node>,
|
||||
handlers: HashMap<ProtocolId, Box<NetworkProtocolHandler<Message>>>,
|
||||
type SharedConnectionEntry = Arc<Mutex<ConnectionEntry>>;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct ProtocolTimer {
|
||||
pub protocol: ProtocolId,
|
||||
pub token: TimerToken, // Handler level token
|
||||
}
|
||||
|
||||
impl<Message> Host<Message> where Message: Send {
|
||||
/// Root IO handler. Manages protocol handlers, IO timers and network connections.
|
||||
pub struct Host<Message> where Message: Send + Sync + Clone {
|
||||
pub info: RwLock<HostInfo>,
|
||||
udp_socket: Mutex<UdpSocket>,
|
||||
tcp_listener: Mutex<TcpListener>,
|
||||
connections: Arc<RwLock<Slab<SharedConnectionEntry>>>,
|
||||
nodes: RwLock<HashMap<NodeId, Node>>,
|
||||
handlers: RwLock<HashMap<ProtocolId, Arc<NetworkProtocolHandler<Message>>>>,
|
||||
timers: RwLock<HashMap<TimerToken, ProtocolTimer>>,
|
||||
timer_counter: RwLock<usize>,
|
||||
}
|
||||
|
||||
impl<Message> Host<Message> where Message: Send + Sync + Clone {
|
||||
pub fn new() -> Host<Message> {
|
||||
let config = NetworkConfiguration::new();
|
||||
let addr = config.listen_address;
|
||||
// Setup the server socket
|
||||
let listener = TcpListener::bind(&addr).unwrap();
|
||||
let tcp_listener = TcpListener::bind(&addr).unwrap();
|
||||
let udp_socket = UdpSocket::bound(&addr).unwrap();
|
||||
Host::<Message> {
|
||||
info: HostInfo {
|
||||
let mut host = Host::<Message> {
|
||||
info: RwLock::new(HostInfo {
|
||||
keys: KeyPair::create().unwrap(),
|
||||
config: config,
|
||||
nonce: H256::random(),
|
||||
protocol_version: 4,
|
||||
client_version: "parity".to_string(),
|
||||
client_version: "parity".to_owned(),
|
||||
listen_port: 0,
|
||||
capabilities: Vec::new(),
|
||||
},
|
||||
udp_socket: udp_socket,
|
||||
listener: listener,
|
||||
connections: Slab::new_starting_at(FIRST_CONNECTION, MAX_CONNECTIONS),
|
||||
timers: HashMap::new(),
|
||||
nodes: HashMap::new(),
|
||||
handlers: HashMap::new(),
|
||||
}
|
||||
}),
|
||||
udp_socket: Mutex::new(udp_socket),
|
||||
tcp_listener: Mutex::new(tcp_listener),
|
||||
connections: Arc::new(RwLock::new(Slab::new_starting_at(FIRST_CONNECTION, MAX_CONNECTIONS))),
|
||||
nodes: RwLock::new(HashMap::new()),
|
||||
handlers: RwLock::new(HashMap::new()),
|
||||
timers: RwLock::new(HashMap::new()),
|
||||
timer_counter: RwLock::new(LAST_CONNECTION + 1),
|
||||
};
|
||||
let port = host.info.read().unwrap().config.listen_address.port();
|
||||
host.info.write().unwrap().deref_mut().listen_port = port;
|
||||
|
||||
/*
|
||||
match ::ifaces::Interface::get_all().unwrap().into_iter().filter(|x| x.kind == ::ifaces::Kind::Packet && x.addr.is_some()).next() {
|
||||
Some(iface) => config.public_address = iface.addr.unwrap(),
|
||||
None => warn!("No public network interface"),
|
||||
*/
|
||||
|
||||
// self.add_node("enode://a9a921de2ff09a9a4d38b623c67b2d6b477a8e654ae95d874750cbbcb31b33296496a7b4421934e2629269e180823e52c15c2b19fc59592ec51ffe4f2de76ed7@127.0.0.1:30303");
|
||||
// GO bootnodes
|
||||
host.add_node("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303"); // IE
|
||||
host.add_node("enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f73104b9fdeb090af9583d3391b7f10cb2ba9e26865dd5fca4fcdc0fb1e3b723c786@54.94.239.50:30303"); // BR
|
||||
host.add_node("enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303"); // SG
|
||||
// ETH/DEV cpp-ethereum (poc-9.ethdev.com)
|
||||
host.add_node("enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9@5.1.83.226:30303");
|
||||
host
|
||||
}
|
||||
|
||||
fn add_node(&mut self, id: &str) {
|
||||
pub fn add_node(&mut self, id: &str) {
|
||||
match Node::from_str(id) {
|
||||
Err(e) => { warn!("Could not add node: {:?}", e); },
|
||||
Ok(n) => {
|
||||
self.nodes.insert(n.id.clone(), n);
|
||||
self.nodes.write().unwrap().insert(n.id.clone(), n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn maintain_network(&mut self, io: &mut IoContext<NetworkIoMessage<Message>>) {
|
||||
pub fn client_version(&self) -> String {
|
||||
self.info.read().unwrap().client_version.clone()
|
||||
}
|
||||
|
||||
pub fn client_id(&self) -> NodeId {
|
||||
self.info.read().unwrap().id().clone()
|
||||
}
|
||||
|
||||
fn maintain_network(&self, io: &IoContext<NetworkIoMessage<Message>>) {
|
||||
self.connect_peers(io);
|
||||
io.event_loop.timeout_ms(Token(IDLE), MAINTENANCE_TIMEOUT).unwrap();
|
||||
}
|
||||
|
||||
fn have_session(&self, id: &NodeId) -> bool {
|
||||
self.connections.iter().any(|e| match e { &ConnectionEntry::Session(ref s) => s.info.id.eq(&id), _ => false })
|
||||
self.connections.read().unwrap().iter().any(|e| match *e.lock().unwrap().deref() { ConnectionEntry::Session(ref s) => s.info.id.eq(&id), _ => false })
|
||||
}
|
||||
|
||||
fn connecting_to(&self, id: &NodeId) -> bool {
|
||||
self.connections.iter().any(|e| match e { &ConnectionEntry::Handshake(ref h) => h.id.eq(&id), _ => false })
|
||||
self.connections.read().unwrap().iter().any(|e| match *e.lock().unwrap().deref() { ConnectionEntry::Handshake(ref h) => h.id.eq(&id), _ => false })
|
||||
}
|
||||
|
||||
fn connect_peers(&mut self, io: &mut IoContext<NetworkIoMessage<Message>>) {
|
||||
fn connect_peers(&self, io: &IoContext<NetworkIoMessage<Message>>) {
|
||||
struct NodeInfo {
|
||||
id: NodeId,
|
||||
peer_type: PeerType
|
||||
@ -292,18 +329,19 @@ impl<Message> Host<Message> where Message: Send {
|
||||
let mut req_conn = 0;
|
||||
//TODO: use nodes from discovery here
|
||||
//for n in self.node_buckets.iter().flat_map(|n| &n.nodes).map(|id| NodeInfo { id: id.clone(), peer_type: self.nodes.get(id).unwrap().peer_type}) {
|
||||
for n in self.nodes.values().map(|n| NodeInfo { id: n.id.clone(), peer_type: n.peer_type }) {
|
||||
let pin = self.info.read().unwrap().deref().config.pin;
|
||||
for n in self.nodes.read().unwrap().values().map(|n| NodeInfo { id: n.id.clone(), peer_type: n.peer_type }) {
|
||||
let connected = self.have_session(&n.id) || self.connecting_to(&n.id);
|
||||
let required = n.peer_type == PeerType::Required;
|
||||
if connected && required {
|
||||
req_conn += 1;
|
||||
}
|
||||
else if !connected && (!self.info.config.pin || required) {
|
||||
else if !connected && (!pin || required) {
|
||||
to_connect.push(n);
|
||||
}
|
||||
}
|
||||
|
||||
for n in to_connect.iter() {
|
||||
for n in &to_connect {
|
||||
if n.peer_type == PeerType::Required {
|
||||
if req_conn < IDEAL_PEERS {
|
||||
self.connect_peer(&n.id, io);
|
||||
@ -312,13 +350,12 @@ impl<Message> Host<Message> where Message: Send {
|
||||
}
|
||||
}
|
||||
|
||||
if !self.info.config.pin
|
||||
{
|
||||
if !pin {
|
||||
let pending_count = 0; //TODO:
|
||||
let peer_count = 0;
|
||||
let mut open_slots = IDEAL_PEERS - peer_count - pending_count + req_conn;
|
||||
if open_slots > 0 {
|
||||
for n in to_connect.iter() {
|
||||
for n in &to_connect {
|
||||
if n.peer_type == PeerType::Optional && open_slots > 0 {
|
||||
open_slots -= 1;
|
||||
self.connect_peer(&n.id, io);
|
||||
@ -328,23 +365,27 @@ impl<Message> Host<Message> where Message: Send {
|
||||
}
|
||||
}
|
||||
|
||||
fn connect_peer(&mut self, id: &NodeId, io: &mut IoContext<NetworkIoMessage<Message>>) {
|
||||
#[allow(single_match)]
|
||||
#[allow(block_in_if_condition_stmt)]
|
||||
fn connect_peer(&self, id: &NodeId, io: &IoContext<NetworkIoMessage<Message>>) {
|
||||
if self.have_session(id)
|
||||
{
|
||||
warn!("Aborted connect. Node already connected.");
|
||||
return;
|
||||
}
|
||||
if self.connecting_to(id)
|
||||
{
|
||||
if self.connecting_to(id) {
|
||||
warn!("Aborted connect. Node already connecting.");
|
||||
return;
|
||||
}
|
||||
|
||||
let socket = {
|
||||
let node = self.nodes.get_mut(id).unwrap();
|
||||
node.last_attempted = Some(::time::now());
|
||||
|
||||
match TcpStream::connect(&node.endpoint.address) {
|
||||
let address = {
|
||||
let mut nodes = self.nodes.write().unwrap();
|
||||
let node = nodes.get_mut(id).unwrap();
|
||||
node.last_attempted = Some(::time::now());
|
||||
node.endpoint.address
|
||||
};
|
||||
match TcpStream::connect(&address) {
|
||||
Ok(socket) => socket,
|
||||
Err(_) => {
|
||||
warn!("Cannot connect to node");
|
||||
@ -353,224 +394,182 @@ impl<Message> Host<Message> where Message: Send {
|
||||
}
|
||||
};
|
||||
|
||||
let nonce = self.info.next_nonce();
|
||||
match self.connections.insert_with(|token| ConnectionEntry::Handshake(Handshake::new(Token(token), id, socket, &nonce).expect("Can't create handshake"))) {
|
||||
Some(token) => {
|
||||
match self.connections.get_mut(token) {
|
||||
Some(&mut ConnectionEntry::Handshake(ref mut h)) => {
|
||||
h.start(&self.info, true)
|
||||
.and_then(|_| h.register(io.event_loop))
|
||||
.unwrap_or_else (|e| {
|
||||
debug!(target: "net", "Handshake create error: {:?}", e);
|
||||
});
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
None => { warn!("Max connections reached") }
|
||||
let nonce = self.info.write().unwrap().next_nonce();
|
||||
if self.connections.write().unwrap().insert_with(|token| {
|
||||
let mut handshake = Handshake::new(token, id, socket, &nonce).expect("Can't create handshake");
|
||||
handshake.start(io, &self.info.read().unwrap(), true).and_then(|_| io.register_stream(token)).unwrap_or_else (|e| {
|
||||
debug!(target: "net", "Handshake create error: {:?}", e);
|
||||
});
|
||||
Arc::new(Mutex::new(ConnectionEntry::Handshake(handshake)))
|
||||
}).is_none() {
|
||||
warn!("Max connections reached");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn accept(&mut self, _io: &mut IoContext<NetworkIoMessage<Message>>) {
|
||||
fn accept(&self, _io: &IoContext<NetworkIoMessage<Message>>) {
|
||||
trace!(target: "net", "accept");
|
||||
}
|
||||
|
||||
fn connection_writable<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage<Message>>) {
|
||||
let mut kill = false;
|
||||
#[allow(single_match)]
|
||||
fn connection_writable(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
|
||||
let mut create_session = false;
|
||||
match self.connections.get_mut(token) {
|
||||
Some(&mut ConnectionEntry::Handshake(ref mut h)) => {
|
||||
h.writable(io.event_loop, &self.info).unwrap_or_else(|e| {
|
||||
debug!(target: "net", "Handshake write error: {:?}", e);
|
||||
kill = true;
|
||||
});
|
||||
create_session = h.done();
|
||||
},
|
||||
Some(&mut ConnectionEntry::Session(ref mut s)) => {
|
||||
s.writable(io.event_loop, &self.info).unwrap_or_else(|e| {
|
||||
debug!(target: "net", "Session write error: {:?}", e);
|
||||
kill = true;
|
||||
});
|
||||
let mut kill = false;
|
||||
if let Some(connection) = self.connections.read().unwrap().get(token).cloned() {
|
||||
match *connection.lock().unwrap().deref_mut() {
|
||||
ConnectionEntry::Handshake(ref mut h) => {
|
||||
match h.writable(io, &self.info.read().unwrap()) {
|
||||
Err(e) => {
|
||||
debug!(target: "net", "Handshake write error: {:?}", e);
|
||||
kill = true;
|
||||
},
|
||||
Ok(_) => ()
|
||||
}
|
||||
if h.done() {
|
||||
create_session = true;
|
||||
}
|
||||
},
|
||||
ConnectionEntry::Session(ref mut s) => {
|
||||
match s.writable(io, &self.info.read().unwrap()) {
|
||||
Err(e) => {
|
||||
debug!(target: "net", "Session write error: {:?}", e);
|
||||
kill = true;
|
||||
},
|
||||
Ok(_) => ()
|
||||
}
|
||||
io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
warn!(target: "net", "Received event for unknown connection");
|
||||
}
|
||||
}
|
||||
}
|
||||
if kill {
|
||||
self.kill_connection(token, io);
|
||||
self.kill_connection(token, io); //TODO: mark connection as dead an check in kill_connection
|
||||
return;
|
||||
} else if create_session {
|
||||
self.start_session(token, io);
|
||||
}
|
||||
match self.connections.get_mut(token) {
|
||||
Some(&mut ConnectionEntry::Session(ref mut s)) => {
|
||||
s.reregister(io.event_loop).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e));
|
||||
},
|
||||
_ => (),
|
||||
io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e));
|
||||
}
|
||||
}
|
||||
|
||||
fn connection_closed<'s>(&'s mut self, token: TimerToken, io: &mut IoContext<'s, NetworkIoMessage<Message>>) {
|
||||
fn connection_closed(&self, token: TimerToken, io: &IoContext<NetworkIoMessage<Message>>) {
|
||||
self.kill_connection(token, io);
|
||||
}
|
||||
|
||||
fn connection_readable<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage<Message>>) {
|
||||
let mut kill = false;
|
||||
let mut create_session = false;
|
||||
fn connection_readable(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
|
||||
let mut ready_data: Vec<ProtocolId> = Vec::new();
|
||||
let mut packet_data: Option<(ProtocolId, PacketId, Vec<u8>)> = None;
|
||||
match self.connections.get_mut(token) {
|
||||
Some(&mut ConnectionEntry::Handshake(ref mut h)) => {
|
||||
h.readable(io.event_loop, &self.info).unwrap_or_else(|e| {
|
||||
debug!(target: "net", "Handshake read error: {:?}", e);
|
||||
kill = true;
|
||||
});
|
||||
create_session = h.done();
|
||||
},
|
||||
Some(&mut ConnectionEntry::Session(ref mut s)) => {
|
||||
let sd = { s.readable(io.event_loop, &self.info).unwrap_or_else(|e| {
|
||||
debug!(target: "net", "Session read error: {:?}", e);
|
||||
kill = true;
|
||||
SessionData::None
|
||||
}) };
|
||||
match sd {
|
||||
SessionData::Ready => {
|
||||
for (p, _) in self.handlers.iter_mut() {
|
||||
if s.have_capability(p) {
|
||||
ready_data.push(p);
|
||||
let mut create_session = false;
|
||||
let mut kill = false;
|
||||
if let Some(connection) = self.connections.read().unwrap().get(token).cloned() {
|
||||
match *connection.lock().unwrap().deref_mut() {
|
||||
ConnectionEntry::Handshake(ref mut h) => {
|
||||
if let Err(e) = h.readable(io, &self.info.read().unwrap()) {
|
||||
debug!(target: "net", "Handshake read error: {:?}", e);
|
||||
kill = true;
|
||||
}
|
||||
if h.done() {
|
||||
create_session = true;
|
||||
}
|
||||
},
|
||||
ConnectionEntry::Session(ref mut s) => {
|
||||
match s.readable(io, &self.info.read().unwrap()) {
|
||||
Err(e) => {
|
||||
debug!(target: "net", "Handshake read error: {:?}", e);
|
||||
kill = true;
|
||||
},
|
||||
Ok(SessionData::Ready) => {
|
||||
for (p, _) in self.handlers.read().unwrap().iter() {
|
||||
if s.have_capability(p) {
|
||||
ready_data.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
SessionData::Packet {
|
||||
data,
|
||||
protocol,
|
||||
packet_id,
|
||||
} => {
|
||||
match self.handlers.get_mut(protocol) {
|
||||
None => { warn!(target: "net", "No handler found for protocol: {:?}", protocol) },
|
||||
Some(_) => packet_data = Some((protocol, packet_id, data)),
|
||||
}
|
||||
},
|
||||
SessionData::None => {},
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
warn!(target: "net", "Received event for unknown connection");
|
||||
}
|
||||
}
|
||||
if kill {
|
||||
self.kill_connection(token, io);
|
||||
return;
|
||||
}
|
||||
if create_session {
|
||||
self.start_session(token, io);
|
||||
}
|
||||
for p in ready_data {
|
||||
let mut h = self.handlers.get_mut(p).unwrap();
|
||||
h.connected(&mut NetworkContext::new(io, p, Some(token), &mut self.connections, &mut self.timers), &token);
|
||||
}
|
||||
if let Some((p, packet_id, data)) = packet_data {
|
||||
let mut h = self.handlers.get_mut(p).unwrap();
|
||||
h.read(&mut NetworkContext::new(io, p, Some(token), &mut self.connections, &mut self.timers), &token, packet_id, &data[1..]);
|
||||
}
|
||||
|
||||
match self.connections.get_mut(token) {
|
||||
Some(&mut ConnectionEntry::Session(ref mut s)) => {
|
||||
s.reregister(io.event_loop).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e));
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn start_session(&mut self, token: StreamToken, io: &mut IoContext<NetworkIoMessage<Message>>) {
|
||||
let info = &self.info;
|
||||
// TODO: use slab::replace_with (currently broken)
|
||||
/*
|
||||
match self.connections.remove(token) {
|
||||
Some(ConnectionEntry::Handshake(h)) => {
|
||||
match Session::new(h, io.event_loop, info) {
|
||||
Ok(session) => {
|
||||
assert!(token == self.connections.insert(ConnectionEntry::Session(session)).ok().unwrap());
|
||||
},
|
||||
Err(e) => {
|
||||
debug!(target: "net", "Session construction error: {:?}", e);
|
||||
},
|
||||
Ok(SessionData::Packet {
|
||||
data,
|
||||
protocol,
|
||||
packet_id,
|
||||
}) => {
|
||||
match self.handlers.read().unwrap().get(protocol) {
|
||||
None => { warn!(target: "net", "No handler found for protocol: {:?}", protocol) },
|
||||
Some(_) => packet_data = Some((protocol, packet_id, data)),
|
||||
}
|
||||
},
|
||||
Ok(SessionData::None) => {},
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => panic!("Error updating slab with session")
|
||||
}*/
|
||||
self.connections.replace_with(token, |c| {
|
||||
match c {
|
||||
ConnectionEntry::Handshake(h) => Session::new(h, io.event_loop, info)
|
||||
.map(|s| Some(ConnectionEntry::Session(s)))
|
||||
.unwrap_or_else(|e| {
|
||||
debug!(target: "net", "Session construction error: {:?}", e);
|
||||
None
|
||||
}),
|
||||
_ => { panic!("No handshake to create a session from"); }
|
||||
}
|
||||
}).expect("Error updating slab with session");
|
||||
}
|
||||
if kill {
|
||||
self.kill_connection(token, io); //TODO: mark connection as dead an check in kill_connection
|
||||
return;
|
||||
} else if create_session {
|
||||
self.start_session(token, io);
|
||||
io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e));
|
||||
}
|
||||
for p in ready_data {
|
||||
let h = self.handlers.read().unwrap().get(p).unwrap().clone();
|
||||
h.connected(&NetworkContext::new(io, p, Some(token), self.connections.clone()), &token);
|
||||
}
|
||||
if let Some((p, packet_id, data)) = packet_data {
|
||||
let h = self.handlers.read().unwrap().get(p).unwrap().clone();
|
||||
h.read(&NetworkContext::new(io, p, Some(token), self.connections.clone()), &token, packet_id, &data[1..]);
|
||||
}
|
||||
io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Token registration error: {:?}", e));
|
||||
}
|
||||
|
||||
fn connection_timeout<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage<Message>>) {
|
||||
fn start_session(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
|
||||
self.connections.write().unwrap().replace_with(token, |c| {
|
||||
match Arc::try_unwrap(c).ok().unwrap().into_inner().unwrap() {
|
||||
ConnectionEntry::Handshake(h) => {
|
||||
let session = Session::new(h, io, &self.info.read().unwrap()).expect("Session creation error");
|
||||
io.update_registration(token).expect("Error updating session registration");
|
||||
Some(Arc::new(Mutex::new(ConnectionEntry::Session(session))))
|
||||
},
|
||||
_ => { None } // handshake expired
|
||||
}
|
||||
}).ok();
|
||||
}
|
||||
|
||||
fn connection_timeout(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
|
||||
self.kill_connection(token, io)
|
||||
}
|
||||
|
||||
fn kill_connection<'s>(&'s mut self, token: StreamToken, io: &mut IoContext<'s, NetworkIoMessage<Message>>) {
|
||||
fn kill_connection(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
|
||||
let mut to_disconnect: Vec<ProtocolId> = Vec::new();
|
||||
let mut remove = true;
|
||||
match self.connections.get_mut(token) {
|
||||
Some(&mut ConnectionEntry::Handshake(_)) => (), // just abandon handshake
|
||||
Some(&mut ConnectionEntry::Session(ref mut s)) if s.is_ready() => {
|
||||
for (p, _) in self.handlers.iter_mut() {
|
||||
if s.have_capability(p) {
|
||||
to_disconnect.push(p);
|
||||
}
|
||||
{
|
||||
let mut connections = self.connections.write().unwrap();
|
||||
if let Some(connection) = connections.get(token).cloned() {
|
||||
match *connection.lock().unwrap().deref_mut() {
|
||||
ConnectionEntry::Handshake(_) => {
|
||||
connections.remove(token);
|
||||
},
|
||||
ConnectionEntry::Session(ref mut s) if s.is_ready() => {
|
||||
for (p, _) in self.handlers.read().unwrap().iter() {
|
||||
if s.have_capability(p) {
|
||||
to_disconnect.push(p);
|
||||
}
|
||||
}
|
||||
connections.remove(token);
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
remove = false;
|
||||
},
|
||||
}
|
||||
}
|
||||
for p in to_disconnect {
|
||||
let mut h = self.handlers.get_mut(p).unwrap();
|
||||
h.disconnected(&mut NetworkContext::new(io, p, Some(token), &mut self.connections, &mut self.timers), &token);
|
||||
}
|
||||
if remove {
|
||||
self.connections.remove(token);
|
||||
let h = self.handlers.read().unwrap().get(p).unwrap().clone();
|
||||
h.disconnected(&NetworkContext::new(io, p, Some(token), self.connections.clone()), &token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Message: Send + 'static {
|
||||
impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Message: Send + Sync + Clone + 'static {
|
||||
/// Initialize networking
|
||||
fn initialize(&mut self, io: &mut IoContext<NetworkIoMessage<Message>>) {
|
||||
/*
|
||||
match ::ifaces::Interface::get_all().unwrap().into_iter().filter(|x| x.kind == ::ifaces::Kind::Packet && x.addr.is_some()).next() {
|
||||
Some(iface) => config.public_address = iface.addr.unwrap(),
|
||||
None => warn!("No public network interface"),
|
||||
*/
|
||||
|
||||
// Start listening for incoming connections
|
||||
io.event_loop.register(&self.listener, Token(TCP_ACCEPT), EventSet::readable(), PollOpt::edge()).unwrap();
|
||||
io.event_loop.timeout_ms(Token(IDLE), MAINTENANCE_TIMEOUT).unwrap();
|
||||
// open the udp socket
|
||||
io.event_loop.register(&self.udp_socket, Token(NODETABLE_RECEIVE), EventSet::readable(), PollOpt::edge()).unwrap();
|
||||
io.event_loop.timeout_ms(Token(NODETABLE_MAINTAIN), 7200).unwrap();
|
||||
let port = self.info.config.listen_address.port();
|
||||
self.info.listen_port = port;
|
||||
|
||||
self.add_node("enode://a9a921de2ff09a9a4d38b623c67b2d6b477a8e654ae95d874750cbbcb31b33296496a7b4421934e2629269e180823e52c15c2b19fc59592ec51ffe4f2de76ed7@127.0.0.1:30303");
|
||||
/* // GO bootnodes
|
||||
self.add_node("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303"); // IE
|
||||
self.add_node("enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f73104b9fdeb090af9583d3391b7f10cb2ba9e26865dd5fca4fcdc0fb1e3b723c786@54.94.239.50:30303"); // BR
|
||||
self.add_node("enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303"); // SG
|
||||
// ETH/DEV cpp-ethereum (poc-9.ethdev.com)
|
||||
self.add_node("enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9@5.1.83.226:30303");*/
|
||||
fn initialize(&self, io: &IoContext<NetworkIoMessage<Message>>) {
|
||||
io.register_stream(TCP_ACCEPT).expect("Error registering TCP listener");
|
||||
io.register_stream(NODETABLE_RECEIVE).expect("Error registering UDP listener");
|
||||
io.register_timer(IDLE, MAINTENANCE_TIMEOUT).expect("Error registering Network idle timer");
|
||||
//io.register_timer(NODETABLE_MAINTAIN, 7200);
|
||||
}
|
||||
|
||||
fn stream_hup<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage<Message>>, stream: StreamToken) {
|
||||
fn stream_hup(&self, io: &IoContext<NetworkIoMessage<Message>>, stream: StreamToken) {
|
||||
trace!(target: "net", "Hup: {}", stream);
|
||||
match stream {
|
||||
FIRST_CONNECTION ... LAST_CONNECTION => self.connection_closed(stream, io),
|
||||
@ -578,7 +577,7 @@ impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Messa
|
||||
};
|
||||
}
|
||||
|
||||
fn stream_readable<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage<Message>>, stream: StreamToken) {
|
||||
fn stream_readable(&self, io: &IoContext<NetworkIoMessage<Message>>, stream: StreamToken) {
|
||||
match stream {
|
||||
FIRST_CONNECTION ... LAST_CONNECTION => self.connection_readable(stream, io),
|
||||
NODETABLE_RECEIVE => {},
|
||||
@ -587,65 +586,97 @@ impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Messa
|
||||
}
|
||||
}
|
||||
|
||||
fn stream_writable<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage<Message>>, stream: StreamToken) {
|
||||
fn stream_writable(&self, io: &IoContext<NetworkIoMessage<Message>>, stream: StreamToken) {
|
||||
match stream {
|
||||
FIRST_CONNECTION ... LAST_CONNECTION => self.connection_writable(stream, io),
|
||||
NODETABLE_RECEIVE => {},
|
||||
_ => panic!("Received unknown writable token"),
|
||||
}
|
||||
}
|
||||
|
||||
fn timeout<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage<Message>>, token: TimerToken) {
|
||||
fn timeout(&self, io: &IoContext<NetworkIoMessage<Message>>, token: TimerToken) {
|
||||
match token {
|
||||
IDLE => self.maintain_network(io),
|
||||
FIRST_CONNECTION ... LAST_CONNECTION => self.connection_timeout(token, io),
|
||||
NODETABLE_DISCOVERY => {},
|
||||
NODETABLE_MAINTAIN => {},
|
||||
_ => match self.timers.get_mut(&token).map(|p| *p) {
|
||||
Some(protocol) => match self.handlers.get_mut(protocol) {
|
||||
None => { warn!(target: "net", "No handler found for protocol: {:?}", protocol) },
|
||||
Some(h) => { h.timeout(&mut NetworkContext::new(io, protocol, Some(token), &mut self.connections, &mut self.timers), token); }
|
||||
},
|
||||
None => {} // time not registerd through us
|
||||
_ => match self.timers.read().unwrap().get(&token).cloned() {
|
||||
Some(timer) => match self.handlers.read().unwrap().get(timer.protocol).cloned() {
|
||||
None => { warn!(target: "net", "No handler found for protocol: {:?}", timer.protocol) },
|
||||
Some(h) => { h.timeout(&NetworkContext::new(io, timer.protocol, None, self.connections.clone()), timer.token); }
|
||||
},
|
||||
None => { warn!("Unknown timer token: {}", token); } // timer is not registerd through us
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn message<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage<Message>>, message: &'s mut NetworkIoMessage<Message>) {
|
||||
match message {
|
||||
&mut NetworkIoMessage::AddHandler {
|
||||
ref mut handler,
|
||||
fn message(&self, io: &IoContext<NetworkIoMessage<Message>>, message: &NetworkIoMessage<Message>) {
|
||||
match *message {
|
||||
NetworkIoMessage::AddHandler {
|
||||
ref handler,
|
||||
ref protocol,
|
||||
ref versions
|
||||
} => {
|
||||
let mut h = mem::replace(handler, None).unwrap();
|
||||
h.initialize(&mut NetworkContext::new(io, protocol, None, &mut self.connections, &mut self.timers));
|
||||
self.handlers.insert(protocol, h);
|
||||
let h = handler.clone();
|
||||
h.initialize(&NetworkContext::new(io, protocol, None, self.connections.clone()));
|
||||
self.handlers.write().unwrap().insert(protocol, h);
|
||||
let mut info = self.info.write().unwrap();
|
||||
for v in versions {
|
||||
self.info.capabilities.push(CapabilityInfo { protocol: protocol, version: *v, packet_count:0 });
|
||||
info.capabilities.push(CapabilityInfo { protocol: protocol, version: *v, packet_count:0 });
|
||||
}
|
||||
},
|
||||
&mut NetworkIoMessage::Send {
|
||||
ref peer,
|
||||
ref packet_id,
|
||||
NetworkIoMessage::AddTimer {
|
||||
ref protocol,
|
||||
ref data,
|
||||
ref delay,
|
||||
ref token,
|
||||
} => {
|
||||
match self.connections.get_mut(*peer as usize) {
|
||||
Some(&mut ConnectionEntry::Session(ref mut s)) => {
|
||||
s.send_packet(protocol, *packet_id as u8, &data).unwrap_or_else(|e| {
|
||||
warn!(target: "net", "Send error: {:?}", e);
|
||||
}); //TODO: don't copy vector data
|
||||
},
|
||||
_ => {
|
||||
warn!(target: "net", "Send: Peer does not exist");
|
||||
}
|
||||
}
|
||||
let handler_token = {
|
||||
let mut timer_counter = self.timer_counter.write().unwrap();
|
||||
let counter = timer_counter.deref_mut();
|
||||
let handler_token = *counter;
|
||||
*counter += 1;
|
||||
handler_token
|
||||
};
|
||||
self.timers.write().unwrap().insert(handler_token, ProtocolTimer { protocol: protocol, token: *token });
|
||||
io.register_timer(handler_token, *delay).expect("Error registering timer");
|
||||
},
|
||||
&mut NetworkIoMessage::User(ref message) => {
|
||||
for (p, h) in self.handlers.iter_mut() {
|
||||
h.message(&mut NetworkContext::new(io, p, None, &mut self.connections, &mut self.timers), &message);
|
||||
NetworkIoMessage::User(ref message) => {
|
||||
for (p, h) in self.handlers.read().unwrap().iter() {
|
||||
h.message(&NetworkContext::new(io, p, None, self.connections.clone()), &message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn register_stream(&self, stream: StreamToken, reg: Token, event_loop: &mut EventLoop<IoManager<NetworkIoMessage<Message>>>) {
|
||||
match stream {
|
||||
FIRST_CONNECTION ... LAST_CONNECTION => {
|
||||
if let Some(connection) = self.connections.read().unwrap().get(stream).cloned() {
|
||||
match *connection.lock().unwrap().deref() {
|
||||
ConnectionEntry::Handshake(ref h) => h.register_socket(reg, event_loop).expect("Error registering socket"),
|
||||
ConnectionEntry::Session(_) => warn!("Unexpected session stream registration")
|
||||
}
|
||||
} else {} // expired
|
||||
}
|
||||
NODETABLE_RECEIVE => event_loop.register(self.udp_socket.lock().unwrap().deref(), Token(NODETABLE_RECEIVE), EventSet::all(), PollOpt::edge()).expect("Error registering stream"),
|
||||
TCP_ACCEPT => event_loop.register(self.tcp_listener.lock().unwrap().deref(), Token(TCP_ACCEPT), EventSet::all(), PollOpt::edge()).expect("Error registering stream"),
|
||||
_ => warn!("Unexpected stream registration")
|
||||
}
|
||||
}
|
||||
|
||||
fn update_stream(&self, stream: StreamToken, reg: Token, event_loop: &mut EventLoop<IoManager<NetworkIoMessage<Message>>>) {
|
||||
match stream {
|
||||
FIRST_CONNECTION ... LAST_CONNECTION => {
|
||||
if let Some(connection) = self.connections.read().unwrap().get(stream).cloned() {
|
||||
match *connection.lock().unwrap().deref() {
|
||||
ConnectionEntry::Handshake(ref h) => h.update_socket(reg, event_loop).expect("Error updating socket"),
|
||||
ConnectionEntry::Session(ref s) => s.update_socket(reg, event_loop).expect("Error updating socket"),
|
||||
}
|
||||
} else {} // expired
|
||||
}
|
||||
NODETABLE_RECEIVE => event_loop.reregister(self.udp_socket.lock().unwrap().deref(), Token(NODETABLE_RECEIVE), EventSet::all(), PollOpt::edge()).expect("Error reregistering stream"),
|
||||
TCP_ACCEPT => event_loop.reregister(self.tcp_listener.lock().unwrap().deref(), Token(TCP_ACCEPT), EventSet::all(), PollOpt::edge()).expect("Error reregistering stream"),
|
||||
_ => warn!("Unexpected stream update")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,39 +8,40 @@
|
||||
///
|
||||
/// struct MyHandler;
|
||||
///
|
||||
/// #[derive(Clone)]
|
||||
/// struct MyMessage {
|
||||
/// data: u32
|
||||
/// }
|
||||
///
|
||||
/// impl NetworkProtocolHandler<MyMessage> for MyHandler {
|
||||
/// fn initialize(&mut self, io: &mut NetworkContext<MyMessage>) {
|
||||
/// io.register_timer(1000);
|
||||
/// fn initialize(&self, io: &NetworkContext<MyMessage>) {
|
||||
/// io.register_timer(0, 1000);
|
||||
/// }
|
||||
///
|
||||
/// fn read(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
/// fn read(&self, io: &NetworkContext<MyMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
/// println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
|
||||
/// }
|
||||
///
|
||||
/// fn connected(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId) {
|
||||
/// fn connected(&self, io: &NetworkContext<MyMessage>, peer: &PeerId) {
|
||||
/// println!("Connected {}", peer);
|
||||
/// }
|
||||
///
|
||||
/// fn disconnected(&mut self, io: &mut NetworkContext<MyMessage>, peer: &PeerId) {
|
||||
/// fn disconnected(&self, io: &NetworkContext<MyMessage>, peer: &PeerId) {
|
||||
/// println!("Disconnected {}", peer);
|
||||
/// }
|
||||
///
|
||||
/// fn timeout(&mut self, io: &mut NetworkContext<MyMessage>, timer: TimerToken) {
|
||||
/// fn timeout(&self, io: &NetworkContext<MyMessage>, timer: TimerToken) {
|
||||
/// println!("Timeout {}", timer);
|
||||
/// }
|
||||
///
|
||||
/// fn message(&mut self, io: &mut NetworkContext<MyMessage>, message: &MyMessage) {
|
||||
/// fn message(&self, io: &NetworkContext<MyMessage>, message: &MyMessage) {
|
||||
/// println!("Message {}", message.data);
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// fn main () {
|
||||
/// let mut service = NetworkService::<MyMessage>::start().expect("Error creating network service");
|
||||
/// service.register_protocol(Box::new(MyHandler), "myproto", &[1u8]);
|
||||
/// service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]);
|
||||
///
|
||||
/// // Wait for quit condition
|
||||
/// // ...
|
||||
@ -57,36 +58,78 @@ mod error;
|
||||
mod node;
|
||||
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type PeerId = host::PeerId;
|
||||
pub use network::host::PeerId;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type PacketId = host::PacketId;
|
||||
pub use network::host::PacketId;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type NetworkContext<'s,'io, Message> = host::NetworkContext<'s, 'io, Message>;
|
||||
pub use network::host::NetworkContext;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type NetworkService<Message> = service::NetworkService<Message>;
|
||||
pub use network::service::NetworkService;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub use network::host::NetworkIoMessage;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type NetworkIoMessage<Message> = host::NetworkIoMessage<Message>;
|
||||
pub use network::host::NetworkIoMessage::User as UserMessage;
|
||||
/// TODO [arkpar] Please document me
|
||||
pub type NetworkError = error::NetworkError;
|
||||
pub use network::error::NetworkError;
|
||||
|
||||
use io::*;
|
||||
use io::TimerToken;
|
||||
|
||||
/// Network IO protocol handler. This needs to be implemented for each new subprotocol.
|
||||
/// All the handler function are called from within IO event loop.
|
||||
/// `Message` is the type for message data.
|
||||
pub trait NetworkProtocolHandler<Message>: Send where Message: Send {
|
||||
pub trait NetworkProtocolHandler<Message>: Sync + Send where Message: Send + Sync + Clone {
|
||||
/// Initialize the handler
|
||||
fn initialize(&mut self, _io: &mut NetworkContext<Message>) {}
|
||||
fn initialize(&self, _io: &NetworkContext<Message>) {}
|
||||
/// Called when new network packet received.
|
||||
fn read(&mut self, io: &mut NetworkContext<Message>, peer: &PeerId, packet_id: u8, data: &[u8]);
|
||||
fn read(&self, io: &NetworkContext<Message>, peer: &PeerId, packet_id: u8, data: &[u8]);
|
||||
/// Called when new peer is connected. Only called when peer supports the same protocol.
|
||||
fn connected(&mut self, io: &mut NetworkContext<Message>, peer: &PeerId);
|
||||
fn connected(&self, io: &NetworkContext<Message>, peer: &PeerId);
|
||||
/// Called when a previously connected peer disconnects.
|
||||
fn disconnected(&mut self, io: &mut NetworkContext<Message>, peer: &PeerId);
|
||||
fn disconnected(&self, io: &NetworkContext<Message>, peer: &PeerId);
|
||||
/// Timer function called after a timeout created with `NetworkContext::timeout`.
|
||||
fn timeout(&mut self, _io: &mut NetworkContext<Message>, _timer: TimerToken) {}
|
||||
fn timeout(&self, _io: &NetworkContext<Message>, _timer: TimerToken) {}
|
||||
/// Called when a broadcasted message is received. The message can only be sent from a different IO handler.
|
||||
fn message(&mut self, _io: &mut NetworkContext<Message>, _message: &Message) {}
|
||||
fn message(&self, _io: &NetworkContext<Message>, _message: &Message) {}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_net_service() {
|
||||
|
||||
use std::sync::Arc;
|
||||
struct MyHandler;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct MyMessage {
|
||||
data: u32
|
||||
}
|
||||
|
||||
impl NetworkProtocolHandler<MyMessage> for MyHandler {
|
||||
fn initialize(&self, io: &NetworkContext<MyMessage>) {
|
||||
io.register_timer(0, 1000).unwrap();
|
||||
}
|
||||
|
||||
fn read(&self, _io: &NetworkContext<MyMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
|
||||
}
|
||||
|
||||
fn connected(&self, _io: &NetworkContext<MyMessage>, peer: &PeerId) {
|
||||
println!("Connected {}", peer);
|
||||
}
|
||||
|
||||
fn disconnected(&self, _io: &NetworkContext<MyMessage>, peer: &PeerId) {
|
||||
println!("Disconnected {}", peer);
|
||||
}
|
||||
|
||||
fn timeout(&self, _io: &NetworkContext<MyMessage>, timer: TimerToken) {
|
||||
println!("Timeout {}", timer);
|
||||
}
|
||||
|
||||
fn message(&self, _io: &NetworkContext<MyMessage>, message: &MyMessage) {
|
||||
println!("Message {}", message.data);
|
||||
}
|
||||
}
|
||||
|
||||
let mut service = NetworkService::<MyMessage>::start().expect("Error creating network service");
|
||||
service.register_protocol(Arc::new(MyHandler), "myproto", &[1u8]).unwrap();
|
||||
}
|
||||
|
@ -20,14 +20,16 @@ pub struct NodeEndpoint {
|
||||
pub udp_port: u16
|
||||
}
|
||||
|
||||
impl NodeEndpoint {
|
||||
impl FromStr for NodeEndpoint {
|
||||
type Err = UtilError;
|
||||
|
||||
/// Create endpoint from string. Performs name resolution if given a host name.
|
||||
fn from_str(s: &str) -> Result<NodeEndpoint, UtilError> {
|
||||
let address = s.to_socket_addrs().map(|mut i| i.next());
|
||||
match address {
|
||||
Ok(Some(a)) => Ok(NodeEndpoint {
|
||||
address: a,
|
||||
address_str: s.to_string(),
|
||||
address_str: s.to_owned(),
|
||||
udp_port: a.port()
|
||||
}),
|
||||
Ok(_) => Err(UtilError::AddressResolve(None)),
|
||||
|
@ -1,23 +1,24 @@
|
||||
use std::sync::*;
|
||||
use error::*;
|
||||
use network::{NetworkProtocolHandler};
|
||||
use network::error::{NetworkError};
|
||||
use network::host::{Host, NetworkIoMessage, PeerId, PacketId, ProtocolId};
|
||||
use network::host::{Host, NetworkIoMessage, ProtocolId};
|
||||
use io::*;
|
||||
|
||||
/// IO Service with networking
|
||||
/// `Message` defines a notification data type.
|
||||
pub struct NetworkService<Message> where Message: Send + 'static {
|
||||
pub struct NetworkService<Message> where Message: Send + Sync + Clone + 'static {
|
||||
io_service: IoService<NetworkIoMessage<Message>>,
|
||||
host_info: String,
|
||||
}
|
||||
|
||||
impl<Message> NetworkService<Message> where Message: Send + 'static {
|
||||
impl<Message> NetworkService<Message> where Message: Send + Sync + Clone + 'static {
|
||||
/// Starts IO event loop
|
||||
pub fn start() -> Result<NetworkService<Message>, UtilError> {
|
||||
let mut io_service = try!(IoService::<NetworkIoMessage<Message>>::start());
|
||||
let host = Box::new(Host::new());
|
||||
let host_info = host.info.client_version.clone();
|
||||
info!("NetworkService::start(): id={:?}", host.info.id());
|
||||
let host = Arc::new(Host::new());
|
||||
let host_info = host.client_version();
|
||||
info!("NetworkService::start(): id={:?}", host.client_id());
|
||||
try!(io_service.register_handler(host));
|
||||
Ok(NetworkService {
|
||||
io_service: io_service,
|
||||
@ -25,21 +26,10 @@ impl<Message> NetworkService<Message> where Message: Send + 'static {
|
||||
})
|
||||
}
|
||||
|
||||
/// Send a message over the network. Normaly `HostIo::send` should be used. This can be used from non-io threads.
|
||||
pub fn send(&mut self, peer: &PeerId, packet_id: PacketId, protocol: ProtocolId, data: &[u8]) -> Result<(), NetworkError> {
|
||||
try!(self.io_service.send_message(NetworkIoMessage::Send {
|
||||
peer: *peer,
|
||||
packet_id: packet_id,
|
||||
protocol: protocol,
|
||||
data: data.to_vec()
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Regiter a new protocol handler with the event loop.
|
||||
pub fn register_protocol(&mut self, handler: Box<NetworkProtocolHandler<Message>+Send>, protocol: ProtocolId, versions: &[u8]) -> Result<(), NetworkError> {
|
||||
pub fn register_protocol(&mut self, handler: Arc<NetworkProtocolHandler<Message>+Send + Sync>, protocol: ProtocolId, versions: &[u8]) -> Result<(), NetworkError> {
|
||||
try!(self.io_service.send_message(NetworkIoMessage::AddHandler {
|
||||
handler: Some(handler),
|
||||
handler: handler,
|
||||
protocol: protocol,
|
||||
versions: versions.to_vec(),
|
||||
}));
|
||||
@ -55,7 +45,5 @@ impl<Message> NetworkService<Message> where Message: Send + 'static {
|
||||
pub fn io(&mut self) -> &mut IoService<NetworkIoMessage<Message>> {
|
||||
&mut self.io_service
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ use rlp::*;
|
||||
use network::connection::{EncryptedConnection, Packet};
|
||||
use network::handshake::Handshake;
|
||||
use error::*;
|
||||
use io::{IoContext};
|
||||
use network::error::{NetworkError, DisconnectReason};
|
||||
use network::host::*;
|
||||
use network::node::NodeId;
|
||||
@ -84,7 +85,7 @@ const PACKET_LAST: u8 = 0x7f;
|
||||
|
||||
impl Session {
|
||||
/// Create a new session out of comepleted handshake. Consumes handshake object.
|
||||
pub fn new<Host:Handler<Timeout=Token>>(h: Handshake, event_loop: &mut EventLoop<Host>, host: &HostInfo) -> Result<Session, UtilError> {
|
||||
pub fn new<Message>(h: Handshake, _io: &IoContext<Message>, host: &HostInfo) -> Result<Session, UtilError> where Message: Send + Sync + Clone {
|
||||
let id = h.id.clone();
|
||||
let connection = try!(EncryptedConnection::new(h));
|
||||
let mut session = Session {
|
||||
@ -99,7 +100,6 @@ impl Session {
|
||||
};
|
||||
try!(session.write_hello(host));
|
||||
try!(session.write_ping());
|
||||
try!(session.connection.register(event_loop));
|
||||
Ok(session)
|
||||
}
|
||||
|
||||
@ -109,16 +109,16 @@ impl Session {
|
||||
}
|
||||
|
||||
/// Readable IO handler. Returns packet data if available.
|
||||
pub fn readable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>, host: &HostInfo) -> Result<SessionData, UtilError> {
|
||||
match try!(self.connection.readable(event_loop)) {
|
||||
pub fn readable<Message>(&mut self, io: &IoContext<Message>, host: &HostInfo) -> Result<SessionData, UtilError> where Message: Send + Sync + Clone {
|
||||
match try!(self.connection.readable(io)) {
|
||||
Some(data) => Ok(try!(self.read_packet(data, host))),
|
||||
None => Ok(SessionData::None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Writable IO handler. Sends pending packets.
|
||||
pub fn writable<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>, _host: &HostInfo) -> Result<(), UtilError> {
|
||||
self.connection.writable(event_loop)
|
||||
pub fn writable<Message>(&mut self, io: &IoContext<Message>, _host: &HostInfo) -> Result<(), UtilError> where Message: Send + Sync + Clone {
|
||||
self.connection.writable(io)
|
||||
}
|
||||
|
||||
/// Checks if peer supports given capability
|
||||
@ -127,8 +127,8 @@ impl Session {
|
||||
}
|
||||
|
||||
/// Update registration with the event loop. Should be called at the end of the IO handler.
|
||||
pub fn reregister<Host:Handler>(&mut self, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
|
||||
self.connection.reregister(event_loop)
|
||||
pub fn update_socket<Host:Handler>(&self, reg:Token, event_loop: &mut EventLoop<Host>) -> Result<(), UtilError> {
|
||||
self.connection.update_socket(reg, event_loop)
|
||||
}
|
||||
|
||||
/// Send a protocol packet to peer.
|
||||
@ -182,7 +182,7 @@ impl Session {
|
||||
// map to protocol
|
||||
let protocol = self.info.capabilities[i].protocol;
|
||||
let pid = packet_id - self.info.capabilities[i].id_offset;
|
||||
return Ok(SessionData::Packet { data: packet.data, protocol: protocol, packet_id: pid } )
|
||||
Ok(SessionData::Packet { data: packet.data, protocol: protocol, packet_id: pid } )
|
||||
},
|
||||
_ => {
|
||||
debug!(target: "net", "Unkown packet: {:?}", packet_id);
|
||||
@ -212,7 +212,7 @@ impl Session {
|
||||
// Intersect with host capabilities
|
||||
// Leave only highset mutually supported capability version
|
||||
let mut caps: Vec<SessionCapabilityInfo> = Vec::new();
|
||||
for hc in host.capabilities.iter() {
|
||||
for hc in &host.capabilities {
|
||||
if peer_caps.iter().any(|c| c.protocol == hc.protocol && c.version == hc.version) {
|
||||
caps.push(SessionCapabilityInfo {
|
||||
protocol: hc.protocol,
|
||||
|
@ -169,7 +169,7 @@ impl HashDB for OverlayDB {
|
||||
match k {
|
||||
Some(&(ref d, rc)) if rc > 0 => Some(d),
|
||||
_ => {
|
||||
let memrc = k.map(|&(_, rc)| rc).unwrap_or(0);
|
||||
let memrc = k.map_or(0, |&(_, rc)| rc);
|
||||
match self.payload(key) {
|
||||
Some(x) => {
|
||||
let (d, rc) = x;
|
||||
@ -194,16 +194,11 @@ impl HashDB for OverlayDB {
|
||||
match k {
|
||||
Some(&(_, rc)) if rc > 0 => true,
|
||||
_ => {
|
||||
let memrc = k.map(|&(_, rc)| rc).unwrap_or(0);
|
||||
let memrc = k.map_or(0, |&(_, rc)| rc);
|
||||
match self.payload(key) {
|
||||
Some(x) => {
|
||||
let (_, rc) = x;
|
||||
if rc as i32 + memrc > 0 {
|
||||
true
|
||||
}
|
||||
else {
|
||||
false
|
||||
}
|
||||
rc as i32 + memrc > 0
|
||||
}
|
||||
// Replace above match arm with this once https://github.com/rust-lang/rust/issues/15287 is done.
|
||||
//Some((d, rc)) if rc + memrc > 0 => true,
|
||||
|
@ -41,7 +41,7 @@ impl Stream for RlpStream {
|
||||
stream
|
||||
}
|
||||
|
||||
fn append<'a, E>(&'a mut self, object: &E) -> &'a mut RlpStream where E: Encodable {
|
||||
fn append<E>(&mut self, object: &E) -> &mut RlpStream where E: Encodable {
|
||||
// encode given value and add it at the end of the stream
|
||||
object.encode(&mut self.encoder);
|
||||
|
||||
@ -52,7 +52,7 @@ impl Stream for RlpStream {
|
||||
self
|
||||
}
|
||||
|
||||
fn append_list<'a>(&'a mut self, len: usize) -> &'a mut RlpStream {
|
||||
fn append_list(&mut self, len: usize) -> &mut RlpStream {
|
||||
match len {
|
||||
0 => {
|
||||
// we may finish, if the appended list len is equal 0
|
||||
@ -69,7 +69,7 @@ impl Stream for RlpStream {
|
||||
self
|
||||
}
|
||||
|
||||
fn append_empty_data<'a>(&'a mut self) -> &'a mut RlpStream {
|
||||
fn append_empty_data(&mut self) -> &mut RlpStream {
|
||||
// self push raw item
|
||||
self.encoder.bytes.push(0x80);
|
||||
|
||||
|
@ -9,7 +9,7 @@ pub trait Decoder: Sized {
|
||||
/// TODO [arkpar] Please document me
|
||||
fn as_list(&self) -> Result<Vec<Self>, DecoderError>;
|
||||
/// TODO [Gav Wood] Please document me
|
||||
fn as_rlp<'a>(&'a self) -> &'a UntrustedRlp<'a>;
|
||||
fn as_rlp(&self) -> &UntrustedRlp;
|
||||
/// TODO [debris] Please document me
|
||||
fn as_raw(&self) -> &[u8];
|
||||
}
|
||||
@ -255,7 +255,7 @@ pub trait Stream: Sized {
|
||||
/// assert_eq!(out, vec![0xca, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g', 0x80]);
|
||||
/// }
|
||||
/// ```
|
||||
fn append_list<'a>(&'a mut self, len: usize) -> &'a mut Self;
|
||||
fn append_list(&mut self, len: usize) -> &mut Self;
|
||||
|
||||
/// Apends null to the end of stream, chainable.
|
||||
///
|
||||
@ -270,7 +270,7 @@ pub trait Stream: Sized {
|
||||
/// assert_eq!(out, vec![0xc2, 0x80, 0x80]);
|
||||
/// }
|
||||
/// ```
|
||||
fn append_empty_data<'a>(&'a mut self) -> &'a mut Self;
|
||||
fn append_empty_data(&mut self) -> &mut Self;
|
||||
|
||||
/// Appends raw (pre-serialised) RLP data. Use with caution. Chainable.
|
||||
fn append_raw<'a>(&'a mut self, bytes: &[u8], item_count: usize) -> &'a mut Self;
|
||||
|
@ -15,25 +15,25 @@ fn rlp_at() {
|
||||
assert!(rlp.is_list());
|
||||
//let animals = <Vec<String> as rlp::Decodable>::decode_untrusted(&rlp).unwrap();
|
||||
let animals: Vec<String> = rlp.as_val().unwrap();
|
||||
assert_eq!(animals, vec!["cat".to_string(), "dog".to_string()]);
|
||||
assert_eq!(animals, vec!["cat".to_owned(), "dog".to_owned()]);
|
||||
|
||||
let cat = rlp.at(0).unwrap();
|
||||
assert!(cat.is_data());
|
||||
assert_eq!(cat.as_raw(), &[0x83, b'c', b'a', b't']);
|
||||
//assert_eq!(String::decode_untrusted(&cat).unwrap(), "cat".to_string());
|
||||
assert_eq!(cat.as_val::<String>().unwrap(), "cat".to_string());
|
||||
//assert_eq!(String::decode_untrusted(&cat).unwrap(), "cat".to_owned());
|
||||
assert_eq!(cat.as_val::<String>().unwrap(), "cat".to_owned());
|
||||
|
||||
let dog = rlp.at(1).unwrap();
|
||||
assert!(dog.is_data());
|
||||
assert_eq!(dog.as_raw(), &[0x83, b'd', b'o', b'g']);
|
||||
//assert_eq!(String::decode_untrusted(&dog).unwrap(), "dog".to_string());
|
||||
assert_eq!(dog.as_val::<String>().unwrap(), "dog".to_string());
|
||||
//assert_eq!(String::decode_untrusted(&dog).unwrap(), "dog".to_owned());
|
||||
assert_eq!(dog.as_val::<String>().unwrap(), "dog".to_owned());
|
||||
|
||||
let cat_again = rlp.at(0).unwrap();
|
||||
assert!(cat_again.is_data());
|
||||
assert_eq!(cat_again.as_raw(), &[0x83, b'c', b'a', b't']);
|
||||
//assert_eq!(String::decode_untrusted(&cat_again).unwrap(), "cat".to_string());
|
||||
assert_eq!(cat_again.as_val::<String>().unwrap(), "cat".to_string());
|
||||
//assert_eq!(String::decode_untrusted(&cat_again).unwrap(), "cat".to_owned());
|
||||
assert_eq!(cat_again.as_val::<String>().unwrap(), "cat".to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
@ -268,13 +268,13 @@ fn decode_untrusted_u256() {
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_str() {
|
||||
let tests = vec![DTestPair("cat".to_string(), vec![0x83, b'c', b'a', b't']),
|
||||
DTestPair("dog".to_string(), vec![0x83, b'd', b'o', b'g']),
|
||||
DTestPair("Marek".to_string(),
|
||||
let tests = vec![DTestPair("cat".to_owned(), vec![0x83, b'c', b'a', b't']),
|
||||
DTestPair("dog".to_owned(), vec![0x83, b'd', b'o', b'g']),
|
||||
DTestPair("Marek".to_owned(),
|
||||
vec![0x85, b'M', b'a', b'r', b'e', b'k']),
|
||||
DTestPair("".to_string(), vec![0x80]),
|
||||
DTestPair("".to_owned(), vec![0x80]),
|
||||
DTestPair("Lorem ipsum dolor sit amet, consectetur adipisicing elit"
|
||||
.to_string(),
|
||||
.to_owned(),
|
||||
vec![0xb8, 0x38, b'L', b'o', b'r', b'e', b'm', b' ', b'i',
|
||||
b'p', b's', b'u', b'm', b' ', b'd', b'o', b'l', b'o',
|
||||
b'r', b' ', b's', b'i', b't', b' ', b'a', b'm', b'e',
|
||||
@ -311,14 +311,14 @@ fn decode_untrusted_vector_u64() {
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_vector_str() {
|
||||
let tests = vec![DTestPair(vec!["cat".to_string(), "dog".to_string()],
|
||||
let tests = vec![DTestPair(vec!["cat".to_owned(), "dog".to_owned()],
|
||||
vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'])];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_untrusted_vector_of_vectors_str() {
|
||||
let tests = vec![DTestPair(vec![vec!["cat".to_string()]],
|
||||
let tests = vec![DTestPair(vec![vec!["cat".to_owned()]],
|
||||
vec![0xc5, 0xc4, 0x83, b'c', b'a', b't'])];
|
||||
run_decode_tests(tests);
|
||||
}
|
||||
|
@ -288,7 +288,7 @@ impl<'a> BasicDecoder<'a> {
|
||||
|
||||
/// Return first item info
|
||||
fn payload_info(bytes: &[u8]) -> Result<PayloadInfo, DecoderError> {
|
||||
let item = match bytes.first().map(|&x| x) {
|
||||
let item = match bytes.first().cloned() {
|
||||
None => return Err(DecoderError::RlpIsTooShort),
|
||||
Some(0...0x7f) => PayloadInfo::new(0, 1),
|
||||
Some(l @ 0x80...0xb7) => PayloadInfo::new(1, l as usize - 0x80),
|
||||
@ -324,7 +324,7 @@ impl<'a> Decoder for BasicDecoder<'a> {
|
||||
|
||||
let bytes = self.rlp.as_raw();
|
||||
|
||||
match bytes.first().map(|&x| x) {
|
||||
match bytes.first().cloned() {
|
||||
// rlp is too short
|
||||
None => Err(DecoderError::RlpIsTooShort),
|
||||
// single byt value
|
||||
@ -355,12 +355,12 @@ impl<'a> Decoder for BasicDecoder<'a> {
|
||||
|
||||
fn as_list(&self) -> Result<Vec<Self>, DecoderError> {
|
||||
let v: Vec<BasicDecoder<'a>> = self.rlp.iter()
|
||||
.map(| i | BasicDecoder::new(i))
|
||||
.map(BasicDecoder::new)
|
||||
.collect();
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
fn as_rlp<'s>(&'s self) -> &'s UntrustedRlp<'s> {
|
||||
fn as_rlp(&self) -> &UntrustedRlp {
|
||||
&self.rlp
|
||||
}
|
||||
}
|
||||
@ -405,6 +405,7 @@ impl<T> Decodable for Option<T> where T: Decodable {
|
||||
macro_rules! impl_array_decodable {
|
||||
($index_type:ty, $len:expr ) => (
|
||||
impl<T> Decodable for [T; $len] where T: Decodable {
|
||||
#[allow(len_zero)]
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
let decoders = try!(decoder.as_list());
|
||||
|
||||
|
@ -42,7 +42,7 @@ pub trait Squeeze {
|
||||
|
||||
impl<K, T> Squeeze for HashMap<K, T> where K: Eq + Hash + Clone + HeapSizeOf, T: HeapSizeOf {
|
||||
fn squeeze(&mut self, size: usize) {
|
||||
if self.len() == 0 {
|
||||
if self.is_empty() {
|
||||
return
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ impl<K, T> Squeeze for HashMap<K, T> where K: Eq + Hash + Clone + HeapSizeOf, T:
|
||||
let all_entries = size_of_entry * self.len();
|
||||
let mut shrinked_size = all_entries;
|
||||
|
||||
while self.len() > 0 && shrinked_size > size {
|
||||
while !self.is_empty() && shrinked_size > size {
|
||||
// could be optimized
|
||||
let key = self.keys().next().unwrap().clone();
|
||||
self.remove(&key);
|
||||
|
@ -38,6 +38,7 @@ pub struct TrieDB<'db> {
|
||||
pub hash_count: usize,
|
||||
}
|
||||
|
||||
#[allow(wrong_self_convention)]
|
||||
impl<'db> TrieDB<'db> {
|
||||
/// Create a new trie with the backing database `db` and `root`
|
||||
/// Panics, if `root` does not exist
|
||||
@ -103,7 +104,7 @@ impl<'db> TrieDB<'db> {
|
||||
|
||||
match node {
|
||||
Node::Extension(_, payload) => handle_payload(payload),
|
||||
Node::Branch(payloads, _) => for payload in payloads.iter() { handle_payload(payload) },
|
||||
Node::Branch(payloads, _) => for payload in &payloads { handle_payload(payload) },
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
@ -141,12 +142,9 @@ impl<'db> TrieDB<'db> {
|
||||
},
|
||||
Node::Branch(ref nodes, ref value) => {
|
||||
try!(writeln!(f, ""));
|
||||
match value {
|
||||
&Some(v) => {
|
||||
try!(self.fmt_indent(f, deepness + 1));
|
||||
try!(writeln!(f, "=: {:?}", v.pretty()))
|
||||
},
|
||||
&None => {}
|
||||
if let Some(v) = *value {
|
||||
try!(self.fmt_indent(f, deepness + 1));
|
||||
try!(writeln!(f, "=: {:?}", v.pretty()))
|
||||
}
|
||||
for i in 0..16 {
|
||||
match self.get_node(nodes[i]) {
|
||||
|
@ -50,6 +50,7 @@ enum MaybeChanged<'a> {
|
||||
Changed(Bytes),
|
||||
}
|
||||
|
||||
#[allow(wrong_self_convention)]
|
||||
impl<'db> TrieDBMut<'db> {
|
||||
/// Create a new trie with the backing database `db` and empty `root`
|
||||
/// Initialise to the state entailed by the genesis block.
|
||||
@ -145,7 +146,7 @@ impl<'db> TrieDBMut<'db> {
|
||||
|
||||
match node {
|
||||
Node::Extension(_, payload) => handle_payload(payload),
|
||||
Node::Branch(payloads, _) => for payload in payloads.iter() { handle_payload(payload) },
|
||||
Node::Branch(payloads, _) => for payload in &payloads { handle_payload(payload) },
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
@ -178,12 +179,9 @@ impl<'db> TrieDBMut<'db> {
|
||||
},
|
||||
Node::Branch(ref nodes, ref value) => {
|
||||
try!(writeln!(f, ""));
|
||||
match value {
|
||||
&Some(v) => {
|
||||
try!(self.fmt_indent(f, deepness + 1));
|
||||
try!(writeln!(f, "=: {:?}", v.pretty()))
|
||||
},
|
||||
&None => {}
|
||||
if let Some(v) = *value {
|
||||
try!(self.fmt_indent(f, deepness + 1));
|
||||
try!(writeln!(f, "=: {:?}", v.pretty()))
|
||||
}
|
||||
for i in 0..16 {
|
||||
match self.get_node(nodes[i]) {
|
||||
@ -331,6 +329,7 @@ impl<'db> TrieDBMut<'db> {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(cyclomatic_complexity)]
|
||||
/// Determine the RLP of the node, assuming we're inserting `partial` into the
|
||||
/// node currently of data `old`. This will *not* delete any hash of `old` from the database;
|
||||
/// it will just return the new RLP that includes the new node.
|
||||
@ -694,7 +693,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn populate_trie<'db>(db: &'db mut HashDB, root: &'db mut H256, v: &Vec<(Vec<u8>, Vec<u8>)>) -> TrieDBMut<'db> {
|
||||
fn populate_trie<'db>(db: &'db mut HashDB, root: &'db mut H256, v: &[(Vec<u8>, Vec<u8>)]) -> TrieDBMut<'db> {
|
||||
let mut t = TrieDBMut::new(db, root);
|
||||
for i in 0..v.len() {
|
||||
let key: &[u8]= &v[i].0;
|
||||
@ -704,8 +703,8 @@ mod tests {
|
||||
t
|
||||
}
|
||||
|
||||
fn unpopulate_trie<'a, 'db>(t: &mut TrieDBMut<'db>, v: &Vec<(Vec<u8>, Vec<u8>)>) {
|
||||
for i in v.iter() {
|
||||
fn unpopulate_trie<'db>(t: &mut TrieDBMut<'db>, v: &[(Vec<u8>, Vec<u8>)]) {
|
||||
for i in v {
|
||||
let key: &[u8]= &i.0;
|
||||
t.remove(&key);
|
||||
}
|
||||
@ -761,7 +760,7 @@ mod tests {
|
||||
println!("TRIE MISMATCH");
|
||||
println!("");
|
||||
println!("{:?} vs {:?}", memtrie.root(), real);
|
||||
for i in x.iter() {
|
||||
for i in &x {
|
||||
println!("{:?} -> {:?}", i.0.pretty(), i.1.pretty());
|
||||
}
|
||||
println!("{:?}", memtrie);
|
||||
@ -774,7 +773,7 @@ mod tests {
|
||||
println!("");
|
||||
println!("remaining: {:?}", memtrie.db_items_remaining());
|
||||
println!("{:?} vs {:?}", memtrie.root(), real);
|
||||
for i in x.iter() {
|
||||
for i in &x {
|
||||
println!("{:?} -> {:?}", i.0.pretty(), i.1.pretty());
|
||||
}
|
||||
println!("{:?}", memtrie);
|
||||
@ -1051,12 +1050,12 @@ mod tests {
|
||||
println!("TRIE MISMATCH");
|
||||
println!("");
|
||||
println!("ORIGINAL... {:?}", memtrie.root());
|
||||
for i in x.iter() {
|
||||
for i in &x {
|
||||
println!("{:?} -> {:?}", i.0.pretty(), i.1.pretty());
|
||||
}
|
||||
println!("{:?}", memtrie);
|
||||
println!("SORTED... {:?}", memtrie_sorted.root());
|
||||
for i in y.iter() {
|
||||
for i in &y {
|
||||
println!("{:?} -> {:?}", i.0.pretty(), i.1.pretty());
|
||||
}
|
||||
println!("{:?}", memtrie_sorted);
|
||||
|
@ -200,7 +200,7 @@ macro_rules! construct_uint {
|
||||
#[inline]
|
||||
fn byte(&self, index: usize) -> u8 {
|
||||
let &$name(ref arr) = self;
|
||||
(arr[index / 8] >> ((index % 8)) * 8) as u8
|
||||
(arr[index / 8] >> (((index % 8)) * 8)) as u8
|
||||
}
|
||||
|
||||
fn to_bytes(&self, bytes: &mut[u8]) {
|
||||
@ -446,16 +446,16 @@ macro_rules! construct_uint {
|
||||
|
||||
impl FromJson for $name {
|
||||
fn from_json(json: &Json) -> Self {
|
||||
match json {
|
||||
&Json::String(ref s) => {
|
||||
match *json {
|
||||
Json::String(ref s) => {
|
||||
if s.len() >= 2 && &s[0..2] == "0x" {
|
||||
FromStr::from_str(&s[2..]).unwrap_or(Default::default())
|
||||
FromStr::from_str(&s[2..]).unwrap_or_else(|_| Default::default())
|
||||
} else {
|
||||
Uint::from_dec_str(s).unwrap_or(Default::default())
|
||||
Uint::from_dec_str(s).unwrap_or_else(|_| Default::default())
|
||||
}
|
||||
},
|
||||
&Json::U64(u) => From::from(u),
|
||||
&Json::I64(i) => From::from(i as u64),
|
||||
Json::U64(u) => From::from(u),
|
||||
Json::I64(i) => From::from(i as u64),
|
||||
_ => Uint::zero(),
|
||||
}
|
||||
}
|
||||
@ -488,7 +488,7 @@ macro_rules! construct_uint {
|
||||
for i in 0..bytes.len() {
|
||||
let rev = bytes.len() - 1 - i;
|
||||
let pos = rev / 8;
|
||||
ret[pos] += (bytes[i] as u64) << (rev % 8) * 8;
|
||||
ret[pos] += (bytes[i] as u64) << ((rev % 8) * 8);
|
||||
}
|
||||
$name(ret)
|
||||
}
|
||||
@ -500,7 +500,7 @@ macro_rules! construct_uint {
|
||||
fn from_str(value: &str) -> Result<$name, Self::Err> {
|
||||
let bytes: Vec<u8> = match value.len() % 2 == 0 {
|
||||
true => try!(value.from_hex()),
|
||||
false => try!(("0".to_string() + value).from_hex())
|
||||
false => try!(("0".to_owned() + value).from_hex())
|
||||
};
|
||||
|
||||
let bytes_ref: &[u8] = &bytes;
|
||||
@ -1061,6 +1061,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(eq_op)]
|
||||
pub fn uint256_comp_test() {
|
||||
let small = U256([10u64, 0, 0, 0]);
|
||||
let big = U256([0x8C8C3EE70C644118u64, 0x0209E7378231E632, 0, 0]);
|
||||
|
Loading…
Reference in New Issue
Block a user