mirror of
https://github.com/alexgo-io/bitcoin-indexer.git
synced 2026-06-14 08:29:31 +08:00
fix: command for db patch
This commit is contained in:
@@ -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;
|
||||
@@ -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)]
|
||||
@@ -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());
|
||||
@@ -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(())
|
||||
|
||||
@@ -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>)>,
|
||||
),
|
||||
@@ -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,
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user