From 009dc147190a836c9b0e64f1ca6e7b367947eda1 Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Tue, 15 Oct 2024 21:02:05 +0530 Subject: [PATCH] chore: refactor cached_view into own module --- crates/services/p2p/src/lib.rs | 2 + crates/services/p2p/src/service.rs | 87 ++---------------------------- 2 files changed, 5 insertions(+), 84 deletions(-) diff --git a/crates/services/p2p/src/lib.rs b/crates/services/p2p/src/lib.rs index fbd82c2545..375eeb7351 100644 --- a/crates/services/p2p/src/lib.rs +++ b/crates/services/p2p/src/lib.rs @@ -16,6 +16,8 @@ pub mod request_response; pub mod service; mod utils; +mod cached_view; + pub use gossipsub::config as gossipsub_config; pub use heartbeat::Config; diff --git a/crates/services/p2p/src/service.rs b/crates/services/p2p/src/service.rs index 6fdd1b087a..89f097f9ec 100644 --- a/crates/services/p2p/src/service.rs +++ b/crates/services/p2p/src/service.rs @@ -1,4 +1,5 @@ use crate::{ + cached_view::CachedView, codecs::postcard::PostcardCodec, config::{ Config, @@ -27,12 +28,7 @@ use crate::{ }, }; use anyhow::anyhow; -use dashmap::DashMap; -use fuel_core_metrics::p2p_metrics::{ - increment_p2p_req_res_cache_hits, - increment_p2p_req_res_cache_misses, - set_blocks_requested, -}; +use fuel_core_metrics::p2p_metrics::set_blocks_requested; use fuel_core_services::{ stream::BoxStream, AsyncProcessor, @@ -43,10 +39,7 @@ use fuel_core_services::{ SyncProcessor, TraceErr, }; -use fuel_core_storage::{ - transactional::AtomicView, - Result as StorageResult, -}; +use fuel_core_storage::transactional::AtomicView; use fuel_core_types::{ blockchain::SealedBlockHeader, fuel_tx::{ @@ -498,80 +491,6 @@ impl Task { } } -struct CachedView { - sealed_block_headers: DashMap, Vec>, - transactions_on_blocks: DashMap, Vec>, - metrics: bool, -} - -impl CachedView { - fn new(metrics: bool) -> Self { - Self { - sealed_block_headers: DashMap::new(), - transactions_on_blocks: DashMap::new(), - metrics, - } - } - - fn clear(&self) { - self.sealed_block_headers.clear(); - self.transactions_on_blocks.clear(); - } - - fn update_metrics(&self, update_fn: U) - where - U: FnOnce(), - { - if self.metrics { - update_fn() - } - } - - fn get_sealed_headers( - &self, - view: &V, - block_height_range: Range, - ) -> StorageResult>> - where - V: P2pDb, - { - if let Some(headers) = self.sealed_block_headers.get(&block_height_range) { - self.update_metrics(increment_p2p_req_res_cache_hits); - Ok(Some(headers.clone())) - } else { - self.update_metrics(increment_p2p_req_res_cache_misses); - let headers = view.get_sealed_headers(block_height_range.clone())?; - if let Some(headers) = &headers { - self.sealed_block_headers - .insert(block_height_range, headers.clone()); - } - Ok(headers) - } - } - - fn get_transactions( - &self, - view: &V, - block_height_range: Range, - ) -> StorageResult>> - where - V: P2pDb, - { - if let Some(transactions) = self.transactions_on_blocks.get(&block_height_range) { - self.update_metrics(increment_p2p_req_res_cache_hits); - Ok(Some(transactions.clone())) - } else { - self.update_metrics(increment_p2p_req_res_cache_misses); - let transactions = view.get_transactions(block_height_range.clone())?; - if let Some(transactions) = &transactions { - self.transactions_on_blocks - .insert(block_height_range, transactions.clone()); - } - Ok(transactions) - } - } -} - impl Task where P: TaskP2PService + 'static,