Browse Source

Replace /blocks/tip with /tip/height and /tip/hash

Instead of redirecting, we now return either the height or the hash directly.

(cherry picked from commit 14f885ef5ac25dd8b9ccf40325dc890b9f94e652)
liquid_e
Nadav Ivgi 6 years ago
committed by Lawrence Nahum
parent
commit
b15f0a38d6
  1. 4
      src/index.rs
  2. 4
      src/query.rs
  3. 10
      src/rest.rs

4
src/index.rs

@ -391,6 +391,10 @@ impl Index {
*headers = read_indexed_headers(store); *headers = read_indexed_headers(store);
} }
pub fn best_height(&self) -> usize {
self.headers.read().unwrap().len() - 1
}
pub fn best_header(&self) -> Option<HeaderEntry> { pub fn best_header(&self) -> Option<HeaderEntry> {
let headers = self.headers.read().unwrap(); let headers = self.headers.read().unwrap();
headers.header_by_blockhash(headers.tip()).cloned() headers.header_by_blockhash(headers.tip()).cloned()

4
src/query.rs

@ -391,6 +391,10 @@ impl Query {
self.app.index().best_header_hash() self.app.index().best_header_hash()
} }
pub fn get_best_height(&self) -> usize {
self.app.index().best_height()
}
pub fn get_block_status(&self, hash: &Sha256dHash) -> BlockStatus { pub fn get_block_status(&self, hash: &Sha256dHash) -> BlockStatus {
// get_header_by_hash looks up the height first, then fetches the header by that. // get_header_by_hash looks up the height first, then fetches the header by that.
// if the block is no longer the best block at this height, it'll return None. // if the block is no longer the best block at this height, it'll return None.

10
src/rest.rs

@ -242,10 +242,12 @@ fn handle_request(req: Request<Body>, query: &Arc<Query>, network: &Network) ->
let path: Vec<&str> = uri.path().split('/').skip(1).collect(); let path: Vec<&str> = uri.path().split('/').skip(1).collect();
info!("path {:?}", path); info!("path {:?}", path);
match (req.method(), path.get(0), path.get(1), path.get(2), path.get(3)) { match (req.method(), path.get(0), path.get(1), path.get(2), path.get(3)) {
(&Method::GET, Some(&"blocks"), Some(&"tip"), None, None) => { (&Method::GET, Some(&"tip"), Some(&"hash"), None, None) =>
let best_header_hash = query.get_best_header_hash(); Ok(http_message(StatusCode::OK, query.get_best_header_hash().be_hex_string())),
Ok(redirect(StatusCode::TEMPORARY_REDIRECT, format!("/block/{}", best_header_hash)))
}, (&Method::GET, Some(&"tip"), Some(&"height"), None, None) =>
Ok(http_message(StatusCode::OK, query.get_best_height().to_string())),
(&Method::GET, Some(&"blocks"), start_height, None, None) => { (&Method::GET, Some(&"blocks"), start_height, None, None) => {
let start_height = start_height.and_then(|height| height.parse::<usize>().ok()); let start_height = start_height.and_then(|height| height.parse::<usize>().ok());
blocks(&query, start_height) blocks(&query, start_height)

Loading…
Cancel
Save