Skip to main content

zebra_network/peer/
minimum_peer_version.rs

1//! Watches for chain tip height updates to determine the minimum supported peer protocol version.
2
3use std::fmt;
4
5use zebra_chain::{chain_tip::ChainTip, parameters::Network};
6
7use crate::protocol::external::types::Version;
8
9#[cfg(any(test, feature = "proptest-impl"))]
10mod tests;
11
12/// A helper type to monitor the chain tip in order to determine the minimum peer protocol version
13/// that is currently supported.
14#[derive(Clone)]
15pub struct MinimumPeerVersion<C> {
16    network: Network,
17    chain_tip: C,
18    current_minimum: Version,
19}
20
21impl<C> fmt::Debug for MinimumPeerVersion<C> {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        // skip the chain tip to avoid locking issues
24        f.debug_struct(std::any::type_name::<MinimumPeerVersion<C>>())
25            .field("network", &self.network)
26            .field("current_minimum", &self.current_minimum)
27            .finish()
28    }
29}
30
31impl<C> MinimumPeerVersion<C>
32where
33    C: ChainTip,
34{
35    /// Create a new [`MinimumPeerVersion`] to track the minimum supported peer protocol version
36    /// for the current `chain_tip` on the `network`.
37    pub fn new(chain_tip: C, network: &Network) -> Self {
38        MinimumPeerVersion {
39            network: network.clone(),
40            chain_tip,
41            current_minimum: Version::min_remote_for_height(network, None),
42        }
43    }
44
45    /// Retrieve the current minimum supported peer protocol version.
46    pub fn current(&mut self) -> Version {
47        self.current_minimum =
48            Version::min_remote_for_height(&self.network, self.chain_tip.best_tip_height());
49        self.current_minimum
50    }
51
52    /// Returns a reference to the underlying chain tip.
53    pub fn chain_tip(&self) -> &C {
54        &self.chain_tip
55    }
56}