From ef54e452bfe6d33ee6f65493ef8f6bd6ab458bba Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Fri, 8 Mar 2019 18:37:24 +0200 Subject: [PATCH] Fix more Clippy warnings --- examples/index.rs | 2 +- src/bin/electrs.rs | 2 +- src/daemon.rs | 33 +++++++++++++++++++-------------- src/index.rs | 2 +- src/query.rs | 6 +++--- src/rpc.rs | 4 ++-- src/signal.rs | 2 +- src/util.rs | 6 +++++- 8 files changed, 33 insertions(+), 24 deletions(-) diff --git a/examples/index.rs b/examples/index.rs index 51780f2..79ba852 100644 --- a/examples/index.rs +++ b/examples/index.rs @@ -12,7 +12,7 @@ use electrs::{ use error_chain::ChainedError; fn run() -> Result<()> { - let signal = Waiter::new(); + let signal = Waiter::start(); let config = Config::from_args(); let metrics = Metrics::new(config.monitoring_addr); metrics.start(); diff --git a/src/bin/electrs.rs b/src/bin/electrs.rs index 19640da..31dadc6 100644 --- a/src/bin/electrs.rs +++ b/src/bin/electrs.rs @@ -23,7 +23,7 @@ use electrs::{ }; fn run_server(config: &Config) -> Result<()> { - let signal = Waiter::new(); + let signal = Waiter::start(); let metrics = Metrics::new(config.monitoring_addr); metrics.start(); diff --git a/src/daemon.rs b/src/daemon.rs index e98fb98..35d9541 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -8,7 +8,7 @@ use bitcoin_hashes::hex::{FromHex, ToHex}; use bitcoin_hashes::sha256d::Hash as Sha256dHash; use glob; use hex; -use serde_json::{from_str, from_value, Value}; +use serde_json::{from_str, from_value, Map, Value}; use std::collections::{HashMap, HashSet}; use std::io::{BufRead, BufReader, Lines, Write}; use std::net::{SocketAddr, TcpStream}; @@ -55,23 +55,28 @@ fn tx_from_value(value: Value) -> Result { /// Parse JSONRPC error code, if exists. fn parse_error_code(err: &Value) -> Option { + if err.is_null() { + return None; + } err.as_object()?.get("code")?.as_i64() } +fn check_error_code(reply_obj: &Map, method: &str) -> Result<()> { + if let Some(err) = reply_obj.get("error") { + if let Some(code) = parse_error_code(&err) { + match code { + // RPC_IN_WARMUP -> retry by later reconnection + -28 => bail!(ErrorKind::Connection(err.to_string())), + _ => bail!("{} RPC error: {}", method, err), + } + } + } + Ok(()) +} + fn parse_jsonrpc_reply(mut reply: Value, method: &str, expected_id: u64) -> Result { if let Some(reply_obj) = reply.as_object_mut() { - if let Some(err) = reply_obj.get("error") { - if !err.is_null() { - if let Some(code) = parse_error_code(&err) { - match code { - // RPC_IN_WARMUP -> retry by later reconnection - -28 => bail!(ErrorKind::Connection(err.to_string())), - _ => (), - } - } - bail!("{} RPC error: {}", method, err); - } - } + check_error_code(reply_obj, method)?; let id = reply_obj .get("id") .chain_err(|| format!("no id in reply: {:?}", reply_obj))? @@ -612,7 +617,7 @@ impl Daemon { bestblockhash: &Sha256dHash, ) -> Result> { // Iterate back over headers until known blockash is found: - if indexed_headers.len() == 0 { + if indexed_headers.is_empty() { return self.get_all_headers(bestblockhash); } debug!( diff --git a/src/index.rs b/src/index.rs index 24ee59c..153b869 100644 --- a/src/index.rs +++ b/src/index.rs @@ -133,7 +133,7 @@ impl TxRow { } } - pub fn filter_prefix(txid_prefix: &HashPrefix) -> Bytes { + pub fn filter_prefix(txid_prefix: HashPrefix) -> Bytes { [b"T", &txid_prefix[..]].concat() } diff --git a/src/query.rs b/src/query.rs index c0e2269..f925b91 100644 --- a/src/query.rs +++ b/src/query.rs @@ -149,9 +149,9 @@ fn txrow_by_txid(store: &ReadStore, txid: &Sha256dHash) -> Option { Some(TxRow::from_row(&Row { key, value })) } -fn txrows_by_prefix(store: &ReadStore, txid_prefix: &HashPrefix) -> Vec { +fn txrows_by_prefix(store: &ReadStore, txid_prefix: HashPrefix) -> Vec { store - .scan(&TxRow::filter_prefix(&txid_prefix)) + .scan(&TxRow::filter_prefix(txid_prefix)) .iter() .map(|row| TxRow::from_row(row)) .collect() @@ -230,7 +230,7 @@ impl Query { ) -> Result> { let mut txns = vec![]; for txid_prefix in prefixes { - for tx_row in txrows_by_prefix(store, &txid_prefix) { + for tx_row in txrows_by_prefix(store, txid_prefix) { let txid: Sha256dHash = deserialize(&tx_row.key.txid).unwrap(); let txn = self .tx_cache diff --git a/src/rpc.rs b/src/rpc.rs index 877431e..85f7bdb 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -503,7 +503,7 @@ impl RPC { } fn start_acceptor(addr: SocketAddr) -> Channel> { - let chan = Channel::new(); + let chan = Channel::unbounded(); let acceptor = chan.sender(); spawn_thread("acceptor", move || { let listener = @@ -531,7 +531,7 @@ impl RPC { "# of Electrum subscriptions", )), }); - let notification = Channel::new(); + let notification = Channel::unbounded(); RPC { notification: notification.sender(), server: Some(spawn_thread("rpc", move || { diff --git a/src/signal.rs b/src/signal.rs index 75929ad..5650194 100644 --- a/src/signal.rs +++ b/src/signal.rs @@ -10,7 +10,7 @@ pub struct Waiter { } impl Waiter { - pub fn new() -> Waiter { + pub fn start() -> Waiter { Waiter { signal: chan_signal::notify(&[chan_signal::Signal::INT, chan_signal::Signal::TERM]), } diff --git a/src/util.rs b/src/util.rs index 0f6d906..b9f7e9d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -187,6 +187,10 @@ impl HeaderList { self.headers.len() } + pub fn is_empty(&self) -> bool { + self.headers.is_empty() + } + pub fn iter(&self) -> slice::Iter { self.headers.iter() } @@ -222,7 +226,7 @@ pub struct Channel { } impl Channel { - pub fn new() -> Channel { + pub fn unbounded() -> Self { let (tx, rx) = channel(); Channel { tx, rx } }