kaspa_p2p_lib/convert/
header.rs1use crate::pb as protowire;
2use kaspa_consensus_core::{header::Header, BlueWorkType};
3use kaspa_hashes::Hash;
4
5use super::error::ConversionError;
6use super::option::TryIntoOptionEx;
7
8impl From<&Header> for protowire::BlockHeader {
13 fn from(item: &Header) -> Self {
14 Self {
15 version: item.version.into(),
16 parents: item.parents_by_level.iter().map(protowire::BlockLevelParents::from).collect(),
17 hash_merkle_root: Some(item.hash_merkle_root.into()),
18 accepted_id_merkle_root: Some(item.accepted_id_merkle_root.into()),
19 utxo_commitment: Some(item.utxo_commitment.into()),
20 timestamp: item.timestamp.try_into().expect("timestamp is always convertible to i64"),
21 bits: item.bits,
22 nonce: item.nonce,
23 daa_score: item.daa_score,
24 blue_work: item.blue_work.to_be_bytes_var(),
26 blue_score: item.blue_score,
27 pruning_point: Some(item.pruning_point.into()),
28 }
29 }
30}
31
32impl From<&Vec<Hash>> for protowire::BlockLevelParents {
33 fn from(item: &Vec<Hash>) -> Self {
34 Self { parent_hashes: item.iter().map(|h| h.into()).collect() }
35 }
36}
37
38impl TryFrom<protowire::BlockHeader> for Header {
43 type Error = ConversionError;
44 fn try_from(item: protowire::BlockHeader) -> Result<Self, Self::Error> {
45 Ok(Self::new_finalized(
46 item.version.try_into()?,
47 item.parents.into_iter().map(Vec::<Hash>::try_from).collect::<Result<Vec<Vec<Hash>>, ConversionError>>()?,
48 item.hash_merkle_root.try_into_ex()?,
49 item.accepted_id_merkle_root.try_into_ex()?,
50 item.utxo_commitment.try_into_ex()?,
51 item.timestamp.try_into()?,
52 item.bits,
53 item.nonce,
54 item.daa_score,
55 BlueWorkType::from_be_bytes_var(&item.blue_work)?,
57 item.blue_score,
58 item.pruning_point.try_into_ex()?,
59 ))
60 }
61}
62
63impl TryFrom<protowire::BlockLevelParents> for Vec<Hash> {
64 type Error = ConversionError;
65 fn try_from(item: protowire::BlockLevelParents) -> Result<Self, Self::Error> {
66 item.parent_hashes.into_iter().map(|x| x.try_into()).collect()
67 }
68}