Skip to content

Commit 27f6838

Browse files
author
Ludo Galabru
committed
fix: command for db patch
1 parent a7d8153 commit 27f6838

File tree

2 files changed

+56
-4
lines changed
  • components
    • chainhook-cli/src/cli
    • chainhook-event-observer/src/hord/db

2 files changed

+56
-4
lines changed

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

+40-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use chainhook_event_observer::hord::db::{
1616
delete_data_in_hord_db, fetch_and_cache_blocks_in_hord_db,
1717
find_inscriptions_at_wached_outpoint, find_latest_compacted_block_known, initialize_hord_db,
1818
open_readonly_hord_db_conn, open_readwrite_hord_db_conn,
19-
retrieve_satoshi_point_using_local_storage,
19+
retrieve_satoshi_point_using_local_storage, find_all_inscriptions, find_compacted_block_at_block_height, patch_inscription_number,
2020
};
2121
use chainhook_event_observer::observer::BitcoinConfig;
2222
use chainhook_event_observer::utils::Context;
@@ -198,6 +198,9 @@ enum DbCommand {
198198
/// Rebuild inscriptions entries for a given block
199199
#[clap(name = "drop", bin_name = "drop")]
200200
Drop(DropHordDbCommand),
201+
/// Patch DB
202+
#[clap(name = "patch", bin_name = "patch")]
203+
Patch(PatchHordDbCommand),
201204
}
202205

203206
#[derive(Subcommand, PartialEq, Clone, Debug)]
@@ -298,6 +301,13 @@ struct DropHordDbCommand {
298301
pub config_path: Option<String>,
299302
}
300303

304+
#[derive(Parser, PartialEq, Clone, Debug)]
305+
struct PatchHordDbCommand {
306+
/// Load config file path
307+
#[clap(long = "config-path")]
308+
pub config_path: Option<String>,
309+
}
310+
301311
pub fn main() {
302312
let logger = hiro_system_kit::log::setup_logger();
303313
let _guard = hiro_system_kit::log::setup_global_logger(logger.clone());
@@ -618,6 +628,35 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
618628
cmd.end_block - cmd.start_block + 1
619629
);
620630
}
631+
DbCommand::Patch(cmd) => {
632+
let config = Config::default(false, false, false, &cmd.config_path)?;
633+
let rw_hord_db_conn =
634+
open_readwrite_hord_db_conn(&config.expected_cache_path(), &ctx)?;
635+
636+
let inscriptions_per_blocks = find_all_inscriptions(&rw_hord_db_conn);
637+
let mut inscription_number = 0;
638+
for (block_height, inscriptions) in inscriptions_per_blocks.iter() {
639+
let block = match find_compacted_block_at_block_height(*block_height as u32, &rw_hord_db_conn) {
640+
Some(block) => block,
641+
None => continue,
642+
};
643+
644+
for (txid, _) in inscriptions.iter() {
645+
for (txid_n, _, _) in block.0.1.iter() {
646+
if txid.hash[2..10].eq(&hex::encode(txid_n)) {
647+
let inscription_id = format!("{}i0", &txid.hash[2..]);
648+
patch_inscription_number(&inscription_id, inscription_number, &rw_hord_db_conn, &ctx);
649+
info!(
650+
ctx.expect_logger(),
651+
"Patch inscription_number: {} {}",
652+
inscription_id, inscription_number
653+
);
654+
}
655+
}
656+
inscription_number += 1;
657+
}
658+
}
659+
}
621660
},
622661
}
623662
Ok(())

components/chainhook-event-observer/src/hord/db/mod.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ fn open_existing_readonly_db(path: &PathBuf, ctx: &Context) -> Connection {
160160
#[derive(Debug, Serialize, Deserialize)]
161161
// pub struct CompactedBlock(Vec<(Vec<(u32, u16, u64)>, Vec<u64>)>);
162162
pub struct CompactedBlock(
163-
(
163+
pub (
164164
([u8; 4], u64),
165165
Vec<([u8; 4], Vec<([u8; 4], u32, u16, u64)>, Vec<u64>)>,
166166
),
@@ -312,6 +312,20 @@ pub fn update_transfered_inscription(
312312
}
313313
}
314314

315+
pub fn patch_inscription_number(
316+
inscription_id: &str,
317+
inscription_number: u64,
318+
hord_db_conn: &Connection,
319+
ctx: &Context,
320+
) {
321+
if let Err(e) = hord_db_conn.execute(
322+
"UPDATE inscriptions SET inscription_number = ? WHERE inscription_id = ?",
323+
rusqlite::params![&inscription_number, &inscription_id],
324+
) {
325+
ctx.try_log(|logger| slog::error!(logger, "{}", e.to_string()));
326+
}
327+
}
328+
315329
pub fn find_latest_inscription_block_height(
316330
hord_db_conn: &Connection,
317331
_ctx: &Context,
@@ -412,8 +426,7 @@ pub fn find_inscriptions_at_wached_outpoint(
412426
.prepare("SELECT inscription_id, inscription_number, ordinal_number, offset FROM inscriptions WHERE outpoint_to_watch = ? ORDER BY offset ASC")
413427
.map_err(|e| format!("unable to query inscriptions table: {}", e.to_string()))?;
414428
let mut results = vec![];
415-
let mut rows = stmt
416-
.query(args)
429+
let mut rows = stmt.query(args)
417430
.map_err(|e| format!("unable to query inscriptions table: {}", e.to_string()))?;
418431
while let Ok(Some(row)) = rows.next() {
419432
let inscription_id: String = row.get(0).unwrap();

0 commit comments

Comments
 (0)