Skip to content

Commit

Permalink
feat: Migrate networking trait
Browse files Browse the repository at this point in the history
  • Loading branch information
nmccarty committed Feb 14, 2022
1 parent badb61f commit 63fdf55
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 106 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions phaselock-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ version = "0.0.6-dev"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-std = "1.10.0"
async-tungstenite = "0.16.1"
bincode = "1.3.3"
blake3 = "1.3.1"
futures = "0.3.21"
hex_fmt = "0.3.0"
rand = "0.7.3"
rand_chacha = "0.2.2"
serde = { version = "1.0.136", features = ["derive"] }
serde_bytes = "0.11.5"
snafu = "0.7.0"
threshold_crypto = "0.4.0"
1 change: 1 addition & 0 deletions phaselock-types/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Common traits for the `PhaseLock` protocol
pub mod block_contents;
pub mod network;
pub mod state;

pub use block_contents::BlockContents;
Expand Down
110 changes: 110 additions & 0 deletions phaselock-types/src/traits/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//! Network access abstraction
//!
//! Contains types and traits used by `PhaseLock` to abstract over network access
use async_tungstenite::tungstenite::error as werror;
use futures::future::BoxFuture;
use serde::{de::DeserializeOwned, Serialize};
use snafu::Snafu;

use crate::PubKey;

/// A boxed future trait object with a static lifetime
pub type BoxedFuture<T> = BoxFuture<'static, T>;

/// Error type for networking
#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
pub enum NetworkError {
/// A Listener failed to send a message
ListenerSend,
/// Could not deliver a message to a specified recipient
CouldNotDeliver,
/// Attempted to deliver a message to an unknown node
NoSuchNode,
/// Failed to serialize a message
FailedToSerialize {
/// Originating bincode error
source: bincode::Error,
},
/// Failed to deserealize a message
FailedToDeserialize {
/// originating bincode error
source: bincode::Error,
},
/// WebSockets specific error
WebSocket {
/// Originating websockets error
source: werror::Error,
},
/// Error orginiating from within the executor
ExecutorError {
/// Originating async_std error
source: async_std::io::Error,
},
/// Failed to decode a socket specification
SocketDecodeError {
/// Input that was given
input: String,
/// Originating io error
source: std::io::Error,
},
/// Failed to bind a listener socket
FailedToBindListener {
/// originating io error
source: std::io::Error,
},
/// No sockets were open
NoSocketsError {
/// Input that was given
input: String,
},
/// Generic error type for compatibility if needed
Other {
/// Originating error
inner: Box<dyn std::error::Error + Send + Sync>,
},
/// Channel error
ChannelSend,
/// Could not complete handshake
IdentityHandshake,
/// The underlying connection has been shut down
ShutDown,
}

/// Describes, generically, the behaviors a networking implementation must have
pub trait NetworkingImplementation<M>: Send + Sync
where
M: Serialize + DeserializeOwned + Send + Clone + 'static,
{
/// Broadcasts a message to the network
///
/// Should provide that the message eventually reach all non-faulty nodes
fn broadcast_message(&self, message: M) -> BoxFuture<'_, Result<(), NetworkError>>;
/// Sends a direct message to a specific node
fn message_node(
&self,
message: M,
recipient: PubKey,
) -> BoxFuture<'_, Result<(), NetworkError>>;
/// Moves out the entire queue of received broadcast messages, should there be any
///
/// Provided as a future to allow the backend to do async locking
fn broadcast_queue(&self) -> BoxFuture<'_, Result<Vec<M>, NetworkError>>;
/// Provides a future for the next received broadcast
///
/// Will unwrap the underlying `NetworkMessage`
fn next_broadcast(&self) -> BoxFuture<'_, Result<M, NetworkError>>;
/// Moves out the entire queue of received direct messages to this node
fn direct_queue(&self) -> BoxFuture<'_, Result<Vec<M>, NetworkError>>;
/// Provides a future for the next received direct message to this node
///
/// Will unwrap the underlying `NetworkMessage`
fn next_direct(&self) -> BoxFuture<'_, Result<M, NetworkError>>;
/// Node's currently known to the networking implementation
///
/// Kludge function to work around leader election
fn known_nodes(&self) -> BoxFuture<'_, Vec<PubKey>>;
/// Object safe clone
fn obj_clone(&self) -> Box<dyn NetworkingImplementation<M> + 'static>;
}
112 changes: 6 additions & 106 deletions src/traits/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,112 +8,12 @@
//! In future, this module will contain a production ready networking implementation, very probably
//! one libp2p based.
use crate::PubKey;

