Skip to content

Commit

Permalink
feat: introduce check command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru committed Jun 30, 2023
1 parent a2d3b14 commit f17dc4c
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions components/chainhook-cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ use crate::storage::{
};

use chainhook_sdk::bitcoincore_rpc::{Auth, Client, RpcApi};
use chainhook_sdk::chainhooks::bitcoin::BitcoinApplyTransactionPayload;
use chainhook_sdk::chainhooks::types::{
BitcoinChainhookFullSpecification, BitcoinChainhookNetworkSpecification, BitcoinPredicateType,
ChainhookFullSpecification, FileHook, HookAction, OrdinalOperations,
StacksChainhookFullSpecification, StacksChainhookNetworkSpecification, StacksPredicate,
StacksPrintEventBasedPredicate,
BitcoinChainhookFullSpecification, BitcoinChainhookNetworkSpecification,
BitcoinChainhookSpecification, BitcoinPredicateType, ChainhookFullSpecification, FileHook,
HookAction, OrdinalOperations, StacksChainhookFullSpecification,
StacksChainhookNetworkSpecification, StacksPredicate, StacksPrintEventBasedPredicate,
};
use chainhook_sdk::hord::db::{
delete_data_in_hord_db, fetch_and_cache_blocks_in_hord_db, find_last_block_inserted,
Expand Down Expand Up @@ -81,6 +82,9 @@ enum PredicatesCommand {
/// Scan blocks (one-off) from specified network and apply provided predicate
#[clap(name = "scan", bin_name = "scan")]
Scan(ScanPredicate),
/// Check given predicate
#[clap(name = "check", bin_name = "check")]
Check(CheckPredicate),
}

#[derive(Subcommand, PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -147,6 +151,25 @@ struct ScanPredicate {
pub config_path: Option<String>,
}

#[derive(Parser, PartialEq, Clone, Debug)]
struct CheckPredicate {
/// Chainhook spec file to check (json format)
pub predicate_path: String,
/// Target Testnet network
#[clap(long = "testnet", conflicts_with = "mainnet")]
pub testnet: bool,
/// Target Mainnet network
#[clap(long = "mainnet", conflicts_with = "testnet")]
pub mainnet: bool,
/// Load config file path
#[clap(
long = "config-path",
conflicts_with = "mainnet",
conflicts_with = "testnet"
)]
pub config_path: Option<String>,
}

#[derive(Subcommand, PartialEq, Clone, Debug)]
enum ServiceCommand {
/// Start chainhook-cli
Expand Down Expand Up @@ -661,6 +684,41 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
}
}
}
PredicatesCommand::Check(cmd) => {
let config = Config::default(false, cmd.testnet, cmd.mainnet, &cmd.config_path)?;
let predicate: ChainhookFullSpecification =
load_predicate_from_path(&cmd.predicate_path)?;

match predicate {
ChainhookFullSpecification::Bitcoin(predicate) => {
let _ = match predicate
.into_selected_network_specification(&config.network.bitcoin_network)
{
Ok(predicate) => predicate,
Err(e) => {
return Err(format!(
"Specification missing for network {:?}: {e}",
config.network.bitcoin_network
));
}
};
}
ChainhookFullSpecification::Stacks(predicate) => {
let _ = match predicate
.into_selected_network_specification(&config.network.stacks_network)
{
Ok(predicate) => predicate,
Err(e) => {
return Err(format!(
"Specification missing for network {:?}: {e}",
config.network.bitcoin_network
));
}
};
}
}
println!("✔️ Predicate {} successfully checked", cmd.predicate_path);
}
},
Command::Hord(HordCommand::Scan(subcmd)) => match subcmd {
ScanCommand::Inscriptions(cmd) => {
Expand Down

0 comments on commit f17dc4c

Please sign in to comment.