use fuel_core_types::{
blockchain::SealedBlockHeader,
services::p2p::Transactions,
};
use libp2p::{
request_response::OutboundFailure,
PeerId,
};
use serde::{
Deserialize,
Serialize,
};
use std::ops::Range;
use thiserror::Error;
use tokio::sync::oneshot;
pub(crate) const REQUEST_RESPONSE_PROTOCOL_ID: &str = "/fuel/req_res/0.0.1";
#[cfg(test)]
pub(crate) const MAX_REQUEST_SIZE: usize = core::mem::size_of::<RequestMessage>();
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
pub enum RequestMessage {
SealedHeaders(Range<u32>),
Transactions(Range<u32>),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ResponseMessage {
SealedHeaders(Option<Vec<SealedBlockHeader>>),
Transactions(Option<Vec<Transactions>>),
}
pub type OnResponse<T> = oneshot::Sender<(PeerId, Result<T, ResponseError>)>;
#[derive(Debug)]
pub enum ResponseSender {
SealedHeaders(OnResponse<Option<Vec<SealedBlockHeader>>>),
Transactions(OnResponse<Option<Vec<Transactions>>>),
}
#[derive(Debug, Error)]
pub enum RequestError {
#[error("Not currently connected to any peers")]
NoPeersConnected,
}
#[derive(Debug, Error)]
pub enum ResponseError {
#[error("P2P outbound error {0}")]
P2P(OutboundFailure),
#[error("Peer response message was of incorrect type")]
TypeMismatch,
}
#[derive(Debug, Eq, PartialEq, Error)]
pub enum ResponseSendError {
#[error("Response channel does not exist")]
ResponseChannelDoesNotExist,
#[error("Failed to send response")]
SendingResponseFailed,
#[error("Failed to convert response to intermediate format")]
ConversionToIntermediateFailed,
}