use std::{
fmt::{Debug, Display},
future::Future,
hash::Hash,
time::Duration,
};
use futures::stream::BoxStream;
use nimiq_serde::{Deserialize, DeserializeError, Serialize};
use nimiq_utils::tagged_signing::{TaggedKeyPair, TaggedSignable};
use thiserror::Error;
use tokio_stream::wrappers::errors::BroadcastStreamRecvError;
use crate::{
peer_info::*,
request::{Message, Request, RequestError},
};
#[derive(Clone, Debug)]
pub enum NetworkEvent<P> {
PeerJoined(P, PeerInfo),
PeerLeft(P),
DhtReady,
}
pub type SubscribeEvents<PeerId> =
BoxStream<'static, Result<NetworkEvent<PeerId>, BroadcastStreamRecvError>>;
const DEFAULT_GOSSIPSUB_RATE_LIMIT_TIME_WINDOW: Duration = Duration::from_secs(10);
pub trait Topic {
type Item: Serialize + Deserialize + Send + Sync + Debug + 'static;
const BUFFER_SIZE: usize;
const NAME: &'static str;
const VALIDATE: bool;
const MAX_MESSAGES: u32;
const TIME_WINDOW: Duration = DEFAULT_GOSSIPSUB_RATE_LIMIT_TIME_WINDOW;
const MAX_MESSAGE_SIZE: usize = MIN_SUPPORTED_MSG_SIZE;
}
pub const MIN_SUPPORTED_MSG_SIZE: usize = 1024 * 1024;
#[derive(Copy, Clone, Debug)]
pub enum MsgAcceptance {
Accept,
Reject,
Ignore,
}
pub trait PubsubId<PeerId>: Clone + Send + Sync + Debug {
fn propagation_source(&self) -> PeerId;
}
pub const MIN_SUPPORTED_REQ_SIZE: usize = 20 * 1024;
pub const MIN_SUPPORTED_RESP_SIZE: usize = 10 * 1024 * 1024;
#[derive(Copy, Clone, Debug)]
pub enum CloseReason {
Other,
RemoteClosed,
GoingOffline,
Error,
MaliciousPeer,
}
#[derive(Debug, Error)]
pub enum SendError {
#[error("{0}")]
Serialization(#[from] DeserializeError),
#[error("Peer connection already closed")]
AlreadyClosed,
}
pub trait Network: Send + Sync + Unpin + 'static {
type PeerId: Copy + Debug + Display + Ord + Hash + Send + Sync + Unpin + 'static;
type AddressType: Debug + Display + 'static;
type Error: std::error::Error;
type PubsubId: PubsubId<Self::PeerId> + Send + Sync + Unpin;
type RequestId: Copy + Debug + Display + Eq + Send + Sync + 'static;
fn get_peers(&self) -> Vec<Self::PeerId>;
fn has_peer(&self, peer_id: Self::PeerId) -> bool;
fn get_peer_info(&self, peer_id: Self::PeerId) -> Option<PeerInfo>;
fn get_peers_by_services(
&self,
services: Services,
min_peers: usize,
) -> impl Future<Output = Result<Vec<Self::PeerId>, Self::Error>> + Send;
fn peer_provides_required_services(&self, peer_id: Self::PeerId) -> bool;
fn peer_provides_services(&self, peer_id: Self::PeerId, services: Services) -> bool;
fn disconnect_peer(
&self,
peer_id: Self::PeerId,
close_reason: CloseReason,
) -> impl Future<Output = ()> + Send;
fn subscribe_events(&self) -> SubscribeEvents<Self::PeerId>;
fn subscribe<T>(
&self,
) -> impl Future<Output = Result<BoxStream<'static, (T::Item, Self::PubsubId)>, Self::Error>> + Send
where
T: Topic + Sync;
fn unsubscribe<T>(&self) -> impl Future<Output = Result<(), Self::Error>> + Send
where
T: Topic + Sync;
fn publish<T>(&self, item: T::Item) -> impl Future<Output = Result<(), Self::Error>> + Send
where
T: Topic + Sync;
fn subscribe_subtopic<T>(
&self,
subtopic: String,
) -> impl Future<Output = Result<BoxStream<'static, (T::Item, Self::PubsubId)>, Self::Error>> + Send
where
T: Topic + Sync;
fn unsubscribe_subtopic<T>(
&self,
subtopic: String,
) -> impl Future<Output = Result<(), Self::Error>> + Send
where
T: Topic + Sync;
fn publish_subtopic<T>(
&self,
subtopic: String,
item: T::Item,
) -> impl Future<Output = Result<(), Self::Error>> + Send
where
T: Topic + Sync;
fn validate_message<T>(&self, id: Self::PubsubId, acceptance: MsgAcceptance)
where
T: Topic + Sync;
fn dht_get<K, V, T>(
&self,
k: &K,
) -> impl Future<Output = Result<Option<V>, Self::Error>> + Send
where
K: AsRef<[u8]> + Send + Sync,
V: Deserialize + Send + Sync + TaggedSignable + Ord,
T: TaggedKeyPair + Send + Sync + Serialize + Deserialize;
fn dht_put<K, V, T>(
&self,
k: &K,
v: &V,
keypair: &T,
) -> impl Future<Output = Result<(), Self::Error>> + Send
where
K: AsRef<[u8]> + Send + Sync,
V: Serialize + Send + Sync + TaggedSignable + Clone + Ord,
T: TaggedKeyPair + Send + Sync + Serialize + Deserialize;
fn dial_peer(
&self,
peer_id: Self::PeerId,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn dial_address(
&self,
address: Self::AddressType,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn get_local_peer_id(&self) -> Self::PeerId;
fn message<M: Message>(
&self,
request: M,
peer_id: Self::PeerId,
) -> impl Future<Output = Result<(), RequestError>> + Send;
fn request<Req: Request>(
&self,
request: Req,
peer_id: Self::PeerId,
) -> impl Future<Output = Result<Req::Response, RequestError>> + Send;
fn receive_messages<M: Message>(&self) -> BoxStream<'static, (M, Self::PeerId)>;
fn receive_requests<Req: Request>(
&self,
) -> BoxStream<'static, (Req, Self::RequestId, Self::PeerId)>;
fn respond<Req: Request>(
&self,
request_id: Self::RequestId,
response: Req::Response,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
}