use async_tungstenite::tungstenite::error as werror;
use futures::future::BoxFuture;
use serde::{de::DeserializeOwned, Serialize};
use snafu::Snafu;

pub mod memory_network;
pub mod w_network;

/// A boxed future trait object with a static lifetime
pub type BoxedFuture<T> = BoxFuture<'static, T>;

/// Error type for networking
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum NetworkError {
/// A Listener failed to send a message
ListenerSend,
/// Could not deliver a message to a specified recipient
CouldNotDeliver,
/// Attempted to deliver a message to an unknown node
NoSuchNode,
/// Failed to serialize a message
FailedToSerialize {
/// Originating bincode error
source: bincode::Error,
},
/// Failed to deserealize a message
FailedToDeserialize {
/// originating bincode error
source: bincode::Error,
},
/// WebSockets specific error
WebSocket {
/// Originating websockets error
source: werror::Error,
},
/// Error orginiating from within the executor
ExecutorError {
/// Originating async_std error
source: async_std::io::Error,
},
/// Failed to decode a socket specification
SocketDecodeError {
/// Input that was given
input: String,
/// Originating io error
source: std::io::Error,
},
/// Failed to bind a listener socket
FailedToBindListener {
/// originating io error
source: std::io::Error,
},
/// No sockets were open
NoSocketsError {
/// Input that was given
input: String,
},
/// Generic error type for compatibility if needed
Other {
/// Originating error
inner: Box<dyn std::error::Error + Send + Sync>,
},
/// Channel error
ChannelSend,
/// Could not complete handshake
IdentityHandshake,
/// The underlying connection has been shut down
ShutDown,
}

/// Describes, generically, the behaviors a networking implementation must have
pub trait NetworkingImplementation<M>: Send + Sync
where
M: Serialize + DeserializeOwned + Send + Clone + 'static,
{
/// Broadcasts a message to the network
///
/// Should provide that the message eventually reach all non-faulty nodes
fn broadcast_message(&self, message: M) -> BoxFuture<'_, Result<(), NetworkError>>;
/// Sends a direct message to a specific node
fn message_node(
&self,
message: M,
recipient: PubKey,
) -> BoxFuture<'_, Result<(), NetworkError>>;
/// Moves out the entire queue of received broadcast messages, should there be any
///
/// Provided as a future to allow the backend to do async locking
fn broadcast_queue(&self) -> BoxFuture<'_, Result<Vec<M>, NetworkError>>;
/// Provides a future for the next received broadcast
///
/// Will unwrap the underlying `NetworkMessage`
fn next_broadcast(&self) -> BoxFuture<'_, Result<M, NetworkError>>;
/// Moves out the entire queue of received direct messages to this node
fn direct_queue(&self) -> BoxFuture<'_, Result<Vec<M>, NetworkError>>;
/// Provides a future for the next received direct message to this node
///
/// Will unwrap the underlying `NetworkMessage`
fn next_direct(&self) -> BoxFuture<'_, Result<M, NetworkError>>;
/// Node's currently known to the networking implementation
///
/// Kludge function to work around leader election
fn known_nodes(&self) -> BoxFuture<'_, Vec<PubKey>>;
/// Object safe clone
fn obj_clone(&self) -> Box<dyn NetworkingImplementation<M> + 'static>;
}
pub use phaselock_types::traits::network::{
BoxedFuture, ChannelSendSnafu, CouldNotDeliverSnafu, ExecutorSnafu, FailedToBindListenerSnafu,
FailedToDeserializeSnafu, FailedToSerializeSnafu, IdentityHandshakeSnafu, ListenerSendSnafu,
NetworkError, NetworkingImplementation, NoSocketsSnafu, NoSuchNodeSnafu, OtherSnafu,
ShutDownSnafu, SocketDecodeSnafu, WebSocketSnafu,
};

0 comments on commit 63fdf55

Please sign in to comment.