chore: fix lint warning (#283)
This commit is contained in:
parent
faa11a5bfd
commit
f515a6115c
15 changed files with 133 additions and 112 deletions
|
|
@ -95,7 +95,7 @@ pub struct PeerServer {
|
|||
|
||||
impl PeerServer {
|
||||
pub async fn accept(listener: &TcpListener, magic: u64) -> Result<Self, Error> {
|
||||
let (bearer, _) = Bearer::accept_tcp(&listener)
|
||||
let (bearer, _) = Bearer::accept_tcp(listener)
|
||||
.await
|
||||
.map_err(Error::ConnectFailure)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use crate::multiplexer;
|
|||
use super::{Message, State};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
pub enum ClientError {
|
||||
#[error("attempted to receive message while agency is ours")]
|
||||
AgencyIsOurs,
|
||||
|
||||
|
|
@ -74,57 +74,60 @@ impl Client {
|
|||
}
|
||||
}
|
||||
|
||||
fn assert_agency_is_ours(&self) -> Result<(), Error> {
|
||||
fn assert_agency_is_ours(&self) -> Result<(), ClientError> {
|
||||
if !self.has_agency() {
|
||||
Err(Error::AgencyIsTheirs)
|
||||
Err(ClientError::AgencyIsTheirs)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_agency_is_theirs(&self) -> Result<(), Error> {
|
||||
fn assert_agency_is_theirs(&self) -> Result<(), ClientError> {
|
||||
if self.has_agency() {
|
||||
Err(Error::AgencyIsOurs)
|
||||
Err(ClientError::AgencyIsOurs)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_outbound_state(&self, msg: &Message) -> Result<(), Error> {
|
||||
fn assert_outbound_state(&self, msg: &Message) -> Result<(), ClientError> {
|
||||
match (&self.0, msg) {
|
||||
(State::Idle, Message::RequestRange { .. }) => Ok(()),
|
||||
(State::Idle, Message::ClientDone) => Ok(()),
|
||||
_ => Err(Error::InvalidOutbound),
|
||||
_ => Err(ClientError::InvalidOutbound),
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_inbound_state(&self, msg: &Message) -> Result<(), Error> {
|
||||
fn assert_inbound_state(&self, msg: &Message) -> Result<(), ClientError> {
|
||||
match (&self.0, msg) {
|
||||
(State::Busy, Message::StartBatch) => Ok(()),
|
||||
(State::Busy, Message::NoBlocks) => Ok(()),
|
||||
(State::Streaming, Message::Block { .. }) => Ok(()),
|
||||
(State::Streaming, Message::BatchDone) => Ok(()),
|
||||
_ => Err(Error::InvalidInbound),
|
||||
_ => Err(ClientError::InvalidInbound),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_message(&mut self, msg: &Message) -> Result<(), Error> {
|
||||
pub async fn send_message(&mut self, msg: &Message) -> Result<(), ClientError> {
|
||||
self.assert_agency_is_ours()?;
|
||||
self.assert_outbound_state(msg)?;
|
||||
self.1.send_msg_chunks(msg).await.map_err(Error::Plexer)?;
|
||||
self.1
|
||||
.send_msg_chunks(msg)
|
||||
.await
|
||||
.map_err(ClientError::Plexer)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn recv_message(&mut self) -> Result<Message, Error> {
|
||||
pub async fn recv_message(&mut self) -> Result<Message, ClientError> {
|
||||
self.assert_agency_is_theirs()?;
|
||||
let msg = self.1.recv_full_msg().await.map_err(Error::Plexer)?;
|
||||
let msg = self.1.recv_full_msg().await.map_err(ClientError::Plexer)?;
|
||||
self.assert_inbound_state(&msg)?;
|
||||
|
||||
Ok(msg)
|
||||
}
|
||||
|
||||
pub async fn send_request_range(&mut self, range: (Point, Point)) -> Result<(), Error> {
|
||||
pub async fn send_request_range(&mut self, range: (Point, Point)) -> Result<(), ClientError> {
|
||||
let msg = Message::RequestRange { range };
|
||||
self.send_message(&msg).await?;
|
||||
self.0 = State::Busy;
|
||||
|
|
@ -132,7 +135,7 @@ impl Client {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn recv_while_busy(&mut self) -> Result<HasBlocks, Error> {
|
||||
pub async fn recv_while_busy(&mut self) -> Result<HasBlocks, ClientError> {
|
||||
match self.recv_message().await? {
|
||||
Message::StartBatch => {
|
||||
info!("batch start");
|
||||
|
|
@ -144,7 +147,7 @@ impl Client {
|
|||
self.0 = State::Idle;
|
||||
Ok(None)
|
||||
}
|
||||
_ => Err(Error::InvalidInbound),
|
||||
_ => Err(ClientError::InvalidInbound),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -154,7 +157,7 @@ impl Client {
|
|||
///
|
||||
/// * `range` - A tuple of two `Point` instances representing the start and
|
||||
/// end of the requested block range.
|
||||
pub async fn request_range(&mut self, range: Range) -> Result<HasBlocks, Error> {
|
||||
pub async fn request_range(&mut self, range: Range) -> Result<HasBlocks, ClientError> {
|
||||
self.send_request_range(range).await?;
|
||||
debug!("range requested");
|
||||
self.recv_while_busy().await
|
||||
|
|
@ -164,7 +167,7 @@ impl Client {
|
|||
///
|
||||
/// Returns a block's body if a block is received, or `None` if the
|
||||
/// streaming has ended.
|
||||
pub async fn recv_while_streaming(&mut self) -> Result<Option<Body>, Error> {
|
||||
pub async fn recv_while_streaming(&mut self) -> Result<Option<Body>, ClientError> {
|
||||
debug!("waiting for stream");
|
||||
|
||||
match self.recv_message().await? {
|
||||
|
|
@ -173,7 +176,7 @@ impl Client {
|
|||
self.0 = State::Idle;
|
||||
Ok(None)
|
||||
}
|
||||
_ => Err(Error::InvalidInbound),
|
||||
_ => Err(ClientError::InvalidInbound),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -185,20 +188,20 @@ impl Client {
|
|||
///
|
||||
/// Returns the block's body if the block is found, or an `Error` if the
|
||||
/// block is not found or an invalid message is received.
|
||||
pub async fn fetch_single(&mut self, point: Point) -> Result<Body, Error> {
|
||||
pub async fn fetch_single(&mut self, point: Point) -> Result<Body, ClientError> {
|
||||
self.request_range((point.clone(), point))
|
||||
.await?
|
||||
.ok_or(Error::NoBlocks)?;
|
||||
.ok_or(ClientError::NoBlocks)?;
|
||||
|
||||
let body = self
|
||||
.recv_while_streaming()
|
||||
.await?
|
||||
.ok_or(Error::InvalidInbound)?;
|
||||
.ok_or(ClientError::InvalidInbound)?;
|
||||
|
||||
debug!("body received");
|
||||
|
||||
match self.recv_while_streaming().await? {
|
||||
Some(_) => Err(Error::InvalidInbound),
|
||||
Some(_) => Err(ClientError::InvalidInbound),
|
||||
None => Ok(body),
|
||||
}
|
||||
}
|
||||
|
|
@ -212,8 +215,10 @@ impl Client {
|
|||
///
|
||||
/// Returns a vector of block bodies for the requested range, or an `Error`
|
||||
/// if the range is not found.
|
||||
pub async fn fetch_range(&mut self, range: Range) -> Result<Vec<Body>, Error> {
|
||||
self.request_range(range).await?.ok_or(Error::NoBlocks)?;
|
||||
pub async fn fetch_range(&mut self, range: Range) -> Result<Vec<Body>, ClientError> {
|
||||
self.request_range(range)
|
||||
.await?
|
||||
.ok_or(ClientError::NoBlocks)?;
|
||||
|
||||
let mut all = vec![];
|
||||
|
||||
|
|
@ -230,7 +235,7 @@ impl Client {
|
|||
///
|
||||
/// Returns `Ok(())` if the message is sent successfully, or an `Error` if
|
||||
/// the agency is not ours.
|
||||
pub async fn send_done(&mut self) -> Result<(), Error> {
|
||||
pub async fn send_done(&mut self) -> Result<(), ClientError> {
|
||||
let msg = Message::ClientDone;
|
||||
self.send_message(&msg).await?;
|
||||
self.0 = State::Done;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use crate::multiplexer;
|
|||
use super::{Body, Message, Range, State};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
pub enum ServerError {
|
||||
#[error("attempted to receive message while agency is ours")]
|
||||
AgencyIsOurs,
|
||||
|
||||
|
|
@ -62,57 +62,60 @@ impl Server {
|
|||
}
|
||||
}
|
||||
|
||||
fn assert_agency_is_ours(&self) -> Result<(), Error> {
|
||||
fn assert_agency_is_ours(&self) -> Result<(), ServerError> {
|
||||
if !self.has_agency() {
|
||||
Err(Error::AgencyIsTheirs)
|
||||
Err(ServerError::AgencyIsTheirs)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_agency_is_theirs(&self) -> Result<(), Error> {
|
||||
fn assert_agency_is_theirs(&self) -> Result<(), ServerError> {
|
||||
if self.has_agency() {
|
||||
Err(Error::AgencyIsOurs)
|
||||
Err(ServerError::AgencyIsOurs)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_outbound_state(&self, msg: &Message) -> Result<(), Error> {
|
||||
fn assert_outbound_state(&self, msg: &Message) -> Result<(), ServerError> {
|
||||
match (&self.0, msg) {
|
||||
(State::Busy, Message::NoBlocks) => Ok(()),
|
||||
(State::Busy, Message::StartBatch) => Ok(()),
|
||||
(State::Streaming, Message::Block { .. }) => Ok(()),
|
||||
(State::Streaming, Message::BatchDone) => Ok(()),
|
||||
_ => Err(Error::InvalidOutbound),
|
||||
_ => Err(ServerError::InvalidOutbound),
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_inbound_state(&self, msg: &Message) -> Result<(), Error> {
|
||||
fn assert_inbound_state(&self, msg: &Message) -> Result<(), ServerError> {
|
||||
match (&self.0, msg) {
|
||||
(State::Idle, Message::RequestRange { .. }) => Ok(()),
|
||||
(State::Idle, Message::ClientDone) => Ok(()),
|
||||
_ => Err(Error::InvalidInbound),
|
||||
_ => Err(ServerError::InvalidInbound),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_message(&mut self, msg: &Message) -> Result<(), Error> {
|
||||
pub async fn send_message(&mut self, msg: &Message) -> Result<(), ServerError> {
|
||||
self.assert_agency_is_ours()?;
|
||||
self.assert_outbound_state(msg)?;
|
||||
self.1.send_msg_chunks(msg).await.map_err(Error::Plexer)?;
|
||||
self.1
|
||||
.send_msg_chunks(msg)
|
||||
.await
|
||||
.map_err(ServerError::Plexer)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn recv_message(&mut self) -> Result<Message, Error> {
|
||||
pub async fn recv_message(&mut self) -> Result<Message, ServerError> {
|
||||
self.assert_agency_is_theirs()?;
|
||||
let msg = self.1.recv_full_msg().await.map_err(Error::Plexer)?;
|
||||
let msg = self.1.recv_full_msg().await.map_err(ServerError::Plexer)?;
|
||||
self.assert_inbound_state(&msg)?;
|
||||
|
||||
Ok(msg)
|
||||
}
|
||||
|
||||
pub async fn send_start_batch(&mut self) -> Result<(), Error> {
|
||||
pub async fn send_start_batch(&mut self) -> Result<(), ServerError> {
|
||||
let msg = Message::StartBatch;
|
||||
self.send_message(&msg).await?;
|
||||
self.0 = State::Streaming;
|
||||
|
|
@ -120,7 +123,7 @@ impl Server {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_no_blocks(&mut self) -> Result<(), Error> {
|
||||
pub async fn send_no_blocks(&mut self) -> Result<(), ServerError> {
|
||||
let msg = Message::NoBlocks;
|
||||
self.send_message(&msg).await?;
|
||||
self.0 = State::Idle;
|
||||
|
|
@ -128,14 +131,14 @@ impl Server {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_block(&mut self, body: Body) -> Result<(), Error> {
|
||||
pub async fn send_block(&mut self, body: Body) -> Result<(), ServerError> {
|
||||
let msg = Message::Block { body };
|
||||
self.send_message(&msg).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_batch_done(&mut self) -> Result<(), Error> {
|
||||
pub async fn send_batch_done(&mut self) -> Result<(), ServerError> {
|
||||
let msg = Message::BatchDone;
|
||||
self.send_message(&msg).await?;
|
||||
self.0 = State::Idle;
|
||||
|
|
@ -150,7 +153,7 @@ impl Server {
|
|||
/// progess the server state to `Busy`. If the message is a `ClientDone`,
|
||||
/// return None and progress the server state to `Done`. For any other
|
||||
/// incoming message type return an `Error`.
|
||||
pub async fn recv_while_idle(&mut self) -> Result<Option<BlockRequest>, Error> {
|
||||
pub async fn recv_while_idle(&mut self) -> Result<Option<BlockRequest>, ServerError> {
|
||||
match self.recv_message().await? {
|
||||
Message::RequestRange { range } => {
|
||||
self.0 = State::Busy;
|
||||
|
|
@ -162,7 +165,7 @@ impl Server {
|
|||
|
||||
Ok(None)
|
||||
}
|
||||
_ => Err(Error::InvalidInbound),
|
||||
_ => Err(ServerError::InvalidInbound),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -174,7 +177,7 @@ impl Server {
|
|||
///
|
||||
/// * `blocks` - Ordered list of block bodies corresponding to the client's
|
||||
/// requested range.
|
||||
pub async fn send_block_range(&mut self, blocks: Vec<Body>) -> Result<(), Error> {
|
||||
pub async fn send_block_range(&mut self, blocks: Vec<Body>) -> Result<(), ServerError> {
|
||||
if blocks.is_empty() {
|
||||
self.send_no_blocks().await
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use crate::multiplexer;
|
|||
use super::{BlockContent, HeaderContent, IntersectResponse, Message, State, Tip};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
pub enum ClientError {
|
||||
#[error("attempted to receive message while agency is ours")]
|
||||
AgencyIsOurs,
|
||||
|
||||
|
|
@ -79,32 +79,32 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn assert_agency_is_ours(&self) -> Result<(), Error> {
|
||||
fn assert_agency_is_ours(&self) -> Result<(), ClientError> {
|
||||
if !self.has_agency() {
|
||||
Err(Error::AgencyIsTheirs)
|
||||
Err(ClientError::AgencyIsTheirs)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_agency_is_theirs(&self) -> Result<(), Error> {
|
||||
fn assert_agency_is_theirs(&self) -> Result<(), ClientError> {
|
||||
if self.has_agency() {
|
||||
Err(Error::AgencyIsOurs)
|
||||
Err(ClientError::AgencyIsOurs)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_outbound_state(&self, msg: &Message<O>) -> Result<(), Error> {
|
||||
fn assert_outbound_state(&self, msg: &Message<O>) -> Result<(), ClientError> {
|
||||
match (&self.0, msg) {
|
||||
(State::Idle, Message::RequestNext) => Ok(()),
|
||||
(State::Idle, Message::FindIntersect(_)) => Ok(()),
|
||||
(State::Idle, Message::Done) => Ok(()),
|
||||
_ => Err(Error::InvalidOutbound),
|
||||
_ => Err(ClientError::InvalidOutbound),
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_inbound_state(&self, msg: &Message<O>) -> Result<(), Error> {
|
||||
fn assert_inbound_state(&self, msg: &Message<O>) -> Result<(), ClientError> {
|
||||
match (&self.0, msg) {
|
||||
(State::CanAwait, Message::RollForward(_, _)) => Ok(()),
|
||||
(State::CanAwait, Message::RollBackward(_, _)) => Ok(()),
|
||||
|
|
@ -113,7 +113,7 @@ where
|
|||
(State::MustReply, Message::RollBackward(_, _)) => Ok(()),
|
||||
(State::Intersect, Message::IntersectFound(_, _)) => Ok(()),
|
||||
(State::Intersect, Message::IntersectNotFound(_)) => Ok(()),
|
||||
_ => Err(Error::InvalidInbound),
|
||||
_ => Err(ClientError::InvalidInbound),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -127,11 +127,14 @@ where
|
|||
///
|
||||
/// Returns an error if the agency is not ours or if the outbound state is
|
||||
/// invalid.
|
||||
pub async fn send_message(&mut self, msg: &Message<O>) -> Result<(), Error> {
|
||||
pub async fn send_message(&mut self, msg: &Message<O>) -> Result<(), ClientError> {
|
||||
self.assert_agency_is_ours()?;
|
||||
self.assert_outbound_state(msg)?;
|
||||
|
||||
self.1.send_msg_chunks(msg).await.map_err(Error::Plexer)?;
|
||||
self.1
|
||||
.send_msg_chunks(msg)
|
||||
.await
|
||||
.map_err(ClientError::Plexer)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -142,10 +145,10 @@ where
|
|||
///
|
||||
/// Returns an error if the agency is not theirs or if the inbound state is
|
||||
/// invalid.
|
||||
pub async fn recv_message(&mut self) -> Result<Message<O>, Error> {
|
||||
pub async fn recv_message(&mut self) -> Result<Message<O>, ClientError> {
|
||||
self.assert_agency_is_theirs()?;
|
||||
|
||||
let msg = self.1.recv_full_msg().await.map_err(Error::Plexer)?;
|
||||
let msg = self.1.recv_full_msg().await.map_err(ClientError::Plexer)?;
|
||||
|
||||
self.assert_inbound_state(&msg)?;
|
||||
|
||||
|
|
@ -163,7 +166,7 @@ where
|
|||
///
|
||||
/// Returns an error if the message cannot be sent or if it's not valid for
|
||||
/// the current state of the client.
|
||||
pub async fn send_find_intersect(&mut self, points: Vec<Point>) -> Result<(), Error> {
|
||||
pub async fn send_find_intersect(&mut self, points: Vec<Point>) -> Result<(), ClientError> {
|
||||
let msg = Message::FindIntersect(points);
|
||||
self.send_message(&msg).await?;
|
||||
self.0 = State::Intersect;
|
||||
|
|
@ -178,7 +181,7 @@ where
|
|||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the inbound message is invalid.
|
||||
pub async fn recv_intersect_response(&mut self) -> Result<IntersectResponse, Error> {
|
||||
pub async fn recv_intersect_response(&mut self) -> Result<IntersectResponse, ClientError> {
|
||||
debug!("waiting for intersect response");
|
||||
|
||||
match self.recv_message().await? {
|
||||
|
|
@ -190,7 +193,7 @@ where
|
|||
self.0 = State::Idle;
|
||||
Ok((None, tip))
|
||||
}
|
||||
_ => Err(Error::InvalidInbound),
|
||||
_ => Err(ClientError::InvalidInbound),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -205,12 +208,15 @@ where
|
|||
///
|
||||
/// Returns an error if the intersection point cannot be found or if there
|
||||
/// is a communication error.
|
||||
pub async fn find_intersect(&mut self, points: Vec<Point>) -> Result<IntersectResponse, Error> {
|
||||
pub async fn find_intersect(
|
||||
&mut self,
|
||||
points: Vec<Point>,
|
||||
) -> Result<IntersectResponse, ClientError> {
|
||||
self.send_find_intersect(points).await?;
|
||||
self.recv_intersect_response().await
|
||||
}
|
||||
|
||||
pub async fn send_request_next(&mut self) -> Result<(), Error> {
|
||||
pub async fn send_request_next(&mut self) -> Result<(), ClientError> {
|
||||
let msg = Message::RequestNext;
|
||||
self.send_message(&msg).await?;
|
||||
self.0 = State::CanAwait;
|
||||
|
|
@ -223,7 +229,7 @@ where
|
|||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the inbound message is invalid.
|
||||
pub async fn recv_while_can_await(&mut self) -> Result<NextResponse<O>, Error> {
|
||||
pub async fn recv_while_can_await(&mut self) -> Result<NextResponse<O>, ClientError> {
|
||||
match self.recv_message().await? {
|
||||
Message::AwaitReply => {
|
||||
self.0 = State::MustReply;
|
||||
|
|
@ -237,7 +243,7 @@ where
|
|||
self.0 = State::Idle;
|
||||
Ok(NextResponse::RollBackward(a, b))
|
||||
}
|
||||
_ => Err(Error::InvalidInbound),
|
||||
_ => Err(ClientError::InvalidInbound),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -246,7 +252,7 @@ where
|
|||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the inbound message is invalid.
|
||||
pub async fn recv_while_must_reply(&mut self) -> Result<NextResponse<O>, Error> {
|
||||
pub async fn recv_while_must_reply(&mut self) -> Result<NextResponse<O>, ClientError> {
|
||||
match self.recv_message().await? {
|
||||
Message::RollForward(a, b) => {
|
||||
self.0 = State::Idle;
|
||||
|
|
@ -256,7 +262,7 @@ where
|
|||
self.0 = State::Idle;
|
||||
Ok(NextResponse::RollBackward(a, b))
|
||||
}
|
||||
_ => Err(Error::InvalidInbound),
|
||||
_ => Err(ClientError::InvalidInbound),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -266,7 +272,7 @@ where
|
|||
///
|
||||
/// Returns an error if the message cannot be sent or if the state is not
|
||||
/// idle.
|
||||
pub async fn request_next(&mut self) -> Result<NextResponse<O>, Error> {
|
||||
pub async fn request_next(&mut self) -> Result<NextResponse<O>, ClientError> {
|
||||
debug!("requesting next block");
|
||||
|
||||
self.send_request_next().await?;
|
||||
|
|
@ -280,12 +286,12 @@ where
|
|||
///
|
||||
/// Returns an error if the intersection point cannot be found or if there
|
||||
/// is a communication error.
|
||||
pub async fn intersect_origin(&mut self) -> Result<Point, Error> {
|
||||
pub async fn intersect_origin(&mut self) -> Result<Point, ClientError> {
|
||||
debug!("intersecting origin");
|
||||
|
||||
let (point, _) = self.find_intersect(vec![Point::Origin]).await?;
|
||||
|
||||
point.ok_or(Error::IntersectionNotFound)
|
||||
point.ok_or(ClientError::IntersectionNotFound)
|
||||
}
|
||||
|
||||
/// Attempts to intersect the chain at the latest known tip
|
||||
|
|
@ -294,17 +300,17 @@ where
|
|||
///
|
||||
/// Returns an error if the intersection point cannot be found or if there
|
||||
/// is a communication error.
|
||||
pub async fn intersect_tip(&mut self) -> Result<Point, Error> {
|
||||
pub async fn intersect_tip(&mut self) -> Result<Point, ClientError> {
|
||||
let (_, Tip(point, _)) = self.find_intersect(vec![Point::Origin]).await?;
|
||||
|
||||
debug!(?point, "found tip value");
|
||||
|
||||
let (point, _) = self.find_intersect(vec![point]).await?;
|
||||
|
||||
point.ok_or(Error::IntersectionNotFound)
|
||||
point.ok_or(ClientError::IntersectionNotFound)
|
||||
}
|
||||
|
||||
pub async fn send_done(&mut self) -> Result<(), Error> {
|
||||
pub async fn send_done(&mut self) -> Result<(), ClientError> {
|
||||
let msg = Message::Done;
|
||||
self.send_message(&msg).await?;
|
||||
self.0 = State::Done;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use crate::multiplexer;
|
|||
use super::{BlockContent, HeaderContent, Message, State, Tip};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
pub enum ServerError {
|
||||
#[error("attempted to receive message while agency is ours")]
|
||||
AgencyIsOurs,
|
||||
|
||||
|
|
@ -75,23 +75,23 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn assert_agency_is_ours(&self) -> Result<(), Error> {
|
||||
fn assert_agency_is_ours(&self) -> Result<(), ServerError> {
|
||||
if !self.has_agency() {
|
||||
Err(Error::AgencyIsTheirs)
|
||||
Err(ServerError::AgencyIsTheirs)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_agency_is_theirs(&self) -> Result<(), Error> {
|
||||
fn assert_agency_is_theirs(&self) -> Result<(), ServerError> {
|
||||
if self.has_agency() {
|
||||
Err(Error::AgencyIsOurs)
|
||||
Err(ServerError::AgencyIsOurs)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_outbound_state(&self, msg: &Message<O>) -> Result<(), Error> {
|
||||
fn assert_outbound_state(&self, msg: &Message<O>) -> Result<(), ServerError> {
|
||||
match (&self.0, msg) {
|
||||
(State::CanAwait, Message::RollForward(_, _)) => Ok(()),
|
||||
(State::CanAwait, Message::RollBackward(_, _)) => Ok(()),
|
||||
|
|
@ -100,16 +100,16 @@ where
|
|||
(State::MustReply, Message::RollBackward(_, _)) => Ok(()),
|
||||
(State::Intersect, Message::IntersectFound(_, _)) => Ok(()),
|
||||
(State::Intersect, Message::IntersectNotFound(_)) => Ok(()),
|
||||
_ => Err(Error::InvalidOutbound),
|
||||
_ => Err(ServerError::InvalidOutbound),
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_inbound_state(&self, msg: &Message<O>) -> Result<(), Error> {
|
||||
fn assert_inbound_state(&self, msg: &Message<O>) -> Result<(), ServerError> {
|
||||
match (&self.0, msg) {
|
||||
(State::Idle, Message::RequestNext) => Ok(()),
|
||||
(State::Idle, Message::FindIntersect(_)) => Ok(()),
|
||||
(State::Idle, Message::Done) => Ok(()),
|
||||
_ => Err(Error::InvalidInbound),
|
||||
_ => Err(ServerError::InvalidInbound),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -123,11 +123,14 @@ where
|
|||
///
|
||||
/// Returns an error if the agency is not ours or if the outbound state is
|
||||
/// invalid.
|
||||
pub async fn send_message(&mut self, msg: &Message<O>) -> Result<(), Error> {
|
||||
pub async fn send_message(&mut self, msg: &Message<O>) -> Result<(), ServerError> {
|
||||
self.assert_agency_is_ours()?;
|
||||
self.assert_outbound_state(msg)?;
|
||||
|
||||
self.1.send_msg_chunks(msg).await.map_err(Error::Plexer)?;
|
||||
self.1
|
||||
.send_msg_chunks(msg)
|
||||
.await
|
||||
.map_err(ServerError::Plexer)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -138,10 +141,10 @@ where
|
|||
///
|
||||
/// Returns an error if the agency is not theirs or if the inbound state is
|
||||
/// invalid.
|
||||
async fn recv_message(&mut self) -> Result<Message<O>, Error> {
|
||||
async fn recv_message(&mut self) -> Result<Message<O>, ServerError> {
|
||||
self.assert_agency_is_theirs()?;
|
||||
|
||||
let msg = self.1.recv_full_msg().await.map_err(Error::Plexer)?;
|
||||
let msg = self.1.recv_full_msg().await.map_err(ServerError::Plexer)?;
|
||||
|
||||
self.assert_inbound_state(&msg)?;
|
||||
|
||||
|
|
@ -154,7 +157,7 @@ where
|
|||
///
|
||||
/// Returns an error if the agency is not theirs or if the inbound message
|
||||
/// is invalid for Idle protocol state.
|
||||
pub async fn recv_while_idle(&mut self) -> Result<Option<ClientRequest>, Error> {
|
||||
pub async fn recv_while_idle(&mut self) -> Result<Option<ClientRequest>, ServerError> {
|
||||
match self.recv_message().await? {
|
||||
Message::FindIntersect(points) => {
|
||||
self.0 = State::Intersect;
|
||||
|
|
@ -169,7 +172,7 @@ where
|
|||
|
||||
Ok(None)
|
||||
}
|
||||
_ => Err(Error::InvalidInbound),
|
||||
_ => Err(ServerError::InvalidInbound),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -183,7 +186,7 @@ where
|
|||
///
|
||||
/// Returns an error if the message cannot be sent or if it's not valid for
|
||||
/// the current state of the server.
|
||||
pub async fn send_intersect_not_found(&mut self, tip: Tip) -> Result<(), Error> {
|
||||
pub async fn send_intersect_not_found(&mut self, tip: Tip) -> Result<(), ServerError> {
|
||||
debug!("send intersect not found");
|
||||
|
||||
let msg = Message::IntersectNotFound(tip);
|
||||
|
|
@ -205,7 +208,11 @@ where
|
|||
///
|
||||
/// Returns an error if the message cannot be sent or if it's not valid for
|
||||
/// the current state of the server.
|
||||
pub async fn send_intersect_found(&mut self, point: Point, tip: Tip) -> Result<(), Error> {
|
||||
pub async fn send_intersect_found(
|
||||
&mut self,
|
||||
point: Point,
|
||||
tip: Tip,
|
||||
) -> Result<(), ServerError> {
|
||||
debug!("send intersect found ({point:?}");
|
||||
|
||||
let msg = Message::IntersectFound(point, tip);
|
||||
|
|
@ -227,7 +234,7 @@ where
|
|||
///
|
||||
/// Returns an error if the message cannot be sent or if it's not valid for
|
||||
/// the current state of the server.
|
||||
pub async fn send_roll_forward(&mut self, content: O, tip: Tip) -> Result<(), Error> {
|
||||
pub async fn send_roll_forward(&mut self, content: O, tip: Tip) -> Result<(), ServerError> {
|
||||
debug!("send roll forward");
|
||||
|
||||
let msg = Message::RollForward(content, tip);
|
||||
|
|
@ -248,7 +255,7 @@ where
|
|||
///
|
||||
/// Returns an error if the message cannot be sent or if it's not valid for
|
||||
/// the current state of the server.
|
||||
pub async fn send_roll_backward(&mut self, point: Point, tip: Tip) -> Result<(), Error> {
|
||||
pub async fn send_roll_backward(&mut self, point: Point, tip: Tip) -> Result<(), ServerError> {
|
||||
debug!("send roll backward {point:?}");
|
||||
|
||||
let msg = Message::RollBackward(point, tip);
|
||||
|
|
@ -269,7 +276,7 @@ where
|
|||
///
|
||||
/// Returns an error if the message cannot be sent or if it's not valid for
|
||||
/// the current state of the server.
|
||||
pub async fn send_await_reply(&mut self) -> Result<(), Error> {
|
||||
pub async fn send_await_reply(&mut self) -> Result<(), ServerError> {
|
||||
debug!("send await reply");
|
||||
|
||||
let msg = Message::AwaitReply;
|
||||
|
|
|
|||
|
|
@ -501,7 +501,7 @@ mod tests {
|
|||
|
||||
let channel = AgentChannel::for_client(0, &ingress, &egress);
|
||||
|
||||
egress.0.send((0 ^ 0x8000, input)).unwrap();
|
||||
egress.0.send((0x8000, input)).unwrap();
|
||||
|
||||
let mut buf = ChannelBuffer::new(channel);
|
||||
|
||||
|
|
@ -525,7 +525,7 @@ mod tests {
|
|||
|
||||
while !input.is_empty() {
|
||||
let chunk = Vec::from(input.drain(0..2).as_slice());
|
||||
egress.0.send((0 ^ 0x8000, chunk)).unwrap();
|
||||
egress.0.send((0x8000, chunk)).unwrap();
|
||||
}
|
||||
|
||||
let mut buf = ChannelBuffer::new(channel);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue