From 6e474307323105d7bef3ebe9d7f3fa1633bd1965 Mon Sep 17 00:00:00 2001 From: Theo Butler Date: Mon, 27 Nov 2023 17:06:53 -0500 Subject: [PATCH] fix: avoid retaining uncled blocks in cache --- graph-gateway/src/chains/mod.rs | 10 ++++++++-- prelude/src/epoch_cache.rs | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/graph-gateway/src/chains/mod.rs b/graph-gateway/src/chains/mod.rs index f0af1dc2..0842aa32 100644 --- a/graph-gateway/src/chains/mod.rs +++ b/graph-gateway/src/chains/mod.rs @@ -187,11 +187,17 @@ impl Actor { } } - self.handle_block(head.block.clone()).await; - let blocks_per_minute = self.block_rate_estimator.update(head.block.number); self.blocks_per_minute_tx.write(blocks_per_minute); + // Remove prior blocks from the past minute. This avoids retaining uncled blocks that are + // frequently used. + let cutoff = head.block.number - blocks_per_minute; + self.number_to_hash.retain(|n, _| *n < cutoff); + self.hash_to_number.retain(|_, n| *n < cutoff); + + self.handle_block(head.block.clone()).await; + with_metric(&METRICS.chain_head, &[&self.chain], |g| { g.set(head.block.number as i64) }); diff --git a/prelude/src/epoch_cache.rs b/prelude/src/epoch_cache.rs index 1dbbc213..905be7a9 100644 --- a/prelude/src/epoch_cache.rs +++ b/prelude/src/epoch_cache.rs @@ -82,6 +82,13 @@ impl EpochCache { f(v); } } + + pub fn retain(&mut self, mut f: F) + where + F: FnMut(&K, &mut V) -> bool, + { + self.0.retain(|k, (v, _)| f(k, v)); + } } #[cfg(test)]