Skip to content

Commit 7835653

Browse files
committed
periodically remove old missing fetch info
Signed-off-by: Eval EXEC <execvy@gmail.com>
1 parent 6fe27f8 commit 7835653

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

light-client-lib/src/protocols/filter/block_filter.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@ impl FilterProtocol {
151151
db_matched_blocks.blocks_count,
152152
matched_blocks.len(),
153153
);
154-
db_matched_blocks.blocks.iter().for_each(|b| {
155-
debug!("db matched block: {}, proved={}", b.hash, b.proved);
156-
});
157154
let option = matched_blocks.is_empty();
158155

159156
if option {

light-client-lib/src/protocols/light_client/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,11 @@ impl LightClientProtocol {
705705
}
706706

707707
async fn get_idle_blocks(&mut self, nc: &BoxedCKBProtocolContext) {
708+
// Clean up old missing headers (uncle blocks) older than 1 hour
709+
// to prevent unbounded memory growth in fetching_headers
710+
const MAX_MISSING_AGE_MS: u64 = 3_600_000; // 1 hour
711+
self.peers.cleanup_old_missing_headers(MAX_MISSING_AGE_MS);
712+
708713
let tip_header = self.storage.get_tip_header();
709714
let matched_blocks = self.peers.matched_blocks().read().await;
710715

light-client-lib/src/protocols/light_client/peers.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,32 @@ impl Peers {
12421242
}
12431243
}
12441244
}
1245+
1246+
/// Clean up old missing entries from fetching_headers to prevent unbounded memory growth
1247+
/// This is called periodically to remove uncle blocks that were marked as missing
1248+
/// but are no longer needed (older than max_age_ms)
1249+
pub(crate) fn cleanup_old_missing_headers(&self, max_age_ms: u64) {
1250+
let now = unix_time_as_millis();
1251+
let mut removed_count = 0;
1252+
1253+
self.fetching_headers.retain(|_hash, info| {
1254+
if info.missing && now.saturating_sub(info.added_ts) > max_age_ms {
1255+
removed_count += 1;
1256+
false // Remove this entry
1257+
} else {
1258+
true // Keep this entry
1259+
}
1260+
});
1261+
1262+
if removed_count > 0 {
1263+
log::debug!(
1264+
"Cleaned up {} old missing headers (age > {}ms)",
1265+
removed_count,
1266+
max_age_ms
1267+
);
1268+
}
1269+
}
1270+
12451271
// mark all fetching hashes (headers/txs) as timeout
12461272
pub(crate) fn mark_fetching_headers_timeout(&self, peer_index: PeerIndex) {
12471273
if let Some(peer) = self.get_peer(&peer_index) {

light-client-lib/src/storage/db/native.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use ckb_types::{
1818
utilities::{build_filter_data, calc_filter_hash},
1919
U256,
2020
};
21-
use log::info;
2221
use rocksdb::{
2322
ops::{Delete, GetPinned},
2423
prelude::{Get, Iterate, Open, Put, WriteOps},

light-client-lib/src/tests/protocols/light_client/send_last_state_proof.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,12 +2035,15 @@ async fn test_with_reorg_blocks(param: ReorgTestParameter) {
20352035
} else {
20362036
assert!(earliest_matched_blocks_opt.is_some());
20372037
assert_eq!(
2038-
earliest_matched_blocks_opt.unwrap().0,
2038+
earliest_matched_blocks_opt.unwrap().start_number,
20392039
earliest_matched_number
20402040
);
20412041
assert!(latest_matched_blocks_opt.is_some());
20422042
let expected_latest = cmp::min(prev_last_number, latest_kept_matched_block);
2043-
assert_eq!(latest_matched_blocks_opt.unwrap().0, expected_latest);
2043+
assert_eq!(
2044+
latest_matched_blocks_opt.unwrap().start_number,
2045+
expected_latest
2046+
);
20442047
}
20452048
}
20462049
assert_eq!(

0 commit comments

Comments
 (0)