From 96f4c10453cc6c3941e22d9a282c35ee14bf84bc Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 20 Oct 2016 23:41:15 +0200 Subject: [PATCH] sweep most unwraps from ethcore crate, dapps crate (#2762) * sweep most unwraps from ethcore crate * purge unwrap from dapps server * whitespace [ci:none] --- Cargo.lock | 1 + dapps/Cargo.toml | 1 + dapps/js-glue/src/lib.rs.in | 1 + dapps/src/api/response.rs | 12 ++++++++---- dapps/src/apps/cache.rs | 7 ++++--- dapps/src/apps/urlhint.rs | 6 +++--- dapps/src/handlers/content.rs | 15 ++++++++------- dapps/src/handlers/echo.rs | 12 ++++++++---- dapps/src/lib.rs | 11 ++++++++--- dapps/src/page/handler.rs | 7 ++++++- dapps/src/proxypac.rs | 2 +- dapps/src/router/mod.rs | 16 ++++++++++++---- ethcore/src/account_provider.rs | 5 +++-- ethcore/src/block.rs | 2 +- ethcore/src/blockchain/blockchain.rs | 3 ++- ethcore/src/cache_manager.rs | 17 +++++++++++------ ethcore/src/client/client.rs | 6 +++++- ethcore/src/engines/mod.rs | 8 ++++++-- ethcore/src/evm/interpreter/mod.rs | 6 +++--- ethcore/src/evm/jit.rs | 2 +- ethcore/src/executive.rs | 12 ++++++++++-- ethcore/src/header.rs | 10 ++++++---- ethcore/src/miner/miner.rs | 10 +++++++--- ethcore/src/miner/price_info.rs | 15 ++++++++++----- ethcore/src/miner/transaction_queue.rs | 4 ++-- ethcore/src/spec/spec.rs | 3 ++- ethcore/src/state/mod.rs | 17 +++++++++-------- ethcore/src/state_db.rs | 9 +++++---- ethcore/src/trace/db.rs | 2 +- ethcore/src/types/transaction.rs | 5 +++-- ethcore/src/verification/verification.rs | 3 ++- 31 files changed, 150 insertions(+), 80 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7863b8890..8c00acfba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -335,6 +335,7 @@ dependencies = [ "jsonrpc-http-server 6.1.1 (git+https://github.com/ethcore/jsonrpc-http-server.git)", "linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime_guess 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "parity-dapps 1.4.0 (git+https://github.com/ethcore/parity-ui.git)", "parity-dapps-glue 1.4.0", diff --git a/dapps/Cargo.toml b/dapps/Cargo.toml index 733bb9b0e..77ed00e19 100644 --- a/dapps/Cargo.toml +++ b/dapps/Cargo.toml @@ -29,6 +29,7 @@ ethcore-util = { path = "../util" } fetch = { path = "../util/fetch" } parity-ui = { path = "./ui" } parity-dapps-glue = { path = "./js-glue" } +mime = "0.2" ### DEPRECATED parity-dapps = { git = "https://github.com/ethcore/parity-ui.git", version = "1.4" } parity-dapps-home = { git = "https://github.com/ethcore/parity-ui.git", version = "1.4" } diff --git a/dapps/js-glue/src/lib.rs.in b/dapps/js-glue/src/lib.rs.in index fa6c17de5..07c57705f 100644 --- a/dapps/js-glue/src/lib.rs.in +++ b/dapps/js-glue/src/lib.rs.in @@ -27,6 +27,7 @@ use std::default::Default; pub struct File { pub path: &'static str, pub content: &'static [u8], + // TODO: use strongly-typed MIME. pub content_type: &'static str, } diff --git a/dapps/src/api/response.rs b/dapps/src/api/response.rs index ce6eb2921..4064490b3 100644 --- a/dapps/src/api/response.rs +++ b/dapps/src/api/response.rs @@ -19,12 +19,16 @@ use serde_json; use endpoint::Handler; use handlers::{ContentHandler, EchoHandler}; -pub fn as_json(val: &T) -> Box { - Box::new(ContentHandler::ok(serde_json::to_string(val).unwrap(), "application/json".to_owned())) +pub fn as_json(val: &T) -> Box { + let json = serde_json::to_string(val) + .expect("serialization to string is infallible; qed"); + Box::new(ContentHandler::ok(json, mime!(Application/Json))) } -pub fn as_json_error(val: &T) -> Box { - Box::new(ContentHandler::not_found(serde_json::to_string(val).unwrap(), "application/json".to_owned())) +pub fn as_json_error(val: &T) -> Box { + let json = serde_json::to_string(val) + .expect("serialization to string is infallible; qed"); + Box::new(ContentHandler::not_found(json, mime!(Application/Json))) } pub fn ping_response(local_domain: &str) -> Box { diff --git a/dapps/src/apps/cache.rs b/dapps/src/apps/cache.rs index be9521cf9..54c1354cd 100644 --- a/dapps/src/apps/cache.rs +++ b/dapps/src/apps/cache.rs @@ -54,8 +54,10 @@ impl ContentCache { } let mut removed = Vec::with_capacity(len - expected_size); - while len > expected_size { - let entry = self.cache.pop_front().unwrap(); + + while self.cache.len() > expected_size { + let entry = self.cache.pop_front().expect("expected_size bounded at 0, len is greater; qed"); + match entry.1 { ContentStatus::Fetching(ref fetch) => { trace!(target: "dapps", "Aborting {} because of limit.", entry.0); @@ -73,7 +75,6 @@ impl ContentCache { } removed.push(entry); - len -= 1; } removed } diff --git a/dapps/src/apps/urlhint.rs b/dapps/src/apps/urlhint.rs index 2b86c0777..27769d07a 100644 --- a/dapps/src/apps/urlhint.rs +++ b/dapps/src/apps/urlhint.rs @@ -156,9 +156,9 @@ impl URLHintContract { } let mut it = vec.into_iter(); - let account_slash_repo = it.next().unwrap(); - let commit = it.next().unwrap(); - let owner = it.next().unwrap(); + let account_slash_repo = it.next().expect("element 0 of 3-len vector known to exist; qed"); + let commit = it.next().expect("element 1 of 3-len vector known to exist; qed"); + let owner = it.next().expect("element 2 of 3-len vector known to exist qed"); match (account_slash_repo, commit, owner) { (Token::String(account_slash_repo), Token::FixedBytes(commit), Token::Address(owner)) => { diff --git a/dapps/src/handlers/content.rs b/dapps/src/handlers/content.rs index e1eb1cd1e..6bde9cf84 100644 --- a/dapps/src/handlers/content.rs +++ b/dapps/src/handlers/content.rs @@ -19,6 +19,7 @@ use std::io::Write; use hyper::{header, server, Decoder, Encoder, Next}; use hyper::net::HttpStream; +use hyper::mime::Mime; use hyper::status::StatusCode; use util::version; @@ -29,22 +30,22 @@ use handlers::add_security_headers; pub struct ContentHandler { code: StatusCode, content: String, - mimetype: String, + mimetype: Mime, write_pos: usize, safe_to_embed_at_port: Option, } impl ContentHandler { - pub fn ok(content: String, mimetype: String) -> Self { + pub fn ok(content: String, mimetype: Mime) -> Self { Self::new(StatusCode::Ok, content, mimetype) } - pub fn not_found(content: String, mimetype: String) -> Self { + pub fn not_found(content: String, mimetype: Mime) -> Self { Self::new(StatusCode::NotFound, content, mimetype) } pub fn html(code: StatusCode, content: String, embeddable_at: Option) -> Self { - Self::new_embeddable(code, content, "text/html".into(), embeddable_at) + Self::new_embeddable(code, content, mime!(Text/Html), embeddable_at) } pub fn error(code: StatusCode, title: &str, message: &str, details: Option<&str>) -> Self { @@ -61,11 +62,11 @@ impl ContentHandler { ), embeddable_at) } - pub fn new(code: StatusCode, content: String, mimetype: String) -> Self { + pub fn new(code: StatusCode, content: String, mimetype: Mime) -> Self { Self::new_embeddable(code, content, mimetype, None) } - pub fn new_embeddable(code: StatusCode, content: String, mimetype: String, embeddable_at: Option) -> Self { + pub fn new_embeddable(code: StatusCode, content: String, mimetype: Mime, embeddable_at: Option) -> Self { ContentHandler { code: code, content: content, @@ -87,7 +88,7 @@ impl server::Handler for ContentHandler { fn on_response(&mut self, res: &mut server::Response) -> Next { res.set_status(self.code); - res.headers_mut().set(header::ContentType(self.mimetype.parse().unwrap())); + res.headers_mut().set(header::ContentType(self.mimetype.clone())); add_security_headers(&mut res.headers_mut(), self.safe_to_embed_at_port.clone()); Next::write() } diff --git a/dapps/src/handlers/echo.rs b/dapps/src/handlers/echo.rs index b2ea8f580..9266de0c6 100644 --- a/dapps/src/handlers/echo.rs +++ b/dapps/src/handlers/echo.rs @@ -82,7 +82,7 @@ impl server::Handler for EchoHandler { // Don't even read the payload if origin is forbidden! if let Cors::Forbidden = self.cors { - self.handler = Some(ContentHandler::ok(String::new(), "text/plain".into())); + self.handler = Some(ContentHandler::ok(String::new(), mime!(Text/Plain))); Next::write() } else { Next::read() @@ -92,7 +92,7 @@ impl server::Handler for EchoHandler { fn on_request_readable(&mut self, decoder: &mut Decoder) -> Next { match decoder.read_to_string(&mut self.content) { Ok(0) => { - self.handler = Some(ContentHandler::ok(self.content.clone(), "application/json".into())); + self.handler = Some(ContentHandler::ok(self.content.clone(), mime!(Application/Json))); Next::write() }, Ok(_) => Next::read(), @@ -114,11 +114,15 @@ impl server::Handler for EchoHandler { ])); headers.set(header::AccessControlAllowOrigin::Value(domain.clone())); } - self.handler.as_mut().unwrap().on_response(res) + self.handler.as_mut() + .expect("handler always set in on_request, which is before now; qed") + .on_response(res) } fn on_response_writable(&mut self, encoder: &mut Encoder) -> Next { - self.handler.as_mut().unwrap().on_response_writable(encoder) + self.handler.as_mut() + .expect("handler always set in on_request, which is before now; qed") + .on_response_writable(encoder) } } diff --git a/dapps/src/lib.rs b/dapps/src/lib.rs index 8fd826ff9..9e9a26ee3 100644 --- a/dapps/src/lib.rs +++ b/dapps/src/lib.rs @@ -43,10 +43,8 @@ #![warn(missing_docs)] #![cfg_attr(feature="nightly", plugin(clippy))] -#[macro_use] -extern crate log; -extern crate url as url_lib; extern crate hyper; +extern crate url as url_lib; extern crate unicase; extern crate serde; extern crate serde_json; @@ -61,6 +59,13 @@ extern crate ethcore_rpc; extern crate ethcore_util as util; extern crate linked_hash_map; extern crate fetch; + +#[macro_use] +extern crate log; + +#[macro_use] +extern crate mime; + #[cfg(test)] extern crate ethcore_devtools as devtools; diff --git a/dapps/src/page/handler.rs b/dapps/src/page/handler.rs index f975a98c8..0962f22c7 100644 --- a/dapps/src/page/handler.rs +++ b/dapps/src/page/handler.rs @@ -128,7 +128,12 @@ impl server::Handler for PageHandler { match self.file { ServedFile::File(ref f) => { res.set_status(StatusCode::Ok); - res.headers_mut().set(header::ContentType(f.content_type().parse().unwrap())); + + match f.content_type().parse() { + Ok(mime) => res.headers_mut().set(header::ContentType(mime)), + Err(()) => debug!(target: "page_handler", "invalid MIME type: {}", f.content_type()), + } + // Security headers: add_security_headers(&mut res.headers_mut(), self.safe_to_embed_at_port); Next::write() diff --git a/dapps/src/proxypac.rs b/dapps/src/proxypac.rs index aaa68dc0c..cd225330a 100644 --- a/dapps/src/proxypac.rs +++ b/dapps/src/proxypac.rs @@ -42,7 +42,7 @@ function FindProxyForURL(url, host) {{ }} "#, DAPPS_DOMAIN, path.host, path.port); - Box::new(ContentHandler::ok(content, "application/javascript".to_owned())) + Box::new(ContentHandler::ok(content, mime!(Application/Javascript))) } } diff --git a/dapps/src/router/mod.rs b/dapps/src/router/mod.rs index e3ff6e64f..2748b5d24 100644 --- a/dapps/src/router/mod.rs +++ b/dapps/src/router/mod.rs @@ -81,11 +81,15 @@ impl server::Handler for Router { self.handler = match endpoint { // First check special endpoints (ref path, ref endpoint) if self.special.contains_key(endpoint) => { - self.special.get(endpoint).unwrap().to_async_handler(path.clone().unwrap_or_default(), control) + self.special.get(endpoint) + .expect("special known to contain key; qed") + .to_async_handler(path.clone().unwrap_or_default(), control) }, // Then delegate to dapp (Some(ref path), _) if self.endpoints.contains_key(&path.app_id) => { - self.endpoints.get(&path.app_id).unwrap().to_async_handler(path.clone(), control) + self.endpoints.get(&path.app_id) + .expect("special known to contain key; qed") + .to_async_handler(path.clone(), control) }, // Try to resolve and fetch the dapp (Some(ref path), _) if self.fetch.contains(&path.app_id) => { @@ -108,7 +112,9 @@ impl server::Handler for Router { }, // RPC by default _ => { - self.special.get(&SpecialEndpoint::Rpc).unwrap().to_async_handler(EndpointPath::default(), control) + self.special.get(&SpecialEndpoint::Rpc) + .expect("RPC endpoint always stored; qed") + .to_async_handler(EndpointPath::default(), control) } }; @@ -143,7 +149,9 @@ impl Router { allowed_hosts: Option>, ) -> Self { - let handler = special.get(&SpecialEndpoint::Utils).unwrap().to_handler(EndpointPath::default()); + let handler = special.get(&SpecialEndpoint::Utils) + .expect("Utils endpoint always stored; qed") + .to_handler(EndpointPath::default()); Router { control: Some(control), main_page: main_page, diff --git a/ethcore/src/account_provider.rs b/ethcore/src/account_provider.rs index 3f4511f4b..5c1398e02 100644 --- a/ethcore/src/account_provider.rs +++ b/ethcore/src/account_provider.rs @@ -176,7 +176,8 @@ impl AccountProvider { AccountProvider { unlocked: Mutex::new(HashMap::new()), address_book: Mutex::new(AddressBook::new(Default::default())), - sstore: Box::new(EthStore::open(Box::new(NullDir::default())).unwrap()) + sstore: Box::new(EthStore::open(Box::new(NullDir::default())) + .expect("NullDir load always succeeds; qed")) } } @@ -187,7 +188,7 @@ impl AccountProvider { /// Creates new random account and returns address and public key pub fn new_account_and_public(&self, password: &str) -> Result<(Address, Public), Error> { - let acc = Random.generate().unwrap(); + let acc = Random.generate().expect("secp context has generation capabilities; qed"); let public = acc.public().clone(); let secret = acc.secret().clone(); let address = try!(self.sstore.insert_account(secret, password)); diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index 5cd983511..6645bf492 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -350,7 +350,7 @@ impl<'x> OpenBlock<'x> { let t = outcome.trace; self.block.traces.as_mut().map(|traces| traces.push(t)); self.block.receipts.push(outcome.receipt); - Ok(self.block.receipts.last().unwrap()) + Ok(self.block.receipts.last().expect("receipt just pushed; qed")) } Err(x) => Err(From::from(x)) } diff --git a/ethcore/src/blockchain/blockchain.rs b/ethcore/src/blockchain/blockchain.rs index 67d9cd894..799fa383d 100644 --- a/ethcore/src/blockchain/blockchain.rs +++ b/ethcore/src/blockchain/blockchain.rs @@ -132,7 +132,8 @@ pub trait BlockProvider { /// Returns the header of the genesis block. fn genesis_header(&self) -> Header { - self.block_header(&self.genesis_hash()).unwrap() + self.block_header(&self.genesis_hash()) + .expect("Genesis header always stored; qed") } /// Returns numbers of blocks containing given bloom. diff --git a/ethcore/src/cache_manager.rs b/ethcore/src/cache_manager.rs index 715f68a57..02c8f08a1 100644 --- a/ethcore/src/cache_manager.rs +++ b/ethcore/src/cache_manager.rs @@ -55,18 +55,23 @@ impl CacheManager where T: Eq + Hash { } for _ in 0..COLLECTION_QUEUE_SIZE { - let current_size = notify_unused(self.cache_usage.pop_back().unwrap()); - self.cache_usage.push_front(Default::default()); - if current_size < self.max_cache_size { - break; + if let Some(back) = self.cache_usage.pop_back() { + let current_size = notify_unused(back); + self.cache_usage.push_front(Default::default()); + if current_size < self.max_cache_size { + break + } } } } fn rotate_cache_if_needed(&mut self) { + if self.cache_usage.len() == 0 { return } + if self.cache_usage[0].len() * self.bytes_per_cache_entry > self.pref_cache_size / COLLECTION_QUEUE_SIZE { - let cache = self.cache_usage.pop_back().unwrap(); - self.cache_usage.push_front(cache); + if let Some(cache) = self.cache_usage.pop_back() { + self.cache_usage.push_front(cache); + } } } } diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 1204cd12f..ffb44c8c2 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -478,7 +478,11 @@ impl Client { if number >= self.history { let n = number - self.history; - state.mark_canonical(&mut batch, n, &chain.block_hash(n).unwrap()).expect("DB commit failed"); + if let Some(ancient_hash) = chain.block_hash(n) { + state.mark_canonical(&mut batch, n, &ancient_hash).expect("DB commit failed"); + } else { + debug!(target: "client", "Missing expected hash for block {}", n); + } } let route = chain.insert_block(&mut batch, block_data, receipts); diff --git a/ethcore/src/engines/mod.rs b/ethcore/src/engines/mod.rs index 5234553ef..e6325957a 100644 --- a/ethcore/src/engines/mod.rs +++ b/ethcore/src/engines/mod.rs @@ -123,10 +123,14 @@ pub trait Engine : Sync + Send { fn is_builtin(&self, a: &Address) -> bool { self.builtins().contains_key(a) } /// Determine the code execution cost of the builtin contract with address `a`. /// Panics if `is_builtin(a)` is not true. - fn cost_of_builtin(&self, a: &Address, input: &[u8]) -> U256 { self.builtins().get(a).unwrap().cost(input.len()) } + fn cost_of_builtin(&self, a: &Address, input: &[u8]) -> U256 { + self.builtins().get(a).expect("queried cost of nonexistent builtin").cost(input.len()) + } /// Execution the builtin contract `a` on `input` and return `output`. /// Panics if `is_builtin(a)` is not true. - fn execute_builtin(&self, a: &Address, input: &[u8], output: &mut BytesRef) { self.builtins().get(a).unwrap().execute(input, output); } + fn execute_builtin(&self, a: &Address, input: &[u8], output: &mut BytesRef) { + self.builtins().get(a).expect("attempted to execute nonexistent builtin").execute(input, output); + } // TODO: sealing stuff - though might want to leave this for later. } diff --git a/ethcore/src/evm/interpreter/mod.rs b/ethcore/src/evm/interpreter/mod.rs index 51dd8bacd..f9d386a21 100644 --- a/ethcore/src/evm/interpreter/mod.rs +++ b/ethcore/src/evm/interpreter/mod.rs @@ -102,7 +102,7 @@ impl evm::Evm for Interpreter { let mut informant = informant::EvmInformant::new(ext.depth()); - let code = ¶ms.code.as_ref().unwrap(); + let code = ¶ms.code.as_ref().expect("exec always called with code; qed"); let valid_jump_destinations = self.cache.jump_destinations(¶ms.code_hash, code); let mut gasometer = Gasometer::::new(try!(Cost::from_u256(params.gas))); @@ -318,11 +318,11 @@ impl Interpreter { // Get sender & receive addresses, check if we have balance let (sender_address, receive_address, has_balance, call_type) = match instruction { instructions::CALL => { - let has_balance = ext.balance(¶ms.address) >= value.unwrap(); + let has_balance = ext.balance(¶ms.address) >= value.expect("value set for all but delegate call; qed"); (¶ms.address, &code_address, has_balance, CallType::Call) }, instructions::CALLCODE => { - let has_balance = ext.balance(¶ms.address) >= value.unwrap(); + let has_balance = ext.balance(¶ms.address) >= value.expect("value set for all but delegate call; qed"); (¶ms.address, ¶ms.address, has_balance, CallType::CallCode) }, instructions::DELEGATECALL => (¶ms.sender, ¶ms.address, true, CallType::DelegateCall), diff --git a/ethcore/src/evm/jit.rs b/ethcore/src/evm/jit.rs index 3487d9a59..16a7b29e5 100644 --- a/ethcore/src/evm/jit.rs +++ b/ethcore/src/evm/jit.rs @@ -359,7 +359,7 @@ impl evm::Evm for JitEvm { data.timestamp = ext.env_info().timestamp as i64; self.context = Some(unsafe { evmjit::ContextHandle::new(data, schedule, &mut ext_handle) }); - let mut context = self.context.as_mut().unwrap(); + let mut context = self.context.as_mut().expect("context handle set on the prior line; qed"); let res = context.exec(); match res { diff --git a/ethcore/src/executive.rs b/ethcore/src/executive.rs index 54fd90203..5856086c6 100644 --- a/ethcore/src/executive.rs +++ b/ethcore/src/executive.rs @@ -427,8 +427,16 @@ impl<'a> Executive<'a> { trace!("exec::finalize: t.gas={}, sstore_refunds={}, suicide_refunds={}, refunds_bound={}, gas_left_prerefund={}, refunded={}, gas_left={}, gas_used={}, refund_value={}, fees_value={}\n", t.gas, sstore_refunds, suicide_refunds, refunds_bound, gas_left_prerefund, refunded, gas_left, gas_used, refund_value, fees_value); - trace!("exec::finalize: Refunding refund_value={}, sender={}\n", refund_value, t.sender().unwrap()); - self.state.add_balance(&t.sender().unwrap(), &refund_value); + let sender = match t.sender() { + Ok(sender) => sender, + Err(e) => { + debug!(target: "executive", "attempted to finalize transaction without sender: {}", e); + return Err(ExecutionError::Internal); + } + }; + + trace!("exec::finalize: Refunding refund_value={}, sender={}\n", refund_value, sender); + self.state.add_balance(&sender, &refund_value); trace!("exec::finalize: Compensating author: fees_value={}, author={}\n", fees_value, &self.info.author); self.state.add_balance(&self.info.author, &fees_value); diff --git a/ethcore/src/header.rs b/ethcore/src/header.rs index 7d86cfd61..50f1f573c 100644 --- a/ethcore/src/header.rs +++ b/ethcore/src/header.rs @@ -199,8 +199,9 @@ impl Header { match &mut *hash { &mut Some(ref h) => h.clone(), hash @ &mut None => { - *hash = Some(self.rlp_sha3(Seal::With)); - hash.as_ref().unwrap().clone() + let h = self.rlp_sha3(Seal::With); + *hash = Some(h.clone()); + h } } } @@ -211,8 +212,9 @@ impl Header { match &mut *hash { &mut Some(ref h) => h.clone(), hash @ &mut None => { - *hash = Some(self.rlp_sha3(Seal::Without)); - hash.as_ref().unwrap().clone() + let h = self.rlp_sha3(Seal::Without); + *hash = Some(h.clone()); + h } } } diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 8e88db29c..43b6ca721 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -136,7 +136,7 @@ impl GasPriceCalibrator { let gas_per_tx: f32 = 21000.0; let wei_per_gas: f32 = wei_per_usd * usd_per_tx / gas_per_tx; info!(target: "miner", "Updated conversion rate to Ξ1 = {} ({} wei/gas)", Colour::White.bold().paint(format!("US${}", usd_per_eth)), Colour::Yellow.bold().paint(format!("{}", wei_per_gas))); - set_price(U256::from_dec_str(&format!("{:.0}", wei_per_gas)).unwrap()); + set_price(U256::from(wei_per_gas as u64)); }) { self.next_calibration = Instant::now() + self.options.recalibration_period; } else { @@ -747,7 +747,7 @@ impl MinerService for Miner { let mut transaction_queue = self.transaction_queue.lock(); let import = self.add_transactions_to_queue( chain, vec![transaction], TransactionOrigin::Local, &mut transaction_queue - ).pop().unwrap(); + ).pop().expect("one result returned per added transaction; one added => one result; qed"); match import { Ok(ref res) => { @@ -869,7 +869,11 @@ impl MinerService for Miner { gas_used: receipt.gas_used - prev_gas, contract_address: match tx.action { Action::Call(_) => None, - Action::Create => Some(contract_address(&tx.sender().unwrap(), &tx.nonce)), + Action::Create => { + let sender = tx.sender() + .expect("transactions in pending block have already been checked for valid sender; qed"); + Some(contract_address(&sender, &tx.nonce)) + } }, logs: receipt.logs.clone(), } diff --git a/ethcore/src/miner/price_info.rs b/ethcore/src/miner/price_info.rs index 268a5dfb4..77bc1ce0f 100644 --- a/ethcore/src/miner/price_info.rs +++ b/ethcore/src/miner/price_info.rs @@ -52,7 +52,8 @@ impl Handler for SetPriceH .and_then(|json| json.find_path(&["result", "ethusd"]) .and_then(|obj| match *obj { Json::String(ref s) => Some((self.set_price)(PriceInfo { - ethusd: FromStr::from_str(s).unwrap() + ethusd: FromStr::from_str(s) + .expect("Etherscan API will always return properly formatted price; qed") })), _ => None, })); @@ -67,10 +68,14 @@ impl PriceInfo { let client = try!(Client::new().map_err(|_| ())); thread::spawn(move || { let (tx, rx) = mpsc::channel(); - let _ = client.request(FromStr::from_str("http://api.etherscan.io/api?module=stats&action=ethprice").unwrap(), SetPriceHandler { - set_price: set_price, - channel: tx, - }).ok().and_then(|_| rx.recv().ok()); + let url = FromStr::from_str("http://api.etherscan.io/api?module=stats&action=ethprice") + .expect("string known to be a valid URL; qed"); + let _ = client.request( + url, + SetPriceHandler { + set_price: set_price, + channel: tx, + }).ok().and_then(|_| rx.recv().ok()); client.close(); }); Ok(()) diff --git a/ethcore/src/miner/transaction_queue.rs b/ethcore/src/miner/transaction_queue.rs index 1adbd3a62..fa0cce1e6 100644 --- a/ethcore/src/miner/transaction_queue.rs +++ b/ethcore/src/miner/transaction_queue.rs @@ -700,7 +700,7 @@ impl TransactionQueue { None => vec![], }; for k in nonces_from_sender { - let order = self.current.drop(&sender, &k).unwrap(); + let order = self.current.drop(&sender, &k).expect("transaction known to be in self.current; qed"); self.current.insert(sender, k, order.penalize()); } // Same thing for future @@ -709,7 +709,7 @@ impl TransactionQueue { None => vec![], }; for k in nonces_from_sender { - let order = self.future.drop(&sender, &k).unwrap(); + let order = self.future.drop(&sender, &k).expect("transaction known to be in self.future; qed"); self.future.insert(sender, k, order.penalize()); } } diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index 34f7afff4..694ec4be8 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -150,7 +150,8 @@ impl Spec { if self.state_root_memo.read().is_none() { *self.state_root_memo.write() = Some(self.genesis_state.root()); } - self.state_root_memo.read().as_ref().unwrap().clone() + self.state_root_memo.read().as_ref().cloned() + .expect("state root memo ensured to be set at this point; qed") } /// Get the known knodes of the network in enode format. diff --git a/ethcore/src/state/mod.rs b/ethcore/src/state/mod.rs index edabad2d7..e84a96ac2 100644 --- a/ethcore/src/state/mod.rs +++ b/ethcore/src/state/mod.rs @@ -520,7 +520,7 @@ impl State { } { - let mut trie = factories.trie.from_existing(db.as_hashdb_mut(), root).unwrap(); + let mut trie = try!(factories.trie.from_existing(db.as_hashdb_mut(), root)); for (address, ref mut a) in accounts.iter_mut().filter(|&(_, ref a)| a.is_dirty()) { a.state = AccountState::Committed; match a.account { @@ -691,14 +691,15 @@ impl State { } self.note_cache(a); - match &mut self.cache.borrow_mut().get_mut(a).unwrap().account { - &mut Some(ref mut acc) => not_default(acc), - slot => *slot = Some(default()), - } - - // at this point the account is guaranteed to be in the cache. + // at this point the entry is guaranteed to be in the cache. RefMut::map(self.cache.borrow_mut(), |c| { - let mut entry = c.get_mut(a).unwrap(); + let mut entry = c.get_mut(a).expect("entry known to exist in the cache; qed"); + + match &mut entry.account { + &mut Some(ref mut acc) => not_default(acc), + slot => *slot = Some(default()), + } + // set the dirty flag after changing account data. entry.state = AccountState::Dirty; match entry.account { diff --git a/ethcore/src/state_db.rs b/ethcore/src/state_db.rs index 01f384570..2823b9b1b 100644 --- a/ethcore/src/state_db.rs +++ b/ethcore/src/state_db.rs @@ -134,10 +134,11 @@ impl StateDB { let hash_count_entry = db.get(COL_ACCOUNT_BLOOM, ACCOUNT_BLOOM_HASHCOUNT_KEY) .expect("Low-level database error"); - if hash_count_entry.is_none() { - return Bloom::new(ACCOUNT_BLOOM_SPACE, DEFAULT_ACCOUNT_PRESET); - } - let hash_count_bytes = hash_count_entry.unwrap(); + let hash_count_bytes = match hash_count_entry { + Some(bytes) => bytes, + None => return Bloom::new(ACCOUNT_BLOOM_SPACE, DEFAULT_ACCOUNT_PRESET), + }; + assert_eq!(hash_count_bytes.len(), 1); let hash_count = hash_count_bytes[0]; diff --git a/ethcore/src/trace/db.rs b/ethcore/src/trace/db.rs index c5b004f79..bc867983d 100644 --- a/ethcore/src/trace/db.rs +++ b/ethcore/src/trace/db.rs @@ -129,7 +129,7 @@ impl TraceDB where T: DatabaseExtras { pub fn new(config: Config, tracesdb: Arc, extras: Arc) -> Self { let mut batch = DBTransaction::new(&tracesdb); batch.put(db::COL_TRACE, b"version", TRACE_DB_VER); - tracesdb.write(batch).unwrap(); + tracesdb.write(batch).expect("failed to update version"); TraceDB { traces: RwLock::new(HashMap::new()), diff --git a/ethcore/src/types/transaction.rs b/ethcore/src/types/transaction.rs index f32a2f4dd..247a1b301 100644 --- a/ethcore/src/types/transaction.rs +++ b/ethcore/src/types/transaction.rs @@ -21,7 +21,7 @@ use std::cell::*; use rlp::*; use util::sha3::Hashable; use util::{H256, Address, U256, Bytes, HeapSizeOf}; -use ethkey::{Signature, sign, Secret, Public, recover, public_to_address, Error as EthkeyError}; +use ethkey::{Signature, Secret, Public, recover, public_to_address, Error as EthkeyError}; use error::*; use evm::Schedule; use header::BlockNumber; @@ -143,7 +143,8 @@ impl Transaction { /// Signs the transaction as coming from `sender`. pub fn sign(self, secret: &Secret) -> SignedTransaction { - let sig = sign(secret, &self.hash()).unwrap(); + let sig = ::ethkey::sign(secret, &self.hash()) + .expect("data is valid and context has signing capabilities; qed"); self.with_signature(sig) } diff --git a/ethcore/src/verification/verification.rs b/ethcore/src/verification/verification.rs index 7269765a0..2d297499d 100644 --- a/ethcore/src/verification/verification.rs +++ b/ethcore/src/verification/verification.rs @@ -108,7 +108,8 @@ pub fn verify_block_family(header: &Header, bytes: &[u8], engine: &Engine, bc: & match bc.block_details(&hash) { Some(details) => { excluded.insert(details.parent.clone()); - let b = bc.block(&hash).unwrap(); + let b = bc.block(&hash) + .expect("parent already known to be stored; qed"); excluded.extend(BlockView::new(&b).uncle_hashes()); hash = details.parent; }