zebrad/components/sync/
end_of_support.rs1use std::time::Duration;
4
5use chrono::{DateTime, Utc};
6use color_eyre::Report;
7
8use zebra_chain::{
9 block::Height,
10 chain_tip::{ChainTip, NetworkChainTipHeightEstimator},
11 parameters::Network,
12};
13
14use crate::application::release_version;
15
16pub const ESTIMATED_RELEASE_HEIGHT: u32 = 3_629_432;
22
23pub const EOS_PANIC_AFTER: u32 = 16;
33
34pub const EOS_WARN_AFTER: u32 = EOS_PANIC_AFTER - 4;
39
40pub const EOS_PANIC_MESSAGE_HEADER: &str = "Zebra refuses to run";
42
43pub const EOS_WARN_MESSAGE_HEADER: &str = "Your Zebra release is too old and it will stop running";
45
46const CHECK_INTERVAL: Duration = Duration::from_secs(60 * 60);
48
49const INITIAL_WAIT: Duration = Duration::from_secs(10);
51
52pub async fn start(
54 network: Network,
55 latest_chain_tip: impl ChainTip + std::fmt::Debug,
56) -> Result<(), Report> {
57 info!("Starting end of support task");
58
59 tokio::time::sleep(INITIAL_WAIT).await;
60
61 loop {
62 if network == Network::Mainnet {
63 if let Some(tip_height) = latest_chain_tip.best_tip_height() {
64 check(tip_height, &network);
65 }
66 } else {
67 info!("Release always valid in Testnet");
68 }
69 tokio::time::sleep(CHECK_INTERVAL).await;
70 }
71}
72
73pub fn end_of_support_height(network: &Network) -> Option<Height> {
80 (network == &Network::Mainnet).then(|| estimated_height_after_release(network, EOS_PANIC_AFTER))
81}
82
83pub fn estimated_height_after_release(network: &Network, days: u32) -> Height {
86 let release_time = DateTime::<Utc>::UNIX_EPOCH;
88 NetworkChainTipHeightEstimator::new(release_time, Height(ESTIMATED_RELEASE_HEIGHT), network)
89 .estimate_height_at(release_time + chrono::Duration::days(days.into()))
90}
91
92pub fn check(tip_height: Height, network: &Network) {
94 info!("Checking if Zebra release is inside support range ...");
95
96 let panic_height = estimated_height_after_release(network, EOS_PANIC_AFTER);
97 let warn_height = estimated_height_after_release(network, EOS_WARN_AFTER);
98
99 if tip_height > panic_height {
100 panic!(
101 "{EOS_PANIC_MESSAGE_HEADER} if the release date is older than {EOS_PANIC_AFTER} days. \
102 \nRelease name: {}, Estimated release height: {ESTIMATED_RELEASE_HEIGHT} \
103 \nHint: Download and install the latest Zebra release from: https://github.com/ZcashFoundation/zebra/releases/latest",
104 release_version()
105 );
106 } else if tip_height > warn_height {
107 warn!(
108 "{EOS_WARN_MESSAGE_HEADER} at block {}. \
109 \nRelease name: {}, Estimated release height: {ESTIMATED_RELEASE_HEIGHT} \
110 \nHint: Download and install the latest Zebra release from: https://github.com/ZcashFoundation/zebra/releases/latest", panic_height.0, release_version()
111 );
112 } else {
113 info!("Zebra release is supported until block {}, please report bugs at https://github.com/ZcashFoundation/zebra/issues", panic_height.0);
114 }
115}