Skip to content

Commit

Permalink
feat!: refactor difficulty handling (#186)
Browse files Browse the repository at this point in the history
Description
---
Completely refactor how we handle the backend and how we handle
difficulty.
  • Loading branch information
SWvheerden authored Nov 27, 2024
1 parent 301b47c commit d642d6c
Show file tree
Hide file tree
Showing 9 changed files with 946 additions and 792 deletions.
2 changes: 1 addition & 1 deletion src/server/grpc/p2pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ where S: ShareChain
.map(|block| Arc::<P2Block>::unwrap_or_clone(block))
.collect();
new_blocks.append(&mut uncles);
if new_tip {
if new_tip.new_tip.is_some() {
let total_pow = share_chain.get_total_chain_pow().await;
let notify = NotifyNewTipBlock::new(self.local_peer_id, new_blocks, total_pow);
let res = self
Expand Down
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ pub mod grpc;
pub mod http;
pub mod p2p;

pub const PROTOCOL_VERSION: u64 = 22;
pub const PROTOCOL_VERSION: u64 = 23;
114 changes: 50 additions & 64 deletions src/server/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: BSD-3-Clause

use std::{
collections::{HashMap, HashSet},
collections::HashMap,
fmt::Display,
fs,
hash::Hash,
Expand Down Expand Up @@ -96,6 +96,7 @@ use crate::{
},
sharechain::{
p2block::{P2Block, CURRENT_CHAIN_ID},
p2chain::ChainAddResult,
ShareChain,
},
};
Expand Down Expand Up @@ -442,7 +443,6 @@ where S: ShareChain

/// Broadcasting a new mined [`Block`] to the network (assume it is already validated with the network).
async fn broadcast_block(&mut self, result: Result<NotifyNewTipBlock, RecvError>) {
dbg!("Broadcast block");
// if self.sync_in_progress.load(Ordering::SeqCst) {
// return;
// }
Expand Down Expand Up @@ -668,32 +668,30 @@ where S: ShareChain
let blocks: Vec<_> = blocks.into_iter().map(Arc::new).collect();
match share_chain.add_synced_blocks(&blocks).await {
Ok(new_tip) => {
info!(target: LOG_TARGET, squad = &self.config.squad; "[{:?}]New tip notify blocks added to share chain, new tip added [{}]", algo, new_tip);
},
Err(crate::sharechain::error::ShareChainError::BlockParentDoesNotExist {
missing_parents,
}) => {
let num_missing_parents = missing_parents.len();
if num_missing_parents > 5 {
info!(target: LOG_TARGET, squad = &self.config.squad; "We are missing more than 5 blocks, we are missing: {}", num_missing_parents);
return Ok(MessageAcceptance::Accept);
}

if our_tip > max_payload_height.saturating_sub(10) ||
our_tip < max_payload_height.saturating_add(5)
{
info!(target: LOG_TARGET, squad = &self.config.squad; "Our tip({}) is too far off their new block({}) waiting for sync", our_tip, max_payload_height);
return Ok(MessageAcceptance::Accept);
info!(target: LOG_TARGET, squad = &self.config.squad; "[{:?}]New tip notify blocks added to share chain: {}", algo, new_tip);
let missing_parents = new_tip.to_missing_parents_vec();
if !missing_parents.is_empty() {
if missing_parents.len() > 5 {
info!(target: LOG_TARGET, squad = &self.config.squad; "We are missing more than 5 blocks, we are missing: {}", missing_parents.len());
return Ok(MessageAcceptance::Accept);
}

if our_tip > max_payload_height.saturating_sub(10) ||
our_tip < max_payload_height.saturating_add(5)
{
info!(target: LOG_TARGET, squad = &self.config.squad; "Our tip({}) is too far off their new block({}) waiting for sync", our_tip, max_payload_height);
return Ok(MessageAcceptance::Accept);
}
info!(target: LOG_TARGET, squad = &self.config.squad; "We are missing less than 5 blocks, sending sync request with missing blocks to {}", propagation_source);
let sync_share_chain = SyncShareChain {
algo,
peer: propagation_source,
missing_parents,
is_from_new_block_notify: true,
};

let _ = self.inner_request_tx.send(InnerRequest::DoSyncChain(sync_share_chain));
}
info!(target: LOG_TARGET, squad = &self.config.squad; "We are missing less than 5 blocks, sending sync request with missing blocks to {}", propagation_source);
let sync_share_chain = SyncShareChain {
algo,
peer: propagation_source,
missing_parents,
is_from_new_block_notify: true,
};

let _ = self.inner_request_tx.send(InnerRequest::DoSyncChain(sync_share_chain));
},
Err(error) => {
error!(target: LOG_TARGET, squad = &self.config.squad; "Failed to add synced blocks to share chain: {error:?}");
Expand Down Expand Up @@ -982,19 +980,18 @@ where S: ShareChain
tokio::spawn(async move {
match share_chain.add_synced_blocks(&blocks).await {
Ok(new_tip) => {
info!(target: LOG_TARGET, squad; "[{:?}] Synced blocks added to share chain, new tip added [{}]",algo, new_tip);
info!(target: LOG_TARGET, squad; "[{:?}] blocks for the following heights where added : {:?}", algo, blocks.iter().map(|block| block.height.to_string()).collect::<Vec<String>>());
},
Err(crate::sharechain::error::ShareChainError::BlockParentDoesNotExist { missing_parents }) => {
info!(target: LOG_TARGET, squad; "[{:?}] missing parents {:?}", algo, missing_parents.iter().map(|(height, hash)| format!("{}({:x}{:x}{:x}{:x})",height.to_string(), hash[0], hash[1], hash[2], hash[3])).collect::<Vec<String>>());
let sync_share_chain = SyncShareChain {
algo,
peer,
missing_parents,
is_from_new_block_notify: false,
};
info!(target: LOG_TARGET, squad; "[{:?}] Synced blocks added to share chain: {}",algo, new_tip);
let missing_parents = new_tip.to_missing_parents_vec();
if !missing_parents.is_empty() {
let sync_share_chain = SyncShareChain {
algo,
peer,
missing_parents,
is_from_new_block_notify: false,
};

let _ = tx.send(InnerRequest::DoSyncChain(sync_share_chain));
let _ = tx.send(InnerRequest::DoSyncChain(sync_share_chain));
}
},
Err(error) => {
error!(target: LOG_TARGET, squad; "Failed to add synced blocks to share chain: {error:?}");
Expand Down Expand Up @@ -1469,41 +1466,32 @@ where S: ShareChain
tokio::spawn(async move {
blocks.sort_by(|a, b| a.height.cmp(&b.height));
let last_block_from_them = blocks.last().map(|b| (b.height, b.hash));
let mut missing_blocks = HashSet::new();
let mut new_tip = false;
let mut new_tip = ChainAddResult::default();
let mut blocks_added = Vec::new();
for b in &blocks {
match share_chain.add_synced_blocks(&[b.clone()]).await {
Ok(result) => {
blocks_added.push(format!("{}({})", b.height, &b.hash.to_hex()[0..8]));
if result {
new_tip = result;
}
new_tip.combine(result);
},
Err(error) => match error {
crate::sharechain::error::ShareChainError::BlockParentDoesNotExist { missing_parents } => {
for (height, hash) in missing_parents {
missing_blocks.insert((height, hash));
}
},
_ => {
error!(target: SYNC_REQUEST_LOG_TARGET, squad; "Failed to add Catchup synced blocks to share chain: {error:?}");
network_peer_store
.write()
.await
.move_to_grey_list(peer, format!("Block failed validation: {error}"));
return;
},
Err(error) => {
error!(target: SYNC_REQUEST_LOG_TARGET, squad; "Failed to add Catchup synced blocks to share chain: {error:?}");
network_peer_store
.write()
.await
.move_to_grey_list(peer, format!("Block failed validation: {error}"));
return;
},
}
}
info!(target: LOG_TARGET, "[{:?}][new tip: {}] Blocks added {:?}", new_tip, algo, blocks_added);
if missing_blocks.len() > 0 {
warn!(target: SYNC_REQUEST_LOG_TARGET, squad; "Catchup sync Reporting missing blocks({}): {:?}", missing_blocks.len(), missing_blocks.iter().map(|(height, hash)| format!("{}({:x}{:x}{:x}{:x})",height.to_string(), hash[0], hash[1], hash[2], hash[3])).collect::<Vec<String>>());
info!(target: LOG_TARGET, "[{:?}] Blocks via catchup sync added {:?}", algo, blocks_added);
info!(target: LOG_TARGET, "[{:?}] Blocks via catchup sync result {}", algo, new_tip);
let missing_parents = new_tip.to_missing_parents_vec();
if missing_parents.len() > 0 {
let sync_share_chain = SyncShareChain {
algo,
peer,
missing_parents: missing_blocks.into_iter().collect(),
missing_parents,
is_from_new_block_notify: false,
};
let _ = tx.send(InnerRequest::DoSyncChain(sync_share_chain));
Expand Down Expand Up @@ -1713,8 +1701,6 @@ where S: ShareChain
}

async fn attempt_relay_reservation(&mut self) {
dbg!("Attempt relay reservation");

// Can happen that a previous lock already set the relaty
if self.swarm.external_addresses().count() > 0 {
warn!(target: LOG_TARGET, "No need to relay, we have an external address or relay already");
Expand Down
6 changes: 4 additions & 2 deletions src/sharechain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ pub enum ShareChainError {
BlockLevelNotFound,
#[error("Validation error: {0}")]
ValidationError(#[from] ValidationError),
#[error("Missing parents")]
BlockParentDoesNotExist { missing_parents: Vec<(u64, FixedHash)> },
#[error("Missing block validation params!")]
MissingBlockValidationParams,
#[error("Uncle block was in main chain. Height: {height}, Hash: {hash}")]
UncleInMainChain { height: u64, hash: FixedHash },
#[error("Uncle block does not link back to main chain")]
UncleParentNotInMainChain,
#[error("Block does not have correct total work accumulated")]
BlockTotalWorkMismatch,
}

#[derive(Error, Debug)]
Expand Down
Loading

0 comments on commit d642d6c

Please sign in to comment.