Skip to content

Commit bf26302

Browse files
committed
Add the /headers[/:from_hash[/:count]] endpoint
Add a new endpoint to download headers in bulk, up to 2000 with a single request. Headers are returned in binary form, with the VarInt-encoded number of items in the body preceeding them. By default `from_hash` is the current best hash, but a different starting block can be specified. The returned list goes "backwards" returning the `count - 1` blocks *before* `from_hash` plus the header of `from_hash` itself. This allows caching the response indefinitely, since it's guaranteed that the headers that come before a given block will never change. Returns an error if `from_hash` is not a valid block or it isn't found in the blockchain. If `count` is greater than the limit of `2000` it will be silently capped to said value.
1 parent abfbce7 commit bf26302

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/new_index/schema.rs

+19
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,25 @@ impl ChainQuery {
424424
}
425425
}
426426

427+
pub fn get_headers(&self, from: &BlockHash, count: usize) -> Option<Vec<BlockHeader>> {
428+
let _timer = self.start_timer("get_headers");
429+
let store = self.store.indexed_headers.read().unwrap();
430+
431+
if let Some(from_header) = store.header_by_blockhash(from) {
432+
Some(
433+
store
434+
.iter()
435+
.rev()
436+
.skip(self.best_height() - from_header.height())
437+
.take(count)
438+
.map(|e| e.header().clone())
439+
.collect(),
440+
)
441+
} else {
442+
None
443+
}
444+
}
445+
427446
pub fn get_block_header(&self, hash: &BlockHash) -> Option<BlockHeader> {
428447
let _timer = self.start_timer("get_block_header");
429448
Some(self.header_by_hash(hash)?.header().clone())

src/rest.rs

+34
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,40 @@ fn handle_request(
627627
path.get(3),
628628
path.get(4),
629629
) {
630+
(&Method::GET, Some(&"headers"), hash, count, None, None) => {
631+
let count = count
632+
.and_then(|c| c.parse::<usize>().ok())
633+
.map(|c| c.max(1).min(2000))
634+
.unwrap_or(2000);
635+
let from_hash = hash
636+
.map(|h| BlockHash::from_hex(h))
637+
.transpose()?
638+
.unwrap_or_else(|| query.chain().best_hash());
639+
640+
let headers = match query.chain().get_headers(&from_hash, count) {
641+
Some(headers) => headers,
642+
None => return Err(HttpError::not_found("Block not found".to_string())),
643+
};
644+
645+
let mut raw = Vec::with_capacity(8 + 80 * headers.len());
646+
raw.append(&mut encode::serialize(
647+
&encode::VarInt(headers.len() as u64),
648+
));
649+
for header in headers.into_iter() {
650+
raw.append(&mut encode::serialize(&header));
651+
}
652+
653+
let cache_ttl = match hash {
654+
None => TTL_SHORT,
655+
Some(_) => TTL_SHORT,
656+
};
657+
Ok(Response::builder()
658+
.status(StatusCode::OK)
659+
.header("Content-Type", "application/octet-stream")
660+
.header("Cache-Control", format!("public, max-age={:}", cache_ttl))
661+
.body(Body::from(raw))
662+
.unwrap())
663+
}
630664
(&Method::GET, Some(&"blocks"), Some(&"tip"), Some(&"hash"), None, None) => http_message(
631665
StatusCode::OK,
632666
query.chain().best_hash().to_hex(),

0 commit comments

Comments
 (0)