pub mod message;
pub mod metrics;
pub mod warp;
use crate::{role::Roles, types::ReputationChange};
use futures::Stream;
use libp2p_identity::PeerId;
use message::{BlockAnnounce, BlockRequest, BlockResponse};
use sc_consensus::{import_queue::RuntimeOrigin, IncomingBlock};
use sp_consensus::BlockOrigin;
use sp_runtime::{
traits::{Block as BlockT, NumberFor},
Justifications,
};
use warp::WarpSyncProgress;
use std::{any::Any, fmt, fmt::Formatter, pin::Pin, sync::Arc, task::Poll};
#[derive(Debug)]
pub struct PeerInfo<Block: BlockT> {
pub best_hash: Block::Hash,
pub best_number: NumberFor<Block>,
}
#[derive(Clone, Debug)]
pub struct ExtendedPeerInfo<B: BlockT> {
pub roles: Roles,
pub best_hash: B::Hash,
pub best_number: NumberFor<B>,
}
#[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(Clone, Eq, PartialEq, Debug)]
pub struct StateDownloadProgress {
pub percentage: u32,
pub size: u64,
}
#[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 num_connected_peers: u32,
pub queued_blocks: u32,
pub state_sync: Option<StateDownloadProgress>,
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, Clone, PartialEq, Eq)]
pub enum OnBlockData<Block: BlockT> {
Import(BlockOrigin, Vec<IncomingBlock<Block>>),
Request(PeerId, BlockRequest<Block>),
Continue,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OnBlockJustification<Block: BlockT> {
Nothing,
Import {
peer: PeerId,
hash: Block::Hash,
number: NumberFor<Block>,
justifications: Justifications,
},
}
#[derive(Debug)]
pub enum OnStateData<Block: BlockT> {
Import(BlockOrigin, IncomingBlock<Block>),
Continue,
}
#[derive(Debug)]
pub enum ImportResult<B: BlockT> {
BlockImport(BlockOrigin, Vec<IncomingBlock<B>>),
JustificationImport(RuntimeOrigin, B::Hash, NumberFor<B>, Justifications),
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SyncMode {
Full,
LightState {
skip_proofs: bool,
storage_chain_mode: bool,
},
Warp,
}
impl SyncMode {
pub fn is_warp(&self) -> bool {
matches!(self, Self::Warp)
}
pub fn light_state(&self) -> bool {
matches!(self, Self::LightState { .. })
}
}
impl Default for SyncMode {
fn default() -> Self {
Self::Full
}
}
#[derive(Debug)]
pub struct Metrics {
pub queued_blocks: u32,
pub fork_targets: u32,
pub justifications: metrics::Metrics,
}
#[derive(Debug)]
pub enum PeerRequest<B: BlockT> {
Block(BlockRequest<B>),
State,
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)
}
}
pub trait ChainSync<Block: BlockT>: Send {
fn peer_info(&self, who: &PeerId) -> Option<PeerInfo<Block>>;
fn status(&self) -> SyncStatus<Block>;
fn num_sync_requests(&self) -> usize;
fn num_downloaded_blocks(&self) -> usize;
fn num_peers(&self) -> usize;
fn num_active_peers(&self) -> usize;
fn new_peer(
&mut self,
who: PeerId,
best_hash: Block::Hash,
best_number: NumberFor<Block>,
) -> Result<Option<BlockRequest<Block>>, BadPeer>;
fn update_chain_info(&mut self, best_hash: &Block::Hash, best_number: NumberFor<Block>);
fn request_justification(&mut self, hash: &Block::Hash, number: NumberFor<Block>);
fn clear_justification_requests(&mut self);
fn set_sync_fork_request(
&mut self,
peers: Vec<PeerId>,
hash: &Block::Hash,
number: NumberFor<Block>,
);
fn on_block_data(
&mut self,
who: &PeerId,
request: Option<BlockRequest<Block>>,
response: BlockResponse<Block>,
) -> Result<OnBlockData<Block>, BadPeer>;
fn on_block_justification(
&mut self,
who: PeerId,
response: BlockResponse<Block>,
) -> Result<OnBlockJustification<Block>, BadPeer>;
fn on_justification_import(
&mut self,
hash: Block::Hash,
number: NumberFor<Block>,
success: bool,
);
fn on_block_finalized(&mut self, hash: &Block::Hash, number: NumberFor<Block>);
fn on_validated_block_announce(
&mut self,
is_best: bool,
who: PeerId,
announce: &BlockAnnounce<Block::Header>,
);
fn peer_disconnected(&mut self, who: &PeerId);
fn metrics(&self) -> Metrics;
fn poll(&mut self, cx: &mut std::task::Context) -> Poll<()>;
fn send_block_request(&mut self, who: PeerId, request: BlockRequest<Block>);
}