Commit d284ba77 authored by Dagur Valberg Johannsson's avatar Dagur Valberg Johannsson
Browse files

Add cp_height support to blockchain.block.header/s

parent 537e85f2
......@@ -406,6 +406,33 @@ impl Query {
Ok((branch, pos))
}
pub fn get_header_merkle_proof(
&self,
height: usize,
cp_height: usize,
) -> Result<(Vec<Sha256dHash>, Sha256dHash)> {
if cp_height < height {
return Err(format!("cp_height #{} < height #{}", cp_height, height))?;
}
let best_height = self.get_best_header()?.height();
if best_height < cp_height {
return Err(format!(
"cp_height #{} above best block height #{}",
cp_height, best_height
))?;
}
let index = self.app.index();
let header_hashes: Vec<Sha256dHash> = (0..cp_height + 1)
.into_iter()
.map(|height| index.get_header(height).unwrap().hash().clone())
.collect();
let (branch, root) = create_merkle_branch_and_root(header_hashes, height);
Ok((branch, root))
}
pub fn get_id_from_pos(
&self,
height: usize,
......
......@@ -133,18 +133,39 @@ impl Connection {
fn blockchain_block_header(&self, params: &[Value]) -> Result<Value> {
let height = usize_from_value(params.get(0), "height")?;
let header: String = self
let mut cp_height = 0;
if params.len() > 1 {
cp_height = usize_from_value(params.get(1), "cp_height")?;
}
let raw_header_hex: String = self
.query
.get_headers(&[height])
.into_iter()
.map(|entry| hex::encode(&serialize(entry.header())))
.collect();
Ok(json!(header))
if cp_height == 0 {
return Ok(json!(raw_header_hex));
}
let (branch, root) = self.query.get_header_merkle_proof(height, cp_height)?;
let branch_vec: Vec<String> = branch.into_iter().map(|b| b.be_hex_string()).collect();
return Ok(json!({
"header": raw_header_hex,
"root": root.be_hex_string(),
"branch": branch_vec
}));
}
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 heights: Vec<usize> = (start_height..(start_height + count)).collect();
let headers: Vec<String> = self
.query
......@@ -152,10 +173,27 @@ impl Connection {
.into_iter()
.map(|entry| hex::encode(&serialize(entry.header())))
.collect();
if count == 0 || cp_height == 0 {
return Ok(json!({
"count": headers.len(),
"hex": headers.join(""),
"max": 2016,
}));
}
let (branch, root) = self
.query
.get_header_merkle_proof(start_height + (count - 1), cp_height)?;
let branch_vec: Vec<String> = branch.into_iter().map(|b| b.be_hex_string()).collect();
Ok(json!({
"count": headers.len(),
"hex": headers.join(""),
"max": 2016,
"root": root.be_hex_string(),
"branch" : branch_vec
}))
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment