Skip to content

Commit 826b596

Browse files
committed
Add errors and remove old implementation
1 parent f0f12be commit 826b596

File tree

5 files changed

+14
-52
lines changed

5 files changed

+14
-52
lines changed

consensus/core/src/errors/consensus.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ pub enum ConsensusError {
3232
#[error("difficulty error: {0}")]
3333
DifficultyError(#[from] DifficultyError),
3434

35+
#[error("window data has only {0} entries -- this usually happens when the node has just began syncing")]
36+
InsufficientWindowData(usize),
37+
3538
#[error("{0}")]
3639
General(&'static str),
3740

consensus/core/src/errors/difficulty.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ use thiserror::Error;
22

33
#[derive(Error, Debug, Clone)]
44
pub enum DifficultyError {
5-
#[error("under min allowed window size ({0} < {1})")]
6-
UnderMinWindowSizeAllowed(usize, usize),
7-
8-
#[error("window data has only {0} entries -- this usually happens when the node has just began syncing")]
9-
InsufficientWindowData(usize),
10-
115
#[error("min window timestamp is equal to the max window timestamp")]
126
EmptyTimestampRange,
137
}

consensus/src/consensus/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::{
4141
processes::{
4242
difficulty::calc_work,
4343
ghostdag::ordering::SortableBlock,
44-
window::{WindowManager, WindowType},
44+
window::{self, WindowManager, WindowType},
4545
},
4646
};
4747
use kaspa_consensus_core::{
@@ -470,6 +470,10 @@ impl Consensus {
470470
}
471471

472472
fn estimate_network_hashes_per_second_impl(&self, ghostdag_data: &GhostdagData, window_size: usize) -> ConsensusResult<u64> {
473+
if window_size == 0 {
474+
return Err(ConsensusError::General("window size cannot be zero"));
475+
}
476+
473477
let mut count = 0;
474478
let mut red_work: BlueWorkType = 0.into();
475479
let mut bottom = ghostdag_data.selected_parent;
@@ -485,6 +489,11 @@ impl Consensus {
485489
break;
486490
}
487491
}
492+
493+
if count < window_size {
494+
return Err(ConsensusError::InsufficientWindowData(count));
495+
}
496+
488497
let sp_header = self.headers_store.get_header(ghostdag_data.selected_parent).unwrap();
489498
let bottom_header = self.headers_store.get_header(bottom).unwrap();
490499
let blue_work = sp_header.blue_work - bottom_header.blue_work;
@@ -1135,7 +1144,7 @@ impl ConsensusApi for Consensus {
11351144
let ghostdag_data = self.ghostdag_store.get_data(hash).unwrap();
11361145
// The selected parent header is used within to check for sampling activation, so we verify its existence first
11371146
if !self.headers_store.has(ghostdag_data.selected_parent).unwrap() {
1138-
return Err(ConsensusError::DifficultyError(DifficultyError::InsufficientWindowData(0)));
1147+
return Err(ConsensusError::InsufficientWindowData(0));
11391148
}
11401149
self.estimate_network_hashes_per_second_impl(&ghostdag_data, window_size)
11411150
}

consensus/src/processes/difficulty.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,6 @@ trait DifficultyManagerExtension {
4444
.collect()
4545
}
4646

47-
fn internal_estimate_network_hashes_per_second(&self, window: &BlockWindowHeap) -> DifficultyResult<u64> {
48-
// TODO: perhaps move this const
49-
const MIN_WINDOW_SIZE: usize = 1000;
50-
let window_size = window.len();
51-
if window_size < MIN_WINDOW_SIZE {
52-
return Err(DifficultyError::UnderMinWindowSizeAllowed(window_size, MIN_WINDOW_SIZE));
53-
}
54-
let difficulty_blocks = self.get_difficulty_blocks(window);
55-
let (min_ts, max_ts) = difficulty_blocks.iter().map(|x| x.timestamp).minmax().into_option().unwrap();
56-
if min_ts == max_ts {
57-
return Err(DifficultyError::EmptyTimestampRange);
58-
}
59-
let window_duration = (max_ts - min_ts) / 1000; // Divided by 1000 to convert milliseconds to seconds
60-
if window_duration == 0 {
61-
return Ok(0);
62-
}
63-
64-
let (min_blue_work, max_blue_work) =
65-
difficulty_blocks.iter().map(|x| x.sortable_block.blue_work).minmax().into_option().unwrap();
66-
67-
Ok(((max_blue_work - min_blue_work) / window_duration).as_u64())
68-
}
69-
7047
#[inline]
7148
fn check_min_difficulty_window_size(difficulty_window_size: usize, min_difficulty_window_size: usize) {
7249
assert!(
@@ -156,10 +133,6 @@ impl<T: HeaderStoreReader> FullDifficultyManager<T> {
156133
let new_target = average_target * max(max_ts - min_ts, 1) / (self.target_time_per_block * difficulty_blocks_len);
157134
Uint256::try_from(new_target.min(self.max_difficulty_target)).expect("max target < Uint256::MAX").compact_target_bits()
158135
}
159-
160-
pub fn estimate_network_hashes_per_second(&self, window: &BlockWindowHeap) -> DifficultyResult<u64> {
161-
self.internal_estimate_network_hashes_per_second(window)
162-
}
163136
}
164137

165138
impl<T: HeaderStoreReader> DifficultyManagerExtension for FullDifficultyManager<T> {
@@ -396,10 +369,6 @@ impl<T: HeaderStoreReader, U: GhostdagStoreReader> SampledDifficultyManager<T, U
396369

397370
Uint256::try_from(new_target.min(self.max_difficulty_target)).expect("max target < Uint256::MAX").compact_target_bits()
398371
}
399-
400-
pub fn estimate_network_hashes_per_second(&self, window: &BlockWindowHeap) -> DifficultyResult<u64> {
401-
self.internal_estimate_network_hashes_per_second(window)
402-
}
403372
}
404373

405374
impl<T: HeaderStoreReader, U: GhostdagStoreReader> DifficultyManagerExtension for SampledDifficultyManager<T, U> {

consensus/src/processes/window.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ pub trait WindowManager {
5757
fn calculate_difficulty_bits(&self, ghostdag_data: &GhostdagData, daa_window: &DaaWindow) -> u32;
5858
fn calc_past_median_time(&self, ghostdag_data: &GhostdagData) -> Result<(u64, Arc<BlockWindowHeap>), RuleError>;
5959
fn calc_past_median_time_for_known_hash(&self, hash: Hash) -> Result<u64, RuleError>;
60-
fn estimate_network_hashes_per_second(&self, window: Arc<BlockWindowHeap>) -> DifficultyResult<u64>;
6160
fn window_size(&self, ghostdag_data: &GhostdagData, window_type: WindowType) -> usize;
6261
fn sample_rate(&self, ghostdag_data: &GhostdagData, window_type: WindowType) -> u64;
6362

@@ -263,10 +262,6 @@ impl<T: GhostdagStoreReader, U: BlockWindowCacheReader + BlockWindowCacheWriter,
263262
}
264263
}
265264

266-
fn estimate_network_hashes_per_second(&self, window: Arc<BlockWindowHeap>) -> DifficultyResult<u64> {
267-
self.difficulty_manager.estimate_network_hashes_per_second(&window)
268-
}
269-
270265
fn window_size(&self, _ghostdag_data: &GhostdagData, window_type: WindowType) -> usize {
271266
match window_type {
272267
WindowType::DifficultyWindow => self.difficulty_window_size,
@@ -682,10 +677,6 @@ impl<T: GhostdagStoreReader, U: BlockWindowCacheReader + BlockWindowCacheWriter,
682677
}
683678
}
684679

685-
fn estimate_network_hashes_per_second(&self, window: Arc<BlockWindowHeap>) -> DifficultyResult<u64> {
686-
self.difficulty_manager.estimate_network_hashes_per_second(&window)
687-
}
688-
689680
fn window_size(&self, _ghostdag_data: &GhostdagData, window_type: WindowType) -> usize {
690681
match window_type {
691682
WindowType::DifficultyWindow => self.difficulty_window_size,
@@ -865,10 +856,6 @@ impl<T: GhostdagStoreReader, U: BlockWindowCacheReader + BlockWindowCacheWriter,
865856
}
866857
}
867858

868-
fn estimate_network_hashes_per_second(&self, window: Arc<BlockWindowHeap>) -> DifficultyResult<u64> {
869-
self.sampled_window_manager.estimate_network_hashes_per_second(window)
870-
}
871-
872859
fn window_size(&self, ghostdag_data: &GhostdagData, window_type: WindowType) -> usize {
873860
match self.sampling(ghostdag_data.selected_parent) {
874861
true => self.sampled_window_manager.window_size(ghostdag_data, window_type),

0 commit comments

Comments
 (0)