Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally add rangeproofs to block api #2999

Merged
merged 1 commit into from
Sep 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Optionally add rangeproofs to block api
jaspervdm committed Aug 25, 2019
commit 6c7d0b125a2ac159a652d18d9fbe9d7ea2ade7a6
43 changes: 29 additions & 14 deletions api/src/handlers/blocks_api.rs
Original file line number Diff line number Diff line change
@@ -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())
}

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