Skip to content

Commit 9fc766e

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 dcc895f commit 9fc766e

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);
@@ -226,6 +230,15 @@ impl Cache for HeaderCache {
226230
self.0.get(block_hash)
227231
}
228232

233+
fn insert_during_diff(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
234+
self.0.insert(block_hash, block_header);
235+
236+
// Remove headers older than our newest header minus a week.
237+
let best_height = self.0.iter().map(|(_, header)| header.height).max().unwrap_or(0);
238+
let cutoff_height = best_height.saturating_sub(6 * 24 * 7);
239+
self.0.retain(|_, header| header.height >= cutoff_height);
240+
}
241+
229242
fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
230243
self.0.insert(block_hash, block_header);
231244

@@ -244,6 +257,10 @@ impl Cache for &mut HeaderCache {
244257
self.0.get(block_hash)
245258
}
246259

260+
fn insert_during_diff(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
261+
(*self).insert_during_diff(block_hash, block_header);
262+
}
263+
247264
fn block_connected(&mut self, block_hash: BlockHash, block_header: ValidatedBlockHeader) {
248265
(*self).block_connected(block_hash, block_header);
249266
}
@@ -380,7 +397,7 @@ where
380397
/// First resolves `prev_best_block` to a `ValidatedBlockHeader` using the `previous_blocks`
381398
/// field as fallback if needed, then finds the common ancestor.
382399
async fn find_difference_from_best_block<P: Poll>(
383-
&self, current_header: ValidatedBlockHeader, prev_best_block: BestBlock,
400+
&mut self, current_header: ValidatedBlockHeader, prev_best_block: BestBlock,
384401
chain_poller: &mut P,
385402
) -> BlockSourceResult<ChainDifference> {
386403
// Try to resolve the header for the previous best block. First try the block_hash,
@@ -404,6 +421,7 @@ where
404421
)?;
405422
if let Ok(header) = chain_poller.get_header(block_hash, Some(height)).await {
406423
found_header = Some(header);
424+
self.header_cache.insert_during_diff(*block_hash, header);
407425
break;
408426
}
409427
}

0 commit comments

Comments
 (0)