use futures::Stream;
use sc_network_common::{role::Roles, types::ReputationChange};
use crate::strategy::{state_sync::StateSyncProgress, warp::WarpSyncProgress};
use sc_network_common::sync::message::BlockRequest;
use sc_network_types::PeerId;
use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::{any::Any, fmt, fmt::Formatter, pin::Pin, sync::Arc};
#[derive(Debug)]
pub struct PeerInfo<Block: BlockT> {
pub best_hash: Block::Hash,
pub best_number: NumberFor<Block>,
}
#[derive(Debug)]
pub struct ExtendedPeerInfo<B: BlockT> {
pub roles: Roles,
pub best_hash: B::Hash,
pub best_number: NumberFor<B>,
}
impl<B> Clone for ExtendedPeerInfo<B>
where
B: BlockT,
{
fn clone(&self) -> Self {
Self { roles: self.roles, best_hash: self.best_hash, best_number: self.best_number }
}
}
impl<B> Copy for ExtendedPeerInfo<B> where B: BlockT {}
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum SyncState<BlockNumber> {
Idle,
Downloading { target: BlockNumber },
Importing { target: BlockNumber },
}
impl<BlockNumber> SyncState<BlockNumber> {
pub fn is_major_syncing(&self) -> bool {
!matches!(self, SyncState::Idle)
}
}
#[derive(Debug, Clone)]
pub struct SyncStatus<Block: BlockT> {
pub state: SyncState<NumberFor<Block>>,
pub best_seen_block: Option<NumberFor<Block>>,
pub num_peers: u32,
pub queued_blocks: u32,
pub state_sync: Option<StateSyncProgress>,
pub warp_sync: Option<WarpSyncProgress<Block>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BadPeer(pub PeerId, pub ReputationChange);
impl fmt::Display for BadPeer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Bad peer {}; Reputation change: {:?}", self.0, self.1)
}
}
impl std::error::Error for BadPeer {}
#[derive(Debug)]
pub enum PeerRequest<B: BlockT> {
Block(BlockRequest<B>),
State,
WarpProof,
}
#[derive(Debug)]
pub enum PeerRequestType {
Block,
State,
WarpProof,
}
impl<B: BlockT> PeerRequest<B> {
pub fn get_type(&self) -> PeerRequestType {
match self {
PeerRequest::Block(_) => PeerRequestType::Block,
PeerRequest::State => PeerRequestType::State,
PeerRequest::WarpProof => PeerRequestType::WarpProof,
}
}
}
pub struct OpaqueStateRequest(pub Box<dyn Any + Send>);
impl fmt::Debug for OpaqueStateRequest {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("OpaqueStateRequest").finish()
}
}
pub struct OpaqueStateResponse(pub Box<dyn Any + Send>);
impl fmt::Debug for OpaqueStateResponse {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("OpaqueStateResponse").finish()
}
}
#[async_trait::async_trait]
pub trait SyncStatusProvider<Block: BlockT>: Send + Sync {
async fn status(&self) -> Result<SyncStatus<Block>, ()>;
}
#[async_trait::async_trait]
impl<T, Block> SyncStatusProvider<Block> for Arc<T>
where
T: ?Sized,
T: SyncStatusProvider<Block>,
Block: BlockT,
{
async fn status(&self) -> Result<SyncStatus<Block>, ()> {
T::status(self).await
}
}
pub enum SyncEvent {
PeerConnected(PeerId),
PeerDisconnected(PeerId),
}
pub trait SyncEventStream: Send + Sync {
fn event_stream(&self, name: &'static str) -> Pin<Box<dyn Stream<Item = SyncEvent> + Send>>;
}
impl<T> SyncEventStream for Arc<T>
where
T: ?Sized,
T: SyncEventStream,
{
fn event_stream(&self, name: &'static str) -> Pin<Box<dyn Stream<Item = SyncEvent> + Send>> {
T::event_stream(self, name)
}
}