kaspa_p2p_lib/convert/
messages.rs

1use super::{
2    error::ConversionError,
3    model::{
4        trusted::{TrustedDataEntry, TrustedDataPackage},
5        version::Version,
6    },
7    option::TryIntoOptionEx,
8};
9use crate::pb as protowire;
10use kaspa_consensus_core::{
11    header::Header,
12    pruning::{PruningPointProof, PruningPointsList},
13    tx::{TransactionId, TransactionOutpoint, UtxoEntry},
14};
15use kaspa_hashes::Hash;
16use kaspa_utils::networking::{IpAddress, PeerId};
17
18use std::sync::Arc;
19
20// ----------------------------------------------------------------------------
21// consensus_core to protowire
22// ----------------------------------------------------------------------------
23
24impl From<Version> for protowire::VersionMessage {
25    fn from(item: Version) -> Self {
26        Self {
27            protocol_version: item.protocol_version,
28            services: item.services,
29            timestamp: item.timestamp as i64,
30            address: item.address.map(|x| x.into()),
31            id: item.id.as_bytes().to_vec(),
32            user_agent: item.user_agent,
33            disable_relay_tx: item.disable_relay_tx,
34            subnetwork_id: item.subnetwork_id.map(|x| x.into()),
35            network: item.network.clone(),
36        }
37    }
38}
39
40// ----------------------------------------------------------------------------
41// protowire to consensus_core
42// ----------------------------------------------------------------------------
43
44impl TryFrom<protowire::VersionMessage> for Version {
45    type Error = ConversionError;
46    fn try_from(msg: protowire::VersionMessage) -> Result<Self, Self::Error> {
47        Ok(Self {
48            protocol_version: msg.protocol_version,
49            services: msg.services,
50            timestamp: msg.timestamp as u64,
51            address: if msg.address.is_none() { None } else { Some(msg.address.unwrap().try_into()?) },
52            id: PeerId::from_slice(&msg.id)?,
53            user_agent: msg.user_agent.clone(),
54            disable_relay_tx: msg.disable_relay_tx,
55            subnetwork_id: if msg.subnetwork_id.is_none() { None } else { Some(msg.subnetwork_id.unwrap().try_into()?) },
56            network: msg.network.clone(),
57        })
58    }
59}
60
61impl TryFrom<protowire::RequestHeadersMessage> for (Hash, Hash) {
62    type Error = ConversionError;
63    fn try_from(msg: protowire::RequestHeadersMessage) -> Result<Self, Self::Error> {
64        Ok((msg.high_hash.try_into_ex()?, msg.low_hash.try_into_ex()?))
65    }
66}
67
68impl TryFrom<protowire::RequestIbdChainBlockLocatorMessage> for (Option<Hash>, Option<Hash>) {
69    type Error = ConversionError;
70    fn try_from(msg: protowire::RequestIbdChainBlockLocatorMessage) -> Result<Self, Self::Error> {
71        let low = match msg.low_hash {
72            Some(low) => Some(low.try_into()?),
73            None => None,
74        };
75
76        let high = match msg.high_hash {
77            Some(high) => Some(high.try_into()?),
78            None => None,
79        };
80
81        Ok((low, high))
82    }
83}
84
85impl TryFrom<protowire::PruningPointProofMessage> for PruningPointProof {
86    type Error = ConversionError;
87    fn try_from(msg: protowire::PruningPointProofMessage) -> Result<Self, Self::Error> {
88        msg.headers.into_iter().map(|v| v.try_into()).collect()
89    }
90}
91
92impl TryFrom<protowire::PruningPointsMessage> for PruningPointsList {
93    type Error = ConversionError;
94    fn try_from(msg: protowire::PruningPointsMessage) -> Result<Self, Self::Error> {
95        msg.headers.into_iter().map(|x| x.try_into().map(Arc::new)).collect()
96    }
97}
98
99impl TryFrom<protowire::TrustedDataMessage> for TrustedDataPackage {
100    type Error = ConversionError;
101    fn try_from(msg: protowire::TrustedDataMessage) -> Result<Self, Self::Error> {
102        Ok(Self::new(
103            msg.daa_window.into_iter().map(|x| x.try_into()).collect::<Result<Vec<_>, Self::Error>>()?,
104            msg.ghostdag_data.into_iter().map(|x| x.try_into()).collect::<Result<Vec<_>, Self::Error>>()?,
105        ))
106    }
107}
108
109impl TryFrom<protowire::BlockWithTrustedDataV4Message> for TrustedDataEntry {
110    type Error = ConversionError;
111    fn try_from(msg: protowire::BlockWithTrustedDataV4Message) -> Result<Self, Self::Error> {
112        Ok(Self::new(msg.block.try_into_ex()?, msg.daa_window_indices, msg.ghostdag_data_indices))
113    }
114}
115
116impl TryFrom<protowire::IbdChainBlockLocatorMessage> for Vec<Hash> {
117    type Error = ConversionError;
118    fn try_from(msg: protowire::IbdChainBlockLocatorMessage) -> Result<Self, Self::Error> {
119        msg.block_locator_hashes.into_iter().map(|v| v.try_into()).collect()
120    }
121}
122
123impl TryFrom<protowire::BlockHeadersMessage> for Vec<Arc<Header>> {
124    type Error = ConversionError;
125    fn try_from(msg: protowire::BlockHeadersMessage) -> Result<Self, Self::Error> {
126        msg.block_headers.into_iter().map(|v| v.try_into().map(Arc::new)).collect()
127    }
128}
129
130impl TryFrom<protowire::PruningPointUtxoSetChunkMessage> for Vec<(TransactionOutpoint, UtxoEntry)> {
131    type Error = ConversionError;
132
133    fn try_from(msg: protowire::PruningPointUtxoSetChunkMessage) -> Result<Self, Self::Error> {
134        msg.outpoint_and_utxo_entry_pairs.into_iter().map(|p| p.try_into()).collect()
135    }
136}
137
138impl TryFrom<protowire::RequestPruningPointUtxoSetMessage> for Hash {
139    type Error = ConversionError;
140
141    fn try_from(msg: protowire::RequestPruningPointUtxoSetMessage) -> Result<Self, Self::Error> {
142        msg.pruning_point_hash.try_into_ex()
143    }
144}
145
146impl TryFrom<protowire::InvRelayBlockMessage> for Hash {
147    type Error = ConversionError;
148
149    fn try_from(msg: protowire::InvRelayBlockMessage) -> Result<Self, Self::Error> {
150        msg.hash.try_into_ex()
151    }
152}
153
154impl TryFrom<protowire::RequestRelayBlocksMessage> for Vec<Hash> {
155    type Error = ConversionError;
156
157    fn try_from(msg: protowire::RequestRelayBlocksMessage) -> Result<Self, Self::Error> {
158        msg.hashes.into_iter().map(|v| v.try_into()).collect()
159    }
160}
161
162impl TryFrom<protowire::RequestIbdBlocksMessage> for Vec<Hash> {
163    type Error = ConversionError;
164
165    fn try_from(msg: protowire::RequestIbdBlocksMessage) -> Result<Self, Self::Error> {
166        msg.hashes.into_iter().map(|v| v.try_into()).collect()
167    }
168}
169
170impl TryFrom<protowire::BlockLocatorMessage> for Vec<Hash> {
171    type Error = ConversionError;
172
173    fn try_from(msg: protowire::BlockLocatorMessage) -> Result<Self, Self::Error> {
174        msg.hashes.into_iter().map(|v| v.try_into()).collect()
175    }
176}
177
178impl TryFrom<protowire::AddressesMessage> for Vec<(IpAddress, u16)> {
179    type Error = ConversionError;
180
181    fn try_from(msg: protowire::AddressesMessage) -> Result<Self, Self::Error> {
182        msg.address_list.into_iter().map(|addr| addr.try_into()).collect::<Result<_, _>>()
183    }
184}
185
186impl TryFrom<protowire::RequestTransactionsMessage> for Vec<TransactionId> {
187    type Error = ConversionError;
188
189    fn try_from(msg: protowire::RequestTransactionsMessage) -> Result<Self, Self::Error> {
190        msg.ids.into_iter().map(|v| v.try_into()).collect()
191    }
192}
193
194impl TryFrom<protowire::InvTransactionsMessage> for Vec<TransactionId> {
195    type Error = ConversionError;
196
197    fn try_from(msg: protowire::InvTransactionsMessage) -> Result<Self, Self::Error> {
198        msg.ids.into_iter().map(|v| v.try_into()).collect()
199    }
200}
201
202impl TryFrom<protowire::TransactionNotFoundMessage> for TransactionId {
203    type Error = ConversionError;
204
205    fn try_from(msg: protowire::TransactionNotFoundMessage) -> Result<Self, Self::Error> {
206        msg.id.try_into_ex()
207    }
208}
209
210impl TryFrom<protowire::RequestBlockLocatorMessage> for (Hash, u32) {
211    type Error = ConversionError;
212    fn try_from(msg: protowire::RequestBlockLocatorMessage) -> Result<Self, Self::Error> {
213        Ok((msg.high_hash.try_into_ex()?, msg.limit))
214    }
215}
216
217impl TryFrom<protowire::RequestAntipastMessage> for (Hash, Hash) {
218    type Error = ConversionError;
219    fn try_from(msg: protowire::RequestAntipastMessage) -> Result<Self, Self::Error> {
220        Ok((msg.block_hash.try_into_ex()?, msg.context_hash.try_into_ex()?))
221    }
222}