Skip to main content

zebrad/components/sync/
end_of_support.rs

1//! End of support checking task.
2
3use 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
16/// The estimated height that this release will be published.
17///
18/// For 7.2.0 it is chosen so that the halt is Mainnet block 3,684,728, where 7.0.0 halts, so it
19/// may only move by a whole number of days of blocks (3,456 a day), together with
20/// `EOS_PANIC_AFTER`.
21pub const ESTIMATED_RELEASE_HEIGHT: u32 = 3_629_432;
22
23/// The maximum number of days after `ESTIMATED_RELEASE_HEIGHT` where a Zebra server will run
24/// without halting.
25///
26/// Notes:
27///
28/// - Zebra will exit with a panic if the current tip height is bigger than the
29///   `ESTIMATED_RELEASE_HEIGHT` plus this number of days.
30/// - Currently set to 16 days, so 7.2.0 halts around 17 December 2026, after block 3,684,728.
31///   That is the height where 7.0.0 halts, so `ESTIMATED_RELEASE_HEIGHT` is not rounded.
32pub const EOS_PANIC_AFTER: u32 = 16;
33
34/// The number of days after `ESTIMATED_RELEASE_HEIGHT` when Zebra starts displaying end of
35/// support warnings.
36///
37/// Zebra warns for the last 4 days of the support window.
38pub const EOS_WARN_AFTER: u32 = EOS_PANIC_AFTER - 4;
39
40/// A string which is part of the panic that will be displayed if Zebra halts.
41pub const EOS_PANIC_MESSAGE_HEADER: &str = "Zebra refuses to run";
42
43/// A string which is part of the warning that will be displayed if Zebra release is close to halting.
44pub const EOS_WARN_MESSAGE_HEADER: &str = "Your Zebra release is too old and it will stop running";
45
46/// The amount of time between end of support checks.
47const CHECK_INTERVAL: Duration = Duration::from_secs(60 * 60);
48
49/// Wait a few seconds at startup so `best_tip_height` is always `Some`.
50const INITIAL_WAIT: Duration = Duration::from_secs(10);
51
52/// Start the end of support checking task for Mainnet.
53pub 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
73/// Returns the estimated last supported height for this release, or `None` on networks where
74/// end of support is not enforced.
75///
76/// The node runs up to and including this height, and halts with an end of support panic when
77/// the tip goes past it. This matches zcashd, where `end_of_service.block_height` is also the
78/// threshold rather than the first halted block.
79pub fn end_of_support_height(network: &Network) -> Option<Height> {
80    (network == &Network::Mainnet).then(|| estimated_height_after_release(network, EOS_PANIC_AFTER))
81}
82
83/// Returns the estimated height `days` after [`ESTIMATED_RELEASE_HEIGHT`] on `network`,
84/// following the target block spacing in force at each height.
85pub fn estimated_height_after_release(network: &Network, days: u32) -> Height {
86    // Only the elapsed time matters, so any reference time works.
87    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
92/// Check if the current release is too old and panic if so.
93pub 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}