2023-11-30 18:09:05 +00:00
|
|
|
//! This module contains the implementations of [`TryFrom`] and [`From`] to convert between
|
|
|
|
//! [`Message`], [`PeerRequest`] and [`PeerResponse`].
|
|
|
|
|
2024-07-04 20:05:22 +00:00
|
|
|
use cuprate_wire::{Message, ProtocolMessage};
|
2023-11-30 18:09:05 +00:00
|
|
|
|
2024-07-04 20:05:22 +00:00
|
|
|
use crate::{PeerRequest, PeerResponse, ProtocolRequest, ProtocolResponse};
|
2023-11-30 18:09:05 +00:00
|
|
|
|
2024-01-13 00:07:35 +00:00
|
|
|
#[derive(Debug)]
|
2023-11-30 18:09:05 +00:00
|
|
|
pub struct MessageConversionError;
|
|
|
|
|
2024-07-04 20:05:22 +00:00
|
|
|
impl From<ProtocolRequest> for ProtocolMessage {
|
|
|
|
fn from(value: ProtocolRequest) -> Self {
|
|
|
|
match value {
|
2024-09-21 00:37:06 +00:00
|
|
|
ProtocolRequest::GetObjects(val) => Self::GetObjectsRequest(val),
|
|
|
|
ProtocolRequest::GetChain(val) => Self::ChainRequest(val),
|
|
|
|
ProtocolRequest::FluffyMissingTxs(val) => Self::FluffyMissingTransactionsRequest(val),
|
|
|
|
ProtocolRequest::GetTxPoolCompliment(val) => Self::GetTxPoolCompliment(val),
|
|
|
|
ProtocolRequest::NewBlock(val) => Self::NewBlock(val),
|
|
|
|
ProtocolRequest::NewFluffyBlock(val) => Self::NewFluffyBlock(val),
|
|
|
|
ProtocolRequest::NewTransactions(val) => Self::NewTransactions(val),
|
2023-11-30 18:09:05 +00:00
|
|
|
}
|
2024-07-04 20:05:22 +00:00
|
|
|
}
|
2023-11-30 18:09:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 20:05:22 +00:00
|
|
|
impl TryFrom<ProtocolMessage> for ProtocolRequest {
|
|
|
|
type Error = MessageConversionError;
|
2023-11-30 18:09:05 +00:00
|
|
|
|
2024-07-04 20:05:22 +00:00
|
|
|
fn try_from(value: ProtocolMessage) -> Result<Self, Self::Error> {
|
|
|
|
Ok(match value {
|
2024-09-21 00:37:06 +00:00
|
|
|
ProtocolMessage::GetObjectsRequest(val) => Self::GetObjects(val),
|
|
|
|
ProtocolMessage::ChainRequest(val) => Self::GetChain(val),
|
|
|
|
ProtocolMessage::FluffyMissingTransactionsRequest(val) => Self::FluffyMissingTxs(val),
|
|
|
|
ProtocolMessage::GetTxPoolCompliment(val) => Self::GetTxPoolCompliment(val),
|
|
|
|
ProtocolMessage::NewBlock(val) => Self::NewBlock(val),
|
|
|
|
ProtocolMessage::NewFluffyBlock(val) => Self::NewFluffyBlock(val),
|
|
|
|
ProtocolMessage::NewTransactions(val) => Self::NewTransactions(val),
|
2024-07-04 20:05:22 +00:00
|
|
|
ProtocolMessage::GetObjectsResponse(_) | ProtocolMessage::ChainEntryResponse(_) => {
|
|
|
|
return Err(MessageConversionError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-11-30 18:09:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 20:05:22 +00:00
|
|
|
impl From<PeerRequest> for Message {
|
|
|
|
fn from(value: PeerRequest) -> Self {
|
|
|
|
match value {
|
2024-09-21 00:37:06 +00:00
|
|
|
PeerRequest::Admin(val) => Self::Request(val),
|
|
|
|
PeerRequest::Protocol(val) => Self::Protocol(val.into()),
|
2024-07-04 20:05:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-30 18:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Message> for PeerRequest {
|
|
|
|
type Error = MessageConversionError;
|
|
|
|
|
|
|
|
fn try_from(value: Message) -> Result<Self, Self::Error> {
|
|
|
|
match value {
|
2024-09-21 00:37:06 +00:00
|
|
|
Message::Request(req) => Ok(Self::Admin(req)),
|
|
|
|
Message::Protocol(pro) => Ok(Self::Protocol(pro.try_into()?)),
|
2024-07-04 20:05:22 +00:00
|
|
|
Message::Response(_) => Err(MessageConversionError),
|
2023-11-30 18:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-04 20:05:22 +00:00
|
|
|
impl TryFrom<ProtocolResponse> for ProtocolMessage {
|
|
|
|
type Error = MessageConversionError;
|
|
|
|
|
|
|
|
fn try_from(value: ProtocolResponse) -> Result<Self, Self::Error> {
|
|
|
|
Ok(match value {
|
2024-09-21 00:37:06 +00:00
|
|
|
ProtocolResponse::NewTransactions(val) => Self::NewTransactions(val),
|
|
|
|
ProtocolResponse::NewFluffyBlock(val) => Self::NewFluffyBlock(val),
|
|
|
|
ProtocolResponse::GetChain(val) => Self::ChainEntryResponse(val),
|
|
|
|
ProtocolResponse::GetObjects(val) => Self::GetObjectsResponse(val),
|
2024-07-04 20:05:22 +00:00
|
|
|
ProtocolResponse::NA => return Err(MessageConversionError),
|
|
|
|
})
|
2023-11-30 18:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-04 20:05:22 +00:00
|
|
|
impl TryFrom<ProtocolMessage> for ProtocolResponse {
|
|
|
|
type Error = MessageConversionError;
|
2023-11-30 18:09:05 +00:00
|
|
|
|
2024-07-04 20:05:22 +00:00
|
|
|
fn try_from(value: ProtocolMessage) -> Result<Self, Self::Error> {
|
|
|
|
Ok(match value {
|
2024-09-21 00:37:06 +00:00
|
|
|
ProtocolMessage::NewTransactions(val) => Self::NewTransactions(val),
|
|
|
|
ProtocolMessage::NewFluffyBlock(val) => Self::NewFluffyBlock(val),
|
|
|
|
ProtocolMessage::ChainEntryResponse(val) => Self::GetChain(val),
|
|
|
|
ProtocolMessage::GetObjectsResponse(val) => Self::GetObjects(val),
|
2024-07-04 20:05:22 +00:00
|
|
|
ProtocolMessage::ChainRequest(_)
|
|
|
|
| ProtocolMessage::FluffyMissingTransactionsRequest(_)
|
|
|
|
| ProtocolMessage::GetObjectsRequest(_)
|
|
|
|
| ProtocolMessage::GetTxPoolCompliment(_)
|
|
|
|
| ProtocolMessage::NewBlock(_) => return Err(MessageConversionError),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-11-30 18:09:05 +00:00
|
|
|
|
|
|
|
impl TryFrom<Message> for PeerResponse {
|
|
|
|
type Error = MessageConversionError;
|
|
|
|
|
|
|
|
fn try_from(value: Message) -> Result<Self, Self::Error> {
|
|
|
|
match value {
|
2024-09-21 00:37:06 +00:00
|
|
|
Message::Response(res) => Ok(Self::Admin(res)),
|
|
|
|
Message::Protocol(pro) => Ok(Self::Protocol(pro.try_into()?)),
|
2024-07-04 20:05:22 +00:00
|
|
|
Message::Request(_) => Err(MessageConversionError),
|
2023-11-30 18:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<PeerResponse> for Message {
|
|
|
|
type Error = MessageConversionError;
|
|
|
|
|
|
|
|
fn try_from(value: PeerResponse) -> Result<Self, Self::Error> {
|
|
|
|
Ok(match value {
|
2024-09-21 00:37:06 +00:00
|
|
|
PeerResponse::Admin(val) => Self::Response(val),
|
|
|
|
PeerResponse::Protocol(val) => Self::Protocol(val.try_into()?),
|
2023-11-30 18:09:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|