Skip to content

Commit b1596f9

Browse files
committed
Use the header cache across listeners during initial disconnect
In `lightning-blocksync::init::synchronize_listeners`, we may have many listeners we want to do a chain diff on. When doing so, we should make sure we utilize our header cache, rather than querying our chain source for every header we need for each listener. Here we do so, inserting into the cache as we do chain diffs. On my node with a bitcoind on localhost, this brings the calculate-differences step of `init::synchronize_listeners` from ~500ms to under 150ms.
1 parent b2cc509 commit b1596f9

File tree

1 file changed

+19
-1
lines changed
  • lightning-block-sync/src

1 file changed

+19
-1
lines changed

lightning-block-sync/src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ pub(crate) trait Cache {
198198
/// Retrieves the block header keyed by the given block hash.
199199
fn look_up(&self, block_hash: &BlockHash) -> Option<&ValidatedBlockHeader>;
200200

201+
/// Inserts the given block header during a find_difference operation, implying it might not be
202+
/// the best header.
203+
fn insert_during_diff(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader);
204+
201205
/// Called when a block has been connected to the best chain to ensure it is available to be
202206
/// disconnected later if needed.
203207
fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader);
@@ -229,6 +233,15 @@ impl Cache for HeaderCache {
229233
self.0.get(block_hash)
230234
}
231235

236+
fn insert_during_diff(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
237+
self.0.insert(block_hash, block_header);
238+
239+
// Remove headers older than our newest header minus a week.
240+
let best_height = self.0.iter().map(|(_, header)| header.height).max().unwrap_or(0);
241+
let cutoff_height = best_height.saturating_sub(HEADER_CACHE_LIMIT);
242+
self.0.retain(|_, header| header.height >= cutoff_height);
243+
}
244+
232245
fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
233246
self.0.insert(block_hash, block_header);
234247

@@ -247,6 +260,10 @@ impl Cache for &mut HeaderCache {
247260
self.0.get(block_hash)
248261
}
249262

263+
fn insert_during_diff(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
264+
(*self).insert_during_diff(block_hash, block_header);
265+
}
266+
250267
fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
251268
(*self).block_connected(block_hash, block_header);
252269
}
@@ -383,7 +400,7 @@ where
383400
/// First resolves `prev_best_block` to a `ValidatedBlockHeader` using the `previous_blocks`
384401
/// field as fallback if needed, then finds the common ancestor.
385402
async fn find_difference_from_best_block<P: Poll>(
386-
&self, current_header: ValidatedBlockHeader, prev_best_block: BestBlock,
403+
&mut self, current_header: ValidatedBlockHeader, prev_best_block: BestBlock,
387404
chain_poller: &mut P,
388405
) -> BlockSourceResult<ChainDifference> {
389406
// Try to resolve the header for the previous best block. First try the block_hash,
@@ -408,6 +425,7 @@ where
408425
)?;
409426
if let Ok(header) = chain_poller.get_header(block_hash, Some(height)).await {
410427
found_header = Some(header);
428+
self.header_cache.insert_during_diff(*block_hash, header);
411429
break;
412430
}
413431
}

0 commit comments

Comments
 (0)