Skip to content

Commit

Permalink
fix: avoid retaining uncled blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Nov 27, 2023
1 parent f5bc80e commit a9a5389
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions graph-gateway/src/chains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand Down
7 changes: 7 additions & 0 deletions prelude/src/epoch_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ impl<K: Eq + Hash, V, const C: u8> EpochCache<K, V, C> {
f(v);
}
}

pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&K, &mut V) -> bool,
{
self.0.retain(|k, (v, _)| f(k, v));
}
}

#[cfg(test)]
Expand Down

0 comments on commit a9a5389

Please sign in to comment.