let start_time = request.start_time();
let tip_and_latest_blocks = match request.chain_tip() {
- Some(chain_tip) => Some(fetch_tip_and_latest_blocks(&self.inner, chain_tip)?),
+ Some(chain_tip) => Some(fetch_tip_and_latest_blocks(
+ &self.inner,
+ chain_tip,
+ &mut self.block_header_cache.lock().unwrap(),
+ )?),
None => None,
};
let start_time = request.start_time();
let tip_and_latest_blocks = match request.chain_tip() {
- Some(chain_tip) => Some(fetch_tip_and_latest_blocks(&self.inner, chain_tip)?),
+ Some(chain_tip) => Some(fetch_tip_and_latest_blocks(
+ &self.inner,
+ chain_tip,
+ &mut self.block_header_cache.lock().unwrap(),
+ )?),
None => None,
};
fn fetch_tip_and_latest_blocks(
client: &impl ElectrumApi,
prev_tip: CheckPoint<BlockHash>,
+ block_header_cache: &mut HashMap<u32, Header>,
) -> Result<(CheckPoint<BlockHash>, BTreeMap<u32, BlockHash>), Error> {
let HeaderNotification { height, .. } = client.block_headers_subscribe()?;
let new_tip_height = height as u32;
// to construct our checkpoint update.
let mut new_blocks = {
let start_height = new_tip_height.saturating_sub(CHAIN_SUFFIX_LENGTH - 1);
- let hashes = client
+ let headers = client
.block_headers(start_height as _, CHAIN_SUFFIX_LENGTH as _)?
- .headers
- .into_iter()
- .map(|h| h.block_hash());
- (start_height..).zip(hashes).collect::<BTreeMap<u32, _>>()
+ .headers;
+ block_header_cache.extend((start_height..).zip(headers.iter().copied()));
+
+ (start_height..)
+ .zip(headers.into_iter().map(|h| h.block_hash()))
+ .collect::<BTreeMap<u32, BlockHash>>()
};
// Find the "point of agreement" (if any).
new_tip_height >= cp_block.height,
"already checked that electrum's tip cannot be smaller"
);
- let hash = client.block_header(cp_block.height as _)?.block_hash();
+ let header = client.block_header(cp_block.height as _)?;
+ block_header_cache.insert(cp_block.height, header);
+ let hash = header.block_hash();
new_blocks.insert(cp_block.height, hash);
hash
}