Skip to content

Commit

Permalink
feat: add get block command to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru committed Jun 9, 2023
1 parent 3ea9f59 commit 97de0b0
Showing 1 changed file with 73 additions and 37 deletions.
110 changes: 73 additions & 37 deletions components/chainhook-cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::scan::bitcoin::scan_bitcoin_chainstate_via_rpc_using_predicate;
use crate::scan::stacks::scan_stacks_chainstate_via_csv_using_predicate;
use crate::service::Service;
use crate::storage::{
get_last_block_height_inserted, is_stacks_block_present, open_readonly_stacks_db_conn,
get_last_block_height_inserted, get_stacks_block_at_block_height, is_stacks_block_present,
open_readonly_stacks_db_conn,
};

use chainhook_sdk::bitcoincore_rpc::{Auth, Client, RpcApi};
Expand Down Expand Up @@ -242,6 +243,9 @@ enum StacksDbCommand {
/// Update database using latest Stacks archive file
#[clap(name = "update", bin_name = "update")]
Update(UpdateDbCommand),
/// Retrieve a block from the Stacks db
#[clap(name = "get", bin_name = "get")]
GetBlock(GetBlockDbCommand),
}

#[derive(Subcommand, PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -385,6 +389,16 @@ struct UpdateDbCommand {
pub config_path: Option<String>,
}

#[derive(Parser, PartialEq, Clone, Debug)]
struct GetBlockDbCommand {
/// Block index to retrieve
#[clap(long = "block-height")]
pub block_height: u64,
/// Load config file path
#[clap(long = "config-path")]
pub config_path: Option<String>,
}

#[derive(Parser, PartialEq, Clone, Debug)]
struct InitHordDbCommand {
/// Load config file path
Expand Down Expand Up @@ -880,46 +894,68 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
unimplemented!()
}
},
Command::Stacks(StacksCommand::Db(StacksDbCommand::Update(cmd))) => {
let mut config = Config::default(false, false, false, &cmd.config_path)?;
download_stacks_dataset_if_required(&mut config, &ctx).await;
}
Command::Stacks(StacksCommand::Db(StacksDbCommand::Check(cmd))) => {
let config = Config::default(false, false, false, &cmd.config_path)?;
// Delete data, if any
{
let stacks_db = open_readonly_stacks_db_conn(&config.expected_cache_path(), &ctx)?;
let mut missing_blocks = vec![];
let mut min = 0;
let mut max = 0;
if let Some(tip) = get_last_block_height_inserted(&stacks_db, &ctx) {
min = 1;
max = tip;
for index in 1..=tip {
let block_identifier = BlockIdentifier {
index,
hash: "".into(),
};
if !is_stacks_block_present(&block_identifier, 3, &stacks_db) {
missing_blocks.push(index);
}
Command::Stacks(subcmd) => match subcmd {
StacksCommand::Db(StacksDbCommand::GetBlock(cmd)) => {
let mut config = Config::default(false, false, false, &cmd.config_path)?;
let stacks_db = open_readonly_stacks_db_conn(&config.expected_cache_path(), &ctx)
.expect("unable to read stacks_db");
match get_stacks_block_at_block_height(cmd.block_height, true, 10, &stacks_db) {
Ok(Some(block)) => {
info!(ctx.expect_logger(), "{}", json!(block));
}
Ok(None) => {
warn!(
ctx.expect_logger(),
"Block {} not present in database", cmd.block_height
);
}
Err(e) => {
error!(ctx.expect_logger(), "{e}",);
}
}
if missing_blocks.is_empty() {
info!(
ctx.expect_logger(),
"Stacks db successfully checked ({min}, {max})"
);
} else {
warn!(
ctx.expect_logger(),
"Stacks db includes {} missing entries ({min}, {max}): {:?}",
missing_blocks.len(),
missing_blocks
);
}
StacksCommand::Db(StacksDbCommand::Update(cmd)) => {
let mut config = Config::default(false, false, false, &cmd.config_path)?;
download_stacks_dataset_if_required(&mut config, &ctx).await;
}
StacksCommand::Db(StacksDbCommand::Check(cmd)) => {
let config = Config::default(false, false, false, &cmd.config_path)?;
// Delete data, if any
{
let stacks_db =
open_readonly_stacks_db_conn(&config.expected_cache_path(), &ctx)?;
let mut missing_blocks = vec![];
let mut min = 0;
let mut max = 0;
if let Some(tip) = get_last_block_height_inserted(&stacks_db, &ctx) {
min = 1;
max = tip;
for index in 1..=tip {
let block_identifier = BlockIdentifier {
index,
hash: "".into(),
};
if !is_stacks_block_present(&block_identifier, 3, &stacks_db) {
missing_blocks.push(index);
}
}
}
if missing_blocks.is_empty() {
info!(
ctx.expect_logger(),
"Stacks db successfully checked ({min}, {max})"
);
} else {
warn!(
ctx.expect_logger(),
"Stacks db includes {} missing entries ({min}, {max}): {:?}",
missing_blocks.len(),
missing_blocks
);
}
}
}
}
},
}
Ok(())
}
Expand Down

0 comments on commit 97de0b0

Please sign in to comment.