Skip to content

Commit 991e33f

Browse files
author
Ludo Galabru
committed
feat: improve cli ergonomics
1 parent e2fe0b1 commit 991e33f

File tree

1 file changed

+73
-27
lines changed
  • components/ordhook-cli/src/cli

1 file changed

+73
-27
lines changed

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

+73-27
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use ordhook::db::{
3030
};
3131
use ordhook::download::download_ordinals_dataset_if_required;
3232
use ordhook::scan::bitcoin::scan_bitcoin_chainstate_via_rpc_using_predicate;
33-
use ordhook::service::Service;
33+
use ordhook::service::{start_observer_forwarding, Service};
3434
use reqwest::Client as HttpClient;
3535
use std::collections::BTreeMap;
3636
use std::io::{BufReader, Read};
@@ -155,31 +155,56 @@ enum RepairCommand {
155155
Inscriptions(RepairStorageCommand),
156156
/// Rewrite transfers data in hord.sqlite
157157
#[clap(name = "transfers", bin_name = "transfers")]
158-
Transfers(RepairTransfersCommand),
158+
Transfers(RepairStorageCommand),
159159
}
160160

161161
#[derive(Parser, PartialEq, Clone, Debug)]
162162
struct RepairStorageCommand {
163-
/// Starting block
164-
pub start_block: u64,
165-
/// Ending block
166-
pub end_block: u64,
163+
/// Interval of blocks (--interval 767430:800000)
164+
#[clap(long = "interval", conflicts_with = "blocks")]
165+
pub blocks_interval: Option<String>,
166+
/// List of blocks (--blocks 767430,767431,767433,800000)
167+
#[clap(long = "blocks", conflicts_with = "interval")]
168+
pub blocks: Option<String>,
167169
/// Network threads
168-
pub network_threads: usize,
170+
#[clap(long = "network-threads")]
171+
pub network_threads: Option<usize>,
169172
/// Load config file path
170173
#[clap(long = "config-path")]
171174
pub config_path: Option<String>,
175+
/// Cascade to observers
176+
#[clap(short, long, action = clap::ArgAction::SetTrue)]
177+
pub repair_observers: Option<bool>,
172178
}
173179

174-
#[derive(Parser, PartialEq, Clone, Debug)]
175-
struct RepairTransfersCommand {
176-
/// Starting block
177-
pub start_block: u64,
178-
/// Ending block
179-
pub end_block: u64,
180-
/// Load config file path
181-
#[clap(long = "config-path")]
182-
pub config_path: Option<String>,
180+
impl RepairStorageCommand {
181+
pub fn get_blocks(&self) -> Vec<u64> {
182+
let blocks = match (&self.blocks_interval, &self.blocks) {
183+
(Some(interval), None) => {
184+
let blocks = interval.split(":").collect::<Vec<_>>();
185+
let start_block: u64 = blocks
186+
.get(0)
187+
.expect("unable to get start_block")
188+
.parse::<u64>()
189+
.expect("unable to parse start_block");
190+
let end_block: u64 = blocks
191+
.get(1)
192+
.expect("unable to get end_block")
193+
.parse::<u64>()
194+
.expect("unable to parse end_block");
195+
BlockHeights::BlockRange(start_block, end_block).get_sorted_entries()
196+
}
197+
(None, Some(blocks)) => {
198+
let blocks = blocks
199+
.split(",")
200+
.map(|b| b.parse::<u64>().expect("unable to parse block"))
201+
.collect::<Vec<_>>();
202+
BlockHeights::Blocks(blocks).get_sorted_entries()
203+
}
204+
_ => unreachable!(),
205+
};
206+
blocks.into()
207+
}
183208
}
184209

185210
#[derive(Subcommand, PartialEq, Clone, Debug)]
@@ -650,15 +675,18 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
650675
RepairCommand::Blocks(cmd) => {
651676
let config = ConfigFile::default(false, false, false, &cmd.config_path)?;
652677
let mut ordhook_config = config.get_ordhook_config();
653-
ordhook_config.network_thread_max = cmd.network_threads;
654-
678+
if let Some(network_threads) = cmd.network_threads {
679+
ordhook_config.network_thread_max = network_threads;
680+
}
681+
if let Some(network_threads) = cmd.network_threads {
682+
ordhook_config.network_thread_max = network_threads;
683+
}
684+
let blocks = cmd.get_blocks();
655685
let block_ingestion_processor =
656686
start_block_archiving_processor(&config, ctx, false, None);
657-
658687
download_and_pipeline_blocks(
659688
&config,
660-
cmd.start_block,
661-
cmd.end_block,
689+
blocks,
662690
ordhook_config.first_inscription_height,
663691
Some(&block_ingestion_processor),
664692
10_000,
@@ -669,15 +697,24 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
669697
RepairCommand::Inscriptions(cmd) => {
670698
let config = ConfigFile::default(false, false, false, &cmd.config_path)?;
671699
let mut ordhook_config = config.get_ordhook_config();
672-
ordhook_config.network_thread_max = cmd.network_threads;
673-
700+
if let Some(network_threads) = cmd.network_threads {
701+
ordhook_config.network_thread_max = network_threads;
702+
}
703+
let block_post_processor = match cmd.repair_observers {
704+
Some(true) => {
705+
let tx_replayer =
706+
start_observer_forwarding(&config.get_event_observer_config(), &ctx);
707+
Some(tx_replayer)
708+
}
709+
_ => None,
710+
};
711+
let blocks = cmd.get_blocks();
674712
let inscription_indexing_processor =
675-
start_inscription_indexing_processor(&config, ctx, None);
713+
start_inscription_indexing_processor(&config, ctx, block_post_processor);
676714

677715
download_and_pipeline_blocks(
678716
&config,
679-
cmd.start_block,
680-
cmd.end_block,
717+
blocks,
681718
ordhook_config.first_inscription_height,
682719
Some(&inscription_indexing_processor),
683720
10_000,
@@ -687,9 +724,18 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
687724
}
688725
RepairCommand::Transfers(cmd) => {
689726
let config = ConfigFile::default(false, false, false, &cmd.config_path)?;
727+
let block_post_processor = match cmd.repair_observers {
728+
Some(true) => {
729+
let tx_replayer =
730+
start_observer_forwarding(&config.get_event_observer_config(), &ctx);
731+
Some(tx_replayer)
732+
}
733+
_ => None,
734+
};
690735
let service = Service::new(config, ctx.clone());
736+
let blocks = cmd.get_blocks();
691737
service
692-
.replay_transfers(cmd.start_block, cmd.end_block, None)
738+
.replay_transfers(blocks, block_post_processor)
693739
.await?;
694740
}
695741
},

0 commit comments

Comments
 (0)