Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A0-1761: Enhance gossip network interface #822

Merged
merged 4 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions finality-aleph/src/network/gossip/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! A P2P-based gossip network, for now only for sending broadcasts.
use std::{
collections::HashSet,
fmt::{Debug, Display},
hash::Hash,
};
Expand All @@ -15,18 +16,34 @@ mod service;
pub use service::Service;

#[async_trait::async_trait]
/// Interface for the gossip network, currently only supports broadcasting and receiving data.
/// Interface for the gossip network. This represents a P2P network and a lot of the properties of
/// this interface result from that. In particular we might know the ID of a given peer, but not be
/// connected to them directly.
pub trait Network<D: Data>: Send + 'static {
type Error: Display + Send;
type PeerId: Clone + Debug + Eq + Hash + Send + 'static;
maciejnems marked this conversation as resolved.
Show resolved Hide resolved

/// Attempt to send data to a peer. Might silently fail if we are not connected to them.
fn send_to(&mut self, data: D, peer_id: Self::PeerId) -> Result<(), Self::Error>;

/// Send data to a random peer, preferably from a list. It should send the data to a randomly
/// chosen peer from the provided list, but if it cannot (e.g. because it's not connected) it
/// will send to a random available peer. No guarantees any peer gets it even if no errors are
/// returned, retry appropriately.
fn send_to_random(
&mut self,
data: D,
peer_ids: HashSet<Self::PeerId>,
) -> Result<(), Self::Error>;

/// Broadcast data to all directly connected peers. Network-wide broadcasts have to be
/// implemented on top of this abstraction. Note that there might be no currently connected
/// peers, so there are no guarantees any single call sends anything even if no errors are
/// returned, retry appropriately.
fn broadcast(&mut self, data: D) -> Result<(), Self::Error>;

/// Receive some data from the network.
async fn next(&mut self) -> Result<D, Self::Error>;
/// Receive some data from the network, including information about who sent it.
async fn next(&mut self) -> Result<(D, Self::PeerId), Self::Error>;
maciejnems marked this conversation as resolved.
Show resolved Hide resolved
}

/// The Authentication protocol is used for validator discovery.
Expand All @@ -51,7 +68,7 @@ pub trait NetworkSender: Send + Sync + 'static {
pub enum Event<P> {
StreamOpened(P, Protocol),
StreamClosed(P, Protocol),
Messages(Vec<(Protocol, Bytes)>),
Messages(P, Vec<(Protocol, Bytes)>),
}

#[async_trait::async_trait]
Expand All @@ -63,7 +80,7 @@ pub trait EventStream<P> {
pub trait RawNetwork: Clone + Send + Sync + 'static {
type SenderError: std::error::Error;
type NetworkSender: NetworkSender;
type PeerId: Clone + Debug + Eq + Hash + Send;
type PeerId: Clone + Debug + Eq + Hash + Send + 'static;
type EventStream: EventStream<Self::PeerId>;

/// Returns a stream of events representing what happens on the network.
Expand Down
Loading