Skip to content

Commit ab022e6

Browse files
author
Ludo Galabru
committed
feat: introduce sync command
1 parent 5bca822 commit ab022e6

File tree

1 file changed

+47
-4
lines changed
  • components/chainhook-cli/src/cli

1 file changed

+47
-4
lines changed

components/chainhook-cli/src/cli/mod.rs

+47-4
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,12 @@ enum DbCommand {
183183
/// Init hord db
184184
#[clap(name = "init", bin_name = "init")]
185185
Init(InitHordDbCommand),
186-
/// Update hord db
187-
#[clap(name = "update", bin_name = "update")]
188-
Update(UpdateHordDbCommand),
186+
/// Rewrite hord db
187+
#[clap(name = "rewrite", bin_name = "rewrite")]
188+
Rewrite(UpdateHordDbCommand),
189+
/// Catch-up hord db
190+
#[clap(name = "sync", bin_name = "sync")]
191+
Sync(SyncHordDbCommand),
189192
/// Rebuild inscriptions entries for a given block
190193
#[clap(name = "drop", bin_name = "drop")]
191194
Drop(DropHordDbCommand),
@@ -269,6 +272,15 @@ struct UpdateHordDbCommand {
269272
pub config_path: Option<String>,
270273
}
271274

275+
#[derive(Parser, PartialEq, Clone, Debug)]
276+
struct SyncHordDbCommand {
277+
/// # of Networking thread
278+
pub network_threads: usize,
279+
/// Load config file path
280+
#[clap(long = "config-path")]
281+
pub config_path: Option<String>,
282+
}
283+
272284
#[derive(Parser, PartialEq, Clone, Debug)]
273285
struct DropHordDbCommand {
274286
/// Starting block
@@ -538,7 +550,38 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
538550

539551
perform_hord_db_update(0, end_block, cmd.network_threads, &config, &ctx).await?;
540552
}
541-
DbCommand::Update(cmd) => {
553+
DbCommand::Sync(cmd) => {
554+
let config = Config::default(false, false, false, &cmd.config_path)?;
555+
let auth = Auth::UserPass(
556+
config.network.bitcoind_rpc_username.clone(),
557+
config.network.bitcoind_rpc_password.clone(),
558+
);
559+
560+
let bitcoin_rpc = match Client::new(&config.network.bitcoind_rpc_url, auth) {
561+
Ok(con) => con,
562+
Err(message) => {
563+
return Err(format!("Bitcoin RPC error: {}", message.to_string()));
564+
}
565+
};
566+
567+
let hord_db_conn = open_readonly_hord_db_conn(&config.expected_cache_path(), &ctx)?;
568+
569+
let start_block = find_latest_compacted_block_known(&hord_db_conn) as u64;
570+
571+
let end_block = match bitcoin_rpc.get_blockchain_info() {
572+
Ok(result) => result.blocks,
573+
Err(e) => {
574+
return Err(format!(
575+
"unable to retrieve Bitcoin chain tip ({})",
576+
e.to_string()
577+
));
578+
}
579+
};
580+
581+
perform_hord_db_update(start_block, end_block, cmd.network_threads, &config, &ctx)
582+
.await?;
583+
}
584+
DbCommand::Rewrite(cmd) => {
542585
let config = Config::default(false, false, false, &cmd.config_path)?;
543586
perform_hord_db_update(
544587
cmd.start_block,

0 commit comments

Comments
 (0)