Skip to content

Commit

Permalink
fix: fix catchup sync problem
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed Nov 22, 2024
1 parent 9173b62 commit a17a1bc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
5 changes: 5 additions & 0 deletions src/server/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,11 @@ where S: ShareChain
info!(target: PEER_INFO_LOGGING_LOG_TARGET, "[DIRECT_PEER_EXCHANGE_TOPIC] New peer info: {} with {} peers", response.peer_id, response.best_peers.len());
match response.peer_id.parse::<PeerId>() {
Ok(peer_id) => {
if response.info.squad != self.config.squad.to_string() {
warn!(target: LOG_TARGET, squad = &self.config.squad; "Peer {} is not in the same squad, skipping", peer_id);
let _ = self.swarm.disconnect_peer_id(peer_id);
return;
}
if self.add_peer(response.info.clone(), peer_id).await {
self.swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id);
}
Expand Down
48 changes: 26 additions & 22 deletions src/sharechain/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use super::{
MAIN_REWARD_SHARE,
MAX_BLOCKS_COUNT,
MIN_RANDOMX_SCALING_FACTOR,
MIN_SHA3X_SCALING_FACTOR,
MIN_RANDOMX_DIFFICULTY,
MIN_SHA3X_DIFFICULTY,
SHARE_WINDOW,
UNCLE_REWARD_SHARE,
};
use crate::{
main,
server::{http::stats_collector::StatsBroadcastClient, PROTOCOL_VERSION},
sharechain::{
error::{ShareChainError, ValidationError},
Expand Down Expand Up @@ -316,7 +317,7 @@ impl InMemoryShareChain {
main_chain_only: bool,
) -> Result<Vec<Arc<P2Block>>, ShareChainError> {
let mut res = Vec::with_capacity(page_size);
let mut num_main_chain_blocks = 0;
let mut num_actual_blocks = 0;
let mut level = if let Some(level) = p2_chain.level_at_height(start_height.unwrap_or(0)) {
level
} else {
Expand All @@ -328,27 +329,38 @@ impl InMemoryShareChain {
}
};

// A -> B [unc B1]
// B -> C
// B1 -> C [unc C1]
// C1 -> D
// C -> D

loop {
for block in level.blocks.values() {
if block.hash == level.chain_block {
num_main_chain_blocks += 1;
if main_chain_only {
if main_chain_only {
if block.hash == level.chain_block {
for uncle in &block.uncles {
// Always include all the uncles, if we have them
if let Some(uncle_block) = level.blocks.get(&uncle.1) {
if let Some(uncle_block) =
p2_chain.level_at_height(uncle.0).and_then(|l| l.blocks.get(&uncle.1))
{
// Uncles should never exist in the main chain, so we don't need to worry about
// duplicates
res.push(uncle_block.clone());
}
}

num_actual_blocks += 1;
res.push(block.clone());
}
} else {
num_actual_blocks += 1;
res.push(block.clone());
}

res.push(block.clone());
// Always include at least 2 main chain blocks so that if we called
// this function with the starting mainchain block we can continue asking for more
// blocks
if res.len() >= page_size && (!main_chain_only || num_main_chain_blocks >= 2) {
if num_actual_blocks > page_size {
return Ok(res);
}
}
Expand Down Expand Up @@ -725,18 +737,10 @@ impl ShareChain for InMemoryShareChain {
}

async fn get_target_difficulty(&self, height: u64) -> Difficulty {
let mut min = self
.consensus_manager
.consensus_constants(height)
.min_pow_difficulty(self.pow_algo);
match self.pow_algo {
PowAlgorithm::RandomX => {
min = Difficulty::from_u64(min.as_u64() / MIN_RANDOMX_SCALING_FACTOR).unwrap();
},
PowAlgorithm::Sha3x => {
min = Difficulty::from_u64(min.as_u64() / MIN_SHA3X_SCALING_FACTOR).unwrap();
},
}
let min = match self.pow_algo {
PowAlgorithm::RandomX => Difficulty::from_u64(MIN_RANDOMX_DIFFICULTY).unwrap(),
PowAlgorithm::Sha3x => Difficulty::from_u64(MIN_SHA3X_DIFFICULTY).unwrap(),
};
let max = self
.consensus_manager
.consensus_constants(height)
Expand Down
4 changes: 2 additions & 2 deletions src/sharechain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub const DIFFICULTY_ADJUSTMENT_WINDOW: usize = 90;

pub const BLOCK_TARGET_TIME: u64 = 10;

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

pub mod error;
pub mod in_memory;
Expand Down

0 comments on commit a17a1bc

Please sign in to comment.