1//! End of support checking task.
23use std::time::Duration;
45use color_eyre::Report;
67use zebra_chain::{
8 block::Height,
9 chain_tip::ChainTip,
10 parameters::{Network, NetworkUpgrade},
11};
1213use crate::application::release_version;
1415/// The estimated height that this release will be published.
16pub const ESTIMATED_RELEASE_HEIGHT: u32 = 2_914_600;
1718/// The maximum number of days after `ESTIMATED_RELEASE_HEIGHT` where a Zebra server will run
19/// without halting.
20///
21/// Notes:
22///
23/// - Zebra will exit with a panic if the current tip height is bigger than the
24/// `ESTIMATED_RELEASE_HEIGHT` plus this number of days.
25/// - Currently set to 16 weeks.
26pub const EOS_PANIC_AFTER: u32 = 112;
2728/// The number of days before the end of support where Zebra will display warnings.
29pub const EOS_WARN_AFTER: u32 = EOS_PANIC_AFTER - 14;
3031/// A string which is part of the panic that will be displayed if Zebra halts.
32pub const EOS_PANIC_MESSAGE_HEADER: &str = "Zebra refuses to run";
3334/// A string which is part of the warning that will be displayed if Zebra release is close to halting.
35pub const EOS_WARN_MESSAGE_HEADER: &str = "Your Zebra release is too old and it will stop running";
3637/// The amount of time between end of support checks.
38const CHECK_INTERVAL: Duration = Duration::from_secs(60 * 60);
3940/// Wait a few seconds at startup so `best_tip_height` is always `Some`.
41const INITIAL_WAIT: Duration = Duration::from_secs(10);
4243/// Start the end of support checking task for Mainnet.
44pub async fn start(
45 network: Network,
46 latest_chain_tip: impl ChainTip + std::fmt::Debug,
47) -> Result<(), Report> {
48info!("Starting end of support task");
4950 tokio::time::sleep(INITIAL_WAIT).await;
5152loop {
53if network == Network::Mainnet {
54if let Some(tip_height) = latest_chain_tip.best_tip_height() {
55 check(tip_height, &network);
56 }
57 } else {
58info!("Release always valid in Testnet");
59 }
60 tokio::time::sleep(CHECK_INTERVAL).await;
61 }
62}
6364/// Check if the current release is too old and panic if so.
65pub fn check(tip_height: Height, network: &Network) {
66info!("Checking if Zebra release is inside support range ...");
6768// Get the current block spacing
69let target_block_spacing = NetworkUpgrade::target_spacing_for_height(network, tip_height);
7071// Get the number of blocks per day
72let estimated_blocks_per_day =
73 u32::try_from(chrono::Duration::days(1).num_seconds() / target_block_spacing.num_seconds())
74 .expect("number is always small enough to fit");
7576let panic_height =
77 Height(ESTIMATED_RELEASE_HEIGHT + (EOS_PANIC_AFTER * estimated_blocks_per_day));
78let warn_height =
79 Height(ESTIMATED_RELEASE_HEIGHT + (EOS_WARN_AFTER * estimated_blocks_per_day));
8081if tip_height > panic_height {
82panic!(
83"{EOS_PANIC_MESSAGE_HEADER} if the release date is older than {EOS_PANIC_AFTER} days. \
84 \nRelease name: {}, Estimated release height: {ESTIMATED_RELEASE_HEIGHT} \
85 \nHint: Download and install the latest Zebra release from: https://github.com/ZcashFoundation/zebra/releases/latest",
86 release_version()
87 );
88 } else if tip_height > warn_height {
89warn!(
90"{EOS_WARN_MESSAGE_HEADER} at block {}. \
91 \nRelease name: {}, Estimated release height: {ESTIMATED_RELEASE_HEIGHT} \
92 \nHint: Download and install the latest Zebra release from: https://github.com/ZcashFoundation/zebra/releases/latest", panic_height.0, release_version()
93 );
94 } else {
95info!("Zebra release is supported until block {}, please report bugs at https://github.com/ZcashFoundation/zebra/issues", panic_height.0);
96 }
97}