use std::{
cmp::min,
ops::Add,
time::{Duration, Instant},
};
use chrono::{DateTime, TimeDelta, Utc};
use tokio::sync::watch;
use zebra_chain::{
block::{Height, HeightDiff},
chain_sync_status::ChainSyncStatus,
chain_tip::ChainTip,
fmt::humantime_seconds,
parameters::{Network, NetworkUpgrade},
};
use zebra_state::{ChainTipChange, TipAction, MAX_BLOCK_REORG_HEIGHT};
use crate::components::{
health::ChainTipMetrics,
sync::{RestartReason, SyncStatus},
};
#[cfg_attr(not(feature = "sentry"), allow(dead_code))]
pub(crate) const LOG_TARGET: &str = module_path!();
const LOG_INTERVAL: Duration = Duration::from_secs(60);
const PROGRESS_BAR_INTERVAL: Duration = Duration::from_secs(5);
const MAX_CLOSE_TO_TIP_BLOCKS: HeightDiff = 1;
const MIN_SYNC_WARNING_BLOCKS: HeightDiff = 60;
const SYNC_PERCENT_FRAC_DIGITS: usize = 3;
const MIN_BLOCKS_MINED_AFTER_CHECKPOINT_UPDATE: u32 = 10;
const TIP_BLOCK_DELAY_BACKLOG_MARGIN: TimeDelta = TimeDelta::minutes(2);
pub async fn show_block_chain_progress(
network: Network,
latest_chain_tip: impl ChainTip,
sync_status: SyncStatus,
chain_tip_metrics_sender: watch::Sender<ChainTipMetrics>,
) -> ! {
let min_after_checkpoint_blocks =
MAX_BLOCK_REORG_HEIGHT + MIN_BLOCKS_MINED_AFTER_CHECKPOINT_UPDATE;
let min_after_checkpoint_blocks: HeightDiff = min_after_checkpoint_blocks.into();
let after_checkpoint_height = network
.checkpoint_list()
.max_height()
.add(min_after_checkpoint_blocks)
.expect("hard-coded checkpoint height is far below Height::MAX");
let target_spacing_seconds = u64::try_from(
NetworkUpgrade::target_spacing_for_height(&network, network.checkpoint_list().max_height())
.num_seconds(),
)
.expect("target spacings are positive");
let min_minutes_after_checkpoint_update =
(u64::from(MIN_BLOCKS_MINED_AFTER_CHECKPOINT_UPDATE) * target_spacing_seconds).div_ceil(60);
let mut last_state_change_time = Utc::now();
let mut last_state_change_instant = Instant::now();
let mut last_state_change_height = Height(0);
let mut last_log_time = Instant::now();
let mut last_network_upgrade = None;
let mut last_genesis_wait_log: Option<Instant> = None;
#[cfg(feature = "progress-bar")]
let block_bar = howudoin::new().label("Blocks");
let mut is_chain_metrics_chan_closed = false;
loop {
let now = Utc::now();
let instant_now = Instant::now();
let is_syncer_stopped = sync_status.is_close_to_tip();
let diagnostics = sync_status.diagnostics();
let oldest_in_flight_age = diagnostics.oldest_in_flight_age(instant_now);
metrics::gauge!("sync.downloads.oldest_age_seconds")
.set(oldest_in_flight_age.unwrap_or_default().as_secs_f64());
if let Some(estimated_height) =
latest_chain_tip.estimate_network_chain_tip_height(&network, now)
{
let current_height = latest_chain_tip
.best_tip_height()
.expect("unexpected empty state: estimate requires a block height");
let network_upgrade = NetworkUpgrade::current(&network, current_height);
#[cfg(feature = "sentry")]
{
crate::sentry::record_tip_height(current_height);
crate::sentry::record_close_to_tip(is_syncer_stopped);
}
if let Some(previous_network_upgrade) = last_network_upgrade
.replace(network_upgrade)
.filter(|previous| *previous != network_upgrade)
{
info!(
?current_height,
?network_upgrade,
?previous_network_upgrade,
activation_height = network_upgrade
.activation_height(&network)
.map(|height| height.0),
"best chain tip changed network upgrade",
);
}
#[cfg(feature = "progress-bar")]
if matches!(howudoin::cancelled(), Some(true)) {
block_bar.close();
} else {
block_bar
.set_pos(current_height.0)
.set_len(u64::from(estimated_height.0));
}
let mut remaining_sync_blocks = estimated_height - current_height;
if remaining_sync_blocks < 0 {
remaining_sync_blocks = 0;
}
metrics::gauge!("sync.estimated_network_tip_height").set(estimated_height.0 as f64);
metrics::gauge!("sync.estimated_distance_to_tip").set(remaining_sync_blocks as f64);
if current_height > last_state_change_height {
last_state_change_height = current_height;
last_state_change_time = now;
last_state_change_instant = instant_now;
}
if !is_chain_metrics_chan_closed {
if let Err(err) = chain_tip_metrics_sender.send(ChainTipMetrics::new(
last_state_change_instant,
Some(remaining_sync_blocks),
)) {
tracing::warn!(?err, "chain tip metrics channel closed");
is_chain_metrics_chan_closed = true
};
}
let elapsed_since_log = instant_now.saturating_duration_since(last_log_time);
if elapsed_since_log < LOG_INTERVAL {
tokio::time::sleep(PROGRESS_BAR_INTERVAL).await;
continue;
} else {
last_log_time = instant_now;
}
let target_block_spacing =
NetworkUpgrade::target_spacing_for_height(&network, estimated_height);
let max_block_spacing =
NetworkUpgrade::minimum_difficulty_spacing_for_height(&network, estimated_height);
let min_state_block_interval =
max_block_spacing.unwrap_or(target_block_spacing * 4) * 2;
let target_block_spacing = humantime_seconds(
target_block_spacing
.to_std()
.expect("constant fits in std::Duration"),
);
let max_block_spacing = max_block_spacing
.map(|duration| {
humantime_seconds(duration.to_std().expect("constant fits in std::Duration"))
})
.unwrap_or_else(|| "None".to_string());
let sync_progress = f64::from(current_height.0) / f64::from(estimated_height.0);
let sync_percent = format!(
"{:.frac$}%",
sync_progress * 100.0,
frac = SYNC_PERCENT_FRAC_DIGITS,
);
let time_since_last_state_block_chrono =
now.signed_duration_since(last_state_change_time);
let time_since_last_state_block = humantime_seconds(
time_since_last_state_block_chrono
.to_std()
.unwrap_or_default(),
);
if time_since_last_state_block_chrono > min_state_block_interval {
warn!(
%sync_percent,
?current_height,
?network_upgrade,
%time_since_last_state_block,
%target_block_spacing,
%max_block_spacing,
?is_syncer_stopped,
last_restart = diagnostics.last_restart.map(RestartReason::as_str),
restarts_without_progress = diagnostics.restarts_without_progress,
in_flight = diagnostics.in_flight,
oldest_in_flight_secs = oldest_in_flight_age.map(|age| age.as_secs()),
"chain updates have stalled, \
state height has not increased for {} minutes. \
Hint: check your network connection, \
and your computer clock and time zone",
time_since_last_state_block_chrono.num_minutes(),
);
#[cfg(feature = "progress-bar")]
block_bar.desc(format!("{network_upgrade}: sync has stalled"));
} else if is_syncer_stopped && remaining_sync_blocks > MIN_SYNC_WARNING_BLOCKS {
info!(
%sync_percent,
?current_height,
?network_upgrade,
?remaining_sync_blocks,
?after_checkpoint_height,
%time_since_last_state_block,
"initial sync is very slow, or estimated tip is wrong. \
Hint: check your network connection, \
and your computer clock and time zone",
);
#[cfg(feature = "progress-bar")]
block_bar.desc(format!(
"{network_upgrade}: sync is very slow, or estimated tip is wrong"
));
} else if is_syncer_stopped && current_height <= after_checkpoint_height {
warn!(
%sync_percent,
?current_height,
?network_upgrade,
?remaining_sync_blocks,
?after_checkpoint_height,
%time_since_last_state_block,
"initial sync is very slow, and state is below the highest checkpoint. \
Hint: check your network connection, \
and your computer clock and time zone. \
Dev Hint: were the checkpoints updated in the last {} minutes?",
min_minutes_after_checkpoint_update,
);
#[cfg(feature = "progress-bar")]
block_bar.desc(format!("{network_upgrade}: sync is very slow"));
} else if is_syncer_stopped {
info!(
%sync_percent,
?current_height,
?network_upgrade,
?remaining_sync_blocks,
%time_since_last_state_block,
"finished initial sync to chain tip, using gossiped blocks",
);
#[cfg(feature = "progress-bar")]
block_bar.desc(format!("{network_upgrade}: waiting for next block"));
} else if remaining_sync_blocks <= MAX_CLOSE_TO_TIP_BLOCKS {
info!(
%sync_percent,
?current_height,
?network_upgrade,
?remaining_sync_blocks,
%time_since_last_state_block,
"close to finishing initial sync, \
confirming using syncer and gossiped blocks",
);
#[cfg(feature = "progress-bar")]
block_bar.desc(format!("{network_upgrade}: finishing initial sync"));
} else {
info!(
%sync_percent,
?current_height,
?network_upgrade,
?remaining_sync_blocks,
%time_since_last_state_block,
"estimated progress to chain tip",
);
#[cfg(feature = "progress-bar")]
block_bar.desc(format!("{network_upgrade}: syncing blocks"));
}
} else {
let sync_percent = format!("{:.SYNC_PERCENT_FRAC_DIGITS$} %", 0.0f64,);
#[cfg(feature = "progress-bar")]
let network_upgrade = NetworkUpgrade::Genesis;
let should_log = last_genesis_wait_log.is_none_or(|last_log| {
instant_now.saturating_duration_since(last_log) >= LOG_INTERVAL
});
if should_log {
last_genesis_wait_log = Some(instant_now);
}
if is_syncer_stopped && should_log {
warn!(
%sync_percent,
current_height = %"None",
"initial sync can't download and verify the genesis block. \
Hint: check your network connection, \
and your computer clock and time zone",
);
} else if should_log {
info!(
%sync_percent,
current_height = %"None",
"initial sync is waiting to download the genesis block",
);
}
#[cfg(feature = "progress-bar")]
if is_syncer_stopped {
block_bar.desc(format!("{network_upgrade}: can't download genesis block"));
} else {
block_bar.desc(format!(
"{network_upgrade}: waiting to download genesis block"
));
}
}
tokio::time::sleep(min(LOG_INTERVAL, PROGRESS_BAR_INTERVAL)).await;
}
}
#[cfg_attr(not(feature = "sentry"), allow(unused_variables))]
pub async fn record_tip_block_delay(mut chain_tip_change: ChainTipChange) {
let mut tip_block_delay = TipBlockDelay::new(Utc::now());
while let Ok(tip_action) = chain_tip_change.wait_for_tip_change().await {
if let Some((height, delay)) = tip_block_delay.observe(&tip_action, Utc::now()) {
metrics::histogram!("zcash.chain.tip.block_delay_seconds").record(delay);
#[cfg(feature = "sentry")]
crate::sentry::send_tip_block_delay(height, delay);
}
}
}
struct TipBlockDelay {
tip_since: DateTime<Utc>,
}
impl TipBlockDelay {
fn new(now: DateTime<Utc>) -> Self {
Self { tip_since: now }
}
fn observe(&mut self, tip_action: &TipAction, now: DateTime<Utc>) -> Option<(Height, f64)> {
let delay = match tip_action {
TipAction::Grow { block }
if block.time >= self.tip_since - TIP_BLOCK_DELAY_BACKLOG_MARGIN =>
{
Some((block.height, (now - block.time).as_seconds_f64()))
}
_ => None,
};
self.tip_since = now;
delay
}
}
#[cfg(test)]
mod tests {
use zebra_chain::{block, parameters::Network};
use zebra_state::{ChainTipBlock, ChainTipSender};
use super::*;
fn tip_block(height: u8, parent: u8, time: DateTime<Utc>) -> ChainTipBlock {
ChainTipBlock {
hash: block::Hash([height; 32]),
height: block::Height(height.into()),
time,
transactions: Vec::new(),
transaction_hashes: Vec::new().into(),
previous_block_hash: block::Hash([parent; 32]),
}
}
#[tokio::test]
async fn tip_block_delay_skips_the_backlog_after_a_restart() {
let start = Utc::now();
let at = |seconds| start + TimeDelta::seconds(seconds);
let (mut sender, _, mut change) =
ChainTipSender::new(tip_block(10, 9, at(-1_000)), &Network::Mainnet);
let mut tip_block_delay = TipBlockDelay::new(start);
let steps = [
(None, at(0), None),
(Some(tip_block(11, 10, at(-300))), at(1), None),
(Some(tip_block(12, 11, at(-225))), at(2), None),
(Some(tip_block(13, 12, at(-150))), at(3), None),
(
Some(tip_block(14, 13, at(60))),
at(62),
Some((block::Height(14), 2.0)),
),
(
Some(tip_block(15, 14, at(130))),
at(125),
Some((block::Height(15), -5.0)),
),
(
Some(tip_block(16, 15, at(140))),
at(170),
Some((block::Height(16), 30.0)),
),
(
Some(tip_block(17, 16, at(160))),
at(175),
Some((block::Height(17), 15.0)),
),
(Some(tip_block(19, 18, at(200))), at(201), None),
];
for (new_tip, now, expected) in steps {
if let Some(new_tip) = new_tip {
sender.set_best_non_finalized_tip(new_tip);
}
let tip_action = change
.wait_for_tip_change()
.await
.expect("the sender is still in scope");
assert_eq!(tip_block_delay.observe(&tip_action, now), expected);
}
}
}