Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: optimise new tip notify #224

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading