Skip to content

Commit

Permalink
fix: command for db patch
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed Apr 4, 2023
1 parent a7d8153 commit 27f6838
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
41 changes: 40 additions & 1 deletion components/chainhook-cli/src/cli/mod.rs
Expand Up @@ -16,7 +16,7 @@ use chainhook_event_observer::hord::db::{
delete_data_in_hord_db, fetch_and_cache_blocks_in_hord_db,
find_inscriptions_at_wached_outpoint, find_latest_compacted_block_known, initialize_hord_db,
open_readonly_hord_db_conn, open_readwrite_hord_db_conn,
retrieve_satoshi_point_using_local_storage,
retrieve_satoshi_point_using_local_storage, find_all_inscriptions, find_compacted_block_at_block_height, patch_inscription_number,
};
use chainhook_event_observer::observer::BitcoinConfig;
use chainhook_event_observer::utils::Context;
Expand Down Expand Up @@ -198,6 +198,9 @@ enum DbCommand {
/// Rebuild inscriptions entries for a given block
#[clap(name = "drop", bin_name = "drop")]
Drop(DropHordDbCommand),
/// Patch DB
#[clap(name = "patch", bin_name = "patch")]
Patch(PatchHordDbCommand),
}

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

#[derive(Parser, PartialEq, Clone, Debug)]
struct PatchHordDbCommand {
/// Load config file path
#[clap(long = "config-path")]
pub config_path: Option<String>,
}

pub fn main() {
let logger = hiro_system_kit::log::setup_logger();
let _guard = hiro_system_kit::log::setup_global_logger(logger.clone());
Expand Down Expand Up @@ -618,6 +628,35 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
cmd.end_block - cmd.start_block + 1
);
}
DbCommand::Patch(cmd) => {
let config = Config::default(false, false, false, &cmd.config_path)?;
let rw_hord_db_conn =
open_readwrite_hord_db_conn(&config.expected_cache_path(), &ctx)?;

let inscriptions_per_blocks = find_all_inscriptions(&rw_hord_db_conn);
let mut inscription_number = 0;
for (block_height, inscriptions) in inscriptions_per_blocks.iter() {
let block = match find_compacted_block_at_block_height(*block_height as u32, &rw_hord_db_conn) {
Some(block) => block,
None => continue,
};

for (txid, _) in inscriptions.iter() {
for (txid_n, _, _) in block.0.1.iter() {
if txid.hash[2..10].eq(&hex::encode(txid_n)) {
let inscription_id = format!("{}i0", &txid.hash[2..]);
patch_inscription_number(&inscription_id, inscription_number, &rw_hord_db_conn, &ctx);
info!(
ctx.expect_logger(),
"Patch inscription_number: {} {}",
inscription_id, inscription_number
);
}
}
inscription_number += 1;
}
}
}
},
}
Ok(())
Expand Down
19 changes: 16 additions & 3 deletions components/chainhook-event-observer/src/hord/db/mod.rs
Expand Up @@ -160,7 +160,7 @@ fn open_existing_readonly_db(path: &PathBuf, ctx: &Context) -> Connection {
#[derive(Debug, Serialize, Deserialize)]
// pub struct CompactedBlock(Vec<(Vec<(u32, u16, u64)>, Vec<u64>)>);
pub struct CompactedBlock(
(
pub (
([u8; 4], u64),
Vec<([u8; 4], Vec<([u8; 4], u32, u16, u64)>, Vec<u64>)>,
),
Expand Down Expand Up @@ -312,6 +312,20 @@ pub fn update_transfered_inscription(
}
}

pub fn patch_inscription_number(
inscription_id: &str,
inscription_number: u64,
hord_db_conn: &Connection,
ctx: &Context,
) {
if let Err(e) = hord_db_conn.execute(
"UPDATE inscriptions SET inscription_number = ? WHERE inscription_id = ?",
rusqlite::params![&inscription_number, &inscription_id],
) {
ctx.try_log(|logger| slog::error!(logger, "{}", e.to_string()));
}
}

pub fn find_latest_inscription_block_height(
hord_db_conn: &Connection,
_ctx: &Context,
Expand Down Expand Up @@ -412,8 +426,7 @@ pub fn find_inscriptions_at_wached_outpoint(
.prepare("SELECT inscription_id, inscription_number, ordinal_number, offset FROM inscriptions WHERE outpoint_to_watch = ? ORDER BY offset ASC")
.map_err(|e| format!("unable to query inscriptions table: {}", e.to_string()))?;
let mut results = vec![];
let mut rows = stmt
.query(args)
let mut rows = stmt.query(args)
.map_err(|e| format!("unable to query inscriptions table: {}", e.to_string()))?;
while let Ok(Some(row)) = rows.next() {
let inscription_id: String = row.get(0).unwrap();
Expand Down

0 comments on commit 27f6838

Please sign in to comment.