reth_network_p2p/
sync.rs

1//! Traits used when interacting with the sync status of the network.
2
3use alloy_eips::eip2124::Head;
4use reth_eth_wire_types::BlockRangeUpdate;
5
6/// A type that provides information about whether the node is currently syncing and the network is
7/// currently serving syncing related requests.
8#[auto_impl::auto_impl(&, Arc, Box)]
9pub trait SyncStateProvider: Send + Sync {
10    /// Returns `true` if the network is undergoing sync.
11    fn is_syncing(&self) -> bool;
12
13    /// Returns `true` if the network is undergoing an initial (pipeline) sync.
14    fn is_initially_syncing(&self) -> bool;
15}
16
17/// An updater for updating the [SyncState] and status of the network.
18///
19/// The node is either syncing, or it is idle.
20/// While syncing, the node will download data from the network and process it. The processing
21/// consists of several stages, like recovering senders, executing the blocks and indexing.
22/// Eventually the node reaches the `Finish` stage and will transition to [`SyncState::Idle`], it
23/// which point the node is considered fully synced.
24#[auto_impl::auto_impl(&, Arc, Box)]
25pub trait NetworkSyncUpdater: std::fmt::Debug + Send + Sync + 'static {
26    /// Notifies about a [SyncState] update.
27    fn update_sync_state(&self, state: SyncState);
28
29    /// Updates the status of the p2p node.
30    fn update_status(&self, head: Head);
31
32    /// Updates the advertised block range.
33    fn update_block_range(&self, update: BlockRangeUpdate);
34}
35
36/// The state the network is currently in when it comes to synchronization.
37#[derive(Copy, Clone, Eq, PartialEq, Debug)]
38pub enum SyncState {
39    /// Node sync is complete.
40    ///
41    /// The network just serves requests to keep up of the chain.
42    Idle,
43    /// Network is syncing
44    Syncing,
45}
46
47impl SyncState {
48    /// Whether the node is currently syncing.
49    ///
50    /// Note: this does not include keep-up sync when the state is idle.
51    pub const fn is_syncing(&self) -> bool {
52        !matches!(self, Self::Idle)
53    }
54}
55
56/// A [`NetworkSyncUpdater`] implementation that does nothing.
57#[derive(Clone, Copy, Debug, Default)]
58#[non_exhaustive]
59pub struct NoopSyncStateUpdater;
60
61impl SyncStateProvider for NoopSyncStateUpdater {
62    fn is_syncing(&self) -> bool {
63        false
64    }
65    fn is_initially_syncing(&self) -> bool {
66        false
67    }
68}
69
70impl NetworkSyncUpdater for NoopSyncStateUpdater {
71    fn update_sync_state(&self, _state: SyncState) {}
72    fn update_status(&self, _: Head) {}
73    fn update_block_range(&self, _update: BlockRangeUpdate) {}
74}