cuprate_p2p_core/protocol/
try_from.rs

1//! This module contains the implementations of [`TryFrom`] and [`From`] to convert between
2//! [`Message`], [`PeerRequest`] and [`PeerResponse`].
3
4use cuprate_wire::{Message, ProtocolMessage};
5
6use crate::{BroadcastMessage, PeerRequest, PeerResponse, ProtocolRequest, ProtocolResponse};
7
8#[derive(Debug)]
9pub struct MessageConversionError;
10
11impl From<ProtocolRequest> for ProtocolMessage {
12    fn from(value: ProtocolRequest) -> Self {
13        match value {
14            ProtocolRequest::GetObjects(val) => Self::GetObjectsRequest(val),
15            ProtocolRequest::GetChain(val) => Self::ChainRequest(val),
16            ProtocolRequest::FluffyMissingTxs(val) => Self::FluffyMissingTransactionsRequest(val),
17            ProtocolRequest::GetTxPoolCompliment(val) => Self::GetTxPoolCompliment(val),
18            ProtocolRequest::NewBlock(val) => Self::NewBlock(val),
19            ProtocolRequest::NewFluffyBlock(val) => Self::NewFluffyBlock(val),
20            ProtocolRequest::NewTransactions(val) => Self::NewTransactions(val),
21        }
22    }
23}
24
25impl TryFrom<ProtocolMessage> for ProtocolRequest {
26    type Error = MessageConversionError;
27
28    fn try_from(value: ProtocolMessage) -> Result<Self, Self::Error> {
29        Ok(match value {
30            ProtocolMessage::GetObjectsRequest(val) => Self::GetObjects(val),
31            ProtocolMessage::ChainRequest(val) => Self::GetChain(val),
32            ProtocolMessage::FluffyMissingTransactionsRequest(val) => Self::FluffyMissingTxs(val),
33            ProtocolMessage::GetTxPoolCompliment(val) => Self::GetTxPoolCompliment(val),
34            ProtocolMessage::NewBlock(val) => Self::NewBlock(val),
35            ProtocolMessage::NewFluffyBlock(val) => Self::NewFluffyBlock(val),
36            ProtocolMessage::NewTransactions(val) => Self::NewTransactions(val),
37            ProtocolMessage::GetObjectsResponse(_) | ProtocolMessage::ChainEntryResponse(_) => {
38                return Err(MessageConversionError)
39            }
40        })
41    }
42}
43
44impl From<PeerRequest> for Message {
45    fn from(value: PeerRequest) -> Self {
46        match value {
47            PeerRequest::Admin(val) => Self::Request(val),
48            PeerRequest::Protocol(val) => Self::Protocol(val.into()),
49        }
50    }
51}
52
53impl TryFrom<Message> for PeerRequest {
54    type Error = MessageConversionError;
55
56    fn try_from(value: Message) -> Result<Self, Self::Error> {
57        match value {
58            Message::Request(req) => Ok(Self::Admin(req)),
59            Message::Protocol(pro) => Ok(Self::Protocol(pro.try_into()?)),
60            Message::Response(_) => Err(MessageConversionError),
61        }
62    }
63}
64
65impl TryFrom<ProtocolResponse> for ProtocolMessage {
66    type Error = MessageConversionError;
67
68    fn try_from(value: ProtocolResponse) -> Result<Self, Self::Error> {
69        Ok(match value {
70            ProtocolResponse::NewTransactions(val) => Self::NewTransactions(val),
71            ProtocolResponse::NewFluffyBlock(val) => Self::NewFluffyBlock(val),
72            ProtocolResponse::GetChain(val) => Self::ChainEntryResponse(val),
73            ProtocolResponse::GetObjects(val) => Self::GetObjectsResponse(val),
74            ProtocolResponse::FluffyMissingTransactionsRequest(val) => {
75                Self::FluffyMissingTransactionsRequest(val)
76            }
77            ProtocolResponse::NA => return Err(MessageConversionError),
78        })
79    }
80}
81
82impl TryFrom<ProtocolMessage> for ProtocolResponse {
83    type Error = MessageConversionError;
84
85    fn try_from(value: ProtocolMessage) -> Result<Self, Self::Error> {
86        Ok(match value {
87            ProtocolMessage::NewTransactions(val) => Self::NewTransactions(val),
88            ProtocolMessage::NewFluffyBlock(val) => Self::NewFluffyBlock(val),
89            ProtocolMessage::ChainEntryResponse(val) => Self::GetChain(val),
90            ProtocolMessage::GetObjectsResponse(val) => Self::GetObjects(val),
91            ProtocolMessage::ChainRequest(_)
92            | ProtocolMessage::FluffyMissingTransactionsRequest(_)
93            | ProtocolMessage::GetObjectsRequest(_)
94            | ProtocolMessage::GetTxPoolCompliment(_)
95            | ProtocolMessage::NewBlock(_) => return Err(MessageConversionError),
96        })
97    }
98}
99
100impl TryFrom<Message> for PeerResponse {
101    type Error = MessageConversionError;
102
103    fn try_from(value: Message) -> Result<Self, Self::Error> {
104        match value {
105            Message::Response(res) => Ok(Self::Admin(res)),
106            Message::Protocol(pro) => Ok(Self::Protocol(pro.try_into()?)),
107            Message::Request(_) => Err(MessageConversionError),
108        }
109    }
110}
111
112impl TryFrom<PeerResponse> for Message {
113    type Error = MessageConversionError;
114
115    fn try_from(value: PeerResponse) -> Result<Self, Self::Error> {
116        Ok(match value {
117            PeerResponse::Admin(val) => Self::Response(val),
118            PeerResponse::Protocol(val) => Self::Protocol(val.try_into()?),
119        })
120    }
121}
122
123impl TryFrom<PeerRequest> for BroadcastMessage {
124    type Error = MessageConversionError;
125
126    fn try_from(value: PeerRequest) -> Result<Self, Self::Error> {
127        match value {
128            PeerRequest::Protocol(ProtocolRequest::NewTransactions(txs)) => {
129                Ok(Self::NewTransactions(txs))
130            }
131            PeerRequest::Protocol(ProtocolRequest::NewFluffyBlock(block)) => {
132                Ok(Self::NewFluffyBlock(block))
133            }
134            _ => Err(MessageConversionError),
135        }
136    }
137}
138
139impl From<BroadcastMessage> for PeerRequest {
140    fn from(value: BroadcastMessage) -> Self {
141        match value {
142            BroadcastMessage::NewTransactions(txs) => {
143                Self::Protocol(ProtocolRequest::NewTransactions(txs))
144            }
145            BroadcastMessage::NewFluffyBlock(block) => {
146                Self::Protocol(ProtocolRequest::NewFluffyBlock(block))
147            }
148        }
149    }
150}