Skip to content

Commit

Permalink
chore: move fee history cache fields to inner type (paradigmxyz#5587)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 27, 2023
1 parent 8892d04 commit 02e4135
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions crates/rpc/rpc/src/eth/api/fee_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,48 +37,40 @@ impl Default for FeeHistoryCacheConfig {
/// Wrapper struct for BTreeMap
#[derive(Debug, Clone)]
pub struct FeeHistoryCache {
/// Stores the lower bound of the cache
lower_bound: Arc<AtomicU64>,
upper_bound: Arc<AtomicU64>,
/// Config for FeeHistoryCache, consists of resolution for percentile approximation
/// and max number of blocks
config: FeeHistoryCacheConfig,
/// Stores the entries of the cache
entries: Arc<tokio::sync::RwLock<BTreeMap<u64, FeeHistoryEntry>>>,
#[allow(unused)]
eth_cache: EthStateCache,
inner: Arc<FeeHistoryCacheInner>,
}

impl FeeHistoryCache {
/// Creates new FeeHistoryCache instance, initialize it with the mose recent data, set bounds
pub fn new(eth_cache: EthStateCache, config: FeeHistoryCacheConfig) -> Self {
let init_tree_map = BTreeMap::new();

let entries = Arc::new(tokio::sync::RwLock::new(init_tree_map));

let upper_bound = Arc::new(AtomicU64::new(0));
let lower_bound = Arc::new(AtomicU64::new(0));

FeeHistoryCache { config, entries, upper_bound, lower_bound, eth_cache }
let inner = FeeHistoryCacheInner {
lower_bound: Default::default(),
upper_bound: Default::default(),
config,
entries: Default::default(),
eth_cache,
};
Self { inner: Arc::new(inner) }
}

/// How the cache is configured.
#[inline]
pub fn config(&self) -> &FeeHistoryCacheConfig {
&self.config
&self.inner.config
}

/// Returns the configured resolution for percentile approximation.
#[inline]
pub fn resolution(&self) -> u64 {
self.config.resolution
self.config().resolution
}

/// Processing of the arriving blocks
async fn insert_blocks<I>(&self, blocks: I)
where
I: Iterator<Item = (SealedBlock, Vec<Receipt>)>,
{
let mut entries = self.entries.write().await;
let mut entries = self.inner.entries.write().await;

let percentiles = self.predefined_percentiles();
// Insert all new blocks and calculate approximated rewards
Expand All @@ -96,30 +88,30 @@ impl FeeHistoryCache {
}

// enforce bounds by popping the oldest entries
while entries.len() > self.config.max_blocks as usize {
while entries.len() > self.inner.config.max_blocks as usize {
entries.pop_first();
}

if entries.len() == 0 {
self.upper_bound.store(0, SeqCst);
self.lower_bound.store(0, SeqCst);
self.inner.upper_bound.store(0, SeqCst);
self.inner.lower_bound.store(0, SeqCst);
return
}

let upper_bound = *entries.last_entry().expect("Contains at least one entry").key();
let lower_bound = *entries.first_entry().expect("Contains at least one entry").key();
self.upper_bound.store(upper_bound, SeqCst);
self.lower_bound.store(lower_bound, SeqCst);
self.inner.upper_bound.store(upper_bound, SeqCst);
self.inner.lower_bound.store(lower_bound, SeqCst);
}

/// Get UpperBound value for FeeHistoryCache
pub fn upper_bound(&self) -> u64 {
self.upper_bound.load(SeqCst)
self.inner.upper_bound.load(SeqCst)
}

/// Get LowerBound value for FeeHistoryCache
pub fn lower_bound(&self) -> u64 {
self.lower_bound.load(SeqCst)
self.inner.lower_bound.load(SeqCst)
}

/// Collect fee history for given range.
Expand All @@ -136,7 +128,7 @@ impl FeeHistoryCache {
let lower_bound = self.lower_bound();
let upper_bound = self.upper_bound();
if start_block >= lower_bound && end_block <= upper_bound {
let entries = self.entries.read().await;
let entries = self.inner.entries.read().await;
let result = entries
.range(start_block..=end_block + 1)
.map(|(_, fee_entry)| fee_entry.clone())
Expand All @@ -161,6 +153,21 @@ impl FeeHistoryCache {
}
}

/// Container type for shared state in [FeeHistoryCache]
#[derive(Debug)]
struct FeeHistoryCacheInner {
/// Stores the lower bound of the cache
lower_bound: AtomicU64,
upper_bound: AtomicU64,
/// Config for FeeHistoryCache, consists of resolution for percentile approximation
/// and max number of blocks
config: FeeHistoryCacheConfig,
/// Stores the entries of the cache
entries: tokio::sync::RwLock<BTreeMap<u64, FeeHistoryEntry>>,
#[allow(unused)]
eth_cache: EthStateCache,
}

/// Awaits for new chain events and directly inserts them into the cache so they're available
/// immediately before they need to be fetched from disk.
pub async fn fee_history_cache_new_blocks_task<St, Provider>(
Expand Down

0 comments on commit 02e4135

Please sign in to comment.