add flag 'no_merkle_proof' to /v1/blocks api to get blocks without checking merkle proof (#2843)

This commit is contained in:
Aleksandrov Vladimir 2019-05-31 09:49:40 +03:00 committed by hashmap
parent e345405201
commit 2cb37913ba
4 changed files with 23 additions and 7 deletions

View file

@ -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())
}
@ -141,6 +144,8 @@ 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,
@ -148,7 +153,7 @@ impl Handler for BlockHandler {
)
}
} else {
result_to_response(self.get_block(&h))
result_to_response(self.get_block(&h, true))
}
}
}

View file

@ -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()))?;

View file

@ -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()))?,
};

View file

@ -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
@ -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();
}
@ -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()
@ -598,6 +600,7 @@ impl BlockPrintable {
chain.clone(),
Some(&block.header),
include_proof,
include_merkle_proof,
)
})
.collect::<Result<Vec<_>, _>>()?;
@ -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()