|
1 | 1 | use crate::chainhooks::bitcoin::{
|
2 | 2 | evaluate_bitcoin_chainhooks_on_chain_event, handle_bitcoin_hook_action,
|
3 |
| - BitcoinChainhookOccurrence, BitcoinChainhookOccurrencePayload, |
| 3 | + BitcoinChainhookOccurrence, BitcoinChainhookOccurrencePayload, BitcoinTriggerChainhook, |
4 | 4 | };
|
5 | 5 | use crate::chainhooks::stacks::{
|
6 | 6 | evaluate_stacks_chainhooks_on_chain_event, handle_stacks_hook_action,
|
@@ -446,6 +446,51 @@ pub fn apply_bitcoin_block() {}
|
446 | 446 |
|
447 | 447 | pub fn rollback_bitcoin_block() {}
|
448 | 448 |
|
| 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 | + |
449 | 494 | pub async fn start_observer_commands_handler(
|
450 | 495 | config: EventObserverConfig,
|
451 | 496 | chainhook_store: Arc<RwLock<ChainhookStore>>,
|
@@ -753,49 +798,7 @@ pub async fn start_observer_commands_handler(
|
753 | 798 | }
|
754 | 799 | }
|
755 | 800 |
|
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); |
799 | 802 | ctx.try_log(|logger| {
|
800 | 803 | slog::info!(
|
801 | 804 | logger,
|
|
0 commit comments