Skip to content

Commit

Permalink
feat: scan inscription revealed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru committed Mar 28, 2023
1 parent 3ff472d commit 644d515
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 46 deletions.
87 changes: 58 additions & 29 deletions components/chainhook-cli/src/scan/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use chainhook_event_observer::bitcoincore_rpc::RpcApi;
use chainhook_event_observer::bitcoincore_rpc::{Auth, Client};
use chainhook_event_observer::chainhooks::bitcoin::{
evaluate_bitcoin_chainhooks_on_chain_event, handle_bitcoin_hook_action,
BitcoinChainhookOccurrence,
BitcoinChainhookOccurrence, BitcoinTriggerChainhook,
};
use chainhook_event_observer::chainhooks::types::{
BitcoinChainhookFullSpecification, BitcoinPredicateType, Protocols,
Expand Down Expand Up @@ -133,16 +133,40 @@ pub async fn scan_bitcoin_chain_with_predicate(
let mut blocks_scanned = 0;
let mut actions_triggered = 0;

let event_observer_config = config.get_event_observer_config();
let bitcoin_config = event_observer_config.get_bitcoin_config();

if is_predicate_evaluating_ordinals {
for (_inscription_id, (_inscription_number, _ordinal_number, block_number)) in
inscriptions_cache.into_iter()
{
for (cursor, _inscriptions) in inscriptions_cache.into_iter() {
// Only consider inscriptions in the interval specified
if block_number < start_block || block_number > end_block {
if cursor < start_block || cursor > end_block {
continue;
}

// Retrieve the transaction (including witness data) to get the inscription data
blocks_scanned += 1;

let block_hash = retrieve_block_hash_with_retry(&cursor, &bitcoin_config, ctx).await?;
let block_breakdown =
retrieve_full_block_breakdown_with_retry(&block_hash, &bitcoin_config, ctx).await?;
let block = indexer::bitcoin::standardize_bitcoin_block(
block_breakdown,
&event_observer_config.bitcoin_network,
ctx,
)?;

let chain_event =
BitcoinChainEvent::ChainUpdatedWithBlocks(BitcoinChainUpdatedWithBlocksData {
new_blocks: vec![block],
confirmed_blocks: vec![],
});

let hits = evaluate_bitcoin_chainhooks_on_chain_event(
&chain_event,
vec![&predicate_spec],
ctx,
);

actions_triggered += execute_predicates_action(hits, &ctx).await;
}
} else {
let use_scan_to_seed_hord_db = true;
Expand All @@ -151,9 +175,6 @@ pub async fn scan_bitcoin_chain_with_predicate(
// Start ingestion pipeline
}

let event_observer_config = config.get_event_observer_config();
let bitcoin_config = event_observer_config.get_bitcoin_config();

for cursor in start_block..=end_block {
blocks_scanned += 1;
let block_hash = retrieve_block_hash_with_retry(&cursor, &bitcoin_config, ctx).await?;
Expand Down Expand Up @@ -185,26 +206,8 @@ pub async fn scan_bitcoin_chain_with_predicate(
vec![&predicate_spec],
ctx,
);
for hit in hits.into_iter() {
let proofs = HashMap::new();
match handle_bitcoin_hook_action(hit, &proofs) {
Err(e) => {
error!(ctx.expect_logger(), "unable to handle action {}", e);
}
Ok(action) => {
actions_triggered += 1;
match action {
BitcoinChainhookOccurrence::Http(request) => {
send_request(request, &ctx).await
}
BitcoinChainhookOccurrence::File(path, bytes) => {
file_append(path, bytes, &ctx)
}
BitcoinChainhookOccurrence::Data(_payload) => unreachable!(),
}
}
}
}

actions_triggered += execute_predicates_action(hits, &ctx).await;
}
}
info!(
Expand All @@ -214,3 +217,29 @@ pub async fn scan_bitcoin_chain_with_predicate(

Ok(())
}

pub async fn execute_predicates_action<'a>(
hits: Vec<BitcoinTriggerChainhook<'a>>,
ctx: &Context,
) -> u32 {
let mut actions_triggered = 0;

for hit in hits.into_iter() {
let proofs = HashMap::new();
match handle_bitcoin_hook_action(hit, &proofs) {
Err(e) => {
error!(ctx.expect_logger(), "unable to handle action {}", e);
}
Ok(action) => {
actions_triggered += 1;
match action {
BitcoinChainhookOccurrence::Http(request) => send_request(request, &ctx).await,
BitcoinChainhookOccurrence::File(path, bytes) => file_append(path, bytes, &ctx),
BitcoinChainhookOccurrence::Data(_payload) => unreachable!(),
}
}
}
}

actions_triggered
}
17 changes: 8 additions & 9 deletions components/chainhook-event-observer/src/hord/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,22 +350,22 @@ pub fn find_inscription_with_ordinal_number(
return None;
}

pub fn find_all_inscriptions(hord_db_conn: &Connection) -> BTreeMap<String, (u64, u64, u64)> {
pub fn find_all_inscriptions(hord_db_conn: &Connection) -> BTreeMap<u64, Vec<(String, u64, u64)>> {
let args: &[&dyn ToSql] = &[];
let mut stmt = hord_db_conn
.prepare("SELECT inscription_id, inscription_number, ordinal_number, block_number FROM inscriptions ORDER BY inscription_number ASC")
.prepare("SELECT inscription_id, inscription_number, ordinal_number, block_height FROM inscriptions ORDER BY inscription_number ASC")
.unwrap();
let mut results = BTreeMap::new();
let mut results: BTreeMap<u64, Vec<(String, u64, u64)>> = BTreeMap::new();
let mut rows = stmt.query(args).unwrap();
while let Ok(Some(row)) = rows.next() {
let inscription_id: String = row.get(0).unwrap();
let inscription_number: u64 = row.get(1).unwrap();
let ordinal_number: u64 = row.get(2).unwrap();
let block_number: u64 = row.get(3).unwrap();
results.insert(
inscription_id,
(inscription_number, ordinal_number, block_number),
);
let block_height: u64 = row.get(3).unwrap();
results
.entry(block_height)
.and_modify(|v| v.push((inscription_id.clone(), inscription_number, ordinal_number)))
.or_insert(vec![(inscription_id, inscription_number, ordinal_number)]);
}
return results;
}
Expand Down Expand Up @@ -565,7 +565,6 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
let (block_data_tx, block_data_rx) = crossbeam_channel::bounded(64);
let compress_block_data_pool = ThreadPool::new(16);
let (block_compressed_tx, block_compressed_rx) = crossbeam_channel::bounded(32);
let first_inscription_block_height = 767430;

for block_cursor in start_block..=end_block {
let block_height = block_cursor.clone();
Expand Down
9 changes: 1 addition & 8 deletions components/chainhook-event-observer/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::chainhooks::types::{
};

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

ctx.try_log(|logger| {
slog::info!(
logger,
"Initializing ordinals index in dir `{}`",
config.cache_path
)
});
ctx.try_log(|logger| slog::info!(logger, "Local cache path `{}`", config.cache_path));

let indexer_config = IndexerConfig {
stacks_node_rpc_url: config.stacks_node_rpc_url.clone(),
Expand Down

0 comments on commit 644d515

Please sign in to comment.