1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 01:43:29 +01:00

Update stable Rust to 1.28

Fix formatting warnings.
This commit is contained in:
Roman Zeyde 2018-08-04 22:25:25 +03:00
parent e4556e8929
commit 001f7a7ba5
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
8 changed files with 66 additions and 37 deletions

View File

@ -20,4 +20,4 @@ jobs:
before_script: before_script:
- rustup component add rustfmt-preview - rustup component add rustfmt-preview
script: script:
- cargo fmt --all -- --write-mode=check - cargo fmt --all -- --check

View File

@ -9,7 +9,8 @@ use std::fs;
use std::io::{Cursor, Seek, SeekFrom}; use std::io::{Cursor, Seek, SeekFrom};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::{ use std::sync::{
mpsc::{Receiver, SyncSender}, Arc, Mutex, mpsc::{Receiver, SyncSender},
Arc, Mutex,
}; };
use std::thread; use std::thread;
@ -61,7 +62,8 @@ impl Parser {
fn last_indexed_row(&self) -> Row { fn last_indexed_row(&self) -> Row {
let indexed_blockhashes = self.indexed_blockhashes.lock().unwrap(); let indexed_blockhashes = self.indexed_blockhashes.lock().unwrap();
let last_header = self.current_headers let last_header = self
.current_headers
.iter() .iter()
.take_while(|h| indexed_blockhashes.contains(h.hash())) .take_while(|h| indexed_blockhashes.contains(h.hash()))
.last() .last()
@ -88,7 +90,8 @@ impl Parser {
for block in blocks { for block in blocks {
let blockhash = block.bitcoin_hash(); let blockhash = block.bitcoin_hash();
if let Some(header) = self.current_headers.header_by_blockhash(&blockhash) { if let Some(header) = self.current_headers.header_by_blockhash(&blockhash) {
if self.indexed_blockhashes if self
.indexed_blockhashes
.lock() .lock()
.expect("indexed_blockhashes") .expect("indexed_blockhashes")
.insert(blockhash.clone()) .insert(blockhash.clone())

View File

@ -108,20 +108,24 @@ impl Config {
Network::Regtest => 60401, Network::Regtest => 60401,
}; };
let daemon_rpc_addr: SocketAddr = m.value_of("daemon_rpc_addr") let daemon_rpc_addr: SocketAddr = m
.value_of("daemon_rpc_addr")
.unwrap_or(&format!("127.0.0.1:{}", default_daemon_port)) .unwrap_or(&format!("127.0.0.1:{}", default_daemon_port))
.parse() .parse()
.expect("invalid Bitcoind RPC address"); .expect("invalid Bitcoind RPC address");
let electrum_rpc_addr: SocketAddr = m.value_of("electrum_rpc_addr") let electrum_rpc_addr: SocketAddr = m
.value_of("electrum_rpc_addr")
.unwrap_or(&format!("127.0.0.1:{}", default_electrum_port)) .unwrap_or(&format!("127.0.0.1:{}", default_electrum_port))
.parse() .parse()
.expect("invalid Electrum RPC address"); .expect("invalid Electrum RPC address");
let monitoring_addr: SocketAddr = m.value_of("monitoring_addr") let monitoring_addr: SocketAddr = m
.value_of("monitoring_addr")
.unwrap_or("127.0.0.1:42024") .unwrap_or("127.0.0.1:42024")
.parse() .parse()
.expect("invalid Prometheus monitoring address"); .expect("invalid Prometheus monitoring address");
let mut daemon_dir = m.value_of("daemon_dir") let mut daemon_dir = m
.value_of("daemon_dir")
.map(|p| PathBuf::from(p)) .map(|p| PathBuf::from(p))
.unwrap_or_else(|| { .unwrap_or_else(|| {
let mut default_dir = home_dir().expect("no homedir"); let mut default_dir = home_dir().expect("no homedir");

View File

@ -28,12 +28,11 @@ pub enum Network {
} }
fn parse_hash(value: &Value) -> Result<Sha256dHash> { fn parse_hash(value: &Value) -> Result<Sha256dHash> {
Ok(Sha256dHash::from_hex(value Ok(Sha256dHash::from_hex(
.as_str() value
.chain_err(|| format!("non-string value: {}", value))?) .as_str()
.chain_err(|| { .chain_err(|| format!("non-string value: {}", value))?,
format!("non-hex value: {}", value) ).chain_err(|| format!("non-hex value: {}", value))?)
})?)
} }
fn header_from_value(value: Value) -> Result<BlockHeader> { fn header_from_value(value: Value) -> Result<BlockHeader> {
@ -160,8 +159,10 @@ impl Connection {
signal: Waiter, signal: Waiter,
) -> Result<Connection> { ) -> Result<Connection> {
let conn = tcp_connect(addr, &signal)?; let conn = tcp_connect(addr, &signal)?;
let reader = BufReader::new(conn.try_clone() let reader = BufReader::new(
.chain_err(|| format!("failed to clone {:?}", conn))?); conn.try_clone()
.chain_err(|| format!("failed to clone {:?}", conn))?,
);
Ok(Connection { Ok(Connection {
tx: conn, tx: conn,
rx: reader.lines(), rx: reader.lines(),
@ -193,7 +194,8 @@ impl Connection {
let mut in_header = true; let mut in_header = true;
let mut contents: Option<String> = None; let mut contents: Option<String> = None;
let iter = self.rx.by_ref(); let iter = self.rx.by_ref();
let status = iter.next() let status = iter
.next()
.chain_err(|| { .chain_err(|| {
ErrorKind::Connection("disconnected from daemon while receiving".to_owned()) ErrorKind::Connection("disconnected from daemon while receiving".to_owned())
})? })?
@ -362,7 +364,8 @@ impl Daemon {
fn request(&self, method: &str, params: Value) -> Result<Value> { fn request(&self, method: &str, params: Value) -> Result<Value> {
let id = self.message_id.next(); let id = self.message_id.next();
let req = json!({"method": method, "params": params, "id": id}); let req = json!({"method": method, "params": params, "id": id});
let reply = self.retry_call_jsonrpc(method, &req) let reply = self
.retry_call_jsonrpc(method, &req)
.chain_err(|| format!("RPC failed: {}", req))?; .chain_err(|| format!("RPC failed: {}", req))?;
parse_jsonrpc_reply(reply, method, id) parse_jsonrpc_reply(reply, method, id)
} }
@ -374,7 +377,8 @@ impl Daemon {
.map(|params| json!({"method": method, "params": params, "id": id})) .map(|params| json!({"method": method, "params": params, "id": id}))
.collect(); .collect();
let mut results = vec![]; let mut results = vec![];
let mut replies = self.retry_call_jsonrpc(method, &reqs) let mut replies = self
.retry_call_jsonrpc(method, &reqs)
.chain_err(|| format!("RPC failed: {}", reqs))?; .chain_err(|| format!("RPC failed: {}", reqs))?;
if let Some(replies_vec) = replies.as_array_mut() { if let Some(replies_vec) = replies.as_array_mut() {
for reply in replies_vec { for reply in replies_vec {
@ -410,7 +414,8 @@ impl Daemon {
pub fn getblockheaders(&self, heights: &[usize]) -> Result<Vec<BlockHeader>> { pub fn getblockheaders(&self, heights: &[usize]) -> Result<Vec<BlockHeader>> {
let heights: Vec<Value> = heights.iter().map(|height| json!([height])).collect(); let heights: Vec<Value> = heights.iter().map(|height| json!([height])).collect();
let params_list: Vec<Value> = self.requests("getblockhash", &heights)? let params_list: Vec<Value> = self
.requests("getblockhash", &heights)?
.into_iter() .into_iter()
.map(|hash| json!([hash, /*verbose=*/ false])) .map(|hash| json!([hash, /*verbose=*/ false]))
.collect(); .collect();
@ -507,7 +512,8 @@ impl Daemon {
fn get_all_headers(&self, tip: &Sha256dHash) -> Result<Vec<BlockHeader>> { fn get_all_headers(&self, tip: &Sha256dHash) -> Result<Vec<BlockHeader>> {
let info: Value = self.request("getblockheader", json!([tip.be_hex_string()]))?; let info: Value = self.request("getblockheader", json!([tip.be_hex_string()]))?;
let tip_height = info.get("height") let tip_height = info
.get("height")
.expect("missing height") .expect("missing height")
.as_u64() .as_u64()
.expect("non-numeric height") as usize; .expect("non-numeric height") as usize;
@ -553,7 +559,8 @@ impl Daemon {
if indexed_headers.header_by_blockhash(&blockhash).is_some() { if indexed_headers.header_by_blockhash(&blockhash).is_some() {
break; break;
} }
let header = self.getblockheader(&blockhash) let header = self
.getblockheader(&blockhash)
.chain_err(|| format!("failed to get {} header", blockhash))?; .chain_err(|| format!("failed to get {} header", blockhash))?;
new_headers.push(header); new_headers.push(header);
blockhash = header.prev_blockhash; blockhash = header.prev_blockhash;

View File

@ -233,7 +233,8 @@ fn read_indexed_headers(store: &ReadStore) -> HeaderList {
let null_hash = Sha256dHash::default(); let null_hash = Sha256dHash::default();
let mut blockhash = latest_blockhash; let mut blockhash = latest_blockhash;
while blockhash != null_hash { while blockhash != null_hash {
let header = map.remove(&blockhash) let header = map
.remove(&blockhash)
.expect(&format!("missing {} header in DB", blockhash)); .expect(&format!("missing {} header in DB", blockhash));
blockhash = header.prev_blockhash; blockhash = header.prev_blockhash;
headers.push(header); headers.push(header);
@ -360,7 +361,8 @@ impl Index {
loop { loop {
waiter.poll()?; waiter.poll()?;
let timer = self.stats.start_timer("fetch"); let timer = self.stats.start_timer("fetch");
let batch = chan.receiver() let batch = chan
.receiver()
.recv() .recv()
.expect("block fetch exited prematurely")?; .expect("block fetch exited prematurely")?;
timer.observe_duration(); timer.observe_duration();

View File

@ -45,7 +45,8 @@ impl MempoolStore {
for row in rows { for row in rows {
let (key, value) = row.into_pair(); let (key, value) = row.into_pair();
let no_values_left = { let no_values_left = {
let values = map.get_mut(&key) let values = map
.get_mut(&key)
.expect(&format!("missing key {} in mempool", hex::encode(&key))); .expect(&format!("missing key {} in mempool", hex::encode(&key)));
let last_value = values let last_value = values
.pop() .pop()
@ -250,7 +251,8 @@ impl Tracker {
} }
fn remove(&mut self, txid: &Sha256dHash) { fn remove(&mut self, txid: &Sha256dHash) {
let stats = self.items let stats = self
.items
.remove(txid) .remove(txid)
.expect(&format!("missing mempool tx {}", txid)); .expect(&format!("missing mempool tx {}", txid));
self.index.remove(&stats.tx); self.index.remove(&stats.tx);

View File

@ -202,7 +202,8 @@ impl Query {
for txid_prefix in prefixes { 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 txid: Sha256dHash = deserialize(&tx_row.key.txid).unwrap();
let txn = self.tx_cache let txn = self
.tx_cache
.get_or_else(&txid, || self.load_txn(&txid, Some(tx_row.height)))?; .get_or_else(&txid, || self.load_txn(&txid, Some(tx_row.height)))?;
txns.push(TxnHeight { txns.push(TxnHeight {
txn, txn,
@ -305,9 +306,11 @@ impl Query {
} }
pub fn status(&self, script_hash: &[u8]) -> Result<Status> { pub fn status(&self, script_hash: &[u8]) -> Result<Status> {
let confirmed = self.confirmed_status(script_hash) let confirmed = self
.confirmed_status(script_hash)
.chain_err(|| "failed to get confirmed status")?; .chain_err(|| "failed to get confirmed status")?;
let mempool = self.mempool_status(script_hash, &confirmed.0) let mempool = self
.mempool_status(script_hash, &confirmed.0)
.chain_err(|| "failed to get mempool status")?; .chain_err(|| "failed to get mempool status")?;
Ok(Status { confirmed, mempool }) Ok(Status { confirmed, mempool })
} }
@ -329,7 +332,8 @@ impl Query {
.height .height
} }
}; };
let blockhash = *self.app let blockhash = *self
.app
.index() .index()
.get_header(height as usize) .get_header(height as usize)
.chain_err(|| format!("missing header at height {}", height))? .chain_err(|| format!("missing header at height {}", height))?
@ -355,7 +359,8 @@ impl Query {
tx_hash: &Sha256dHash, tx_hash: &Sha256dHash,
height: usize, height: usize,
) -> Result<(Vec<Sha256dHash>, usize)> { ) -> Result<(Vec<Sha256dHash>, usize)> {
let header_entry = self.app let header_entry = self
.app
.index() .index()
.get_header(height) .get_header(height)
.chain_err(|| format!("missing block #{}", height))?; .chain_err(|| format!("missing block #{}", height))?;

View File

@ -124,7 +124,8 @@ impl Connection {
let start_height = usize_from_value(params.get(0), "start_height")?; let start_height = usize_from_value(params.get(0), "start_height")?;
let count = usize_from_value(params.get(1), "count")?; let count = usize_from_value(params.get(1), "count")?;
let heights: Vec<usize> = (start_height..(start_height + count)).collect(); let heights: Vec<usize> = (start_height..(start_height + count)).collect();
let headers: Vec<String> = self.query let headers: Vec<String> = self
.query
.get_headers(&heights) .get_headers(&heights)
.into_iter() .into_iter()
.map(|entry| hex::encode(&serialize(entry.header()).unwrap())) .map(|entry| hex::encode(&serialize(entry.header()).unwrap()))
@ -219,7 +220,8 @@ impl Connection {
fn blockchain_transaction_get_merkle(&self, params: &[Value]) -> Result<Value> { fn blockchain_transaction_get_merkle(&self, params: &[Value]) -> Result<Value> {
let tx_hash = hash_from_value(params.get(0)).chain_err(|| "bad tx_hash")?; let tx_hash = hash_from_value(params.get(0)).chain_err(|| "bad tx_hash")?;
let height = usize_from_value(params.get(1), "height")?; let height = usize_from_value(params.get(1), "height")?;
let (merkle, pos) = self.query let (merkle, pos) = self
.query
.get_merkle_proof(&tx_hash, height) .get_merkle_proof(&tx_hash, height)
.chain_err(|| "cannot create merkle proof")?; .chain_err(|| "cannot create merkle proof")?;
let merkle: Vec<String> = merkle let merkle: Vec<String> = merkle
@ -233,7 +235,8 @@ impl Connection {
} }
fn handle_command(&mut self, method: &str, params: &[Value], id: &Number) -> Result<Value> { fn handle_command(&mut self, method: &str, params: &[Value], id: &Number) -> Result<Value> {
let timer = self.stats let timer = self
.stats
.latency .latency
.with_label_values(&[method]) .with_label_values(&[method])
.start_timer(); .start_timer();
@ -276,7 +279,8 @@ impl Connection {
} }
fn update_subscriptions(&mut self) -> Result<Vec<Value>> { fn update_subscriptions(&mut self) -> Result<Vec<Value>> {
let timer = self.stats let timer = self
.stats
.latency .latency
.with_label_values(&["periodic_update"]) .with_label_values(&["periodic_update"])
.start_timer(); .start_timer();
@ -340,7 +344,8 @@ impl Connection {
self.send_values(&[reply])? self.send_values(&[reply])?
} }
Message::PeriodicUpdate => { Message::PeriodicUpdate => {
let values = self.update_subscriptions() let values = self
.update_subscriptions()
.chain_err(|| "failed to update subscriptions")?; .chain_err(|| "failed to update subscriptions")?;
self.send_values(&values)? self.send_values(&values)?
} }
@ -360,7 +365,8 @@ impl Connection {
return Ok(()); return Ok(());
} else { } else {
match String::from_utf8(line) { match String::from_utf8(line) {
Ok(req) => tx.send(Message::Request(req)) Ok(req) => tx
.send(Message::Request(req))
.chain_err(|| "channel closed")?, .chain_err(|| "channel closed")?,
Err(err) => { Err(err) => {
let _ = tx.send(Message::Done); let _ = tx.send(Message::Done);