refactor: Merge multiplexer & miniprotocols into single crate (#244)
This commit is contained in:
parent
b63d2052cb
commit
d5f0c2fd80
80 changed files with 1694 additions and 3002 deletions
|
|
@ -1,8 +1,5 @@
|
|||
use pallas_crypto::hash::Hash;
|
||||
use pallas_miniprotocols::Point;
|
||||
use pallas_multiplexer as multiplexer;
|
||||
use thiserror::Error;
|
||||
use tracing::{error, trace};
|
||||
use pallas_network::miniprotocols::Point;
|
||||
|
||||
pub type BlockSlot = u64;
|
||||
pub type BlockHash = Hash<32>;
|
||||
|
|
@ -20,112 +17,10 @@ pub trait Cursor: Send + Sync {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ChainSyncEvent {
|
||||
RollForward(BlockSlot, BlockHash),
|
||||
Rollback(Point),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BlockFetchEvent {
|
||||
pub enum UpstreamEvent {
|
||||
RollForward(BlockSlot, BlockHash, RawBlock),
|
||||
Rollback(Point),
|
||||
}
|
||||
|
||||
// ports used by plexer
|
||||
pub type MuxOutputPort = gasket::messaging::tokio::OutputPort<(u16, multiplexer::Payload)>;
|
||||
pub type DemuxInputPort = gasket::messaging::tokio::InputPort<multiplexer::Payload>;
|
||||
|
||||
// ports used by mini-protocols
|
||||
pub type MuxInputPort = gasket::messaging::tokio::InputPort<(u16, multiplexer::Payload)>;
|
||||
pub type DemuxOutputPort = gasket::messaging::tokio::OutputPort<multiplexer::Payload>;
|
||||
|
||||
// final output port
|
||||
pub type DownstreamPort<A> = gasket::messaging::OutputPort<A, BlockFetchEvent>;
|
||||
|
||||
pub struct ProtocolChannel(pub u16, pub MuxOutputPort, pub DemuxInputPort);
|
||||
|
||||
impl multiplexer::agents::Channel for ProtocolChannel {
|
||||
async fn enqueue_chunk(
|
||||
&mut self,
|
||||
payload: multiplexer::Payload,
|
||||
) -> Result<(), multiplexer::agents::ChannelError> {
|
||||
trace!(
|
||||
protocol = self.0,
|
||||
payload = hex::encode(&payload),
|
||||
"enqueing"
|
||||
);
|
||||
|
||||
let res = self
|
||||
.1
|
||||
.send(gasket::messaging::Message::from((self.0, payload)))
|
||||
.await;
|
||||
|
||||
match res {
|
||||
Ok(_) => Ok(()),
|
||||
Err(error) => {
|
||||
error!(?error, "enqueue chunk failed");
|
||||
Err(multiplexer::agents::ChannelError::NotConnected(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn dequeue_chunk(
|
||||
&mut self,
|
||||
) -> Result<multiplexer::Payload, multiplexer::agents::ChannelError> {
|
||||
let res = self.2.recv().await;
|
||||
|
||||
match res {
|
||||
Ok(msg) => Ok(msg.payload),
|
||||
Err(error) => {
|
||||
error!(?error, "dequeue chunk failed");
|
||||
Err(multiplexer::agents::ChannelError::NotConnected(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("{0}")]
|
||||
Client(String),
|
||||
|
||||
#[error("{0}")]
|
||||
Parse(String),
|
||||
|
||||
#[error("{0}")]
|
||||
Server(String),
|
||||
|
||||
#[error("{0}")]
|
||||
Message(String),
|
||||
|
||||
#[error("{0}")]
|
||||
Custom(String),
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn client(error: impl ToString) -> Error {
|
||||
Error::Client(error.to_string())
|
||||
}
|
||||
|
||||
pub fn parse(error: impl ToString) -> Error {
|
||||
Error::Parse(error.to_string())
|
||||
}
|
||||
|
||||
pub fn server(error: impl ToString) -> Error {
|
||||
Error::Server(error.to_string())
|
||||
}
|
||||
|
||||
pub fn message(error: impl ToString) -> Error {
|
||||
Error::Message(error.to_string())
|
||||
}
|
||||
|
||||
pub fn custom(error: impl Into<Box<dyn std::error::Error>>) -> Error {
|
||||
Error::Custom(format!("{}", error.into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<dyn std::error::Error>> for Error {
|
||||
fn from(err: Box<dyn std::error::Error>) -> Self {
|
||||
Error::custom(err)
|
||||
}
|
||||
}
|
||||
pub type DownstreamPort<A> = gasket::messaging::OutputPort<A, UpstreamEvent>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue