Skip to content

Commit 644d515

Browse files
author
Ludo Galabru
committed
feat: scan inscription revealed
1 parent 3ff472d commit 644d515

File tree

3 files changed

+67
-46
lines changed

3 files changed

+67
-46
lines changed

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

+58-29
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use chainhook_event_observer::bitcoincore_rpc::RpcApi;
33
use chainhook_event_observer::bitcoincore_rpc::{Auth, Client};
44
use chainhook_event_observer::chainhooks::bitcoin::{
55
evaluate_bitcoin_chainhooks_on_chain_event, handle_bitcoin_hook_action,
6-
BitcoinChainhookOccurrence,
6+
BitcoinChainhookOccurrence, BitcoinTriggerChainhook,
77
};
88
use chainhook_event_observer::chainhooks::types::{
99
BitcoinChainhookFullSpecification, BitcoinPredicateType, Protocols,
@@ -133,16 +133,40 @@ pub async fn scan_bitcoin_chain_with_predicate(
133133
let mut blocks_scanned = 0;
134134
let mut actions_triggered = 0;
135135

136+
let event_observer_config = config.get_event_observer_config();
137+
let bitcoin_config = event_observer_config.get_bitcoin_config();
138+
136139
if is_predicate_evaluating_ordinals {
137-
for (_inscription_id, (_inscription_number, _ordinal_number, block_number)) in
138-
inscriptions_cache.into_iter()
139-
{
140+
for (cursor, _inscriptions) in inscriptions_cache.into_iter() {
140141
// Only consider inscriptions in the interval specified
141-
if block_number < start_block || block_number > end_block {
142+
if cursor < start_block || cursor > end_block {
142143
continue;
143144
}
144145

145-
// Retrieve the transaction (including witness data) to get the inscription data
146+
blocks_scanned += 1;
147+
148+
let block_hash = retrieve_block_hash_with_retry(&cursor, &bitcoin_config, ctx).await?;
149+
let block_breakdown =
150+
retrieve_full_block_breakdown_with_retry(&block_hash, &bitcoin_config, ctx).await?;
151+
let block = indexer::bitcoin::standardize_bitcoin_block(
152+
block_breakdown,
153+
&event_observer_config.bitcoin_network,
154+
ctx,
155+
)?;
156+
157+
let chain_event =
158+
BitcoinChainEvent::ChainUpdatedWithBlocks(BitcoinChainUpdatedWithBlocksData {
159+
new_blocks: vec![block],
160+
confirmed_blocks: vec![],
161+
});
162+
163+
let hits = evaluate_bitcoin_chainhooks_on_chain_event(
164+
&chain_event,
165+
vec![&predicate_spec],
166+
ctx,
167+
);
168+
169+
actions_triggered += execute_predicates_action(hits, &ctx).await;
146170
}
147171
} else {
148172
let use_scan_to_seed_hord_db = true;
@@ -151,9 +175,6 @@ pub async fn scan_bitcoin_chain_with_predicate(
151175
// Start ingestion pipeline
152176
}
153177

154-
let event_observer_config = config.get_event_observer_config();
155-
let bitcoin_config = event_observer_config.get_bitcoin_config();
156-
157178
for cursor in start_block..=end_block {
158179
blocks_scanned += 1;
159180
let block_hash = retrieve_block_hash_with_retry(&cursor, &bitcoin_config, ctx).await?;
@@ -185,26 +206,8 @@ pub async fn scan_bitcoin_chain_with_predicate(
185206
vec![&predicate_spec],
186207
ctx,
187208
);
188-
for hit in hits.into_iter() {
189-
let proofs = HashMap::new();
190-
match handle_bitcoin_hook_action(hit, &proofs) {
191-
Err(e) => {
192-
error!(ctx.expect_logger(), "unable to handle action {}", e);
193-
}
194-
Ok(action) => {
195-
actions_triggered += 1;
196-
match action {
197-
BitcoinChainhookOccurrence::Http(request) => {
198-
send_request(request, &ctx).await
199-
}
200-
BitcoinChainhookOccurrence::File(path, bytes) => {
201-
file_append(path, bytes, &ctx)
202-
}
203-
BitcoinChainhookOccurrence::Data(_payload) => unreachable!(),
204-
}
205-
}
206-
}
207-
}
209+
210+
actions_triggered += execute_predicates_action(hits, &ctx).await;
208211
}
209212
}
210213
info!(
@@ -214,3 +217,29 @@ pub async fn scan_bitcoin_chain_with_predicate(
214217

215218
Ok(())
216219
}
220+
221+
pub async fn execute_predicates_action<'a>(
222+
hits: Vec<BitcoinTriggerChainhook<'a>>,
223+
ctx: &Context,
224+
) -> u32 {
225+
let mut actions_triggered = 0;
226+
227+
for hit in hits.into_iter() {
228+
let proofs = HashMap::new();
229+
match handle_bitcoin_hook_action(hit, &proofs) {
230+
Err(e) => {
231+
error!(ctx.expect_logger(), "unable to handle action {}", e);
232+
}
233+
Ok(action) => {
234+
actions_triggered += 1;
235+
match action {
236+
BitcoinChainhookOccurrence::Http(request) => send_request(request, &ctx).await,
237+
BitcoinChainhookOccurrence::File(path, bytes) => file_append(path, bytes, &ctx),
238+
BitcoinChainhookOccurrence::Data(_payload) => unreachable!(),
239+
}
240+
}
241+
}
242+
}
243+
244+
actions_triggered
245+
}

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -350,22 +350,22 @@ pub fn find_inscription_with_ordinal_number(
350350
return None;
351351
}
352352

353-
pub fn find_all_inscriptions(hord_db_conn: &Connection) -> BTreeMap<String, (u64, u64, u64)> {
353+
pub fn find_all_inscriptions(hord_db_conn: &Connection) -> BTreeMap<u64, Vec<(String, u64, u64)>> {
354354
let args: &[&dyn ToSql] = &[];
355355
let mut stmt = hord_db_conn
356-
.prepare("SELECT inscription_id, inscription_number, ordinal_number, block_number FROM inscriptions ORDER BY inscription_number ASC")
356+
.prepare("SELECT inscription_id, inscription_number, ordinal_number, block_height FROM inscriptions ORDER BY inscription_number ASC")
357357
.unwrap();
358-
let mut results = BTreeMap::new();
358+
let mut results: BTreeMap<u64, Vec<(String, u64, u64)>> = BTreeMap::new();
359359
let mut rows = stmt.query(args).unwrap();
360360
while let Ok(Some(row)) = rows.next() {
361361
let inscription_id: String = row.get(0).unwrap();
362362
let inscription_number: u64 = row.get(1).unwrap();
363363
let ordinal_number: u64 = row.get(2).unwrap();
364-
let block_number: u64 = row.get(3).unwrap();
365-
results.insert(
366-
inscription_id,
367-
(inscription_number, ordinal_number, block_number),
368-
);
364+
let block_height: u64 = row.get(3).unwrap();
365+
results
366+
.entry(block_height)
367+
.and_modify(|v| v.push((inscription_id.clone(), inscription_number, ordinal_number)))
368+
.or_insert(vec![(inscription_id, inscription_number, ordinal_number)]);
369369
}
370370
return results;
371371
}
@@ -565,7 +565,6 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
565565
let (block_data_tx, block_data_rx) = crossbeam_channel::bounded(64);
566566
let compress_block_data_pool = ThreadPool::new(16);
567567
let (block_compressed_tx, block_compressed_rx) = crossbeam_channel::bounded(32);
568-
let first_inscription_block_height = 767430;
569568

570569
for block_cursor in start_block..=end_block {
571570
let block_height = block_cursor.clone();

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

+1-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::chainhooks::types::{
1111
};
1212

1313
use crate::hord::db::open_readwrite_hord_db_conn;
14-
use crate::hord::ord::{indexing::updater::OrdinalIndexUpdater, initialize_ordinal_index};
1514
use crate::hord::{
1615
revert_hord_db_with_augmented_bitcoin_block, update_hord_db_and_augment_bitcoin_block,
1716
};
@@ -258,13 +257,7 @@ pub async fn start_event_observer(
258257
// let ordinal_index = if cfg!(feature = "ordinals") {
259258
// Start indexer with a receiver in background thread
260259

261-
ctx.try_log(|logger| {
262-
slog::info!(
263-
logger,
264-
"Initializing ordinals index in dir `{}`",
265-
config.cache_path
266-
)
267-
});
260+
ctx.try_log(|logger| slog::info!(logger, "Local cache path `{}`", config.cache_path));
268261

269262
let indexer_config = IndexerConfig {
270263
stacks_node_rpc_url: config.stacks_node_rpc_url.clone(),

0 commit comments

Comments
 (0)