Skip to content

Commit 2b319ee

Browse files
fix: fix catchup sync problem (#177)
Co-authored-by: SW van Heerden <[email protected]>
1 parent 0121697 commit 2b319ee

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

src/server/p2p/network.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,11 @@ where S: ShareChain
853853
info!(target: PEER_INFO_LOGGING_LOG_TARGET, "[DIRECT_PEER_EXCHANGE_TOPIC] New peer info: {} with {} peers", response.peer_id, response.best_peers.len());
854854
match response.peer_id.parse::<PeerId>() {
855855
Ok(peer_id) => {
856+
if response.info.squad != self.config.squad.to_string() {
857+
warn!(target: LOG_TARGET, squad = &self.config.squad; "Peer {} is not in the same squad, skipping", peer_id);
858+
let _ = self.swarm.disconnect_peer_id(peer_id);
859+
return;
860+
}
856861
if self.add_peer(response.info.clone(), peer_id).await {
857862
self.swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id);
858863
}

src/sharechain/in_memory.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
2424
use super::{
2525
MAIN_REWARD_SHARE,
2626
MAX_BLOCKS_COUNT,
27-
MIN_RANDOMX_SCALING_FACTOR,
28-
MIN_SHA3X_SCALING_FACTOR,
27+
MIN_RANDOMX_DIFFICULTY,
28+
MIN_SHA3X_DIFFICULTY,
2929
SHARE_WINDOW,
3030
UNCLE_REWARD_SHARE,
3131
};
3232
use crate::{
33+
main,
3334
server::{http::stats_collector::StatsBroadcastClient, PROTOCOL_VERSION},
3435
sharechain::{
3536
error::{ShareChainError, ValidationError},
@@ -316,7 +317,7 @@ impl InMemoryShareChain {
316317
main_chain_only: bool,
317318
) -> Result<Vec<Arc<P2Block>>, ShareChainError> {
318319
let mut res = Vec::with_capacity(page_size);
319-
let mut num_main_chain_blocks = 0;
320+
let mut num_actual_blocks = 0;
320321
let mut level = if let Some(level) = p2_chain.level_at_height(start_height.unwrap_or(0)) {
321322
level
322323
} else {
@@ -328,27 +329,33 @@ impl InMemoryShareChain {
328329
}
329330
};
330331

332+
331333
loop {
332334
for block in level.blocks.values() {
333-
if block.hash == level.chain_block {
334-
num_main_chain_blocks += 1;
335-
if main_chain_only {
335+
if main_chain_only {
336+
if block.hash == level.chain_block {
336337
for uncle in &block.uncles {
337338
// Always include all the uncles, if we have them
338-
if let Some(uncle_block) = level.blocks.get(&uncle.1) {
339+
if let Some(uncle_block) =
340+
p2_chain.level_at_height(uncle.0).and_then(|l| l.blocks.get(&uncle.1))
341+
{
339342
// Uncles should never exist in the main chain, so we don't need to worry about
340343
// duplicates
341344
res.push(uncle_block.clone());
342345
}
343346
}
347+
348+
num_actual_blocks += 1;
349+
res.push(block.clone());
344350
}
351+
} else {
352+
num_actual_blocks += 1;
353+
res.push(block.clone());
345354
}
346-
347-
res.push(block.clone());
348355
// Always include at least 2 main chain blocks so that if we called
349356
// this function with the starting mainchain block we can continue asking for more
350357
// blocks
351-
if res.len() >= page_size && (!main_chain_only || num_main_chain_blocks >= 2) {
358+
if num_actual_blocks > page_size {
352359
return Ok(res);
353360
}
354361
}
@@ -725,18 +732,10 @@ impl ShareChain for InMemoryShareChain {
725732
}
726733

727734
async fn get_target_difficulty(&self, height: u64) -> Difficulty {
728-
let mut min = self
729-
.consensus_manager
730-
.consensus_constants(height)
731-
.min_pow_difficulty(self.pow_algo);
732-
match self.pow_algo {
733-
PowAlgorithm::RandomX => {
734-
min = Difficulty::from_u64(min.as_u64() / MIN_RANDOMX_SCALING_FACTOR).unwrap();
735-
},
736-
PowAlgorithm::Sha3x => {
737-
min = Difficulty::from_u64(min.as_u64() / MIN_SHA3X_SCALING_FACTOR).unwrap();
738-
},
739-
}
735+
let min = match self.pow_algo {
736+
PowAlgorithm::RandomX => Difficulty::from_u64(MIN_RANDOMX_DIFFICULTY).unwrap(),
737+
PowAlgorithm::Sha3x => Difficulty::from_u64(MIN_SHA3X_DIFFICULTY).unwrap(),
738+
};
740739
let max = self
741740
.consensus_manager
742741
.consensus_constants(height)

src/sharechain/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ pub const DIFFICULTY_ADJUSTMENT_WINDOW: usize = 90;
5050

5151
pub const BLOCK_TARGET_TIME: u64 = 10;
5252

53-
pub const MIN_RANDOMX_SCALING_FACTOR: u64 = 4;
54-
pub const MIN_SHA3X_SCALING_FACTOR: u64 = 1;
53+
pub const MIN_RANDOMX_DIFFICULTY: u64 = 1_000; // 1 Khs every ten seconds
54+
pub const MIN_SHA3X_DIFFICULTY: u64 = 10_000; // 1 Mhs every ten seconds
5555

5656
pub mod error;
5757
pub mod in_memory;

0 commit comments

Comments
 (0)