Skip to content

Commit

Permalink
optimise new tip notify
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Dec 11, 2024
1 parent 4b96f8b commit 89f5379
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{
fs::File,
io::Write,
panic,
process,
time::{SystemTime, UNIX_EPOCH},
};

Expand Down
3 changes: 1 addition & 2 deletions src/server/grpc/p2pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ where S: ShareChain
.collect();
new_blocks.append(&mut uncles);
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 notify = NotifyNewTipBlock::new(self.local_peer_id, new_blocks);
let res = self
.p2p_client
.broadcast_block(notify)
Expand Down
14 changes: 9 additions & 5 deletions src/server/p2p/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ pub struct NotifyNewTipBlock {
pub version: u64,
peer_id: PeerId,
pub new_blocks: Vec<P2Block>,
pub total_accumulated_difficulty: u128,
pub timestamp: u64,
}

Expand All @@ -238,7 +237,6 @@ impl Display for NotifyNewTipBlock {
writeln!(f, "--------- new tip notify Block -----------------")?;
writeln!(f, "version: {}", self.version)?;
writeln!(f, "from: {}", self.peer_id.to_base58())?;
writeln!(f, "total_accumulated_difficulty: {}", self.total_accumulated_difficulty)?;
writeln!(f, "timestamp: {}", self.timestamp)?;
writeln!(f, "--------- p2pblocks -----------------")?;
for block in &self.new_blocks {
Expand All @@ -251,14 +249,20 @@ impl Display for NotifyNewTipBlock {
impl_conversions!(NotifyNewTipBlock);

impl NotifyNewTipBlock {
pub fn new(peer_id: PeerId, new_blocks: Vec<P2Block>, total_acculumted_difficulty: AccumulatedDifficulty) -> Self {
let total_acculumted_difficulty = total_acculumted_difficulty.as_u128();
pub fn total_proof_of_work(&self) -> AccumulatedDifficulty {
let max_block = self.new_blocks.iter().max_by_key(|x| x.height);
match max_block {
Some(block) => block.total_pow(),
None => AccumulatedDifficulty::min(),
}
}

pub fn new(peer_id: PeerId, new_blocks: Vec<P2Block>) -> Self {
let timestamp = EpochTime::now().as_u64();
Self {
version: PROTOCOL_VERSION,
peer_id,
new_blocks,
total_accumulated_difficulty: total_acculumted_difficulty,
timestamp,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ where S: ShareChain

let our_tip = share_chain.tip_height().await.unwrap_or(0);
let our_pow = share_chain.get_total_chain_pow().await;
if payload.total_accumulated_difficulty < our_pow.as_u128() &&
if payload.total_proof_of_work() < our_pow &&
payload
.new_blocks
.iter()
Expand Down

0 comments on commit 89f5379

Please sign in to comment.