Skip to content

Commit 45661ab

Browse files
author
Ludo Galabru
committed
fix: indexing
1 parent e6db5b4 commit 45661ab

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::block::DigestingCommand;
22
use crate::config::Config;
33
use crate::node::Node;
44
use crate::scan::bitcoin::{
5-
retrieve_satoshi_point_using_bitcoin_rpc, scan_bitcoin_chain_with_predicate,
5+
scan_bitcoin_chain_with_predicate, retrieve_satoshi_point_using_local_storage,
66
};
77
use crate::scan::stacks::scan_stacks_chain_with_predicate;
88

@@ -19,6 +19,7 @@ use chainhook_event_observer::observer::{
1919
};
2020
use chainhook_event_observer::redb::ReadableTable;
2121
use chainhook_event_observer::utils::Context;
22+
use chainhook_types::{BlockIdentifier, TransactionIdentifier};
2223
use clap::{Parser, Subcommand};
2324
use ctrlc;
2425
use hiro_system_kit;
@@ -154,7 +155,7 @@ struct GetSatoshiCommand {
154155
/// Txid
155156
pub txid: String,
156157
/// Output index
157-
pub output_index: usize,
158+
pub block_height: u64,
158159
/// Target Devnet network
159160
#[clap(
160161
long = "devnet",
@@ -289,8 +290,9 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
289290
OrdinalsCommand::Satoshi(cmd) => {
290291
let config =
291292
Config::default(cmd.devnet, cmd.testnet, cmd.mainnet, &cmd.config_path)?;
292-
293-
retrieve_satoshi_point_using_bitcoin_rpc(&config, &cmd.txid, cmd.output_index)
293+
let txid = TransactionIdentifier { hash: cmd.txid.clone() };
294+
let block_id = BlockIdentifier { hash: "".into(), index: cmd.block_height };
295+
retrieve_satoshi_point_using_local_storage(&config, &block_id, &txid)
294296
.await?;
295297
}
296298
},

components/chainhook-cli/src/scan/bitcoin.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use chainhook_event_observer::observer::{
3131
use chainhook_event_observer::redb::ReadableTable;
3232
use chainhook_event_observer::utils::{file_append, send_request, Context};
3333
use chainhook_types::{
34-
BitcoinTransactionData, OrdinalInscriptionRevealData, OrdinalOperation, TransactionIdentifier,
34+
BitcoinTransactionData, OrdinalInscriptionRevealData, OrdinalOperation, TransactionIdentifier, BlockIdentifier,
3535
};
3636
use reqwest::Client as HttpClient;
3737
use rusqlite::{Connection, OpenFlags, Result, ToSql};
@@ -43,14 +43,15 @@ use std::time::Duration;
4343

4444
pub fn initialize_bitcoin_block_traversal_cache(path: &PathBuf) -> Connection {
4545
let conn = create_or_open_readwrite_db(path);
46-
conn.execute(
46+
if let Err(e) = conn.execute(
4747
"CREATE TABLE blocks (
4848
id INTEGER NOT NULL PRIMARY KEY,
4949
compacted_bytes TEXT NOT NULL
5050
)",
5151
[],
52-
)
53-
.unwrap();
52+
) {
53+
println!("Error: {}", e.to_string());
54+
}
5455
conn
5556
}
5657

@@ -189,12 +190,14 @@ pub fn write_compacted_block_to_index(
189190
storage_conn: &Connection,
190191
) {
191192
let serialized_compacted_block = compacted_block.to_hex_bytes();
192-
storage_conn
193-
.execute(
194-
"INSERT INTO blocks (id, compacted_bytes) VALUES (?1, ?2)",
195-
rusqlite::params![&block_id, &serialized_compacted_block],
196-
)
197-
.unwrap();
193+
194+
if let Err(e) = storage_conn
195+
.execute(
196+
"INSERT INTO blocks (id, compacted_bytes) VALUES (?1, ?2)",
197+
rusqlite::params![&block_id, &serialized_compacted_block],
198+
) {
199+
println!("Error: {}", e.to_string());
200+
}
198201
}
199202

200203
pub async fn scan_bitcoin_chain_with_predicate(
@@ -341,17 +344,16 @@ pub async fn scan_bitcoin_chain_with_predicate(
341344
let mut total_hits = vec![];
342345

343346
let (retrieve_ordinal_tx, retrieve_ordinal_rx) =
344-
channel::<std::option::Option<((BitcoinTransactionData, Vec<HookAction>))>>();
347+
channel::<std::option::Option<(BlockIdentifier, BitcoinTransactionData, Vec<HookAction>)>>();
345348
let (process_ordinal_tx, process_ordinal_rx) = channel();
346349
let (cache_block_tx, cache_block_rx) = channel();
347350

348351
let _config = config.clone();
349352
let handle_1 = hiro_system_kit::thread_named("Ordinal retrieval")
350353
.spawn(move || {
351-
while let Ok(Some((mut transaction, actions))) = retrieve_ordinal_rx.recv() {
352-
let txid = &transaction.transaction_identifier.hash[2..];
353-
println!("Retrieving satoshi point for {txid}");
354-
let f = retrieve_satoshi_point_using_bitcoin_rpc(&_config, &txid, 0);
354+
while let Ok(Some((block_identifier, mut transaction, actions))) = retrieve_ordinal_rx.recv() {
355+
println!("Retrieving satoshi point for {}", transaction.transaction_identifier.hash);
356+
let f = retrieve_satoshi_point_using_local_storage(&_config, &block_identifier, &transaction.transaction_identifier);
355357
let (block_number, block_offset) = match hiro_system_kit::nestable_block_on(f) {
356358
Ok(res) => res,
357359
Err(err) => {
@@ -461,7 +463,7 @@ pub async fn scan_bitcoin_chain_with_predicate(
461463
if is_scanning_inscriptions {
462464
pipeline_started = true;
463465
let _ = retrieve_ordinal_tx
464-
.send(Some((tx.clone(), vec![predicate_spec.action.clone()])));
466+
.send(Some((block.block_identifier.clone(), tx.clone(), vec![predicate_spec.action.clone()])));
465467
} else {
466468
hits.push(tx);
467469
}
@@ -579,16 +581,16 @@ pub async fn retrieve_block_hash(config: &Config, block_height: &u64) -> Result<
579581

580582
pub async fn retrieve_satoshi_point_using_local_storage(
581583
config: &Config,
582-
origin_block_height: &u64,
584+
block_identifier: &BlockIdentifier,
583585
transaction_identifier: &TransactionIdentifier,
584586
) -> Result<(u64, u64), String> {
585587
let path = config.get_bitcoin_block_traversal_db_path();
586588
let storage_conn = open_existing_readonly_db(&path);
587589

588590
let mut ordinal_offset = 0;
589-
let mut ordinal_block_number = *origin_block_height as u32;
591+
let mut ordinal_block_number = block_identifier.index as u32;
590592
let txid = {
591-
let bytes = hex::decode(&transaction_identifier.hash).unwrap();
593+
let bytes = hex::decode(&transaction_identifier.hash[2..]).unwrap();
592594
[bytes[0], bytes[1], bytes[2], bytes[3]]
593595
};
594596
let mut tx_cursor = (txid, 0);

0 commit comments

Comments
 (0)