From afa82a0a49817bbaac8ea5301191e8115d339a80 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Tue, 18 Dec 2018 22:34:51 +0200 Subject: [PATCH] Minor fixes before merging to master --- src/query.rs | 34 ++++++++++++++++++---------------- src/rpc.rs | 31 ++++++++++++++++++------------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/query.rs b/src/query.rs index 2c5330a..45d90b5 100644 --- a/src/query.rs +++ b/src/query.rs @@ -412,25 +412,26 @@ impl Query { cp_height: usize, ) -> Result<(Vec, Sha256dHash)> { if cp_height < height { - return Err(format!("cp_height #{} < height #{}", cp_height, height))?; + bail!("cp_height #{} < height #{}", cp_height, height); } let best_height = self.get_best_header()?.height(); if best_height < cp_height { - return Err(format!( + bail!( "cp_height #{} above best block height #{}", - cp_height, best_height - ))?; + cp_height, + best_height + ); } - let index = self.app.index(); - let header_hashes: Vec = (0..cp_height + 1) + let heights: Vec = (0..cp_height + 1).collect(); + let header_hashes: Vec = self + .get_headers(&heights) .into_iter() - .map(|height| index.get_header(height).unwrap().hash().clone()) + .map(|h| *h.hash()) .collect(); - - let (branch, root) = create_merkle_branch_and_root(header_hashes, height); - Ok((branch, root)) + assert_eq!(header_hashes.len(), heights.len()); + Ok(create_merkle_branch_and_root(header_hashes, height)) } pub fn get_id_from_pos( @@ -445,16 +446,17 @@ impl Query { .get_header(height) .chain_err(|| format!("missing block #{}", height))?; - let txids = self.app.daemon().getblocktxids(&header_entry.hash())?; + let txids = self.app.daemon().getblocktxids(header_entry.hash())?; let txid = *txids .get(tx_pos) .chain_err(|| format!("No tx in position #{} in block #{}", tx_pos, height))?; - if want_merkle { - let (branches, _root) = create_merkle_branch_and_root(txids, tx_pos); - return Ok((txid, branches)); - } - return Ok((txid, [].to_vec())); + let branch = if want_merkle { + create_merkle_branch_and_root(txids, tx_pos).0 + } else { + vec![] + }; + Ok((txid, branch)) } pub fn broadcast(&self, txn: &Transaction) -> Result { diff --git a/src/rpc.rs b/src/rpc.rs index ddf029f..0152836 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -30,10 +30,24 @@ fn usize_from_value(val: Option<&Value>, name: &str) -> Result { Ok(val as usize) } +fn usize_from_value_or(val: Option<&Value>, name: &str, default: usize) -> Result { + if val.is_none() { + return Ok(default); + } + usize_from_value(val, name) +} + fn bool_from_value(val: Option<&Value>, name: &str) -> Result { let val = val.chain_err(|| format!("missing {}", name))?; let val = val.as_bool().chain_err(|| format!("not a bool {}", name))?; - Ok(val as bool) + Ok(val) +} + +fn bool_from_value_or(val: Option<&Value>, name: &str, default: bool) -> Result { + if val.is_none() { + return Ok(default); + } + bool_from_value(val, name) } fn unspent_from_status(status: &Status) -> Value { @@ -109,10 +123,7 @@ impl Connection { fn blockchain_block_header(&self, params: &[Value]) -> Result { let height = usize_from_value(params.get(0), "height")?; - let mut cp_height = 0; - if params.len() > 1 { - cp_height = usize_from_value(params.get(1), "cp_height")?; - } + let cp_height = usize_from_value_or(params.get(1), "cp_height", 0)?; let raw_header_hex: String = self .query @@ -138,10 +149,7 @@ impl Connection { fn blockchain_block_headers(&self, params: &[Value]) -> Result { let start_height = usize_from_value(params.get(0), "start_height")?; let count = usize_from_value(params.get(1), "count")?; - let mut cp_height = 0; - if params.len() > 2 { - cp_height = usize_from_value(params.get(2), "cp_height")?; - } + let cp_height = usize_from_value_or(params.get(2), "cp_height", 0)?; let heights: Vec = (start_height..(start_height + count)).collect(); let headers: Vec = self .query @@ -258,10 +266,7 @@ impl Connection { fn blockchain_transaction_id_from_pos(&self, params: &[Value]) -> Result { let height = usize_from_value(params.get(0), "height")?; let tx_pos = usize_from_value(params.get(1), "tx_pos")?; - let mut want_merkle = false; - if params.len() > 2 { - want_merkle = bool_from_value(params.get(2), "merkle")?; - } + let want_merkle = bool_from_value_or(params.get(2), "merkle", false)?; let (txid, merkle) = self.query.get_id_from_pos(height, tx_pos, want_merkle)?;