Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Cache headers in HeaderWithAncestors response.
Browse files Browse the repository at this point in the history
Also fulfills request locally if all headers are in cache.
  • Loading branch information
jimpo committed Aug 21, 2018
1 parent 064c2a6 commit 586f46c
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion ethcore/light/src/on_demand/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,26 @@ impl CheckedRequest {

None
}
CheckedRequest::HeaderWithAncestors(_, ref req) => {
if req.skip != 1 || !req.reverse {
return None;
}

if let Some(&net_request::HashOrNumber::Hash(start)) = req.start.as_ref() {
let mut result = Vec::with_capacity(req.max as usize);
let mut hash = start;
for _ in 0..req.max {
match cache.lock().block_header(&hash) {
Some(header) => {
hash = header.parent_hash();
result.push(header);
}
None => return None,
}
}
Some(Response::HeaderWithAncestors(result))
} else { None }
}
CheckedRequest::Receipts(ref check, ref req) => {
// empty transactions -> no receipts
if check.0.as_ref().ok().map_or(false, |hdr| hdr.receipts_root() == KECCAK_NULL_RLP) {
Expand Down Expand Up @@ -791,7 +811,7 @@ impl HeaderWithAncestors {
/// Check a response for the headers.
pub fn check_response(
&self,
_cache: &Mutex<::cache::Cache>,
cache: &Mutex<::cache::Cache>,
start: &net_request::HashOrNumber,
headers: &[encoded::Header]
) -> Result<Vec<encoded::Header>, Error> {
Expand Down Expand Up @@ -828,6 +848,11 @@ impl HeaderWithAncestors {
}
}

let mut cache = cache.lock();
for header in headers {
cache.insert_block_header(header.hash(), header.clone());
}

Ok(headers.to_vec())
}
}
Expand Down

0 comments on commit 586f46c

Please sign in to comment.