Skip to main content

reth_network_p2p/
sync.rs

1//! Traits used when interacting with the sync status of the network.
2
3use alloy_eips::eip2124::{ForkFilter, 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    /// Replaces the node's active [`ForkFilter`] to adopt a fork schedule that changed at runtime
36    /// (e.g. an L1-signalled upgrade) without a restart.
37    ///
38    /// The caller must build `fork_filter` from the updated chain spec advanced to the node's
39    /// current head, and should do so before the fork's activation timestamp so the node
40    /// announces the upcoming fork ahead of time. Defaults to a no-op for updaters that do not
41    /// track a fork filter.
42    fn set_fork_filter(&self, fork_filter: ForkFilter) {
43        let _ = fork_filter;
44    }
45}
46
47/// The state the network is currently in when it comes to synchronization.
48#[derive(Copy, Clone, Eq, PartialEq, Debug)]
49pub enum SyncState {
50    /// Node sync is complete.
51    ///
52    /// The network just serves requests to keep up of the chain.
53    Idle,
54    /// Network is syncing
55    Syncing,
56}
57
58impl SyncState {
59    /// Whether the node is currently syncing.
60    ///
61    /// Note: this does not include keep-up sync when the state is idle.
62    pub const fn is_syncing(&self) -> bool {
63        !matches!(self, Self::Idle)
64    }
65}
66
67/// A [`NetworkSyncUpdater`] implementation that does nothing.
68#[derive(Clone, Copy, Debug, Default)]
69#[non_exhaustive]
70pub struct NoopSyncStateUpdater;
71
72impl SyncStateProvider for NoopSyncStateUpdater {
73    fn is_syncing(&self) -> bool {
74        false
75    }
76    fn is_initially_syncing(&self) -> bool {
77        false
78    }
79}
80
81impl NetworkSyncUpdater for NoopSyncStateUpdater {
82    fn update_sync_state(&self, _state: SyncState) {}
83    fn update_status(&self, _: Head) {}
84    fn update_block_range(&self, _update: BlockRangeUpdate) {}
85}