From 23c76caafaa811c7825c433aeb2d523054db7ec5 Mon Sep 17 00:00:00 2001 From: Adrian Brink Date: Thu, 6 Apr 2017 20:50:52 +0200 Subject: [PATCH] Add header to cache --- ethcore/light/src/client/header_chain.rs | 31 ++++++++++++++---------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/ethcore/light/src/client/header_chain.rs b/ethcore/light/src/client/header_chain.rs index 340488297..71c0aa314 100644 --- a/ethcore/light/src/client/header_chain.rs +++ b/ethcore/light/src/client/header_chain.rs @@ -365,19 +365,24 @@ impl HeaderChain { /// will be returned. pub fn block_header(&self, id: BlockId) -> Option { let load_from_db = |hash: H256| { - let cached = { - let mut cache = self.cache.lock(); - cache.block_header(&hash) - }; + let mut cache = self.cache.lock(); + let header = cache.block_header(&hash); - if cached.is_some() { - return cached - } else { - match self.db.get(self.col, &hash) { - Ok(val) => val.map(|x| x.to_vec()).map(encoded::Header::new), - Err(e) => { - warn!(target: "chain", "Failed to read from database: {}", e); - None + match header { + Some(header) => Some(header), + None => { + match self.db.get(self.col, &hash) { + Ok(dbValue) => { + dbValue.map(|x| x.to_vec()).map(encoded::Header::new) + .and_then(|header| { + cache.insert_block_header(hash.clone(), header.clone()); + Some(header) + }) + }, + Err(e) => { + warn!(target: "chain", "Failed to read from database: {}", e); + None + } } } } @@ -423,7 +428,7 @@ impl HeaderChain { } } - /// Get the nth CHT root, if it has been computed. + /// Get the nth CHT root, if it's been computed. /// /// CHT root 0 is from block `1..2048`. /// CHT root 1 is from block `2049..4096`