1
0
Fork 0
mirror of https://github.com/romanz/electrs.git synced 2025-02-24 06:57:53 +01:00

Minor fixes before merging to master

This commit is contained in:
Roman Zeyde 2018-12-18 22:34:51 +02:00
parent 8ad6d29caa
commit afa82a0a49
No known key found for this signature in database
GPG key ID: 87CAE5FA46917CBB
2 changed files with 36 additions and 29 deletions

View file

@ -412,25 +412,26 @@ impl Query {
cp_height: usize,
) -> Result<(Vec<Sha256dHash>, 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<Sha256dHash> = (0..cp_height + 1)
let heights: Vec<usize> = (0..cp_height + 1).collect();
let header_hashes: Vec<Sha256dHash> = 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<Sha256dHash> {

View file

@ -30,10 +30,24 @@ fn usize_from_value(val: Option<&Value>, name: &str) -> Result<usize> {
Ok(val as usize)
}
fn usize_from_value_or(val: Option<&Value>, name: &str, default: usize) -> Result<usize> {
if val.is_none() {
return Ok(default);
}
usize_from_value(val, name)
}
fn bool_from_value(val: Option<&Value>, name: &str) -> Result<bool> {
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<bool> {
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<Value> {
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<Value> {
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<usize> = (start_height..(start_height + count)).collect();
let headers: Vec<String> = self
.query
@ -258,10 +266,7 @@ impl Connection {
fn blockchain_transaction_id_from_pos(&self, params: &[Value]) -> Result<Value> {
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)?;