2024-07-04 20:05:22 +00:00
//! This module defines [`PeerRequest`] and [`PeerResponse`]. Cuprate's P2P crates works by translating network messages into an internal
//! request/response enums, this is easy for levin "requests" and "responses" (admin messages) but takes a bit more work with "notifications"
2024-05-02 22:58:22 +00:00
//! (protocol messages).
//!
2024-07-04 20:05:22 +00:00
//! Some notifications are easy to translate, like [`GetObjectsRequest`] is obviously a request but others like [`NewFluffyBlock`] are a
//! bit tricker. To translate a [`NewFluffyBlock`] into a request/ response we will have to look to see if we asked for [`FluffyMissingTransactionsRequest`],
//! if we have, we interpret [`NewFluffyBlock`] as a response, if not, it's a request that doesn't require a response.
2024-05-02 22:58:22 +00:00
//!
2024-07-04 20:05:22 +00:00
//! Here is every P2P request/response.
2024-05-02 22:58:22 +00:00
//!
2024-09-21 00:37:06 +00:00
//! *note admin messages are already request/response so "Handshake" is actually made of a `HandshakeRequest` & `HandshakeResponse`
2024-07-04 20:05:22 +00:00
//!
//! ```md
2024-05-02 22:58:22 +00:00
//! Admin:
//! Handshake,
//! TimedSync,
//! Ping,
//! SupportFlags
//! Protocol:
//! Request: GetObjectsRequest, Response: GetObjectsResponse,
//! Request: ChainRequest, Response: ChainResponse,
//! Request: FluffyMissingTransactionsRequest, Response: NewFluffyBlock, <- these 2 could be requests or responses
//! Request: GetTxPoolCompliment, Response: NewTransactions, <-
//! Request: NewBlock, Response: None,
//! Request: NewFluffyBlock, Response: None,
//! Request: NewTransactions, Response: None
2024-07-04 20:05:22 +00:00
//!```
2024-05-02 22:58:22 +00:00
//!
2024-06-24 01:30:47 +00:00
use cuprate_wire ::{
2023-11-30 18:09:05 +00:00
protocol ::{
ChainRequest , ChainResponse , FluffyMissingTransactionsRequest , GetObjectsRequest ,
GetObjectsResponse , GetTxPoolCompliment , NewBlock , NewFluffyBlock , NewTransactions ,
} ,
2024-07-04 20:05:22 +00:00
AdminRequestMessage , AdminResponseMessage ,
2023-11-30 18:09:05 +00:00
} ;
mod try_from ;
/// An enum representing a request/ response combination, so a handshake request
/// and response would have the same [`MessageID`]. This allows associating the
/// correct response to a request.
#[ derive(Debug, Eq, PartialEq, Copy, Clone) ]
pub enum MessageID {
Handshake ,
TimedSync ,
Ping ,
SupportFlags ,
GetObjects ,
GetChain ,
FluffyMissingTxs ,
GetTxPoolCompliment ,
NewBlock ,
NewFluffyBlock ,
NewTransactions ,
}
2024-05-02 22:58:22 +00:00
pub enum BroadcastMessage {
2024-01-13 00:07:35 +00:00
NewFluffyBlock ( NewFluffyBlock ) ,
2024-05-02 22:58:22 +00:00
NewTransaction ( NewTransactions ) ,
2024-01-13 00:07:35 +00:00
}
2024-05-02 22:58:22 +00:00
#[ derive(Debug, Clone) ]
2024-07-04 20:05:22 +00:00
pub enum ProtocolRequest {
2023-11-30 18:09:05 +00:00
GetObjects ( GetObjectsRequest ) ,
GetChain ( ChainRequest ) ,
FluffyMissingTxs ( FluffyMissingTransactionsRequest ) ,
GetTxPoolCompliment ( GetTxPoolCompliment ) ,
NewBlock ( NewBlock ) ,
NewFluffyBlock ( NewFluffyBlock ) ,
NewTransactions ( NewTransactions ) ,
}
2024-07-04 20:05:22 +00:00
#[ derive(Debug, Clone) ]
pub enum PeerRequest {
Admin ( AdminRequestMessage ) ,
Protocol ( ProtocolRequest ) ,
}
2023-11-30 18:09:05 +00:00
impl PeerRequest {
2024-09-21 00:37:06 +00:00
pub const fn id ( & self ) -> MessageID {
2023-11-30 18:09:05 +00:00
match self {
2024-09-21 00:37:06 +00:00
Self ::Admin ( admin_req ) = > match admin_req {
2024-07-04 20:05:22 +00:00
AdminRequestMessage ::Handshake ( _ ) = > MessageID ::Handshake ,
AdminRequestMessage ::TimedSync ( _ ) = > MessageID ::TimedSync ,
AdminRequestMessage ::Ping = > MessageID ::Ping ,
AdminRequestMessage ::SupportFlags = > MessageID ::SupportFlags ,
} ,
2024-09-21 00:37:06 +00:00
Self ::Protocol ( protocol_request ) = > match protocol_request {
2024-07-04 20:05:22 +00:00
ProtocolRequest ::GetObjects ( _ ) = > MessageID ::GetObjects ,
ProtocolRequest ::GetChain ( _ ) = > MessageID ::GetChain ,
ProtocolRequest ::FluffyMissingTxs ( _ ) = > MessageID ::FluffyMissingTxs ,
ProtocolRequest ::GetTxPoolCompliment ( _ ) = > MessageID ::GetTxPoolCompliment ,
ProtocolRequest ::NewBlock ( _ ) = > MessageID ::NewBlock ,
ProtocolRequest ::NewFluffyBlock ( _ ) = > MessageID ::NewFluffyBlock ,
ProtocolRequest ::NewTransactions ( _ ) = > MessageID ::NewTransactions ,
} ,
2023-11-30 18:09:05 +00:00
}
}
2024-09-21 00:37:06 +00:00
pub const fn needs_response ( & self ) -> bool {
2023-11-30 18:09:05 +00:00
! matches! (
self ,
2024-09-21 00:37:06 +00:00
Self ::Protocol (
2024-07-04 20:05:22 +00:00
ProtocolRequest ::NewBlock ( _ )
| ProtocolRequest ::NewFluffyBlock ( _ )
| ProtocolRequest ::NewTransactions ( _ )
)
2023-11-30 18:09:05 +00:00
)
}
}
2024-05-02 22:58:22 +00:00
#[ derive(Debug, Clone) ]
2024-07-04 20:05:22 +00:00
pub enum ProtocolResponse {
2023-11-30 18:09:05 +00:00
GetObjects ( GetObjectsResponse ) ,
GetChain ( ChainResponse ) ,
NewFluffyBlock ( NewFluffyBlock ) ,
NewTransactions ( NewTransactions ) ,
NA ,
}
2024-07-04 20:05:22 +00:00
#[ derive(Debug, Clone) ]
pub enum PeerResponse {
Admin ( AdminResponseMessage ) ,
Protocol ( ProtocolResponse ) ,
}
2023-11-30 18:09:05 +00:00
2024-07-04 20:05:22 +00:00
impl PeerResponse {
2024-09-21 00:37:06 +00:00
pub const fn id ( & self ) -> Option < MessageID > {
2024-07-04 20:05:22 +00:00
Some ( match self {
2024-09-21 00:37:06 +00:00
Self ::Admin ( admin_res ) = > match admin_res {
2024-07-04 20:05:22 +00:00
AdminResponseMessage ::Handshake ( _ ) = > MessageID ::Handshake ,
AdminResponseMessage ::TimedSync ( _ ) = > MessageID ::TimedSync ,
AdminResponseMessage ::Ping ( _ ) = > MessageID ::Ping ,
AdminResponseMessage ::SupportFlags ( _ ) = > MessageID ::SupportFlags ,
} ,
2024-09-21 00:37:06 +00:00
Self ::Protocol ( protocol_res ) = > match protocol_res {
2024-07-04 20:05:22 +00:00
ProtocolResponse ::GetObjects ( _ ) = > MessageID ::GetObjects ,
ProtocolResponse ::GetChain ( _ ) = > MessageID ::GetChain ,
ProtocolResponse ::NewFluffyBlock ( _ ) = > MessageID ::NewBlock ,
ProtocolResponse ::NewTransactions ( _ ) = > MessageID ::NewFluffyBlock ,
2023-11-30 18:09:05 +00:00
2024-07-04 20:05:22 +00:00
ProtocolResponse ::NA = > return None ,
} ,
} )
2023-11-30 18:09:05 +00:00
}
}