Skip to content

Commit

Permalink
add flag 'no_merkle_proof' to /v1/blocks api to get blocks without …
Browse files Browse the repository at this point in the history
…checking merkle proof (#2843)
  • Loading branch information
pronvis authored and hashmap committed May 31, 2019
1 parent e345405 commit 2cb3791
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
11 changes: 8 additions & 3 deletions api/src/handlers/blocks_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,18 @@ impl Handler for HeaderHandler {
///
/// Optionally return results as "compact blocks" by passing "?compact" query
/// param GET /v1/blocks/<hash>?compact
///
/// Optionally turn off the Merkle proof extraction by passing "?no_merkle_proof" query
/// param GET /v1/blocks/<hash>?no_merkle_proof
pub struct BlockHandler {
pub chain: Weak<chain::Chain>,
}

impl BlockHandler {
fn get_block(&self, h: &Hash) -> Result<BlockPrintable, Error> {
fn get_block(&self, h: &Hash, 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)
BlockPrintable::from_block(&block, chain, false, include_merkle_proof)
.map_err(|_| ErrorKind::Internal("chain error".to_owned()).into())
}

Expand Down Expand Up @@ -141,14 +144,16 @@ impl Handler for BlockHandler {
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),
)
}
} else {
result_to_response(self.get_block(&h))
result_to_response(self.get_block(&h, true))
}
}
}
8 changes: 7 additions & 1 deletion api/src/handlers/chain_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ impl OutputHandler {
.iter()
.filter(|output| commitments.is_empty() || commitments.contains(&output.commit))
.map(|output| {
OutputPrintable::from_output(output, chain.clone(), Some(&header), include_proof)
OutputPrintable::from_output(
output,
chain.clone(),
Some(&header),
include_proof,
true,
)
})
.collect::<Result<Vec<_>, _>>()
.context(ErrorKind::Internal("cain error".to_owned()))?;
Expand Down
2 changes: 1 addition & 1 deletion api/src/handlers/transactions_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl TxHashSetHandler {
outputs: outputs
.2
.iter()
.map(|x| OutputPrintable::from_output(x, chain.clone(), None, true))
.map(|x| OutputPrintable::from_output(x, chain.clone(), None, true, true))
.collect::<Result<Vec<_>, _>>()
.context(ErrorKind::Internal("cain error".to_owned()))?,
};
Expand Down
9 changes: 7 additions & 2 deletions api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl OutputPrintable {
chain: Arc<chain::Chain>,
block_header: Option<&core::BlockHeader>,
include_proof: bool,
include_merkle_proof: bool,
) -> Result<OutputPrintable, chain::Error> {
let output_type = if output.is_coinbase() {
OutputType::Coinbase
Expand All @@ -282,7 +283,7 @@ impl OutputPrintable {
// We require the rewind() to be stable even after the PMMR is pruned and
// compacted so we can still recreate the necessary proof.
let mut merkle_proof = None;
if output.is_coinbase() && !spent {
if include_merkle_proof && output.is_coinbase() && !spent {
if let Some(block_header) = block_header {
merkle_proof = chain.get_merkle_proof(&out_id, &block_header).ok();
}
Expand Down Expand Up @@ -583,6 +584,7 @@ impl BlockPrintable {
block: &core::Block,
chain: Arc<chain::Chain>,
include_proof: bool,
include_merkle_proof: bool,
) -> Result<BlockPrintable, chain::Error> {
let inputs = block
.inputs()
Expand All @@ -598,6 +600,7 @@ impl BlockPrintable {
chain.clone(),
Some(&block.header),
include_proof,
include_merkle_proof,
)
})
.collect::<Result<Vec<_>, _>>()?;
Expand Down Expand Up @@ -639,7 +642,9 @@ impl CompactBlockPrintable {
let out_full = cb
.out_full()
.iter()
.map(|x| OutputPrintable::from_output(x, chain.clone(), Some(&block.header), false))
.map(|x| {
OutputPrintable::from_output(x, chain.clone(), Some(&block.header), false, true)
})
.collect::<Result<Vec<_>, _>>()?;
let kern_full = cb
.kern_full()
Expand Down

0 comments on commit 2cb3791

Please sign in to comment.