Skip to content

Commit

Permalink
Add pre-checkpoint daa_score+timestamp data
Browse files Browse the repository at this point in the history
  • Loading branch information
coderofstuff committed Oct 2, 2023
1 parent 8c2a40f commit 579926e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
3 changes: 2 additions & 1 deletion components/consensusmanager/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use kaspa_consensus_core::{
block::Block,
block_count::BlockCount,
blockstatus::BlockStatus,
daa_score_timestamp::DaaScoreTimestamp,
errors::consensus::ConsensusResult,
header::Header,
pruning::{PruningPointProof, PruningPointTrustedData, PruningPointsList},
Expand Down Expand Up @@ -236,7 +237,7 @@ 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>> {
pub async fn async_get_chain_block_samples(&self) -> Vec<DaaScoreTimestamp> {
self.clone().spawn_blocking(|c| c.get_chain_block_samples()).await
}

Expand Down
3 changes: 2 additions & 1 deletion consensus/core/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
block_count::BlockCount,
blockstatus::BlockStatus,
coinbase::MinerData,
daa_score_timestamp::DaaScoreTimestamp,
errors::{
block::{BlockProcessResult, RuleError},
coinbase::CoinbaseResult,
Expand Down Expand Up @@ -93,7 +94,7 @@ pub trait ConsensusApi: Send + Sync {
unimplemented!()
}

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

Expand Down
23 changes: 23 additions & 0 deletions consensus/core/src/daa_score_timestamp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::header::Header;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DaaScoreTimestamp {
pub daa_score: u64,
pub timestamp: u64,
}

impl From<Header> for DaaScoreTimestamp {
fn from(header: Header) -> DaaScoreTimestamp {
DaaScoreTimestamp { daa_score: header.daa_score, timestamp: header.timestamp }
}
}

impl From<Arc<Header>> for DaaScoreTimestamp {
fn from(header: Arc<Header>) -> DaaScoreTimestamp {
DaaScoreTimestamp { daa_score: header.daa_score, timestamp: header.timestamp }
}
}
1 change: 1 addition & 0 deletions consensus/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod blockstatus;
pub mod coinbase;
pub mod config;
pub mod constants;
pub mod daa_score_timestamp;
pub mod errors;
pub mod hashing;
pub mod header;
Expand Down
40 changes: 35 additions & 5 deletions consensus/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use kaspa_consensus_core::{
blockhash::BlockHashExtensions,
blockstatus::BlockStatus,
coinbase::MinerData,
daa_score_timestamp::DaaScoreTimestamp,
errors::{
coinbase::CoinbaseResult,
consensus::{ConsensusError, ConsensusResult},
Expand All @@ -54,6 +55,7 @@ use kaspa_consensus_core::{
errors::{difficulty::DifficultyError, pruning::PruningImportError},
header::Header,
muhash::MuHashExtensions,
network::NetworkType,
pruning::{PruningPointProof, PruningPointTrustedData, PruningPointsList},
trusted::{ExternalGhostdagData, TrustedBlock},
tx::{MutableTransaction, Transaction, TransactionOutpoint, UtxoEntry},
Expand Down Expand Up @@ -451,9 +453,36 @@ impl ConsensusApi for Consensus {

/// 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>> {
fn get_chain_block_samples(&self) -> Vec<DaaScoreTimestamp> {
// Sorted from genesis to latest pruning_point_headers
let mut pp_headers = self.pruning_point_headers();
let mut sample_headers = Vec::<DaaScoreTimestamp>::new();
let pp_headers = self.pruning_point_headers();

// Part 1: Add samples from pruning point headers:
if self.config.net.network_type == NetworkType::Mainnet {
// For mainnet, we add extra data from before checkpoint genesis:
sample_headers.push(DaaScoreTimestamp { daa_score: 0, timestamp: 1636298787842 });
sample_headers.push(DaaScoreTimestamp { daa_score: 87133, timestamp: 1636386662010 });
sample_headers.push(DaaScoreTimestamp { daa_score: 176797, timestamp: 1636473700804 });
sample_headers.push(DaaScoreTimestamp { daa_score: 264837, timestamp: 1636560706885 });
sample_headers.push(DaaScoreTimestamp { daa_score: 355974, timestamp: 1636650005662 });
sample_headers.push(DaaScoreTimestamp { daa_score: 445152, timestamp: 1636737841327 });
sample_headers.push(DaaScoreTimestamp { daa_score: 536709, timestamp: 1636828600930 });
sample_headers.push(DaaScoreTimestamp { daa_score: 624635, timestamp: 1636912614350 });
sample_headers.push(DaaScoreTimestamp { daa_score: 712234, timestamp: 1636999362832 });
sample_headers.push(DaaScoreTimestamp { daa_score: 801831, timestamp: 1637088292662 });
sample_headers.push(DaaScoreTimestamp { daa_score: 890716, timestamp: 1637174890675 });
sample_headers.push(DaaScoreTimestamp { daa_score: 978396, timestamp: 1637260956454 });
sample_headers.push(DaaScoreTimestamp { daa_score: 1068387, timestamp: 1637349078269 });
sample_headers.push(DaaScoreTimestamp { daa_score: 1139626, timestamp: 1637418723538 });
sample_headers.push(DaaScoreTimestamp { daa_score: 1218320, timestamp: 1637495941516 });
}

for header in pp_headers.iter() {
sample_headers.push(DaaScoreTimestamp { daa_score: header.daa_score, timestamp: header.timestamp });
}

// Part 2: Add samples from recent chain blocks
let sc_read = self.storage.selected_chain_store.read();
let low = pp_headers.last().unwrap().hash;
let high = sc_read.get_tip().unwrap().1;
Expand All @@ -464,14 +493,15 @@ impl ConsensusApi for Consensus {

if step_size == 0 {
// Happens when not fully synced yet
return pp_headers;
return sample_headers;
}

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());
let chain_block_header = self.storage.headers_store.get_header(sc_read.get_by_index(index).unwrap()).unwrap();
sample_headers.push(DaaScoreTimestamp::from(chain_block_header));
}

pp_headers
sample_headers
}

fn get_virtual_parents(&self) -> BlockHashSet {
Expand Down

0 comments on commit 579926e

Please sign in to comment.