-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(models::peer): Move PeerBlockNotification to separate file
Also add a test that the hash in this notification matches the block hash hwen converted from a block.
- Loading branch information
1 parent
3ab413c
commit 44260b5
Showing
3 changed files
with
49 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters