1use futures::Stream;
22use sc_network_common::{role::Roles, types::ReputationChange};
23
24use crate::strategy::{state_sync::StateSyncProgress, warp::WarpSyncProgress};
25
26use sc_network_common::sync::message::BlockRequest;
27use sc_network_types::PeerId;
28use sp_runtime::traits::{Block as BlockT, NumberFor};
29
30use std::{any::Any, fmt, fmt::Formatter, pin::Pin, sync::Arc};
31
32#[derive(Debug)]
34pub struct PeerInfo<Block: BlockT> {
35 pub best_hash: Block::Hash,
37 pub best_number: NumberFor<Block>,
39}
40
41#[derive(Debug)]
43pub struct ExtendedPeerInfo<B: BlockT> {
44 pub roles: Roles,
46 pub best_hash: B::Hash,
48 pub best_number: NumberFor<B>,
50}
51
52impl<B> Clone for ExtendedPeerInfo<B>
53where
54 B: BlockT,
55{
56 fn clone(&self) -> Self {
57 Self { roles: self.roles, best_hash: self.best_hash, best_number: self.best_number }
58 }
59}
60
61impl<B> Copy for ExtendedPeerInfo<B> where B: BlockT {}
62
63#[derive(Clone, Eq, PartialEq, Debug)]
65pub enum SyncState<BlockNumber> {
66 Idle,
68 Downloading { target: BlockNumber },
70 Importing { target: BlockNumber },
72}
73
74impl<BlockNumber> SyncState<BlockNumber> {
75 pub fn is_major_syncing(&self) -> bool {
77 !matches!(self, SyncState::Idle)
78 }
79}
80
81#[derive(Debug, Clone)]
83pub struct SyncStatus<Block: BlockT> {
84 pub state: SyncState<NumberFor<Block>>,
86 pub best_seen_block: Option<NumberFor<Block>>,
88 pub num_peers: u32,
90 pub queued_blocks: u32,
92 pub state_sync: Option<StateSyncProgress>,
94 pub warp_sync: Option<WarpSyncProgress<Block>>,
96}
97
98#[derive(Debug, Clone, PartialEq, Eq)]
100pub struct BadPeer(pub PeerId, pub ReputationChange);
101
102impl fmt::Display for BadPeer {
103 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
104 write!(f, "Bad peer {}; Reputation change: {:?}", self.0, self.1)
105 }
106}
107
108impl std::error::Error for BadPeer {}
109
110#[derive(Debug)]
111pub enum PeerRequest<B: BlockT> {
112 Block(BlockRequest<B>),
113 State,
114 WarpProof,
115}
116
117#[derive(Debug)]
118pub enum PeerRequestType {
119 Block,
120 State,
121 WarpProof,
122}
123
124impl<B: BlockT> PeerRequest<B> {
125 pub fn get_type(&self) -> PeerRequestType {
126 match self {
127 PeerRequest::Block(_) => PeerRequestType::Block,
128 PeerRequest::State => PeerRequestType::State,
129 PeerRequest::WarpProof => PeerRequestType::WarpProof,
130 }
131 }
132}
133
134pub struct OpaqueStateRequest(pub Box<dyn Any + Send>);
138
139impl fmt::Debug for OpaqueStateRequest {
140 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
141 f.debug_struct("OpaqueStateRequest").finish()
142 }
143}
144
145pub struct OpaqueStateResponse(pub Box<dyn Any + Send>);
149
150impl fmt::Debug for OpaqueStateResponse {
151 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
152 f.debug_struct("OpaqueStateResponse").finish()
153 }
154}
155
156#[async_trait::async_trait]
158pub trait SyncStatusProvider<Block: BlockT>: Send + Sync {
159 async fn status(&self) -> Result<SyncStatus<Block>, ()>;
161}
162
163#[async_trait::async_trait]
164impl<T, Block> SyncStatusProvider<Block> for Arc<T>
165where
166 T: ?Sized,
167 T: SyncStatusProvider<Block>,
168 Block: BlockT,
169{
170 async fn status(&self) -> Result<SyncStatus<Block>, ()> {
171 T::status(self).await
172 }
173}
174
175pub enum SyncEvent {
177 PeerConnected(PeerId),
179
180 PeerDisconnected(PeerId),
182}
183
184pub trait SyncEventStream: Send + Sync {
185 fn event_stream(&self, name: &'static str) -> Pin<Box<dyn Stream<Item = SyncEvent> + Send>>;
187}
188
189impl<T> SyncEventStream for Arc<T>
190where
191 T: ?Sized,
192 T: SyncEventStream,
193{
194 fn event_stream(&self, name: &'static str) -> Pin<Box<dyn Stream<Item = SyncEvent> + Send>> {
195 T::event_stream(self, name)
196 }
197}