Skip to content

Commit

Permalink
refactor(models::peer): Move PeerBlockNotification to separate file
Browse files Browse the repository at this point in the history
Also add a test that the hash in this notification matches the block
hash hwen converted from a block.
  • Loading branch information
Sword-Smith committed Jan 16, 2025
1 parent 3ab413c commit 44260b5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 46 deletions.
42 changes: 2 additions & 40 deletions src/models/peer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod peer_block_notifications;
pub mod transaction_notification;
pub mod transfer_block;
pub mod transfer_transaction;
Expand All @@ -6,6 +7,7 @@ use std::fmt::Display;
use std::net::SocketAddr;
use std::time::SystemTime;

use peer_block_notifications::PeerBlockNotification;
use serde::Deserialize;
use serde::Serialize;
use tracing::trace;
Expand All @@ -17,7 +19,6 @@ use super::blockchain::block::block_header::BlockHeader;
use super::blockchain::block::block_height::BlockHeight;
use super::blockchain::block::difficulty_control::ProofOfWork;
use super::blockchain::block::Block;
use super::blockchain::shared::Hash;
use super::channel::BlockProposalNotification;
use super::state::transaction_kernel_id::TransactionKernelId;
use crate::config_models::network::Network;
Expand Down Expand Up @@ -405,45 +406,6 @@ pub struct HandshakeData {
pub is_archival_node: bool,
}

/// Used to tell peers that a new block has been found without having to
/// send the entire block
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct PeerBlockNotification {
pub hash: Digest,
pub height: BlockHeight,
pub(crate) cumulative_proof_of_work: ProofOfWork,
}

impl From<&Block> for PeerBlockNotification {
fn from(block: &Block) -> Self {
PeerBlockNotification {
hash: block.hash(),
height: block.kernel.header.height,
cumulative_proof_of_work: block.kernel.header.cumulative_proof_of_work,
}
}
}

impl From<Block> for PeerBlockNotification {
fn from(block: Block) -> Self {
PeerBlockNotification {
hash: block.hash(),
height: block.kernel.header.height,
cumulative_proof_of_work: block.kernel.header.cumulative_proof_of_work,
}
}
}

impl From<&BlockHeader> for PeerBlockNotification {
fn from(value: &BlockHeader) -> Self {
PeerBlockNotification {
hash: Hash::hash(value),
height: value.height,
cumulative_proof_of_work: value.cumulative_proof_of_work,
}
}
}

/// A message sent between peers to inform them whether the connection was
/// accepted or refused (and if so, for what reason).
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
Expand Down
44 changes: 44 additions & 0 deletions src/models/peer/peer_block_notifications.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use serde::Deserialize;
use serde::Serialize;
use tasm_lib::prelude::Digest;

use crate::models::blockchain::block::block_height::BlockHeight;
use crate::models::blockchain::block::difficulty_control::ProofOfWork;
use crate::models::blockchain::block::Block;

/// Used to tell peers that a new block has been found without having to
/// send the entire block
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct PeerBlockNotification {
pub hash: Digest,
pub height: BlockHeight,
pub(crate) cumulative_proof_of_work: ProofOfWork,
}

impl From<&Block> for PeerBlockNotification {
fn from(block: &Block) -> Self {
PeerBlockNotification {
hash: block.hash(),
height: block.kernel.header.height,
cumulative_proof_of_work: block.kernel.header.cumulative_proof_of_work,
}
}
}

#[cfg(test)]
mod tests {
use super::PeerBlockNotification;
use crate::models::blockchain::block::validity::block_primitive_witness::test::deterministic_block_primitive_witness;

#[test]
fn block_notification_hash_matches_block_hash() {
let witness = deterministic_block_primitive_witness();
let a_block = witness.predecessor_block();
let as_notification: PeerBlockNotification = a_block.into();
assert_eq!(
a_block.hash(),
as_notification.hash,
"Block notification hash must match block hash"
);
}
}
9 changes: 3 additions & 6 deletions src/peer_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,14 +773,11 @@ impl PeerLoopHandler {
debug!("Got BlockNotificationRequest");

peer.send(PeerMessage::BlockNotification(
(&self
.global_state_lock
self.global_state_lock
.lock_guard()
.await
.chain
.light_state()
.kernel
.header)
.into(),
))
.await?;
Expand Down Expand Up @@ -1209,7 +1206,7 @@ impl PeerLoopHandler {
if new_block_height > peer_state_info.highest_shared_block_height {
debug!("Sending PeerMessage::BlockNotification");
peer_state_info.highest_shared_block_height = new_block_height;
peer.send(PeerMessage::BlockNotification((*block).into()))
peer.send(PeerMessage::BlockNotification(block.as_ref().into()))
.await?;
debug!("Sent PeerMessage::BlockNotification");
}
Expand Down Expand Up @@ -2591,7 +2588,7 @@ mod peer_loop_tests {
Action::Write(PeerMessage::BlockRequestByHash(block_2.hash())),
//
// Now make the interruption of the block reconciliation process
Action::Read(PeerMessage::BlockNotification(block_5.clone().into())),
Action::Read(PeerMessage::BlockNotification((&block_5).into())),
//
// Complete the block reconciliation process by requesting the last block
// in this process, to get back to a mutually known block.
Expand Down

0 comments on commit 44260b5

Please sign in to comment.