Skip to content

Commit

Permalink
Optionally add rangeproofs to block api (#2999)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdm authored and antiochp committed Sep 3, 2019
1 parent 928279a commit 0e62b7e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
43 changes: 29 additions & 14 deletions api/src/handlers/blocks_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,15 @@ pub struct 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 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())
}

Expand Down Expand Up @@ -141,19 +146,29 @@ impl Handler for BlockHandler {
Ok(h) => h,
};

if let Some(param) = req.uri().query() {
if param == "compact" {
result_to_response(self.get_compact_block(&h))
} else if param == "no_merkle_proof" {
result_to_response(self.get_block(&h, false))
} else {
response(
StatusCode::BAD_REQUEST,
format!("unsupported query parameter: {}", param),
)
let mut include_proof = false;
let mut include_merkle_proof = true;
if let Some(params) = req.uri().query() {
let query = url::form_urlencoded::parse(params.as_bytes());
let mut compact = false;
for (param, _) in query {
match param.as_ref() {
"compact" => compact = true,
"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))
}
}
3 changes: 2 additions & 1 deletion doc/api/node_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
### GET Blocks

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**

Expand Down

0 comments on commit 0e62b7e

Please sign in to comment.