1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::path::PathBuf;
use abscissa_core::{Command, Options, Runnable};
use color_eyre::eyre::{eyre, Result};
use zebra_chain::{
block::{self, Height},
chain_tip::ChainTip,
parameters::Network,
};
use zebra_state::LatestChainTip;
use crate::prelude::app_config;
#[derive(Command, Debug, Options)]
pub struct TipHeightCmd {
#[options(help = "path to directory with the Zebra chain state")]
cache_dir: Option<PathBuf>,
#[options(default = "mainnet", help = "the network of the chain to load")]
network: Network,
}
impl Runnable for TipHeightCmd {
#[allow(clippy::print_stdout)]
fn run(&self) {
match self.load_tip_height() {
Ok(height) => println!("{}", height.0),
Err(error) => tracing::error!("Failed to read chain tip height from state: {error}"),
}
}
}
impl TipHeightCmd {
fn load_tip_height(&self) -> Result<block::Height> {
let latest_chain_tip = self.load_latest_chain_tip();
latest_chain_tip
.best_tip_height()
.ok_or_else(|| eyre!("State directory doesn't have a chain tip block"))
}
fn load_latest_chain_tip(&self) -> LatestChainTip {
let mut config = app_config().state.clone();
if let Some(cache_dir) = self.cache_dir.clone() {
config.cache_dir = cache_dir;
}
let (_state_service, _read_state_service, latest_chain_tip, _chain_tip_change) =
zebra_state::init(config, self.network, Height::MAX, 0);
latest_chain_tip
}
}