Skip to content

Commit

Permalink
Sampling for chain blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
coderofstuff committed Sep 2, 2023
1 parent 5433fc5 commit 874ffff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions components/consensusmanager/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<Header>> {
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
Expand Down
4 changes: 4 additions & 0 deletions consensus/core/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ pub trait ConsensusApi: Send + Sync {
unimplemented!()
}

fn get_chain_block_samples(&self) -> Vec<Arc<Header>> {
unimplemented!()
}

fn get_virtual_parents(&self) -> BlockHashSet {
unimplemented!()
}
Expand Down
22 changes: 22 additions & 0 deletions consensus/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ use tokio::sync::oneshot;

use self::{services::ConsensusServices, storage::ConsensusStorage};

use crate::model::stores::selected_chain::SelectedChainStoreReader;

pub struct Consensus {
// DB
db: Arc<DB>,
Expand Down Expand Up @@ -447,6 +449,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<Arc<Header>> {
// 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()
}
Expand Down
4 changes: 1 addition & 3 deletions rpc/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,10 @@ impl RpcApi for RpcCoreService {
request: GetDaaScoreTimestampEstimateRequest,
) -> RpcResult<GetDaaScoreTimestampEstimateResponse> {
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::<u64, u64>::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;
Expand Down

0 comments on commit 874ffff

Please sign in to comment.