Skip to content

Commit 6574008

Browse files
author
Ludo Galabru
committed
fix: include proof on scan commands
1 parent bf384e9 commit 6574008

File tree

2 files changed

+54
-57
lines changed
  • components

2 files changed

+54
-57
lines changed

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

+7-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use chainhook_event_observer::indexer;
2020
use chainhook_event_observer::indexer::bitcoin::{
2121
retrieve_block_hash_with_retry, retrieve_full_block_breakdown_with_retry,
2222
};
23+
use chainhook_event_observer::observer::{gather_proofs, EventObserverConfig};
2324
use chainhook_event_observer::utils::{file_append, send_request, Context};
2425
use chainhook_types::{BitcoinChainEvent, BitcoinChainUpdatedWithBlocksData};
2526
use std::collections::{BTreeMap, HashMap};
@@ -189,7 +190,8 @@ pub async fn scan_bitcoin_chain_with_predicate(
189190
ctx,
190191
);
191192

192-
actions_triggered += execute_predicates_action(hits, &ctx).await;
193+
actions_triggered +=
194+
execute_predicates_action(hits, &event_observer_config, &ctx).await;
193195
}
194196
} else {
195197
let use_scan_to_seed_hord_db = true;
@@ -209,15 +211,6 @@ pub async fn scan_bitcoin_chain_with_predicate(
209211
ctx,
210212
)?;
211213

212-
if use_scan_to_seed_hord_db {
213-
// Inject new block in ingestion pipeline
214-
//
215-
// let _ = cache_block_tx.send(Some((
216-
// block_breakdown.height as u32,
217-
// CompactedBlock::from_full_block(&block_breakdown),
218-
// )));
219-
}
220-
221214
let chain_event =
222215
BitcoinChainEvent::ChainUpdatedWithBlocks(BitcoinChainUpdatedWithBlocksData {
223216
new_blocks: vec![block],
@@ -230,7 +223,8 @@ pub async fn scan_bitcoin_chain_with_predicate(
230223
ctx,
231224
);
232225

233-
actions_triggered += execute_predicates_action(hits, &ctx).await;
226+
actions_triggered +=
227+
execute_predicates_action(hits, &event_observer_config, &ctx).await;
234228
}
235229
}
236230
info!(
@@ -243,12 +237,12 @@ pub async fn scan_bitcoin_chain_with_predicate(
243237

244238
pub async fn execute_predicates_action<'a>(
245239
hits: Vec<BitcoinTriggerChainhook<'a>>,
240+
config: &EventObserverConfig,
246241
ctx: &Context,
247242
) -> u32 {
248243
let mut actions_triggered = 0;
249-
244+
let proofs = gather_proofs(&hits, &config, &ctx);
250245
for hit in hits.into_iter() {
251-
let proofs = HashMap::new();
252246
match handle_bitcoin_hook_action(hit, &proofs) {
253247
Err(e) => {
254248
error!(ctx.expect_logger(), "unable to handle action {}", e);

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

+47-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::chainhooks::bitcoin::{
22
evaluate_bitcoin_chainhooks_on_chain_event, handle_bitcoin_hook_action,
3-
BitcoinChainhookOccurrence, BitcoinChainhookOccurrencePayload,
3+
BitcoinChainhookOccurrence, BitcoinChainhookOccurrencePayload, BitcoinTriggerChainhook,
44
};
55
use crate::chainhooks::stacks::{
66
evaluate_stacks_chainhooks_on_chain_event, handle_stacks_hook_action,
@@ -446,6 +446,51 @@ pub fn apply_bitcoin_block() {}
446446

447447
pub fn rollback_bitcoin_block() {}
448448

449+
pub fn gather_proofs<'a>(
450+
chainhooks_to_trigger: &Vec<BitcoinTriggerChainhook<'a>>,
451+
config: &EventObserverConfig,
452+
ctx: &Context,
453+
) -> HashMap<&'a TransactionIdentifier, String> {
454+
let bitcoin_client_rpc = Client::new(
455+
&config.bitcoin_node_rpc_url,
456+
Auth::UserPass(
457+
config.bitcoin_node_username.to_string(),
458+
config.bitcoin_node_password.to_string(),
459+
),
460+
)
461+
.expect("unable to build http client");
462+
463+
let mut proofs = HashMap::new();
464+
for hook_to_trigger in chainhooks_to_trigger.iter() {
465+
for (transactions, block) in hook_to_trigger.apply.iter() {
466+
for transaction in transactions.iter() {
467+
if !proofs.contains_key(&transaction.transaction_identifier) {
468+
ctx.try_log(|logger| {
469+
slog::info!(
470+
logger,
471+
"collecting proof for transaction {}",
472+
transaction.transaction_identifier.hash
473+
)
474+
});
475+
match get_bitcoin_proof(
476+
&bitcoin_client_rpc,
477+
&transaction.transaction_identifier,
478+
&block.block_identifier,
479+
) {
480+
Ok(proof) => {
481+
proofs.insert(&transaction.transaction_identifier, proof);
482+
}
483+
Err(e) => {
484+
ctx.try_log(|logger| slog::error!(logger, "{e}"));
485+
}
486+
}
487+
}
488+
}
489+
}
490+
}
491+
proofs
492+
}
493+
449494
pub async fn start_observer_commands_handler(
450495
config: EventObserverConfig,
451496
chainhook_store: Arc<RwLock<ChainhookStore>>,
@@ -753,49 +798,7 @@ pub async fn start_observer_commands_handler(
753798
}
754799
}
755800

756-
let bitcoin_client_rpc = Client::new(
757-
&config.bitcoin_node_rpc_url,
758-
Auth::UserPass(
759-
config.bitcoin_node_username.to_string(),
760-
config.bitcoin_node_password.to_string(),
761-
),
762-
)
763-
.expect("unable to build http client");
764-
765-
let mut proofs = HashMap::new();
766-
for hook_to_trigger in chainhooks_to_trigger.iter() {
767-
for (transactions, block) in hook_to_trigger.apply.iter() {
768-
for transaction in transactions.iter() {
769-
if !proofs.contains_key(&transaction.transaction_identifier)
770-
{
771-
ctx.try_log(|logger| {
772-
slog::info!(
773-
logger,
774-
"collecting proof for transaction {}",
775-
transaction.transaction_identifier.hash
776-
)
777-
});
778-
match get_bitcoin_proof(
779-
&bitcoin_client_rpc,
780-
&transaction.transaction_identifier,
781-
&block.block_identifier,
782-
) {
783-
Ok(proof) => {
784-
proofs.insert(
785-
&transaction.transaction_identifier,
786-
proof,
787-
);
788-
}
789-
Err(e) => {
790-
ctx.try_log(|logger| {
791-
slog::error!(logger, "{e}")
792-
});
793-
}
794-
}
795-
}
796-
}
797-
}
798-
}
801+
let proofs = gather_proofs(&chainhooks_to_trigger, &config, &ctx);
799802
ctx.try_log(|logger| {
800803
slog::info!(
801804
logger,

0 commit comments

Comments
 (0)