Optionally add rangeproofs to block api (#2999)

This commit is contained in:
jaspervdm 2019-09-03 16:52:33 +02:00 committed by Antioch Peverell
parent 928279a676
commit 0e62b7e43e
2 changed files with 31 additions and 15 deletions

View file

@ -87,10 +87,15 @@ pub struct BlockHandler {
} }
impl BlockHandler { impl BlockHandler {
fn get_block(&self, h: &Hash, include_merkle_proof: bool) -> Result<BlockPrintable, Error> { fn get_block(
&self,
h: &Hash,
include_proof: bool,
include_merkle_proof: bool,
) -> Result<BlockPrintable, Error> {
let chain = w(&self.chain)?; let chain = w(&self.chain)?;
let block = chain.get_block(h).context(ErrorKind::NotFound)?; let block = chain.get_block(h).context(ErrorKind::NotFound)?;
BlockPrintable::from_block(&block, chain, false, include_merkle_proof) BlockPrintable::from_block(&block, chain, include_proof, include_merkle_proof)
.map_err(|_| ErrorKind::Internal("chain error".to_owned()).into()) .map_err(|_| ErrorKind::Internal("chain error".to_owned()).into())
} }
@ -141,19 +146,29 @@ impl Handler for BlockHandler {
Ok(h) => h, Ok(h) => h,
}; };
if let Some(param) = req.uri().query() { let mut include_proof = false;
if param == "compact" { let mut include_merkle_proof = true;
result_to_response(self.get_compact_block(&h)) if let Some(params) = req.uri().query() {
} else if param == "no_merkle_proof" { let query = url::form_urlencoded::parse(params.as_bytes());
result_to_response(self.get_block(&h, false)) let mut compact = false;
} else { for (param, _) in query {
response( match param.as_ref() {
StatusCode::BAD_REQUEST, "compact" => compact = true,
format!("unsupported query parameter: {}", param), "no_merkle_proof" => include_merkle_proof = false,
) "include_proof" => include_proof = true,
_ => {
return response(
StatusCode::BAD_REQUEST,
format!("unsupported query parameter: {}", param),
)
}
}
}
if compact {
return result_to_response(self.get_compact_block(&h));
} }
} else {
result_to_response(self.get_block(&h, true))
} }
result_to_response(self.get_block(&h, include_proof, include_merkle_proof))
} }
} }

View file

@ -36,7 +36,8 @@
### GET Blocks ### GET Blocks
Returns data about a specific block given a hash, a height or an unspent commit. Returns data about a specific block given a hash, a height or an unspent commit.
Optionally return results as "compact blocks" by passing `?compact` query.
Optionally, Merkle proofs can be excluded from the results by adding `?no_merkle_proof`, rangeproofs can be included by adding `?include_proof` or results can be returned as "compact blocks" by adding `?compact`.
* **URL** * **URL**