reth_network_p2p/
sync.rs

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