Skip to content

Commit 4a6eea6

Browse files
committed
Move is_nearly_synced logic from params to rule engine
1 parent 8e52faa commit 4a6eea6

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

consensus/core/src/config/params.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ use crate::{
1010
};
1111
use kaspa_addresses::Prefix;
1212
use kaspa_math::Uint256;
13-
use std::{
14-
cmp::min,
15-
time::{SystemTime, UNIX_EPOCH},
16-
};
13+
use std::cmp::min;
1714

1815
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
1916
pub struct ForkActivation(u64);
@@ -136,10 +133,6 @@ pub struct Params {
136133
pub runtime_sig_op_counting: ForkActivation,
137134
}
138135

139-
fn unix_now() -> u64 {
140-
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64
141-
}
142-
143136
impl Params {
144137
/// Returns the size of the full blocks window that is inspected to calculate the past median time (legacy)
145138
#[inline]
@@ -238,7 +231,7 @@ impl Params {
238231
}
239232
}
240233

241-
fn expected_daa_window_duration_in_milliseconds(&self, selected_parent_daa_score: u64) -> u64 {
234+
pub fn expected_daa_window_duration_in_milliseconds(&self, selected_parent_daa_score: u64) -> u64 {
242235
if self.sampling_activation.is_active(selected_parent_daa_score) {
243236
self.target_time_per_block * self.difficulty_sample_rate * self.sampled_difficulty_window_size as u64
244237
} else {
@@ -264,26 +257,6 @@ impl Params {
264257
min(self.pruning_depth, anticone_finalization_depth)
265258
}
266259

267-
/// Returns whether the sink timestamp is recent enough and the node is considered synced or nearly synced.
268-
pub fn is_nearly_synced(&self, sink_timestamp: u64, sink_daa_score: u64) -> bool {
269-
if self.net.is_mainnet() {
270-
// We consider the node close to being synced if the sink (virtual selected parent) block
271-
// timestamp is within DAA window duration far in the past. Blocks mined over such DAG state would
272-
// enter the DAA window of fully-synced nodes and thus contribute to overall network difficulty
273-
unix_now() < sink_timestamp + self.expected_daa_window_duration_in_milliseconds(sink_daa_score)
274-
} else {
275-
// For testnets we consider the node to be synced if the sink timestamp is within a time range which
276-
// is overwhelmingly unlikely to pass without mined blocks even if net hashrate decreased dramatically.
277-
//
278-
// This period is smaller than the above mainnet calculation in order to ensure that an IBDing miner
279-
// with significant testnet hashrate does not overwhelm the network with deep side-DAGs.
280-
//
281-
// We use DAA duration as baseline and scale it down with BPS (and divide by 3 for mining only when very close to current time on TN11)
282-
let max_expected_duration_without_blocks_in_milliseconds = self.target_time_per_block * NEW_DIFFICULTY_WINDOW_DURATION / 3; // = DAA duration in milliseconds / bps / 3
283-
unix_now() < sink_timestamp + max_expected_duration_without_blocks_in_milliseconds
284-
}
285-
}
286-
287260
pub fn network_name(&self) -> String {
288261
self.net.to_prefixed()
289262
}

protocol/mining/src/rule_engine.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88

99
use kaspa_consensus_core::{
1010
api::counters::ProcessingCounters,
11-
config::Config,
11+
config::{params::NEW_DIFFICULTY_WINDOW_DURATION, Config},
1212
daa_score_timestamp::DaaScoreTimestamp,
1313
network::NetworkType::{Mainnet, Testnet},
1414
};
@@ -133,12 +133,32 @@ impl MiningRuleEngine {
133133
is_nearly_synced || self.use_sync_rate_rule.load(std::sync::atomic::Ordering::Relaxed)
134134
}
135135

136-
/// Returns whether this consensus is considered synced or close to being synced.
136+
/// Returns whether the sink timestamp is recent enough and the node is considered synced or nearly synced.
137137
///
138138
/// This info is used to determine if it's ok to use a block template from this node for mining purposes.
139139
pub fn is_nearly_synced(&self, sink_daa_score_timestamp: DaaScoreTimestamp) -> bool {
140-
// See comment within `config.is_nearly_synced`
141-
self.config.is_nearly_synced(sink_daa_score_timestamp.timestamp, sink_daa_score_timestamp.daa_score)
140+
let sink_timestamp = sink_daa_score_timestamp.timestamp;
141+
142+
if self.config.net.is_mainnet() {
143+
// We consider the node close to being synced if the sink (virtual selected parent) block
144+
// timestamp is within DAA window duration far in the past. Blocks mined over such DAG state would
145+
// enter the DAA window of fully-synced nodes and thus contribute to overall network difficulty
146+
let daa_window_duration = self.config.expected_daa_window_duration_in_milliseconds(sink_daa_score_timestamp.daa_score);
147+
148+
unix_now() < sink_timestamp + daa_window_duration
149+
} else {
150+
// For testnets we consider the node to be synced if the sink timestamp is within a time range which
151+
// is overwhelmingly unlikely to pass without mined blocks even if net hashrate decreased dramatically.
152+
//
153+
// This period is smaller than the above mainnet calculation in order to ensure that an IBDing miner
154+
// with significant testnet hashrate does not overwhelm the network with deep side-DAGs.
155+
//
156+
// We use DAA duration as baseline and scale it down with BPS (and divide by 3 for mining only when very close to current time on TN11)
157+
let max_expected_duration_without_blocks_in_milliseconds =
158+
self.config.target_time_per_block * NEW_DIFFICULTY_WINDOW_DURATION / 3; // = DAA duration in milliseconds / bps / 3
159+
160+
unix_now() < sink_timestamp + max_expected_duration_without_blocks_in_milliseconds
161+
}
142162
}
143163

144164
fn has_sufficient_peer_connectivity(&self) -> bool {

0 commit comments

Comments
 (0)