Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
remove qp2p error as a public type
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan-DPC committed Apr 28, 2021
1 parent 4314a8a commit 45cf624
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
/// Internal error.
#[derive(Debug, Error)]
#[allow(missing_docs)]
#[allow(clippy::large_enum_variant)]
pub enum Error {
#[error("Failed signature check.")]
FailedSignature,
#[error("Cannot route.")]
CannotRoute,
#[error("Network layer error: {0}")]
Network(#[from] qp2p::Error),
#[error("The config is invalid")]
InvalidConfig,
#[error("Cannot connect to the endpoint")]
CannotConnectEndpoint,
#[error("Address not reachable")]
AddressNotReachable,
#[error("The node is not in a state to handle the action.")]
InvalidState,
#[error("Invalid source location.")]
Expand Down
27 changes: 19 additions & 8 deletions src/routing/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ impl Comm {
transport_config: qp2p::Config,
event_tx: mpsc::Sender<ConnectionEvent>,
) -> Result<Self> {
let quic_p2p = QuicP2p::with_config(Some(transport_config), &[], true)?;
let quic_p2p = QuicP2p::with_config(Some(transport_config), &[], true)
.map_err(|_| Error::InvalidConfig)?;

// Don't bootstrap, just create an endpoint to listen to
// the incoming messages from other nodes.
// This also returns the a channel where we can listen for
// disconnection events.
let (endpoint, _incoming_connections, incoming_messages, disconnections) =
quic_p2p.new_endpoint().await?;
let (endpoint, _incoming_connections, incoming_messages, disconnections) = quic_p2p
.new_endpoint()
.await
.map_err(|_| Error::CannotConnectEndpoint)?;

let _ = task::spawn(handle_incoming_messages(
incoming_messages,
Expand All @@ -65,12 +68,16 @@ impl Comm {
transport_config: qp2p::Config,
event_tx: mpsc::Sender<ConnectionEvent>,
) -> Result<(Self, SocketAddr)> {
let quic_p2p = QuicP2p::with_config(Some(transport_config), &[], true)?;
let quic_p2p = QuicP2p::with_config(Some(transport_config), &[], true)
.map_err(|_| Error::InvalidConfig)?;

// Bootstrap to the network returning the connection to a node.
// We can use the returned channels to listen for incoming messages and disconnection events
let (endpoint, _incoming_connections, incoming_messages, disconnections, bootstrap_addr) =
quic_p2p.bootstrap().await?;
quic_p2p
.bootstrap()
.await
.map_err(|_| Error::CannotConnectEndpoint)?;

let _ = task::spawn(handle_incoming_messages(
incoming_messages,
Expand Down Expand Up @@ -130,15 +137,19 @@ impl Comm {
..Default::default()
};

let qp2p = QuicP2p::with_config(Some(qp2p_config), &[], false)?;
let (connectivity_endpoint, _, _, _) = qp2p.new_endpoint().await?;
let qp2p = QuicP2p::with_config(Some(qp2p_config), &[], false)
.map_err(|_| Error::InvalidConfig)?;
let (connectivity_endpoint, _, _, _) = qp2p
.new_endpoint()
.await
.map_err(|_| Error::CannotConnectEndpoint)?;

connectivity_endpoint
.is_reachable(peer)
.await
.map_err(|err| {
info!("Peer {} is NOT externally reachable: {}", peer, err);
err.into()
Error::AddressNotReachable
})
.map(|()| {
info!("Peer {} is externally reachable.", peer);
Expand Down

0 comments on commit 45cf624

Please sign in to comment.