Add header to cache
This commit is contained in:
parent
8a7ca6f0ba
commit
23c76caafa
@ -365,19 +365,24 @@ impl HeaderChain {
|
|||||||
/// will be returned.
|
/// will be returned.
|
||||||
pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
pub fn block_header(&self, id: BlockId) -> Option<encoded::Header> {
|
||||||
let load_from_db = |hash: H256| {
|
let load_from_db = |hash: H256| {
|
||||||
let cached = {
|
let mut cache = self.cache.lock();
|
||||||
let mut cache = self.cache.lock();
|
let header = cache.block_header(&hash);
|
||||||
cache.block_header(&hash)
|
|
||||||
};
|
|
||||||
|
|
||||||
if cached.is_some() {
|
match header {
|
||||||
return cached
|
Some(header) => Some(header),
|
||||||
} else {
|
None => {
|
||||||
match self.db.get(self.col, &hash) {
|
match self.db.get(self.col, &hash) {
|
||||||
Ok(val) => val.map(|x| x.to_vec()).map(encoded::Header::new),
|
Ok(dbValue) => {
|
||||||
Err(e) => {
|
dbValue.map(|x| x.to_vec()).map(encoded::Header::new)
|
||||||
warn!(target: "chain", "Failed to read from database: {}", e);
|
.and_then(|header| {
|
||||||
None
|
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 0 is from block `1..2048`.
|
||||||
/// CHT root 1 is from block `2049..4096`
|
/// CHT root 1 is from block `2049..4096`
|
||||||
|
Loading…
Reference in New Issue
Block a user