Kernel lookup api method (#3000)

* Kernel lookup api

* Min and max height parameters, scan backwards

* Return null instead of 404 if kernel not found

* Return TxKernel instead of TxKernelPrintable

* Update description of KernelHandler
This commit is contained in:
jaspervdm
2019-08-30 11:03:12 +02:00
committed by Antioch Peverell
parent d36a0b29ef
commit 30156cdcf9
5 changed files with 173 additions and 3 deletions
+5 -1
View File
@@ -26,6 +26,7 @@ use self::blocks_api::HeaderHandler;
use self::chain_api::ChainCompactHandler;
use self::chain_api::ChainHandler;
use self::chain_api::ChainValidationHandler;
use self::chain_api::KernelHandler;
use self::chain_api::OutputHandler;
use self::peers_api::PeerHandler;
use self::peers_api::PeersAllHandler;
@@ -119,7 +120,9 @@ pub fn build_router(
let output_handler = OutputHandler {
chain: Arc::downgrade(&chain),
};
let kernel_handler = KernelHandler {
chain: Arc::downgrade(&chain),
};
let block_handler = BlockHandler {
chain: Arc::downgrade(&chain),
};
@@ -171,6 +174,7 @@ pub fn build_router(
router.add_route("/v1/headers/*", Arc::new(header_handler))?;
router.add_route("/v1/chain", Arc::new(chain_tip_handler))?;
router.add_route("/v1/chain/outputs/*", Arc::new(output_handler))?;
router.add_route("/v1/chain/kernels/*", Arc::new(kernel_handler))?;
router.add_route("/v1/chain/compact", Arc::new(chain_compact_handler))?;
router.add_route("/v1/chain/validate", Arc::new(chain_validation_handler))?;
router.add_route("/v1/txhashset/*", Arc::new(txhashset_handler))?;
+69
View File
@@ -197,3 +197,72 @@ impl Handler for OutputHandler {
}
}
}
/// Kernel handler, search for a kernel by excess commitment
/// GET /v1/chain/kernels/XXX?min_height=YYY&max_height=ZZZ
/// The `min_height` and `max_height` parameters are optional
pub struct KernelHandler {
pub chain: Weak<chain::Chain>,
}
impl KernelHandler {
fn get_kernel(&self, req: Request<Body>) -> Result<Option<LocatedTxKernel>, Error> {
let excess = req
.uri()
.path()
.trim_end_matches('/')
.rsplit('/')
.next()
.ok_or(ErrorKind::RequestError("missing excess".into()))?;
let excess = util::from_hex(excess.to_owned())
.map_err(|_| ErrorKind::RequestError("invalid excess hex".into()))?;
if excess.len() != 33 {
return Err(ErrorKind::RequestError("invalid excess length".into()).into());
}
let excess = Commitment::from_vec(excess);
let chain = w(&self.chain)?;
let mut min_height: Option<u64> = None;
let mut max_height: Option<u64> = None;
// Check query parameters for minimum and maximum search height
if let Some(q) = req.uri().query() {
let params = QueryParams::from(q);
if let Some(h) = params.get("min_height") {
let h = h
.parse()
.map_err(|_| ErrorKind::RequestError("invalid minimum height".into()))?;
// Default is genesis
min_height = if h == 0 { None } else { Some(h) };
}
if let Some(h) = params.get("max_height") {
let h = h
.parse()
.map_err(|_| ErrorKind::RequestError("invalid maximum height".into()))?;
// Default is current head
let head_height = chain
.head()
.map_err(|e| ErrorKind::Internal(format!("{}", e)))?
.height;
max_height = if h >= head_height { None } else { Some(h) };
}
}
let kernel = chain
.get_kernel_height(&excess, min_height, max_height)
.map_err(|e| ErrorKind::Internal(format!("{}", e)))?
.map(|(tx_kernel, height, mmr_index)| LocatedTxKernel {
tx_kernel,
height,
mmr_index,
});
Ok(kernel)
}
}
impl Handler for KernelHandler {
fn get(&self, req: Request<Body>) -> ResponseFuture {
result_to_response(self.get_kernel(req))
}
}
+8 -1
View File
@@ -17,7 +17,7 @@ use std::sync::Arc;
use crate::chain;
use crate::core::core::hash::Hashed;
use crate::core::core::merkle_proof::MerkleProof;
use crate::core::core::KernelFeatures;
use crate::core::core::{KernelFeatures, TxKernel};
use crate::core::{core, ser};
use crate::p2p;
use crate::util;
@@ -699,6 +699,13 @@ pub struct OutputListing {
pub outputs: Vec<OutputPrintable>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LocatedTxKernel {
pub tx_kernel: TxKernel,
pub height: u64,
pub mmr_index: u64,
}
#[derive(Serialize, Deserialize)]
pub struct PoolInfo {
/// Size of the pool