diff --git a/src/new_index/schema.rs b/src/new_index/schema.rs index 120eae454..8210325e1 100644 --- a/src/new_index/schema.rs +++ b/src/new_index/schema.rs @@ -424,6 +424,25 @@ impl ChainQuery { } } + pub fn get_headers(&self, from: &BlockHash, count: usize) -> Option> { + let _timer = self.start_timer("get_headers"); + let store = self.store.indexed_headers.read().unwrap(); + + if let Some(from_header) = store.header_by_blockhash(from) { + Some( + store + .iter() + .rev() + .skip(self.best_height() - from_header.height()) + .take(count) + .map(|e| e.header().clone()) + .collect(), + ) + } else { + None + } + } + pub fn get_block_header(&self, hash: &BlockHash) -> Option { let _timer = self.start_timer("get_block_header"); Some(self.header_by_hash(hash)?.header().clone()) diff --git a/src/rest.rs b/src/rest.rs index 00a971df8..8e8ea6e10 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -627,6 +627,40 @@ fn handle_request( path.get(3), path.get(4), ) { + (&Method::GET, Some(&"headers"), hash, count, None, None) => { + let count = count + .and_then(|c| c.parse::().ok()) + .map(|c| c.max(1).min(2000)) + .unwrap_or(2000); + let from_hash = hash + .map(|h| BlockHash::from_hex(h)) + .transpose()? + .unwrap_or_else(|| query.chain().best_hash()); + + let headers = match query.chain().get_headers(&from_hash, count) { + Some(headers) => headers, + None => return Err(HttpError::not_found("Block not found".to_string())), + }; + + let mut raw = Vec::with_capacity(8 + 80 * headers.len()); + raw.append(&mut encode::serialize( + &encode::VarInt(headers.len() as u64), + )); + for header in headers.into_iter() { + raw.append(&mut encode::serialize(&header)); + } + + let cache_ttl = match hash { + None => TTL_SHORT, + Some(_) => TTL_SHORT, + }; + Ok(Response::builder() + .status(StatusCode::OK) + .header("Content-Type", "application/octet-stream") + .header("Cache-Control", format!("public, max-age={:}", cache_ttl)) + .body(Body::from(raw)) + .unwrap()) + } (&Method::GET, Some(&"blocks"), Some(&"tip"), Some(&"hash"), None, None) => http_message( StatusCode::OK, query.chain().best_hash().to_hex(),