From 0e62b7e43e07cb39345f338d716c418627bbb79d Mon Sep 17 00:00:00 2001 From: jaspervdm Date: Tue, 3 Sep 2019 16:52:33 +0200 Subject: [PATCH] Optionally add rangeproofs to block api (#2999) --- api/src/handlers/blocks_api.rs | 43 +++++++++++++++++++++++----------- doc/api/node_api.md | 3 ++- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/api/src/handlers/blocks_api.rs b/api/src/handlers/blocks_api.rs index 9675ef52b..7fb0caa67 100644 --- a/api/src/handlers/blocks_api.rs +++ b/api/src/handlers/blocks_api.rs @@ -87,10 +87,15 @@ pub struct BlockHandler { } impl BlockHandler { - fn get_block(&self, h: &Hash, include_merkle_proof: bool) -> Result { + fn get_block( + &self, + h: &Hash, + include_proof: bool, + include_merkle_proof: bool, + ) -> Result { 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)) } } diff --git a/doc/api/node_api.md b/doc/api/node_api.md index 8529038a0..a0912a074 100644 --- a/doc/api/node_api.md +++ b/doc/api/node_api.md @@ -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**