diff --git a/components/consensusmanager/src/session.rs b/components/consensusmanager/src/session.rs index 31f22c452d..9da860218a 100644 --- a/components/consensusmanager/src/session.rs +++ b/components/consensusmanager/src/session.rs @@ -236,6 +236,10 @@ impl ConsensusSessionOwned { self.clone().spawn_blocking(|c| c.get_headers_selected_tip()).await } + pub async fn async_get_chain_block_samples(&self) -> Vec> { + self.clone().spawn_blocking(|c| c.get_chain_block_samples()).await + } + /// Returns the anticone of block `hash` from the POV of `context`, i.e. `anticone(hash) ∩ past(context)`. /// Since this might be an expensive operation for deep blocks, we allow the caller to specify a limit /// `max_traversal_allowed` on the maximum amount of blocks to traverse for obtaining the answer diff --git a/consensus/core/src/api/mod.rs b/consensus/core/src/api/mod.rs index 6bc21677b5..88dee96495 100644 --- a/consensus/core/src/api/mod.rs +++ b/consensus/core/src/api/mod.rs @@ -93,6 +93,10 @@ pub trait ConsensusApi: Send + Sync { unimplemented!() } + fn get_chain_block_samples(&self) -> Vec> { + unimplemented!() + } + fn get_virtual_parents(&self) -> BlockHashSet { unimplemented!() } diff --git a/consensus/src/consensus/mod.rs b/consensus/src/consensus/mod.rs index cab615e8fb..d8244eb022 100644 --- a/consensus/src/consensus/mod.rs +++ b/consensus/src/consensus/mod.rs @@ -78,6 +78,8 @@ use tokio::sync::oneshot; use self::{services::ConsensusServices, storage::ConsensusStorage}; +use crate::model::stores::selected_chain::SelectedChainStoreReader; + pub struct Consensus { // DB db: Arc, @@ -438,6 +440,26 @@ impl ConsensusApi for Consensus { Ok(self.services.dag_traversal_manager.calculate_chain_path(hash, self.get_sink())) } + /// Returns a Vec of header samples since genesis + /// Ordered ascending by daa_score, first entry is genesis + fn get_chain_block_samples(&self) -> Vec> { + // Sorted from genesis to latest pruning_point_headers + let mut pp_headers = self.pruning_point_headers(); + let sc_read = self.storage.selected_chain_store.read(); + let low = pp_headers.last().unwrap().hash; + let high = sc_read.get_tip().unwrap().1; + + let low_index = sc_read.get_by_hash(low).unwrap_option().unwrap_or(0); + let high_index = sc_read.get_by_hash(high).unwrap_option().unwrap_or(0); + let step_size = (high_index - low_index) / 3; + + for index in (low_index+step_size..=high_index).step_by(step_size as usize) { + pp_headers.push(self.storage.headers_store.get_header(sc_read.get_by_index(index).unwrap()).unwrap()); + } + + pp_headers + } + fn get_virtual_parents(&self) -> BlockHashSet { self.virtual_stores.read().state.get().unwrap().parents.iter().copied().collect() } diff --git a/rpc/service/src/service.rs b/rpc/service/src/service.rs index d9257fffa2..a4c5fc7288 100644 --- a/rpc/service/src/service.rs +++ b/rpc/service/src/service.rs @@ -517,12 +517,10 @@ impl RpcApi for RpcCoreService { request: GetDaaScoreTimestampEstimateRequest, ) -> RpcResult { let session = self.consensus_manager.consensus().session().await; - let mut headers = session.async_pruning_point_headers().await; + let headers = session.async_get_chain_block_samples().await; let mut requested_daa_scores = request.daa_scores.clone(); let mut daa_score_timestamp_map = HashMap::::new(); - // TODO: Add more headers here from recent times, after last pp. Do so before reversing - headers.reverse(); requested_daa_scores.sort_by(|a, b| b.cmp(a)); let mut header_idx = 0;