use std::fmt;
use zebra_chain::{chain_tip::ChainTip, parameters::Network};
use crate::protocol::external::types::Version;
#[cfg(any(test, feature = "proptest-impl"))]
mod tests;
#[derive(Clone)]
pub struct MinimumPeerVersion<C> {
network: Network,
chain_tip: C,
current_minimum: Version,
}
impl<C> fmt::Debug for MinimumPeerVersion<C> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(std::any::type_name::<MinimumPeerVersion<C>>())
.field("network", &self.network)
.field("current_minimum", &self.current_minimum)
.finish()
}
}
impl<C> MinimumPeerVersion<C>
where
C: ChainTip,
{
pub fn new(chain_tip: C, network: &Network) -> Self {
MinimumPeerVersion {
network: network.clone(),
chain_tip,
current_minimum: Version::min_remote_for_height(network, None),
}
}
pub fn current(&mut self) -> Version {
self.current_minimum =
Version::min_remote_for_height(&self.network, self.chain_tip.best_tip_height());
self.current_minimum
}
pub fn chain_tip(&self) -> &C {
&self.chain_tip
}
}