use anyhow::Error;
use async_trait::async_trait;
use clap::Parser;
use tari_utilities::hex::Hex;
use thiserror::Error;
use super::{CommandContext, HandleCommand};
#[derive(Debug, Parser)]
pub struct Args {
start: u64,
end: Option<u64>,
}
#[async_trait]
impl HandleCommand<Args> for CommandContext {
async fn handle_command(&mut self, args: Args) -> Result<(), Error> {
self.list_headers(args.start, args.end).await
}
}
#[derive(Error, Debug)]
enum ArgsError {
#[error("No headers found")]
NoHeaders,
}
impl CommandContext {
pub async fn list_headers(&self, start: u64, end: Option<u64>) -> Result<(), Error> {
let headers = self.get_chain_headers(start, end).await?;
if headers.is_empty() {
Err(ArgsError::NoHeaders.into())
} else {
for header in headers {
println!("\n\nHeader hash: {}", header.hash().to_hex());
println!("{header}");
let adjusted = self
.blockchain_db
.fetch_adjusted_target_difficulty(*header.hash())
.await?;
println!("Adjusted target difficulty (TIP-004 backoff): {adjusted}");
}
Ok(())
}
}
}